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/rest-api/rest-block-type-controller.php

    r54155 r54671  
    337337        $this->assertNull( $data['editor_style'] );
    338338        $this->assertNull( $data['style'] );
     339    }
     340
     341    /**
     342     * @ticket 56733
     343     */
     344    public function test_get_item_deprecated() {
     345        $block_type = 'fake/deprecated';
     346        $settings   = array(
     347            'editor_script' => 'hello_world',
     348            'script'        => 'gutenberg',
     349            'view_script'   => 'foo_bar',
     350            'editor_style'  => 'guten_tag',
     351            'style'         => 'out_of_style',
     352        );
     353        register_block_type( $block_type, $settings );
     354        wp_set_current_user( self::$admin_id );
     355        $request  = new WP_REST_Request( 'GET', '/wp/v2/block-types/' . $block_type );
     356        $response = rest_get_server()->dispatch( $request );
     357        $data     = $response->get_data();
     358        $this->assertSameSets(
     359            array( 'hello_world' ),
     360            $data['editor_script_handles'],
     361            "Endpoint doesn't return correct array for editor_script_handles."
     362        );
     363        $this->assertSameSets(
     364            array( 'gutenberg' ),
     365            $data['script_handles'],
     366            "Endpoint doesn't return correct array for script_handles."
     367        );
     368        $this->assertSameSets(
     369            array( 'foo_bar' ),
     370            $data['view_script_handles'],
     371            "Endpoint doesn't return correct array for view_script_handles."
     372        );
     373        $this->assertSameSets(
     374            array( 'guten_tag' ),
     375            $data['editor_style_handles'],
     376            "Endpoint doesn't return correct array for editor_style_handles."
     377        );
     378        $this->assertSameSets(
     379            array( 'out_of_style' ),
     380            $data['style_handles'],
     381            "Endpoint doesn't return correct array for style_handles."
     382        );
     383        // Deprecated properties.
     384        $this->assertSame(
     385            'hello_world',
     386            $data['editor_script'],
     387            "Endpoint doesn't return correct string for editor_script."
     388        );
     389        $this->assertSame(
     390            'gutenberg',
     391            $data['script'],
     392            "Endpoint doesn't return correct string for script."
     393        );
     394        $this->assertSame(
     395            'foo_bar',
     396            $data['view_script'],
     397            "Endpoint doesn't return correct string for view_script."
     398        );
     399        $this->assertSame(
     400            'guten_tag',
     401            $data['editor_style'],
     402            "Endpoint doesn't return correct string for editor_style."
     403        );
     404        $this->assertSame(
     405            'out_of_style',
     406            $data['style'],
     407            "Endpoint doesn't return correct string for style."
     408        );
     409    }
     410
     411    /**
     412     * @ticket 56733
     413     */
     414    public function test_get_item_deprecated_with_arrays() {
     415        $block_type = 'fake/deprecated-with-arrays';
     416        $settings   = array(
     417            'editor_script' => array( 'hello', 'world' ),
     418            'script'        => array( 'gutenberg' ),
     419            'view_script'   => array( 'foo', 'bar' ),
     420            'editor_style'  => array( 'guten', 'tag' ),
     421            'style'         => array( 'out', 'of', 'style' ),
     422        );
     423        register_block_type( $block_type, $settings );
     424        wp_set_current_user( self::$admin_id );
     425        $request  = new WP_REST_Request( 'GET', '/wp/v2/block-types/' . $block_type );
     426        $response = rest_get_server()->dispatch( $request );
     427        $data     = $response->get_data();
     428        $this->assertSameSets(
     429            $settings['editor_script'],
     430            $data['editor_script_handles'],
     431            "Endpoint doesn't return correct array for editor_script_handles."
     432        );
     433        $this->assertSameSets(
     434            $settings['script'],
     435            $data['script_handles'],
     436            "Endpoint doesn't return correct array for script_handles."
     437        );
     438        $this->assertSameSets(
     439            $settings['view_script'],
     440            $data['view_script_handles'],
     441            "Endpoint doesn't return correct array for view_script_handles."
     442        );
     443        $this->assertSameSets(
     444            $settings['editor_style'],
     445            $data['editor_style_handles'],
     446            "Endpoint doesn't return correct array for editor_style_handles."
     447        );
     448        $this->assertSameSets(
     449            $settings['style'],
     450            $data['style_handles'],
     451            "Endpoint doesn't return correct array for style_handles."
     452        );
     453        // Deprecated properties.
     454        // Since the schema only allows strings or null (but no arrays), we return the first array item.
     455        // Deprecated properties.
     456        $this->assertSame(
     457            'hello',
     458            $data['editor_script'],
     459            "Endpoint doesn't return first array element for editor_script."
     460        );
     461        $this->assertSame(
     462            'gutenberg',
     463            $data['script'],
     464            "Endpoint doesn't return first array element for script."
     465        );
     466        $this->assertSame(
     467            'foo',
     468            $data['view_script'],
     469            "Endpoint doesn't return first array element for view_script."
     470        );
     471        $this->assertSame(
     472            'guten',
     473            $data['editor_style'],
     474            "Endpoint doesn't return first array element for editor_style."
     475        );
     476        $this->assertSame(
     477            'out',
     478            $data['style'],
     479            "Endpoint doesn't return first array element for style."
     480        );
    339481    }
    340482
Note: See TracChangeset for help on using the changeset viewer.