[ Index ]

PHP Cross Reference of WordPress Trunk (Updated Daily)

Search

title

Body

[close]

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

   1  <?php
   2  /**
   3   * REST API: WP_REST_Block_Patterns_Controller class
   4   *
   5   * @package    WordPress
   6   * @subpackage REST_API
   7   * @since      6.0.0
   8   */
   9  
  10  /**
  11   * Core class used to access block patterns via the REST API.
  12   *
  13   * @since 6.0.0
  14   *
  15   * @see WP_REST_Controller
  16   */
  17  class WP_REST_Block_Patterns_Controller extends WP_REST_Controller {
  18  
  19      /**
  20       * Defines whether remote patterns should be loaded.
  21       *
  22       * @since 6.0.0
  23       * @var bool
  24       */
  25      private $remote_patterns_loaded;
  26  
  27      /**
  28       * An array that maps old categories names to new ones.
  29       *
  30       * @since 6.2.0
  31       * @var array
  32       */
  33      protected static $categories_migration = array(
  34          'buttons' => 'call-to-action',
  35          'columns' => 'text',
  36          'query'   => 'posts',
  37      );
  38  
  39      /**
  40       * Constructs the controller.
  41       *
  42       * @since 6.0.0
  43       */
  44  	public function __construct() {
  45          $this->namespace = 'wp/v2';
  46          $this->rest_base = 'block-patterns/patterns';
  47      }
  48  
  49      /**
  50       * Registers the routes for the objects of the controller.
  51       *
  52       * @since 6.0.0
  53       */
  54  	public function register_routes() {
  55          register_rest_route(
  56              $this->namespace,
  57              '/' . $this->rest_base,
  58              array(
  59                  array(
  60                      'methods'             => WP_REST_Server::READABLE,
  61                      'callback'            => array( $this, 'get_items' ),
  62                      'permission_callback' => array( $this, 'get_items_permissions_check' ),
  63                  ),
  64                  'schema' => array( $this, 'get_public_item_schema' ),
  65              )
  66          );
  67      }
  68  
  69      /**
  70       * Checks whether a given request has permission to read block patterns.
  71       *
  72       * @since 6.0.0
  73       *
  74       * @param WP_REST_Request $request Full details about the request.
  75       * @return true|WP_Error True if the request has read access, WP_Error object otherwise.
  76       */
  77  	public function get_items_permissions_check( $request ) {
  78          if ( current_user_can( 'edit_posts' ) ) {
  79              return true;
  80          }
  81  
  82          foreach ( get_post_types( array( 'show_in_rest' => true ), 'objects' ) as $post_type ) {
  83              if ( current_user_can( $post_type->cap->edit_posts ) ) {
  84                  return true;
  85              }
  86          }
  87  
  88          return new WP_Error(
  89              'rest_cannot_view',
  90              __( 'Sorry, you are not allowed to view the registered block patterns.' ),
  91              array( 'status' => rest_authorization_required_code() )
  92          );
  93      }
  94  
  95      /**
  96       * Retrieves all block patterns.
  97       *
  98       * @since 6.0.0
  99       * @since 6.2.0 Added migration for old core pattern categories to the new ones.
 100       *
 101       * @param WP_REST_Request $request Full details about the request.
 102       * @return WP_REST_Response|WP_Error Response object on success, or WP_Error object on failure.
 103       */
 104  	public function get_items( $request ) {
 105          if ( ! $this->remote_patterns_loaded ) {
 106              // Load block patterns from w.org.
 107              _load_remote_block_patterns(); // Patterns with the `core` keyword.
 108              _load_remote_featured_patterns(); // Patterns in the `featured` category.
 109              _register_remote_theme_patterns(); // Patterns requested by current theme.
 110  
 111              $this->remote_patterns_loaded = true;
 112          }
 113  
 114          $response = array();
 115          $patterns = WP_Block_Patterns_Registry::get_instance()->get_all_registered();
 116          foreach ( $patterns as $pattern ) {
 117              $migrated_pattern = $this->migrate_pattern_categories( $pattern );
 118              $prepared_pattern = $this->prepare_item_for_response( $migrated_pattern, $request );
 119              $response[]       = $this->prepare_response_for_collection( $prepared_pattern );
 120          }
 121          return rest_ensure_response( $response );
 122      }
 123  
 124      /**
 125       * Migrates old core pattern categories to the new categories.
 126       *
 127       * Core pattern categories are revamped. Migration is needed to ensure
 128       * backwards compatibility.
 129       *
 130       * @since 6.2.0
 131       *
 132       * @param array $pattern Raw pattern as registered, before applying any changes.
 133       * @return array Migrated pattern.
 134       */
 135  	protected function migrate_pattern_categories( $pattern ) {
 136          // No categories to migrate.
 137          if (
 138              ! isset( $pattern['categories'] ) ||
 139              ! is_array( $pattern['categories'] )
 140          ) {
 141              return $pattern;
 142          }
 143  
 144          foreach ( $pattern['categories'] as $index => $category ) {
 145              // If the category exists as a key, then it needs migration.
 146              if ( isset( static::$categories_migration[ $category ] ) ) {
 147                  $pattern['categories'][ $index ] = static::$categories_migration[ $category ];
 148              }
 149          }
 150  
 151          return $pattern;
 152      }
 153  
 154      /**
 155       * Prepare a raw block pattern before it gets output in a REST API response.
 156       *
 157       * @since 6.0.0
 158       * @since 6.3.0 Added `source` property.
 159       *
 160       * @param array           $item    Raw pattern as registered, before any changes.
 161       * @param WP_REST_Request $request Request object.
 162       * @return WP_REST_Response|WP_Error Response object on success, or WP_Error object on failure.
 163       */
 164  	public function prepare_item_for_response( $item, $request ) {
 165          $fields = $this->get_fields_for_response( $request );
 166          $keys   = array(
 167              'name'          => 'name',
 168              'title'         => 'title',
 169              'content'       => 'content',
 170              'description'   => 'description',
 171              'viewportWidth' => 'viewport_width',
 172              'inserter'      => 'inserter',
 173              'categories'    => 'categories',
 174              'keywords'      => 'keywords',
 175              'blockTypes'    => 'block_types',
 176              'postTypes'     => 'post_types',
 177              'templateTypes' => 'template_types',
 178              'source'        => 'source',
 179          );
 180          $data   = array();
 181          foreach ( $keys as $item_key => $rest_key ) {
 182              if ( isset( $item[ $item_key ] ) && rest_is_field_included( $rest_key, $fields ) ) {
 183                  $data[ $rest_key ] = $item[ $item_key ];
 184              }
 185          }
 186  
 187          $context = ! empty( $request['context'] ) ? $request['context'] : 'view';
 188          $data    = $this->add_additional_fields_to_object( $data, $request );
 189          $data    = $this->filter_response_by_context( $data, $context );
 190          return rest_ensure_response( $data );
 191      }
 192  
 193      /**
 194       * Retrieves the block pattern schema, conforming to JSON Schema.
 195       *
 196       * @since 6.0.0
 197       * @since 6.3.0 Added `source` property.
 198       *
 199       * @return array Item schema data.
 200       */
 201  	public function get_item_schema() {
 202          if ( $this->schema ) {
 203              return $this->add_additional_fields_schema( $this->schema );
 204          }
 205  
 206          $schema = array(
 207              '$schema'    => 'http://json-schema.org/draft-04/schema#',
 208              'title'      => 'block-pattern',
 209              'type'       => 'object',
 210              'properties' => array(
 211                  'name'           => array(
 212                      'description' => __( 'The pattern name.' ),
 213                      'type'        => 'string',
 214                      'readonly'    => true,
 215                      'context'     => array( 'view', 'edit', 'embed' ),
 216                  ),
 217                  'title'          => array(
 218                      'description' => __( 'The pattern title, in human readable format.' ),
 219                      'type'        => 'string',
 220                      'readonly'    => true,
 221                      'context'     => array( 'view', 'edit', 'embed' ),
 222                  ),
 223                  'content'        => array(
 224                      'description' => __( 'The pattern content.' ),
 225                      'type'        => 'string',
 226                      'readonly'    => true,
 227                      'context'     => array( 'view', 'edit', 'embed' ),
 228                  ),
 229                  'description'    => array(
 230                      'description' => __( 'The pattern detailed description.' ),
 231                      'type'        => 'string',
 232                      'readonly'    => true,
 233                      'context'     => array( 'view', 'edit', 'embed' ),
 234                  ),
 235                  'viewport_width' => array(
 236                      'description' => __( 'The pattern viewport width for inserter preview.' ),
 237                      'type'        => 'number',
 238                      'readonly'    => true,
 239                      'context'     => array( 'view', 'edit', 'embed' ),
 240                  ),
 241                  'inserter'       => array(
 242                      'description' => __( 'Determines whether the pattern is visible in inserter.' ),
 243                      'type'        => 'boolean',
 244                      'readonly'    => true,
 245                      'context'     => array( 'view', 'edit', 'embed' ),
 246                  ),
 247                  'categories'     => array(
 248                      'description' => __( 'The pattern category slugs.' ),
 249                      'type'        => 'array',
 250                      'readonly'    => true,
 251                      'context'     => array( 'view', 'edit', 'embed' ),
 252                  ),
 253                  'keywords'       => array(
 254                      'description' => __( 'The pattern keywords.' ),
 255                      'type'        => 'array',
 256                      'readonly'    => true,
 257                      'context'     => array( 'view', 'edit', 'embed' ),
 258                  ),
 259                  'block_types'    => array(
 260                      'description' => __( 'Block types that the pattern is intended to be used with.' ),
 261                      'type'        => 'array',
 262                      'readonly'    => true,
 263                      'context'     => array( 'view', 'edit', 'embed' ),
 264                  ),
 265                  'post_types'     => array(
 266                      'description' => __( 'An array of post types that the pattern is restricted to be used with.' ),
 267                      'type'        => 'array',
 268                      'readonly'    => true,
 269                      'context'     => array( 'view', 'edit', 'embed' ),
 270                  ),
 271                  'template_types' => array(
 272                      'description' => __( 'An array of template types where the pattern fits.' ),
 273                      'type'        => 'array',
 274                      'readonly'    => true,
 275                      'context'     => array( 'view', 'edit', 'embed' ),
 276                  ),
 277                  'source'         => array(
 278                      'description' => __( 'Where the pattern comes from e.g. core' ),
 279                      'type'        => 'string',
 280                      'readonly'    => true,
 281                      'context'     => array( 'view', 'edit', 'embed' ),
 282                      'enum'        => array(
 283                          'core',
 284                          'plugin',
 285                          'theme',
 286                          'pattern-directory/core',
 287                          'pattern-directory/theme',
 288                          'pattern-directory/featured',
 289                      ),
 290                  ),
 291              ),
 292          );
 293  
 294          $this->schema = $schema;
 295  
 296          return $this->add_additional_fields_schema( $this->schema );
 297      }
 298  }


Generated : Thu Apr 25 08:20:02 2024 Cross-referenced by PHPXref