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/class-wp-block-type.php

    r54210 r54670  
    296296     * @param string $name Deprecated property name.
    297297     *
    298      * @return string|null|void The value read from the new property if the first item in the array provided,
    299      *                          null when value not found, or void when unknown property name provided.
     298     * @return string|string[]|null|void The value read from the new property if the first item in the array provided,
     299     *                                   null when value not found, or void when unknown property name provided.
    300300     */
    301301    public function __get( $name ) {
     
    305305
    306306        $new_name = $name . '_handles';
     307
     308        if ( ! property_exists( $this, $new_name ) || ! is_array( $this->{$new_name} ) ) {
     309            return null;
     310        }
     311
     312        if ( count( $this->{$new_name} ) > 1 ) {
     313            return $this->{$new_name};
     314        }
    307315        return isset( $this->{$new_name}[0] ) ? $this->{$new_name}[0] : null;
    308316    }
     
    344352        }
    345353
     354        $new_name = $name . '_handles';
     355
     356        if ( is_array( $value ) ) {
     357            $filtered = array_filter( $value, 'is_string' );
     358
     359            if ( count( $filtered ) !== count( $value ) ) {
     360                    _doing_it_wrong(
     361                        __METHOD__,
     362                        sprintf(
     363                            /* translators: %s: The '$value' argument. */
     364                            __( 'The %s argument must be a string or a string array.' ),
     365                            '<code>$value</code>'
     366                        ),
     367                        '6.1.0'
     368                    );
     369            }
     370
     371            $this->{$new_name} = array_values( $filtered );
     372            return;
     373        }
     374
    346375        if ( ! is_string( $value ) ) {
    347376            return;
    348377        }
    349378
    350         $new_name             = $name . '_handles';
    351         $this->{$new_name}[0] = $value;
     379        $this->{$new_name} = array( $value );
    352380    }
    353381
Note: See TracChangeset for help on using the changeset viewer.