Make WordPress Core


Ignore:
Timestamp:
09/14/2022 10:50:26 AM (4 years ago)
Author:
gziolo
Message:

Blocks: Allow registering multiple items for all supported asset types

Follow-up #54337, [52069]. Part of https://github.com/WordPress/gutenberg/issues/41236. More details in https://github.com/WordPress/gutenberg/issues/33542.

Allow passing more than one script per block for editorScript, script, and viewScript fields in the block.json metadata file. This aligns with the previously added changes for style and editorStyle fields.

This change impacts the WP_Block_Type class and the REST API endpoint for block types. To ensure backward compatibiliy old names were soft deprecated in favor of new fields that work with array values and have _handles suffix.

Props zieladam, dlh, timothyblynjacobs, aristath, bernhard-reiter.
Fixes #56408.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/rest-api/endpoints/class-wp-rest-block-types-controller.php

    r53760 r54155  
    257257                }
    258258
    259                 $schema       = $this->get_item_schema();
    260                 $extra_fields = array(
    261                         'api_version',
    262                         'name',
    263                         'title',
    264                         'description',
    265                         'icon',
    266                         'category',
    267                         'keywords',
    268                         'parent',
    269                         'ancestor',
    270                         'provides_context',
    271                         'uses_context',
    272                         'supports',
    273                         'styles',
    274                         'textdomain',
    275                         'example',
     259                $schema            = $this->get_item_schema();
     260                // Fields deprecated in WordPress 6.1, but left in the schema for backwards compatibility.
     261                $deprecated_fields = array(
    276262                        'editor_script',
    277263                        'script',
     
    279265                        'editor_style',
    280266                        'style',
    281                         'variations',
     267                );
     268                $extra_fields      = array_merge(
     269                        array(
     270                                'api_version',
     271                                'name',
     272                                'title',
     273                                'description',
     274                                'icon',
     275                                'category',
     276                                'keywords',
     277                                'parent',
     278                                'ancestor',
     279                                'provides_context',
     280                                'uses_context',
     281                                'supports',
     282                                'styles',
     283                                'textdomain',
     284                                'example',
     285                                'editor_script_handles',
     286                                'script_handles',
     287                                'view_script_handles',
     288                                'editor_style_handles',
     289                                'style_handles',
     290                                'variations',
     291                        ),
     292                        $deprecated_fields
    282293                );
    283294                foreach ( $extra_fields as $extra_field ) {
     
    438449                );
    439450
    440                 $schema = array(
     451                $this->schema = array(
    441452                        '$schema'    => 'http://json-schema.org/draft-04/schema#',
    442453                        'title'      => 'block-type',
    443454                        'type'       => 'object',
    444455                        'properties' => array(
    445                                 'api_version'      => array(
     456                                'api_version'           => array(
    446457                                        'description' => __( 'Version of block API.' ),
    447458                                        'type'        => 'integer',
     
    450461                                        'readonly'    => true,
    451462                                ),
    452                                 'title'            => array(
     463                                'title'                 => array(
    453464                                        'description' => __( 'Title of block type.' ),
    454465                                        'type'        => 'string',
     
    457468                                        'readonly'    => true,
    458469                                ),
    459                                 'name'             => array(
     470                                'name'                  => array(
    460471                                        'description' => __( 'Unique name identifying the block type.' ),
    461472                                        'type'        => 'string',
     
    464475                                        'readonly'    => true,
    465476                                ),
    466                                 'description'      => array(
     477                                'description'           => array(
    467478                                        'description' => __( 'Description of block type.' ),
    468479                                        'type'        => 'string',
     
    471482                                        'readonly'    => true,
    472483                                ),
    473                                 'icon'             => $icon_definition,
    474                                 'attributes'       => array(
     484                                'icon'                  => $icon_definition,
     485                                'attributes'            => array(
    475486                                        'description'          => __( 'Block attributes.' ),
    476487                                        'type'                 => array( 'object', 'null' ),
     
    483494                                        'readonly'             => true,
    484495                                ),
    485                                 'provides_context' => array(
     496                                'provides_context'      => array(
    486497                                        'description'          => __( 'Context provided by blocks of this type.' ),
    487498                                        'type'                 => 'object',
     
    494505                                        'readonly'             => true,
    495506                                ),
    496                                 'uses_context'     => array(
     507                                'uses_context'          => array(
    497508                                        'description' => __( 'Context values inherited by blocks of this type.' ),
    498509                                        'type'        => 'array',
     
    504515                                        'readonly'    => true,
    505516                                ),
    506                                 'supports'         => array(
     517                                'supports'              => array(
    507518                                        'description' => __( 'Block supports.' ),
    508519                                        'type'        => 'object',
     
    512523                                        'readonly'    => true,
    513524                                ),
    514                                 'category'         => $category_definition,
    515                                 'is_dynamic'       => array(
     525                                'category'              => $category_definition,
     526                                'is_dynamic'            => array(
    516527                                        'description' => __( 'Is the block dynamically rendered.' ),
    517528                                        'type'        => 'boolean',
     
    520531                                        'readonly'    => true,
    521532                                ),
    522                                 'editor_script'    => array(
    523                                         'description' => __( 'Editor script handle.' ),
    524                                         'type'        => array( 'string', 'null' ),
    525                                         'default'     => null,
    526                                         'context'     => array( 'embed', 'view', 'edit' ),
    527                                         'readonly'    => true,
    528                                 ),
    529                                 'script'           => array(
    530                                         'description' => __( 'Public facing and editor script handle.' ),
    531                                         'type'        => array( 'string', 'null' ),
    532                                         'default'     => null,
    533                                         'context'     => array( 'embed', 'view', 'edit' ),
    534                                         'readonly'    => true,
    535                                 ),
    536                                 'view_script'      => array(
    537                                         'description' => __( 'Public facing script handle.' ),
    538                                         'type'        => array( 'string', 'null' ),
    539                                         'default'     => null,
    540                                         'context'     => array( 'embed', 'view', 'edit' ),
    541                                         'readonly'    => true,
    542                                 ),
    543                                 'editor_style'     => array(
    544                                         'description' => __( 'Editor style handle.' ),
    545                                         'type'        => array( 'string', 'null' ),
    546                                         'default'     => null,
    547                                         'context'     => array( 'embed', 'view', 'edit' ),
    548                                         'readonly'    => true,
    549                                 ),
    550                                 'style'            => array(
    551                                         'description' => __( 'Public facing and editor style handle.' ),
    552                                         'type'        => array( 'string', 'null' ),
    553                                         'default'     => null,
    554                                         'context'     => array( 'embed', 'view', 'edit' ),
    555                                         'readonly'    => true,
    556                                 ),
    557                                 'styles'           => array(
     533                                'editor_script_handles' => array(
     534                                        'description' => __( 'Editor script handles.' ),
     535                                        'type'        => array( 'array' ),
     536                                        'default'     => array(),
     537                                        'items'       => array(
     538                                                'type' => 'string',
     539                                        ),
     540                                        'context'     => array( 'embed', 'view', 'edit' ),
     541                                        'readonly'    => true,
     542                                ),
     543                                'script_handles'        => array(
     544                                        'description' => __( 'Public facing and editor script handles.' ),
     545                                        'type'        => array( 'array' ),
     546                                        'default'     => array(),
     547                                        'items'       => array(
     548                                                'type' => 'string',
     549                                        ),
     550                                        'context'     => array( 'embed', 'view', 'edit' ),
     551                                        'readonly'    => true,
     552                                ),
     553                                'view_script_handles'   => array(
     554                                        'description' => __( 'Public facing script handles.' ),
     555                                        'type'        => array( 'array' ),
     556                                        'default'     => array(),
     557                                        'items'       => array(
     558                                                'type' => 'string',
     559                                        ),
     560                                        'context'     => array( 'embed', 'view', 'edit' ),
     561                                        'readonly'    => true,
     562                                ),
     563                                'editor_style_handles'  => array(
     564                                        'description' => __( 'Editor style handles.' ),
     565                                        'type'        => array( 'array' ),
     566                                        'default'     => array(),
     567                                        'items'       => array(
     568                                                'type' => 'string',
     569                                        ),
     570                                        'context'     => array( 'embed', 'view', 'edit' ),
     571                                        'readonly'    => true,
     572                                ),
     573                                'style_handles'         => array(
     574                                        'description' => __( 'Public facing and editor style handles.' ),
     575                                        'type'        => array( 'array' ),
     576                                        'default'     => array(),
     577                                        'items'       => array(
     578                                                'type' => 'string',
     579                                        ),
     580                                        'context'     => array( 'embed', 'view', 'edit' ),
     581                                        'readonly'    => true,
     582                                ),
     583                                'styles'                => array(
    558584                                        'description' => __( 'Block style variations.' ),
    559585                                        'type'        => 'array',
     
    584610                                        'readonly'    => true,
    585611                                ),
    586                                 'variations'       => array(
     612                                'variations'            => array(
    587613                                        'description' => __( 'Block variations.' ),
    588614                                        'type'        => 'array',
     
    636662                                        'default'     => null,
    637663                                ),
    638                                 'textdomain'       => array(
     664                                'textdomain'            => array(
    639665                                        'description' => __( 'Public text domain.' ),
    640666                                        'type'        => array( 'string', 'null' ),
     
    643669                                        'readonly'    => true,
    644670                                ),
    645                                 'parent'           => array(
     671                                'parent'                => array(
    646672                                        'description' => __( 'Parent blocks.' ),
    647673                                        'type'        => array( 'array', 'null' ),
     
    653679                                        'readonly'    => true,
    654680                                ),
    655                                 'ancestor'         => array(
     681                                'ancestor'              => array(
    656682                                        'description' => __( 'Ancestor blocks.' ),
    657683                                        'type'        => array( 'array', 'null' ),
     
    668694                );
    669695
    670                 $this->schema = $schema;
     696                // Properties deprecated in WordPress 6.1, but left in the schema for backwards compatibility.
     697                $deprecated_properties = array(
     698                        'editor_script' => array(
     699                                'description' => __( 'Editor script handle. DEPRECATED: Use `editor_script_handles` instead.' ),
     700                                'type'        => array( 'string', 'null' ),
     701                                'default'     => null,
     702                                'context'     => array( 'embed', 'view', 'edit' ),
     703                                'readonly'    => true,
     704                        ),
     705                        'script'        => array(
     706                                'description' => __( 'Public facing and editor script handle. DEPRECATED: Use `script_handles` instead.' ),
     707                                'type'        => array( 'string', 'null' ),
     708                                'default'     => null,
     709                                'context'     => array( 'embed', 'view', 'edit' ),
     710                                'readonly'    => true,
     711                        ),
     712                        'view_script'   => array(
     713                                'description' => __( 'Public facing script handle. DEPRECATED: Use `view_script_handles` instead.' ),
     714                                'type'        => array( 'string', 'null' ),
     715                                'default'     => null,
     716                                'context'     => array( 'embed', 'view', 'edit' ),
     717                                'readonly'    => true,
     718                        ),
     719                        'editor_style'  => array(
     720                                'description' => __( 'Editor style handle. DEPRECATED: Use `editor_style_handles` instead.' ),
     721                                'type'        => array( 'string', 'null' ),
     722                                'default'     => null,
     723                                'context'     => array( 'embed', 'view', 'edit' ),
     724                                'readonly'    => true,
     725                        ),
     726                        'style'         => array(
     727                                'description' => __( 'Public facing and editor style handle. DEPRECATED: Use `style_handles` instead.' ),
     728                                'type'        => array( 'string', 'null' ),
     729                                'default'     => null,
     730                                'context'     => array( 'embed', 'view', 'edit' ),
     731                                'readonly'    => true,
     732                        ),
     733                );
     734                $this->schema['properties'] = array_merge( $this->schema['properties'], $deprecated_properties );
    671735
    672736                return $this->add_additional_fields_schema( $this->schema );
Note: See TracChangeset for help on using the changeset viewer.