[ Index ]

PHP Cross Reference of WordPress Trunk (Updated Daily)

Search

title

Body

[close]

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

   1  <?php
   2  /**
   3   * REST API: WP_REST_Block_Types_Controller class
   4   *
   5   * @package WordPress
   6   * @subpackage REST_API
   7   * @since 5.5.0
   8   */
   9  
  10  /**
  11   * Core class used to access block types via the REST API.
  12   *
  13   * @since 5.5.0
  14   *
  15   * @see WP_REST_Controller
  16   */
  17  class WP_REST_Block_Types_Controller extends WP_REST_Controller {
  18  
  19      const NAME_PATTERN = '^[a-z][a-z0-9-]*/[a-z][a-z0-9-]*$';
  20  
  21      /**
  22       * Instance of WP_Block_Type_Registry.
  23       *
  24       * @since 5.5.0
  25       * @var WP_Block_Type_Registry
  26       */
  27      protected $block_registry;
  28  
  29      /**
  30       * Instance of WP_Block_Styles_Registry.
  31       *
  32       * @since 5.5.0
  33       * @var WP_Block_Styles_Registry
  34       */
  35      protected $style_registry;
  36  
  37      /**
  38       * Constructor.
  39       *
  40       * @since 5.5.0
  41       */
  42  	public function __construct() {
  43          $this->namespace      = 'wp/v2';
  44          $this->rest_base      = 'block-types';
  45          $this->block_registry = WP_Block_Type_Registry::get_instance();
  46          $this->style_registry = WP_Block_Styles_Registry::get_instance();
  47      }
  48  
  49      /**
  50       * Registers the routes for block types.
  51       *
  52       * @since 5.5.0
  53       *
  54       * @see register_rest_route()
  55       */
  56  	public function register_routes() {
  57  
  58          register_rest_route(
  59              $this->namespace,
  60              '/' . $this->rest_base,
  61              array(
  62                  array(
  63                      'methods'             => WP_REST_Server::READABLE,
  64                      'callback'            => array( $this, 'get_items' ),
  65                      'permission_callback' => array( $this, 'get_items_permissions_check' ),
  66                      'args'                => $this->get_collection_params(),
  67                  ),
  68                  'schema' => array( $this, 'get_public_item_schema' ),
  69              )
  70          );
  71  
  72          register_rest_route(
  73              $this->namespace,
  74              '/' . $this->rest_base . '/(?P<namespace>[a-zA-Z0-9_-]+)',
  75              array(
  76                  array(
  77                      'methods'             => WP_REST_Server::READABLE,
  78                      'callback'            => array( $this, 'get_items' ),
  79                      'permission_callback' => array( $this, 'get_items_permissions_check' ),
  80                      'args'                => $this->get_collection_params(),
  81                  ),
  82                  'schema' => array( $this, 'get_public_item_schema' ),
  83              )
  84          );
  85  
  86          register_rest_route(
  87              $this->namespace,
  88              '/' . $this->rest_base . '/(?P<namespace>[a-zA-Z0-9_-]+)/(?P<name>[a-zA-Z0-9_-]+)',
  89              array(
  90                  'args'   => array(
  91                      'name'      => array(
  92                          'description' => __( 'Block name.' ),
  93                          'type'        => 'string',
  94                      ),
  95                      'namespace' => array(
  96                          'description' => __( 'Block namespace.' ),
  97                          'type'        => 'string',
  98                      ),
  99                  ),
 100                  array(
 101                      'methods'             => WP_REST_Server::READABLE,
 102                      'callback'            => array( $this, 'get_item' ),
 103                      'permission_callback' => array( $this, 'get_item_permissions_check' ),
 104                      'args'                => array(
 105                          'context' => $this->get_context_param( array( 'default' => 'view' ) ),
 106                      ),
 107                  ),
 108                  'schema' => array( $this, 'get_public_item_schema' ),
 109              )
 110          );
 111      }
 112  
 113      /**
 114       * Checks whether a given request has permission to read post block types.
 115       *
 116       * @since 5.5.0
 117       *
 118       * @param WP_REST_Request $request Full details about the request.
 119       * @return true|WP_Error True if the request has read access, WP_Error object otherwise.
 120       */
 121  	public function get_items_permissions_check( $request ) {
 122          return $this->check_read_permission();
 123      }
 124  
 125      /**
 126       * Retrieves all post block types, depending on user context.
 127       *
 128       * @since 5.5.0
 129       *
 130       * @param WP_REST_Request $request Full details about the request.
 131       * @return WP_REST_Response|WP_Error Response object on success, or WP_Error object on failure.
 132       */
 133  	public function get_items( $request ) {
 134          $data        = array();
 135          $block_types = $this->block_registry->get_all_registered();
 136  
 137          // Retrieve the list of registered collection query parameters.
 138          $registered = $this->get_collection_params();
 139          $namespace  = '';
 140          if ( isset( $registered['namespace'] ) && ! empty( $request['namespace'] ) ) {
 141              $namespace = $request['namespace'];
 142          }
 143  
 144          foreach ( $block_types as $obj ) {
 145              if ( $namespace ) {
 146                  list ( $block_namespace ) = explode( '/', $obj->name );
 147  
 148                  if ( $namespace !== $block_namespace ) {
 149                      continue;
 150                  }
 151              }
 152              $block_type = $this->prepare_item_for_response( $obj, $request );
 153              $data[]     = $this->prepare_response_for_collection( $block_type );
 154          }
 155  
 156          return rest_ensure_response( $data );
 157      }
 158  
 159      /**
 160       * Checks if a given request has access to read a block type.
 161       *
 162       * @since 5.5.0
 163       *
 164       * @param WP_REST_Request $request Full details about the request.
 165       * @return true|WP_Error True if the request has read access for the item, WP_Error object otherwise.
 166       */
 167  	public function get_item_permissions_check( $request ) {
 168          $check = $this->check_read_permission();
 169          if ( is_wp_error( $check ) ) {
 170              return $check;
 171          }
 172          $block_name = sprintf( '%s/%s', $request['namespace'], $request['name'] );
 173          $block_type = $this->get_block( $block_name );
 174          if ( is_wp_error( $block_type ) ) {
 175              return $block_type;
 176          }
 177  
 178          return true;
 179      }
 180  
 181      /**
 182       * Checks whether a given block type should be visible.
 183       *
 184       * @since 5.5.0
 185       *
 186       * @return true|WP_Error True if the block type is visible, WP_Error otherwise.
 187       */
 188  	protected function check_read_permission() {
 189          if ( current_user_can( 'edit_posts' ) ) {
 190              return true;
 191          }
 192          foreach ( get_post_types( array( 'show_in_rest' => true ), 'objects' ) as $post_type ) {
 193              if ( current_user_can( $post_type->cap->edit_posts ) ) {
 194                  return true;
 195              }
 196          }
 197  
 198          return new WP_Error( 'rest_block_type_cannot_view', __( 'Sorry, you are not allowed to manage block types.' ), array( 'status' => rest_authorization_required_code() ) );
 199      }
 200  
 201      /**
 202       * Get the block, if the name is valid.
 203       *
 204       * @since 5.5.0
 205       *
 206       * @param string $name Block name.
 207       * @return WP_Block_Type|WP_Error Block type object if name is valid, WP_Error otherwise.
 208       */
 209  	protected function get_block( $name ) {
 210          $block_type = $this->block_registry->get_registered( $name );
 211          if ( empty( $block_type ) ) {
 212              return new WP_Error( 'rest_block_type_invalid', __( 'Invalid block type.' ), array( 'status' => 404 ) );
 213          }
 214  
 215          return $block_type;
 216      }
 217  
 218      /**
 219       * Retrieves a specific block type.
 220       *
 221       * @since 5.5.0
 222       *
 223       * @param WP_REST_Request $request Full details about the request.
 224       * @return WP_REST_Response|WP_Error Response object on success, or WP_Error object on failure.
 225       */
 226  	public function get_item( $request ) {
 227          $block_name = sprintf( '%s/%s', $request['namespace'], $request['name'] );
 228          $block_type = $this->get_block( $block_name );
 229          if ( is_wp_error( $block_type ) ) {
 230              return $block_type;
 231          }
 232          $data = $this->prepare_item_for_response( $block_type, $request );
 233  
 234          return rest_ensure_response( $data );
 235      }
 236  
 237      /**
 238       * Prepares a block type object for serialization.
 239       *
 240       * @since 5.5.0
 241       * @since 5.9.0 Renamed `$block_type` to `$item` to match parent class for PHP 8 named parameter support.
 242       * @since 6.3.0 Added `selectors` field.
 243       * @since 6.5.0 Added `view_script_module_ids` field.
 244       *
 245       * @param WP_Block_Type   $item    Block type data.
 246       * @param WP_REST_Request $request Full details about the request.
 247       * @return WP_REST_Response Block type data.
 248       */
 249  	public function prepare_item_for_response( $item, $request ) {
 250          // Restores the more descriptive, specific name for use within this method.
 251          $block_type = $item;
 252  
 253          $fields = $this->get_fields_for_response( $request );
 254          $data   = array();
 255  
 256          if ( rest_is_field_included( 'attributes', $fields ) ) {
 257              $data['attributes'] = $block_type->get_attributes();
 258          }
 259  
 260          if ( rest_is_field_included( 'is_dynamic', $fields ) ) {
 261              $data['is_dynamic'] = $block_type->is_dynamic();
 262          }
 263  
 264          $schema = $this->get_item_schema();
 265          // Fields deprecated in WordPress 6.1, but left in the schema for backwards compatibility.
 266          $deprecated_fields = array(
 267              'editor_script',
 268              'script',
 269              'view_script',
 270              'editor_style',
 271              'style',
 272          );
 273          $extra_fields      = array_merge(
 274              array(
 275                  'api_version',
 276                  'name',
 277                  'title',
 278                  'description',
 279                  'icon',
 280                  'category',
 281                  'keywords',
 282                  'parent',
 283                  'ancestor',
 284                  'allowed_blocks',
 285                  'provides_context',
 286                  'uses_context',
 287                  'selectors',
 288                  'supports',
 289                  'styles',
 290                  'textdomain',
 291                  'example',
 292                  'editor_script_handles',
 293                  'script_handles',
 294                  'view_script_handles',
 295                  'view_script_module_ids',
 296                  'editor_style_handles',
 297                  'style_handles',
 298                  'view_style_handles',
 299                  'variations',
 300                  'block_hooks',
 301              ),
 302              $deprecated_fields
 303          );
 304          foreach ( $extra_fields as $extra_field ) {
 305              if ( rest_is_field_included( $extra_field, $fields ) ) {
 306                  if ( isset( $block_type->$extra_field ) ) {
 307                      $field = $block_type->$extra_field;
 308                      if ( in_array( $extra_field, $deprecated_fields, true ) && is_array( $field ) ) {
 309                          // Since the schema only allows strings or null (but no arrays), we return the first array item.
 310                          $field = ! empty( $field ) ? array_shift( $field ) : '';
 311                      }
 312                  } elseif ( array_key_exists( 'default', $schema['properties'][ $extra_field ] ) ) {
 313                      $field = $schema['properties'][ $extra_field ]['default'];
 314                  } else {
 315                      $field = '';
 316                  }
 317                  $data[ $extra_field ] = rest_sanitize_value_from_schema( $field, $schema['properties'][ $extra_field ] );
 318              }
 319          }
 320  
 321          if ( rest_is_field_included( 'styles', $fields ) ) {
 322              $styles         = $this->style_registry->get_registered_styles_for_block( $block_type->name );
 323              $styles         = array_values( $styles );
 324              $data['styles'] = wp_parse_args( $styles, $data['styles'] );
 325              $data['styles'] = array_filter( $data['styles'] );
 326          }
 327  
 328          $context = ! empty( $request['context'] ) ? $request['context'] : 'view';
 329          $data    = $this->add_additional_fields_to_object( $data, $request );
 330          $data    = $this->filter_response_by_context( $data, $context );
 331  
 332          $response = rest_ensure_response( $data );
 333  
 334          if ( rest_is_field_included( '_links', $fields ) || rest_is_field_included( '_embedded', $fields ) ) {
 335              $response->add_links( $this->prepare_links( $block_type ) );
 336          }
 337  
 338          /**
 339           * Filters a block type returned from the REST API.
 340           *
 341           * Allows modification of the block type data right before it is returned.
 342           *
 343           * @since 5.5.0
 344           *
 345           * @param WP_REST_Response $response   The response object.
 346           * @param WP_Block_Type    $block_type The original block type object.
 347           * @param WP_REST_Request  $request    Request used to generate the response.
 348           */
 349          return apply_filters( 'rest_prepare_block_type', $response, $block_type, $request );
 350      }
 351  
 352      /**
 353       * Prepares links for the request.
 354       *
 355       * @since 5.5.0
 356       *
 357       * @param WP_Block_Type $block_type Block type data.
 358       * @return array Links for the given block type.
 359       */
 360  	protected function prepare_links( $block_type ) {
 361          list( $namespace ) = explode( '/', $block_type->name );
 362  
 363          $links = array(
 364              'collection' => array(
 365                  'href' => rest_url( sprintf( '%s/%s', $this->namespace, $this->rest_base ) ),
 366              ),
 367              'self'       => array(
 368                  'href' => rest_url( sprintf( '%s/%s/%s', $this->namespace, $this->rest_base, $block_type->name ) ),
 369              ),
 370              'up'         => array(
 371                  'href' => rest_url( sprintf( '%s/%s/%s', $this->namespace, $this->rest_base, $namespace ) ),
 372              ),
 373          );
 374  
 375          if ( $block_type->is_dynamic() ) {
 376              $links['https://api.w.org/render-block'] = array(
 377                  'href' => add_query_arg(
 378                      'context',
 379                      'edit',
 380                      rest_url( sprintf( '%s/%s/%s', 'wp/v2', 'block-renderer', $block_type->name ) )
 381                  ),
 382              );
 383          }
 384  
 385          return $links;
 386      }
 387  
 388      /**
 389       * Retrieves the block type' schema, conforming to JSON Schema.
 390       *
 391       * @since 5.5.0
 392       * @since 6.3.0 Added `selectors` field.
 393       *
 394       * @return array Item schema data.
 395       */
 396  	public function get_item_schema() {
 397          if ( $this->schema ) {
 398              return $this->add_additional_fields_schema( $this->schema );
 399          }
 400  
 401          // rest_validate_value_from_schema doesn't understand $refs, pull out reused definitions for readability.
 402          $inner_blocks_definition = array(
 403              'description' => __( 'The list of inner blocks used in the example.' ),
 404              'type'        => 'array',
 405              'items'       => array(
 406                  'type'       => 'object',
 407                  'properties' => array(
 408                      'name'        => array(
 409                          'description' => __( 'The name of the inner block.' ),
 410                          'type'        => 'string',
 411                          'pattern'     => self::NAME_PATTERN,
 412                          'required'    => true,
 413                      ),
 414                      'attributes'  => array(
 415                          'description' => __( 'The attributes of the inner block.' ),
 416                          'type'        => 'object',
 417                      ),
 418                      'innerBlocks' => array(
 419                          'description' => __( "A list of the inner block's own inner blocks. This is a recursive definition following the parent innerBlocks schema." ),
 420                          'type'        => 'array',
 421                      ),
 422                  ),
 423              ),
 424          );
 425  
 426          $example_definition = array(
 427              'description' => __( 'Block example.' ),
 428              'type'        => array( 'object', 'null' ),
 429              'default'     => null,
 430              'properties'  => array(
 431                  'attributes'  => array(
 432                      'description' => __( 'The attributes used in the example.' ),
 433                      'type'        => 'object',
 434                  ),
 435                  'innerBlocks' => $inner_blocks_definition,
 436              ),
 437              'context'     => array( 'embed', 'view', 'edit' ),
 438              'readonly'    => true,
 439          );
 440  
 441          $keywords_definition = array(
 442              'description' => __( 'Block keywords.' ),
 443              'type'        => 'array',
 444              'items'       => array(
 445                  'type' => 'string',
 446              ),
 447              'default'     => array(),
 448              'context'     => array( 'embed', 'view', 'edit' ),
 449              'readonly'    => true,
 450          );
 451  
 452          $icon_definition = array(
 453              'description' => __( 'Icon of block type.' ),
 454              'type'        => array( 'string', 'null' ),
 455              'default'     => null,
 456              'context'     => array( 'embed', 'view', 'edit' ),
 457              'readonly'    => true,
 458          );
 459  
 460          $category_definition = array(
 461              'description' => __( 'Block category.' ),
 462              'type'        => array( 'string', 'null' ),
 463              'default'     => null,
 464              'context'     => array( 'embed', 'view', 'edit' ),
 465              'readonly'    => true,
 466          );
 467  
 468          $this->schema = array(
 469              '$schema'    => 'http://json-schema.org/draft-04/schema#',
 470              'title'      => 'block-type',
 471              'type'       => 'object',
 472              'properties' => array(
 473                  'api_version'            => array(
 474                      'description' => __( 'Version of block API.' ),
 475                      'type'        => 'integer',
 476                      'default'     => 1,
 477                      'context'     => array( 'embed', 'view', 'edit' ),
 478                      'readonly'    => true,
 479                  ),
 480                  'title'                  => array(
 481                      'description' => __( 'Title of block type.' ),
 482                      'type'        => 'string',
 483                      'default'     => '',
 484                      'context'     => array( 'embed', 'view', 'edit' ),
 485                      'readonly'    => true,
 486                  ),
 487                  'name'                   => array(
 488                      'description' => __( 'Unique name identifying the block type.' ),
 489                      'type'        => 'string',
 490                      'pattern'     => self::NAME_PATTERN,
 491                      'required'    => true,
 492                      'context'     => array( 'embed', 'view', 'edit' ),
 493                      'readonly'    => true,
 494                  ),
 495                  'description'            => array(
 496                      'description' => __( 'Description of block type.' ),
 497                      'type'        => 'string',
 498                      'default'     => '',
 499                      'context'     => array( 'embed', 'view', 'edit' ),
 500                      'readonly'    => true,
 501                  ),
 502                  'icon'                   => $icon_definition,
 503                  'attributes'             => array(
 504                      'description'          => __( 'Block attributes.' ),
 505                      'type'                 => array( 'object', 'null' ),
 506                      'properties'           => array(),
 507                      'default'              => null,
 508                      'additionalProperties' => array(
 509                          'type' => 'object',
 510                      ),
 511                      'context'              => array( 'embed', 'view', 'edit' ),
 512                      'readonly'             => true,
 513                  ),
 514                  'provides_context'       => array(
 515                      'description'          => __( 'Context provided by blocks of this type.' ),
 516                      'type'                 => 'object',
 517                      'properties'           => array(),
 518                      'additionalProperties' => array(
 519                          'type' => 'string',
 520                      ),
 521                      'default'              => array(),
 522                      'context'              => array( 'embed', 'view', 'edit' ),
 523                      'readonly'             => true,
 524                  ),
 525                  'uses_context'           => array(
 526                      'description' => __( 'Context values inherited by blocks of this type.' ),
 527                      'type'        => 'array',
 528                      'default'     => array(),
 529                      'items'       => array(
 530                          'type' => 'string',
 531                      ),
 532                      'context'     => array( 'embed', 'view', 'edit' ),
 533                      'readonly'    => true,
 534                  ),
 535                  'selectors'              => array(
 536                      'description' => __( 'Custom CSS selectors.' ),
 537                      'type'        => 'object',
 538                      'default'     => array(),
 539                      'properties'  => array(),
 540                      'context'     => array( 'embed', 'view', 'edit' ),
 541                      'readonly'    => true,
 542                  ),
 543                  'supports'               => array(
 544                      'description' => __( 'Block supports.' ),
 545                      'type'        => 'object',
 546                      'default'     => array(),
 547                      'properties'  => array(),
 548                      'context'     => array( 'embed', 'view', 'edit' ),
 549                      'readonly'    => true,
 550                  ),
 551                  'category'               => $category_definition,
 552                  'is_dynamic'             => array(
 553                      'description' => __( 'Is the block dynamically rendered.' ),
 554                      'type'        => 'boolean',
 555                      'default'     => false,
 556                      'context'     => array( 'embed', 'view', 'edit' ),
 557                      'readonly'    => true,
 558                  ),
 559                  'editor_script_handles'  => array(
 560                      'description' => __( 'Editor script handles.' ),
 561                      'type'        => array( 'array' ),
 562                      'default'     => array(),
 563                      'items'       => array(
 564                          'type' => 'string',
 565                      ),
 566                      'context'     => array( 'embed', 'view', 'edit' ),
 567                      'readonly'    => true,
 568                  ),
 569                  'script_handles'         => array(
 570                      'description' => __( 'Public facing and editor script handles.' ),
 571                      'type'        => array( 'array' ),
 572                      'default'     => array(),
 573                      'items'       => array(
 574                          'type' => 'string',
 575                      ),
 576                      'context'     => array( 'embed', 'view', 'edit' ),
 577                      'readonly'    => true,
 578                  ),
 579                  'view_script_handles'    => array(
 580                      'description' => __( 'Public facing script handles.' ),
 581                      'type'        => array( 'array' ),
 582                      'default'     => array(),
 583                      'items'       => array(
 584                          'type' => 'string',
 585                      ),
 586                      'context'     => array( 'embed', 'view', 'edit' ),
 587                      'readonly'    => true,
 588                  ),
 589                  'view_script_module_ids' => array(
 590                      'description' => __( 'Public facing script module IDs.' ),
 591                      'type'        => array( 'array' ),
 592                      'default'     => array(),
 593                      'items'       => array(
 594                          'type' => 'string',
 595                      ),
 596                      'context'     => array( 'embed', 'view', 'edit' ),
 597                      'readonly'    => true,
 598                  ),
 599                  'editor_style_handles'   => array(
 600                      'description' => __( 'Editor style handles.' ),
 601                      'type'        => array( 'array' ),
 602                      'default'     => array(),
 603                      'items'       => array(
 604                          'type' => 'string',
 605                      ),
 606                      'context'     => array( 'embed', 'view', 'edit' ),
 607                      'readonly'    => true,
 608                  ),
 609                  'style_handles'          => array(
 610                      'description' => __( 'Public facing and editor style handles.' ),
 611                      'type'        => array( 'array' ),
 612                      'default'     => array(),
 613                      'items'       => array(
 614                          'type' => 'string',
 615                      ),
 616                      'context'     => array( 'embed', 'view', 'edit' ),
 617                      'readonly'    => true,
 618                  ),
 619                  'view_style_handles'     => array(
 620                      'description' => __( 'Public facing style handles.' ),
 621                      'type'        => array( 'array' ),
 622                      'default'     => array(),
 623                      'items'       => array(
 624                          'type' => 'string',
 625                      ),
 626                      'context'     => array( 'embed', 'view', 'edit' ),
 627                      'readonly'    => true,
 628                  ),
 629                  'styles'                 => array(
 630                      'description' => __( 'Block style variations.' ),
 631                      'type'        => 'array',
 632                      'items'       => array(
 633                          'type'       => 'object',
 634                          'properties' => array(
 635                              'name'         => array(
 636                                  'description' => __( 'Unique name identifying the style.' ),
 637                                  'type'        => 'string',
 638                                  'required'    => true,
 639                              ),
 640                              'label'        => array(
 641                                  'description' => __( 'The human-readable label for the style.' ),
 642                                  'type'        => 'string',
 643                              ),
 644                              'inline_style' => array(
 645                                  'description' => __( 'Inline CSS code that registers the CSS class required for the style.' ),
 646                                  'type'        => 'string',
 647                              ),
 648                              'style_handle' => array(
 649                                  'description' => __( 'Contains the handle that defines the block style.' ),
 650                                  'type'        => 'string',
 651                              ),
 652                          ),
 653                      ),
 654                      'default'     => array(),
 655                      'context'     => array( 'embed', 'view', 'edit' ),
 656                      'readonly'    => true,
 657                  ),
 658                  'variations'             => array(
 659                      'description' => __( 'Block variations.' ),
 660                      'type'        => 'array',
 661                      'items'       => array(
 662                          'type'       => 'object',
 663                          'properties' => array(
 664                              'name'        => array(
 665                                  'description' => __( 'The unique and machine-readable name.' ),
 666                                  'type'        => 'string',
 667                                  'required'    => true,
 668                              ),
 669                              'title'       => array(
 670                                  'description' => __( 'A human-readable variation title.' ),
 671                                  'type'        => 'string',
 672                                  'required'    => true,
 673                              ),
 674                              'description' => array(
 675                                  'description' => __( 'A detailed variation description.' ),
 676                                  'type'        => 'string',
 677                                  'required'    => false,
 678                              ),
 679                              'category'    => $category_definition,
 680                              'icon'        => $icon_definition,
 681                              'isDefault'   => array(
 682                                  'description' => __( 'Indicates whether the current variation is the default one.' ),
 683                                  'type'        => 'boolean',
 684                                  'required'    => false,
 685                                  'default'     => false,
 686                              ),
 687                              'attributes'  => array(
 688                                  'description' => __( 'The initial values for attributes.' ),
 689                                  'type'        => 'object',
 690                              ),
 691                              'innerBlocks' => $inner_blocks_definition,
 692                              'example'     => $example_definition,
 693                              'scope'       => array(
 694                                  'description' => __( 'The list of scopes where the variation is applicable. When not provided, it assumes all available scopes.' ),
 695                                  'type'        => array( 'array', 'null' ),
 696                                  'default'     => null,
 697                                  'items'       => array(
 698                                      'type' => 'string',
 699                                      'enum' => array( 'block', 'inserter', 'transform' ),
 700                                  ),
 701                                  'readonly'    => true,
 702                              ),
 703                              'keywords'    => $keywords_definition,
 704                          ),
 705                      ),
 706                      'readonly'    => true,
 707                      'context'     => array( 'embed', 'view', 'edit' ),
 708                      'default'     => null,
 709                  ),
 710                  'textdomain'             => array(
 711                      'description' => __( 'Public text domain.' ),
 712                      'type'        => array( 'string', 'null' ),
 713                      'default'     => null,
 714                      'context'     => array( 'embed', 'view', 'edit' ),
 715                      'readonly'    => true,
 716                  ),
 717                  'parent'                 => array(
 718                      'description' => __( 'Parent blocks.' ),
 719                      'type'        => array( 'array', 'null' ),
 720                      'items'       => array(
 721                          'type'    => 'string',
 722                          'pattern' => self::NAME_PATTERN,
 723                      ),
 724                      'default'     => null,
 725                      'context'     => array( 'embed', 'view', 'edit' ),
 726                      'readonly'    => true,
 727                  ),
 728                  'ancestor'               => array(
 729                      'description' => __( 'Ancestor blocks.' ),
 730                      'type'        => array( 'array', 'null' ),
 731                      'items'       => array(
 732                          'type'    => 'string',
 733                          'pattern' => self::NAME_PATTERN,
 734                      ),
 735                      'default'     => null,
 736                      'context'     => array( 'embed', 'view', 'edit' ),
 737                      'readonly'    => true,
 738                  ),
 739                  'allowed_blocks'         => array(
 740                      'description' => __( 'Allowed child block types.' ),
 741                      'type'        => array( 'array', 'null' ),
 742                      'items'       => array(
 743                          'type'    => 'string',
 744                          'pattern' => self::NAME_PATTERN,
 745                      ),
 746                      'default'     => null,
 747                      'context'     => array( 'embed', 'view', 'edit' ),
 748                      'readonly'    => true,
 749                  ),
 750                  'keywords'               => $keywords_definition,
 751                  'example'                => $example_definition,
 752                  'block_hooks'            => array(
 753                      'description'       => __( 'This block is automatically inserted near any occurrence of the block types used as keys of this map, into a relative position given by the corresponding value.' ),
 754                      'type'              => 'object',
 755                      'patternProperties' => array(
 756                          self::NAME_PATTERN => array(
 757                              'type' => 'string',
 758                              'enum' => array( 'before', 'after', 'first_child', 'last_child' ),
 759                          ),
 760                      ),
 761                      'default'           => array(),
 762                      'context'           => array( 'embed', 'view', 'edit' ),
 763                      'readonly'          => true,
 764                  ),
 765              ),
 766          );
 767  
 768          // Properties deprecated in WordPress 6.1, but left in the schema for backwards compatibility.
 769          $deprecated_properties      = array(
 770              'editor_script' => array(
 771                  'description' => __( 'Editor script handle. DEPRECATED: Use `editor_script_handles` instead.' ),
 772                  'type'        => array( 'string', 'null' ),
 773                  'default'     => null,
 774                  'context'     => array( 'embed', 'view', 'edit' ),
 775                  'readonly'    => true,
 776              ),
 777              'script'        => array(
 778                  'description' => __( 'Public facing and editor script handle. DEPRECATED: Use `script_handles` instead.' ),
 779                  'type'        => array( 'string', 'null' ),
 780                  'default'     => null,
 781                  'context'     => array( 'embed', 'view', 'edit' ),
 782                  'readonly'    => true,
 783              ),
 784              'view_script'   => array(
 785                  'description' => __( 'Public facing script handle. DEPRECATED: Use `view_script_handles` instead.' ),
 786                  'type'        => array( 'string', 'null' ),
 787                  'default'     => null,
 788                  'context'     => array( 'embed', 'view', 'edit' ),
 789                  'readonly'    => true,
 790              ),
 791              'editor_style'  => array(
 792                  'description' => __( 'Editor style handle. DEPRECATED: Use `editor_style_handles` instead.' ),
 793                  'type'        => array( 'string', 'null' ),
 794                  'default'     => null,
 795                  'context'     => array( 'embed', 'view', 'edit' ),
 796                  'readonly'    => true,
 797              ),
 798              'style'         => array(
 799                  'description' => __( 'Public facing and editor style handle. DEPRECATED: Use `style_handles` instead.' ),
 800                  'type'        => array( 'string', 'null' ),
 801                  'default'     => null,
 802                  'context'     => array( 'embed', 'view', 'edit' ),
 803                  'readonly'    => true,
 804              ),
 805          );
 806          $this->schema['properties'] = array_merge( $this->schema['properties'], $deprecated_properties );
 807  
 808          return $this->add_additional_fields_schema( $this->schema );
 809      }
 810  
 811      /**
 812       * Retrieves the query params for collections.
 813       *
 814       * @since 5.5.0
 815       *
 816       * @return array Collection parameters.
 817       */
 818  	public function get_collection_params() {
 819          return array(
 820              'context'   => $this->get_context_param( array( 'default' => 'view' ) ),
 821              'namespace' => array(
 822                  'description' => __( 'Block namespace.' ),
 823                  'type'        => 'string',
 824              ),
 825          );
 826      }
 827  }


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