Make WordPress Core

Changeset 58429


Ignore:
Timestamp:
06/18/2024 07:07:52 AM (4 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.

Location:
trunk
Files:
4 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' );
  • trunk/src/wp-includes/class-wp-theme-json-resolver.php

    r58428 r58429  
    222222     * @since 6.0.0 Added an `$options` parameter to allow the theme data to be returned without theme supports.
    223223     * @since 6.6.0 Add support for 'default-font-sizes' and 'default-spacing-sizes' theme supports.
     224     *              Register the block style variations coming from the partials and the theme.json.
    224225     *
    225226     * @param array $deprecated Deprecated. Not used.
     
    247248                $theme_json_data = array( 'version' => WP_Theme_JSON::LATEST_SCHEMA );
    248249            }
     250
     251            // Register variations defined by the theme.
     252            $variations = $theme_json_data['styles']['blocks']['variations'] ?? array();
     253            wp_register_block_style_variations_from_theme_json_data( $variations );
     254
     255            // Register variations defined by theme partials (theme.json files in the styles directory).
     256            $variations = static::get_style_variations( 'block' );
     257            wp_register_block_style_variations_from_theme_json_data( $variations );
    249258
    250259            /**
     
    489498     * @since 5.9.0
    490499     * @since 6.6.0 The 'isGlobalStylesUserThemeJSON' flag is left on the user data.
     500     *              Register the block style variations coming from the user data.
    491501     *
    492502     * @return WP_Theme_JSON Entity that holds styles for user data.
     
    529539                $config = $decoded_data;
    530540            }
     541
     542            // Register variations defined by the user.
     543            $variations = $config['styles']['blocks']['variations'] ?? array();
     544            wp_register_block_style_variations_from_theme_json_data( $variations );
    531545        }
    532546
  • trunk/src/wp-includes/rest-api/endpoints/class-wp-rest-global-styles-controller.php

    r58394 r58429  
    232232     * @since 5.9.0
    233233     * @since 6.2.0 Added validation of styles.css property.
    234      * @since 6.6.0 Added registration of newly created style variations provided by the user.
     234     * @since 6.6.0 Added registration of block style variations from theme.json sources (theme.json, user theme.json, partials).
    235235     *
    236236     * @param WP_REST_Request $request Request object.
     
    266266            }
    267267
    268             /*
    269              * If the incoming request is going to create a new variation
    270              * that is not yet registered, we register it here.
    271              * This is because the variations are registered on init,
    272              * but we want this endpoint to return the new variation immediately:
    273              * if we don't register it, it'll be stripped out of the response
    274              * just in this request (subsequent ones will be ok).
    275              * Take the variations defined in styles.blocks.variations from the incoming request
    276              * that are not part of the $exsting_config.
    277              */
    278             if ( isset( $request['styles']['blocks']['variations'] ) ) {
    279                 $existing_variations = isset( $existing_config['styles']['blocks']['variations'] ) ? $existing_config['styles']['blocks']['variations'] : array();
    280                 $new_variations      = array_diff_key( $request['styles']['blocks']['variations'], $existing_variations );
    281                 if ( ! empty( $new_variations ) ) {
    282                     wp_register_block_style_variations_from_theme_json_data( $new_variations );
    283                 }
     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'] );
    284274            }
    285275
  • trunk/tests/phpunit/tests/block-supports/block-style-variations.php

    r58413 r58429  
    6767        switch_theme( 'block-theme' );
    6868
    69         /*
    70          * Trigger block style registration that occurs on `init` action.
    71          * do_action( 'init' ) could be used here however this direct call
    72          * means only the updates being tested are performed.
    73          */
    74         wp_register_block_style_variations_from_theme();
     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();
    7573
    7674        $variation_styles_data = array(
Note: See TracChangeset for help on using the changeset viewer.