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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  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. public function register_routes() {
  32. $block_types = WP_Block_Type_Registry::get_instance()->get_all_registered();
  33. foreach ( $block_types as $block_type ) {
  34. if ( ! $block_type->is_dynamic() ) {
  35. continue;
  36. }
  37. register_rest_route(
  38. $this->namespace,
  39. '/' . $this->rest_base . '/(?P<name>' . $block_type->name . ')',
  40. array(
  41. 'args' => array(
  42. 'name' => array(
  43. 'description' => __( 'Unique registered name for the block.' ),
  44. 'type' => 'string',
  45. ),
  46. ),
  47. array(
  48. 'methods' => WP_REST_Server::READABLE,
  49. 'callback' => array( $this, 'get_item' ),
  50. 'permission_callback' => array( $this, 'get_item_permissions_check' ),
  51. 'args' => array(
  52. 'context' => $this->get_context_param( array( 'default' => 'view' ) ),
  53. 'attributes' => array(
  54. /* translators: %s: The name of the block. */
  55. 'description' => sprintf( __( 'Attributes for %s block' ), $block_type->name ),
  56. 'type' => 'object',
  57. 'additionalProperties' => false,
  58. 'properties' => $block_type->get_attributes(),
  59. 'default' => array(),
  60. ),
  61. 'post_id' => array(
  62. 'description' => __( 'ID of the post context.' ),
  63. 'type' => 'integer',
  64. ),
  65. ),
  66. ),
  67. 'schema' => array( $this, 'get_public_item_schema' ),
  68. )
  69. );
  70. }
  71. }
  72. /**
  73. * Checks if a given request has access to read blocks.
  74. *
  75. * @since 5.0.0
  76. *
  77. * @param WP_REST_Request $request Request.
  78. * @return true|WP_Error True if the request has read access, WP_Error object otherwise.
  79. */
  80. public function get_item_permissions_check( $request ) {
  81. global $post;
  82. $post_id = isset( $request['post_id'] ) ? intval( $request['post_id'] ) : 0;
  83. if ( 0 < $post_id ) {
  84. $post = get_post( $post_id );
  85. if ( ! $post || ! current_user_can( 'edit_post', $post->ID ) ) {
  86. return new WP_Error(
  87. 'block_cannot_read',
  88. __( 'Sorry, you are not allowed to read blocks of this post.' ),
  89. array(
  90. 'status' => rest_authorization_required_code(),
  91. )
  92. );
  93. }
  94. } else {
  95. if ( ! current_user_can( 'edit_posts' ) ) {
  96. return new WP_Error(
  97. 'block_cannot_read',
  98. __( 'Sorry, you are not allowed to read blocks as this user.' ),
  99. array(
  100. 'status' => rest_authorization_required_code(),
  101. )
  102. );
  103. }
  104. }
  105. return true;
  106. }
  107. /**
  108. * Returns block output from block's registered render_callback.
  109. *
  110. * @since 5.0.0
  111. *
  112. * @param WP_REST_Request $request Full details about the request.
  113. * @return WP_REST_Response|WP_Error Response object on success, or WP_Error object on failure.
  114. */
  115. public function get_item( $request ) {
  116. global $post;
  117. $post_id = isset( $request['post_id'] ) ? intval( $request['post_id'] ) : 0;
  118. if ( 0 < $post_id ) {
  119. $post = get_post( $post_id );
  120. // Set up postdata since this will be needed if post_id was set.
  121. setup_postdata( $post );
  122. }
  123. $registry = WP_Block_Type_Registry::get_instance();
  124. $block = $registry->get_registered( $request['name'] );
  125. if ( null === $block ) {
  126. return new WP_Error(
  127. 'block_invalid',
  128. __( 'Invalid block.' ),
  129. array(
  130. 'status' => 404,
  131. )
  132. );
  133. }
  134. $data = array(
  135. 'rendered' => $block->render( $request->get_param( 'attributes' ) ),
  136. );
  137. return rest_ensure_response( $data );
  138. }
  139. /**
  140. * Retrieves block's output schema, conforming to JSON Schema.
  141. *
  142. * @since 5.0.0
  143. *
  144. * @return array Item schema data.
  145. */
  146. public function get_item_schema() {
  147. if ( $this->schema ) {
  148. return $this->schema;
  149. }
  150. $this->schema = array(
  151. '$schema' => 'http://json-schema.org/schema#',
  152. 'title' => 'rendered-block',
  153. 'type' => 'object',
  154. 'properties' => array(
  155. 'rendered' => array(
  156. 'description' => __( 'The rendered block.' ),
  157. 'type' => 'string',
  158. 'required' => true,
  159. 'context' => array( 'edit' ),
  160. ),
  161. ),
  162. );
  163. return $this->schema;
  164. }
  165. }