Make WordPress Core

Changeset 57590


Ignore:
Timestamp:
02/12/2024 11:40:49 AM (5 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.

Location:
trunk
Files:
2 added
2 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    );
  • trunk/tests/phpunit/tests/blocks/register.php

    r57565 r57590  
    5353     */
    5454    public function tear_down() {
    55         $registry = WP_Block_Type_Registry::get_instance();
    56 
    57         foreach ( array( 'core/test-static', 'core/test-dynamic', 'tests/notice' ) as $block_name ) {
    58             if ( $registry->is_registered( $block_name ) ) {
    59                 $registry->unregister( $block_name );
     55        // Removes test block types registered by test cases.
     56        $block_types = WP_Block_Type_Registry::get_instance()->get_all_registered();
     57        foreach ( $block_types as $block_type ) {
     58            $block_name = $block_type->name;
     59            if ( str_starts_with( $block_name, 'tests/' ) ) {
     60                unregister_block_type( $block_name );
    6061            }
    6162        }
    6263
    6364        foreach ( wp_scripts()->registered as $script_handle => $script ) {
    64             if ( str_starts_with( $script_handle, 'unit-tests-' ) ) {
     65            if ( str_starts_with( $script_handle, 'tests-' ) ) {
    6566                wp_deregister_script( $script_handle );
    6667            }
     
    8384     */
    8485    public function test_register_affects_main_registry() {
    85         $name     = 'core/test-static';
     86        $name     = 'tests/static';
    8687        $settings = array(
    8788            'icon' => 'text',
     
    9899     */
    99100    public function test_unregister_affects_main_registry() {
    100         $name     = 'core/test-static';
     101        $name     = 'tests/static';
    101102        $settings = array(
    102103            'icon' => 'text',
     
    142143     */
    143144    public function test_generate_block_asset_handle() {
    144         $block_name = 'unit-tests/my-block';
    145 
    146         $this->assertSame(
    147             'unit-tests-my-block-editor-script',
     145        $block_name = 'tests/my-block';
     146
     147        $this->assertSame(
     148            'tests-my-block-editor-script',
    148149            generate_block_asset_handle( $block_name, 'editorScript' )
    149150        );
    150151        $this->assertSame(
    151             'unit-tests-my-block-script',
     152            'tests-my-block-script',
    152153            generate_block_asset_handle( $block_name, 'script', 0 )
    153154        );
    154155        $this->assertSame(
    155             'unit-tests-my-block-view-script-100',
     156            'tests-my-block-view-script-100',
    156157            generate_block_asset_handle( $block_name, 'viewScript', 99 )
    157158        );
    158159        $this->assertSame(
    159             'unit-tests-my-block-view-script-module',
     160            'tests-my-block-view-script-module',
    160161            generate_block_asset_handle( $block_name, 'viewScriptModule' )
    161162        );
    162163        $this->assertSame(
    163             'unit-tests-my-block-view-script-module-2',
     164            'tests-my-block-view-script-module-2',
    164165            generate_block_asset_handle( $block_name, 'viewScriptModule', 1 )
    165166        );
    166167        $this->assertSame(
    167             'unit-tests-my-block-view-script-module-100',
     168            'tests-my-block-view-script-module-100',
    168169            generate_block_asset_handle( $block_name, 'viewScriptModule', 99 )
    169170        );
    170171        $this->assertSame(
    171             'unit-tests-my-block-editor-style-2',
     172            'tests-my-block-editor-style-2',
    172173            generate_block_asset_handle( $block_name, 'editorStyle', 1 )
    173174        );
    174175        $this->assertSame(
    175             'unit-tests-my-block-style',
     176            'tests-my-block-style',
    176177            generate_block_asset_handle( $block_name, 'style' )
    177178        );
    178179        // @ticket 59673
    179180        $this->assertSame(
    180             'unit-tests-my-block-view-style',
     181            'tests-my-block-view-style',
    181182            generate_block_asset_handle( $block_name, 'viewStyle' ),
    182183            'asset handle for viewStyle is not generated correctly'
     
    336337        $metadata = array(
    337338            'file'             => __FILE__,
    338             'name'             => 'unit-tests/test-block',
     339            'name'             => 'tests/test-block',
    339340            'viewScriptModule' => 'file:./blocks/notice/missing-asset.js',
    340341        );
    341342        $result   = register_block_script_module_id( $metadata, 'viewScriptModule' );
    342343
    343         $this->assertSame( 'unit-tests-test-block-view-script-module', $result );
     344        $this->assertSame( 'tests-test-block-view-script-module', $result );
    344345    }
    345346
     
    377378        $metadata = array(
    378379            'file'             => DIR_TESTDATA . '/blocks/notice/block.json',
    379             'name'             => 'unit-tests/test-block',
     380            'name'             => 'tests/test-block',
    380381            'viewScriptModule' => 'file:./block.js',
    381382        );
    382383        $result   = register_block_script_module_id( $metadata, 'viewScriptModule' );
    383384
    384         $this->assertSame( 'unit-tests-test-block-view-script-module', $result );
    385 
    386         // Test the behavior directly within the unit test
     385        $this->assertSame( 'tests-test-block-view-script-module', $result );
     386
     387        // Test the behavior directly within the unit test.
    387388        $this->assertFalse(
    388389            strpos(
     
    431432        $metadata = array(
    432433            'file'   => __FILE__,
    433             'name'   => 'unit-tests/test-block',
     434            'name'   => 'tests/test-block',
    434435            'script' => 'file:./blocks/notice/missing-asset.js',
    435436        );
    436437        $result   = register_block_script_handle( $metadata, 'script' );
    437438
    438         $this->assertSame( 'unit-tests-test-block-script', $result );
     439        $this->assertSame( 'tests-test-block-script', $result );
    439440    }
    440441
     
    445446        $metadata = array(
    446447            'file'   => DIR_TESTDATA . '/blocks/notice/block.json',
    447             'name'   => 'unit-tests/test-block',
     448            'name'   => 'tests/test-block',
    448449            'script' => 'file:./block.js',
    449450        );
    450451        $result   = register_block_script_handle( $metadata, 'script' );
    451452
    452         $this->assertSame( 'unit-tests-test-block-script', $result );
    453 
    454         // Test the behavior directly within the unit test
     453        $this->assertSame( 'tests-test-block-script', $result );
     454
     455        // Test the behavior directly within the unit test.
    455456        $this->assertFalse(
    456457            strpos(
     
    465466                trailingslashit( wp_normalize_path( get_stylesheet_directory() ) )
    466467            ) === 0
     468        );
     469    }
     470
     471    /**
     472     * @ticket 60485
     473     */
     474    public function test_success_register_block_script_handle_with_custom_handle_name() {
     475        $custom_script_handle = 'tests-my-shared-script';
     476        $metadata             = array(
     477            'file'   => DIR_TESTDATA . '/blocks/notice/block.json',
     478            'name'   => 'tests/sample-block',
     479            'script' => 'file:./shared-script.js',
     480        );
     481        $result               = register_block_script_handle( $metadata, 'script' );
     482
     483        $this->assertSame( $custom_script_handle, $result );
     484        $this->assertStringEndsWith(
     485            'shared-script.js',
     486            wp_scripts()->registered[ $custom_script_handle ]->src
     487        );
     488    }
     489
     490    /**
     491     * @ticket 60485
     492     */
     493    public function test_reuse_registered_block_script_handle_with_custom_handle_name() {
     494        $custom_script_handle = 'tests-my-shared-script';
     495        $custom_script_src    = 'https://example.com/foo.js';
     496        wp_register_script( $custom_script_handle, $custom_script_src );
     497
     498        $this->assertTrue(
     499            wp_script_is( $custom_script_handle, 'registered' )
     500        );
     501
     502        $metadata = array(
     503            'file'   => DIR_TESTDATA . '/blocks/notice/block.json',
     504            'name'   => 'tests/sample-block',
     505            'script' => 'file:./shared-script.js',
     506        );
     507        $result   = register_block_script_handle( $metadata, 'script' );
     508
     509        $this->assertSame( $custom_script_handle, $result );
     510        $this->assertSame(
     511            $custom_script_src,
     512            wp_scripts()->registered[ $custom_script_handle ]->src
    467513        );
    468514    }
     
    621667        $metadata = array(
    622668            'file'      => DIR_TESTDATA . '/blocks/notice/block.json',
    623             'name'      => 'unit-tests/test-block',
     669            'name'      => 'tests/test-block',
    624670            'style'     => 'file:./block.css',
    625671            'viewStyle' => 'file:./block-view.css',
     
    627673        $result   = register_block_style_handle( $metadata, 'style' );
    628674
    629         $this->assertSame( 'unit-tests-test-block-style', $result );
    630         $this->assertFalse( wp_styles()->get_data( 'unit-tests-test-block-style', 'rtl' ) );
     675        $this->assertSame( 'tests-test-block-style', $result );
     676        $this->assertFalse( wp_styles()->get_data( 'tests-test-block-style', 'rtl' ) );
    631677
    632678        // @ticket 50328
    633679        $this->assertSame(
    634680            wp_normalize_path( realpath( DIR_TESTDATA . '/blocks/notice/block.css' ) ),
    635             wp_normalize_path( wp_styles()->get_data( 'unit-tests-test-block-style', 'path' ) )
     681            wp_normalize_path( wp_styles()->get_data( 'tests-test-block-style', 'path' ) )
    636682        );
    637683
    638684        // Test viewStyle property
    639685        $result = register_block_style_handle( $metadata, 'viewStyle' );
    640         $this->assertSame( 'unit-tests-test-block-view-style', $result );
     686        $this->assertSame( 'tests-test-block-view-style', $result );
    641687
    642688        // @ticket 59673
    643689        $this->assertSame(
    644690            wp_normalize_path( realpath( DIR_TESTDATA . '/blocks/notice/block-view.css' ) ),
    645             wp_normalize_path( wp_styles()->get_data( 'unit-tests-test-block-view-style', 'path' ) ),
     691            wp_normalize_path( wp_styles()->get_data( 'tests-test-block-view-style', 'path' ) ),
    646692            'viewStyle asset path is not correct'
    647693        );
    648694
    649         // Test the behavior directly within the unit test
     695        // Test the behavior directly within the unit test.
    650696        $this->assertFalse(
    651697            strpos(
     
    676722        $metadata = array(
    677723            'file'  => DIR_TESTDATA . '/blocks/notice/block.json',
    678             'name'  => 'unit-tests/test-block-rtl',
     724            'name'  => 'tests/test-block-rtl',
    679725            'style' => 'file:./block.css',
    680726        );
     
    684730
    685731        $handle       = register_block_style_handle( $metadata, 'style' );
    686         $extra_rtl    = wp_styles()->get_data( 'unit-tests-test-block-rtl-style', 'rtl' );
    687         $extra_suffix = wp_styles()->get_data( 'unit-tests-test-block-rtl-style', 'suffix' );
    688         $extra_path   = wp_normalize_path( wp_styles()->get_data( 'unit-tests-test-block-rtl-style', 'path' ) );
     732        $extra_rtl    = wp_styles()->get_data( 'tests-test-block-rtl-style', 'rtl' );
     733        $extra_suffix = wp_styles()->get_data( 'tests-test-block-rtl-style', 'suffix' );
     734        $extra_path   = wp_normalize_path( wp_styles()->get_data( 'tests-test-block-rtl-style', 'path' ) );
    689735
    690736        $wp_locale->text_direction = $orig_text_dir;
    691737
    692738        $this->assertSame(
    693             'unit-tests-test-block-rtl-style',
     739            'tests-test-block-rtl-style',
    694740            $handle,
    695741            'The handle did not match the expected handle.'
     
    721767        $metadata = array(
    722768            'file'  => DIR_TESTDATA . '/blocks/notice/block.json',
    723             'name'  => 'unit-tests/test-block-nonexistent-stylesheet',
     769            'name'  => 'tests/test-block-nonexistent-stylesheet',
    724770            'style' => 'file:./nonexistent.css',
    725771        );
     
    727773
    728774        global $wp_styles;
    729         $this->assertFalse( $wp_styles->registered['unit-tests-test-block-nonexistent-stylesheet-style']->src );
     775        $this->assertFalse( $wp_styles->registered['tests-test-block-nonexistent-stylesheet-style']->src );
    730776    }
    731777
     
    10451091        $this->assertSame(
    10461092            wp_normalize_path( realpath( DIR_TESTDATA . '/blocks/notice/block.css' ) ),
    1047             wp_normalize_path( wp_styles()->get_data( 'unit-tests-test-block-style', 'path' ) )
     1093            wp_normalize_path( wp_styles()->get_data( 'tests-test-block-style', 'path' ) )
    10481094        );
    10491095
     
    10511097        $this->assertSame(
    10521098            wp_normalize_path( realpath( DIR_TESTDATA . '/blocks/notice/block-view.css' ) ),
    1053             wp_normalize_path( wp_styles()->get_data( 'unit-tests-test-block-view-style', 'path' ) ),
     1099            wp_normalize_path( wp_styles()->get_data( 'tests-test-block-view-style', 'path' ) ),
    10541100            'viewStyle asset path is not correct'
    10551101        );
     
    10901136    public function test_register_block_type_accepts_editor_script_array( $editor_script, $expected ) {
    10911137        $settings = array( 'editor_script' => $editor_script );
    1092         register_block_type( 'core/test-static', $settings );
     1138        register_block_type( 'tests/static', $settings );
    10931139
    10941140        $registry   = WP_Block_Type_Registry::get_instance();
    1095         $block_type = $registry->get_registered( 'core/test-static' );
     1141        $block_type = $registry->get_registered( 'tests/static' );
    10961142        $this->assertObjectHasProperty( 'editor_script_handles', $block_type );
    10971143        $actual_script         = $block_type->editor_script;
     
    11561202    public function test_register_block_type_throws_doing_it_wrong( $editor_script, $expected ) {
    11571203        $settings = array( 'editor_script' => $editor_script );
    1158         register_block_type( 'core/test-static', $settings );
     1204        register_block_type( 'tests/static', $settings );
    11591205
    11601206        $registry   = WP_Block_Type_Registry::get_instance();
    1161         $block_type = $registry->get_registered( 'core/test-static' );
     1207        $block_type = $registry->get_registered( 'tests/static' );
    11621208        $this->assertObjectHasProperty( 'editor_script_handles', $block_type );
    11631209        $actual_script         = $block_type->editor_script;
     
    12491295     */
    12501296    public function test_get_dynamic_block_names() {
    1251         register_block_type( 'core/test-static', array() );
    1252         register_block_type( 'core/test-dynamic', array( 'render_callback' => array( $this, 'render_stub' ) ) );
     1297        register_block_type( 'tests/static', array() );
     1298        register_block_type( 'tests/dynamic', array( 'render_callback' => array( $this, 'render_stub' ) ) );
    12531299
    12541300        $dynamic_block_names = get_dynamic_block_names();
    12551301
    1256         $this->assertContains( 'core/test-dynamic', $dynamic_block_names );
    1257         $this->assertNotContains( 'core/test-static', $dynamic_block_names );
     1302        $this->assertContains( 'tests/dynamic', $dynamic_block_names );
     1303        $this->assertNotContains( 'tests/static', $dynamic_block_names );
    12581304    }
    12591305
Note: See TracChangeset for help on using the changeset viewer.