Make WordPress Core


Ignore:
Timestamp:
10/24/2022 02:30:20 PM (3 years ago)
Author:
Bernhard Reiter
Message:

Blocks: Allow arrays for deprecated asset types in block registration.

In register_block_type, continue to allow passing arrays as the editor_script, script, view_script, editor_style, and style arguments. Note that those fields were soft-deprecated in favor of their _handles counterparts in [54155], which would allow specifying multiple items. At the same time, the deprecated fields were limited to string or null.

However, this broke existing code that passed an array as one of those arguments. For backwards compatibility, this change thus restores the previous behavior. It is implemented in WP_Block_Type as a pair of __get() and __set() methods that wrap around the corresponding _handles members, which are arrays of strings.

It also affects the REST API endpoint for block types. The latter’s schema has never allowed for anything other than string or null for any of those fields. For this reason, it now returns the first element of the array stored in the corresponding _handles member in WP_Block_Type.

Follow-up [54155].
Props nendeb55, costdev, gziolo, spacedmonkey, mukesh27, sergeybiryukov, audrasjb.
Merges [54670] to the 6.1 branch.
Fixes #56707.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/6.1/tests/phpunit/tests/blocks/register.php

    r54472 r54671  
    554554
    555555    /**
     556     * Tests that an array value for 'editor_script' is correctly set and retrieved.
     557     *
     558     * As 'editor_script' is now a deprecated property, this should also set
     559     * the value for the 'editor_script_handles' property.
     560     *
     561     * @ticket 56707
     562     *
     563     * @covers ::register_block_type
     564     * @covers WP_Block_Type::__set
     565     * @covers WP_Block_Type::__get
     566     *
     567     * @dataProvider data_register_block_type_accepts_editor_script_array
     568     *
     569     * @param array $editor_script The editor script array to register.
     570     * @param array $expected      The expected registered editor script.
     571     */
     572    public function test_register_block_type_accepts_editor_script_array( $editor_script, $expected ) {
     573        $settings = array( 'editor_script' => $editor_script );
     574        register_block_type( 'core/test-static', $settings );
     575
     576        $registry   = WP_Block_Type_Registry::get_instance();
     577        $block_type = $registry->get_registered( 'core/test-static' );
     578        $this->assertObjectHasAttribute( 'editor_script_handles', $block_type );
     579        $actual_script         = $block_type->editor_script;
     580        $actual_script_handles = $block_type->editor_script_handles;
     581
     582        $this->assertSame(
     583            $expected,
     584            $actual_script,
     585            'editor_script was not set to the correct value.'
     586        );
     587
     588        $this->assertSame(
     589            (array) $expected,
     590            $actual_script_handles,
     591            'editor_script_handles was not set to the correct value.'
     592        );
     593    }
     594
     595    /**
     596     * Data provider for test_register_block_type_accepts_editor_script_array().
     597     *
     598     * @return array
     599     */
     600    public function data_register_block_type_accepts_editor_script_array() {
     601        return array(
     602            'an empty array'      => array(
     603                'editor_script' => array(),
     604                'expected'      => null,
     605            ),
     606            'a single item array' => array(
     607                'editor_script' => array( 'hello' ),
     608                'expected'      => 'hello',
     609            ),
     610            'a multi-item array'  => array(
     611                'editor_script' => array( 'hello', 'world' ),
     612                'expected'      => array( 'hello', 'world' ),
     613            ),
     614        );
     615    }
     616
     617    /**
     618     * Tests that an array value for 'editor_script' containing invalid values
     619     * correctly triggers _doing_it_wrong(), filters the value, and sets the
     620     * property to the result.
     621     *
     622     * As 'editor_script' is now a deprecated property, this should also set
     623     * the value for the 'editor_script_handles' property.
     624     *
     625     * @ticket 56707
     626     *
     627     * @covers ::register_block_type
     628     * @covers WP_Block_Type::__set
     629     * @covers WP_Block_Type::__get
     630     *
     631     * @dataProvider data_register_block_type_throws_doing_it_wrong
     632     *
     633     * @expectedIncorrectUsage WP_Block_Type::__set
     634     *
     635     * @param array $editor_script The editor script array to register.
     636     * @param array $expected      The expected registered editor script.
     637     */
     638    public function test_register_block_type_throws_doing_it_wrong( $editor_script, $expected ) {
     639        $settings = array( 'editor_script' => $editor_script );
     640        register_block_type( 'core/test-static', $settings );
     641
     642        $registry   = WP_Block_Type_Registry::get_instance();
     643        $block_type = $registry->get_registered( 'core/test-static' );
     644        $this->assertObjectHasAttribute( 'editor_script_handles', $block_type );
     645        $actual_script         = $block_type->editor_script;
     646        $actual_script_handles = $block_type->editor_script_handles;
     647
     648        $this->assertSame(
     649            $expected,
     650            $actual_script,
     651            'editor_script was not set to the correct value.'
     652        );
     653
     654        $this->assertSame(
     655            (array) $expected,
     656            $actual_script_handles,
     657            'editor_script_handles was not set to the correct value.'
     658        );
     659    }
     660
     661    /**
     662     * Data provider for test_register_block_type_throws_doing_it_wrong().
     663     *
     664     * @return array
     665     */
     666    public function data_register_block_type_throws_doing_it_wrong() {
     667        return array(
     668            'a non-string array'     => array(
     669                'editor_script' => array( null, false, true, -1, 0, 1, -1.0, 0.0, 1.0, INF, NAN, new stdClass() ),
     670                'expected'      => null,
     671            ),
     672            'a partial string array' => array(
     673                'editor_script' => array( null, false, 'script.js', true, 0, 'actions.js', 1, INF ),
     674                'expected'      => array( 'script.js', 'actions.js' ),
     675            ),
     676            'a partial string array that results in one item with non-zero index' => array(
     677                'editor_script' => array( null, false, 'script.js' ),
     678                'expected'      => 'script.js',
     679            ),
     680        );
     681    }
     682
     683    /**
    556684     * @ticket 52301
    557685     */
Note: See TracChangeset for help on using the changeset viewer.