[ Index ] |
PHP Cross Reference of WordPress |
[Summary view] [Print] [Text view]
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 * @see register_rest_route() 35 */ 36 public function register_routes() { 37 register_rest_route( 38 $this->namespace, 39 '/' . $this->rest_base . '/(?P<name>[a-z0-9-]+/[a-z0-9-]+)', 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' => array( WP_REST_Server::READABLE, WP_REST_Server::CREATABLE ), 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 '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', 93 ), 94 ), 95 ), 96 'schema' => array( $this, 'get_public_item_schema' ), 97 ) 98 ); 99 } 100 101 /** 102 * Checks if a given request has access to read blocks. 103 * 104 * @since 5.0.0 105 * 106 * @global WP_Post $post Global post object. 107 * 108 * @param WP_REST_Request $request Request. 109 * @return true|WP_Error True if the request has read access, WP_Error object otherwise. 110 */ 111 public function get_item_permissions_check( $request ) { 112 global $post; 113 114 $post_id = isset( $request['post_id'] ) ? (int) $request['post_id'] : 0; 115 116 if ( $post_id > 0 ) { 117 $post = get_post( $post_id ); 118 119 if ( ! $post || ! current_user_can( 'edit_post', $post->ID ) ) { 120 return new WP_Error( 121 'block_cannot_read', 122 __( 'Sorry, you are not allowed to read blocks of this post.' ), 123 array( 124 'status' => rest_authorization_required_code(), 125 ) 126 ); 127 } 128 } else { 129 if ( ! current_user_can( 'edit_posts' ) ) { 130 return new WP_Error( 131 'block_cannot_read', 132 __( 'Sorry, you are not allowed to read blocks as this user.' ), 133 array( 134 'status' => rest_authorization_required_code(), 135 ) 136 ); 137 } 138 } 139 140 return true; 141 } 142 143 /** 144 * Returns block output from block's registered render_callback. 145 * 146 * @since 5.0.0 147 * 148 * @global WP_Post $post Global post object. 149 * 150 * @param WP_REST_Request $request Full details about the request. 151 * @return WP_REST_Response|WP_Error Response object on success, or WP_Error object on failure. 152 */ 153 public function get_item( $request ) { 154 global $post; 155 156 $post_id = isset( $request['post_id'] ) ? (int) $request['post_id'] : 0; 157 158 if ( $post_id > 0 ) { 159 $post = get_post( $post_id ); 160 161 // Set up postdata since this will be needed if post_id was set. 162 setup_postdata( $post ); 163 } 164 165 $registry = WP_Block_Type_Registry::get_instance(); 166 $registered = $registry->get_registered( $request['name'] ); 167 168 if ( null === $registered || ! $registered->is_dynamic() ) { 169 return new WP_Error( 170 'block_invalid', 171 __( 'Invalid block.' ), 172 array( 173 'status' => 404, 174 ) 175 ); 176 } 177 178 $attributes = $request->get_param( 'attributes' ); 179 180 // Create an array representation simulating the output of parse_blocks. 181 $block = array( 182 'blockName' => $request['name'], 183 'attrs' => $attributes, 184 'innerHTML' => '', 185 'innerContent' => array(), 186 ); 187 188 // Render using render_block to ensure all relevant filters are used. 189 $data = array( 190 'rendered' => render_block( $block ), 191 ); 192 193 return rest_ensure_response( $data ); 194 } 195 196 /** 197 * Retrieves block's output schema, conforming to JSON Schema. 198 * 199 * @since 5.0.0 200 * 201 * @return array Item schema data. 202 */ 203 public function get_item_schema() { 204 if ( $this->schema ) { 205 return $this->schema; 206 } 207 208 $this->schema = array( 209 '$schema' => 'http://json-schema.org/schema#', 210 'title' => 'rendered-block', 211 'type' => 'object', 212 'properties' => array( 213 'rendered' => array( 214 'description' => __( 'The rendered block.' ), 215 'type' => 'string', 216 'required' => true, 217 'context' => array( 'edit' ), 218 ), 219 ), 220 ); 221 222 return $this->schema; 223 } 224 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
Generated: Sun Dec 22 01:00:02 2024 | Cross-referenced by PHPXref 0.7.1 |