[ Index ]

PHP Cross Reference of WordPress Trunk (Updated Daily)

Search

title

Body

[close]

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

   1  <?php
   2  /**
   3   * Synced patterns 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 synced patterns (formerly called reusable blocks).
  13   * Patterns are stored as posts with the wp_block 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 pattern 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 pattern 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       * @since 6.3.0 Adds the `wp_pattern_sync_status` postmeta property to the top level of response.
  44       *
  45       * @param array  $data    Response data to filter.
  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.
  54           * It doesn't make sense for a pattern to have rendered content on its own,
  55           * 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          // Add the core wp_pattern_sync_status meta as top level property to the response.
  61          $data['wp_pattern_sync_status'] = isset( $data['meta']['wp_pattern_sync_status'] ) ? $data['meta']['wp_pattern_sync_status'] : '';
  62          unset( $data['meta']['wp_pattern_sync_status'] );
  63          return $data;
  64      }
  65  
  66      /**
  67       * Retrieves the pattern's schema, conforming to JSON Schema.
  68       *
  69       * @since 5.0.0
  70       *
  71       * @return array Item schema data.
  72       */
  73  	public function get_item_schema() {
  74          if ( $this->schema ) {
  75              return $this->add_additional_fields_schema( $this->schema );
  76          }
  77  
  78          $schema = parent::get_item_schema();
  79  
  80          /*
  81           * Allow all contexts to access `title.raw` and `content.raw`.
  82           * Clients always need the raw markup of a pattern to do anything useful,
  83           * e.g. parse it or display it in an editor.
  84           */
  85          $schema['properties']['title']['properties']['raw']['context']   = array( 'view', 'edit' );
  86          $schema['properties']['content']['properties']['raw']['context'] = array( 'view', 'edit' );
  87  
  88          /*
  89           * Remove `title.rendered` and `content.rendered` from the schema.
  90           * It doesn't make sense for a pattern to have rendered content on its own,
  91           * since rendering a block requires it to be inside a post or a page.
  92           */
  93          unset( $schema['properties']['title']['properties']['rendered'] );
  94          unset( $schema['properties']['content']['properties']['rendered'] );
  95  
  96          $this->schema = $schema;
  97  
  98          return $this->add_additional_fields_schema( $this->schema );
  99      }
 100  }


Generated : Sat Dec 21 08:20:01 2024 Cross-referenced by PHPXref