[ Index ]

PHP Cross Reference of WordPress Trunk (Updated Daily)

Search

title

Body

[close]

/wp-includes/rest-api/endpoints/ -> class-wp-rest-block-renderer-controller.php (source)

   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       * @param WP_REST_Request $request Request.
 107       * @return true|WP_Error True if the request has read access, WP_Error object otherwise.
 108       */
 109  	public function get_item_permissions_check( $request ) {
 110          global $post;
 111  
 112          $post_id = isset( $request['post_id'] ) ? intval( $request['post_id'] ) : 0;
 113  
 114          if ( 0 < $post_id ) {
 115              $post = get_post( $post_id );
 116  
 117              if ( ! $post || ! current_user_can( 'edit_post', $post->ID ) ) {
 118                  return new WP_Error(
 119                      'block_cannot_read',
 120                      __( 'Sorry, you are not allowed to read blocks of this post.' ),
 121                      array(
 122                          'status' => rest_authorization_required_code(),
 123                      )
 124                  );
 125              }
 126          } else {
 127              if ( ! current_user_can( 'edit_posts' ) ) {
 128                  return new WP_Error(
 129                      'block_cannot_read',
 130                      __( 'Sorry, you are not allowed to read blocks as this user.' ),
 131                      array(
 132                          'status' => rest_authorization_required_code(),
 133                      )
 134                  );
 135              }
 136          }
 137  
 138          return true;
 139      }
 140  
 141      /**
 142       * Returns block output from block's registered render_callback.
 143       *
 144       * @since 5.0.0
 145       *
 146       * @param WP_REST_Request $request Full details about the request.
 147       * @return WP_REST_Response|WP_Error Response object on success, or WP_Error object on failure.
 148       */
 149  	public function get_item( $request ) {
 150          global $post;
 151  
 152          $post_id = isset( $request['post_id'] ) ? intval( $request['post_id'] ) : 0;
 153  
 154          if ( 0 < $post_id ) {
 155              $post = get_post( $post_id );
 156  
 157              // Set up postdata since this will be needed if post_id was set.
 158              setup_postdata( $post );
 159          }
 160  
 161          $registry   = WP_Block_Type_Registry::get_instance();
 162          $registered = $registry->get_registered( $request['name'] );
 163  
 164          if ( null === $registered || ! $registered->is_dynamic() ) {
 165              return new WP_Error(
 166                  'block_invalid',
 167                  __( 'Invalid block.' ),
 168                  array(
 169                      'status' => 404,
 170                  )
 171              );
 172          }
 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.
 185          $data = array(
 186              'rendered' => render_block( $block ),
 187          );
 188  
 189          return rest_ensure_response( $data );
 190      }
 191  
 192      /**
 193       * Retrieves block's output schema, conforming to JSON Schema.
 194       *
 195       * @since 5.0.0
 196       *
 197       * @return array Item schema data.
 198       */
 199  	public function get_item_schema() {
 200          if ( $this->schema ) {
 201              return $this->schema;
 202          }
 203  
 204          $this->schema = array(
 205              '$schema'    => 'http://json-schema.org/schema#',
 206              'title'      => 'rendered-block',
 207              'type'       => 'object',
 208              'properties' => array(
 209                  'rendered' => array(
 210                      'description' => __( 'The rendered block.' ),
 211                      'type'        => 'string',
 212                      'required'    => true,
 213                      'context'     => array( 'edit' ),
 214                  ),
 215              ),
 216          );
 217  
 218          return $this->schema;
 219      }
 220  }


Generated : Thu Sep 24 08:20:02 2020 Cross-referenced by PHPXref