Няма описание

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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  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. * @param WP_REST_Request $request Request.
  97. * @return true|WP_Error True if the request has read access, WP_Error object otherwise.
  98. */
  99. public function get_item_permissions_check( $request ) {
  100. global $post;
  101. $post_id = isset( $request['post_id'] ) ? (int) $request['post_id'] : 0;
  102. if ( 0 < $post_id ) {
  103. $post = get_post( $post_id );
  104. if ( ! $post || ! current_user_can( 'edit_post', $post->ID ) ) {
  105. return new WP_Error(
  106. 'block_cannot_read',
  107. __( 'Sorry, you are not allowed to read blocks of this post.' ),
  108. array(
  109. 'status' => rest_authorization_required_code(),
  110. )
  111. );
  112. }
  113. } else {
  114. if ( ! current_user_can( 'edit_posts' ) ) {
  115. return new WP_Error(
  116. 'block_cannot_read',
  117. __( 'Sorry, you are not allowed to read blocks as this user.' ),
  118. array(
  119. 'status' => rest_authorization_required_code(),
  120. )
  121. );
  122. }
  123. }
  124. return true;
  125. }
  126. /**
  127. * Returns block output from block's registered render_callback.
  128. *
  129. * @since 5.0.0
  130. *
  131. * @param WP_REST_Request $request Full details about the request.
  132. * @return WP_REST_Response|WP_Error Response object on success, or WP_Error object on failure.
  133. */
  134. public function get_item( $request ) {
  135. global $post;
  136. $post_id = isset( $request['post_id'] ) ? (int) $request['post_id'] : 0;
  137. if ( 0 < $post_id ) {
  138. $post = get_post( $post_id );
  139. // Set up postdata since this will be needed if post_id was set.
  140. setup_postdata( $post );
  141. }
  142. $registry = WP_Block_Type_Registry::get_instance();
  143. $registered = $registry->get_registered( $request['name'] );
  144. if ( null === $registered || ! $registered->is_dynamic() ) {
  145. return new WP_Error(
  146. 'block_invalid',
  147. __( 'Invalid block.' ),
  148. array(
  149. 'status' => 404,
  150. )
  151. );
  152. }
  153. $attributes = $request->get_param( 'attributes' );
  154. // Create an array representation simulating the output of parse_blocks.
  155. $block = array(
  156. 'blockName' => $request['name'],
  157. 'attrs' => $attributes,
  158. 'innerHTML' => '',
  159. 'innerContent' => array(),
  160. );
  161. // Render using render_block to ensure all relevant filters are used.
  162. $data = array(
  163. 'rendered' => render_block( $block ),
  164. );
  165. return rest_ensure_response( $data );
  166. }
  167. /**
  168. * Retrieves block's output schema, conforming to JSON Schema.
  169. *
  170. * @since 5.0.0
  171. *
  172. * @return array Item schema data.
  173. */
  174. public function get_item_schema() {
  175. if ( $this->schema ) {
  176. return $this->schema;
  177. }
  178. $this->schema = array(
  179. '$schema' => 'http://json-schema.org/schema#',
  180. 'title' => 'rendered-block',
  181. 'type' => 'object',
  182. 'properties' => array(
  183. 'rendered' => array(
  184. 'description' => __( 'The rendered block.' ),
  185. 'type' => 'string',
  186. 'required' => true,
  187. 'context' => array( 'edit' ),
  188. ),
  189. ),
  190. );
  191. return $this->schema;
  192. }
  193. }