Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

55 changes: 0 additions & 55 deletions src/wp-includes/block-supports/block-style-variations.php
Original file line number Diff line number Diff line change
Expand Up @@ -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' );
14 changes: 14 additions & 0 deletions src/wp-includes/class-wp-theme-json-resolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,7 @@ protected static function has_same_registered_blocks( $origin ) {
* @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 {
Expand All @@ -247,6 +248,14 @@ public static function get_theme_data( $deprecated = array(), $options = array()
$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.
*
Expand Down Expand Up @@ -488,6 +497,7 @@ public static function get_user_data_from_wp_global_styles( $theme, $create_post
*
* @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.
*/
Expand Down Expand Up @@ -528,6 +538,10 @@ public static function get_user_data() {
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 */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,7 @@ public function update_item_permissions_check( $request ) {
*
* @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.
Expand Down Expand Up @@ -265,22 +265,12 @@ protected function prepare_item_for_database( $request ) {
$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'] ) ) {
Expand Down
10 changes: 4 additions & 6 deletions tests/phpunit/tests/block-supports/block-style-variations.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,12 +66,10 @@ public function filter_set_theme_root() {
public function test_add_registered_block_styles_to_theme_data() {
switch_theme( 'block-theme' );

/*
* Trigger block style registration that occurs on `init` action.
* do_action( 'init' ) could be used here however this direct call
* means only the updates being tested are performed.
*/
wp_register_block_style_variations_from_theme();
// Register theme-defined variations.
WP_Theme_JSON_Resolver::get_theme_data();
// Register user-defined variations.
WP_Theme_JSON_Resolver::get_user_data();

$variation_styles_data = array(
'color' => array(
Expand Down
Loading