Make WordPress Core

Changeset 57559


Ignore:
Timestamp:
02/08/2024 08:16:59 AM (8 months ago)
Author:
gziolo
Message:

Editor: Make asset file optional for block scripts

It is no longer a hard requirement that a *.asset.php file is present to register a script for block.

Fixes #57234.
Props joefusco, gziolo, spacedmonkey, colorful-tones.

Location:
trunk
Files:
2 edited

Legend:

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

    r57550 r57559  
    125125/**
    126126 * Finds a script handle for the selected block metadata field. It detects
    127  * when a path to file was provided and finds a corresponding asset file
    128  * with details necessary to register the script under automatically
     127 * when a path to file was provided and optionally finds a corresponding asset
     128 * file with details necessary to register the script under automatically
    129129 * generated handle name. It returns unprocessed script handle otherwise.
    130130 *
    131131 * @since 5.5.0
    132132 * @since 6.1.0 Added `$index` parameter.
     133 * @since 6.5.0 The asset file is optional.
    133134 *
    134135 * @param array  $metadata   Block metadata.
     
    164165    );
    165166
    166     if ( empty( $script_asset_path ) ) {
    167         _doing_it_wrong(
    168             __FUNCTION__,
    169             sprintf(
    170                 /* translators: 1: Asset file location, 2: Field name, 3: Block name.  */
    171                 __( 'The asset file (%1$s) for the "%2$s" defined in "%3$s" block definition is missing.' ),
    172                 $script_asset_raw_path,
    173                 $field_name,
    174                 $metadata['name']
    175             ),
    176             '5.5.0'
    177         );
    178         return false;
    179     }
    180 
    181167    $script_path_norm = wp_normalize_path( realpath( $path . '/' . $script_path ) );
    182168    $script_uri       = get_block_asset_url( $script_path_norm );
     
    187173    }
    188174
    189     $script_asset        = require $script_asset_path;
     175    // Asset file for blocks is optional. See https://core.trac.wordpress.org/ticket/60460.
     176    $script_asset        = ! empty( $script_asset_path ) ? require $script_asset_path : array();
    190177    $script_dependencies = isset( $script_asset['dependencies'] ) ? $script_asset['dependencies'] : array();
    191178    $result              = wp_register_script(
  • trunk/tests/phpunit/tests/blocks/register.php

    r57493 r57559  
    6161        }
    6262
     63        foreach ( wp_scripts()->registered as $script_handle => $script ) {
     64            if ( str_starts_with( $script_handle, 'unit-tests-' ) ) {
     65                wp_deregister_script( $script_handle );
     66            }
     67        }
     68
    6369        parent::tear_down();
    6470    }
     
    227233
    228234    /**
    229      * @expectedIncorrectUsage register_block_script_handle
    230      * @ticket 50263
    231      */
    232     public function test_missing_asset_file_register_block_script_handle() {
     235     * @ticket 50263
     236     */
     237    public function test_handle_passed_register_block_script_handle() {
     238        $metadata = array(
     239            'script' => 'test-script-handle',
     240        );
     241        $result   = register_block_script_handle( $metadata, 'script' );
     242
     243        $this->assertSame( 'test-script-handle', $result );
     244    }
     245
     246    public function test_handles_passed_register_block_script_handles() {
     247        $metadata = array(
     248            'script' => array( 'test-script-handle', 'test-script-handle-2' ),
     249        );
     250
     251        $result = register_block_script_handle( $metadata, 'script' );
     252        $this->assertSame( 'test-script-handle', $result );
     253
     254        $result = register_block_script_handle( $metadata, 'script', 1 );
     255        $this->assertSame( 'test-script-handle-2', $result, 1 );
     256    }
     257
     258    /**
     259     * @ticket 50263
     260     * @ticket 60460
     261     */
     262    public function test_missing_asset_file_register_block_script_handle_with_default_settings() {
    233263        $metadata = array(
    234264            'file'   => __FILE__,
     
    238268        $result   = register_block_script_handle( $metadata, 'script' );
    239269
    240         $this->assertFalse( $result );
    241     }
    242 
    243     /**
    244      * @ticket 50263
    245      */
    246     public function test_handle_passed_register_block_script_handle() {
    247         $metadata = array(
    248             'script' => 'test-script-handle',
    249         );
    250         $result   = register_block_script_handle( $metadata, 'script' );
    251 
    252         $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 );
     270        $this->assertSame( 'unit-tests-test-block-script', $result );
    265271    }
    266272
Note: See TracChangeset for help on using the changeset viewer.