2018-12-13 21:35:38 -05:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* Block Renderer REST API: WP_REST_Block_Renderer_Controller class
|
|
|
|
*
|
|
|
|
* @package WordPress
|
|
|
|
* @subpackage REST_API
|
|
|
|
* @since 5.0.0
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Controller which provides REST endpoint for rendering a block.
|
|
|
|
*
|
|
|
|
* @since 5.0.0
|
|
|
|
*
|
|
|
|
* @see WP_REST_Controller
|
|
|
|
*/
|
|
|
|
class WP_REST_Block_Renderer_Controller extends WP_REST_Controller {
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Constructs the controller.
|
|
|
|
*
|
|
|
|
* @since 5.0.0
|
|
|
|
*/
|
|
|
|
public function __construct() {
|
|
|
|
$this->namespace = 'wp/v2';
|
|
|
|
$this->rest_base = 'block-renderer';
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Registers the necessary REST API routes, one for each dynamic block.
|
|
|
|
*
|
|
|
|
* @since 5.0.0
|
2020-02-28 07:31:08 -05:00
|
|
|
*
|
|
|
|
* @see register_rest_route()
|
2018-12-13 21:35:38 -05:00
|
|
|
*/
|
|
|
|
public function register_routes() {
|
|
|
|
$block_types = WP_Block_Type_Registry::get_instance()->get_all_registered();
|
|
|
|
|
|
|
|
foreach ( $block_types as $block_type ) {
|
|
|
|
if ( ! $block_type->is_dynamic() ) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
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.' ),
|
|
|
|
'type' => 'string',
|
|
|
|
),
|
|
|
|
),
|
|
|
|
array(
|
2020-05-03 18:58:05 -04:00
|
|
|
'methods' => array( WP_REST_Server::READABLE, WP_REST_Server::CREATABLE ),
|
2018-12-13 21:35:38 -05:00
|
|
|
'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(
|
2019-09-02 20:41:05 -04:00
|
|
|
/* translators: %s: The name of the block. */
|
2018-12-13 21:35:38 -05:00
|
|
|
'description' => sprintf( __( 'Attributes for %s block' ), $block_type->name ),
|
|
|
|
'type' => 'object',
|
|
|
|
'additionalProperties' => false,
|
|
|
|
'properties' => $block_type->get_attributes(),
|
REST API: Preserve unknown, respect `null` in server-side block rendering.
- Skips validation where there is no attribute definition, but keeps the attribute value. Previously, the attribute would be omitted from the attributes passed to `render_callback`. Notably, this resolves an issue where `render_callback` cannot receive a block's `align` and `customClassName` attribute values, since these are defined as a client-side filter.
- Validates `null` as a proper value in its own right. Previously, a client implementation of a block could track `{“attribute":null}` as an explicitly empty value, and the server would wrongly initiate defaulting behavior. The new behavior will now only populate a default value if the attribute is not defined at all, including when unset in its being invalid per the attribute schema.
Props aduth, noisysocks, youknowriad, danielbachhuber.
Merges [43918] to trunk.
See #45145 for the patch, #45098 for the original ticket.
Built from https://develop.svn.wordpress.org/trunk@44269
git-svn-id: http://core.svn.wordpress.org/trunk@44099 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2018-12-17 13:00:52 -05:00
|
|
|
'default' => array(),
|
2018-12-13 21:35:38 -05:00
|
|
|
),
|
|
|
|
'post_id' => array(
|
|
|
|
'description' => __( 'ID of the post context.' ),
|
|
|
|
'type' => 'integer',
|
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
'schema' => array( $this, 'get_public_item_schema' ),
|
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Checks if a given request has access to read blocks.
|
|
|
|
*
|
|
|
|
* @since 5.0.0
|
|
|
|
*
|
|
|
|
* @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( $request ) {
|
|
|
|
global $post;
|
|
|
|
|
|
|
|
$post_id = isset( $request['post_id'] ) ? intval( $request['post_id'] ) : 0;
|
|
|
|
|
|
|
|
if ( 0 < $post_id ) {
|
|
|
|
$post = get_post( $post_id );
|
|
|
|
|
|
|
|
if ( ! $post || ! current_user_can( 'edit_post', $post->ID ) ) {
|
|
|
|
return new WP_Error(
|
|
|
|
'block_cannot_read',
|
|
|
|
__( 'Sorry, you are not allowed to read blocks of this post.' ),
|
|
|
|
array(
|
|
|
|
'status' => rest_authorization_required_code(),
|
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if ( ! current_user_can( 'edit_posts' ) ) {
|
|
|
|
return new WP_Error(
|
|
|
|
'block_cannot_read',
|
|
|
|
__( 'Sorry, you are not allowed to read blocks as this user.' ),
|
|
|
|
array(
|
|
|
|
'status' => rest_authorization_required_code(),
|
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns block output from block's registered render_callback.
|
|
|
|
*
|
|
|
|
* @since 5.0.0
|
|
|
|
*
|
|
|
|
* @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( $request ) {
|
|
|
|
global $post;
|
|
|
|
|
|
|
|
$post_id = isset( $request['post_id'] ) ? intval( $request['post_id'] ) : 0;
|
|
|
|
|
|
|
|
if ( 0 < $post_id ) {
|
|
|
|
$post = get_post( $post_id );
|
|
|
|
|
|
|
|
// Set up postdata since this will be needed if post_id was set.
|
|
|
|
setup_postdata( $post );
|
|
|
|
}
|
|
|
|
$registry = WP_Block_Type_Registry::get_instance();
|
|
|
|
|
2020-02-25 10:05:06 -05:00
|
|
|
if ( null === $registry->get_registered( $request['name'] ) ) {
|
2018-12-13 21:35:38 -05:00
|
|
|
return new WP_Error(
|
|
|
|
'block_invalid',
|
|
|
|
__( 'Invalid block.' ),
|
|
|
|
array(
|
|
|
|
'status' => 404,
|
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2020-02-25 10:05:06 -05:00
|
|
|
$attributes = $request->get_param( 'attributes' );
|
|
|
|
|
|
|
|
// Create an array representation simulating the output of parse_blocks.
|
|
|
|
$block = array(
|
|
|
|
'blockName' => $request['name'],
|
|
|
|
'attrs' => $attributes,
|
|
|
|
'innerHTML' => '',
|
|
|
|
'innerContent' => array(),
|
|
|
|
);
|
|
|
|
|
|
|
|
// Render using render_block to ensure all relevant filters are used.
|
2018-12-13 21:35:38 -05:00
|
|
|
$data = array(
|
2020-02-25 10:05:06 -05:00
|
|
|
'rendered' => render_block( $block ),
|
2018-12-13 21:35:38 -05:00
|
|
|
);
|
2020-01-28 19:45:18 -05:00
|
|
|
|
2018-12-13 21:35:38 -05:00
|
|
|
return rest_ensure_response( $data );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Retrieves block's output schema, conforming to JSON Schema.
|
|
|
|
*
|
|
|
|
* @since 5.0.0
|
|
|
|
*
|
|
|
|
* @return array Item schema data.
|
|
|
|
*/
|
|
|
|
public function get_item_schema() {
|
2019-08-15 17:09:55 -04:00
|
|
|
if ( $this->schema ) {
|
|
|
|
return $this->schema;
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->schema = array(
|
2018-12-13 21:35:38 -05:00
|
|
|
'$schema' => 'http://json-schema.org/schema#',
|
|
|
|
'title' => 'rendered-block',
|
|
|
|
'type' => 'object',
|
|
|
|
'properties' => array(
|
|
|
|
'rendered' => array(
|
|
|
|
'description' => __( 'The rendered block.' ),
|
|
|
|
'type' => 'string',
|
|
|
|
'required' => true,
|
|
|
|
'context' => array( 'edit' ),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
);
|
2020-01-28 19:45:18 -05:00
|
|
|
|
2019-08-15 17:09:55 -04:00
|
|
|
return $this->schema;
|
2018-12-13 21:35:38 -05:00
|
|
|
}
|
|
|
|
}
|