Make WordPress Core


Ignore:
Timestamp:
06/02/2026 02:57:06 PM (5 months ago)
Author:
wildworks
Message:

Blocks: Preserve zero values in wrapper attributes.

The string "0" and the integer 0 were previously dropped when merging block wrapper attributes because falsy values were filtered out. Strings and numbers are now kept and cast to a string, so zero values are preserved.

Type annotations and null checks are also improved.

Props westonruter, wildworks.
Fixes #64452.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/class-wp-block-supports.php

    r62068 r62450  
    1414 *
    1515 * @access private
     16 *
     17 * @phpstan-type ApplyCallback callable( WP_Block_Type, array<string, mixed> ): array<string, mixed>
     18 * @phpstan-type RegisterCallback callable( WP_Block_Type ): void
    1619 */
    1720#[AllowDynamicProperties]
    … …  
    2326         * @since 5.6.0
    2427         * @var array
     28         * @phpstan-var array<string, array{
     29         *     name: string,
     30         *     apply?: ApplyCallback,
     31         *     register_attribute?: RegisterCallback,
     32         * }>
    2533         */
    2634        private $block_supports = array();
    … …  
    3038         *
    3139         * @since 5.6.0
    32          * @var array
     40         * @var array|null
     41         * @phpstan-var array<string, mixed>|null
    3342         */
    3443        public static $block_to_render = null;
    … …  
    6372         *
    6473         * @since 5.6.0
     74         *
     75         * @return void
    6576         */
    6677        public static function init() {
    … …  
    7889         * @param string $block_support_name   Block support name.
    7990         * @param array  $block_support_config Array containing the properties of the block support.
     91         *
     92         * @phpstan-param array{
     93         *     apply?: ApplyCallback,
     94         *     register_attribute?: RegisterCallback,
     95         * } $block_support_config
     96         *
     97         * @return void
    8098         */
    8199        public function register( $block_support_name, $block_support_config ) {
    … …  
    95113         */
    96114        public function apply_block_supports() {
     115                if ( ! is_array( self::$block_to_render ) ) {
     116                        return array();
     117                }
     118
    97119                $block_type = WP_Block_Type_Registry::get_instance()->get_registered(
    98120                        self::$block_to_render['blockName']
    … …  
    100122
    101123                // If no render_callback, assume styles have been previously handled.
    102                 if ( ! $block_type || empty( $block_type ) ) {
     124                if ( ! $block_type ) {
    103125                        return array();
    104126                }
    … …  
    122144                        if ( ! empty( $new_attributes ) ) {
    123145                                foreach ( $new_attributes as $attribute_name => $attribute_value ) {
    124                                         if ( empty( $output[ $attribute_name ] ) ) {
     146                                        if ( ! is_scalar( $attribute_value ) || is_bool( $attribute_value ) ) {
     147                                                continue;
     148                                        }
     149                                        $attribute_value = (string) $attribute_value;
     150                                        if ( ! array_key_exists( $attribute_name, $output ) || '' === $output[ $attribute_name ] ) {
    125151                                                $output[ $attribute_name ] = $attribute_value;
    126152                                        } else {
    … …  
    138164         *
    139165         * @since 5.6.0
     166         *
     167         * @return void
    140168         */
    141169        private function register_attributes() {
    … …  
    197225                                (array) preg_split( '/\s+/', $new_attribute, -1, PREG_SPLIT_NO_EMPTY )
    198226                        );
    199                         $classes = array_unique( array_filter( $classes ) );
     227                        $classes = array_unique( $classes );
    200228                        return implode( ' ', $classes );
    201229                },
    … …  
    208236        );
    209237
     238        // Accept strings and numbers (cast to string); reject other types (bool, null, array, object).
    210239        $attributes = array();
    211240        foreach ( $attribute_merge_callbacks as $attribute_name => $merge_callback ) {
    212241                $new_attribute   = $new_attributes[ $attribute_name ] ?? '';
    213242                $extra_attribute = $extra_attributes[ $attribute_name ] ?? '';
    214                 $new_attribute   = is_string( $new_attribute ) ? $new_attribute : '';
    215                 $extra_attribute = is_string( $extra_attribute ) ? $extra_attribute : '';
     243                $new_attribute   = is_scalar( $new_attribute ) && ! is_bool( $new_attribute ) ? (string) $new_attribute : '';
     244                $extra_attribute = is_scalar( $extra_attribute ) && ! is_bool( $extra_attribute ) ? (string) $extra_attribute : '';
    216245
    217246                if ( '' === $new_attribute && '' === $extra_attribute ) {
    … …  
    223252
    224253        foreach ( $extra_attributes as $attribute_name => $value ) {
    225                 if ( ! isset( $attribute_merge_callbacks[ $attribute_name ] ) ) {
    226                         $attributes[ $attribute_name ] = $value;
     254                if ( ! isset( $attribute_merge_callbacks[ $attribute_name ] ) && is_scalar( $value ) && ! is_bool( $value ) ) {
     255                        $attributes[ $attribute_name ] = (string) $value;
    227256                }
    228257        }
Note: See TracChangeset for help on using the changeset viewer.