[ Index ]

PHP Cross Reference of WordPress

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          $block_types = WP_Block_Type_Registry::get_instance()->get_all_registered();
  38  
  39          foreach ( $block_types as $block_type ) {
  40              if ( ! $block_type->is_dynamic() ) {
  41                  continue;
  42              }
  43  
  44              register_rest_route(
  45                  $this->namespace,
  46                  '/' . $this->rest_base . '/(?P<name>' . $block_type->name . ')',
  47                  array(
  48                      'args'   => array(
  49                          'name' => array(
  50                              'description' => __( 'Unique registered name for the block.' ),
  51                              'type'        => 'string',
  52                          ),
  53                      ),
  54                      array(
  55                          'methods'             => WP_REST_Server::READABLE,
  56                          'callback'            => array( $this, 'get_item' ),
  57                          'permission_callback' => array( $this, 'get_item_permissions_check' ),
  58                          'args'                => array(
  59                              'context'    => $this->get_context_param( array( 'default' => 'view' ) ),
  60                              'attributes' => array(
  61                                  /* translators: %s: The name of the block. */
  62                                  'description'          => sprintf( __( 'Attributes for %s block' ), $block_type->name ),
  63                                  'type'                 => 'object',
  64                                  'additionalProperties' => false,
  65                                  'properties'           => $block_type->get_attributes(),
  66                                  'default'              => array(),
  67                              ),
  68                              'post_id'    => array(
  69                                  'description' => __( 'ID of the post context.' ),
  70                                  'type'        => 'integer',
  71                              ),
  72                          ),
  73                      ),
  74                      'schema' => array( $this, 'get_public_item_schema' ),
  75                  )
  76              );
  77          }
  78      }
  79  
  80      /**
  81       * Checks if a given request has access to read blocks.
  82       *
  83       * @since 5.0.0
  84       *
  85       * @param WP_REST_Request $request Request.
  86       * @return true|WP_Error True if the request has read access, WP_Error object otherwise.
  87       */
  88  	public function get_item_permissions_check( $request ) {
  89          global $post;
  90  
  91          $post_id = isset( $request['post_id'] ) ? intval( $request['post_id'] ) : 0;
  92  
  93          if ( 0 < $post_id ) {
  94              $post = get_post( $post_id );
  95  
  96              if ( ! $post || ! current_user_can( 'edit_post', $post->ID ) ) {
  97                  return new WP_Error(
  98                      'block_cannot_read',
  99                      __( 'Sorry, you are not allowed to read blocks of this post.' ),
 100                      array(
 101                          'status' => rest_authorization_required_code(),
 102                      )
 103                  );
 104              }
 105          } else {
 106              if ( ! current_user_can( 'edit_posts' ) ) {
 107                  return new WP_Error(
 108                      'block_cannot_read',
 109                      __( 'Sorry, you are not allowed to read blocks as this user.' ),
 110                      array(
 111                          'status' => rest_authorization_required_code(),
 112                      )
 113                  );
 114              }
 115          }
 116  
 117          return true;
 118      }
 119  
 120      /**
 121       * Returns block output from block's registered render_callback.
 122       *
 123       * @since 5.0.0
 124       *
 125       * @param WP_REST_Request $request Full details about the request.
 126       * @return WP_REST_Response|WP_Error Response object on success, or WP_Error object on failure.
 127       */
 128  	public function get_item( $request ) {
 129          global $post;
 130  
 131          $post_id = isset( $request['post_id'] ) ? intval( $request['post_id'] ) : 0;
 132  
 133          if ( 0 < $post_id ) {
 134              $post = get_post( $post_id );
 135  
 136              // Set up postdata since this will be needed if post_id was set.
 137              setup_postdata( $post );
 138          }
 139          $registry = WP_Block_Type_Registry::get_instance();
 140  
 141          if ( null === $registry->get_registered( $request['name'] ) ) {
 142              return new WP_Error(
 143                  'block_invalid',
 144                  __( 'Invalid block.' ),
 145                  array(
 146                      'status' => 404,
 147                  )
 148              );
 149          }
 150  
 151          $attributes = $request->get_param( 'attributes' );
 152  
 153          // Create an array representation simulating the output of parse_blocks.
 154          $block = array(
 155              'blockName'    => $request['name'],
 156              'attrs'        => $attributes,
 157              'innerHTML'    => '',
 158              'innerContent' => array(),
 159          );
 160  
 161          // Render using render_block to ensure all relevant filters are used.
 162          $data = array(
 163              'rendered' => render_block( $block ),
 164          );
 165  
 166          return rest_ensure_response( $data );
 167      }
 168  
 169      /**
 170       * Retrieves block's output schema, conforming to JSON Schema.
 171       *
 172       * @since 5.0.0
 173       *
 174       * @return array Item schema data.
 175       */
 176  	public function get_item_schema() {
 177          if ( $this->schema ) {
 178              return $this->schema;
 179          }
 180  
 181          $this->schema = array(
 182              '$schema'    => 'http://json-schema.org/schema#',
 183              'title'      => 'rendered-block',
 184              'type'       => 'object',
 185              'properties' => array(
 186                  'rendered' => array(
 187                      'description' => __( 'The rendered block.' ),
 188                      'type'        => 'string',
 189                      'required'    => true,
 190                      'context'     => array( 'edit' ),
 191                  ),
 192              ),
 193          );
 194  
 195          return $this->schema;
 196      }
 197  }


Generated: Sat Apr 4 01:00:03 2020 Cross-referenced by PHPXref 0.7.1