Make WordPress Core


Ignore:
Timestamp:
10/24/2022 02:14:25 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.
Fixes #56707.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/rest-api/endpoints/class-wp-rest-block-types-controller.php

    r54210 r54670  
    296296                if ( isset( $block_type->$extra_field ) ) {
    297297                    $field = $block_type->$extra_field;
     298                    if ( in_array( $extra_field, $deprecated_fields, true ) && is_array( $field ) ) {
     299                        // Since the schema only allows strings or null (but no arrays), we return the first array item.
     300                        $field = ! empty( $field ) ? array_shift( $field ) : '';
     301                    }
    298302                } elseif ( array_key_exists( 'default', $schema['properties'][ $extra_field ] ) ) {
    299303                    $field = $schema['properties'][ $extra_field ]['default'];
Note: See TracChangeset for help on using the changeset viewer.