mirror of
git://develop.git.wordpress.org/
synced 2025-04-13 00:22:52 +02:00
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. git-svn-id: https://develop.svn.wordpress.org/branches/6.1@54671 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
parent
f8daa24858
commit
74792354b2
@ -295,8 +295,8 @@ class WP_Block_Type {
|
||||
*
|
||||
* @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 ) ) {
|
||||
@ -304,6 +304,14 @@ class WP_Block_Type {
|
||||
}
|
||||
|
||||
$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;
|
||||
}
|
||||
|
||||
@ -343,12 +351,32 @@ class WP_Block_Type {
|
||||
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 );
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -295,6 +295,10 @@ class WP_REST_Block_Types_Controller extends WP_REST_Controller {
|
||||
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 {
|
||||
|
@ -552,6 +552,134 @@ class Tests_Blocks_Register extends WP_UnitTestCase {
|
||||
$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
|
||||
*/
|
||||
|
@ -338,6 +338,148 @@ class REST_Block_Type_Controller_Test extends WP_Test_REST_Controller_Testcase {
|
||||
$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(
|
||||
|
Loading…
x
Reference in New Issue
Block a user