Skip to content

Commit

Permalink
Blocks: Allow arrays for deprecated asset types in block registration.
Browse files Browse the repository at this point in the history
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.
Fixes #56707.

git-svn-id: https://develop.svn.wordpress.org/trunk@54670 602fd350-edb4-49c9-b593-d223f7449a82
  • Loading branch information
ockham committed Oct 24, 2022
1 parent 799d7dc commit d962be1
Show file tree
Hide file tree
Showing 4 changed files with 306 additions and 4 deletions.
36 changes: 32 additions & 4 deletions src/wp-includes/class-wp-block-type.php
Original file line number Diff line number Diff line change
Expand Up @@ -295,15 +295,23 @@ public function __construct( $block_type, $args = array() ) {
*
* @param string $name Deprecated property name.
*
* @return string|null|void The value read from the new property if the first item in the array provided,
* null when value not found, or void when unknown property name provided.
* @return string|string[]|null|void The value read from the new property if the first item in the array provided,
* null when value not found, or void when unknown property name provided.
*/
public function __get( $name ) {
if ( ! in_array( $name, $this->deprecated_properties ) ) {
return;
}

$new_name = $name . '_handles';

if ( ! property_exists( $this, $new_name ) || ! is_array( $this->{$new_name} ) ) {
return null;
}

if ( count( $this->{$new_name} ) > 1 ) {
return $this->{$new_name};
}
return isset( $this->{$new_name}[0] ) ? $this->{$new_name}[0] : null;
}

Expand Down Expand Up @@ -343,12 +351,32 @@ public function __set( $name, $value ) {
return;
}

$new_name = $name . '_handles';

if ( is_array( $value ) ) {
$filtered = array_filter( $value, 'is_string' );

if ( count( $filtered ) !== count( $value ) ) {
_doing_it_wrong(
__METHOD__,
sprintf(
/* translators: %s: The '$value' argument. */
__( 'The %s argument must be a string or a string array.' ),
'<code>$value</code>'
),
'6.1.0'
);
}

$this->{$new_name} = array_values( $filtered );
return;
}

if ( ! is_string( $value ) ) {
return;
}

$new_name = $name . '_handles';
$this->{$new_name}[0] = $value;
$this->{$new_name} = array( $value );
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -295,6 +295,10 @@ public function prepare_item_for_response( $item, $request ) {
if ( rest_is_field_included( $extra_field, $fields ) ) {
if ( isset( $block_type->$extra_field ) ) {
$field = $block_type->$extra_field;
if ( in_array( $extra_field, $deprecated_fields, true ) && is_array( $field ) ) {
// Since the schema only allows strings or null (but no arrays), we return the first array item.
$field = ! empty( $field ) ? array_shift( $field ) : '';
}
} elseif ( array_key_exists( 'default', $schema['properties'][ $extra_field ] ) ) {
$field = $schema['properties'][ $extra_field ]['default'];
} else {
Expand Down
128 changes: 128 additions & 0 deletions tests/phpunit/tests/blocks/register.php
Original file line number Diff line number Diff line change
Expand Up @@ -552,6 +552,134 @@ public function test_block_register_block_type_proxy_for_metadata() {
$this->assertSame( 'tests/notice', $result->name );
}

/**
* Tests that an array value for 'editor_script' is correctly set and retrieved.
*
* As 'editor_script' is now a deprecated property, this should also set
* the value for the 'editor_script_handles' property.
*
* @ticket 56707
*
* @covers ::register_block_type
* @covers WP_Block_Type::__set
* @covers WP_Block_Type::__get
*
* @dataProvider data_register_block_type_accepts_editor_script_array
*
* @param array $editor_script The editor script array to register.
* @param array $expected The expected registered editor script.
*/
public function test_register_block_type_accepts_editor_script_array( $editor_script, $expected ) {
$settings = array( 'editor_script' => $editor_script );
register_block_type( 'core/test-static', $settings );

$registry = WP_Block_Type_Registry::get_instance();
$block_type = $registry->get_registered( 'core/test-static' );
$this->assertObjectHasAttribute( 'editor_script_handles', $block_type );
$actual_script = $block_type->editor_script;
$actual_script_handles = $block_type->editor_script_handles;

$this->assertSame(
$expected,
$actual_script,
'editor_script was not set to the correct value.'
);

$this->assertSame(
(array) $expected,
$actual_script_handles,
'editor_script_handles was not set to the correct value.'
);
}

/**
* Data provider for test_register_block_type_accepts_editor_script_array().
*
* @return array
*/
public function data_register_block_type_accepts_editor_script_array() {
return array(
'an empty array' => array(
'editor_script' => array(),
'expected' => null,
),
'a single item array' => array(
'editor_script' => array( 'hello' ),
'expected' => 'hello',
),
'a multi-item array' => array(
'editor_script' => array( 'hello', 'world' ),
'expected' => array( 'hello', 'world' ),
),
);
}

/**
* Tests that an array value for 'editor_script' containing invalid values
* correctly triggers _doing_it_wrong(), filters the value, and sets the
* property to the result.
*
* As 'editor_script' is now a deprecated property, this should also set
* the value for the 'editor_script_handles' property.
*
* @ticket 56707
*
* @covers ::register_block_type
* @covers WP_Block_Type::__set
* @covers WP_Block_Type::__get
*
* @dataProvider data_register_block_type_throws_doing_it_wrong
*
* @expectedIncorrectUsage WP_Block_Type::__set
*
* @param array $editor_script The editor script array to register.
* @param array $expected The expected registered editor script.
*/
public function test_register_block_type_throws_doing_it_wrong( $editor_script, $expected ) {
$settings = array( 'editor_script' => $editor_script );
register_block_type( 'core/test-static', $settings );

$registry = WP_Block_Type_Registry::get_instance();
$block_type = $registry->get_registered( 'core/test-static' );
$this->assertObjectHasAttribute( 'editor_script_handles', $block_type );
$actual_script = $block_type->editor_script;
$actual_script_handles = $block_type->editor_script_handles;

$this->assertSame(
$expected,
$actual_script,
'editor_script was not set to the correct value.'
);

$this->assertSame(
(array) $expected,
$actual_script_handles,
'editor_script_handles was not set to the correct value.'
);
}

/**
* Data provider for test_register_block_type_throws_doing_it_wrong().
*
* @return array
*/
public function data_register_block_type_throws_doing_it_wrong() {
return array(
'a non-string array' => array(
'editor_script' => array( null, false, true, -1, 0, 1, -1.0, 0.0, 1.0, INF, NAN, new stdClass() ),
'expected' => null,
),
'a partial string array' => array(
'editor_script' => array( null, false, 'script.js', true, 0, 'actions.js', 1, INF ),
'expected' => array( 'script.js', 'actions.js' ),
),
'a partial string array that results in one item with non-zero index' => array(
'editor_script' => array( null, false, 'script.js' ),
'expected' => 'script.js',
),
);
}

/**
* @ticket 52301
*/
Expand Down
142 changes: 142 additions & 0 deletions tests/phpunit/tests/rest-api/rest-block-type-controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -338,6 +338,148 @@ public function test_get_item_defaults() {
$this->assertNull( $data['style'] );
}

/**
* @ticket 56733
*/
public function test_get_item_deprecated() {
$block_type = 'fake/deprecated';
$settings = array(
'editor_script' => 'hello_world',
'script' => 'gutenberg',
'view_script' => 'foo_bar',
'editor_style' => 'guten_tag',
'style' => 'out_of_style',
);
register_block_type( $block_type, $settings );
wp_set_current_user( self::$admin_id );
$request = new WP_REST_Request( 'GET', '/wp/v2/block-types/' . $block_type );
$response = rest_get_server()->dispatch( $request );
$data = $response->get_data();
$this->assertSameSets(
array( 'hello_world' ),
$data['editor_script_handles'],
"Endpoint doesn't return correct array for editor_script_handles."
);
$this->assertSameSets(
array( 'gutenberg' ),
$data['script_handles'],
"Endpoint doesn't return correct array for script_handles."
);
$this->assertSameSets(
array( 'foo_bar' ),
$data['view_script_handles'],
"Endpoint doesn't return correct array for view_script_handles."
);
$this->assertSameSets(
array( 'guten_tag' ),
$data['editor_style_handles'],
"Endpoint doesn't return correct array for editor_style_handles."
);
$this->assertSameSets(
array( 'out_of_style' ),
$data['style_handles'],
"Endpoint doesn't return correct array for style_handles."
);
// Deprecated properties.
$this->assertSame(
'hello_world',
$data['editor_script'],
"Endpoint doesn't return correct string for editor_script."
);
$this->assertSame(
'gutenberg',
$data['script'],
"Endpoint doesn't return correct string for script."
);
$this->assertSame(
'foo_bar',
$data['view_script'],
"Endpoint doesn't return correct string for view_script."
);
$this->assertSame(
'guten_tag',
$data['editor_style'],
"Endpoint doesn't return correct string for editor_style."
);
$this->assertSame(
'out_of_style',
$data['style'],
"Endpoint doesn't return correct string for style."
);
}

/**
* @ticket 56733
*/
public function test_get_item_deprecated_with_arrays() {
$block_type = 'fake/deprecated-with-arrays';
$settings = array(
'editor_script' => array( 'hello', 'world' ),
'script' => array( 'gutenberg' ),
'view_script' => array( 'foo', 'bar' ),
'editor_style' => array( 'guten', 'tag' ),
'style' => array( 'out', 'of', 'style' ),
);
register_block_type( $block_type, $settings );
wp_set_current_user( self::$admin_id );
$request = new WP_REST_Request( 'GET', '/wp/v2/block-types/' . $block_type );
$response = rest_get_server()->dispatch( $request );
$data = $response->get_data();
$this->assertSameSets(
$settings['editor_script'],
$data['editor_script_handles'],
"Endpoint doesn't return correct array for editor_script_handles."
);
$this->assertSameSets(
$settings['script'],
$data['script_handles'],
"Endpoint doesn't return correct array for script_handles."
);
$this->assertSameSets(
$settings['view_script'],
$data['view_script_handles'],
"Endpoint doesn't return correct array for view_script_handles."
);
$this->assertSameSets(
$settings['editor_style'],
$data['editor_style_handles'],
"Endpoint doesn't return correct array for editor_style_handles."
);
$this->assertSameSets(
$settings['style'],
$data['style_handles'],
"Endpoint doesn't return correct array for style_handles."
);
// Deprecated properties.
// Since the schema only allows strings or null (but no arrays), we return the first array item.
// Deprecated properties.
$this->assertSame(
'hello',
$data['editor_script'],
"Endpoint doesn't return first array element for editor_script."
);
$this->assertSame(
'gutenberg',
$data['script'],
"Endpoint doesn't return first array element for script."
);
$this->assertSame(
'foo',
$data['view_script'],
"Endpoint doesn't return first array element for view_script."
);
$this->assertSame(
'guten',
$data['editor_style'],
"Endpoint doesn't return first array element for editor_style."
);
$this->assertSame(
'out',
$data['style'],
"Endpoint doesn't return first array element for style."
);
}

public function test_get_variation() {
$block_type = 'fake/variations';
$settings = array(
Expand Down

0 comments on commit d962be1

Please sign in to comment.