[ Index ]

PHP Cross Reference of WordPress

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 WP_Post $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          // By default the read_post capability is mapped to edit_posts.
  32          if ( ! current_user_can( 'read_post', $post->ID ) ) {
  33              return false;
  34          }
  35  
  36          return parent::check_read_permission( $post );
  37      }
  38  
  39      /**
  40       * Filters a response based on the context defined in the schema.
  41       *
  42       * @since 5.0.0
  43       *
  44       * @param array  $data    Response data to filter.
  45       * @param string $context Context defined in the schema.
  46       * @return array Filtered response.
  47       */
  48  	public function filter_response_by_context( $data, $context ) {
  49          $data = parent::filter_response_by_context( $data, $context );
  50  
  51          /*
  52           * Remove `title.rendered` and `content.rendered` from the response. It
  53           * doesn't make sense for a reusable block to have rendered content on its
  54           * own, since rendering a block requires it to be inside a post or a page.
  55           */
  56          unset( $data['title']['rendered'] );
  57          unset( $data['content']['rendered'] );
  58  
  59          return $data;
  60      }
  61  
  62      /**
  63       * Retrieves the block's schema, conforming to JSON Schema.
  64       *
  65       * @since 5.0.0
  66       *
  67       * @return array Item schema data.
  68       */
  69  	public function get_item_schema() {
  70          // Do not cache this schema because all properties are derived from parent controller.
  71          $schema = parent::get_item_schema();
  72  
  73          /*
  74           * Allow all contexts to access `title.raw` and `content.raw`. Clients always
  75           * need the raw markup of a reusable block to do anything useful, e.g. parse
  76           * it or display it in an editor.
  77           */
  78          $schema['properties']['title']['properties']['raw']['context']   = array( 'view', 'edit' );
  79          $schema['properties']['content']['properties']['raw']['context'] = array( 'view', 'edit' );
  80  
  81          /*
  82           * Remove `title.rendered` and `content.rendered` from the schema. It doesn’t
  83           * make sense for a reusable block to have rendered content on its own, since
  84           * rendering a block requires it to be inside a post or a page.
  85           */
  86          unset( $schema['properties']['title']['properties']['rendered'] );
  87          unset( $schema['properties']['content']['properties']['rendered'] );
  88  
  89          return $schema;
  90      }
  91  
  92  }


Generated: Thu Nov 21 01:00:03 2024 Cross-referenced by PHPXref 0.7.1