diff --git a/backport-changelog/6.6/6873.md b/backport-changelog/6.6/6873.md new file mode 100644 index 0000000000000..745966d0dc040 --- /dev/null +++ b/backport-changelog/6.6/6873.md @@ -0,0 +1,3 @@ +https://github.com/WordPress/wordpress-develop/pull/6873 + +* https://github.com/WordPress/gutenberg/pull/62712 diff --git a/lib/block-supports/block-style-variations.php b/lib/block-supports/block-style-variations.php index 4adefa0f4e440..12c2453681b41 100644 --- a/lib/block-supports/block-style-variations.php +++ b/lib/block-supports/block-style-variations.php @@ -208,170 +208,6 @@ function gutenberg_render_block_style_variation_class_name( $block_content, $blo return $tags->get_updated_html(); } -/** - * Collects block style variation data for merging with theme.json data. - * - * @since 6.6.0 - * - * @param array $variations Shared block style variations. - * - * @return array Block variations data to be merged under `styles.blocks`. - */ -function gutenberg_resolve_block_style_variations( $variations ) { - $variations_data = array(); - - if ( empty( $variations ) ) { - return $variations_data; - } - - $have_named_variations = ! wp_is_numeric_array( $variations ); - - foreach ( $variations as $key => $variation ) { - $supported_blocks = $variation['blockTypes'] ?? array(); - - /* - * Standalone theme.json partial files for block style variations - * will have their styles under a top-level property by the same name. - * Variations defined within an existing theme.json or theme style - * variation will themselves already be the required styles data. - */ - $variation_data = $variation['styles'] ?? $variation; - - if ( empty( $variation_data ) ) { - continue; - } - - /* - * Block style variations read in via standalone theme.json partials - * need to have their name set to the kebab case version of their title. - */ - $variation_name = $have_named_variations ? $key : ( $variation['slug'] ?? _wp_to_kebab_case( $variation['title'] ) ); - - foreach ( $supported_blocks as $block_type ) { - // Add block style variation data under current block type. - $path = array( $block_type, 'variations', $variation_name ); - _wp_array_set( $variations_data, $path, $variation_data ); - } - } - - return $variations_data; -} - -/** - * Merges variations data with existing theme.json data ensuring that the - * current theme.json data values take precedence. - * - * @since 6.6.0 - * - * @param array $variations_data Block style variations data keyed by block type. - * @param WP_Theme_JSON_Data_Gutenberg $theme_json Current theme.json data. - * @param string $origin Origin for the theme.json data. - * - * @return WP_Theme_JSON_Gutenberg The merged theme.json data. - */ -function gutenberg_merge_block_style_variations_data( $variations_data, $theme_json, $origin = 'theme' ) { - if ( empty( $variations_data ) ) { - return $theme_json; - } - - $variations_theme_json_data = array( - 'version' => WP_Theme_JSON_Gutenberg::LATEST_SCHEMA, - 'styles' => array( 'blocks' => $variations_data ), - ); - - $variations_theme_json = new WP_Theme_JSON_Data_Gutenberg( $variations_theme_json_data, $origin ); - - /* - * Merge the current theme.json data over shared variation data so that - * any explicit per block variation values take precedence. - */ - return $variations_theme_json->update_with( $theme_json->get_data() ); -} - -/** - * Merges any shared block style variation definitions from a theme style - * variation into their appropriate block type within theme json styles. Any - * custom user selections already made will take precedence over the shared - * style variation value. - * - * @since 6.6.0 - * - * @param WP_Theme_JSON_Data_Gutenberg $theme_json Current theme.json data. - * - * @return WP_Theme_JSON_Data_Gutenberg - */ -function gutenberg_resolve_block_style_variations_from_theme_style_variation( $theme_json ) { - $theme_json_data = $theme_json->get_data(); - $shared_variations = $theme_json_data['styles']['blocks']['variations'] ?? array(); - $variations_data = gutenberg_resolve_block_style_variations( $shared_variations ); - - return gutenberg_merge_block_style_variations_data( $variations_data, $theme_json, 'user' ); -} - -/** - * Merges block style variation data sourced from standalone partial - * theme.json files. - * - * @since 6.6.0 - * - * @param WP_Theme_JSON_Data_Gutenberg $theme_json Current theme.json data. - * - * @return WP_Theme_JSON_Data_Gutenberg - */ -function gutenberg_resolve_block_style_variations_from_theme_json_partials( $theme_json ) { - $block_style_variations = WP_Theme_JSON_Resolver_Gutenberg::get_style_variations( 'block' ); - $variations_data = gutenberg_resolve_block_style_variations( $block_style_variations ); - - return gutenberg_merge_block_style_variations_data( $variations_data, $theme_json ); -} - -/** - * Merges shared block style variations registered within the - * `styles.blocks.variations` property of the primary theme.json file. - * - * @since 6.6.0 - * - * @param WP_Theme_JSON_Data_Gutenberg $theme_json Current theme.json data. - * - * @return WP_Theme_JSON_Data_Gutenberg - */ -function gutenberg_resolve_block_style_variations_from_primary_theme_json( $theme_json ) { - $theme_json_data = $theme_json->get_data(); - $block_style_variations = $theme_json_data['styles']['blocks']['variations'] ?? array(); - $variations_data = gutenberg_resolve_block_style_variations( $block_style_variations ); - - return gutenberg_merge_block_style_variations_data( $variations_data, $theme_json ); -} - -/** - * Merges block style variations registered via the block styles registry with a - * style object, under their appropriate block types within theme.json styles. - * Any variation values defined within the theme.json specific to a block type - * will take precedence over these shared definitions. - * - * @since 6.6.0 - * - * @param WP_Theme_JSON_Data_Gutenberg $theme_json Current theme.json data. - * - * @return WP_Theme_JSON_Data_Gutenberg - */ -function gutenberg_resolve_block_style_variations_from_styles_registry( $theme_json ) { - $registry = WP_Block_Styles_Registry::get_instance(); - $styles = $registry->get_all_registered(); - $variations_data = array(); - - foreach ( $styles as $block_type => $variations ) { - foreach ( $variations as $variation_name => $variation ) { - if ( ! empty( $variation['style_data'] ) ) { - $path = array( $block_type, 'variations', $variation_name ); - _wp_array_set( $variations_data, $path, $variation['style_data'] ); - } - } - } - - return gutenberg_merge_block_style_variations_data( $variations_data, $theme_json ); -} - /** * Enqueues styles for block style variations. * @@ -395,71 +231,34 @@ function gutenberg_enqueue_block_style_variation_styles() { remove_action( 'wp_enqueue_scripts', 'wp_enqueue_block_style_variation_styles' ); } -if ( function_exists( 'wp_resolve_block_style_variations_from_primary_theme_json' ) ) { - remove_filter( 'wp_theme_json_data_theme', 'wp_resolve_block_style_variations_from_primary_theme_json' ); -} -if ( function_exists( 'wp_resolve_block_style_variations_from_theme_json_partials' ) ) { - remove_filter( 'wp_theme_json_data_theme', 'wp_resolve_block_style_variations_from_theme_json_partials' ); -} -if ( function_exists( 'wp_resolve_block_style_variations_from_styles_registry' ) ) { - remove_filter( 'wp_theme_json_data_theme', 'wp_resolve_block_style_variations_from_styles_registry' ); -} -if ( function_exists( 'wp_resolve_block_style_variations_from_theme_style_variation' ) ) { - remove_filter( 'wp_theme_json_data_user', 'wp_resolve_block_style_variations_from_theme_style_variation' ); -} - // Add Gutenberg filters and action. add_filter( 'render_block_data', 'gutenberg_render_block_style_variation_support_styles', 10, 2 ); add_filter( 'render_block', 'gutenberg_render_block_style_variation_class_name', 10, 2 ); add_action( 'wp_enqueue_scripts', 'gutenberg_enqueue_block_style_variation_styles', 1 ); -// Resolve block style variations from all their potential sources. The order here is deliberate. -add_filter( 'wp_theme_json_data_theme', 'gutenberg_resolve_block_style_variations_from_primary_theme_json', 10, 1 ); -add_filter( 'wp_theme_json_data_theme', 'gutenberg_resolve_block_style_variations_from_theme_json_partials', 10, 1 ); -add_filter( 'wp_theme_json_data_theme', 'gutenberg_resolve_block_style_variations_from_styles_registry', 10, 1 ); - -add_filter( 'wp_theme_json_data_user', 'gutenberg_resolve_block_style_variations_from_theme_style_variation', 10, 1 ); - - /** - * Registers any block style variations contained within the provided - * theme.json data. + * Registers block style variations read in from theme.json partials. * * @access private * * @param array $variations Shared block style variations. */ -function gutenberg_register_block_style_variations_from_theme_json_data( $variations ) { +function gutenberg_register_block_style_variations_from_theme_json_partials( $variations ) { if ( empty( $variations ) ) { return; } - $registry = WP_Block_Styles_Registry::get_instance(); - $have_named_variations = ! wp_is_numeric_array( $variations ); - - foreach ( $variations as $key => $variation ) { - $supported_blocks = $variation['blockTypes'] ?? array(); + $registry = WP_Block_Styles_Registry::get_instance(); - /* - * Standalone theme.json partial files for block style variations - * will have their styles under a top-level property by the same name. - * Variations defined within an existing theme.json or theme style - * variation will themselves already be the required styles data. - */ - $variation_data = $variation['styles'] ?? $variation; - - if ( empty( $variation_data ) ) { + foreach ( $variations as $variation ) { + if ( empty( $variation['blockTypes'] ) || empty( $variation['styles'] ) ) { continue; } - /* - * Block style variations read in via standalone theme.json partials - * need to have their name set to the kebab case version of their title. - */ - $variation_name = $have_named_variations ? $key : ( $variation['slug'] ?? _wp_to_kebab_case( $variation['title'] ) ); + $variation_name = $variation['slug'] ?? _wp_to_kebab_case( $variation['title'] ); $variation_label = $variation['title'] ?? $variation_name; - foreach ( $supported_blocks as $block_type ) { + foreach ( $variation['blockTypes'] as $block_type ) { $registered_styles = $registry->get_registered_styles_for_block( $block_type ); // Register block style variation if it hasn't already been registered. @@ -475,3 +274,19 @@ function gutenberg_register_block_style_variations_from_theme_json_data( $variat } } } + +// DO NOT BACKPORT TO CORE. +// To be removed when core has backported this PR. +if ( function_exists( 'wp_resolve_block_style_variations_from_styles_registry' ) ) { + remove_filter( 'wp_theme_json_data_theme', 'wp_resolve_block_style_variations_from_styles_registry' ); +} +if ( function_exists( 'wp_resolve_block_style_variations_from_primary_theme_json' ) ) { + remove_filter( 'wp_theme_json_data_theme', 'wp_resolve_block_style_variations_from_primary_theme_json' ); +} +if ( function_exists( 'wp_resolve_block_style_variations_from_theme_json_partials' ) ) { + remove_filter( 'wp_theme_json_data_theme', 'wp_resolve_block_style_variations_from_theme_json_partials' ); +} +if ( function_exists( 'wp_resolve_block_style_variations_from_theme_style_variation' ) ) { + remove_filter( 'wp_theme_json_data_user', 'wp_resolve_block_style_variations_from_theme_style_variation' ); +} +// END OF DO NOT BACKPORT TO CORE. diff --git a/lib/class-wp-rest-global-styles-controller-gutenberg.php b/lib/class-wp-rest-global-styles-controller-gutenberg.php index c933a18a4fa59..421408e6f20b4 100644 --- a/lib/class-wp-rest-global-styles-controller-gutenberg.php +++ b/lib/class-wp-rest-global-styles-controller-gutenberg.php @@ -331,13 +331,9 @@ protected function prepare_item_for_database( $request ) { $config['styles'] = $existing_config['styles']; } - // Register theme-defined variations. - WP_Theme_JSON_Resolver_Gutenberg::get_theme_data(); - - // Register user-defined variations. - if ( isset( $request['styles']['blocks']['variations'] ) && ! empty( $config['styles']['blocks']['variations'] ) ) { - gutenberg_register_block_style_variations_from_theme_json_data( $config['styles']['blocks']['variations'] ); - } + // Register theme-defined variations e.g. from block style variation partials under `/styles`. + $variations = WP_Theme_JSON_Resolver_Gutenberg::get_style_variations( 'block' ); + gutenberg_register_block_style_variations_from_theme_json_partials( $variations ); if ( isset( $request['settings'] ) ) { $config['settings'] = $request['settings']; @@ -710,7 +706,12 @@ public function get_theme_items( $request ) { ); } - $response = array(); + $response = array(); + + // Register theme-defined variations e.g. from block style variation partials under `/styles`. + $partials = WP_Theme_JSON_Resolver_Gutenberg::get_style_variations( 'block' ); + gutenberg_register_block_style_variations_from_theme_json_partials( $partials ); + $variations = WP_Theme_JSON_Resolver_Gutenberg::get_style_variations(); // Add resolved theme asset links. diff --git a/lib/class-wp-theme-json-gutenberg.php b/lib/class-wp-theme-json-gutenberg.php index 6bcc1728e7f9e..9762bb610f77b 100644 --- a/lib/class-wp-theme-json-gutenberg.php +++ b/lib/class-wp-theme-json-gutenberg.php @@ -730,8 +730,8 @@ public static function get_element_class_name( $element ) { * Constructor. * * @since 5.8.0 - * @since 6.6.0 Key spacingScale by origin, and pre-generate the - * spacingSizes from spacingScale. + * @since 6.6.0 Key spacingScale by origin, and pre-generate the spacingSizes from spacingScale. + * Added unwrapping of shared block style variations into block type variations if registered. * * @param array $theme_json A structure that follows the theme.json schema. * @param string $origin Optional. What source of data this object represents. @@ -747,6 +747,7 @@ public function __construct( $theme_json = array( 'version' => WP_Theme_JSON_Gut $valid_block_names = array_keys( $registry->get_all_registered() ); $valid_element_names = array_keys( static::ELEMENTS ); $valid_variations = static::get_valid_block_style_variations(); + $this->theme_json = static::unwrap_shared_block_style_variations( $this->theme_json, $valid_variations ); $this->theme_json = static::sanitize( $this->theme_json, $valid_block_names, $valid_element_names, $valid_variations ); $this->theme_json = static::maybe_opt_in_into_settings( $this->theme_json ); @@ -790,6 +791,74 @@ public function __construct( $theme_json = array( 'version' => WP_Theme_JSON_Gut } } + /** + * Unwraps shared block style variations. + * + * It takes the shared variations (styles.variations.variationName) and + * applies them to all the blocks that have the given variation registered + * (styles.blocks.blockType.variations.variationName). + * + * For example, given the `core/paragraph` and `core/group` blocks have + * registered the `section-a` style variation, and given the following input: + * + * { + * "styles": { + * "variations": { + * "section-a": { "color": { "background": "backgroundColor" } } + * } + * } + * } + * + * It returns the following output: + * + * { + * "styles": { + * "blocks": { + * "core/paragraph": { + * "variations": { + * "section-a": { "color": { "background": "backgroundColor" } } + * }, + * }, + * "core/group": { + * "variations": { + * "section-a": { "color": { "background": "backgroundColor" } } + * } + * } + * } + * } + * } + * + * @since 6.6.0 + * + * @param array $theme_json A structure that follows the theme.json schema. + * @param array $valid_variations Valid block style variations. + * + * @return array Theme json data with shared variation definitions unwrapped under appropriate block types. + */ + private static function unwrap_shared_block_style_variations( $theme_json, $valid_variations ) { + if ( empty( $theme_json['styles']['variations'] ) || empty( $valid_variations ) ) { + return $theme_json; + } + + $new_theme_json = $theme_json; + $variations = $new_theme_json['styles']['variations']; + + foreach ( $valid_variations as $block_type => $registered_variations ) { + foreach ( $registered_variations as $variation_name ) { + $block_level_data = $new_theme_json['styles']['blocks'][ $block_type ]['variations'][ $variation_name ] ?? array(); + $top_level_data = $variations[ $variation_name ] ?? array(); + $merged_data = array_replace_recursive( $top_level_data, $block_level_data ); + if ( ! empty( $merged_data ) ) { + _wp_array_set( $new_theme_json, array( 'styles', 'blocks', $block_type, 'variations', $variation_name ), $merged_data ); + } + } + } + + unset( $new_theme_json['styles']['variations'] ); + + return $new_theme_json; + } + /** * Enables some opt-in settings if theme declared support. * @@ -952,12 +1021,6 @@ protected static function sanitize( $input, $valid_block_names, $valid_element_n $schema['settings']['blocks'] = $schema_settings_blocks; $schema['settings']['typography']['fontFamilies'] = static::schema_in_root_and_per_origin( static::FONT_FAMILY_SCHEMA ); - /* - * Shared block style variations can be registered from the theme.json data so we can't - * validate them against pre-registered block style variations. - */ - $schema['styles']['blocks']['variations'] = null; - // Remove anything that's not present in the schema. foreach ( array( 'styles', 'settings' ) as $subtree ) { if ( ! isset( $input[ $subtree ] ) ) { @@ -3310,7 +3373,7 @@ protected static function filter_slugs( $node, $slugs ) { * @since 5.9.0 * @since 6.6.0 Added support for block style variation element styles and $origin parameter. * - * @param array $theme_json Structure to sanitize. + * @param array $theme_json Structure to sanitize. * @param string $origin Optional. What source of data this object represents. * One of 'blocks', 'default', 'theme', or 'custom'. Default 'theme'. * @return array Sanitized structure. diff --git a/lib/class-wp-theme-json-resolver-gutenberg.php b/lib/class-wp-theme-json-resolver-gutenberg.php index 6a8d2558d1c85..fba107b981402 100644 --- a/lib/class-wp-theme-json-resolver-gutenberg.php +++ b/lib/class-wp-theme-json-resolver-gutenberg.php @@ -220,7 +220,8 @@ protected static function has_same_registered_blocks( $origin ) { * @since 5.8.0 * @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. + * @since 6.6.0 Added support for 'default-font-sizes' and 'default-spacing-sizes' theme supports. + * Added registration and merging of block style variations from partial theme.json files and the block styles registry. * * @param array $deprecated Deprecated. Not used. * @param array $options { @@ -247,13 +248,28 @@ public static function get_theme_data( $deprecated = array(), $options = array() $theme_json_data = array( 'version' => WP_Theme_JSON_Gutenberg::LATEST_SCHEMA ); } - // Register variations defined by the theme. - $variations = $theme_json_data['styles']['blocks']['variations'] ?? array(); - gutenberg_register_block_style_variations_from_theme_json_data( $variations ); - - // Register variations defined by theme partials (theme.json files in the styles directory). + /* + * Register variations defined by theme partials (theme.json files in the styles directory). + * This is required so the variations pass sanitization of theme.json data. + */ $variations = static::get_style_variations( 'block' ); - gutenberg_register_block_style_variations_from_theme_json_data( $variations ); + gutenberg_register_block_style_variations_from_theme_json_partials( $variations ); + + /* + * Source variations from the block styles registry and block style variation files. Then, merge them into the existing theme.json data. + * + * In case the same style properties are defined in several sources, this is how we should resolve the values, + * from higher to lower priority: + * + * - styles.blocks.blockType.variations from theme.json + * - styles.variations from theme.json + * - variations from block style variation files + * - variations from block styles registry + * + * See test_add_registered_block_styles_to_theme_data and test_unwraps_block_style_variations. + */ + $theme_json_data = static::inject_variations_from_block_style_variation_files( $theme_json_data, $variations ); + $theme_json_data = static::inject_variations_from_block_styles_registry( $theme_json_data ); /** * Filters the data provided by the theme for global styles and settings. @@ -547,10 +563,6 @@ public static function get_user_data() { } } - // Register variations defined by the user. - $variations = $config['styles']['blocks']['variations'] ?? array(); - gutenberg_register_block_style_variations_from_theme_json_data( $variations ); - /** This filter is documented in wp-includes/class-wp-theme-json-resolver.php */ $theme_json = apply_filters( 'wp_theme_json_data_user', new WP_Theme_JSON_Data_Gutenberg( $config, 'custom' ) ); static::$user = $theme_json->get_theme_json(); @@ -859,4 +871,84 @@ public static function resolve_theme_file_uris( $theme_json ) { return $theme_json; } + + /** + * Adds variations sourced from block style variations files to the supplied theme.json data. + * + * @since 6.6.0 + * + * @param array $data Array following the theme.json specification. + * @param array $variations Shared block style variations. + * @return array Theme json data including shared block style variation definitions. + */ + private static function inject_variations_from_block_style_variation_files( $data, $variations ) { + if ( empty( $variations ) ) { + return $data; + } + + foreach ( $variations as $variation ) { + if ( empty( $variation['styles'] ) || empty( $variation['blockTypes'] ) ) { + continue; + } + + $variation_name = $variation['slug'] ?? _wp_to_kebab_case( $variation['title'] ); + + foreach ( $variation['blockTypes'] as $block_type ) { + // First, override partial styles with any top-level styles. + $top_level_data = $data['styles']['variations'][ $variation_name ] ?? array(); + if ( ! empty( $top_level_data ) ) { + $variation['styles'] = array_replace_recursive( $variation['styles'], $top_level_data ); + } + + // Then, override styles so far with any block-level styles. + $block_level_data = $data['styles']['blocks'][ $block_type ]['variations'][ $variation_name ] ?? array(); + if ( ! empty( $block_level_data ) ) { + $variation['styles'] = array_replace_recursive( $variation['styles'], $block_level_data ); + } + + $path = array( 'styles', 'blocks', $block_type, 'variations', $variation_name ); + _wp_array_set( $data, $path, $variation['styles'] ); + } + } + + return $data; + } + + /** + * Adds variations sourced from the block styles registry to the supplied theme.json data. + * + * @since 6.6.0 + * + * @param array $data Array following the theme.json specification. + * @return array Theme json data including variations from the block styles registry. + */ + private static function inject_variations_from_block_styles_registry( $data ) { + $registry = WP_Block_Styles_Registry::get_instance(); + $styles = $registry->get_all_registered(); + + foreach ( $styles as $block_type => $variations ) { + foreach ( $variations as $variation_name => $variation ) { + if ( empty( $variation['style_data'] ) ) { + continue; + } + + // First, override registry styles with any top-level styles. + $top_level_data = $data['styles']['variations'][ $variation_name ] ?? array(); + if ( ! empty( $top_level_data ) ) { + $variation['style_data'] = array_replace_recursive( $variation['style_data'], $top_level_data ); + } + + // Then, override styles so far with any block-level styles. + $block_level_data = $data['styles']['blocks'][ $block_type ]['variations'][ $variation_name ] ?? array(); + if ( ! empty( $block_level_data ) ) { + $variation['style_data'] = array_replace_recursive( $variation['style_data'], $block_level_data ); + } + + $path = array( 'styles', 'blocks', $block_type, 'variations', $variation_name ); + _wp_array_set( $data, $path, $variation['style_data'] ); + } + } + + return $data; + } } diff --git a/lib/theme-i18n.json b/lib/theme-i18n.json index fe541e65c676b..e4d14502132cb 100644 --- a/lib/theme-i18n.json +++ b/lib/theme-i18n.json @@ -81,15 +81,6 @@ } } }, - "styles": { - "blocks": { - "variations": { - "*": { - "title": "Style variation name" - } - } - } - }, "customTemplates": [ { "title": "Custom template name" diff --git a/phpunit/block-supports/block-style-variations-test.php b/phpunit/block-supports/block-style-variations-test.php index ca84d3a93ec4e..0236beff468ee 100644 --- a/phpunit/block-supports/block-style-variations-test.php +++ b/phpunit/block-supports/block-style-variations-test.php @@ -98,12 +98,6 @@ public function filter_set_theme_root() { public function test_add_registered_block_styles_to_theme_data() { switch_theme( 'block-theme' ); - // Register theme-defined variations. - WP_Theme_JSON_Resolver_Gutenberg::get_theme_data(); - - // Register user-defined variations. - WP_Theme_JSON_Resolver_Gutenberg::get_user_data(); - $variation_styles_data = array( 'color' => array( 'background' => 'darkslateblue', @@ -158,13 +152,6 @@ public function test_add_registered_block_styles_to_theme_data() { $group_styles = $theme_json['styles']['blocks']['core/group'] ?? array(); $expected = array( 'variations' => array( - 'WithSlug' => array( - 'color' => array( - 'background' => 'aliceblue', - 'text' => 'midnightblue', - ), - ), - 'my-variation' => $variation_styles_data, /* * The following block style variations are registered @@ -183,12 +170,23 @@ public function test_add_registered_block_styles_to_theme_data() { 'text' => 'lightblue', ), ), + + /* + * Manually registered variations. + */ + 'WithSlug' => array( + 'color' => array( + 'background' => 'aliceblue', + 'text' => 'midnightblue', + ), + ), + 'my-variation' => $variation_styles_data, ), ); unregister_block_style( 'core/group', 'my-variation' ); unregister_block_style( 'core/group', 'WithSlug' ); - $this->assertSameSetsWithIndex( $group_styles, $expected ); + $this->assertSameSetsWithIndex( $expected, $group_styles, 'Variation data does not match' ); } } diff --git a/phpunit/class-wp-rest-global-styles-controller-gutenberg-test.php b/phpunit/class-wp-rest-global-styles-controller-gutenberg-test.php index 66a236c1c40a9..3fd0bb21ce562 100644 --- a/phpunit/class-wp-rest-global-styles-controller-gutenberg-test.php +++ b/phpunit/class-wp-rest-global-styles-controller-gutenberg-test.php @@ -528,6 +528,18 @@ public function test_update_item_with_custom_block_style_variations() { grant_super_admin( self::$admin_id ); } + /* + * For variations to be resolved they have to have been registered + * via either a theme.json partial or through the WP_Block_Styles_Registry. + */ + register_block_style( + 'core/group', + array( + 'name' => 'fromThemeStyleVariation', + 'label' => 'From Theme Style Variation', + ) + ); + $group_variations = array( 'fromThemeStyleVariation' => array( 'color' => array( @@ -541,16 +553,16 @@ public function test_update_item_with_custom_block_style_variations() { $request->set_body_params( array( 'styles' => array( - 'blocks' => array( - 'variations' => array( - 'fromThemeStyleVariation' => array( - 'blockTypes' => array( 'core/group', 'core/columns' ), - 'color' => array( - 'background' => '#000000', - 'text' => '#ffffff', - ), + 'variations' => array( + 'fromThemeStyleVariation' => array( + 'blockTypes' => array( 'core/group', 'core/columns' ), + 'color' => array( + 'background' => '#000000', + 'text' => '#ffffff', ), ), + ), + 'blocks' => array( 'core/group' => array( 'variations' => $group_variations, ), diff --git a/phpunit/class-wp-theme-json-resolver-test.php b/phpunit/class-wp-theme-json-resolver-test.php index 6333c3d1dd776..e7a1b44049393 100644 --- a/phpunit/class-wp-theme-json-resolver-test.php +++ b/phpunit/class-wp-theme-json-resolver-test.php @@ -1289,4 +1289,93 @@ public function test_get_resolved_theme_uris() { $this->assertSame( $expected_data, $actual ); } + + /** + * Tests that block style variations data gets merged in the following + * priority order, from highest priority to lowest. + * + * - `styles.blocks.blockType.variations` from theme.json + * - `styles.variations` from theme.json + * - variations from block style variation files under `/styles` + * - variations from `WP_Block_Styles_Registry` + */ + public function test_block_style_variation_merge_order() { + switch_theme( 'block-theme-child-with-block-style-variations' ); + + /* + * Register style for a block that isn't included in the block style variation's partial + * theme.json's blockTypes. The name must match though so we can ensure the partial's + * styles do not get applied to this block. + */ + register_block_style( + 'core/heading', + array( + 'name' => 'block-style-variation-b', + 'label' => 'Heading only variation', + ) + ); + + // Register variation for a block that will be partially overridden at all levels. + register_block_style( + 'core/media-text', + array( + 'name' => 'block-style-variation-a', + 'label' => 'Block Style Variation A', + 'style_data' => array( + 'color' => array( + 'background' => 'pink', + 'gradient' => 'var(--custom)', + ), + ), + ) + ); + + $data = WP_Theme_JSON_Resolver_Gutenberg::get_theme_data()->get_raw_data(); + $block_styles = $data['styles']['blocks'] ?? array(); + $actual = array_intersect_key( + $block_styles, + array_flip( array( 'core/button', 'core/media-text', 'core/heading' ) ) + ); + $expected = array( + 'core/button' => array( + 'variations' => array( + 'outline' => array( + 'color' => array( + 'background' => 'red', + 'text' => 'white', + ), + ), + ), + ), + 'core/media-text' => array( + 'variations' => array( + 'block-style-variation-a' => array( + 'color' => array( + 'background' => 'blue', + 'gradient' => 'var(--custom)', + 'text' => 'aliceblue', + ), + 'typography' => array( + 'fontSize' => '1.5em', + 'lineHeight' => '1.4em', + ), + ), + ), + ), + 'core/heading' => array( + 'variations' => array( + 'block-style-variation-b' => array( + 'typography' => array( + 'fontSize' => '3em', + ), + ), + ), + ), + ); + + unregister_block_style( 'core/heading', 'block-style-variation-b' ); + unregister_block_style( 'core/media-text', 'block-style-variation-a' ); + + $this->assertSameSetsWithIndex( $expected, $actual, 'Merged variation styles do not match.' ); + } } diff --git a/phpunit/class-wp-theme-json-test.php b/phpunit/class-wp-theme-json-test.php index 30653c8f1c627..b188ce4fce6ab 100644 --- a/phpunit/class-wp-theme-json-test.php +++ b/phpunit/class-wp-theme-json-test.php @@ -3739,6 +3739,89 @@ public function test_sanitize_for_unregistered_style_variations() { $this->assertSameSetsWithIndex( $expected, $sanitized_theme_json, 'Sanitized theme.json styles does not match' ); } + public function test_unwraps_block_style_variations() { + gutenberg_register_block_style( + array( 'core/paragraph', 'core/group' ), + array( + 'name' => 'myVariation', + 'label' => 'My variation', + ) + ); + + $input = new WP_Theme_JSON_Gutenberg( + array( + 'version' => WP_Theme_JSON_Gutenberg::LATEST_SCHEMA, + 'styles' => array( + 'variations' => array( + 'myVariation' => array( + 'color' => array( + 'background' => 'topLevel', + 'gradient' => 'topLevel', + ), + 'typography' => array( + 'fontFamily' => 'topLevel', + ), + ), + ), + 'blocks' => array( + 'core/paragraph' => array( + 'variations' => array( + 'myVariation' => array( + 'color' => array( + 'background' => 'blockLevel', + 'text' => 'blockLevel', + ), + 'outline' => array( + 'offset' => 'blockLevel', + ), + ), + ), + ), + ), + ), + ) + ); + + $expected = array( + 'version' => WP_Theme_JSON_Gutenberg::LATEST_SCHEMA, + 'styles' => array( + 'blocks' => array( + 'core/paragraph' => array( + 'variations' => array( + 'myVariation' => array( + 'color' => array( + 'background' => 'blockLevel', + 'gradient' => 'topLevel', + 'text' => 'blockLevel', + ), + 'typography' => array( + 'fontFamily' => 'topLevel', + ), + 'outline' => array( + 'offset' => 'blockLevel', + ), + ), + ), + ), + 'core/group' => array( + 'variations' => array( + 'myVariation' => array( + 'color' => array( + 'background' => 'topLevel', + 'gradient' => 'topLevel', + ), + 'typography' => array( + 'fontFamily' => 'topLevel', + ), + ), + ), + ), + ), + ), + ); + $this->assertSameSetsWithIndex( $expected, $input->get_raw_data(), 'Unwrapped block style variations do not match' ); + } + /** * @dataProvider data_sanitize_for_block_with_style_variations * diff --git a/phpunit/data/themedir1/block-theme-child-with-block-style-variations/theme.json b/phpunit/data/themedir1/block-theme-child-with-block-style-variations/theme.json index a471d8f326a4a..fee9382dce147 100644 --- a/phpunit/data/themedir1/block-theme-child-with-block-style-variations/theme.json +++ b/phpunit/data/themedir1/block-theme-child-with-block-style-variations/theme.json @@ -1,4 +1,55 @@ { "$schema": "https://schemas.wp.org/trunk/theme.json", - "version": 3 + "version": 3, + "styles": { + "variations": { + "outline": { + "color": { + "background": "green", + "text": "white" + } + }, + "block-style-variation-a": { + "color": { + "background": "darkseagreen" + }, + "typography": { + "fontSize": "2em", + "lineHeight": "1.4em" + } + } + }, + "blocks": { + "core/button": { + "variations": { + "outline": { + "color": { + "background": "red" + } + } + } + }, + "core/media-text": { + "variations": { + "block-style-variation-a": { + "color": { + "background": "blue" + }, + "typography": { + "fontSize": "1.5em" + } + } + } + }, + "core/heading": { + "variations": { + "block-style-variation-b": { + "typography": { + "fontSize": "3em" + } + } + } + } + } + } } diff --git a/schemas/json/theme.json b/schemas/json/theme.json index 5b9e37ef7378a..ec42bd1d54f0b 100644 --- a/schemas/json/theme.json +++ b/schemas/json/theme.json @@ -1938,9 +1938,6 @@ "stylesBlocksPropertiesComplete": { "type": "object", "properties": { - "variations": { - "$ref": "#/definitions/stylesBlocksSharedVariationProperties" - }, "core/archives": { "$ref": "#/definitions/stylesPropertiesAndElementsComplete" }, @@ -2271,15 +2268,15 @@ } ] }, - "stylesBlocksSharedVariationProperties": { + "stylesVariationsProperties": { "type": "object", "patternProperties": { "^[a-z][a-z0-9-]*$": { - "$ref": "#/definitions/stylesSharedVariationProperties" + "$ref": "#/definitions/stylesVariationProperties" } } }, - "stylesSharedVariationProperties": { + "stylesVariationProperties": { "type": "object", "allOf": [ { @@ -2287,16 +2284,6 @@ }, { "properties": { - "title": { - "type": "string", - "description": "Style variation name." - }, - "blockTypes": { - "type": "array", - "items": { - "type": "string" - } - }, "border": {}, "color": {}, "dimensions": {}, @@ -2833,6 +2820,9 @@ } }, "additionalProperties": false + }, + "variations": { + "$ref": "#/definitions/stylesVariationsProperties" } }, "additionalProperties": false