From ca3cfae57917b68381a56de35f0d1ed94bfe56c9 Mon Sep 17 00:00:00 2001 From: Bernhard Reiter Date: Mon, 24 Oct 2022 14:16:12 +0000 Subject: [PATCH] Blocks: Allow arrays for deprecated asset types in block registration. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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 --- wp-includes/class-wp-block-type.php | 36 ++++++++++++++++--- .../class-wp-rest-block-types-controller.php | 4 +++ wp-includes/version.php | 2 +- 3 files changed, 37 insertions(+), 5 deletions(-) diff --git a/wp-includes/class-wp-block-type.php b/wp-includes/class-wp-block-type.php index 19cd1c0791..505c504037 100644 --- a/wp-includes/class-wp-block-type.php +++ b/wp-includes/class-wp-block-type.php @@ -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.' ), + '$value' + ), + '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 ); } /** diff --git a/wp-includes/rest-api/endpoints/class-wp-rest-block-types-controller.php b/wp-includes/rest-api/endpoints/class-wp-rest-block-types-controller.php index af29924c76..e1f34baef6 100644 --- a/wp-includes/rest-api/endpoints/class-wp-rest-block-types-controller.php +++ b/wp-includes/rest-api/endpoints/class-wp-rest-block-types-controller.php @@ -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 { diff --git a/wp-includes/version.php b/wp-includes/version.php index 14cd6edaf5..c51d5ce726 100644 --- a/wp-includes/version.php +++ b/wp-includes/version.php @@ -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.