wp/wp-includes/rest-api/endpoints/class-wp-rest-block-renderer-controller.php
changeset 9 177826044cd9
child 16 a86126ab1dd4
equal deleted inserted replaced
8:c7c34916027a 9:177826044cd9
       
     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 /**
       
    11  * Controller which provides REST endpoint for rendering a block.
       
    12  *
       
    13  * @since 5.0.0
       
    14  *
       
    15  * @see WP_REST_Controller
       
    16  */
       
    17 class WP_REST_Block_Renderer_Controller extends WP_REST_Controller {
       
    18 
       
    19 	/**
       
    20 	 * Constructs the controller.
       
    21 	 *
       
    22 	 * @since 5.0.0
       
    23 	 */
       
    24 	public function __construct() {
       
    25 		$this->namespace = 'wp/v2';
       
    26 		$this->rest_base = 'block-renderer';
       
    27 	}
       
    28 
       
    29 	/**
       
    30 	 * Registers the necessary REST API routes, one for each dynamic block.
       
    31 	 *
       
    32 	 * @since 5.0.0
       
    33 	 */
       
    34 	public function register_routes() {
       
    35 		$block_types = WP_Block_Type_Registry::get_instance()->get_all_registered();
       
    36 
       
    37 		foreach ( $block_types as $block_type ) {
       
    38 			if ( ! $block_type->is_dynamic() ) {
       
    39 				continue;
       
    40 			}
       
    41 
       
    42 			register_rest_route(
       
    43 				$this->namespace,
       
    44 				'/' . $this->rest_base . '/(?P<name>' . $block_type->name . ')',
       
    45 				array(
       
    46 					'args'   => array(
       
    47 						'name' => array(
       
    48 							'description' => __( 'Unique registered name for the block.' ),
       
    49 							'type'        => 'string',
       
    50 						),
       
    51 					),
       
    52 					array(
       
    53 						'methods'             => WP_REST_Server::READABLE,
       
    54 						'callback'            => array( $this, 'get_item' ),
       
    55 						'permission_callback' => array( $this, 'get_item_permissions_check' ),
       
    56 						'args'                => array(
       
    57 							'context'    => $this->get_context_param( array( 'default' => 'view' ) ),
       
    58 							'attributes' => array(
       
    59 								/* translators: %s is the name of the block */
       
    60 								'description'          => sprintf( __( 'Attributes for %s block' ), $block_type->name ),
       
    61 								'type'                 => 'object',
       
    62 								'additionalProperties' => false,
       
    63 								'properties'           => $block_type->get_attributes(),
       
    64 								'default'              => array(),
       
    65 							),
       
    66 							'post_id'    => array(
       
    67 								'description' => __( 'ID of the post context.' ),
       
    68 								'type'        => 'integer',
       
    69 							),
       
    70 						),
       
    71 					),
       
    72 					'schema' => array( $this, 'get_public_item_schema' ),
       
    73 				)
       
    74 			);
       
    75 		}
       
    76 	}
       
    77 
       
    78 	/**
       
    79 	 * Checks if a given request has access to read blocks.
       
    80 	 *
       
    81 	 * @since 5.0.0
       
    82 	 *
       
    83 	 * @param WP_REST_Request $request Request.
       
    84 	 * @return true|WP_Error True if the request has read access, WP_Error object otherwise.
       
    85 	 */
       
    86 	public function get_item_permissions_check( $request ) {
       
    87 		global $post;
       
    88 
       
    89 		$post_id = isset( $request['post_id'] ) ? intval( $request['post_id'] ) : 0;
       
    90 
       
    91 		if ( 0 < $post_id ) {
       
    92 			$post = get_post( $post_id );
       
    93 
       
    94 			if ( ! $post || ! current_user_can( 'edit_post', $post->ID ) ) {
       
    95 				return new WP_Error(
       
    96 					'block_cannot_read',
       
    97 					__( 'Sorry, you are not allowed to read blocks of this post.' ),
       
    98 					array(
       
    99 						'status' => rest_authorization_required_code(),
       
   100 					)
       
   101 				);
       
   102 			}
       
   103 		} else {
       
   104 			if ( ! current_user_can( 'edit_posts' ) ) {
       
   105 				return new WP_Error(
       
   106 					'block_cannot_read',
       
   107 					__( 'Sorry, you are not allowed to read blocks as this user.' ),
       
   108 					array(
       
   109 						'status' => rest_authorization_required_code(),
       
   110 					)
       
   111 				);
       
   112 			}
       
   113 		}
       
   114 
       
   115 		return true;
       
   116 	}
       
   117 
       
   118 	/**
       
   119 	 * Returns block output from block's registered render_callback.
       
   120 	 *
       
   121 	 * @since 5.0.0
       
   122 	 *
       
   123 	 * @param WP_REST_Request $request Full details about the request.
       
   124 	 * @return WP_REST_Response|WP_Error Response object on success, or WP_Error object on failure.
       
   125 	 */
       
   126 	public function get_item( $request ) {
       
   127 		global $post;
       
   128 
       
   129 		$post_id = isset( $request['post_id'] ) ? intval( $request['post_id'] ) : 0;
       
   130 
       
   131 		if ( 0 < $post_id ) {
       
   132 			$post = get_post( $post_id );
       
   133 
       
   134 			// Set up postdata since this will be needed if post_id was set.
       
   135 			setup_postdata( $post );
       
   136 		}
       
   137 		$registry = WP_Block_Type_Registry::get_instance();
       
   138 		$block    = $registry->get_registered( $request['name'] );
       
   139 
       
   140 		if ( null === $block ) {
       
   141 			return new WP_Error(
       
   142 				'block_invalid',
       
   143 				__( 'Invalid block.' ),
       
   144 				array(
       
   145 					'status' => 404,
       
   146 				)
       
   147 			);
       
   148 		}
       
   149 
       
   150 		$data = array(
       
   151 			'rendered' => $block->render( $request->get_param( 'attributes' ) ),
       
   152 		);
       
   153 		return rest_ensure_response( $data );
       
   154 	}
       
   155 
       
   156 	/**
       
   157 	 * Retrieves block's output schema, conforming to JSON Schema.
       
   158 	 *
       
   159 	 * @since 5.0.0
       
   160 	 *
       
   161 	 * @return array Item schema data.
       
   162 	 */
       
   163 	public function get_item_schema() {
       
   164 		return array(
       
   165 			'$schema'    => 'http://json-schema.org/schema#',
       
   166 			'title'      => 'rendered-block',
       
   167 			'type'       => 'object',
       
   168 			'properties' => array(
       
   169 				'rendered' => array(
       
   170 					'description' => __( 'The rendered block.' ),
       
   171 					'type'        => 'string',
       
   172 					'required'    => true,
       
   173 					'context'     => array( 'edit' ),
       
   174 				),
       
   175 			),
       
   176 		);
       
   177 	}
       
   178 }