From ffd77feae4d460f7e18dcf67eab84cb3a533fc25 Mon Sep 17 00:00:00 2001 From: tellthemachines Date: Fri, 7 Jun 2024 11:54:15 +1000 Subject: [PATCH 1/6] Update custom CSS handling to be consistent with block global styles. --- src/wp-includes/block-editor.php | 2 +- src/wp-includes/class-wp-theme-json.php | 12 ++++++++++++ src/wp-includes/default-filters.php | 3 --- src/wp-includes/script-loader.php | 12 ++++++++++++ 4 files changed, 25 insertions(+), 4 deletions(-) diff --git a/src/wp-includes/block-editor.php b/src/wp-includes/block-editor.php index fdc4846f69f80..00633d4d7edd9 100644 --- a/src/wp-includes/block-editor.php +++ b/src/wp-includes/block-editor.php @@ -532,7 +532,7 @@ function get_block_editor_settings( array $custom_settings, $block_editor_contex * entered by users does not break other global styles. */ $global_styles[] = array( - 'css' => wp_get_global_styles_custom_css(), + 'css' => wp_get_global_stylesheet( array( 'custom-css' ) ), '__unstableType' => 'user', 'isGlobalStyles' => true, ); diff --git a/src/wp-includes/class-wp-theme-json.php b/src/wp-includes/class-wp-theme-json.php index 0cd90575a5e20..6f068fa05524b 100644 --- a/src/wp-includes/class-wp-theme-json.php +++ b/src/wp-includes/class-wp-theme-json.php @@ -1423,6 +1423,12 @@ public function get_stylesheet( $types = array( 'variables', 'styles', 'presets' $stylesheet .= $this->get_preset_classes( $setting_nodes, $origins ); } + // Load the custom CSS last so it has the highest specificity. + if ( in_array( 'custom-css', $types, true ) ) { + // Add the global styles root CSS. + $stylesheet .= _wp_array_get( $this->theme_json, array( 'styles', 'css' ) ); + } + return $stylesheet; } @@ -2692,6 +2698,7 @@ private static function get_block_nodes( $theme_json, $selectors = array(), $opt 'duotone' => $duotone_selector, 'features' => $feature_selectors, 'variations' => $variation_selectors, + 'css' => $selector, ); if ( isset( $theme_json['styles']['blocks'][ $name ]['elements'] ) ) { @@ -2913,6 +2920,11 @@ static function ( $pseudo_selector ) use ( $selector ) { $block_rules .= $this->process_blocks_custom_css( $node['css'], $selector ); } + // 7. Generate and append any custom CSS rules. + if ( isset( $node['css'] ) ) { + $block_rules .= $this->process_blocks_custom_css( $node['css'], $selector ); + } + return $block_rules; } diff --git a/src/wp-includes/default-filters.php b/src/wp-includes/default-filters.php index 2c182747a5bc0..6fba4441d50bd 100644 --- a/src/wp-includes/default-filters.php +++ b/src/wp-includes/default-filters.php @@ -600,9 +600,6 @@ add_action( 'wp_enqueue_scripts', 'wp_enqueue_global_styles' ); add_action( 'wp_footer', 'wp_enqueue_global_styles', 1 ); -// Global styles custom CSS. -add_action( 'wp_enqueue_scripts', 'wp_enqueue_global_styles_custom_css' ); - // Block supports, and other styles parsed and stored in the Style Engine. add_action( 'wp_enqueue_scripts', 'wp_enqueue_stored_styles' ); add_action( 'wp_footer', 'wp_enqueue_stored_styles', 1 ); diff --git a/src/wp-includes/script-loader.php b/src/wp-includes/script-loader.php index 715477ad8ecdf..5906e0d35d851 100644 --- a/src/wp-includes/script-loader.php +++ b/src/wp-includes/script-loader.php @@ -2504,6 +2504,18 @@ function wp_enqueue_global_styles() { $stylesheet = wp_get_global_stylesheet(); + /* + * Dequeue the Customizer's custom CSS + * and add it before the global styles custom CSS. + */ + remove_action( 'wp_head', 'wp_custom_css_cb', 101 ); + // Get the custom CSS from the Customizer and add it to the global stylesheet. + $custom_css = wp_get_custom_css(); + $stylesheet .= $custom_css; + + // Add the global styles custom CSS at the end. + $stylesheet .= wp_get_global_stylesheet( array( 'custom-css' ) ); + if ( empty( $stylesheet ) ) { return; } From a38ca8b3cfcb20c63ab7acaf1e279ba201125aa2 Mon Sep 17 00:00:00 2001 From: tellthemachines Date: Mon, 17 Jun 2024 14:00:15 +1000 Subject: [PATCH 2/6] Update comment. --- src/wp-includes/class-wp-theme-json.php | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/src/wp-includes/class-wp-theme-json.php b/src/wp-includes/class-wp-theme-json.php index 6f068fa05524b..180cb43e8fc31 100644 --- a/src/wp-includes/class-wp-theme-json.php +++ b/src/wp-includes/class-wp-theme-json.php @@ -2915,13 +2915,8 @@ static function ( $pseudo_selector ) use ( $selector ) { } } - // 7. Generate and append any custom CSS rules pertaining to nested block style variations. - if ( isset( $node['css'] ) && ! $is_root_selector ) { - $block_rules .= $this->process_blocks_custom_css( $node['css'], $selector ); - } - // 7. Generate and append any custom CSS rules. - if ( isset( $node['css'] ) ) { + if ( isset( $node['css'] ) && ! $is_root_selector ) { $block_rules .= $this->process_blocks_custom_css( $node['css'], $selector ); } From 0da6bec64a73c36115decf9eb3508e56d94385d7 Mon Sep 17 00:00:00 2001 From: tellthemachines Date: Tue, 9 Jul 2024 15:43:48 +1000 Subject: [PATCH 3/6] Deprecate redundant functions. --- src/wp-includes/class-wp-theme-json.php | 2 ++ src/wp-includes/global-styles-and-settings.php | 2 ++ src/wp-includes/script-loader.php | 2 ++ 3 files changed, 6 insertions(+) diff --git a/src/wp-includes/class-wp-theme-json.php b/src/wp-includes/class-wp-theme-json.php index 180cb43e8fc31..90de255c82971 100644 --- a/src/wp-includes/class-wp-theme-json.php +++ b/src/wp-includes/class-wp-theme-json.php @@ -1473,10 +1473,12 @@ protected function process_blocks_custom_css( $css, $selector ) { * Returns the global styles custom CSS. * * @since 6.2.0 + * @deprecated 6.7.0 Use {@see 'get_stylesheet'} instead. * * @return string The global styles custom CSS. */ public function get_custom_css() { + _deprecated_function( __METHOD__, '6.7.0', 'get_stylesheet' ); // Add the global styles root CSS. $stylesheet = isset( $this->theme_json['styles']['css'] ) ? $this->theme_json['styles']['css'] : ''; diff --git a/src/wp-includes/global-styles-and-settings.php b/src/wp-includes/global-styles-and-settings.php index b413273a64974..b2c41879f5195 100644 --- a/src/wp-includes/global-styles-and-settings.php +++ b/src/wp-includes/global-styles-and-settings.php @@ -247,10 +247,12 @@ function wp_get_global_stylesheet( $types = array() ) { * Gets the global styles custom CSS from theme.json. * * @since 6.2.0 + * @deprecated 6.7.0 Use {@see 'wp_get_global_stylesheet'} instead. * * @return string The global styles custom CSS. */ function wp_get_global_styles_custom_css() { + _deprecated_function( __FUNCTION__, '6.7.0', 'wp_get_global_stylesheet' ); if ( ! wp_theme_has_theme_json() ) { return ''; } diff --git a/src/wp-includes/script-loader.php b/src/wp-includes/script-loader.php index 5906e0d35d851..a050141f4c6f2 100644 --- a/src/wp-includes/script-loader.php +++ b/src/wp-includes/script-loader.php @@ -2532,8 +2532,10 @@ function wp_enqueue_global_styles() { * Enqueues the global styles custom css defined via theme.json. * * @since 6.2.0 + * @deprecated 6.7.0 Use {@see 'wp_enqueue_global_styles'} instead. */ function wp_enqueue_global_styles_custom_css() { + _deprecated_function( __FUNCTION__, '6.7.0', 'wp_enqueue_global_styles' ); if ( ! wp_is_block_theme() ) { return; } From 93fa3b2a491d64b66c1436e650011576baefe42a Mon Sep 17 00:00:00 2001 From: tellthemachines Date: Tue, 9 Jul 2024 15:47:57 +1000 Subject: [PATCH 4/6] Move to deprecated file --- src/wp-includes/deprecated.php | 77 +++++++++++++++++++ .../global-styles-and-settings.php | 54 ------------- src/wp-includes/script-loader.php | 23 ------ 3 files changed, 77 insertions(+), 77 deletions(-) diff --git a/src/wp-includes/deprecated.php b/src/wp-includes/deprecated.php index 8084cdd96cfac..457b7fc136718 100644 --- a/src/wp-includes/deprecated.php +++ b/src/wp-includes/deprecated.php @@ -6310,3 +6310,80 @@ function wp_interactivity_process_directives_of_interactive_blocks( array $parse _deprecated_function( __FUNCTION__, '6.6.0' ); return $parsed_block; } + +/** + * Gets the global styles custom CSS from theme.json. + * + * @since 6.2.0 + * @deprecated 6.7.0 Use {@see 'wp_get_global_stylesheet'} instead. + * + * @return string The global styles custom CSS. + */ +function wp_get_global_styles_custom_css() { + _deprecated_function( __FUNCTION__, '6.7.0', 'wp_get_global_stylesheet' ); + if ( ! wp_theme_has_theme_json() ) { + return ''; + } + /* + * Ignore cache when the development mode is set to 'theme', so it doesn't interfere with the theme + * developer's workflow. + */ + $can_use_cached = ! wp_is_development_mode( 'theme' ); + + /* + * By using the 'theme_json' group, this data is marked to be non-persistent across requests. + * @see `wp_cache_add_non_persistent_groups()`. + * + * The rationale for this is to make sure derived data from theme.json + * is always fresh from the potential modifications done via hooks + * that can use dynamic data (modify the stylesheet depending on some option, + * settings depending on user permissions, etc.). + * See some of the existing hooks to modify theme.json behavior: + * @see https://make.wordpress.org/core/2022/10/10/filters-for-theme-json-data/ + * + * A different alternative considered was to invalidate the cache upon certain + * events such as options add/update/delete, user meta, etc. + * It was judged not enough, hence this approach. + * @see https://github.com/WordPress/gutenberg/pull/45372 + */ + $cache_key = 'wp_get_global_styles_custom_css'; + $cache_group = 'theme_json'; + if ( $can_use_cached ) { + $cached = wp_cache_get( $cache_key, $cache_group ); + if ( $cached ) { + return $cached; + } + } + + $tree = WP_Theme_JSON_Resolver::get_merged_data(); + $stylesheet = $tree->get_custom_css(); + + if ( $can_use_cached ) { + wp_cache_set( $cache_key, $stylesheet, $cache_group ); + } + + return $stylesheet; +} + +/** + * Enqueues the global styles custom css defined via theme.json. + * + * @since 6.2.0 + * @deprecated 6.7.0 Use {@see 'wp_enqueue_global_styles'} instead. + */ +function wp_enqueue_global_styles_custom_css() { + _deprecated_function( __FUNCTION__, '6.7.0', 'wp_enqueue_global_styles' ); + if ( ! wp_is_block_theme() ) { + return; + } + + // Don't enqueue Customizer's custom CSS separately. + remove_action( 'wp_head', 'wp_custom_css_cb', 101 ); + + $custom_css = wp_get_custom_css(); + $custom_css .= wp_get_global_styles_custom_css(); + + if ( ! empty( $custom_css ) ) { + wp_add_inline_style( 'global-styles', $custom_css ); + } +} diff --git a/src/wp-includes/global-styles-and-settings.php b/src/wp-includes/global-styles-and-settings.php index b2c41879f5195..efa68ce36b114 100644 --- a/src/wp-includes/global-styles-and-settings.php +++ b/src/wp-includes/global-styles-and-settings.php @@ -243,60 +243,6 @@ function wp_get_global_stylesheet( $types = array() ) { return $stylesheet; } -/** - * Gets the global styles custom CSS from theme.json. - * - * @since 6.2.0 - * @deprecated 6.7.0 Use {@see 'wp_get_global_stylesheet'} instead. - * - * @return string The global styles custom CSS. - */ -function wp_get_global_styles_custom_css() { - _deprecated_function( __FUNCTION__, '6.7.0', 'wp_get_global_stylesheet' ); - if ( ! wp_theme_has_theme_json() ) { - return ''; - } - /* - * Ignore cache when the development mode is set to 'theme', so it doesn't interfere with the theme - * developer's workflow. - */ - $can_use_cached = ! wp_is_development_mode( 'theme' ); - - /* - * By using the 'theme_json' group, this data is marked to be non-persistent across requests. - * @see `wp_cache_add_non_persistent_groups()`. - * - * The rationale for this is to make sure derived data from theme.json - * is always fresh from the potential modifications done via hooks - * that can use dynamic data (modify the stylesheet depending on some option, - * settings depending on user permissions, etc.). - * See some of the existing hooks to modify theme.json behavior: - * @see https://make.wordpress.org/core/2022/10/10/filters-for-theme-json-data/ - * - * A different alternative considered was to invalidate the cache upon certain - * events such as options add/update/delete, user meta, etc. - * It was judged not enough, hence this approach. - * @see https://github.com/WordPress/gutenberg/pull/45372 - */ - $cache_key = 'wp_get_global_styles_custom_css'; - $cache_group = 'theme_json'; - if ( $can_use_cached ) { - $cached = wp_cache_get( $cache_key, $cache_group ); - if ( $cached ) { - return $cached; - } - } - - $tree = WP_Theme_JSON_Resolver::get_merged_data(); - $stylesheet = $tree->get_custom_css(); - - if ( $can_use_cached ) { - wp_cache_set( $cache_key, $stylesheet, $cache_group ); - } - - return $stylesheet; -} - /** * Adds global style rules to the inline style for each block. * diff --git a/src/wp-includes/script-loader.php b/src/wp-includes/script-loader.php index a050141f4c6f2..9a79043bfc5c9 100644 --- a/src/wp-includes/script-loader.php +++ b/src/wp-includes/script-loader.php @@ -2528,29 +2528,6 @@ function wp_enqueue_global_styles() { wp_add_global_styles_for_blocks(); } -/** - * Enqueues the global styles custom css defined via theme.json. - * - * @since 6.2.0 - * @deprecated 6.7.0 Use {@see 'wp_enqueue_global_styles'} instead. - */ -function wp_enqueue_global_styles_custom_css() { - _deprecated_function( __FUNCTION__, '6.7.0', 'wp_enqueue_global_styles' ); - if ( ! wp_is_block_theme() ) { - return; - } - - // Don't enqueue Customizer's custom CSS separately. - remove_action( 'wp_head', 'wp_custom_css_cb', 101 ); - - $custom_css = wp_get_custom_css(); - $custom_css .= wp_get_global_styles_custom_css(); - - if ( ! empty( $custom_css ) ) { - wp_add_inline_style( 'global-styles', $custom_css ); - } -} - /** * Checks if the editor scripts and styles for all registered block types * should be enqueued on the current screen. From 3b4926d04d5bd25d8a2c0e30fa8d70dd9f4395b5 Mon Sep 17 00:00:00 2001 From: tellthemachines Date: Tue, 9 Jul 2024 16:01:14 +1000 Subject: [PATCH 5/6] Add new tests and delete deprecated test --- tests/phpunit/tests/theme/wpThemeJson.php | 40 +++++++++++++++++++---- 1 file changed, 34 insertions(+), 6 deletions(-) diff --git a/tests/phpunit/tests/theme/wpThemeJson.php b/tests/phpunit/tests/theme/wpThemeJson.php index fea2013e17e1e..7190399d8dc10 100644 --- a/tests/phpunit/tests/theme/wpThemeJson.php +++ b/tests/phpunit/tests/theme/wpThemeJson.php @@ -5035,15 +5035,34 @@ public function test_get_top_level_background_image_styles() { } /** - * @ticket 57536 - * @ticket 61165 + * Tests that base custom CSS is generated correctly. + * + * @ticket 61395 + */ + public function test_get_stylesheet_handles_base_custom_css() { + $theme_json = new WP_Theme_JSON( + array( + 'version' => WP_Theme_JSON::LATEST_SCHEMA, + 'styles' => array( + 'css' => 'body {color:purple;}', + ), + ) + ); + + $custom_css = 'body {color:purple;}'; + $this->assertSame( $custom_css, $theme_json->get_stylesheet( array( 'custom-css' ) ) ); + } + + /** + * Tests that block custom CSS is generated correctly. + * + * @ticket 61395 */ - public function test_get_custom_css_handles_global_custom_css() { + public function test_get_styles_for_block_handles_block_custom_css() { $theme_json = new WP_Theme_JSON( array( 'version' => WP_Theme_JSON::LATEST_SCHEMA, 'styles' => array( - 'css' => 'body {color:purple;}', 'blocks' => array( 'core/paragraph' => array( 'css' => 'color:red;', @@ -5053,8 +5072,17 @@ public function test_get_custom_css_handles_global_custom_css() { ) ); - $custom_css = 'body {color:purple;}:root :where(p){color:red;}'; - $this->assertSame( $custom_css, $theme_json->get_custom_css() ); + $paragraph_node = array( + 'name' => 'core/paragraph', + 'path' => array( 'styles', 'blocks', 'core/paragraph' ), + 'selector' => 'p', + 'selectors' => array( + 'root' => 'p', + ), + ); + + $custom_css = ':root :where(p){color:red;}'; + $this->assertSame( $custom_css, $theme_json->get_styles_for_block( $paragraph_node ) ); } /** From 56d544825335a7b4e24bbf8aff4279c6db8e15f7 Mon Sep 17 00:00:00 2001 From: tellthemachines Date: Wed, 10 Jul 2024 14:02:15 +1000 Subject: [PATCH 6/6] Check if it's a block theme before dequeuing customizer CSS --- src/wp-includes/script-loader.php | 22 ++++++++++++---------- 1 file changed, 12 insertions(+), 10 deletions(-) diff --git a/src/wp-includes/script-loader.php b/src/wp-includes/script-loader.php index 9a79043bfc5c9..b7da065ad1796 100644 --- a/src/wp-includes/script-loader.php +++ b/src/wp-includes/script-loader.php @@ -2504,17 +2504,19 @@ function wp_enqueue_global_styles() { $stylesheet = wp_get_global_stylesheet(); - /* - * Dequeue the Customizer's custom CSS - * and add it before the global styles custom CSS. - */ - remove_action( 'wp_head', 'wp_custom_css_cb', 101 ); - // Get the custom CSS from the Customizer and add it to the global stylesheet. - $custom_css = wp_get_custom_css(); - $stylesheet .= $custom_css; + if ( $is_block_theme ) { + /* + * Dequeue the Customizer's custom CSS + * and add it before the global styles custom CSS. + */ + remove_action( 'wp_head', 'wp_custom_css_cb', 101 ); + // Get the custom CSS from the Customizer and add it to the global stylesheet. + $custom_css = wp_get_custom_css(); + $stylesheet .= $custom_css; - // Add the global styles custom CSS at the end. - $stylesheet .= wp_get_global_stylesheet( array( 'custom-css' ) ); + // Add the global styles custom CSS at the end. + $stylesheet .= wp_get_global_stylesheet( array( 'custom-css' ) ); + } if ( empty( $stylesheet ) ) { return;