wp/wp-includes/rest-api/endpoints/class-wp-rest-block-renderer-controller.php
changeset 16 a86126ab1dd4
parent 9 177826044cd9
child 18 be944660c56a
equal deleted inserted replaced
15:3d4e9c994f10 16:a86126ab1dd4
    28 
    28 
    29 	/**
    29 	/**
    30 	 * Registers the necessary REST API routes, one for each dynamic block.
    30 	 * Registers the necessary REST API routes, one for each dynamic block.
    31 	 *
    31 	 *
    32 	 * @since 5.0.0
    32 	 * @since 5.0.0
       
    33 	 *
       
    34 	 * @see register_rest_route()
    33 	 */
    35 	 */
    34 	public function register_routes() {
    36 	public function register_routes() {
    35 		$block_types = WP_Block_Type_Registry::get_instance()->get_all_registered();
    37 		register_rest_route(
    36 
    38 			$this->namespace,
    37 		foreach ( $block_types as $block_type ) {
    39 			'/' . $this->rest_base . '/(?P<name>[a-z0-9-]+/[a-z0-9-]+)',
    38 			if ( ! $block_type->is_dynamic() ) {
    40 			array(
    39 				continue;
    41 				'args'   => array(
    40 			}
    42 					'name' => array(
    41 
    43 						'description' => __( 'Unique registered name for the block.' ),
    42 			register_rest_route(
    44 						'type'        => 'string',
    43 				$this->namespace,
    45 					),
    44 				'/' . $this->rest_base . '/(?P<name>' . $block_type->name . ')',
    46 				),
    45 				array(
    47 				array(
    46 					'args'   => array(
    48 					'methods'             => array( WP_REST_Server::READABLE, WP_REST_Server::CREATABLE ),
    47 						'name' => array(
    49 					'callback'            => array( $this, 'get_item' ),
    48 							'description' => __( 'Unique registered name for the block.' ),
    50 					'permission_callback' => array( $this, 'get_item_permissions_check' ),
    49 							'type'        => 'string',
    51 					'args'                => array(
       
    52 						'context'    => $this->get_context_param( array( 'default' => 'view' ) ),
       
    53 						'attributes' => array(
       
    54 							'description'       => __( 'Attributes for the block' ),
       
    55 							'type'              => 'object',
       
    56 							'default'           => array(),
       
    57 							'validate_callback' => static function ( $value, $request ) {
       
    58 								$block = WP_Block_Type_Registry::get_instance()->get_registered( $request['name'] );
       
    59 
       
    60 								if ( ! $block ) {
       
    61 									// This will get rejected in ::get_item().
       
    62 									return true;
       
    63 								}
       
    64 
       
    65 								$schema = array(
       
    66 									'type'                 => 'object',
       
    67 									'properties'           => $block->get_attributes(),
       
    68 									'additionalProperties' => false,
       
    69 								);
       
    70 
       
    71 								return rest_validate_value_from_schema( $value, $schema );
       
    72 							},
       
    73 							'sanitize_callback' => static function ( $value, $request ) {
       
    74 								$block = WP_Block_Type_Registry::get_instance()->get_registered( $request['name'] );
       
    75 
       
    76 								if ( ! $block ) {
       
    77 									// This will get rejected in ::get_item().
       
    78 									return true;
       
    79 								}
       
    80 
       
    81 								$schema = array(
       
    82 									'type'                 => 'object',
       
    83 									'properties'           => $block->get_attributes(),
       
    84 									'additionalProperties' => false,
       
    85 								);
       
    86 
       
    87 								return rest_sanitize_value_from_schema( $value, $schema );
       
    88 							},
       
    89 						),
       
    90 						'post_id'    => array(
       
    91 							'description' => __( 'ID of the post context.' ),
       
    92 							'type'        => 'integer',
    50 						),
    93 						),
    51 					),
    94 					),
    52 					array(
    95 				),
    53 						'methods'             => WP_REST_Server::READABLE,
    96 				'schema' => array( $this, 'get_public_item_schema' ),
    54 						'callback'            => array( $this, 'get_item' ),
    97 			)
    55 						'permission_callback' => array( $this, 'get_item_permissions_check' ),
    98 		);
    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 	}
    99 	}
    77 
   100 
    78 	/**
   101 	/**
    79 	 * Checks if a given request has access to read blocks.
   102 	 * Checks if a given request has access to read blocks.
    80 	 *
   103 	 *
   132 			$post = get_post( $post_id );
   155 			$post = get_post( $post_id );
   133 
   156 
   134 			// Set up postdata since this will be needed if post_id was set.
   157 			// Set up postdata since this will be needed if post_id was set.
   135 			setup_postdata( $post );
   158 			setup_postdata( $post );
   136 		}
   159 		}
   137 		$registry = WP_Block_Type_Registry::get_instance();
   160 
   138 		$block    = $registry->get_registered( $request['name'] );
   161 		$registry   = WP_Block_Type_Registry::get_instance();
   139 
   162 		$registered = $registry->get_registered( $request['name'] );
   140 		if ( null === $block ) {
   163 
       
   164 		if ( null === $registered || ! $registered->is_dynamic() ) {
   141 			return new WP_Error(
   165 			return new WP_Error(
   142 				'block_invalid',
   166 				'block_invalid',
   143 				__( 'Invalid block.' ),
   167 				__( 'Invalid block.' ),
   144 				array(
   168 				array(
   145 					'status' => 404,
   169 					'status' => 404,
   146 				)
   170 				)
   147 			);
   171 			);
   148 		}
   172 		}
   149 
   173 
       
   174 		$attributes = $request->get_param( 'attributes' );
       
   175 
       
   176 		// Create an array representation simulating the output of parse_blocks.
       
   177 		$block = array(
       
   178 			'blockName'    => $request['name'],
       
   179 			'attrs'        => $attributes,
       
   180 			'innerHTML'    => '',
       
   181 			'innerContent' => array(),
       
   182 		);
       
   183 
       
   184 		// Render using render_block to ensure all relevant filters are used.
   150 		$data = array(
   185 		$data = array(
   151 			'rendered' => $block->render( $request->get_param( 'attributes' ) ),
   186 			'rendered' => render_block( $block ),
   152 		);
   187 		);
       
   188 
   153 		return rest_ensure_response( $data );
   189 		return rest_ensure_response( $data );
   154 	}
   190 	}
   155 
   191 
   156 	/**
   192 	/**
   157 	 * Retrieves block's output schema, conforming to JSON Schema.
   193 	 * Retrieves block's output schema, conforming to JSON Schema.
   159 	 * @since 5.0.0
   195 	 * @since 5.0.0
   160 	 *
   196 	 *
   161 	 * @return array Item schema data.
   197 	 * @return array Item schema data.
   162 	 */
   198 	 */
   163 	public function get_item_schema() {
   199 	public function get_item_schema() {
   164 		return array(
   200 		if ( $this->schema ) {
       
   201 			return $this->schema;
       
   202 		}
       
   203 
       
   204 		$this->schema = array(
   165 			'$schema'    => 'http://json-schema.org/schema#',
   205 			'$schema'    => 'http://json-schema.org/schema#',
   166 			'title'      => 'rendered-block',
   206 			'title'      => 'rendered-block',
   167 			'type'       => 'object',
   207 			'type'       => 'object',
   168 			'properties' => array(
   208 			'properties' => array(
   169 				'rendered' => array(
   209 				'rendered' => array(
   172 					'required'    => true,
   212 					'required'    => true,
   173 					'context'     => array( 'edit' ),
   213 					'context'     => array( 'edit' ),
   174 				),
   214 				),
   175 			),
   215 			),
   176 		);
   216 		);
       
   217 
       
   218 		return $this->schema;
   177 	}
   219 	}
   178 }
   220 }