From 16ca4ac777d7f77d34eb23ed3584ee3c130061bd Mon Sep 17 00:00:00 2001 From: George Mamadashvili Date: Thu, 4 May 2023 11:26:17 +0400 Subject: [PATCH 1/6] Plugin: Use bundled files for enqueuing global styles --- .../get-global-styles-and-settings.php | 78 ---------- lib/global-styles-and-settings.php | 139 ++++++++++++++++++ lib/load.php | 2 + lib/script-loader.php | 59 ++++++++ 4 files changed, 200 insertions(+), 78 deletions(-) create mode 100644 lib/global-styles-and-settings.php create mode 100644 lib/script-loader.php diff --git a/lib/compat/wordpress-6.2/get-global-styles-and-settings.php b/lib/compat/wordpress-6.2/get-global-styles-and-settings.php index e02a0466a0b98f..138ee95065b7a9 100644 --- a/lib/compat/wordpress-6.2/get-global-styles-and-settings.php +++ b/lib/compat/wordpress-6.2/get-global-styles-and-settings.php @@ -90,84 +90,6 @@ function gutenberg_get_global_styles_custom_css() { return $stylesheet; } -/** - * Returns the stylesheet resulting of merging core, theme, and user data. - * - * @param array $types Types of styles to load. Optional. - * It accepts as values: 'variables', 'presets', 'styles', 'base-layout-styles. - * If empty, it'll load the following: - * - for themes without theme.json: 'variables', 'presets', 'base-layout-styles'. - * - for themes with theme.json: 'variables', 'presets', 'styles'. - * - * @return string Stylesheet. - */ -function gutenberg_get_global_stylesheet( $types = array() ) { - // Ignore cache when `WP_DEBUG` is enabled, so it doesn't interfere with the theme developers workflow. - $can_use_cached = empty( $types ) && ! WP_DEBUG; - $cache_key = 'gutenberg_get_global_stylesheet'; - $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_Gutenberg::get_merged_data(); - $supports_theme_json = wp_theme_has_theme_json(); - if ( empty( $types ) && ! $supports_theme_json ) { - $types = array( 'variables', 'presets', 'base-layout-styles' ); - } elseif ( empty( $types ) ) { - $types = array( 'variables', 'presets', 'styles' ); - } - - /* - * If variables are part of the stylesheet, - * we 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 ) ) { - /* - * We only use the default, theme, and custom origins. - * This is because styles for blocks origin are added - * at a later phase (render cycle) so we only render the ones in use. - * @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' ) ); - } - - /* - * For the remaining types (presets, styles), we do consider origins: - * - * - themes without theme.json: only the classes for the presets defined by core - * - themes with theme.json: the presets and styles classes, both from core and the theme - */ - $styles_rest = ''; - if ( ! empty( $types ) ) { - /* - * We only use the default, theme, and custom origins. - * This is because styles for blocks origin are added - * at a later phase (render cycle) so we only render the ones in use. - * @see wp_add_global_styles_for_blocks - */ - $origins = array( 'default', 'theme', 'custom' ); - if ( ! $supports_theme_json ) { - $origins = array( 'default' ); - } - $styles_rest = $tree->get_stylesheet( $types, $origins ); - } - $stylesheet = $styles_variables . $styles_rest; - if ( $can_use_cached ) { - wp_cache_set( $cache_key, $stylesheet, $cache_group ); - } - return $stylesheet; -} - /** * Function to get the settings resulting of merging core, theme, and user data. * diff --git a/lib/global-styles-and-settings.php b/lib/global-styles-and-settings.php new file mode 100644 index 00000000000000..a096d6e3f936a8 --- /dev/null +++ b/lib/global-styles-and-settings.php @@ -0,0 +1,139 @@ +get_stylesheet( array( 'variables' ), $origins ); + $types = array_diff( $types, array( 'variables' ) ); + } + + /* + * For the remaining types (presets, styles), we do consider origins: + * + * - themes without theme.json: only the classes for the presets defined by core + * - themes with theme.json: the presets and styles classes, both from core and the theme + */ + $styles_rest = ''; + if ( ! empty( $types ) ) { + /* + * We only use the default, theme, and custom origins. + * This is because styles for blocks origin are added + * at a later phase (render cycle) so we only render the ones in use. + * @see wp_add_global_styles_for_blocks + */ + $origins = array( 'default', 'theme', 'custom' ); + if ( ! $supports_theme_json ) { + $origins = array( 'default' ); + } + $styles_rest = $tree->get_stylesheet( $types, $origins ); + } + $stylesheet = $styles_variables . $styles_rest; + 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. + * + * @return void + */ +function gutenberg_add_global_styles_for_blocks() { + $tree = WP_Theme_JSON_Resolver_Gutenberg::get_merged_data(); + $block_nodes = $tree->get_styles_block_nodes(); + 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; + } + + $stylesheet_handle = 'global-styles'; + if ( isset( $metadata['name'] ) ) { + /* + * These block styles are added on block_render. + * This hooks inline CSS to them so that they are loaded conditionally + * based on whether or not the block is used on the page. + */ + if ( str_starts_with( $metadata['name'], 'core/' ) ) { + $block_name = str_replace( 'core/', '', $metadata['name'] ); + $stylesheet_handle = 'wp-block-' . $block_name; + } + wp_add_inline_style( $stylesheet_handle, $block_css ); + } + + // The likes of block element styles from theme.json do not have $metadata['name'] set. + if ( ! isset( $metadata['name'] ) && ! empty( $metadata['path'] ) ) { + $result = array_values( + array_filter( + $metadata['path'], + function ( $item ) { + if ( strpos( $item, 'core/' ) !== false ) { + return true; + } + return false; + } + ) + ); + if ( isset( $result[0] ) ) { + if ( str_starts_with( $result[0], 'core/' ) ) { + $block_name = str_replace( 'core/', '', $result[0] ); + $stylesheet_handle = 'wp-block-' . $block_name; + } + wp_add_inline_style( $stylesheet_handle, $block_css ); + } + } + } +} diff --git a/lib/load.php b/lib/load.php index 79b076251e54ad..6e5fdff8f79d39 100644 --- a/lib/load.php +++ b/lib/load.php @@ -127,6 +127,8 @@ function gutenberg_is_experiment_enabled( $name ) { } // Plugin specific code. +require __DIR__ . '/script-loader.php'; +require __DIR__ . '/global-styles-and-settings.php'; require __DIR__ . '/class-wp-theme-json-data-gutenberg.php'; require __DIR__ . '/class-wp-theme-json-gutenberg.php'; require __DIR__ . '/class-wp-theme-json-resolver-gutenberg.php'; diff --git a/lib/script-loader.php b/lib/script-loader.php new file mode 100644 index 00000000000000..5ed493685e99f5 --- /dev/null +++ b/lib/script-loader.php @@ -0,0 +1,59 @@ + Date: Thu, 4 May 2023 18:15:46 +0400 Subject: [PATCH 2/6] Override the '__experimentalFeatures' --- lib/block-editor-settings.php | 23 +++++++++ .../wordpress-6.2/block-editor-settings.php | 3 -- .../get-global-styles-and-settings.php | 47 ------------------- lib/global-styles-and-settings.php | 47 +++++++++++++++++++ lib/load.php | 1 + lib/script-loader.php | 3 +- 6 files changed, 72 insertions(+), 52 deletions(-) create mode 100644 lib/block-editor-settings.php diff --git a/lib/block-editor-settings.php b/lib/block-editor-settings.php new file mode 100644 index 00000000000000..86f1c42603cd7d --- /dev/null +++ b/lib/block-editor-settings.php @@ -0,0 +1,23 @@ +get_settings(); - wp_cache_set( $cache_key, $settings, $cache_group ); - } - - return _wp_array_get( $settings, $path, $settings ); -} - /** * Private function to clean the caches used by gutenberg_get_global_settings method. * diff --git a/lib/global-styles-and-settings.php b/lib/global-styles-and-settings.php index a096d6e3f936a8..670563df358c60 100644 --- a/lib/global-styles-and-settings.php +++ b/lib/global-styles-and-settings.php @@ -84,6 +84,53 @@ function gutenberg_get_global_stylesheet( $types = array() ) { return $stylesheet; } +/** + * Function to get the settings resulting of merging core, theme, and user data. + * + * @param array $path Path to the specific setting to retrieve. Optional. + * If empty, will return all settings. + * @param array $context { + * Metadata to know where to retrieve the $path from. Optional. + * + * @type string $block_name Which block to retrieve the settings from. + * If empty, it'll return the settings for the global context. + * @type string $origin Which origin to take data from. + * Valid values are 'all' (core, theme, and user) or 'base' (core and theme). + * If empty or unknown, 'all' is used. + * } + * + * @return array The settings to retrieve. + */ +function gutenberg_get_global_settings( $path = array(), $context = array() ) { + if ( ! empty( $context['block_name'] ) ) { + $new_path = array( 'blocks', $context['block_name'] ); + foreach ( $path as $subpath ) { + $new_path[] = $subpath; + } + $path = $new_path; + } + + // This is the default value when no origin is provided or when it is 'all'. + $origin = 'custom'; + if ( + ! wp_theme_has_theme_json() || + ( isset( $context['origin'] ) && 'base' === $context['origin'] ) + ) { + $origin = 'theme'; + } + + $cache_group = 'theme_json'; + $cache_key = 'gutenberg_get_global_settings_' . $origin; + $settings = wp_cache_get( $cache_key, $cache_group ); + + if ( false === $settings || WP_DEBUG ) { + $settings = WP_Theme_JSON_Resolver_Gutenberg::get_merged_data( $origin )->get_settings(); + wp_cache_set( $cache_key, $settings, $cache_group ); + } + + return _wp_array_get( $settings, $path, $settings ); +} + /** * Adds global style rules to the inline style for each block. * diff --git a/lib/load.php b/lib/load.php index 6e5fdff8f79d39..00fdce2e16b9e8 100644 --- a/lib/load.php +++ b/lib/load.php @@ -134,6 +134,7 @@ function gutenberg_is_experiment_enabled( $name ) { require __DIR__ . '/class-wp-theme-json-resolver-gutenberg.php'; require __DIR__ . '/class-wp-duotone-gutenberg.php'; require __DIR__ . '/blocks.php'; +require __DIR__ . '/block-editor-settings.php'; require __DIR__ . '/client-assets.php'; require __DIR__ . '/demo.php'; require __DIR__ . '/experiments-page.php'; diff --git a/lib/script-loader.php b/lib/script-loader.php index 5ed493685e99f5..a5ba255c2f0d69 100644 --- a/lib/script-loader.php +++ b/lib/script-loader.php @@ -12,8 +12,7 @@ /** * Enqueues the global styles defined via theme.json. * - * Copy of the core `wp_enqueue_global_styles`. - * Uses `WP_Theme_JSON` classes and helper methods bundled with the plugin. + * Copy of the core `wp_enqueue_global_styles`. Uses helper methods bundled with the plugin. * * @return void */ From 6687c4bca5172383112cb4524049d020b7061348 Mon Sep 17 00:00:00 2001 From: George Mamadashvili Date: Fri, 5 May 2023 09:14:27 +0400 Subject: [PATCH 3/6] Recreate global styles for block editors --- lib/block-editor-settings.php | 59 +++++++++++++++++++ .../wordpress-6.2/block-editor-settings.php | 2 +- .../get-global-styles-and-settings.php | 50 ++++++++-------- lib/compat/wordpress-6.2/script-loader.php | 2 +- 4 files changed, 87 insertions(+), 26 deletions(-) diff --git a/lib/block-editor-settings.php b/lib/block-editor-settings.php index 86f1c42603cd7d..bf4154b7281f5e 100644 --- a/lib/block-editor-settings.php +++ b/lib/block-editor-settings.php @@ -15,6 +15,65 @@ * @return array New block editor settings. */ function gutenberg_get_block_editor_settings( $settings ) { + // Recreate global styles. + $global_styles = array(); + $presets = array( + array( + 'css' => 'variables', + '__unstableType' => 'presets', + 'isGlobalStyles' => true, + ), + array( + 'css' => 'presets', + '__unstableType' => 'presets', + 'isGlobalStyles' => true, + ), + ); + foreach ( $presets as $preset_style ) { + $actual_css = gutenberg_get_global_stylesheet( array( $preset_style['css'] ) ); + if ( '' !== $actual_css ) { + $preset_style['css'] = $actual_css; + $global_styles[] = $preset_style; + } + } + + if ( wp_theme_has_theme_json() ) { + $block_classes = array( + 'css' => 'styles', + '__unstableType' => 'theme', + 'isGlobalStyles' => true, + ); + $actual_css = gutenberg_get_global_stylesheet( array( $block_classes['css'] ) ); + if ( '' !== $actual_css ) { + $block_classes['css'] = $actual_css; + $global_styles[] = $block_classes; + } + + /* + * Add the custom CSS as a separate stylesheet so any invalid CSS + * entered by users does not break other global styles. + */ + $global_styles[] = array( + 'css' => wp_get_global_styles_custom_css(), + '__unstableType' => 'user', + 'isGlobalStyles' => true, + ); + } else { + // If there is no `theme.json` file, ensure base layout styles are still available. + $block_classes = array( + 'css' => 'base-layout-styles', + '__unstableType' => 'base-layout', + 'isGlobalStyles' => true, + ); + $actual_css = gutenberg_get_global_stylesheet( array( $block_classes['css'] ) ); + if ( '' !== $actual_css ) { + $block_classes['css'] = $actual_css; + $global_styles[] = $block_classes; + } + } + + $settings['styles'] = array_merge( $global_styles, get_block_editor_theme_styles() ); + // Copied from get_block_editor_settings() at wordpress-develop/block-editor.php. $settings['__experimentalFeatures'] = gutenberg_get_global_settings(); diff --git a/lib/compat/wordpress-6.2/block-editor-settings.php b/lib/compat/wordpress-6.2/block-editor-settings.php index 2fd8dc1e2f6bb8..b22da52e42ce03 100644 --- a/lib/compat/wordpress-6.2/block-editor-settings.php +++ b/lib/compat/wordpress-6.2/block-editor-settings.php @@ -16,7 +16,7 @@ function gutenberg_get_block_editor_settings_6_2( $settings ) { if ( wp_theme_has_theme_json() ) { // Add the custom CSS as separate style sheet so any invalid CSS entered by users does not break other global styles. $settings['styles'][] = array( - 'css' => gutenberg_get_global_styles_custom_css(), + 'css' => wp_get_global_styles_custom_css(), '__unstableType' => 'user', 'isGlobalStyles' => true, ); diff --git a/lib/compat/wordpress-6.2/get-global-styles-and-settings.php b/lib/compat/wordpress-6.2/get-global-styles-and-settings.php index ae6dfc16c97038..e41dc4f9a491b1 100644 --- a/lib/compat/wordpress-6.2/get-global-styles-and-settings.php +++ b/lib/compat/wordpress-6.2/get-global-styles-and-settings.php @@ -59,35 +59,37 @@ function wp_theme_has_theme_json_clean_cache() { } } -/** - * Gets the global styles custom css from theme.json. - * - * @return string - */ -function gutenberg_get_global_styles_custom_css() { - // Ignore cache when `WP_DEBUG` is enabled, so it doesn't interfere with the theme developers workflow. - $can_use_cached = ! WP_DEBUG; - $cache_key = 'gutenberg_get_global_custom_css'; - $cache_group = 'theme_json'; - if ( $can_use_cached ) { - $cached = wp_cache_get( $cache_key, $cache_group ); - if ( $cached ) { - return $cached; +if ( ! function_exists( 'wp_get_global_styles_custom_css' ) ) { + /** + * Gets the global styles custom css from theme.json. + * + * @return string + */ + function wp_get_global_styles_custom_css() { + // Ignore cache when `WP_DEBUG` is enabled, so it doesn't interfere with the theme developers workflow. + $can_use_cached = ! WP_DEBUG; + $cache_key = 'gutenberg_get_global_custom_css'; + $cache_group = 'theme_json'; + if ( $can_use_cached ) { + $cached = wp_cache_get( $cache_key, $cache_group ); + if ( $cached ) { + return $cached; + } } - } - if ( ! wp_theme_has_theme_json() ) { - return ''; - } + if ( ! wp_theme_has_theme_json() ) { + return ''; + } - $tree = WP_Theme_JSON_Resolver_Gutenberg::get_merged_data(); - $stylesheet = $tree->get_custom_css(); + $tree = WP_Theme_JSON_Resolver_Gutenberg::get_merged_data(); + $stylesheet = $tree->get_custom_css(); - if ( $can_use_cached ) { - wp_cache_set( $cache_key, $stylesheet, $cache_group ); - } + if ( $can_use_cached ) { + wp_cache_set( $cache_key, $stylesheet, $cache_group ); + } - return $stylesheet; + return $stylesheet; + } } /** diff --git a/lib/compat/wordpress-6.2/script-loader.php b/lib/compat/wordpress-6.2/script-loader.php index 142cbf4e1477b3..6ead262da62098 100644 --- a/lib/compat/wordpress-6.2/script-loader.php +++ b/lib/compat/wordpress-6.2/script-loader.php @@ -167,7 +167,7 @@ function gutenberg_enqueue_global_styles_custom_css() { remove_action( 'wp_head', 'wp_custom_css_cb', 101 ); $custom_css = wp_get_custom_css(); - $custom_css .= gutenberg_get_global_styles_custom_css(); + $custom_css .= wp_get_global_styles_custom_css(); if ( ! empty( $custom_css ) ) { wp_add_inline_style( 'global-styles', $custom_css ); From f43a4970e2aa0b3090f119de4bded1ca10fbffb5 Mon Sep 17 00:00:00 2001 From: George Mamadashvili Date: Tue, 9 May 2023 20:43:04 +0400 Subject: [PATCH 4/6] Move global styles custom CSS handlers --- lib/block-editor-settings.php | 2 +- .../wordpress-6.2/block-editor-settings.php | 28 ---------------- .../get-global-styles-and-settings.php | 33 ------------------- lib/compat/wordpress-6.2/script-loader.php | 22 ------------- lib/global-styles-and-settings.php | 31 +++++++++++++++++ lib/load.php | 1 - lib/script-loader.php | 23 +++++++++++++ 7 files changed, 55 insertions(+), 85 deletions(-) delete mode 100644 lib/compat/wordpress-6.2/block-editor-settings.php diff --git a/lib/block-editor-settings.php b/lib/block-editor-settings.php index bf4154b7281f5e..c0f514da8d85ef 100644 --- a/lib/block-editor-settings.php +++ b/lib/block-editor-settings.php @@ -54,7 +54,7 @@ function gutenberg_get_block_editor_settings( $settings ) { * entered by users does not break other global styles. */ $global_styles[] = array( - 'css' => wp_get_global_styles_custom_css(), + 'css' => gutenberg_get_global_styles_custom_css(), '__unstableType' => 'user', 'isGlobalStyles' => true, ); diff --git a/lib/compat/wordpress-6.2/block-editor-settings.php b/lib/compat/wordpress-6.2/block-editor-settings.php deleted file mode 100644 index b22da52e42ce03..00000000000000 --- a/lib/compat/wordpress-6.2/block-editor-settings.php +++ /dev/null @@ -1,28 +0,0 @@ - wp_get_global_styles_custom_css(), - '__unstableType' => 'user', - 'isGlobalStyles' => true, - ); - } - - return $settings; -} - -add_filter( 'block_editor_settings_all', 'gutenberg_get_block_editor_settings_6_2', PHP_INT_MAX ); diff --git a/lib/compat/wordpress-6.2/get-global-styles-and-settings.php b/lib/compat/wordpress-6.2/get-global-styles-and-settings.php index e41dc4f9a491b1..d62233df95dc7a 100644 --- a/lib/compat/wordpress-6.2/get-global-styles-and-settings.php +++ b/lib/compat/wordpress-6.2/get-global-styles-and-settings.php @@ -59,39 +59,6 @@ function wp_theme_has_theme_json_clean_cache() { } } -if ( ! function_exists( 'wp_get_global_styles_custom_css' ) ) { - /** - * Gets the global styles custom css from theme.json. - * - * @return string - */ - function wp_get_global_styles_custom_css() { - // Ignore cache when `WP_DEBUG` is enabled, so it doesn't interfere with the theme developers workflow. - $can_use_cached = ! WP_DEBUG; - $cache_key = 'gutenberg_get_global_custom_css'; - $cache_group = 'theme_json'; - if ( $can_use_cached ) { - $cached = wp_cache_get( $cache_key, $cache_group ); - if ( $cached ) { - return $cached; - } - } - - if ( ! wp_theme_has_theme_json() ) { - return ''; - } - - $tree = WP_Theme_JSON_Resolver_Gutenberg::get_merged_data(); - $stylesheet = $tree->get_custom_css(); - - if ( $can_use_cached ) { - wp_cache_set( $cache_key, $stylesheet, $cache_group ); - } - - return $stylesheet; - } -} - /** * Private function to clean the caches used by gutenberg_get_global_settings method. * diff --git a/lib/compat/wordpress-6.2/script-loader.php b/lib/compat/wordpress-6.2/script-loader.php index 6ead262da62098..6ed69244726d33 100644 --- a/lib/compat/wordpress-6.2/script-loader.php +++ b/lib/compat/wordpress-6.2/script-loader.php @@ -152,25 +152,3 @@ function( $settings ) { }, 100 ); - -/** - * Enqueues the global styles custom css. - * - * @since 6.2.0 - */ -function gutenberg_enqueue_global_styles_custom_css() { - 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 ); - } -} -add_action( 'wp_enqueue_scripts', 'gutenberg_enqueue_global_styles_custom_css' ); diff --git a/lib/global-styles-and-settings.php b/lib/global-styles-and-settings.php index 670563df358c60..463d4232f2c9ea 100644 --- a/lib/global-styles-and-settings.php +++ b/lib/global-styles-and-settings.php @@ -131,6 +131,37 @@ function gutenberg_get_global_settings( $path = array(), $context = array() ) { return _wp_array_get( $settings, $path, $settings ); } +/** + * Gets the global styles custom css from theme.json. + * + * @return string + */ +function gutenberg_get_global_styles_custom_css() { + // Ignore cache when `WP_DEBUG` is enabled, so it doesn't interfere with the theme developers workflow. + $can_use_cached = ! WP_DEBUG; + $cache_key = 'gutenberg_get_global_custom_css'; + $cache_group = 'theme_json'; + if ( $can_use_cached ) { + $cached = wp_cache_get( $cache_key, $cache_group ); + if ( $cached ) { + return $cached; + } + } + + if ( ! wp_theme_has_theme_json() ) { + return ''; + } + + $tree = WP_Theme_JSON_Resolver_Gutenberg::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/lib/load.php b/lib/load.php index 00fdce2e16b9e8..39d897409f0876 100644 --- a/lib/load.php +++ b/lib/load.php @@ -73,7 +73,6 @@ function gutenberg_is_experiment_enabled( $name ) { require __DIR__ . '/compat/wordpress-6.2/default-filters.php'; require __DIR__ . '/compat/wordpress-6.2/site-editor.php'; require __DIR__ . '/compat/wordpress-6.2/block-editor.php'; -require __DIR__ . '/compat/wordpress-6.2/block-editor-settings.php'; require __DIR__ . '/compat/wordpress-6.2/theme.php'; require __DIR__ . '/compat/wordpress-6.2/widgets.php'; require __DIR__ . '/compat/wordpress-6.2/menu.php'; diff --git a/lib/script-loader.php b/lib/script-loader.php index a5ba255c2f0d69..9a9f80e830376a 100644 --- a/lib/script-loader.php +++ b/lib/script-loader.php @@ -56,3 +56,26 @@ function gutenberg_enqueue_global_styles() { } add_action( 'wp_enqueue_scripts', 'gutenberg_enqueue_global_styles' ); add_action( 'wp_footer', 'gutenberg_enqueue_global_styles', 1 ); + +/** + * Enqueues the global styles custom css. + * + * @since 6.2.0 + */ +function gutenberg_enqueue_global_styles_custom_css() { + 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 .= gutenberg_get_global_styles_custom_css(); + + if ( ! empty( $custom_css ) ) { + wp_add_inline_style( 'global-styles', $custom_css ); + } +} +remove_action( 'wp_enqueue_scripts', 'wp_enqueue_global_styles_custom_css' ); +add_action( 'wp_enqueue_scripts', 'gutenberg_enqueue_global_styles_custom_css' ); From 33f4e04495c08e5db76b2b38b006d05e620dfae8 Mon Sep 17 00:00:00 2001 From: George Mamadashvili Date: Tue, 9 May 2023 20:45:34 +0400 Subject: [PATCH 5/6] Move _gutenberg_clean_theme_json_caches --- lib/compat/wordpress-6.2/default-filters.php | 7 ------- .../get-global-styles-and-settings.php | 14 -------------- lib/global-styles-and-settings.php | 16 ++++++++++++++++ 3 files changed, 16 insertions(+), 21 deletions(-) diff --git a/lib/compat/wordpress-6.2/default-filters.php b/lib/compat/wordpress-6.2/default-filters.php index 6a5bcb3b75b051..7e797af3e43b1f 100644 --- a/lib/compat/wordpress-6.2/default-filters.php +++ b/lib/compat/wordpress-6.2/default-filters.php @@ -17,13 +17,6 @@ * @package gutenberg */ -/** - * When backporting to core, the existing filters hooked to WP_Theme_JSON_Resolver::clean_cached_data() - * need to be removed. - */ -add_action( 'start_previewing_theme', '_gutenberg_clean_theme_json_caches' ); -add_action( 'switch_theme', '_gutenberg_clean_theme_json_caches' ); - /** * This is a temporary fix to ensure that the block editor styles are enqueued * in the order the iframe expects. diff --git a/lib/compat/wordpress-6.2/get-global-styles-and-settings.php b/lib/compat/wordpress-6.2/get-global-styles-and-settings.php index d62233df95dc7a..eb1481a3808b68 100644 --- a/lib/compat/wordpress-6.2/get-global-styles-and-settings.php +++ b/lib/compat/wordpress-6.2/get-global-styles-and-settings.php @@ -59,20 +59,6 @@ function wp_theme_has_theme_json_clean_cache() { } } -/** - * Private function to clean the caches used by gutenberg_get_global_settings method. - * - * @access private - */ -function _gutenberg_clean_theme_json_caches() { - wp_cache_delete( 'wp_theme_has_theme_json', 'theme_json' ); - wp_cache_delete( 'gutenberg_get_global_stylesheet', 'theme_json' ); - wp_cache_delete( 'gutenberg_get_global_settings_custom', 'theme_json' ); - wp_cache_delete( 'gutenberg_get_global_settings_theme', 'theme_json' ); - wp_cache_delete( 'gutenberg_get_global_custom_css', 'theme_json' ); - WP_Theme_JSON_Resolver_Gutenberg::clean_cached_data(); -} - /** * Tell the cache mechanisms not to persist theme.json data across requests. * The data stored under this cache group: diff --git a/lib/global-styles-and-settings.php b/lib/global-styles-and-settings.php index 463d4232f2c9ea..b239080f298c2c 100644 --- a/lib/global-styles-and-settings.php +++ b/lib/global-styles-and-settings.php @@ -215,3 +215,19 @@ function ( $item ) { } } } + +/** + * Private function to clean the caches used by gutenberg_get_global_settings method. + * + * @access private + */ +function _gutenberg_clean_theme_json_caches() { + wp_cache_delete( 'wp_theme_has_theme_json', 'theme_json' ); + wp_cache_delete( 'gutenberg_get_global_stylesheet', 'theme_json' ); + wp_cache_delete( 'gutenberg_get_global_settings_custom', 'theme_json' ); + wp_cache_delete( 'gutenberg_get_global_settings_theme', 'theme_json' ); + wp_cache_delete( 'gutenberg_get_global_custom_css', 'theme_json' ); + WP_Theme_JSON_Resolver_Gutenberg::clean_cached_data(); +} +add_action( 'start_previewing_theme', '_gutenberg_clean_theme_json_caches' ); +add_action( 'switch_theme', '_gutenberg_clean_theme_json_caches' ); From 6a6b725ce9d05867924696a0dc706bdaf7d52c82 Mon Sep 17 00:00:00 2001 From: George Mamadashvili Date: Tue, 9 May 2023 20:49:53 +0400 Subject: [PATCH 6/6] Override global styles custom CSS properties --- lib/script-loader.php | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/lib/script-loader.php b/lib/script-loader.php index 9a9f80e830376a..2b5c7bb08a676f 100644 --- a/lib/script-loader.php +++ b/lib/script-loader.php @@ -79,3 +79,16 @@ function gutenberg_enqueue_global_styles_custom_css() { } remove_action( 'wp_enqueue_scripts', 'wp_enqueue_global_styles_custom_css' ); add_action( 'wp_enqueue_scripts', 'gutenberg_enqueue_global_styles_custom_css' ); + +/** + * Function that enqueues the CSS Custom Properties coming from theme.json. + * + * @since 5.9.0 + */ +function gutenberg_enqueue_global_styles_css_custom_properties() { + wp_register_style( 'global-styles-css-custom-properties', false ); + wp_add_inline_style( 'global-styles-css-custom-properties', gutenberg_get_global_stylesheet( array( 'variables' ) ) ); + wp_enqueue_style( 'global-styles-css-custom-properties' ); +} +remove_action( 'enqueue_block_editor_assets', 'wp_enqueue_global_styles_css_custom_properties' ); +add_action( 'enqueue_block_editor_assets', 'gutenberg_enqueue_global_styles_css_custom_properties' );