Make WordPress Core


Ignore:
Timestamp:
06/18/2024 07:07:52 AM (6 months ago)
Author:
oandregal
Message:

Do not use init to register block style variations defined via theme.json.

Props oandregal, aaronrobertshaw, joemcgill, ramonopoly, andrewserong, swissspidy.
See #61451.
Fixes #61312.

File:
1 edited

Legend:

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

    r58423 r58429  
    462462    }
    463463}
    464 
    465 /**
    466  * Register shared block style variations defined by the theme.
    467  *
    468  * These can come in three forms:
    469  * - the theme's theme.json
    470  * - the theme's partials (standalone files in `/styles` that only define block style variations)
    471  * - the user's theme.json (for example, theme style variations the user selected)
    472  *
    473  * @since 6.6.0
    474  * @access private
    475  */
    476 function wp_register_block_style_variations_from_theme() {
    477     /*
    478      * Skip any registration of styles if no theme.json or variation partials are present.
    479      *
    480      * Given the possibility of hybrid themes, this check can't rely on if the theme
    481      * is a block theme or not. Instead:
    482      *   - If there is a primary theme.json, continue.
    483      *   - If there is a partials directory, continue.
    484      *   - The only variations to be registered from the global styles user origin,
    485      *     are those that have been copied in from the selected theme style variation.
    486      *     For a theme style variation to be selected it would have to have a partial
    487      *     theme.json file covered by the previous check.
    488      */
    489     $has_partials_directory = is_dir( get_stylesheet_directory() . '/styles' ) || is_dir( get_template_directory() . '/styles' );
    490     if ( ! wp_theme_has_theme_json() && ! $has_partials_directory ) {
    491         return;
    492     }
    493 
    494     // Partials from `/styles`.
    495     $variations_partials = WP_Theme_JSON_Resolver::get_style_variations( 'block' );
    496     wp_register_block_style_variations_from_theme_json_data( $variations_partials );
    497 
    498     /*
    499      * Pull the data from the specific origin instead of the merged data.
    500      * This is because, for 6.6, we only support registering block style variations
    501      * for the 'theme' and 'custom' origins but not for 'default' (core theme.json)
    502      * or 'custom' (theme.json in a block).
    503      *
    504      * When/If we add support for every origin, we should switch to using the public API
    505      * instead, e.g.: wp_get_global_styles( array( 'blocks', 'variations' ) ).
    506      */
    507 
    508     // theme.json of the theme.
    509     $theme_json_theme = WP_Theme_JSON_Resolver::get_theme_data();
    510     $variations_theme = $theme_json_theme->get_data()['styles']['blocks']['variations'] ?? array();
    511     wp_register_block_style_variations_from_theme_json_data( $variations_theme );
    512 
    513     // User data linked for this theme.
    514     $theme_json_user = WP_Theme_JSON_Resolver::get_user_data();
    515     $variations_user = $theme_json_user->get_data()['styles']['blocks']['variations'] ?? array();
    516     wp_register_block_style_variations_from_theme_json_data( $variations_user );
    517 }
    518 add_action( 'init', 'wp_register_block_style_variations_from_theme' );
Note: See TracChangeset for help on using the changeset viewer.