Make WordPress Core


Ignore:
Timestamp:
02/12/2024 11:40:49 AM (4 months ago)
Author:
gziolo
Message:

Blocks: Allow reading the script handle from asset files

In the [documentation for WPDefinedAsset definition](https://developer.wordpress.org/block-editor/reference-guides/block-api/block-metadata/#wpdefinedasset) for block metadata there is a note about the handle. That was missing in WordPress core.

Props gziolo, jsnajdr, youknowriad.
Fixes #60485.

File:
1 edited

Legend:

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

    r57565 r57590  
    192192 * @since 5.5.0
    193193 * @since 6.1.0 Added `$index` parameter.
    194  * @since 6.5.0 The asset file is optional.
     194 * @since 6.5.0 The asset file is optional. Added script handle support in the asset file.
    195195 *
    196196 * @param array  $metadata   Block metadata.
     
    206206    }
    207207
    208     $script_handle = $metadata[ $field_name ];
    209     if ( is_array( $script_handle ) ) {
    210         if ( empty( $script_handle[ $index ] ) ) {
     208    $script_handle_or_path = $metadata[ $field_name ];
     209    if ( is_array( $script_handle_or_path ) ) {
     210        if ( empty( $script_handle_or_path[ $index ] ) ) {
    211211            return false;
    212212        }
    213         $script_handle = $script_handle[ $index ];
    214     }
    215 
    216     $script_path = remove_block_asset_path_prefix( $script_handle );
    217     if ( $script_handle === $script_path ) {
    218         return $script_handle;
     213        $script_handle_or_path = $script_handle_or_path[ $index ];
     214    }
     215
     216    $script_path = remove_block_asset_path_prefix( $script_handle_or_path );
     217    if ( $script_handle_or_path === $script_path ) {
     218        return $script_handle_or_path;
    219219    }
    220220
    221221    $path                  = dirname( $metadata['file'] );
    222222    $script_asset_raw_path = $path . '/' . substr_replace( $script_path, '.asset.php', - strlen( '.js' ) );
    223     $script_handle         = generate_block_asset_handle( $metadata['name'], $field_name, $index );
    224223    $script_asset_path     = wp_normalize_path(
    225224        realpath( $script_asset_raw_path )
    226225    );
    227226
    228     $script_path_norm = wp_normalize_path( realpath( $path . '/' . $script_path ) );
    229     $script_uri       = get_block_asset_url( $script_path_norm );
    230 
    231     $script_args = array();
     227    // Asset file for blocks is optional. See https://core.trac.wordpress.org/ticket/60460.
     228    $script_asset = ! empty( $script_asset_path ) ? require $script_asset_path : array();
     229    $script_handle = isset( $script_asset['handle'] ) ?
     230        $script_asset['handle'] :
     231        generate_block_asset_handle( $metadata['name'], $field_name, $index );
     232    if ( wp_script_is( $script_handle, 'registered' ) ) {
     233        return $script_handle;
     234    }
     235
     236    $script_path_norm    = wp_normalize_path( realpath( $path . '/' . $script_path ) );
     237    $script_uri          = get_block_asset_url( $script_path_norm );
     238    $script_dependencies = isset( $script_asset['dependencies'] ) ? $script_asset['dependencies'] : array();
     239    $block_version       = isset( $metadata['version'] ) ? $metadata['version'] : false;
     240    $script_version      = isset( $script_asset['version'] ) ? $script_asset['version'] : $block_version;
     241    $script_args         = array();
    232242    if ( 'viewScript' === $field_name && $script_uri ) {
    233243        $script_args['strategy'] = 'defer';
    234244    }
    235245
    236     // Asset file for blocks is optional. See https://core.trac.wordpress.org/ticket/60460.
    237     $script_asset        = ! empty( $script_asset_path ) ? require $script_asset_path : array();
    238     $script_dependencies = isset( $script_asset['dependencies'] ) ? $script_asset['dependencies'] : array();
    239     $result              = wp_register_script(
     246    $result = wp_register_script(
    240247        $script_handle,
    241248        $script_uri,
    242249        $script_dependencies,
    243         isset( $script_asset['version'] ) ? $script_asset['version'] : false,
     250        $script_version,
    244251        $script_args
    245252    );
Note: See TracChangeset for help on using the changeset viewer.