class-wp-rest-block-renderer-controller.php 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. <?php
  2. /**
  3. * Block Renderer REST API: WP_REST_Block_Renderer_Controller class
  4. *
  5. * @package WordPress
  6. * @subpackage REST_API
  7. * @since 5.0.0
  8. */
  9. /**
  10. * Controller which provides REST endpoint for rendering a block.
  11. *
  12. * @since 5.0.0
  13. *
  14. * @see WP_REST_Controller
  15. */
  16. class WP_REST_Block_Renderer_Controller extends WP_REST_Controller {
  17. /**
  18. * Constructs the controller.
  19. *
  20. * @since 5.0.0
  21. */
  22. public function __construct() {
  23. $this->namespace = 'wp/v2';
  24. $this->rest_base = 'block-renderer';
  25. }
  26. /**
  27. * Registers the necessary REST API routes, one for each dynamic block.
  28. *
  29. * @since 5.0.0
  30. *
  31. * @see register_rest_route()
  32. */
  33. public function register_routes() {
  34. register_rest_route(
  35. $this->namespace,
  36. '/' . $this->rest_base . '/(?P<name>[a-z0-9-]+/[a-z0-9-]+)',
  37. array(
  38. 'args' => array(
  39. 'name' => array(
  40. 'description' => __( 'Unique registered name for the block.' ),
  41. 'type' => 'string',
  42. ),
  43. ),
  44. array(
  45. 'methods' => array( WP_REST_Server::READABLE, WP_REST_Server::CREATABLE ),
  46. 'callback' => array( $this, 'get_item' ),
  47. 'permission_callback' => array( $this, 'get_item_permissions_check' ),
  48. 'args' => array(
  49. 'context' => $this->get_context_param( array( 'default' => 'view' ) ),
  50. 'attributes' => array(
  51. 'description' => __( 'Attributes for the block.' ),
  52. 'type' => 'object',
  53. 'default' => array(),
  54. 'validate_callback' => static function ( $value, $request ) {
  55. $block = WP_Block_Type_Registry::get_instance()->get_registered( $request['name'] );
  56. if ( ! $block ) {
  57. // This will get rejected in ::get_item().
  58. return true;
  59. }
  60. $schema = array(
  61. 'type' => 'object',
  62. 'properties' => $block->get_attributes(),
  63. 'additionalProperties' => false,
  64. );
  65. return rest_validate_value_from_schema( $value, $schema );
  66. },
  67. 'sanitize_callback' => static function ( $value, $request ) {
  68. $block = WP_Block_Type_Registry::get_instance()->get_registered( $request['name'] );
  69. if ( ! $block ) {
  70. // This will get rejected in ::get_item().
  71. return true;
  72. }
  73. $schema = array(
  74. 'type' => 'object',
  75. 'properties' => $block->get_attributes(),
  76. 'additionalProperties' => false,
  77. );
  78. return rest_sanitize_value_from_schema( $value, $schema );
  79. },
  80. ),
  81. 'post_id' => array(
  82. 'description' => __( 'ID of the post context.' ),
  83. 'type' => 'integer',
  84. ),
  85. ),
  86. ),
  87. 'schema' => array( $this, 'get_public_item_schema' ),
  88. )
  89. );
  90. }
  91. /**
  92. * Checks if a given request has access to read blocks.
  93. *
  94. * @since 5.0.0
  95. *
  96. * @global WP_Post $post Global post object.
  97. *
  98. * @param WP_REST_Request $request Request.
  99. * @return true|WP_Error True if the request has read access, WP_Error object otherwise.
  100. */
  101. public function get_item_permissions_check( $request ) {
  102. global $post;
  103. $post_id = isset( $request['post_id'] ) ? (int) $request['post_id'] : 0;
  104. if ( $post_id > 0 ) {
  105. $post = get_post( $post_id );
  106. if ( ! $post || ! current_user_can( 'edit_post', $post->ID ) ) {
  107. return new WP_Error(
  108. 'block_cannot_read',
  109. __( 'Sorry, you are not allowed to read blocks of this post.' ),
  110. array(
  111. 'status' => rest_authorization_required_code(),
  112. )
  113. );
  114. }
  115. } else {
  116. if ( ! current_user_can( 'edit_posts' ) ) {
  117. return new WP_Error(
  118. 'block_cannot_read',
  119. __( 'Sorry, you are not allowed to read blocks as this user.' ),
  120. array(
  121. 'status' => rest_authorization_required_code(),
  122. )
  123. );
  124. }
  125. }
  126. return true;
  127. }
  128. /**
  129. * Returns block output from block's registered render_callback.
  130. *
  131. * @since 5.0.0
  132. *
  133. * @global WP_Post $post Global post object.
  134. *
  135. * @param WP_REST_Request $request Full details about the request.
  136. * @return WP_REST_Response|WP_Error Response object on success, or WP_Error object on failure.
  137. */
  138. public function get_item( $request ) {
  139. global $post;
  140. $post_id = isset( $request['post_id'] ) ? (int) $request['post_id'] : 0;
  141. if ( $post_id > 0 ) {
  142. $post = get_post( $post_id );
  143. // Set up postdata since this will be needed if post_id was set.
  144. setup_postdata( $post );
  145. }
  146. $registry = WP_Block_Type_Registry::get_instance();
  147. $registered = $registry->get_registered( $request['name'] );
  148. if ( null === $registered || ! $registered->is_dynamic() ) {
  149. return new WP_Error(
  150. 'block_invalid',
  151. __( 'Invalid block.' ),
  152. array(
  153. 'status' => 404,
  154. )
  155. );
  156. }
  157. $attributes = $request->get_param( 'attributes' );
  158. // Create an array representation simulating the output of parse_blocks.
  159. $block = array(
  160. 'blockName' => $request['name'],
  161. 'attrs' => $attributes,
  162. 'innerHTML' => '',
  163. 'innerContent' => array(),
  164. );
  165. // Render using render_block to ensure all relevant filters are used.
  166. $data = array(
  167. 'rendered' => render_block( $block ),
  168. );
  169. return rest_ensure_response( $data );
  170. }
  171. /**
  172. * Retrieves block's output schema, conforming to JSON Schema.
  173. *
  174. * @since 5.0.0
  175. *
  176. * @return array Item schema data.
  177. */
  178. public function get_item_schema() {
  179. if ( $this->schema ) {
  180. return $this->schema;
  181. }
  182. $this->schema = array(
  183. '$schema' => 'http://json-schema.org/schema#',
  184. 'title' => 'rendered-block',
  185. 'type' => 'object',
  186. 'properties' => array(
  187. 'rendered' => array(
  188. 'description' => __( 'The rendered block.' ),
  189. 'type' => 'string',
  190. 'required' => true,
  191. 'context' => array( 'edit' ),
  192. ),
  193. ),
  194. );
  195. return $this->schema;
  196. }
  197. }