Make WordPress Core


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

Blocks: Allow registering multiple items for all supported asset types

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

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

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

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

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/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
Note: See TracChangeset for help on using the changeset viewer.