Make WordPress Core


Ignore:
Timestamp:
09/08/2026 06:04:10 AM (2 months ago)
Author:
westonruter
Message:

Code Quality: Combine registered block styles with array_merge().

In WP_REST_Block_Types_Controller the block styles registered for a block are combined with array_merge(), where wp_parse_args() had been used erroneously on what are two lists. In support of this, a global Block_Style_Properties type alias is introduced for the shape of a registered block style.

Developed in ​https://github.com/WordPress/wordpress-develop/pull/13432.
Follow-up to r48173, r59760, r63522.

Props westonruter, swissspidy.
See #65817, #65860.

File:
1 edited

Legend:

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

    r59970 r63524  
    331331
    332332                if ( rest_is_field_included( 'styles', $fields ) ) {
    333                         $styles         = $this->style_registry->get_registered_styles_for_block( $block_type->name );
    334                         $styles         = array_values( $styles );
    335                         $data['styles'] = wp_parse_args( $styles, $data['styles'] );
     333                        $styles = $this->style_registry->get_registered_styles_for_block( $block_type->name );
     334                        $styles = array_values( $styles );
     335
     336                        /*
     337                         * The loop above assigns this key from rest_sanitize_value_from_schema(), whose
     338                         * return is documented as `mixed|WP_Error`, so what the styles came back as has
     339                         * to be restated here. A WP_Error is why the value is checked rather than cast.
     340                         */
     341                        /**
     342                         * @var array<string, mixed>[] $block_styles
     343                         * @phpstan-var list<Block_Style_Properties> $block_styles
     344                         */
     345                        $block_styles = isset( $data['styles'] ) && is_array( $data['styles'] ) ? $data['styles'] : array();
     346
     347                        $data['styles'] = array_merge( $block_styles, $styles );
    336348                        $data['styles'] = array_filter( $data['styles'] );
    337349                }
Note: See TracChangeset for help on using the changeset viewer.