From fc036aae230958f777058d327016297e571ce630 Mon Sep 17 00:00:00 2001 From: Ben Dwyer Date: Fri, 23 Sep 2022 12:40:52 +0100 Subject: [PATCH 1/4] Backport: Gutenberg #42005 --- src/wp-includes/global-styles-and-settings.php | 5 +++++ src/wp-includes/script-loader.php | 13 ++++++------- 2 files changed, 11 insertions(+), 7 deletions(-) diff --git a/src/wp-includes/global-styles-and-settings.php b/src/wp-includes/global-styles-and-settings.php index 1187837ed651f..bad95ebd05e63 100644 --- a/src/wp-includes/global-styles-and-settings.php +++ b/src/wp-includes/global-styles-and-settings.php @@ -204,6 +204,11 @@ function wp_add_global_styles_for_blocks() { foreach ( $block_nodes as $metadata ) { $block_css = $tree->get_styles_for_block( $metadata ); + if ( ! wp_should_load_separate_core_block_assets() ) { + wp_add_inline_style( 'global-styles', $block_css ); + continue; + } + if ( isset( $metadata['name'] ) ) { $block_name = str_replace( 'core/', '', $metadata['name'] ); /* diff --git a/src/wp-includes/script-loader.php b/src/wp-includes/script-loader.php index 9a45d9a6f5e2e..8aad1fc0de2a8 100644 --- a/src/wp-includes/script-loader.php +++ b/src/wp-includes/script-loader.php @@ -2408,16 +2408,12 @@ function wp_enqueue_global_styles() { return; } - /* + /** * If we are loading CSS for each block separately, then we can load the theme.json CSS conditionally. * This removes the CSS from the global-styles stylesheet and adds it to the inline CSS for each block. + * This filter has to be registered before we call wp_get_global_stylesheet(); */ - if ( $separate_assets ) { - add_filter( 'theme_json_get_style_nodes', 'wp_filter_out_block_nodes' ); - // Add each block as an inline css. - wp_add_global_styles_for_blocks(); - } - + add_filter( 'gutenberg_get_style_nodes', 'filter_out_block_nodes' ); $stylesheet = wp_get_global_stylesheet(); if ( empty( $stylesheet ) ) { @@ -2427,6 +2423,9 @@ function wp_enqueue_global_styles() { wp_register_style( 'global-styles', false, array(), true, true ); wp_add_inline_style( 'global-styles', $stylesheet ); wp_enqueue_style( 'global-styles' ); + + // add each block as an inline css. + wp_add_global_styles_for_blocks(); } /** From 155d6bed3ce4f751fa558b3a861e2a3a6793814f Mon Sep 17 00:00:00 2001 From: Bernie Reiter Date: Mon, 26 Sep 2022 16:24:04 +0200 Subject: [PATCH 2/4] Fix filter name --- src/wp-includes/script-loader.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/wp-includes/script-loader.php b/src/wp-includes/script-loader.php index 8aad1fc0de2a8..d5ae209904982 100644 --- a/src/wp-includes/script-loader.php +++ b/src/wp-includes/script-loader.php @@ -2413,7 +2413,7 @@ function wp_enqueue_global_styles() { * This removes the CSS from the global-styles stylesheet and adds it to the inline CSS for each block. * This filter has to be registered before we call wp_get_global_stylesheet(); */ - add_filter( 'gutenberg_get_style_nodes', 'filter_out_block_nodes' ); + add_filter( 'theme_json_get_style_nodes', 'wp_filter_out_block_nodes' ); $stylesheet = wp_get_global_stylesheet(); if ( empty( $stylesheet ) ) { From ccc17e1e48a79ff29f5de98396b0008555aa6512 Mon Sep 17 00:00:00 2001 From: hellofromtonya Date: Mon, 26 Sep 2022 13:38:46 -0500 Subject: [PATCH 3/4] Add remaining changes from GB PR44363 --- .../class-wp-theme-json-resolver.php | 6 ++---- src/wp-includes/class-wp-theme-json.php | 2 ++ src/wp-includes/global-styles-and-settings.php | 18 +++++++++++++++--- 3 files changed, 19 insertions(+), 7 deletions(-) diff --git a/src/wp-includes/class-wp-theme-json-resolver.php b/src/wp-includes/class-wp-theme-json-resolver.php index 16b74c8c72100..e97c31a4f5ed9 100644 --- a/src/wp-includes/class-wp-theme-json-resolver.php +++ b/src/wp-includes/class-wp-theme-json-resolver.php @@ -295,12 +295,10 @@ public static function get_block_data() { * * @param WP_Theme_JSON_Data Class to access and update the underlying data. */ - $theme_json = apply_filters( 'theme_json_blocks', new WP_Theme_JSON_Data( $config, 'core' ) ); + $theme_json = apply_filters( 'theme_json_blocks', new WP_Theme_JSON_Data( $config, 'blocks' ) ); $config = $theme_json->get_data(); - // Core here means it's the lower level part of the styles chain. - // It can be a core or a third-party block. - return new WP_Theme_JSON( $config, 'core' ); + return new WP_Theme_JSON( $config, 'blocks' ); } /** diff --git a/src/wp-includes/class-wp-theme-json.php b/src/wp-includes/class-wp-theme-json.php index 40262661e4c93..91a56936ac345 100644 --- a/src/wp-includes/class-wp-theme-json.php +++ b/src/wp-includes/class-wp-theme-json.php @@ -50,10 +50,12 @@ class WP_Theme_JSON { * The sources of data this object can represent. * * @since 5.8.0 + * @since 6.1.0 Added 'blocks'. * @var string[] */ const VALID_ORIGINS = array( 'default', + 'blocks', 'theme', 'custom', ); diff --git a/src/wp-includes/global-styles-and-settings.php b/src/wp-includes/global-styles-and-settings.php index bad95ebd05e63..da1cb97c07cee 100644 --- a/src/wp-includes/global-styles-and-settings.php +++ b/src/wp-includes/global-styles-and-settings.php @@ -113,15 +113,21 @@ function wp_get_global_stylesheet( $types = array() ) { } /* - * If variables are part of the stylesheet, - * we add them for all origins (default, theme, user). + * If variables are part of the stylesheet, then add them. * This is so themes without a theme.json still work as before 5.9: * they can override the default presets. * See https://core.trac.wordpress.org/ticket/54782 */ $styles_variables = ''; if ( in_array( 'variables', $types, true ) ) { - $styles_variables = $tree->get_stylesheet( array( 'variables' ) ); + /* + * Only use the default, theme, and custom origins. Why? + * Because styles for `blocks` origin are added at a later phase + * (i.e. in the render cycle). Here, only the ones in use are rendered. + * @see wp_add_global_styles_for_blocks + */ + $origins = array( 'default', 'theme', 'custom' ); + $styles_variables = $tree->get_stylesheet( array( 'variables' ), $origins ); $types = array_diff( $types, array( 'variables' ) ); } @@ -133,6 +139,12 @@ function wp_get_global_stylesheet( $types = array() ) { */ $styles_rest = ''; if ( ! empty( $types ) ) { + /* + * Only use the default, theme, and custom origins. Why? + * Because styles for `blocks` origin are added at a later phase + * (i.e. in the render cycle). Here, only the ones in use are rendered. + * @see wp_add_global_styles_for_blocks + */ $origins = array( 'default', 'theme', 'custom' ); if ( ! $supports_theme_json ) { $origins = array( 'default' ); From 50fc760a4ce7a327b6e845d2e7ce7745f031b650 Mon Sep 17 00:00:00 2001 From: hellofromtonya Date: Mon, 26 Sep 2022 16:05:15 -0500 Subject: [PATCH 4/4] Removes "we" from inline docs --- src/wp-includes/script-loader.php | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/src/wp-includes/script-loader.php b/src/wp-includes/script-loader.php index d5ae209904982..f3014a241f336 100644 --- a/src/wp-includes/script-loader.php +++ b/src/wp-includes/script-loader.php @@ -2408,12 +2408,13 @@ function wp_enqueue_global_styles() { return; } - /** - * If we are loading CSS for each block separately, then we can load the theme.json CSS conditionally. + /* + * If loading the CSS for each block separately, then load the theme.json CSS conditionally. * This removes the CSS from the global-styles stylesheet and adds it to the inline CSS for each block. - * This filter has to be registered before we call wp_get_global_stylesheet(); + * This filter must be registered before calling wp_get_global_stylesheet(); */ add_filter( 'theme_json_get_style_nodes', 'wp_filter_out_block_nodes' ); + $stylesheet = wp_get_global_stylesheet(); if ( empty( $stylesheet ) ) { @@ -2424,7 +2425,7 @@ function wp_enqueue_global_styles() { wp_add_inline_style( 'global-styles', $stylesheet ); wp_enqueue_style( 'global-styles' ); - // add each block as an inline css. + // Add each block as an inline css. wp_add_global_styles_for_blocks(); }