-
Notifications
You must be signed in to change notification settings - Fork 4.7k
Add Server Side Render component. #5602
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 1 commit
cd58776
b0ede76
340136e
514dd4f
a90f40d
0c127e1
b0b977a
395a168
1d05c2d
c40dd25
f323651
c4abc69
6557877
228c756
b48ef81
e39332d
46506a9
ce1f7c6
e0fec83
17e404a
39b867a
4c3129b
3c60d4b
a10bfac
a166324
f0f4a77
6d1ee65
2bbbeb7
6f7d62e
67af6b7
007fb1d
1c9ef56
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
…omponent.
- Loading branch information
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| /** | ||
| * WordPress dependencies | ||
| */ | ||
| import { Component, compose, renderToString } from '@wordpress/element'; | ||
|
|
||
| export class ServerSideRender extends Component { | ||
| render() { | ||
| return ( | ||
| <div>Here will be SSR!</div> | ||
| ); | ||
| } | ||
| } | ||
|
|
||
| export default ServerSideRender; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,130 @@ | ||
| <?php | ||
| /** | ||
| * Blocks Renderer REST API: WP_REST_Blocks_Renderer_Controller class | ||
| * | ||
| * @package gutenberg | ||
| * @since ? | ||
| */ | ||
|
|
||
| /** | ||
| * Controller which provides REST endpoint for rendering blocks. | ||
| * | ||
| * @since ? | ||
| * | ||
| * @see WP_REST_Controller | ||
| */ | ||
| class WP_REST_Blocks_Renderer_Controller extends WP_REST_Controller { | ||
|
|
||
| /** | ||
| * Constructs the controller. | ||
| * | ||
| * @access public | ||
| */ | ||
| public function __construct() { | ||
|
|
||
| // @codingStandardsIgnoreLine - PHPCS mistakes $this->namespace for the namespace keyword. | ||
| $this->namespace = 'gutenberg/v1'; | ||
| $this->rest_base = 'blocks-renderer'; | ||
| } | ||
|
|
||
| /** | ||
| * Registers the necessary REST API routes. | ||
| * | ||
| * @access public | ||
| */ | ||
| public function register_routes() { | ||
|
|
||
| register_rest_route( $this->namespace, '/' . $this->rest_base . '/(?P<name>[\w-]+\/[\w-]+)', array( | ||
|
||
| 'args' => array( | ||
| 'name' => array( | ||
| 'description' => __( 'Unique registered name for the block.' ), | ||
| 'type' => 'string', | ||
| ), | ||
| ), | ||
| array( | ||
| 'methods' => WP_REST_Server::READABLE, | ||
| 'callback' => array( $this, 'get_item_output' ), | ||
| 'permission_callback' => array( $this, 'get_item_output_permissions_check' ), | ||
| 'args' => array( | ||
| 'context' => $this->get_context_param( array( 'default' => 'view' ) ), | ||
| ), | ||
| ), | ||
| 'schema' => array( $this, 'get_public_item_schema' ), | ||
| ) ); | ||
| } | ||
|
|
||
| /** | ||
| * Checks if a given request has access to read blocks. | ||
| * | ||
| * @since ? | ||
| * @access public | ||
| * | ||
| * @return true|WP_Error True if the request has read access, WP_Error object otherwise. | ||
| */ | ||
| public function get_item_output_permissions_check() { | ||
| return true; | ||
| if ( ! current_user_can( 'edit_posts' ) ) { | ||
| return new WP_Error( 'gutenberg_block_cannot_read', __( 'Sorry, you are not allowed to read Gutenberg blocks as this user.', 'gutenberg' ), array( | ||
| 'status' => rest_authorization_required_code(), | ||
| ) ); | ||
| } | ||
|
|
||
| return true; | ||
| } | ||
|
|
||
| /** | ||
| * Returns block output from block's registered render_callback. | ||
| * | ||
| * @since ? | ||
| * @access public | ||
| * | ||
| * @param WP_REST_Request $request Full details about the request. | ||
| * @return WP_REST_Response|WP_Error Response object on success, or WP_Error object on failure. | ||
| */ | ||
| public function get_item_output( $request ) { | ||
| if ( ! isset( $request['name'] ) ) { | ||
| return new WP_Error( 'rest_block_invalid_name', __( 'Invalid block name.' ), array( 'status' => 404 ) ); | ||
| } | ||
|
|
||
| $registry = WP_Block_Type_Registry::get_instance(); | ||
| $block = $registry->get_registered( $request['name'] ); | ||
|
|
||
| if ( ! $block || ! $block instanceof WP_Block_Type) { | ||
| return new WP_Error( 'rest_block_invalid_name', __( 'Invalid block name.' ), array( 'status' => 404 ) ); | ||
| } | ||
|
|
||
| if ( isset( $request['attributes'] ) && is_array( $request['attributes'] ) ) { | ||
| $atts = $request['attributes']; | ||
| } else { | ||
| $atts = array(); | ||
| } | ||
|
|
||
| $data = array( | ||
| 'output' => $block->render( $atts ), | ||
| ); | ||
| return rest_ensure_response( $data ); | ||
| } | ||
|
|
||
| /** | ||
| * Retrieves block's output schema, conforming to JSON Schema. | ||
| * | ||
| * @since ? | ||
| * @access public | ||
| * | ||
| * @return array Item schema data. | ||
| */ | ||
| public function get_item_schema() { | ||
| return array( | ||
| '$schema' => 'http://json-schema.org/schema#', | ||
| 'title' => 'shortcode-block', | ||
| 'type' => 'object', | ||
| 'properties' => array( | ||
| 'html' => array( | ||
| 'description' => __( 'The block\'s output.', 'gutenberg' ), | ||
| 'type' => 'string', | ||
| 'required' => true, | ||
| ),'required' => true, | ||
| ), | ||
| ); | ||
| } | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should this be singular?
Block_Renderer...There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yah, same with the endpoint route.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done in c4abc69