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.
Fixes #56707.
Built from https://develop.svn.wordpress.org/trunk@54670


git-svn-id: http://core.svn.wordpress.org/trunk@54222 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
Bernhard Reiter 2022-10-24 14:16:12 +00:00
parent ba19b8412d
commit ca3cfae579
3 changed files with 37 additions and 5 deletions

View File

@ -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 );
}
/**

View File

@ -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 {

View File

@ -16,7 +16,7 @@
*
* @global string $wp_version
*/
$wp_version = '6.2-alpha-54669';
$wp_version = '6.2-alpha-54670';
/**
* Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.