Make WordPress Core

Changeset 54155


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.

Location:
trunk
Files:
10 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/block-editor.php

    r53419 r54155  
    325325
    326326        foreach ( $block_registry->get_all_registered() as $block_type ) {
    327                 if ( ! empty( $block_type->style ) ) {
    328                         $style_handles[] = $block_type->style;
    329                 }
    330 
    331                 if ( ! empty( $block_type->editor_style ) ) {
    332                         $style_handles[] = $block_type->editor_style;
    333                 }
    334 
    335                 if ( ! empty( $block_type->script ) ) {
    336                         $script_handles[] = $block_type->script;
    337                 }
     327                $style_handles = array_merge(
     328                        $style_handles,
     329                        $block_type->style_handles,
     330                        $block_type->editor_style_handles
     331                );
     332
     333                $script_handles = array_merge(
     334                        $script_handles,
     335                        $block_type->script_handles
     336                );
    338337        }
    339338
  • trunk/src/wp-includes/blocks.php

    r54138 r54155  
    3636 *
    3737 * @since 5.5.0
     38 * @since 6.1.0 Added `$index` parameter.
    3839 *
    3940 * @param string $block_name Name of the block.
    4041 * @param string $field_name Name of the metadata field.
     42 * @param int    $index      Optional. Index of the asset when multiple items passed.
     43 *                           Default 0.
    4144 * @return string Generated asset name for the block's field.
    4245 */
    43 function generate_block_asset_handle( $block_name, $field_name ) {
     46function generate_block_asset_handle( $block_name, $field_name, $index = 0 ) {
    4447        if ( 0 === strpos( $block_name, 'core/' ) ) {
    4548                $asset_handle = str_replace( 'core/', 'wp-block-', $block_name );
    … …  
    4952                if ( 0 === strpos( $field_name, 'view' ) ) {
    5053                        $asset_handle .= '-view';
     54                }
     55                if ( $index > 0 ) {
     56                        $asset_handle .= '-' . ( $index + 1 );
    5157                }
    5258                return $asset_handle;
    … …  
    6066                'style'        => 'style',
    6167        );
    62         return str_replace( '/', '-', $block_name ) .
     68        $asset_handle   = str_replace( '/', '-', $block_name ) .
    6369                '-' . $field_mappings[ $field_name ];
     70        if ( $index > 0 ) {
     71                $asset_handle .= '-' . ( $index + 1 );
     72        }
     73        return $asset_handle;
    6474}
    6575
    … …  
    7181 *
    7282 * @since 5.5.0
     83 * @since 6.1.0 Added `$index` parameter.
    7384 *
    7485 * @param array  $metadata   Block metadata.
    7586 * @param string $field_name Field name to pick from metadata.
     87 * @param int    $index      Optional. Index of the script to register when multiple items passed.
     88 *                           Default 0.
    7689 * @return string|false Script handle provided directly or created through
    7790 *                      script's registration, or false on failure.
    7891 */
    79 function register_block_script_handle( $metadata, $field_name ) {
     92function register_block_script_handle( $metadata, $field_name, $index = 0 ) {
    8093        if ( empty( $metadata[ $field_name ] ) ) {
    8194                return false;
    8295        }
     96
    8397        $script_handle = $metadata[ $field_name ];
    84         $script_path   = remove_block_asset_path_prefix( $metadata[ $field_name ] );
     98        if ( is_array( $script_handle ) ) {
     99                if ( empty( $script_handle[ $index ] ) ) {
     100                        return false;
     101                }
     102                $script_handle = $script_handle[ $index ];
     103        }
     104
     105        $script_path = remove_block_asset_path_prefix( $script_handle );
    85106        if ( $script_handle === $script_path ) {
    86107                return $script_handle;
    87108        }
    88109
    89         $script_handle     = generate_block_asset_handle( $metadata['name'], $field_name );
     110        $script_handle     = generate_block_asset_handle( $metadata['name'], $field_name, $index );
    90111        $script_asset_path = wp_normalize_path(
    91112                realpath(
    … …  
    146167 *
    147168 * @since 5.5.0
     169 * @since 6.1.0 Added `$index` parameter.
    148170 *
    149171 * @param array  $metadata   Block metadata.
    150172 * @param string $field_name Field name to pick from metadata.
     173 * @param int    $index      Optional. Index of the style to register when multiple items passed.
     174 *                           Default 0.
    151175 * @return string|false Style handle provided directly or created through
    152176 *                      style's registration, or false on failure.
    153177 */
    154 function register_block_style_handle( $metadata, $field_name ) {
     178function register_block_style_handle( $metadata, $field_name, $index = 0 ) {
    155179        if ( empty( $metadata[ $field_name ] ) ) {
    156180                return false;
    157181        }
     182
    158183        $wpinc_path_norm = wp_normalize_path( realpath( ABSPATH . WPINC ) );
    159184        $theme_path_norm = wp_normalize_path( get_theme_file_path() );
    160185        $is_core_block   = isset( $metadata['file'] ) && 0 === strpos( $metadata['file'], $wpinc_path_norm );
     186        // Skip registering individual styles for each core block when a bundled version provided.
    161187        if ( $is_core_block && ! wp_should_load_separate_core_block_assets() ) {
    162188                return false;
    163189        }
    164190
     191        $style_handle = $metadata[ $field_name ];
     192        if ( is_array( $style_handle ) ) {
     193                if ( empty( $style_handle[ $index ] ) ) {
     194                        return false;
     195                }
     196                $style_handle = $style_handle[ $index ];
     197        }
     198
     199        $style_path      = remove_block_asset_path_prefix( $style_handle );
     200        $is_style_handle = $style_handle === $style_path;
     201        // Allow only passing style handles for core blocks.
     202        if ( $is_core_block && ! $is_style_handle ) {
     203                return false;
     204        }
     205        // Return the style handle unless it's the first item for every core block that requires special treatment.
     206        if ( $is_style_handle && ! ( $is_core_block && 0 === $index ) ) {
     207                return $style_handle;
     208        }
     209
    165210        // Check whether styles should have a ".min" suffix or not.
    166         $suffix = SCRIPT_DEBUG ? '' : '.min';
    167 
    168         $style_handle = $metadata[ $field_name ];
    169         $style_path   = remove_block_asset_path_prefix( $metadata[ $field_name ] );
    170 
    171         if ( $style_handle === $style_path && ! $is_core_block ) {
    172                 return $style_handle;
    173         }
    174 
     211        $suffix    = SCRIPT_DEBUG ? '' : '.min';
    175212        $style_uri = plugins_url( $style_path, $metadata['file'] );
    176213        if ( $is_core_block ) {
    … …  
    186223        }
    187224
    188         $style_handle   = generate_block_asset_handle( $metadata['name'], $field_name );
     225        $style_handle   = generate_block_asset_handle( $metadata['name'], $field_name, $index );
    189226        $block_dir      = dirname( $metadata['file'] );
    190         $style_file     = realpath( "$block_dir/$style_path" );
     227        $style_file     = wp_normalize_path( realpath( "$block_dir/$style_path" ) );
    191228        $has_style_file = false !== $style_file;
    192229        $version        = ! $is_core_block && isset( $metadata['version'] ) ? $metadata['version'] : false;
    … …  
    312349        }
    313350
    314         if ( ! empty( $metadata['editorScript'] ) ) {
    315                 $settings['editor_script'] = register_block_script_handle(
    316                         $metadata,
    317                         'editorScript'
    318                 );
    319         }
    320 
    321         if ( ! empty( $metadata['script'] ) ) {
    322                 $settings['script'] = register_block_script_handle(
    323                         $metadata,
    324                         'script'
    325                 );
    326         }
    327 
    328         if ( ! empty( $metadata['viewScript'] ) ) {
    329                 $settings['view_script'] = register_block_script_handle(
    330                         $metadata,
    331                         'viewScript'
    332                 );
    333         }
    334 
    335         if ( ! empty( $metadata['editorStyle'] ) ) {
    336                 $settings['editor_style'] = register_block_style_handle(
    337                         $metadata,
    338                         'editorStyle'
    339                 );
    340         }
    341 
    342         if ( ! empty( $metadata['style'] ) ) {
    343                 $settings['style'] = register_block_style_handle(
    344                         $metadata,
    345                         'style'
    346                 );
     351        $script_fields = array(
     352                'editorScript' => 'editor_script_handles',
     353                'script'       => 'script_handles',
     354                'viewScript'   => 'view_script_handles',
     355        );
     356        foreach ( $script_fields as $metadata_field_name => $settings_field_name ) {
     357                if ( ! empty( $metadata[ $metadata_field_name ] ) ) {
     358                        $scripts           = $metadata[ $metadata_field_name ];
     359                        $processed_scripts = array();
     360                        if ( is_array( $scripts ) ) {
     361                                for ( $index = 0; $index < count( $scripts ); $index++ ) {
     362                                        $result = register_block_script_handle(
     363                                                $metadata,
     364                                                $metadata_field_name,
     365                                                $index
     366                                        );
     367                                        if ( $result ) {
     368                                                $processed_scripts[] = $result;
     369                                        }
     370                                }
     371                        } else {
     372                                $result = register_block_script_handle(
     373                                        $metadata,
     374                                        $metadata_field_name
     375                                );
     376                                if ( $result ) {
     377                                        $processed_scripts[] = $result;
     378                                }
     379                        }
     380                        $settings[ $settings_field_name ] = $processed_scripts;
     381                }
     382        }
     383
     384        $style_fields = array(
     385                'editorStyle' => 'editor_style_handles',
     386                'style'       => 'style_handles',
     387        );
     388        foreach ( $style_fields as $metadata_field_name => $settings_field_name ) {
     389                if ( ! empty( $metadata[ $metadata_field_name ] ) ) {
     390                        $styles           = $metadata[ $metadata_field_name ];
     391                        $processed_styles = array();
     392                        if ( is_array( $styles ) ) {
     393                                for ( $index = 0; $index < count( $styles ); $index++ ) {
     394                                        $result = register_block_style_handle(
     395                                                $metadata,
     396                                                $metadata_field_name,
     397                                                $index
     398                                        );
     399                                        if ( $result ) {
     400                                                $processed_styles[] = $result;
     401                                        }
     402                                }
     403                        } else {
     404                                $result = register_block_style_handle(
     405                                        $metadata,
     406                                        $metadata_field_name
     407                                );
     408                                if ( $result ) {
     409                                        $processed_styles[] = $result;
     410                                }
     411                        }
     412                        $settings[ $settings_field_name ] = $processed_styles;
     413                }
    347414        }
    348415
    … …  
    12631330
    12641331/**
    1265  * Allows multiple block styles.
    1266  *
    1267  * @since 5.9.0
    1268  *
    1269  * @param array $metadata Metadata for registering a block type.
    1270  * @return array Metadata for registering a block type.
    1271  */
    1272 function _wp_multiple_block_styles( $metadata ) {
    1273         foreach ( array( 'style', 'editorStyle' ) as $key ) {
    1274                 if ( ! empty( $metadata[ $key ] ) && is_array( $metadata[ $key ] ) ) {
    1275                         $default_style = array_shift( $metadata[ $key ] );
    1276                         foreach ( $metadata[ $key ] as $handle ) {
    1277                                 $args = array( 'handle' => $handle );
    1278                                 if ( 0 === strpos( $handle, 'file:' ) && isset( $metadata['file'] ) ) {
    1279                                         $style_path      = remove_block_asset_path_prefix( $handle );
    1280                                         $theme_path_norm = wp_normalize_path( get_theme_file_path() );
    1281                                         $style_path_norm = wp_normalize_path( realpath( dirname( $metadata['file'] ) . '/' . $style_path ) );
    1282                                         $is_theme_block  = isset( $metadata['file'] ) && 0 === strpos( $metadata['file'], $theme_path_norm );
    1283 
    1284                                         $style_uri = plugins_url( $style_path, $metadata['file'] );
    1285 
    1286                                         if ( $is_theme_block ) {
    1287                                                 $style_uri = get_theme_file_uri( str_replace( $theme_path_norm, '', $style_path_norm ) );
    1288                                         }
    1289 
    1290                                         $args = array(
    1291                                                 'handle' => sanitize_key( "{$metadata['name']}-{$style_path}" ),
    1292                                                 'src'    => $style_uri,
    1293                                         );
    1294                                 }
    1295 
    1296                                 wp_enqueue_block_style( $metadata['name'], $args );
    1297                         }
    1298 
    1299                         // Only return the 1st item in the array.
    1300                         $metadata[ $key ] = $default_style;
    1301                 }
    1302         }
    1303         return $metadata;
    1304 }
    1305 add_filter( 'block_type_metadata', '_wp_multiple_block_styles' );
    1306 
    1307 /**
    13081332 * Helper function that constructs a comment query vars array from the passed
    13091333 * block properties.
  • trunk/src/wp-includes/class-wp-block-type.php

    r54133 r54155  
    167167
    168168        /**
    169          * Block type editor only script handle.
    170          *
    171          * @since 5.0.0
    172          * @var string|null
    173          */
    174         public $editor_script = null;
    175 
    176         /**
    177          * Block type front end and editor script handle.
    178          *
    179          * @since 5.0.0
    180          * @var string|null
    181          */
    182         public $script = null;
    183 
    184         /**
    185          * Block type front end only script handle.
    186          *
    187          * @since 5.9.0
    188          * @var string|null
    189          */
    190         public $view_script = null;
    191 
    192         /**
    193          * Block type editor only style handle.
    194          *
    195          * @since 5.0.0
    196          * @var string|null
    197          */
    198         public $editor_style = null;
    199 
    200         /**
    201          * Block type front end and editor style handle.
    202          *
    203          * @since 5.0.0
    204          * @var string|null
    205          */
    206         public $style = null;
     169         * Block type editor only script handles.
     170         *
     171         * @since 6.1.0
     172         * @var string[]
     173         */
     174        public $editor_script_handles = array();
     175
     176        /**
     177         * Block type front end and editor script handles.
     178         *
     179         * @since 6.1.0
     180         * @var string[]
     181         */
     182        public $script_handles = array();
     183
     184        /**
     185         * Block type front end only script handles.
     186         *
     187         * @since 6.1.0
     188         * @var string[]
     189         */
     190        public $view_script_handles = array();
     191
     192        /**
     193         * Block type editor only style handles.
     194         *
     195         * @since 6.1.0
     196         * @var string[]
     197         */
     198        public $editor_style_handles = array();
     199
     200        /**
     201         * Block type front end and editor style handles.
     202         *
     203         * @since 6.1.0
     204         * @var string[]
     205         */
     206        public $style_handles = array();
     207
     208        /**
     209         * Deprecated block type properties for script and style handles.
     210         *
     211         * @since 6.1.0
     212         * @var string[]
     213         */
     214        private $deprecated_properties = array(
     215                'editor_script',
     216                'script',
     217                'view_script',
     218                'editor_style',
     219                'style',
     220        );
    207221
    208222        /**
    … …  
    229243         * @since 5.9.0 Added the `view_script` property.
    230244         * @since 6.0.0 Added the `ancestor` property.
     245         * @since 6.1.0 Added the `editor_script_handles`, `script_handles`, `view_script_handles,
     246         *              `editor_style_handles`, and `style_handles` properties.
     247         *              Deprecated the `editor_script`, `script`, `view_script`, `editor_style`, and `style` properties.
    231248         *
    232249         * @see register_block_type()
    … …  
    237254         *     however the ones described below are supported by default. Default empty array.
    238255         *
    239          *     @type string        $api_version      Block API version.
    240          *     @type string        $title            Human-readable block type label.
    241          *     @type string|null   $category         Block type category classification, used in
    242          *                                           search interfaces to arrange block types by category.
    243          *     @type string[]|null $parent           Setting parent lets a block require that it is only
    244          *                                           available when nested within the specified blocks.
    245          *     @type string[]|null $ancestor         Setting ancestor makes a block available only inside the specified
    246          *                                           block types at any position of the ancestor's block subtree.
    247          *     @type string|null   $icon             Block type icon.
    248          *     @type string        $description      A detailed block type description.
    249          *     @type string[]      $keywords         Additional keywords to produce block type as
    250          *                                           result in search interfaces.
    251          *     @type string|null   $textdomain       The translation textdomain.
    252          *     @type array[]       $styles           Alternative block styles.
    253          *     @type array[]       $variations       Block variations.
    254          *     @type array|null    $supports         Supported features.
    255          *     @type array|null    $example          Structured data for the block preview.
    256          *     @type callable|null $render_callback  Block type render callback.
    257          *     @type array|null    $attributes       Block type attributes property schemas.
    258          *     @type string[]      $uses_context     Context values inherited by blocks of this type.
    259          *     @type string[]|null $provides_context Context provided by blocks of this type.
    260          *     @type string|null   $editor_script    Block type editor only script handle.
    261          *     @type string|null   $script           Block type front end and editor script handle.
    262          *     @type string|null   $view_script      Block type front end only script handle.
    263          *     @type string|null   $editor_style     Block type editor only style handle.
    264          *     @type string|null   $style            Block type front end and editor style handle.
     256         *     @type string        $api_version              Block API version.
     257         *     @type string        $title                    Human-readable block type label.
     258         *     @type string|null   $category                 Block type category classification, used in
     259         *                                                   search interfaces to arrange block types by category.
     260         *     @type string[]|null $parent                   Setting parent lets a block require that it is only
     261         *                                                   available when nested within the specified blocks.
     262         *     @type string[]|null $ancestor                 Setting ancestor makes a block available only inside the specified
     263         *                                                   block types at any position of the ancestor's block subtree.
     264         *     @type string|null   $icon                     Block type icon.
     265         *     @type string        $description              A detailed block type description.
     266         *     @type string[]      $keywords                 Additional keywords to produce block type as
     267         *                                                   result in search interfaces.
     268         *     @type string|null   $textdomain               The translation textdomain.
     269         *     @type array[]       $styles                   Alternative block styles.
     270         *     @type array[]       $variations               Block variations.
     271         *     @type array|null    $supports                 Supported features.
     272         *     @type array|null    $example                  Structured data for the block preview.
     273         *     @type callable|null $render_callback          Block type render callback.
     274         *     @type array|null    $attributes               Block type attributes property schemas.
     275         *     @type string[]      $uses_context             Context values inherited by blocks of this type.
     276         *     @type string[]|null $provides_context         Context provided by blocks of this type.
     277         *     @type string[]      $editor_script_handles    Block type editor only script handles.
     278         *     @type string[]      $script_handles           Block type front end and editor script handles.
     279         *     @type string[]      $view_script_handles      Block type front end only script handles.
     280         *     @type string[]      $editor_style_handles     Block type editor only style handles.
     281         *     @type string[]      $style_handles            Block type front end and editor style handles.
    265282         * }
    266283         */
    … …  
    269286
    270287                $this->set_props( $args );
     288        }
     289
     290        /**
     291         * Proxies getting values for deprecated properties for script and style handles for backward compatibility.
     292         * Gets the value for the corresponding new property if the first item in the array provided.
     293         *
     294         * @since 6.1.0
     295         *
     296         * @param string $name Deprecated property name.
     297         *
     298         * @return string|null|void The value read from the new property if the first item in the array provided,
     299         *                          null when value not found, or void when unknown property name provided.
     300         */
     301        public function __get( $name ) {
     302                if ( ! in_array( $name, $this->deprecated_properties ) ) {
     303                        return;
     304                }
     305
     306                $new_name = $name . '_handles';
     307                return isset( $this->{$new_name }[0] ) ? $this->{$new_name }[0] : null;
     308        }
     309
     310        /**
     311         * Proxies checking for deprecated properties for script and style handles for backward compatibility.
     312         * Checks whether the corresponding new property has the first item in the array provided.
     313         *
     314         * @since 6.1.0
     315         *
     316         * @param string $name Deprecated property name.
     317         *
     318         * @return boolean Returns true when for the new property the first item in the array exists,
     319         *                     or false otherwise.
     320         */
     321        public function __isset( $name ) {
     322                if ( ! in_array( $name, $this->deprecated_properties ) ) {
     323                        return false;
     324                }
     325
     326                $new_name = $name . '_handles';
     327                return isset( $this->{$new_name }[0] );
     328        }
     329
     330        /**
     331         * Proxies setting values for deprecated properties for script and style handles for backward compatibility.
     332         * Sets the value for the corresponding new property as the first item in the array.
     333         * It also allows setting custom properties for backward compatibility.
     334         *
     335         * @since 6.1.0
     336         *
     337         * @param string $name  Property name.
     338         * @param mixed  $value Property value.
     339         */
     340        public function __set( $name, $value ) {
     341                if ( ! in_array( $name, $this->deprecated_properties ) ) {
     342                        $this->{$name} = $value;
     343                        return;
     344                }
     345
     346                if ( ! is_string( $value ) ) {
     347                        return;
     348                }
     349
     350                $new_name = $name . '_handles';
     351                $this->{$new_name }[0] = $value;
    271352        }
    272353
  • trunk/src/wp-includes/class-wp-block.php

    r54133 r54155  
    261261                }
    262262
    263                 if ( ! empty( $this->block_type->script ) ) {
    264                         wp_enqueue_script( $this->block_type->script );
    265                 }
    266 
    267                 if ( ! empty( $this->block_type->view_script ) && empty( $this->block_type->render_callback ) ) {
    268                         wp_enqueue_script( $this->block_type->view_script );
    269                 }
    270 
    271                 if ( ! empty( $this->block_type->style ) ) {
    272                         wp_enqueue_style( $this->block_type->style );
     263                if ( ( ! empty( $this->block_type->script_handles ) ) ) {
     264                        foreach ( $this->block_type->script_handles as $script_handle ) {
     265                                wp_enqueue_script( $script_handle );
     266                        }
     267                }
     268
     269                if ( ! empty( $this->block_type->view_script_handles ) && empty( $this->block_type->render_callback ) ) {
     270                        foreach ( $this->block_type->view_script_handles as $view_script_handle ) {
     271                                wp_enqueue_script( $view_script_handle );
     272                        }
     273                }
     274
     275                if ( ( ! empty( $this->block_type->style_handles ) ) ) {
     276                        foreach ( $this->block_type->style_handles as $style_handle ) {
     277                                wp_enqueue_style( $style_handle );
     278                        }
    273279                }
    274280
  • trunk/src/wp-includes/deprecated.php

    r53874 r54155  
    44434443        return false;
    44444444}
     4445
     4446/**
     4447  * Allows multiple block styles.
     4448  *
     4449  * @since 5.9.0
     4450  * @deprecated 6.1.0
     4451  *
     4452  * @param array $metadata Metadata for registering a block type.
     4453  * @return array Metadata for registering a block type.
     4454  */
     4455  function _wp_multiple_block_styles( $metadata ) {
     4456        _deprecated_function( __FUNCTION__, '6.1.0' );
     4457        return $metadata;
     4458}
  • 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 );
  • trunk/src/wp-includes/script-loader.php

    r54118 r54155  
    25322532        }
    25332533
    2534         $load_editor_scripts = is_admin() && wp_should_load_block_editor_scripts_and_styles();
     2534        $load_editor_scripts_and_styles = is_admin() && wp_should_load_block_editor_scripts_and_styles();
    25352535
    25362536        $block_registry = WP_Block_Type_Registry::get_instance();
    25372537        foreach ( $block_registry->get_all_registered() as $block_name => $block_type ) {
    2538                 // Front-end styles.
    2539                 if ( ! empty( $block_type->style ) ) {
    2540                         wp_enqueue_style( $block_type->style );
    2541                 }
    2542 
    2543                 // Front-end script.
    2544                 if ( ! empty( $block_type->script ) ) {
    2545                         wp_enqueue_script( $block_type->script );
    2546                 }
    2547 
    2548                 // Editor styles.
    2549                 if ( $load_editor_scripts && ! empty( $block_type->editor_style ) ) {
    2550                         wp_enqueue_style( $block_type->editor_style );
    2551                 }
    2552 
    2553                 // Editor script.
    2554                 if ( $load_editor_scripts && ! empty( $block_type->editor_script ) ) {
    2555                         wp_enqueue_script( $block_type->editor_script );
     2538                // Front-end and editor styles.
     2539                foreach ( $block_type->style_handles as $style_handle ) {
     2540                        wp_enqueue_style( $style_handle );
     2541                }
     2542
     2543                // Front-end and editor scripts.
     2544                foreach ( $block_type->script_handles as $script_handle ) {
     2545                        wp_enqueue_script( $script_handle );
     2546                }
     2547
     2548                if ( $load_editor_scripts_and_styles ) {
     2549                        // Editor styles.
     2550                        foreach ( $block_type->editor_style_handles as $editor_style_handle ) {
     2551                                wp_enqueue_style( $editor_style_handle );
     2552                        }
     2553
     2554                        // Editor scripts.
     2555                        foreach ( $block_type->editor_script_handles as $editor_script_handle ) {
     2556                                wp_enqueue_script( $editor_script_handle );
     2557                        }
    25562558                }
    25572559        }
  • trunk/tests/phpunit/data/blocks/notice/block.json

    r54132 r54155  
    5858        "editorScript": "tests-notice-editor-script",
    5959        "script": "tests-notice-script",
    60         "viewScript": "tests-notice-view-script",
     60        "viewScript": [ "tests-notice-view-script", "tests-notice-view-script-2" ],
    6161        "editorStyle": "tests-notice-editor-style",
    62         "style": "tests-notice-style",
     62        "style": [ "tests-notice-style", "tests-notice-style-2" ],
    6363        "render": "file:./render.php"
    6464}
  • trunk/tests/phpunit/tests/blocks/register.php

    r54132 r54155  
    149149                $this->assertSame(
    150150                        'unit-tests-my-block-script',
    151                         generate_block_asset_handle( $block_name, 'script' )
    152                 );
    153                 $this->assertSame(
    154                         'unit-tests-my-block-view-script',
    155                         generate_block_asset_handle( $block_name, 'viewScript' )
    156                 );
    157                 $this->assertSame(
    158                         'unit-tests-my-block-editor-style',
    159                         generate_block_asset_handle( $block_name, 'editorStyle' )
     151                        generate_block_asset_handle( $block_name, 'script', 0 )
     152                );
     153                $this->assertSame(
     154                        'unit-tests-my-block-view-script-100',
     155                        generate_block_asset_handle( $block_name, 'viewScript', 99 )
     156                );
     157                $this->assertSame(
     158                        'unit-tests-my-block-editor-style-2',
     159                        generate_block_asset_handle( $block_name, 'editorStyle', 1 )
    160160                );
    161161                $this->assertSame(
    … …  
    177177                $this->assertSame(
    178178                        'wp-block-paragraph',
    179                         generate_block_asset_handle( $block_name, 'script' )
    180                 );
    181                 $this->assertSame(
    182                         'wp-block-paragraph-view',
    183                         generate_block_asset_handle( $block_name, 'viewScript' )
    184                 );
    185                 $this->assertSame(
    186                         'wp-block-paragraph-editor',
    187                         generate_block_asset_handle( $block_name, 'editorStyle' )
     179                        generate_block_asset_handle( $block_name, 'script', 0 )
     180                );
     181                $this->assertSame(
     182                        'wp-block-paragraph-view-100',
     183                        generate_block_asset_handle( $block_name, 'viewScript', 99 )
     184                );
     185                $this->assertSame(
     186                        'wp-block-paragraph-editor-2',
     187                        generate_block_asset_handle( $block_name, 'editorStyle', 1 )
    188188                );
    189189                $this->assertSame(
    … …  
    205205         * @ticket 50263
    206206         */
    207         public function test_empty_value_register_block_script_handle() {
     207        public function test_empty_string_value_do_not_register_block_script_handle() {
    208208                $metadata = array( 'script' => '' );
    209209                $result   = register_block_script_handle( $metadata, 'script' );
     210
     211                $this->assertFalse( $result );
     212        }
     213
     214        public function test_empty_array_value_do_not_register_block_script_handle() {
     215                $metadata = array( 'script' => array() );
     216                $result   = register_block_script_handle( $metadata, 'script' );
     217
     218                $this->assertFalse( $result );
     219        }
     220
     221        public function test_wrong_array_index_do_not_register_block_script_handle() {
     222                $metadata = array( 'script' => array( 'test-script-handle' ) );
     223                $result   = register_block_script_handle( $metadata, 'script', 1 );
    210224
    211225                $this->assertFalse( $result );
    … …  
    232246        public function test_handle_passed_register_block_script_handle() {
    233247                $metadata = array(
    234                         'editorScript' => 'test-script-handle',
    235                 );
    236                 $result   = register_block_script_handle( $metadata, 'editorScript' );
     248                        'script' => 'test-script-handle',
     249                );
     250                $result   = register_block_script_handle( $metadata, 'script' );
    237251
    238252                $this->assertSame( 'test-script-handle', $result );
     253        }
     254
     255        public function test_handles_passed_register_block_script_handles() {
     256                $metadata = array(
     257                        'script' => array( 'test-script-handle', 'test-script-handle-2' ),
     258                );
     259
     260                $result = register_block_script_handle( $metadata, 'script' );
     261                $this->assertSame( 'test-script-handle', $result );
     262
     263                $result = register_block_script_handle( $metadata, 'script', 1 );
     264                $this->assertSame( 'test-script-handle-2', $result, 1 );
    239265        }
    240266
    … …  
    282308         * @ticket 50263
    283309         */
    284         public function test_empty_value_found_register_block_style_handle() {
     310        public function test_empty_string_value_do_not_register_block_style_handle() {
    285311                $metadata = array( 'style' => '' );
    286312                $result   = register_block_style_handle( $metadata, 'style' );
    … …  
    289315        }
    290316
     317        public function test_empty_array_value_do_not_register_block_style_handle() {
     318                $metadata = array( 'style' => array() );
     319                $result   = register_block_style_handle( $metadata, 'style' );
     320
     321                $this->assertFalse( $result );
     322        }
     323
     324        public function test_wrong_array_index_do_not_register_block_style_handle() {
     325                $metadata = array( 'style' => array( 'test-style-handle' ) );
     326                $result   = register_block_style_handle( $metadata, 'style', 1 );
     327
     328                $this->assertFalse( $result );
     329        }
     330
    291331        /**
    292332         * @ticket 50263
    … …  
    299339
    300340                $this->assertSame( 'test-style-handle', $result );
     341        }
     342
     343        public function test_handles_passed_register_block_style_handles() {
     344                $metadata = array(
     345                        'style' => array( 'test-style-handle', 'test-style-handle-2' ),
     346                );
     347
     348                $result = register_block_style_handle( $metadata, 'style' );
     349                $this->assertSame( 'test-style-handle', $result );
     350
     351                $result = register_block_style_handle( $metadata, 'style', 1 );
     352                $this->assertSame( 'test-style-handle-2', $result, 1 );
    301353        }
    302354
    … …  
    443495                        $result->example
    444496                );
    445                 $this->assertSame( 'tests-notice-editor-script', $result->editor_script );
    446                 $this->assertSame( 'tests-notice-script', $result->script );
    447                 $this->assertSame( 'tests-notice-view-script', $result->view_script );
    448                 $this->assertSame( 'tests-notice-editor-style', $result->editor_style );
    449                 $this->assertSame( 'tests-notice-style', $result->style );
     497                $this->assertSameSets(
     498                        array( 'tests-notice-editor-script' ),
     499                        $result->editor_script_handles
     500                );
     501                $this->assertSameSets(
     502                        array( 'tests-notice-script' ),
     503                        $result->script_handles
     504                );
     505                $this->assertSameSets(
     506                        array( 'tests-notice-view-script', 'tests-notice-view-script-2' ),
     507                        $result->view_script_handles
     508                );
     509                $this->assertSameSets(
     510                        array( 'tests-notice-editor-style' ),
     511                        $result->editor_style_handles
     512                );
     513                $this->assertSameSets(
     514                        array( 'tests-notice-style', 'tests-notice-style-2' ),
     515                        $result->style_handles
     516                );
    450517
    451518                // @ticket 50328
  • trunk/tests/phpunit/tests/rest-api/rest-block-type-controller.php

    r54058 r54155  
    238238                $this->assertSame( '1', $data['description'] );
    239239                $this->assertNull( $data['icon'] );
    240                 $this->assertNull( $data['editor_script'] );
    241                 $this->assertNull( $data['script'] );
    242                 $this->assertNull( $data['view_script'] );
    243                 $this->assertNull( $data['editor_style'] );
    244                 $this->assertNull( $data['style'] );
     240                $this->assertSameSets( array(), $data['editor_script_handles'] );
     241                $this->assertSameSets( array(), $data['script_handles'] );
     242                $this->assertSameSets( array(), $data['view_script_handles'] );
     243                $this->assertSameSets( array(), $data['editor_style_handles'] );
     244                $this->assertSameSets( array(), $data['style_handles'] );
    245245                $this->assertSameSets( array(), $data['provides_context'] );
    246246                $this->assertSameSetsWithIndex(
    … …  
    261261                $this->assertFalse( $data['is_dynamic'] );
    262262                $this->assertSameSets( array( array() ), $data['variations'] );
     263                // Deprecated properties.
     264                $this->assertNull( $data['editor_script'] );
     265                $this->assertNull( $data['script'] );
     266                $this->assertNull( $data['view_script'] );
     267                $this->assertNull( $data['editor_style'] );
     268                $this->assertNull( $data['style'] );
     269
    263270        }
    264271
    … …  
    300307                $this->assertSame( '', $data['description'] );
    301308                $this->assertNull( $data['icon'] );
    302                 $this->assertNull( $data['editor_script'] );
    303                 $this->assertNull( $data['script'] );
    304                 $this->assertNull( $data['view_script'] );
    305                 $this->assertNull( $data['editor_style'] );
    306                 $this->assertNull( $data['style'] );
     309                $this->assertSameSets( array(), $data['editor_script_handles'] );
     310                $this->assertSameSets( array(), $data['script_handles'] );
     311                $this->assertSameSets( array(), $data['view_script_handles'] );
     312                $this->assertSameSets( array(), $data['editor_style_handles'] );
     313                $this->assertSameSets( array(), $data['style_handles'] );
    307314                $this->assertSameSetsWithIndex(
    308315                        array(
    … …  
    324331                $this->assertFalse( $data['is_dynamic'] );
    325332                $this->assertSameSets( array(), $data['variations'] );
     333                // Deprecated properties.
     334                $this->assertNull( $data['editor_script'] );
     335                $this->assertNull( $data['script'] );
     336                $this->assertNull( $data['view_script'] );
     337                $this->assertNull( $data['editor_style'] );
     338                $this->assertNull( $data['style'] );
    326339        }
    327340
    … …  
    393406                $data       = $response->get_data();
    394407                $properties = $data['schema']['properties'];
    395                 $this->assertCount( 23, $properties );
     408                $this->assertCount( 28, $properties );
    396409                $this->assertArrayHasKey( 'api_version', $properties );
    397410                $this->assertArrayHasKey( 'title', $properties );
    … …  
    406419                $this->assertArrayHasKey( 'category', $properties );
    407420                $this->assertArrayHasKey( 'is_dynamic', $properties );
    408                 $this->assertArrayHasKey( 'editor_script', $properties );
    409                 $this->assertArrayHasKey( 'script', $properties );
    410                 $this->assertArrayHasKey( 'view_script', $properties );
    411                 $this->assertArrayHasKey( 'editor_style', $properties );
    412                 $this->assertArrayHasKey( 'style', $properties );
     421                $this->assertArrayHasKey( 'editor_script_handles', $properties );
     422                $this->assertArrayHasKey( 'script_handles', $properties );
     423                $this->assertArrayHasKey( 'view_script_handles', $properties );
     424                $this->assertArrayHasKey( 'editor_style_handles', $properties );
     425                $this->assertArrayHasKey( 'style_handles', $properties );
    413426                $this->assertArrayHasKey( 'parent', $properties );
    414427                $this->assertArrayHasKey( 'example', $properties );
    … …  
    417430                $this->assertArrayHasKey( 'variations', $properties );
    418431                $this->assertArrayHasKey( 'ancestor', $properties );
     432                // Deprecated properties.
     433                $this->assertArrayHasKey( 'editor_script', $properties );
     434                $this->assertArrayHasKey( 'script', $properties );
     435                $this->assertArrayHasKey( 'view_script', $properties );
     436                $this->assertArrayHasKey( 'editor_style', $properties );
     437                $this->assertArrayHasKey( 'style', $properties );
     438
    419439        }
    420440
    … …  
    519539                        'name',
    520540                        'category',
    521                         'editor_script',
    522                         'script',
    523                         'view_script',
    524                         'editor_style',
    525                         'style',
     541                        'editor_script_handles',
     542                        'script_handles',
     543                        'view_script_handles',
     544                        'editor_style_handles',
     545                        'style_handles',
    526546                        'title',
    527547                        'icon',
    … …  
    535555                        'textdomain',
    536556                        'example',
     557                        // Deprecated fields.
     558                        'editor_script',
     559                        'script',
     560                        'view_script',
     561                        'editor_style',
     562                        'style',
    537563                );
    538564
Note: See TracChangeset for help on using the changeset viewer.