-
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
* Supply block attributes schema as endpoint schema. * Introduce attributes endpoint property and let REST API schema validate and sanitize them. * Ensure that attribute values are sanitized in addition to validated.
- Loading branch information
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -28,30 +28,44 @@ public function __construct() { | |
| } | ||
|
|
||
| /** | ||
| * Registers the necessary REST API routes. | ||
| * Registers the necessary REST API routes, one for each dynamic block. | ||
| * | ||
| * @access public | ||
| */ | ||
| public function register_routes() { | ||
|
|
||
| // @codingStandardsIgnoreLine - PHPCS mistakes $this->namespace for the namespace keyword. | ||
| register_rest_route( $this->namespace, '/' . $this->rest_base . '/(?P<name>[\w-]+\/[\w-]+)', array( | ||
| 'args' => array( | ||
| 'name' => array( | ||
| 'description' => __( 'Unique registered name for the block.', 'gutenberg' ), | ||
| 'type' => 'string', | ||
| $block_types = WP_Block_Type_Registry::get_instance()->get_all_registered(); | ||
| foreach ( $block_types as $block_type ) { | ||
| if ( ! $block_type->is_dynamic() ) { | ||
| continue; | ||
| } | ||
|
|
||
| // @codingStandardsIgnoreLine - PHPCS mistakes $this->namespace for the namespace keyword. | ||
| register_rest_route( $this->namespace, '/' . $this->rest_base . '/(?P<name>' . $block_type->name . ')', array( | ||
| 'args' => array( | ||
| 'name' => array( | ||
| 'description' => __( 'Unique registered name for the block.', 'gutenberg' ), | ||
| 'type' => 'string', | ||
| ), | ||
| ), | ||
| ), | ||
| array( | ||
| 'methods' => WP_REST_Server::READABLE, | ||
| 'callback' => array( $this, 'get_item' ), | ||
| 'permission_callback' => array( $this, 'get_item_permissions_check' ), | ||
| 'args' => array( | ||
| 'context' => $this->get_context_param( array( 'default' => 'view' ) ), | ||
| array( | ||
| 'methods' => WP_REST_Server::READABLE, | ||
| 'callback' => array( $this, 'get_item' ), | ||
| 'permission_callback' => array( $this, 'get_item_permissions_check' ), | ||
| 'args' => array( | ||
| 'context' => $this->get_context_param( array( 'default' => 'view' ) ), | ||
| 'attributes' => array( | ||
| /* translators: %s is the name of the block */ | ||
| 'description' => sprintf( __( 'Attributes for %s block', 'gutenberg' ), $block_type->name ), | ||
| 'type' => 'object', | ||
| 'additionalProperties' => false, | ||
| 'properties' => $block_type->attributes, | ||
| ), | ||
| ), | ||
| ), | ||
| ), | ||
| 'schema' => array( $this, 'get_public_item_schema' ), | ||
| ) ); | ||
| 'schema' => array( $this, 'get_public_item_schema' ), | ||
| ) ); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
|
|
@@ -60,9 +74,10 @@ public function register_routes() { | |
| * @since ? | ||
| * @access public | ||
| * | ||
| * @param WP_REST_Request $request Request. | ||
| * @return true|WP_Error True if the request has read access, WP_Error object otherwise. | ||
| */ | ||
| public function get_item_permissions_check() { | ||
| public function get_item_permissions_check( $request ) { | ||
| 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(), | ||
|
|
@@ -82,43 +97,14 @@ public function get_item_permissions_check() { | |
| * @return WP_REST_Response|WP_Error Response object on success, or WP_Error object on failure. | ||
| */ | ||
| public function get_item( $request ) { | ||
| if ( ! isset( $request['name'] ) ) { | ||
| return new WP_Error( 'rest_block_invalid_name', __( 'Invalid block name.', 'gutenberg' ), array( 'status' => 404 ) ); | ||
| } | ||
|
|
||
| $registry = WP_Block_Type_Registry::get_instance(); | ||
| $block = $registry->get_registered( $request['name'] ); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you return an error here if the block is invalid? |
||
|
|
||
| if ( ! $block || ! $block instanceof WP_Block_Type ) { | ||
| return new WP_Error( 'rest_block_invalid_name', __( 'Invalid block name.', 'gutenberg' ), array( 'status' => 404 ) ); | ||
| } | ||
|
|
||
| $atts = $this->prepare_attributes( $request->get_params() ); | ||
|
|
||
| $data = array( | ||
| 'rendered' => $block->render( $atts ), | ||
| $data = array( | ||
| 'rendered' => $block->render( $request->get_param( 'attributes' ) ), | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @westonruter This isn't working with the current SSR component code since the attributes are all sent as separate params and not as one Just sending
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That's a good question. I think it's important for const apiURL = addQueryArgs( '/gutenberg/v1/block-renderer/' + block, {
attributes,
_wpnonce: wpApiSettings.nonce,
} );In other words, I think
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Couldn't find an existing method at this moment, apparently query-string is intentionally not supporting nested attributes and suggests sending the object as a JSON string. Added a custom method for now to the class to put together the query string supporting objects (
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We may also be passing the full block content to the render callback in #6239 Rather than needing to remember passing all supported arguments to the |
||
| ); | ||
| return rest_ensure_response( $data ); | ||
| } | ||
|
|
||
| /** | ||
| * Fix potential boolean value issues. The values come as strings and "false" and "true" might generate issues if left like this. | ||
| * | ||
| * @param array $attributes Attributes. | ||
| * @return mixed Attributes. | ||
| */ | ||
| public function prepare_attributes( $attributes ) { | ||
| foreach ( $attributes as $key => $value ) { | ||
| if ( 'false' === $value ) { | ||
| $attributes[ $key ] = false; | ||
| } elseif ( 'true' === $value ) { | ||
| $attributes[ $key ] = true; | ||
| } | ||
| } | ||
|
|
||
| return $attributes; | ||
| } | ||
|
|
||
| /** | ||
| * Retrieves block's output schema, conforming to JSON Schema. | ||
| * | ||
|
|
@@ -130,11 +116,11 @@ public function prepare_attributes( $attributes ) { | |
| public function get_item_schema() { | ||
| return array( | ||
| '$schema' => 'http://json-schema.org/schema#', | ||
| 'title' => 'block-renderer', | ||
| 'title' => 'rendered-block', | ||
| 'type' => 'object', | ||
| 'properties' => array( | ||
| 'rendered' => array( | ||
| 'description' => __( 'The block\'s output.', 'gutenberg' ), | ||
| 'description' => __( 'The rendered block.', 'gutenberg' ), | ||
| 'type' => 'string', | ||
| 'required' => true, | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Added within a10bfac. |
||
| ), | ||
|
|
||
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.
Ditto above.
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.
Added an exception for this file within 6d1ee65.