Make WordPress Core


Ignore:
Timestamp:
05/29/2024 07:44:30 AM (5 months ago)
Author:
ellatrix
Message:

Theme JSON: Extract util to get valid block style variations.

Extracts the repeated collection of valid block style variations into a util that can be reused or updated in a single place for future work around extending block style variations.

Backports https://github.com/WordPress/gutenberg/pull/61030.

Props aaronrobertshaw, audrasjb.

Fixes #61121.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/class-wp-theme-json.php

    r58226 r58228  
    732732        $valid_block_names   = array_keys( static::get_blocks_metadata() );
    733733        $valid_element_names = array_keys( static::ELEMENTS );
    734         $valid_variations    = array();
    735         foreach ( self::get_blocks_metadata() as $block_name => $block_meta ) {
    736             if ( ! isset( $block_meta['styleVariations'] ) ) {
    737                 continue;
    738             }
    739             $valid_variations[ $block_name ] = array_keys( $block_meta['styleVariations'] );
    740         }
    741         $theme_json       = static::sanitize( $this->theme_json, $valid_block_names, $valid_element_names, $valid_variations );
    742         $this->theme_json = static::maybe_opt_in_into_settings( $theme_json );
     734        $valid_variations    = static::get_valid_block_style_variations();
     735        $theme_json          = static::sanitize( $this->theme_json, $valid_block_names, $valid_element_names, $valid_variations );
     736        $this->theme_json    = static::maybe_opt_in_into_settings( $theme_json );
    743737
    744738        // Internally, presets are keyed by origin.
     
    30783072        $valid_block_names   = array_keys( static::get_blocks_metadata() );
    30793073        $valid_element_names = array_keys( static::ELEMENTS );
    3080         $valid_variations    = array();
    3081         foreach ( self::get_blocks_metadata() as $block_name => $block_meta ) {
    3082             if ( ! isset( $block_meta['styleVariations'] ) ) {
    3083                 continue;
    3084             }
    3085             $valid_variations[ $block_name ] = array_keys( $block_meta['styleVariations'] );
    3086         }
     3074        $valid_variations    = static::get_valid_block_style_variations();
    30873075
    30883076        $theme_json = static::sanitize( $theme_json, $valid_block_names, $valid_element_names, $valid_variations );
     
    40334021        return implode( ',', $result );
    40344022    }
     4023
     4024    /**
     4025     * Collects valid block style variations keyed by block type.
     4026     *
     4027     * @since 6.6.0
     4028     *
     4029     * @return array Valid block style variations by block type.
     4030     */
     4031    protected static function get_valid_block_style_variations() {
     4032        $valid_variations = array();
     4033        foreach ( self::get_blocks_metadata() as $block_name => $block_meta ) {
     4034            if ( ! isset( $block_meta['styleVariations'] ) ) {
     4035                continue;
     4036            }
     4037            $valid_variations[ $block_name ] = array_keys( $block_meta['styleVariations'] );
     4038        }
     4039
     4040        return $valid_variations;
     4041    }
    40354042}
Note: See TracChangeset for help on using the changeset viewer.