[ Index ]

PHP Cross Reference of WordPress Trunk (Updated Daily)

title

Body

[close]

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

   1  <?php
   2  /**
   3   * Reusable blocks REST API: WP_REST_Blocks_Controller class
   4   *
   5   * @package WordPress
   6   * @subpackage REST_API
   7   * @since 5.0.0
   8   */
   9  
  10  /**
  11   * Controller which provides a REST endpoint for the editor to read, create,
  12   * edit and delete reusable blocks. Blocks are stored as posts with the wp_block
  13   * post type.
  14   *
  15   * @since 5.0.0
  16   *
  17   * @see WP_REST_Posts_Controller
  18   * @see WP_REST_Controller
  19   */
  20  class WP_REST_Blocks_Controller extends WP_REST_Posts_Controller {
  21  
  22      /**
  23       * Checks if a block can be read.
  24       *
  25       * @since 5.0.0
  26       *
  27       * @param object $post Post object that backs the block.
  28       * @return bool Whether the block can be read.
  29       */
  30  	public function check_read_permission( $post ) {
  31          // Ensure that the user is logged in and has the read_blocks capability.
  32          $post_type = get_post_type_object( $post->post_type );
  33          if ( ! current_user_can( $post_type->cap->read_post, $post->ID ) ) {
  34              return false;
  35          }
  36  
  37          return parent::check_read_permission( $post );
  38      }
  39  
  40      /**
  41       * Filters a response based on the context defined in the schema.
  42       *
  43       * @since 5.0.0
  44       *
  45       * @param array  $data    Response data to fiter.
  46       * @param string $context Context defined in the schema.
  47       * @return array Filtered response.
  48       */
  49  	public function filter_response_by_context( $data, $context ) {
  50          $data = parent::filter_response_by_context( $data, $context );
  51  
  52          /*
  53           * Remove `title.rendered` and `content.rendered` from the response. It
  54           * doesn't make sense for a reusable block to have rendered content on its
  55           * own, since rendering a block requires it to be inside a post or a page.
  56           */
  57          unset( $data['title']['rendered'] );
  58          unset( $data['content']['rendered'] );
  59  
  60          return $data;
  61      }
  62  
  63      /**
  64       * Retrieves the block's schema, conforming to JSON Schema.
  65       *
  66       * @since 5.0.0
  67       *
  68       * @return array Item schema data.
  69       */
  70  	public function get_item_schema() {
  71          // Do not cache this schema because all properties are derived from parent controller.
  72          $schema = parent::get_item_schema();
  73  
  74          /*
  75           * Allow all contexts to access `title.raw` and `content.raw`. Clients always
  76           * need the raw markup of a reusable block to do anything useful, e.g. parse
  77           * it or display it in an editor.
  78           */
  79          $schema['properties']['title']['properties']['raw']['context']   = array( 'view', 'edit' );
  80          $schema['properties']['content']['properties']['raw']['context'] = array( 'view', 'edit' );
  81  
  82          /*
  83           * Remove `title.rendered` and `content.rendered` from the schema. It doesn’t
  84           * make sense for a reusable block to have rendered content on its own, since
  85           * rendering a block requires it to be inside a post or a page.
  86           */
  87          unset( $schema['properties']['title']['properties']['rendered'] );
  88          unset( $schema['properties']['content']['properties']['rendered'] );
  89  
  90          return $schema;
  91      }
  92  
  93  }


Generated: Sat Nov 23 20:47:33 2019 Cross-referenced by PHPXref 0.7