Make WordPress Core

Changeset 58466


Ignore:
Timestamp:
06/24/2024 08:49:52 AM (2 years 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.

Location:
trunk
Files:
10 edited

Legend:

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

    r58429 r58466  
    214214
    215215/**
    216  * Collects block style variation data for merging with theme.json data.
    217  *
    218  * @since 6.6.0
    219  * @access private
    220  *
    221  * @param array $variations Shared block style variations.
    222  *
    223  * @return array Block variations data to be merged under `styles.blocks`.
    224  */
    225 function wp_resolve_block_style_variations( $variations ) {
    226         $variations_data = array();
    227 
    228         if ( empty( $variations ) ) {
    229                 return $variations_data;
    230         }
    231 
    232         $have_named_variations = ! wp_is_numeric_array( $variations );
    233 
    234         foreach ( $variations as $key => $variation ) {
    235                 $supported_blocks = $variation['blockTypes'] ?? array();
    236 
    237                 /*
    238                  * Standalone theme.json partial files for block style variations
    239                  * will have their styles under a top-level property by the same name.
    240                  * Variations defined within an existing theme.json or theme style
    241                  * variation will themselves already be the required styles data.
    242                  */
    243                 $variation_data = $variation['styles'] ?? $variation;
    244 
    245                 if ( empty( $variation_data ) ) {
    246                         continue;
    247                 }
    248 
    249                 /*
    250                  * Block style variations read in via standalone theme.json partials
    251                  * need to have their name set to the kebab case version of their title.
    252                  */
    253                 $variation_name = $have_named_variations ? $key : ( $variation['slug'] ?? _wp_to_kebab_case( $variation['title'] ) );
    254 
    255                 foreach ( $supported_blocks as $block_type ) {
    256                         // Add block style variation data under current block type.
    257                         $path = array( $block_type, 'variations', $variation_name );
    258                         _wp_array_set( $variations_data, $path, $variation_data );
    259                 }
    260         }
    261 
    262         return $variations_data;
    263 }
    264 
    265 /**
    266  * Merges variations data with existing theme.json data ensuring that the
    267  * current theme.json data values take precedence.
    268  *
    269  * @since 6.6.0
    270  * @access private
    271  *
    272  * @param array              $variations_data Block style variations data keyed by block type.
    273  * @param WP_Theme_JSON_Data $theme_json      Current theme.json data.
    274  * @param string             $origin          Origin for the theme.json data.
    275  *
    276  * @return WP_Theme_JSON The merged theme.json data.
    277  */
    278 function wp_merge_block_style_variations_data( $variations_data, $theme_json, $origin = 'theme' ) {
    279         if ( empty( $variations_data ) ) {
    280                 return $theme_json;
    281         }
    282 
    283         $variations_theme_json_data = array(
    284                 'version' => WP_Theme_JSON::LATEST_SCHEMA,
    285                 'styles'  => array( 'blocks' => $variations_data ),
    286         );
    287 
    288         $variations_theme_json = new WP_Theme_JSON_Data( $variations_theme_json_data, $origin );
    289 
    290         /*
    291          * Merge the current theme.json data over shared variation data so that
    292          * any explicit per block variation values take precedence.
    293          */
    294         return $variations_theme_json->update_with( $theme_json->get_data() );
    295 }
    296 
    297 /**
    298  * Merges any shared block style variation definitions from a theme style
    299  * variation into their appropriate block type within theme json styles. Any
    300  * custom user selections already made will take precedence over the shared
    301  * style variation value.
    302  *
    303  * @since 6.6.0
    304  * @access private
    305  *
    306  * @param WP_Theme_JSON_Data $theme_json Current theme.json data.
    307  *
    308  * @return WP_Theme_JSON_Data
    309  */
    310 function wp_resolve_block_style_variations_from_theme_style_variation( $theme_json ) {
    311         $theme_json_data   = $theme_json->get_data();
    312         $shared_variations = $theme_json_data['styles']['blocks']['variations'] ?? array();
    313         $variations_data   = wp_resolve_block_style_variations( $shared_variations );
    314 
    315         return wp_merge_block_style_variations_data( $variations_data, $theme_json, 'user' );
    316 }
    317 
    318 /**
    319  * Merges block style variation data sourced from standalone partial
    320  * theme.json files.
    321  *
    322  * @since 6.6.0
    323  * @access private
    324  *
    325  * @param WP_Theme_JSON_Data $theme_json Current theme.json data.
    326  *
    327  * @return WP_Theme_JSON_Data
    328  */
    329 function wp_resolve_block_style_variations_from_theme_json_partials( $theme_json ) {
    330         $block_style_variations = WP_Theme_JSON_Resolver::get_style_variations( 'block' );
    331         $variations_data        = wp_resolve_block_style_variations( $block_style_variations );
    332 
    333         return wp_merge_block_style_variations_data( $variations_data, $theme_json );
    334 }
    335 
    336 /**
    337  * Merges shared block style variations registered within the
    338  * `styles.blocks.variations` property of the primary theme.json file.
    339  *
    340  * @since 6.6.0
    341  * @access private
    342  *
    343  * @param WP_Theme_JSON_Data $theme_json Current theme.json data.
    344  *
    345  * @return WP_Theme_JSON_Data
    346  */
    347 function wp_resolve_block_style_variations_from_primary_theme_json( $theme_json ) {
    348         $theme_json_data        = $theme_json->get_data();
    349         $block_style_variations = $theme_json_data['styles']['blocks']['variations'] ?? array();
    350         $variations_data        = wp_resolve_block_style_variations( $block_style_variations );
    351 
    352         return wp_merge_block_style_variations_data( $variations_data, $theme_json );
    353 }
    354 
    355 /**
    356  * Merges block style variations registered via the block styles registry with a
    357  * style object, under their appropriate block types within theme.json styles.
    358  * Any variation values defined within the theme.json specific to a block type
    359  * will take precedence over these shared definitions.
    360  *
    361  * @since 6.6.0
    362  * @access private
    363  *
    364  * @param WP_Theme_JSON_Data $theme_json Current theme.json data.
    365  *
    366  * @return WP_Theme_JSON_Data
    367  */
    368 function wp_resolve_block_style_variations_from_styles_registry( $theme_json ) {
    369         $registry        = WP_Block_Styles_Registry::get_instance();
    370         $styles          = $registry->get_all_registered();
    371         $variations_data = array();
    372 
    373         foreach ( $styles as $block_type => $variations ) {
    374                 foreach ( $variations as $variation_name => $variation ) {
    375                         if ( ! empty( $variation['style_data'] ) ) {
    376                                 $path = array( $block_type, 'variations', $variation_name );
    377                                 _wp_array_set( $variations_data, $path, $variation['style_data'] );
    378                         }
    379                 }
    380         }
    381 
    382         return wp_merge_block_style_variations_data( $variations_data, $theme_json );
    383 }
    384 
    385 /**
    386216 * Enqueues styles for block style variations.
    387217 *
     
    400230add_action( 'wp_enqueue_scripts', 'wp_enqueue_block_style_variation_styles', 1 );
    401231
    402 // Resolve block style variations from all their potential sources. The order here is deliberate.
    403 add_filter( 'wp_theme_json_data_theme', 'wp_resolve_block_style_variations_from_primary_theme_json', 10, 1 );
    404 add_filter( 'wp_theme_json_data_theme', 'wp_resolve_block_style_variations_from_theme_json_partials', 10, 1 );
    405 add_filter( 'wp_theme_json_data_theme', 'wp_resolve_block_style_variations_from_styles_registry', 10, 1 );
    406 
    407 add_filter( 'wp_theme_json_data_user', 'wp_resolve_block_style_variations_from_theme_style_variation', 10, 1 );
    408 
    409 /**
    410  * Registers any block style variations contained within the provided
    411  * theme.json data.
     232/**
     233 * Registers block style variations read in from theme.json partials.
    412234 *
    413235 * @since 6.6.0
     
    416238 * @param array $variations Shared block style variations.
    417239 */
    418 function wp_register_block_style_variations_from_theme_json_data( $variations ) {
     240function wp_register_block_style_variations_from_theme_json_partials( $variations ) {
    419241        if ( empty( $variations ) ) {
    420                 return $variations;
    421         }
    422 
    423         $registry              = WP_Block_Styles_Registry::get_instance();
    424         $have_named_variations = ! wp_is_numeric_array( $variations );
    425 
    426         foreach ( $variations as $key => $variation ) {
    427                 $supported_blocks = $variation['blockTypes'] ?? array();
    428 
    429                 /*
    430                  * Standalone theme.json partial files for block style variations
    431                  * will have their styles under a top-level property by the same name.
    432                  * Variations defined within an existing theme.json or theme style
    433                  * variation will themselves already be the required styles data.
    434                  */
    435                 $variation_data = $variation['styles'] ?? $variation;
    436 
    437                 if ( empty( $variation_data ) ) {
     242                return;
     243        }
     244
     245        $registry = WP_Block_Styles_Registry::get_instance();
     246
     247        foreach ( $variations as $variation ) {
     248                if ( empty( $variation['blockTypes'] ) || empty( $variation['styles'] ) ) {
    438249                        continue;
    439250                }
    440251
    441                 /*
    442                  * Block style variations read in via standalone theme.json partials
    443                  * need to have their name set to the kebab case version of their title.
    444                  */
    445                 $variation_name  = $have_named_variations ? $key : ( $variation['slug'] ?? _wp_to_kebab_case( $variation['title'] ) );
     252                $variation_name  = $variation['slug'] ?? _wp_to_kebab_case( $variation['title'] );
    446253                $variation_label = $variation['title'] ?? $variation_name;
    447254
    448                 foreach ( $supported_blocks as $block_type ) {
     255                foreach ( $variation['blockTypes'] as $block_type ) {
    449256                        $registered_styles = $registry->get_registered_styles_for_block( $block_type );
    450257
  • 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}
  • trunk/src/wp-includes/class-wp-theme-json.php

    r58444 r58466  
    744744         *
    745745         * @since 5.8.0
    746          * @since 6.6.0 Key spacingScale by origin, and Pre-generate the
    747          *              spacingSizes from spacingScale.
     746         * @since 6.6.0 Key spacingScale by origin, and Pre-generate the spacingSizes from spacingScale.
     747         *              Added unwrapping of shared block style variations into block type variations if registered.
    748748         *
    749749         * @param array  $theme_json A structure that follows the theme.json schema.
     
    760760                $valid_element_names = array_keys( static::ELEMENTS );
    761761                $valid_variations    = static::get_valid_block_style_variations();
     762                $this->theme_json    = static::unwrap_shared_block_style_variations( $this->theme_json, $valid_variations );
    762763                $this->theme_json    = static::sanitize( $this->theme_json, $valid_block_names, $valid_element_names, $valid_variations );
    763764                $this->theme_json    = static::maybe_opt_in_into_settings( $this->theme_json );
     
    801802                        _wp_array_set( $this->theme_json, $sizes_path, $merged_spacing_sizes );
    802803                }
     804        }
     805
     806        /**
     807         * Unwraps shared block style variations.
     808         *
     809         * It takes the shared variations (styles.variations.variationName) and
     810         * applies them to all the blocks that have the given variation registered
     811         * (styles.blocks.blockType.variations.variationName).
     812         *
     813         * For example, given the `core/paragraph` and `core/group` blocks have
     814         * registered the `section-a` style variation, and given the following input:
     815         *
     816         * {
     817         *   "styles": {
     818         *     "variations": {
     819         *       "section-a": { "color": { "background": "backgroundColor" } }
     820         *     }
     821         *   }
     822         * }
     823         *
     824         * It returns the following output:
     825         *
     826         * {
     827         *   "styles": {
     828         *     "blocks": {
     829         *       "core/paragraph": {
     830         *         "variations": {
     831         *             "section-a": { "color": { "background": "backgroundColor" } }
     832         *         },
     833         *       },
     834         *       "core/group": {
     835         *         "variations": {
     836         *           "section-a": { "color": { "background": "backgroundColor" } }
     837         *         }
     838         *       }
     839         *     }
     840         *   }
     841         * }
     842         *
     843         * @since 6.6.0
     844         *
     845         * @param array $theme_json       A structure that follows the theme.json schema.
     846         * @param array $valid_variations Valid block style variations.
     847         * @return array Theme json data with shared variation definitions unwrapped under appropriate block types.
     848         */
     849        private static function unwrap_shared_block_style_variations( $theme_json, $valid_variations ) {
     850                if ( empty( $theme_json['styles']['variations'] ) || empty( $valid_variations ) ) {
     851                        return $theme_json;
     852                }
     853
     854                $new_theme_json = $theme_json;
     855                $variations     = $new_theme_json['styles']['variations'];
     856
     857                foreach ( $valid_variations as $block_type => $registered_variations ) {
     858                        foreach ( $registered_variations as $variation_name ) {
     859                                $block_level_data = $new_theme_json['styles']['blocks'][ $block_type ]['variations'][ $variation_name ] ?? array();
     860                                $top_level_data   = $variations[ $variation_name ] ?? array();
     861                                $merged_data      = array_replace_recursive( $top_level_data, $block_level_data );
     862                                if ( ! empty( $merged_data ) ) {
     863                                        _wp_array_set( $new_theme_json, array( 'styles', 'blocks', $block_type, 'variations', $variation_name ), $merged_data );
     864                                }
     865                        }
     866                }
     867
     868                unset( $new_theme_json['styles']['variations'] );
     869
     870                return $new_theme_json;
    803871        }
    804872
     
    9671035                $schema['settings']['blocks']                     = $schema_settings_blocks;
    9681036                $schema['settings']['typography']['fontFamilies'] = static::schema_in_root_and_per_origin( static::FONT_FAMILY_SCHEMA );
    969 
    970                 /*
    971                  * Shared block style variations can be registered from the theme.json data so we can't
    972                  * validate them against pre-registered block style variations.
    973                  */
    974                 $schema['styles']['blocks']['variations'] = null;
    9751037
    9761038                // Remove anything that's not present in the schema.
  • trunk/src/wp-includes/rest-api/endpoints/class-wp-rest-global-styles-controller.php

    r58429 r58466  
    266266                        }
    267267
    268                         // Register theme-defined variations.
    269                         WP_Theme_JSON_Resolver::get_theme_data();
    270 
    271                         // Register user-defined variations.
    272                         if ( ! empty( $config['styles']['blocks']['variations'] ) ) {
    273                                 wp_register_block_style_variations_from_theme_json_data( $config['styles']['blocks']['variations'] );
    274                         }
     268                        // Register theme-defined variations e.g. from block style variation partials under `/styles`.
     269                        $variations = WP_Theme_JSON_Resolver::get_style_variations( 'block' );
     270                        wp_register_block_style_variations_from_theme_json_partials( $variations );
    275271
    276272                        if ( isset( $request['settings'] ) ) {
     
    636632
    637633                $response   = array();
     634
     635                // Register theme-defined variations e.g. from block style variation partials under `/styles`.
     636                $partials = WP_Theme_JSON_Resolver::get_style_variations( 'block' );
     637                wp_register_block_style_variations_from_theme_json_partials( $partials );
     638
    638639                $variations = WP_Theme_JSON_Resolver::get_style_variations();
    639 
    640640                foreach ( $variations as $variation ) {
    641641                        $variation_theme_json = new WP_Theme_JSON( $variation );
  • trunk/src/wp-includes/theme-i18n.json

    r58412 r58466  
    8181                }
    8282        },
    83         "styles": {
    84                 "blocks": {
    85                         "variations": {
    86                                 "*": {
    87                                         "title": "Style variation name"
    88                                 }
    89                         }
    90                 }
    91         },
    9283        "customTemplates": [
    9384                {
  • trunk/tests/phpunit/data/themedir1/block-theme-child-with-block-style-variations/theme.json

    r58264 r58466  
    11{
    22        "$schema": "https://schemas.wp.org/trunk/theme.json",
    3         "version": 3
     3        "version": 3,
     4        "styles": {
     5                "variations": {
     6                        "outline": {
     7                                "color": {
     8                                        "background": "green",
     9                                        "text": "white"
     10                                }
     11                        },
     12                        "block-style-variation-a": {
     13                                "color": {
     14                                        "background": "darkseagreen"
     15                                },
     16                                "typography": {
     17                                        "fontSize": "2em",
     18                                        "lineHeight": "1.4em"
     19                                }
     20                        }
     21                },
     22                "blocks": {
     23                        "core/button": {
     24                                "variations": {
     25                                        "outline": {
     26                                                "color": {
     27                                                        "background": "red"
     28                                                }
     29                                        }
     30                                }
     31                        },
     32                        "core/media-text": {
     33                                "variations": {
     34                                        "block-style-variation-a": {
     35                                                "color": {
     36                                                        "background": "blue"
     37                                                },
     38                                                "typography": {
     39                                                        "fontSize": "1.5em"
     40                                                }
     41                                        }
     42                                }
     43                        },
     44                        "core/heading": {
     45                                "variations": {
     46                                        "block-style-variation-b": {
     47                                                "typography": {
     48                                                        "fontSize": "3em"
     49                                                }
     50                                        }
     51                                }
     52                        }
     53                }
     54        }
    455}
  • trunk/tests/phpunit/tests/block-supports/block-style-variations.php

    r58429 r58466  
    6363         * @ticket 61312
    6464         * @ticket 61440
     65         * @ticket 61451
    6566         */
    6667        public function test_add_registered_block_styles_to_theme_data() {
    6768                switch_theme( 'block-theme' );
    68 
    69                 // Register theme-defined variations.
    70                 WP_Theme_JSON_Resolver::get_theme_data();
    71                 // Register user-defined variations.
    72                 WP_Theme_JSON_Resolver::get_user_data();
    7369
    7470                $variation_styles_data = array(
     
    126122                $expected     = array(
    127123                        'variations' => array(
    128                                 // @ticket 61440
    129                                 'WithSlug'                => array(
    130                                         'color' => array(
    131                                                 'background' => 'aliceblue',
    132                                                 'text'       => 'midnightblue',
    133                                         ),
    134                                 ),
    135                                 'my-variation'            => $variation_styles_data,
    136124
    137125                                /*
     
    152140                                        ),
    153141                                ),
     142
     143                                /*
     144                                 * Manually registered variations.
     145                                 * @ticket 61440
     146                                 */
     147                                'WithSlug'                => array(
     148                                        'color' => array(
     149                                                'background' => 'aliceblue',
     150                                                'text'       => 'midnightblue',
     151                                        ),
     152                                ),
     153                                'my-variation'            => $variation_styles_data,
    154154                        ),
    155155                );
     
    158158                unregister_block_style( 'core/group', 'WithSlug' );
    159159
    160                 $this->assertSameSetsWithIndex( $expected, $group_styles );
     160                $this->assertSameSetsWithIndex( $expected, $group_styles, 'Variation data does not match' );
    161161        }
    162162}
  • trunk/tests/phpunit/tests/rest-api/rest-global-styles-controller.php

    r58394 r58466  
    610610         * @covers WP_REST_Global_Styles_Controller_Gutenberg::update_item
    611611         * @ticket 61312
     612         * @ticket 61451
    612613         */
    613614        public function test_update_item_with_custom_block_style_variations() {
     
    616617                        grant_super_admin( self::$admin_id );
    617618                }
     619
     620                /*
     621                 * For variations to be resolved they have to have been registered
     622                 * via either a theme.json partial or through the WP_Block_Styles_Registry.
     623                 */
     624                register_block_style(
     625                        'core/group',
     626                        array(
     627                                'name'  => 'fromThemeStyleVariation',
     628                                'label' => 'From Theme Style Variation',
     629                        )
     630                );
    618631
    619632                $group_variations = array(
     
    630643                        array(
    631644                                'styles' => array(
    632                                         'blocks' => array(
    633                                                 'variations' => array(
    634                                                         'fromThemeStyleVariation' => array(
    635                                                                 'blockTypes' => array( 'core/group', 'core/columns' ),
    636                                                                 'color'      => array(
    637                                                                         'background' => '#000000',
    638                                                                         'text'       => '#ffffff',
    639                                                                 ),
     645                                        'variations' => array(
     646                                                'fromThemeStyleVariation' => array(
     647                                                        'blockTypes' => array( 'core/group', 'core/columns' ),
     648                                                        'color'      => array(
     649                                                                'background' => '#000000',
     650                                                                'text'       => '#ffffff',
    640651                                                        ),
    641652                                                ),
     653                                        ),
     654                                        'blocks'     => array(
    642655                                                'core/group' => array(
    643656                                                        'variations' => $group_variations,
  • trunk/tests/phpunit/tests/theme/wpThemeJson.php

    r58444 r58466  
    39323932
    39333933        /**
     3934         * @ticket 61451
     3935         */
     3936        public function test_unwraps_block_style_variations() {
     3937                register_block_style(
     3938                        array( 'core/paragraph', 'core/group' ),
     3939                        array(
     3940                                'name'  => 'myVariation',
     3941                                'label' => 'My variation',
     3942                        )
     3943                );
     3944
     3945                $input = new WP_Theme_JSON(
     3946                        array(
     3947                                'version' => WP_Theme_JSON::LATEST_SCHEMA,
     3948                                'styles'  => array(
     3949                                        'variations' => array(
     3950                                                'myVariation' => array(
     3951                                                        'color'      => array(
     3952                                                                'background' => 'topLevel',
     3953                                                                'gradient'   => 'topLevel',
     3954                                                        ),
     3955                                                        'typography' => array(
     3956                                                                'fontFamily' => 'topLevel',
     3957                                                        ),
     3958                                                ),
     3959                                        ),
     3960                                        'blocks'     => array(
     3961                                                'core/paragraph' => array(
     3962                                                        'variations' => array(
     3963                                                                'myVariation' => array(
     3964                                                                        'color'   => array(
     3965                                                                                'background' => 'blockLevel',
     3966                                                                                'text'       => 'blockLevel',
     3967                                                                        ),
     3968                                                                        'outline' => array(
     3969                                                                                'offset' => 'blockLevel',
     3970                                                                        ),
     3971                                                                ),
     3972                                                        ),
     3973                                                ),
     3974                                        ),
     3975                                ),
     3976                        )
     3977                );
     3978
     3979                $expected = array(
     3980                        'version' => WP_Theme_JSON::LATEST_SCHEMA,
     3981                        'styles'  => array(
     3982                                'blocks' => array(
     3983                                        'core/paragraph' => array(
     3984                                                'variations' => array(
     3985                                                        'myVariation' => array(
     3986                                                                'color'      => array(
     3987                                                                        'background' => 'blockLevel',
     3988                                                                        'gradient'   => 'topLevel',
     3989                                                                        'text'       => 'blockLevel',
     3990                                                                ),
     3991                                                                'typography' => array(
     3992                                                                        'fontFamily' => 'topLevel',
     3993                                                                ),
     3994                                                                'outline'    => array(
     3995                                                                        'offset' => 'blockLevel',
     3996                                                                ),
     3997                                                        ),
     3998                                                ),
     3999                                        ),
     4000                                        'core/group'     => array(
     4001                                                'variations' => array(
     4002                                                        'myVariation' => array(
     4003                                                                'color'      => array(
     4004                                                                        'background' => 'topLevel',
     4005                                                                        'gradient'   => 'topLevel',
     4006                                                                ),
     4007                                                                'typography' => array(
     4008                                                                        'fontFamily' => 'topLevel',
     4009                                                                ),
     4010                                                        ),
     4011                                                ),
     4012                                        ),
     4013                                ),
     4014                        ),
     4015                );
     4016                $this->assertSameSetsWithIndex( $expected, $input->get_raw_data(), 'Unwrapped block style variations do not match' );
     4017        }
     4018
     4019        /**
    39344020         * @ticket 57583
    39354021         *
  • trunk/tests/phpunit/tests/theme/wpThemeJsonResolver.php

    r58413 r58466  
    13221322                $this->assertSame( $expected_data, $actual );
    13231323        }
     1324
     1325        /**
     1326         * Tests that block style variations data gets merged in the following
     1327         * priority order, from highest priority to lowest.
     1328         *
     1329         * - `styles.blocks.blockType.variations` from theme.json
     1330         * - `styles.variations` from theme.json
     1331         * - variations from block style variation files under `/styles`
     1332         * - variations from `WP_Block_Styles_Registry`
     1333         *
     1334         * @ticket 61451
     1335         */
     1336        public function test_block_style_variation_merge_order() {
     1337                switch_theme( 'block-theme-child-with-block-style-variations' );
     1338
     1339                /*
     1340                 * Register style for a block that isn't included in the block style variation's partial
     1341                 * theme.json's blockTypes. The name must match though so we can ensure the partial's
     1342                 * styles do not get applied to this block.
     1343                 */
     1344                register_block_style(
     1345                        'core/heading',
     1346                        array(
     1347                                'name'  => 'block-style-variation-b',
     1348                                'label' => 'Heading only variation',
     1349                        )
     1350                );
     1351
     1352                // Register variation for a block that will be partially overridden at all levels.
     1353                register_block_style(
     1354                        'core/media-text',
     1355                        array(
     1356                                'name'       => 'block-style-variation-a',
     1357                                'label'      => 'Block Style Variation A',
     1358                                'style_data' => array(
     1359                                        'color' => array(
     1360                                                'background' => 'pink',
     1361                                                'gradient'   => 'var(--custom)',
     1362                                        ),
     1363                                ),
     1364                        )
     1365                );
     1366
     1367                $data         = WP_Theme_JSON_Resolver::get_theme_data()->get_raw_data();
     1368                $block_styles = $data['styles']['blocks'] ?? array();
     1369                $actual       = array_intersect_key(
     1370                        $block_styles,
     1371                        array_flip( array( 'core/button', 'core/media-text', 'core/heading' ) )
     1372                );
     1373                $expected     = array(
     1374                        'core/button'     => array(
     1375                                'variations' => array(
     1376                                        'outline' => array(
     1377                                                'color' => array(
     1378                                                        'background' => 'red',
     1379                                                        'text'       => 'white',
     1380                                                ),
     1381                                        ),
     1382                                ),
     1383                        ),
     1384                        'core/media-text' => array(
     1385                                'variations' => array(
     1386                                        'block-style-variation-a' => array(
     1387                                                'color'      => array(
     1388                                                        'background' => 'blue',
     1389                                                        'gradient'   => 'var(--custom)',
     1390                                                        'text'       => 'aliceblue',
     1391                                                ),
     1392                                                'typography' => array(
     1393                                                        'fontSize'   => '1.5em',
     1394                                                        'lineHeight' => '1.4em',
     1395                                                ),
     1396                                        ),
     1397                                ),
     1398                        ),
     1399                        'core/heading'    => array(
     1400                                'variations' => array(
     1401                                        'block-style-variation-b' => array(
     1402                                                'typography' => array(
     1403                                                        'fontSize' => '3em',
     1404                                                ),
     1405                                        ),
     1406                                ),
     1407                        ),
     1408                );
     1409
     1410                unregister_block_style( 'core/heading', 'block-style-variation-b' );
     1411                unregister_block_style( 'core/media-text', 'block-style-variation-a' );
     1412
     1413                $this->assertSameSetsWithIndex( $expected, $actual, 'Merged variation styles do not match.' );
     1414        }
    13241415}
Note: See TracChangeset for help on using the changeset viewer.