Make WordPress Core


Ignore:
Timestamp:
06/24/2024 08:49:52 AM (13 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/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
Note: See TracChangeset for help on using the changeset viewer.