From de096449303fa2cf1b349c1f94e99a0e17f2f520 Mon Sep 17 00:00:00 2001 From: Aaron Robertshaw <60436221+aaronrobertshaw@users.noreply.github.com> Date: Mon, 6 Jan 2025 18:58:17 +1000 Subject: [PATCH] Try merging separator variation colors --- ...class-wp-theme-json-resolver-gutenberg.php | 22 +++++ phpunit/class-wp-theme-json-resolver-test.php | 88 ++++++++++++++++++- 2 files changed, 109 insertions(+), 1 deletion(-) diff --git a/lib/class-wp-theme-json-resolver-gutenberg.php b/lib/class-wp-theme-json-resolver-gutenberg.php index 94d7ec8a532e04..36b6376a533898 100644 --- a/lib/class-wp-theme-json-resolver-gutenberg.php +++ b/lib/class-wp-theme-json-resolver-gutenberg.php @@ -292,6 +292,19 @@ public static function get_theme_data( $deprecated = array(), $options = array() } } + $separator_variations = $theme_json_data['styles']['blocks']['core/separator']['variations'] ?? array(); + foreach ( $separator_variations as $variation => $variation_data ) { + $variation_separator_color = $variation_data['color']['background'] ?? null; + if ( $variation_separator_color ) { + if ( ! isset( $variation_data['color']['text'] ) ) { + _wp_array_set( $theme_json_data, array( 'styles', 'blocks', 'core/separator', 'variations', $variation, 'color', 'text' ), $variation_separator_color ); + } + if ( ! isset( $variation_data['border']['color'] ) ) { + _wp_array_set( $theme_json_data, array( 'styles', 'blocks', 'core/separator', 'variations', $variation, 'border', 'color' ), $variation_separator_color ); + } + } + } + /** * Filters the data provided by the theme for global styles and settings. * @@ -599,6 +612,15 @@ public static function get_user_data() { _wp_array_set( $config, array( 'styles', 'blocks', 'core/separator', 'border', 'color' ), $separator_color ); } + $separator_variations = $config['styles']['blocks']['core/separator']['variations'] ?? array(); + foreach ( $separator_variations as $variation => $variation_data ) { + $variation_separator_color = $variation_data['color']['background'] ?? null; + if ( $variation_separator_color ) { + _wp_array_set( $config, array( 'styles', 'blocks', 'core/separator', 'variations', $variation, 'color', 'text' ), $variation_separator_color ); + _wp_array_set( $config, array( 'styles', 'blocks', 'core/separator', 'variations', $variation, 'border', 'color' ), $variation_separator_color ); + } + } + /** 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(); diff --git a/phpunit/class-wp-theme-json-resolver-test.php b/phpunit/class-wp-theme-json-resolver-test.php index a8f1522c3af210..6761b412437739 100644 --- a/phpunit/class-wp-theme-json-resolver-test.php +++ b/phpunit/class-wp-theme-json-resolver-test.php @@ -103,6 +103,7 @@ public function set_up() { add_filter( 'template_root', array( $this, 'filter_set_theme_root' ) ); $this->queries = array(); // Clear caches. + WP_Theme_JSON_Resolver_Gutenberg::clean_cached_data(); wp_clean_themes_cache(); unset( $GLOBALS['wp_themes'] ); } @@ -1508,7 +1509,8 @@ function ( $path ) use ( $temp_theme_json ) { return $temp_theme_json; } return $path; - } + }, + PHP_INT_MAX ); // Get the merged data. @@ -1611,6 +1613,48 @@ public function data_separator_color_merge_user_data() { ), ), ), + 'block style variations' => array( + 'input_styles' => array( + 'variations' => array( + 'wide' => array( + 'color' => array( + 'background' => 'green', + 'text' => 'red', + ), + ), + 'dots' => array( + 'color' => array( + 'background' => 'blue', + ), + 'border' => array( + 'color' => 'pink', + ), + ), + ), + ), + 'expected_styles' => array( + 'variations' => array( + 'wide' => array( + 'color' => array( + 'background' => 'green', + 'text' => 'green', // Always set to match background. + ), + 'border' => array( + 'color' => 'green', // Always overridden by background. + ), + ), + 'dots' => array( + 'color' => array( + 'background' => 'blue', + 'text' => 'blue', // Always set to match background. + ), + 'border' => array( + 'color' => 'blue', // Always overridden by background. + ), + ), + ), + ), + ), ); } @@ -1705,6 +1749,48 @@ public function data_separator_color_merge_theme_data() { ), ), ), + 'block style variations' => array( + 'input_styles' => array( + 'variations' => array( + 'wide' => array( + 'color' => array( + 'background' => 'green', + 'text' => 'red', + ), + ), + 'dots' => array( + 'color' => array( + 'background' => 'blue', + ), + 'border' => array( + 'color' => 'pink', + ), + ), + ), + ), + 'expected_styles' => array( + 'variations' => array( + 'wide' => array( + 'color' => array( + 'background' => 'green', + 'text' => 'red', // Kept because already defined. + ), + 'border' => array( + 'color' => 'green', // Set because not defined. + ), + ), + 'dots' => array( + 'color' => array( + 'background' => 'blue', + 'text' => 'blue', // Always set to match background. + ), + 'border' => array( + 'color' => 'pink', // Always overridden by background. + ), + ), + ), + ), + ), ); } }