Make WordPress Core


Ignore:
Timestamp:
06/24/2024 08:49:52 AM (5 months ago)
Author:
oandregal
Message:

Section styles: improve performance and conceptual consistency.

These changes involve:

  • Move shared variation definitions from styles.blocks.variations to styles.variations
  • Remove blockTypes from styles.variations.
  • Do not register shared variations from theme style variation or primary theme.json files.
  • Move the merging of theme.json data into the WP_Theme_JSON_Resolver and WP_Theme_JSON classes.

These changes improve performance and are more future-proof API wise.
See conversation at https://github.com/WordPress/gutenberg/issues/62686

Props aaronrobertshaw, oandregal, andrewserong, joemcgill, talldanwp, andrewserong, ramonopoly, richtabor, youknowriad.

See #61312, #61451.

File:
1 edited

Legend:

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

    r58443 r58466  
    232232     * @since 6.0.0 Added an `$options` parameter to allow the theme data to be returned without theme supports.
    233233     * @since 6.6.0 Add support for 'default-font-sizes' and 'default-spacing-sizes' theme supports.
    234      *              Register the block style variations coming from the partials and the theme.json.
     234     *              Added registration and merging of block style variations from partial theme.json files and the block styles registry.
    235235     *
    236236     * @param array $deprecated Deprecated. Not used.
     
    259259            }
    260260
    261             // Register variations defined by the theme.
    262             $variations = $theme_json_data['styles']['blocks']['variations'] ?? array();
    263             wp_register_block_style_variations_from_theme_json_data( $variations );
    264 
    265             // Register variations defined by theme partials (theme.json files in the styles directory).
     261            /*
     262             * Register variations defined by theme partials (theme.json files in the styles directory).
     263             * This is required so the variations pass sanitization of theme.json data.
     264             */
    266265            $variations = static::get_style_variations( 'block' );
    267             wp_register_block_style_variations_from_theme_json_data( $variations );
     266            wp_register_block_style_variations_from_theme_json_partials( $variations );
     267
     268            /*
     269             * Source variations from the block registry and block style variation files. Then, merge them into the existing theme.json data.
     270             *
     271             * In case the same style properties are defined in several sources, this is how we should resolve the values,
     272             * from higher to lower priority:
     273             *
     274             * - styles.blocks.blockType.variations from theme.json
     275             * - styles.variations from theme.json
     276             * - variations from block style variation files
     277             * - variations from block styles registry
     278             *
     279             * See test_add_registered_block_styles_to_theme_data and test_unwraps_block_style_variations.
     280             *
     281             */
     282            $theme_json_data = static::inject_variations_from_block_style_variation_files( $theme_json_data, $variations );
     283            $theme_json_data = static::inject_variations_from_block_styles_registry( $theme_json_data );
    268284
    269285            /**
     
    580596                $config = $decoded_data;
    581597            }
    582 
    583             // Register variations defined by the user.
    584             $variations = $config['styles']['blocks']['variations'] ?? array();
    585             wp_register_block_style_variations_from_theme_json_data( $variations );
    586598        }
    587599
     
    886898     * @since 6.6.0
    887899     *
    888      * @param WP_Theme_JSON  $theme_json A theme json instance.
     900     * @param WP_Theme_JSON $theme_json A theme json instance.
    889901     * @return WP_Theme_JSON Theme merged with resolved paths, if any found.
    890902     */
     
    908920        return $theme_json;
    909921    }
     922
     923    /**
     924     * Adds variations sourced from block style variations files to the supplied theme.json data.
     925     *
     926     * @since 6.6.0
     927     *
     928     * @param array $data   Array following the theme.json specification.
     929     * @param array $variations Shared block style variations.
     930     * @return array Theme json data including shared block style variation definitions.
     931     */
     932    private static function inject_variations_from_block_style_variation_files( $data, $variations ) {
     933        if ( empty( $variations ) ) {
     934            return $data;
     935        }
     936
     937        foreach ( $variations as $variation ) {
     938            if ( empty( $variation['styles'] ) || empty( $variation['blockTypes'] ) ) {
     939                continue;
     940            }
     941
     942            $variation_name = $variation['slug'] ?? _wp_to_kebab_case( $variation['title'] );
     943
     944            foreach ( $variation['blockTypes'] as $block_type ) {
     945                // First, override partial styles with any top-level styles.
     946                $top_level_data = $data['styles']['variations'][ $variation_name ] ?? array();
     947                if ( ! empty( $top_level_data ) ) {
     948                    $variation['styles'] = array_replace_recursive( $variation['styles'], $top_level_data );
     949                }
     950
     951                // Then, override styles so far with any block-level styles.
     952                $block_level_data = $data['styles']['blocks'][ $block_type ]['variations'][ $variation_name ] ?? array();
     953                if ( ! empty( $block_level_data ) ) {
     954                    $variation['styles'] = array_replace_recursive( $variation['styles'], $block_level_data );
     955                }
     956
     957                $path = array( 'styles', 'blocks', $block_type, 'variations', $variation_name );
     958                _wp_array_set( $data, $path, $variation['styles'] );
     959            }
     960        }
     961
     962        return $data;
     963    }
     964
     965    /**
     966     * Adds variations sourced from the block styles registry to the supplied theme.json data.
     967     *
     968     * @since 6.6.0
     969     *
     970     * @param array $data       Array following the theme.json specification.
     971     * @return array Theme json data including shared block style variation definitions.
     972     */
     973    private static function inject_variations_from_block_styles_registry( $data ) {
     974        $registry = WP_Block_Styles_Registry::get_instance();
     975        $styles   = $registry->get_all_registered();
     976
     977        foreach ( $styles as $block_type => $variations ) {
     978            foreach ( $variations as $variation_name => $variation ) {
     979                if ( empty( $variation['style_data'] ) ) {
     980                    continue;
     981                }
     982
     983                // First, override registry styles with any top-level styles.
     984                $top_level_data = $data['styles']['variations'][ $variation_name ] ?? array();
     985                if ( ! empty( $top_level_data ) ) {
     986                    $variation['style_data'] = array_replace_recursive( $variation['style_data'], $top_level_data );
     987                }
     988
     989                // Then, override styles so far with any block-level styles.
     990                $block_level_data = $data['styles']['blocks'][ $block_type ]['variations'][ $variation_name ] ?? array();
     991                if ( ! empty( $block_level_data ) ) {
     992                    $variation['style_data'] = array_replace_recursive( $variation['style_data'], $block_level_data );
     993                }
     994
     995                $path = array( 'styles', 'blocks', $block_type, 'variations', $variation_name );
     996                _wp_array_set( $data, $path, $variation['style_data'] );
     997            }
     998        }
     999
     1000        return $data;
     1001    }
    9101002}
Note: See TracChangeset for help on using the changeset viewer.