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

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

Built from https://develop.svn.wordpress.org/trunk@58429


git-svn-id: http://core.svn.wordpress.org/trunk@57878 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
oandregal 2024-06-18 07:09:06 +00:00
parent e820f8cdf9
commit 9a8a0b3fcd
4 changed files with 22 additions and 73 deletions

View File

@ -461,58 +461,3 @@ function wp_register_block_style_variations_from_theme_json_data( $variations )
}
}
}
/**
* Register shared block style variations defined by the theme.
*
* These can come in three forms:
* - the theme's theme.json
* - the theme's partials (standalone files in `/styles` that only define block style variations)
* - the user's theme.json (for example, theme style variations the user selected)
*
* @since 6.6.0
* @access private
*/
function wp_register_block_style_variations_from_theme() {
/*
* Skip any registration of styles if no theme.json or variation partials are present.
*
* Given the possibility of hybrid themes, this check can't rely on if the theme
* is a block theme or not. Instead:
* - If there is a primary theme.json, continue.
* - If there is a partials directory, continue.
* - The only variations to be registered from the global styles user origin,
* are those that have been copied in from the selected theme style variation.
* For a theme style variation to be selected it would have to have a partial
* theme.json file covered by the previous check.
*/
$has_partials_directory = is_dir( get_stylesheet_directory() . '/styles' ) || is_dir( get_template_directory() . '/styles' );
if ( ! wp_theme_has_theme_json() && ! $has_partials_directory ) {
return;
}
// Partials from `/styles`.
$variations_partials = WP_Theme_JSON_Resolver::get_style_variations( 'block' );
wp_register_block_style_variations_from_theme_json_data( $variations_partials );
/*
* Pull the data from the specific origin instead of the merged data.
* This is because, for 6.6, we only support registering block style variations
* for the 'theme' and 'custom' origins but not for 'default' (core theme.json)
* or 'custom' (theme.json in a block).
*
* When/If we add support for every origin, we should switch to using the public API
* instead, e.g.: wp_get_global_styles( array( 'blocks', 'variations' ) ).
*/
// theme.json of the theme.
$theme_json_theme = WP_Theme_JSON_Resolver::get_theme_data();
$variations_theme = $theme_json_theme->get_data()['styles']['blocks']['variations'] ?? array();
wp_register_block_style_variations_from_theme_json_data( $variations_theme );
// User data linked for this theme.
$theme_json_user = WP_Theme_JSON_Resolver::get_user_data();
$variations_user = $theme_json_user->get_data()['styles']['blocks']['variations'] ?? array();
wp_register_block_style_variations_from_theme_json_data( $variations_user );
}
add_action( 'init', 'wp_register_block_style_variations_from_theme' );

View File

@ -221,6 +221,7 @@ class WP_Theme_JSON_Resolver {
* @since 5.9.0 Theme supports have been inlined and the `$theme_support_data` argument removed.
* @since 6.0.0 Added an `$options` parameter to allow the theme data to be returned without theme supports.
* @since 6.6.0 Add support for 'default-font-sizes' and 'default-spacing-sizes' theme supports.
* Register the block style variations coming from the partials and the theme.json.
*
* @param array $deprecated Deprecated. Not used.
* @param array $options {
@ -247,6 +248,14 @@ class WP_Theme_JSON_Resolver {
$theme_json_data = array( 'version' => WP_Theme_JSON::LATEST_SCHEMA );
}
// Register variations defined by the theme.
$variations = $theme_json_data['styles']['blocks']['variations'] ?? array();
wp_register_block_style_variations_from_theme_json_data( $variations );
// Register variations defined by theme partials (theme.json files in the styles directory).
$variations = static::get_style_variations( 'block' );
wp_register_block_style_variations_from_theme_json_data( $variations );
/**
* Filters the data provided by the theme for global styles and settings.
*
@ -488,6 +497,7 @@ class WP_Theme_JSON_Resolver {
*
* @since 5.9.0
* @since 6.6.0 The 'isGlobalStylesUserThemeJSON' flag is left on the user data.
* Register the block style variations coming from the user data.
*
* @return WP_Theme_JSON Entity that holds styles for user data.
*/
@ -528,6 +538,10 @@ class WP_Theme_JSON_Resolver {
unset( $decoded_data['isGlobalStylesUserThemeJSON'] );
$config = $decoded_data;
}
// Register variations defined by the user.
$variations = $config['styles']['blocks']['variations'] ?? array();
wp_register_block_style_variations_from_theme_json_data( $variations );
}
/** This filter is documented in wp-includes/class-wp-theme-json-resolver.php */

View File

@ -231,7 +231,7 @@ class WP_REST_Global_Styles_Controller extends WP_REST_Posts_Controller {
*
* @since 5.9.0
* @since 6.2.0 Added validation of styles.css property.
* @since 6.6.0 Added registration of newly created style variations provided by the user.
* @since 6.6.0 Added registration of block style variations from theme.json sources (theme.json, user theme.json, partials).
*
* @param WP_REST_Request $request Request object.
* @return stdClass|WP_Error Prepared item on success. WP_Error on when the custom CSS is not valid.
@ -265,22 +265,12 @@ class WP_REST_Global_Styles_Controller extends WP_REST_Posts_Controller {
$config['styles'] = $existing_config['styles'];
}
/*
* If the incoming request is going to create a new variation
* that is not yet registered, we register it here.
* This is because the variations are registered on init,
* but we want this endpoint to return the new variation immediately:
* if we don't register it, it'll be stripped out of the response
* just in this request (subsequent ones will be ok).
* Take the variations defined in styles.blocks.variations from the incoming request
* that are not part of the $exsting_config.
*/
if ( isset( $request['styles']['blocks']['variations'] ) ) {
$existing_variations = isset( $existing_config['styles']['blocks']['variations'] ) ? $existing_config['styles']['blocks']['variations'] : array();
$new_variations = array_diff_key( $request['styles']['blocks']['variations'], $existing_variations );
if ( ! empty( $new_variations ) ) {
wp_register_block_style_variations_from_theme_json_data( $new_variations );
}
// Register theme-defined variations.
WP_Theme_JSON_Resolver::get_theme_data();
// Register user-defined variations.
if ( ! empty( $config['styles']['blocks']['variations'] ) ) {
wp_register_block_style_variations_from_theme_json_data( $config['styles']['blocks']['variations'] );
}
if ( isset( $request['settings'] ) ) {

View File

@ -16,7 +16,7 @@
*
* @global string $wp_version
*/
$wp_version = '6.6-beta2-58428';
$wp_version = '6.6-beta2-58429';
/**
* Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.