From fad27eaeefd1c8fd278e26372fbe88126beaf7ca Mon Sep 17 00:00:00 2001 From: mmtr Date: Wed, 9 Nov 2022 16:10:58 -0700 Subject: [PATCH 01/21] Global Styles: Update `gutenberg_get_global_stylesheet` to use `WP_Object_Cache` --- .../get-global-styles-and-settings.php | 85 --------------- lib/compat/wordpress-6.2/default-filters.php | 4 + .../get-global-styles-and-settings.php | 101 ++++++++++++++++++ phpunit/wp-get-global-stylesheet-test.php | 86 +++++++++++++++ 4 files changed, 191 insertions(+), 85 deletions(-) create mode 100644 phpunit/wp-get-global-stylesheet-test.php diff --git a/lib/compat/wordpress-6.1/get-global-styles-and-settings.php b/lib/compat/wordpress-6.1/get-global-styles-and-settings.php index 35c540ce1c57a6..fd6113c7405c4a 100644 --- a/lib/compat/wordpress-6.1/get-global-styles-and-settings.php +++ b/lib/compat/wordpress-6.1/get-global-styles-and-settings.php @@ -54,88 +54,3 @@ function ( $item ) { } } } - -/** - * Returns the stylesheet resulting of merging core, theme, and user data. - * - * @param array $types Types of styles to load. Optional. - * It accepts 'variables', 'styles', 'presets' as values. - * If empty, it'll load all for themes with theme.json support - * and only [ 'variables', 'presets' ] for themes without theme.json support. - * - * @return string Stylesheet. - */ -function gutenberg_get_global_stylesheet( $types = array() ) { - // Return cached value if it can be used and exists. - // It's cached by theme to make sure that theme switching clears the cache. - $can_use_cached = ( - ( empty( $types ) ) && - ( ! defined( 'WP_DEBUG' ) || ! WP_DEBUG ) && - ( ! defined( 'SCRIPT_DEBUG' ) || ! SCRIPT_DEBUG ) && - ( ! defined( 'REST_REQUEST' ) || ! REST_REQUEST ) && - ! is_admin() - ); - $transient_name = 'gutenberg_global_styles_' . get_stylesheet(); - if ( $can_use_cached ) { - $cached = get_transient( $transient_name ); - 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', 'styles', 'presets' ); - } - - /* - * 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 ) { - // Cache for a minute. - // This cache doesn't need to be any longer, we only want to avoid spikes on high-traffic sites. - set_transient( $transient_name, $stylesheet, MINUTE_IN_SECONDS ); - } - return $stylesheet; -} diff --git a/lib/compat/wordpress-6.2/default-filters.php b/lib/compat/wordpress-6.2/default-filters.php index ac1ce19b425512..d5c01c159db333 100644 --- a/lib/compat/wordpress-6.2/default-filters.php +++ b/lib/compat/wordpress-6.2/default-filters.php @@ -25,3 +25,7 @@ */ add_action( 'switch_theme', 'wp_theme_clean_theme_json_cached_data' ); add_action( 'start_previewing_theme', 'wp_theme_clean_theme_json_cached_data' ); +add_action( 'save_post_wp_global_styles', 'gutenberg_clean_cached_stylesheet' ); +add_action( 'switch_theme', 'gutenberg_clean_cached_stylesheet' ); +add_action( 'start_previewing_theme', 'gutenberg_clean_cached_stylesheet' ); +add_action( 'upgrader_process_complete', '_gutenberg_clean_cached_stylesheet_upon_upgrading_active_theme' ); 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 4dbd9d0ba8bde1..35e21d6d3c3ee1 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 @@ -45,3 +45,104 @@ function wp_theme_clean_theme_json_cached_data() { WP_Theme_JSON_Resolver_Gutenberg::clean_cached_data(); } } + +/** + * Returns the stylesheet resulting of merging core, theme, and user data. + * + * @param array $types Types of styles to load. Optional. + * It accepts 'variables', 'styles', 'presets' as values. + * If empty, it'll load all for themes with theme.json support + * and only [ 'variables', 'presets' ] for themes without theme.json support. + * + * @return string Stylesheet. + */ +function gutenberg_get_global_stylesheet( $types = array() ) { + // Return cached value if it can be used and exists. + // It's cached by theme to make sure that theme switching clears the cache. + $can_use_cached = ( + ( empty( $types ) ) && + ( ! defined( 'WP_DEBUG' ) || ! WP_DEBUG ) && + ( ! defined( 'SCRIPT_DEBUG' ) || ! SCRIPT_DEBUG ) && + ( ! defined( 'REST_REQUEST' ) || ! REST_REQUEST ) && + ! is_admin() + ); + $cache_key = 'gutenberg_global_styles'; + $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', 'styles', 'presets' ); + } + + /* + * 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; +} + +/** + * Invalidate the cached stylesheet. + */ +function gutenberg_clean_cached_stylesheet() { + wp_cache_delete( 'gutenberg_global_styles', 'theme_json' ); +} + +function _gutenberg_clean_cached_stylesheet_upon_upgrading_active_theme( $upgrader, $options ) { + if ( + 'update' === $options['action'] && + 'theme' === $options['type'] && + array_key_exists( get_stylesheet(), $options['themes'] ) + ) { + gutenberg_clean_cached_stylesheet(); + } +} diff --git a/phpunit/wp-get-global-stylesheet-test.php b/phpunit/wp-get-global-stylesheet-test.php new file mode 100644 index 00000000000000..2b8f3db1548ca9 --- /dev/null +++ b/phpunit/wp-get-global-stylesheet-test.php @@ -0,0 +1,86 @@ +orig_theme_dir = $GLOBALS['wp_theme_directories']; + $this->theme_root = realpath( DIR_TESTDATA . '/themedir1' ); + + // /themes is necessary as theme.php functions assume /themes is the root if there is only one root. + $GLOBALS['wp_theme_directories'] = array( WP_CONTENT_DIR . '/themes', $this->theme_root ); + + // Set up the new root. + add_filter( 'theme_root', array( $this, 'filter_set_theme_root' ) ); + add_filter( 'stylesheet_root', array( $this, 'filter_set_theme_root' ) ); + add_filter( 'template_root', array( $this, 'filter_set_theme_root' ) ); + + // Clear caches. + wp_clean_themes_cache(); + unset( $GLOBALS['wp_themes'] ); + } + + public function tear_down() { + $GLOBALS['wp_theme_directories'] = $this->orig_theme_dir; + + // Clear up the filters to modify the theme root. + remove_filter( 'theme_root', array( $this, 'filter_set_theme_root' ) ); + remove_filter( 'stylesheet_root', array( $this, 'filter_set_theme_root' ) ); + remove_filter( 'template_root', array( $this, 'filter_set_theme_root' ) ); + + wp_clean_themes_cache(); + unset( $GLOBALS['wp_themes'] ); + + parent::tear_down(); + } + + /** + * Cleans up global scope. + * + * @global WP_Styles $wp_styles + */ + public function clean_up_global_scope() { + global $wp_styles; + parent::clean_up_global_scope(); + $wp_styles = null; + } + + public function filter_set_theme_root() { + return $this->theme_root; + } + + public function test_global_styles_changes_invalidates_cache() { + switch_theme( 'block-theme' ); + + $user_cpt = WP_Theme_JSON_Resolver_Gutenberg::get_user_data_from_wp_global_styles( wp_get_theme(), true ); + + $config = json_decode( $user_cpt['post_content'], true ); + $config['styles']['elements']['button']['color']['background'] = 'hotpink'; + $user_cpt['post_content'] = wp_json_encode( $config ); + + wp_update_post( $user_cpt, true, false ); + + $styles = gutenberg_get_global_stylesheet(); + $this->assertStringContainsString( '.button{background-color: hotpink;}', $styles ); + } +} From 0a23b43f0e5a73c0f97f2851e30e40e658a1661b Mon Sep 17 00:00:00 2001 From: mmtr Date: Wed, 9 Nov 2022 16:45:03 -0700 Subject: [PATCH 02/21] Add remaining hooks --- lib/compat/wordpress-6.2/default-filters.php | 5 ++++- .../wordpress-6.2/get-global-styles-and-settings.php | 12 ++++++++---- 2 files changed, 12 insertions(+), 5 deletions(-) diff --git a/lib/compat/wordpress-6.2/default-filters.php b/lib/compat/wordpress-6.2/default-filters.php index d5c01c159db333..cd6a2bdf789b7b 100644 --- a/lib/compat/wordpress-6.2/default-filters.php +++ b/lib/compat/wordpress-6.2/default-filters.php @@ -25,7 +25,10 @@ */ add_action( 'switch_theme', 'wp_theme_clean_theme_json_cached_data' ); add_action( 'start_previewing_theme', 'wp_theme_clean_theme_json_cached_data' ); + add_action( 'save_post_wp_global_styles', 'gutenberg_clean_cached_stylesheet' ); add_action( 'switch_theme', 'gutenberg_clean_cached_stylesheet' ); add_action( 'start_previewing_theme', 'gutenberg_clean_cached_stylesheet' ); -add_action( 'upgrader_process_complete', '_gutenberg_clean_cached_stylesheet_upon_upgrading_active_theme' ); +add_action( 'activated_plugin', 'gutenberg_clean_cached_stylesheet' ); +add_action( 'deactivated_plugin', 'gutenberg_clean_cached_stylesheet' ); +add_action( 'upgrader_process_complete', '_gutenberg_clean_cached_stylesheet_upon_upgrading' ); 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 35e21d6d3c3ee1..1006282f004457 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 @@ -137,11 +137,15 @@ function gutenberg_clean_cached_stylesheet() { wp_cache_delete( 'gutenberg_global_styles', 'theme_json' ); } -function _gutenberg_clean_cached_stylesheet_upon_upgrading_active_theme( $upgrader, $options ) { +function _gutenberg_clean_cached_stylesheet_upon_upgrading( $upgrader, $options ) { + if ( 'update' !== $options['action'] ) { + return; + } + if ( - 'update' === $options['action'] && - 'theme' === $options['type'] && - array_key_exists( get_stylesheet(), $options['themes'] ) + 'core' === $options['type'] || + 'plugin' === $options['type'] || + ( 'theme' === $options['type'] && array_key_exists( get_stylesheet(), $options['themes'] ) ) ) { gutenberg_clean_cached_stylesheet(); } From 714819b7af9d15fb123450f64356d7970c66de39 Mon Sep 17 00:00:00 2001 From: mmtr Date: Wed, 9 Nov 2022 16:51:58 -0700 Subject: [PATCH 03/21] Simplify test setup --- phpunit/wp-get-global-stylesheet-test.php | 39 ++--------------------- 1 file changed, 3 insertions(+), 36 deletions(-) diff --git a/phpunit/wp-get-global-stylesheet-test.php b/phpunit/wp-get-global-stylesheet-test.php index 2b8f3db1548ca9..82691e009a2d3c 100644 --- a/phpunit/wp-get-global-stylesheet-test.php +++ b/phpunit/wp-get-global-stylesheet-test.php @@ -7,34 +7,19 @@ class WP_Get_Global_Stylesheet_Test extends WP_UnitTestCase { - /** - * Theme root directory. - * - * @var string - */ - private $theme_root; - - /** - * Original theme directory. - * - * @var string - */ - private $orig_theme_dir; - public function set_up() { parent::set_up(); + $this->theme_root = realpath( __DIR__ . '/data/themedir1' ); $this->orig_theme_dir = $GLOBALS['wp_theme_directories']; - $this->theme_root = realpath( DIR_TESTDATA . '/themedir1' ); // /themes is necessary as theme.php functions assume /themes is the root if there is only one root. $GLOBALS['wp_theme_directories'] = array( WP_CONTENT_DIR . '/themes', $this->theme_root ); - // Set up the new root. add_filter( 'theme_root', array( $this, 'filter_set_theme_root' ) ); add_filter( 'stylesheet_root', array( $this, 'filter_set_theme_root' ) ); add_filter( 'template_root', array( $this, 'filter_set_theme_root' ) ); - + $this->queries = array(); // Clear caches. wp_clean_themes_cache(); unset( $GLOBALS['wp_themes'] ); @@ -42,29 +27,11 @@ public function set_up() { public function tear_down() { $GLOBALS['wp_theme_directories'] = $this->orig_theme_dir; - - // Clear up the filters to modify the theme root. - remove_filter( 'theme_root', array( $this, 'filter_set_theme_root' ) ); - remove_filter( 'stylesheet_root', array( $this, 'filter_set_theme_root' ) ); - remove_filter( 'template_root', array( $this, 'filter_set_theme_root' ) ); - wp_clean_themes_cache(); unset( $GLOBALS['wp_themes'] ); - parent::tear_down(); } - /** - * Cleans up global scope. - * - * @global WP_Styles $wp_styles - */ - public function clean_up_global_scope() { - global $wp_styles; - parent::clean_up_global_scope(); - $wp_styles = null; - } - public function filter_set_theme_root() { return $this->theme_root; } @@ -75,7 +42,7 @@ public function test_global_styles_changes_invalidates_cache() { $user_cpt = WP_Theme_JSON_Resolver_Gutenberg::get_user_data_from_wp_global_styles( wp_get_theme(), true ); $config = json_decode( $user_cpt['post_content'], true ); - $config['styles']['elements']['button']['color']['background'] = 'hotpink'; + $config['styles']['color']['background'] = 'hotpink'; $user_cpt['post_content'] = wp_json_encode( $config ); wp_update_post( $user_cpt, true, false ); From 4f36f72106c1c17dcd584218a8d685782bca8342 Mon Sep 17 00:00:00 2001 From: mmtr Date: Wed, 9 Nov 2022 16:53:12 -0700 Subject: [PATCH 04/21] Rename cache key and functions --- lib/compat/wordpress-6.2/default-filters.php | 12 ++++++------ .../wordpress-6.2/get-global-styles-and-settings.php | 8 ++++---- 2 files changed, 10 insertions(+), 10 deletions(-) diff --git a/lib/compat/wordpress-6.2/default-filters.php b/lib/compat/wordpress-6.2/default-filters.php index cd6a2bdf789b7b..5bdcc3b1416cc7 100644 --- a/lib/compat/wordpress-6.2/default-filters.php +++ b/lib/compat/wordpress-6.2/default-filters.php @@ -26,9 +26,9 @@ add_action( 'switch_theme', 'wp_theme_clean_theme_json_cached_data' ); add_action( 'start_previewing_theme', 'wp_theme_clean_theme_json_cached_data' ); -add_action( 'save_post_wp_global_styles', 'gutenberg_clean_cached_stylesheet' ); -add_action( 'switch_theme', 'gutenberg_clean_cached_stylesheet' ); -add_action( 'start_previewing_theme', 'gutenberg_clean_cached_stylesheet' ); -add_action( 'activated_plugin', 'gutenberg_clean_cached_stylesheet' ); -add_action( 'deactivated_plugin', 'gutenberg_clean_cached_stylesheet' ); -add_action( 'upgrader_process_complete', '_gutenberg_clean_cached_stylesheet_upon_upgrading' ); +add_action( 'save_post_wp_global_styles', 'gutenberg_get_global_stylesheet_clean_cache' ); +add_action( 'switch_theme', 'gutenberg_get_global_stylesheet_clean_cache' ); +add_action( 'start_previewing_theme', 'gutenberg_get_global_stylesheet_clean_cache' ); +add_action( 'activated_plugin', 'gutenberg_get_global_stylesheet_clean_cache' ); +add_action( 'deactivated_plugin', 'gutenberg_get_global_stylesheet_clean_cache' ); +add_action( 'upgrader_process_complete', '_gutenberg_get_global_stylesheet_clean_cache_upon_upgrading' ); 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 1006282f004457..7ebf54e40cc7c4 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 @@ -66,7 +66,7 @@ function gutenberg_get_global_stylesheet( $types = array() ) { ( ! defined( 'REST_REQUEST' ) || ! REST_REQUEST ) && ! is_admin() ); - $cache_key = 'gutenberg_global_styles'; + $cache_key = 'gutenberg_get_global_stylesheet'; $cache_group = 'theme_json'; if ( $can_use_cached ) { $cached = wp_cache_get( $cache_key, $cache_group ); @@ -133,11 +133,11 @@ function gutenberg_get_global_stylesheet( $types = array() ) { /** * Invalidate the cached stylesheet. */ -function gutenberg_clean_cached_stylesheet() { - wp_cache_delete( 'gutenberg_global_styles', 'theme_json' ); +function gutenberg_get_global_stylesheet_clean_cache() { + wp_cache_delete( 'gutenberg_get_global_stylesheet', 'theme_json' ); } -function _gutenberg_clean_cached_stylesheet_upon_upgrading( $upgrader, $options ) { +function _gutenberg_get_global_stylesheet_clean_cache_upon_upgrading( $upgrader, $options ) { if ( 'update' !== $options['action'] ) { return; } From a845e8538d1c04c497f4c4bdf6ebf7e9c2359e61 Mon Sep 17 00:00:00 2001 From: mmtr Date: Wed, 16 Nov 2022 13:29:42 +0100 Subject: [PATCH 05/21] Add filter to shortcircuit the cache --- .../get-global-styles-and-settings.php | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) 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 7ebf54e40cc7c4..bb11dcc9817432 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 @@ -57,14 +57,18 @@ function wp_theme_clean_theme_json_cached_data() { * @return string Stylesheet. */ function gutenberg_get_global_stylesheet( $types = array() ) { - // Return cached value if it can be used and exists. - // It's cached by theme to make sure that theme switching clears the cache. - $can_use_cached = ( + /** + * Filters whether the cached global stylesheet can be used. + * + * @since 14.6.0 + * + * @param boolean $can_use_cached Whether the cached global stylesheet can be used. + */ + $can_use_cached = apply_filters( + 'global_stylesheet_can_use_cache', ( empty( $types ) ) && ( ! defined( 'WP_DEBUG' ) || ! WP_DEBUG ) && - ( ! defined( 'SCRIPT_DEBUG' ) || ! SCRIPT_DEBUG ) && - ( ! defined( 'REST_REQUEST' ) || ! REST_REQUEST ) && - ! is_admin() + ( ! defined( 'SCRIPT_DEBUG' ) || ! SCRIPT_DEBUG ) ); $cache_key = 'gutenberg_get_global_stylesheet'; $cache_group = 'theme_json'; From 6014519082ef46a1691a0b40177278efb886a78d Mon Sep 17 00:00:00 2001 From: mmtr Date: Wed, 16 Nov 2022 13:30:37 +0100 Subject: [PATCH 06/21] Clean cache within `wp_theme_clean_theme_json_cached_data` --- lib/compat/wordpress-6.2/default-filters.php | 11 +++--- .../get-global-styles-and-settings.php | 34 +++++++++---------- 2 files changed, 21 insertions(+), 24 deletions(-) diff --git a/lib/compat/wordpress-6.2/default-filters.php b/lib/compat/wordpress-6.2/default-filters.php index 5bdcc3b1416cc7..8c9517f5433bf5 100644 --- a/lib/compat/wordpress-6.2/default-filters.php +++ b/lib/compat/wordpress-6.2/default-filters.php @@ -25,10 +25,7 @@ */ add_action( 'switch_theme', 'wp_theme_clean_theme_json_cached_data' ); add_action( 'start_previewing_theme', 'wp_theme_clean_theme_json_cached_data' ); - -add_action( 'save_post_wp_global_styles', 'gutenberg_get_global_stylesheet_clean_cache' ); -add_action( 'switch_theme', 'gutenberg_get_global_stylesheet_clean_cache' ); -add_action( 'start_previewing_theme', 'gutenberg_get_global_stylesheet_clean_cache' ); -add_action( 'activated_plugin', 'gutenberg_get_global_stylesheet_clean_cache' ); -add_action( 'deactivated_plugin', 'gutenberg_get_global_stylesheet_clean_cache' ); -add_action( 'upgrader_process_complete', '_gutenberg_get_global_stylesheet_clean_cache_upon_upgrading' ); +add_action( 'save_post_wp_global_styles', 'wp_theme_clean_theme_json_cached_data' ); +add_action( 'activated_plugin', 'wp_theme_clean_theme_json_cached_data' ); +add_action( 'deactivated_plugin', 'wp_theme_clean_theme_json_cached_data' ); +add_action( 'upgrader_process_complete', '_wp_theme_clean_theme_json_cached_data_upon_upgrading' ); 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 bb11dcc9817432..4803256e6ccdb3 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 @@ -43,6 +43,7 @@ function wp_theme_has_theme_json( $clear_cache = false ) { function wp_theme_clean_theme_json_cached_data() { wp_theme_has_theme_json( true ); WP_Theme_JSON_Resolver_Gutenberg::clean_cached_data(); + wp_cache_delete( 'gutenberg_get_global_stylesheet', 'theme_json' ); } } @@ -134,23 +135,22 @@ function gutenberg_get_global_stylesheet( $types = array() ) { return $stylesheet; } -/** - * Invalidate the cached stylesheet. - */ -function gutenberg_get_global_stylesheet_clean_cache() { - wp_cache_delete( 'gutenberg_get_global_stylesheet', 'theme_json' ); -} - -function _gutenberg_get_global_stylesheet_clean_cache_upon_upgrading( $upgrader, $options ) { - if ( 'update' !== $options['action'] ) { - return; - } +if ( ! function_exists( '_wp_theme_clean_theme_json_cached_data_upon_upgrading' ) ) { + /** + * Clean theme.json related cached data after an upgrade. + */ + function _wp_theme_clean_theme_json_cached_data_upon_upgrading( $upgrader, $options ) { + if ( 'update' !== $options['action'] ) { + return; + } - if ( - 'core' === $options['type'] || - 'plugin' === $options['type'] || - ( 'theme' === $options['type'] && array_key_exists( get_stylesheet(), $options['themes'] ) ) - ) { - gutenberg_clean_cached_stylesheet(); + if ( + 'core' === $options['type'] || + 'plugin' === $options['type'] || + ( 'theme' === $options['type'] && array_key_exists( get_stylesheet(), $options['themes'] ) ) + ) { + wp_theme_clean_theme_json_cached_data(); + } } } + From 5acfd408b55426ad394c9d987564f02146cbf11a Mon Sep 17 00:00:00 2001 From: mmtr Date: Wed, 16 Nov 2022 13:31:04 +0100 Subject: [PATCH 07/21] Fix test --- phpunit/wp-get-global-stylesheet-test.php | 59 ++++++++++++++++++++--- 1 file changed, 52 insertions(+), 7 deletions(-) diff --git a/phpunit/wp-get-global-stylesheet-test.php b/phpunit/wp-get-global-stylesheet-test.php index 82691e009a2d3c..40a24e0719a6cb 100644 --- a/phpunit/wp-get-global-stylesheet-test.php +++ b/phpunit/wp-get-global-stylesheet-test.php @@ -7,19 +7,52 @@ class WP_Get_Global_Stylesheet_Test extends WP_UnitTestCase { + /** + * Administrator ID. + * + * @var int + */ + protected static $administrator_id; + + /** + * Theme root directory. + * + * @var string + */ + private $theme_root; + + /** + * Original theme directory. + * + * @var string + */ + private $orig_theme_dir; + + public static function set_up_before_class() { + parent::set_up_before_class(); + + self::$administrator_id = self::factory()->user->create( + array( + 'role' => 'administrator', + 'user_email' => 'administrator@example.com', + ) + ); + } + public function set_up() { parent::set_up(); - $this->theme_root = realpath( __DIR__ . '/data/themedir1' ); $this->orig_theme_dir = $GLOBALS['wp_theme_directories']; + $this->theme_root = realpath( DIR_TESTDATA . '/themedir1' ); // /themes is necessary as theme.php functions assume /themes is the root if there is only one root. $GLOBALS['wp_theme_directories'] = array( WP_CONTENT_DIR . '/themes', $this->theme_root ); + // Set up the new root. add_filter( 'theme_root', array( $this, 'filter_set_theme_root' ) ); add_filter( 'stylesheet_root', array( $this, 'filter_set_theme_root' ) ); add_filter( 'template_root', array( $this, 'filter_set_theme_root' ) ); - $this->queries = array(); + // Clear caches. wp_clean_themes_cache(); unset( $GLOBALS['wp_themes'] ); @@ -27,8 +60,15 @@ public function set_up() { public function tear_down() { $GLOBALS['wp_theme_directories'] = $this->orig_theme_dir; + + // Clear up the filters to modify the theme root. + remove_filter( 'theme_root', array( $this, 'filter_set_theme_root' ) ); + remove_filter( 'stylesheet_root', array( $this, 'filter_set_theme_root' ) ); + remove_filter( 'template_root', array( $this, 'filter_set_theme_root' ) ); + wp_clean_themes_cache(); unset( $GLOBALS['wp_themes'] ); + parent::tear_down(); } @@ -36,18 +76,23 @@ public function filter_set_theme_root() { return $this->theme_root; } - public function test_global_styles_changes_invalidates_cache() { + public function test_global_styles_user_cpt_change_invalidates_cached_stylesheet() { + add_filter( 'global_stylesheet_can_use_cache', '__return_true' ); switch_theme( 'block-theme' ); + wp_set_current_user( self::$administrator_id ); - $user_cpt = WP_Theme_JSON_Resolver_Gutenberg::get_user_data_from_wp_global_styles( wp_get_theme(), true ); + $styles = gutenberg_get_global_stylesheet(); + $this->assertStringNotContainsString( 'hotpink', $styles ); - $config = json_decode( $user_cpt['post_content'], true ); + $user_cpt = WP_Theme_JSON_Resolver_Gutenberg::get_user_data_from_wp_global_styles( wp_get_theme(), true ); + $config = json_decode( $user_cpt['post_content'], true ); $config['styles']['color']['background'] = 'hotpink'; - $user_cpt['post_content'] = wp_json_encode( $config ); + $user_cpt['post_content'] = wp_json_encode( $config ); wp_update_post( $user_cpt, true, false ); $styles = gutenberg_get_global_stylesheet(); - $this->assertStringContainsString( '.button{background-color: hotpink;}', $styles ); + $this->assertStringContainsString( 'background-color: hotpink;', $styles ); + remove_filter( 'global_stylesheet_can_use_cache', '__return_true' ); } } From 4eaeb71f008bdae834a17e638987dc9f09f3aa31 Mon Sep 17 00:00:00 2001 From: mmtr Date: Wed, 16 Nov 2022 13:55:18 +0100 Subject: [PATCH 08/21] Add missing params in docstring --- lib/compat/wordpress-6.2/get-global-styles-and-settings.php | 3 +++ 1 file changed, 3 insertions(+) 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 4803256e6ccdb3..1edb9f0dcdce0c 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 @@ -138,6 +138,9 @@ function gutenberg_get_global_stylesheet( $types = array() ) { if ( ! function_exists( '_wp_theme_clean_theme_json_cached_data_upon_upgrading' ) ) { /** * Clean theme.json related cached data after an upgrade. + * + * @param WP_Upgrader $upgrader WP_Upgrader instance. + * @param array $options Array of bulk item update data. */ function _wp_theme_clean_theme_json_cached_data_upon_upgrading( $upgrader, $options ) { if ( 'update' !== $options['action'] ) { From e8a32cbd57a4a7a7e861bc982e3dc8fc2fd35584 Mon Sep 17 00:00:00 2001 From: mmtr Date: Wed, 16 Nov 2022 15:24:23 +0100 Subject: [PATCH 09/21] Create separate function to clean the cache --- lib/compat/wordpress-6.2/default-filters.php | 10 +++-- .../get-global-styles-and-settings.php | 43 +++++++++++-------- 2 files changed, 30 insertions(+), 23 deletions(-) diff --git a/lib/compat/wordpress-6.2/default-filters.php b/lib/compat/wordpress-6.2/default-filters.php index 8c9517f5433bf5..fce5902b59395f 100644 --- a/lib/compat/wordpress-6.2/default-filters.php +++ b/lib/compat/wordpress-6.2/default-filters.php @@ -25,7 +25,9 @@ */ add_action( 'switch_theme', 'wp_theme_clean_theme_json_cached_data' ); add_action( 'start_previewing_theme', 'wp_theme_clean_theme_json_cached_data' ); -add_action( 'save_post_wp_global_styles', 'wp_theme_clean_theme_json_cached_data' ); -add_action( 'activated_plugin', 'wp_theme_clean_theme_json_cached_data' ); -add_action( 'deactivated_plugin', 'wp_theme_clean_theme_json_cached_data' ); -add_action( 'upgrader_process_complete', '_wp_theme_clean_theme_json_cached_data_upon_upgrading' ); +add_action( 'save_post_wp_global_styles', 'gutenberg_get_global_stylesheet_clean_cache' ); +add_action( 'switch_theme', 'gutenberg_get_global_stylesheet_clean_cache' ); +add_action( 'start_previewing_theme', 'gutenberg_get_global_stylesheet_clean_cache' ); +add_action( 'activated_plugin', 'gutenberg_get_global_stylesheet_clean_cache' ); +add_action( 'deactivated_plugin', 'gutenberg_get_global_stylesheet_clean_cache' ); +add_action( 'upgrader_process_complete', '_gutenberg_get_global_stylesheet_clean_cache_upon_upgrading' ); 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 1edb9f0dcdce0c..505904c5121c26 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 @@ -43,7 +43,6 @@ function wp_theme_has_theme_json( $clear_cache = false ) { function wp_theme_clean_theme_json_cached_data() { wp_theme_has_theme_json( true ); WP_Theme_JSON_Resolver_Gutenberg::clean_cached_data(); - wp_cache_delete( 'gutenberg_get_global_stylesheet', 'theme_json' ); } } @@ -135,25 +134,31 @@ function gutenberg_get_global_stylesheet( $types = array() ) { return $stylesheet; } -if ( ! function_exists( '_wp_theme_clean_theme_json_cached_data_upon_upgrading' ) ) { - /** - * Clean theme.json related cached data after an upgrade. - * - * @param WP_Upgrader $upgrader WP_Upgrader instance. - * @param array $options Array of bulk item update data. - */ - function _wp_theme_clean_theme_json_cached_data_upon_upgrading( $upgrader, $options ) { - if ( 'update' !== $options['action'] ) { - return; - } +/** + * Clean the cache used by the `gutenberg_get_global_stylesheet` function. + */ +function gutenberg_get_global_stylesheet_clean_cache() { + wp_cache_delete( 'gutenberg_get_global_stylesheet', 'theme_json' ); + WP_Theme_JSON_Resolver_Gutenberg::clean_cached_data(); +} - if ( - 'core' === $options['type'] || - 'plugin' === $options['type'] || - ( 'theme' === $options['type'] && array_key_exists( get_stylesheet(), $options['themes'] ) ) - ) { - wp_theme_clean_theme_json_cached_data(); - } +/** + * Private function to clean the cache used by the `gutenberg_get_global_stylesheet` function after an upgrade. + * + * @param WP_Upgrader $upgrader WP_Upgrader instance. + * @param array $options Array of bulk item update data. + */ +function _gutenberg_get_global_stylesheet_clean_cache_upon_upgrading( $upgrader, $options ) { + if ( 'update' !== $options['action'] ) { + return; + } + + if ( + 'core' === $options['type'] || + 'plugin' === $options['type'] || + ( 'theme' === $options['type'] && array_key_exists( get_stylesheet(), $options['themes'] ) ) + ) { + gutenberg_get_global_stylesheet_clean_cache(); } } From 6931aa838ec403200ffae622d9df6d6aea5db942 Mon Sep 17 00:00:00 2001 From: mmtr Date: Wed, 16 Nov 2022 15:28:54 +0100 Subject: [PATCH 10/21] Remove SCRIPT_DEBUG check since it is not usually part of the theme developers workflow --- lib/compat/wordpress-6.2/get-global-styles-and-settings.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) 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 505904c5121c26..bba52af5e28b98 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 @@ -67,8 +67,8 @@ function gutenberg_get_global_stylesheet( $types = array() ) { $can_use_cached = apply_filters( 'global_stylesheet_can_use_cache', ( empty( $types ) ) && - ( ! defined( 'WP_DEBUG' ) || ! WP_DEBUG ) && - ( ! defined( 'SCRIPT_DEBUG' ) || ! SCRIPT_DEBUG ) + // Ignore cache when `WP_DEBUG` is enabled, so it doesn't interfere with the theme developers workflow. + ( ! defined( 'WP_DEBUG' ) || ! WP_DEBUG ) ); $cache_key = 'gutenberg_get_global_stylesheet'; $cache_group = 'theme_json'; From a2b9d6f8ebd463163d62c2287e0e67f21d66e8d7 Mon Sep 17 00:00:00 2001 From: mmtr Date: Wed, 16 Nov 2022 15:47:29 +0100 Subject: [PATCH 11/21] Clear cache from WP_Theme_JSON_Resolver separately --- lib/compat/wordpress-6.2/default-filters.php | 1 + lib/compat/wordpress-6.2/get-global-styles-and-settings.php | 1 - 2 files changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/compat/wordpress-6.2/default-filters.php b/lib/compat/wordpress-6.2/default-filters.php index fce5902b59395f..62e7d1b1d2ca09 100644 --- a/lib/compat/wordpress-6.2/default-filters.php +++ b/lib/compat/wordpress-6.2/default-filters.php @@ -25,6 +25,7 @@ */ add_action( 'switch_theme', 'wp_theme_clean_theme_json_cached_data' ); add_action( 'start_previewing_theme', 'wp_theme_clean_theme_json_cached_data' ); +add_action( 'save_post_wp_global_styles', array( 'WP_Theme_JSON_Resolver', 'clean_cached_data' ) ); add_action( 'save_post_wp_global_styles', 'gutenberg_get_global_stylesheet_clean_cache' ); add_action( 'switch_theme', 'gutenberg_get_global_stylesheet_clean_cache' ); add_action( 'start_previewing_theme', 'gutenberg_get_global_stylesheet_clean_cache' ); 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 bba52af5e28b98..bd9b57748b915d 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 @@ -139,7 +139,6 @@ function gutenberg_get_global_stylesheet( $types = array() ) { */ function gutenberg_get_global_stylesheet_clean_cache() { wp_cache_delete( 'gutenberg_get_global_stylesheet', 'theme_json' ); - WP_Theme_JSON_Resolver_Gutenberg::clean_cached_data(); } /** From 2acbafdf777d7f09c08d31b9df71abd53515d44f Mon Sep 17 00:00:00 2001 From: mmtr Date: Wed, 16 Nov 2022 15:49:32 +0100 Subject: [PATCH 12/21] Rename filter --- lib/compat/wordpress-6.2/get-global-styles-and-settings.php | 2 +- phpunit/wp-get-global-stylesheet-test.php | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) 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 bd9b57748b915d..430b32fb7289fc 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 @@ -65,7 +65,7 @@ function gutenberg_get_global_stylesheet( $types = array() ) { * @param boolean $can_use_cached Whether the cached global stylesheet can be used. */ $can_use_cached = apply_filters( - 'global_stylesheet_can_use_cache', + 'wp_get_global_stylesheet_can_use_cache', ( empty( $types ) ) && // Ignore cache when `WP_DEBUG` is enabled, so it doesn't interfere with the theme developers workflow. ( ! defined( 'WP_DEBUG' ) || ! WP_DEBUG ) diff --git a/phpunit/wp-get-global-stylesheet-test.php b/phpunit/wp-get-global-stylesheet-test.php index 40a24e0719a6cb..676339a1b427af 100644 --- a/phpunit/wp-get-global-stylesheet-test.php +++ b/phpunit/wp-get-global-stylesheet-test.php @@ -77,7 +77,7 @@ public function filter_set_theme_root() { } public function test_global_styles_user_cpt_change_invalidates_cached_stylesheet() { - add_filter( 'global_stylesheet_can_use_cache', '__return_true' ); + add_filter( 'wp_get_global_stylesheet_can_use_cache', '__return_true' ); switch_theme( 'block-theme' ); wp_set_current_user( self::$administrator_id ); @@ -93,6 +93,6 @@ public function test_global_styles_user_cpt_change_invalidates_cached_stylesheet $styles = gutenberg_get_global_stylesheet(); $this->assertStringContainsString( 'background-color: hotpink;', $styles ); - remove_filter( 'global_stylesheet_can_use_cache', '__return_true' ); + remove_filter( 'wp_get_global_stylesheet_can_use_cache', '__return_true' ); } } From 30361b51d8d72cccdfdfdeb5f770e3a4d229fe35 Mon Sep 17 00:00:00 2001 From: mmtr Date: Wed, 16 Nov 2022 15:59:39 +0100 Subject: [PATCH 13/21] Fix linting issues --- lib/compat/wordpress-6.2/get-global-styles-and-settings.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) 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 430b32fb7289fc..c22db12a154423 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 @@ -70,8 +70,8 @@ function gutenberg_get_global_stylesheet( $types = array() ) { // Ignore cache when `WP_DEBUG` is enabled, so it doesn't interfere with the theme developers workflow. ( ! defined( 'WP_DEBUG' ) || ! WP_DEBUG ) ); - $cache_key = 'gutenberg_get_global_stylesheet'; - $cache_group = 'theme_json'; + $cache_key = 'gutenberg_get_global_stylesheet'; + $cache_group = 'theme_json'; if ( $can_use_cached ) { $cached = wp_cache_get( $cache_key, $cache_group ); if ( $cached ) { From 0a371d45b90fd3750044601471945902e69ee22a Mon Sep 17 00:00:00 2001 From: mmtr Date: Thu, 17 Nov 2022 10:53:09 +0100 Subject: [PATCH 14/21] Use WP version in since annotation --- lib/compat/wordpress-6.2/get-global-styles-and-settings.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 c22db12a154423..1a0bb3c60509cb 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 @@ -60,7 +60,7 @@ function gutenberg_get_global_stylesheet( $types = array() ) { /** * Filters whether the cached global stylesheet can be used. * - * @since 14.6.0 + * @since 6.2.0 * * @param boolean $can_use_cached Whether the cached global stylesheet can be used. */ From 18cd94f3b13715196cc9c96f174c4af702818277 Mon Sep 17 00:00:00 2001 From: mmtr Date: Thu, 17 Nov 2022 10:56:15 +0100 Subject: [PATCH 15/21] Check parent theme (props @felixarntz) --- lib/compat/wordpress-6.2/get-global-styles-and-settings.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) 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 1a0bb3c60509cb..58084e16b96649 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 @@ -155,7 +155,8 @@ function _gutenberg_get_global_stylesheet_clean_cache_upon_upgrading( $upgrader, if ( 'core' === $options['type'] || 'plugin' === $options['type'] || - ( 'theme' === $options['type'] && array_key_exists( get_stylesheet(), $options['themes'] ) ) + // Clean cache only if the active theme was updated. + ( 'theme' === $options['type'] && ( isset( $options['themes'][ get_stylesheet() ] ) || isset( $options['themes'][ get_template() ] ) ) ) ) { gutenberg_get_global_stylesheet_clean_cache(); } From 8e2ffb8948a90eeb68272e202da6a7c7c97135a6 Mon Sep 17 00:00:00 2001 From: mmtr Date: Thu, 17 Nov 2022 11:02:09 +0100 Subject: [PATCH 16/21] Invalidate WP_Theme_JSON_Resolver_Gutenberg cache after toggling a plugin --- lib/compat/wordpress-6.2/default-filters.php | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/lib/compat/wordpress-6.2/default-filters.php b/lib/compat/wordpress-6.2/default-filters.php index 62e7d1b1d2ca09..7a0fc32b0b88ce 100644 --- a/lib/compat/wordpress-6.2/default-filters.php +++ b/lib/compat/wordpress-6.2/default-filters.php @@ -25,7 +25,9 @@ */ add_action( 'switch_theme', 'wp_theme_clean_theme_json_cached_data' ); add_action( 'start_previewing_theme', 'wp_theme_clean_theme_json_cached_data' ); -add_action( 'save_post_wp_global_styles', array( 'WP_Theme_JSON_Resolver', 'clean_cached_data' ) ); +add_action( 'save_post_wp_global_styles', array( 'WP_Theme_JSON_Resolver_Gutenberg', 'clean_cached_data' ) ); +add_action( 'activated_plugin', array( 'WP_Theme_JSON_Resolver_Gutenberg', 'clean_cached_data' ) ); +add_action( 'deactivated_plugin', array( 'WP_Theme_JSON_Resolver_Gutenberg', 'clean_cached_data' ) ); add_action( 'save_post_wp_global_styles', 'gutenberg_get_global_stylesheet_clean_cache' ); add_action( 'switch_theme', 'gutenberg_get_global_stylesheet_clean_cache' ); add_action( 'start_previewing_theme', 'gutenberg_get_global_stylesheet_clean_cache' ); From 18f144346bc4bd2b2e4a3783a6a394a6d46d9fde Mon Sep 17 00:00:00 2001 From: mmtr Date: Thu, 17 Nov 2022 11:09:07 +0100 Subject: [PATCH 17/21] Invalidate WP_Theme_JSON_Resolver_Gutenberg cache after an upgrade --- .../class-wp-theme-json-resolver-6-2.php | 26 +++++++++++++++++++ lib/compat/wordpress-6.2/default-filters.php | 1 + .../get-global-styles-and-settings.php | 7 +++++ 3 files changed, 34 insertions(+) diff --git a/lib/compat/wordpress-6.2/class-wp-theme-json-resolver-6-2.php b/lib/compat/wordpress-6.2/class-wp-theme-json-resolver-6-2.php index e10710e0f4709f..856a78a834f0d0 100644 --- a/lib/compat/wordpress-6.2/class-wp-theme-json-resolver-6-2.php +++ b/lib/compat/wordpress-6.2/class-wp-theme-json-resolver-6-2.php @@ -32,4 +32,30 @@ public static function theme_has_support() { return wp_theme_has_theme_json(); } + /** + * Private method to clean the cached data after an upgrade. + * + * It is hooked into the `upgrader_process_complete` action. + * + * @since 6.2.0 + * @see default-filters.php + * + * @param WP_Upgrader $upgrader WP_Upgrader instance. + * @param array $options Array of bulk item update data. + */ + public static function _clean_cached_data_upon_upgrading( $upgrader, $options ) { + if ( 'update' !== $options['action'] ) { + return; + } + + if ( + 'core' === $options['type'] || + 'plugin' === $options['type'] || + // Clean cache only if the active theme was updated. + ( 'theme' === $options['type'] && ( isset( $options['themes'][ get_stylesheet() ] ) || isset( $options['themes'][ get_template() ] ) ) ) + ) { + static::clean_cached_data(); + } + } + } diff --git a/lib/compat/wordpress-6.2/default-filters.php b/lib/compat/wordpress-6.2/default-filters.php index 7a0fc32b0b88ce..ddbecd62f55853 100644 --- a/lib/compat/wordpress-6.2/default-filters.php +++ b/lib/compat/wordpress-6.2/default-filters.php @@ -28,6 +28,7 @@ add_action( 'save_post_wp_global_styles', array( 'WP_Theme_JSON_Resolver_Gutenberg', 'clean_cached_data' ) ); add_action( 'activated_plugin', array( 'WP_Theme_JSON_Resolver_Gutenberg', 'clean_cached_data' ) ); add_action( 'deactivated_plugin', array( 'WP_Theme_JSON_Resolver_Gutenberg', 'clean_cached_data' ) ); +add_action( 'upgrader_process_complete', array( 'WP_Theme_JSON_Resolver_Gutenberg', '_clean_cached_data_upon_upgrading' ) ); add_action( 'save_post_wp_global_styles', 'gutenberg_get_global_stylesheet_clean_cache' ); add_action( 'switch_theme', 'gutenberg_get_global_stylesheet_clean_cache' ); add_action( 'start_previewing_theme', 'gutenberg_get_global_stylesheet_clean_cache' ); 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 58084e16b96649..f8cd5f50c5bf28 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 @@ -136,6 +136,8 @@ function gutenberg_get_global_stylesheet( $types = array() ) { /** * Clean the cache used by the `gutenberg_get_global_stylesheet` function. + * + * @since 6.2.0 */ function gutenberg_get_global_stylesheet_clean_cache() { wp_cache_delete( 'gutenberg_get_global_stylesheet', 'theme_json' ); @@ -144,6 +146,11 @@ function gutenberg_get_global_stylesheet_clean_cache() { /** * Private function to clean the cache used by the `gutenberg_get_global_stylesheet` function after an upgrade. * + * It is hooked into the `upgrader_process_complete` action. + * + * @since 6.2.0 + * @see default-filters.php + * * @param WP_Upgrader $upgrader WP_Upgrader instance. * @param array $options Array of bulk item update data. */ From 4f1a1b8e8ec8a7e2b52588958f94504cdcd31cda Mon Sep 17 00:00:00 2001 From: mmtr Date: Thu, 17 Nov 2022 11:38:19 +0100 Subject: [PATCH 18/21] Remove @since 6.2.0 annotations as we don't know which WP version will include these changes --- .../wordpress-6.2/class-wp-theme-json-resolver-6-2.php | 1 - lib/compat/wordpress-6.2/get-global-styles-and-settings.php | 5 ----- 2 files changed, 6 deletions(-) diff --git a/lib/compat/wordpress-6.2/class-wp-theme-json-resolver-6-2.php b/lib/compat/wordpress-6.2/class-wp-theme-json-resolver-6-2.php index 856a78a834f0d0..7adb37ffbd9e01 100644 --- a/lib/compat/wordpress-6.2/class-wp-theme-json-resolver-6-2.php +++ b/lib/compat/wordpress-6.2/class-wp-theme-json-resolver-6-2.php @@ -37,7 +37,6 @@ public static function theme_has_support() { * * It is hooked into the `upgrader_process_complete` action. * - * @since 6.2.0 * @see default-filters.php * * @param WP_Upgrader $upgrader WP_Upgrader instance. 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 f8cd5f50c5bf28..b290b750d1993a 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 @@ -60,8 +60,6 @@ function gutenberg_get_global_stylesheet( $types = array() ) { /** * Filters whether the cached global stylesheet can be used. * - * @since 6.2.0 - * * @param boolean $can_use_cached Whether the cached global stylesheet can be used. */ $can_use_cached = apply_filters( @@ -136,8 +134,6 @@ function gutenberg_get_global_stylesheet( $types = array() ) { /** * Clean the cache used by the `gutenberg_get_global_stylesheet` function. - * - * @since 6.2.0 */ function gutenberg_get_global_stylesheet_clean_cache() { wp_cache_delete( 'gutenberg_get_global_stylesheet', 'theme_json' ); @@ -148,7 +144,6 @@ function gutenberg_get_global_stylesheet_clean_cache() { * * It is hooked into the `upgrader_process_complete` action. * - * @since 6.2.0 * @see default-filters.php * * @param WP_Upgrader $upgrader WP_Upgrader instance. From 88c1e33e61a20cb01dd0793605380f471e16553f Mon Sep 17 00:00:00 2001 From: Miguel Torres Date: Thu, 17 Nov 2022 14:53:23 +0100 Subject: [PATCH 19/21] Update phpunit/wp-get-global-stylesheet-test.php MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: André <583546+oandregal@users.noreply.github.com> --- phpunit/wp-get-global-stylesheet-test.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/phpunit/wp-get-global-stylesheet-test.php b/phpunit/wp-get-global-stylesheet-test.php index 676339a1b427af..cb4d0242ce3e3a 100644 --- a/phpunit/wp-get-global-stylesheet-test.php +++ b/phpunit/wp-get-global-stylesheet-test.php @@ -82,7 +82,7 @@ public function test_global_styles_user_cpt_change_invalidates_cached_stylesheet wp_set_current_user( self::$administrator_id ); $styles = gutenberg_get_global_stylesheet(); - $this->assertStringNotContainsString( 'hotpink', $styles ); + $this->assertStringNotContainsString( 'background-color: hotpink;', $styles ); $user_cpt = WP_Theme_JSON_Resolver_Gutenberg::get_user_data_from_wp_global_styles( wp_get_theme(), true ); $config = json_decode( $user_cpt['post_content'], true ); From 9dcdde71b135b01629dc754e554711123ababe1b Mon Sep 17 00:00:00 2001 From: mmtr Date: Fri, 18 Nov 2022 10:31:48 +0100 Subject: [PATCH 20/21] Add missing params to actions --- lib/compat/wordpress-6.2/default-filters.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/compat/wordpress-6.2/default-filters.php b/lib/compat/wordpress-6.2/default-filters.php index 8830ceb10f454e..999f8ba29146fb 100644 --- a/lib/compat/wordpress-6.2/default-filters.php +++ b/lib/compat/wordpress-6.2/default-filters.php @@ -23,10 +23,10 @@ add_action( 'save_post_wp_global_styles', array( 'WP_Theme_JSON_Resolver_Gutenberg', 'clean_cached_data' ) ); add_action( 'activated_plugin', array( 'WP_Theme_JSON_Resolver_Gutenberg', 'clean_cached_data' ) ); add_action( 'deactivated_plugin', array( 'WP_Theme_JSON_Resolver_Gutenberg', 'clean_cached_data' ) ); -add_action( 'upgrader_process_complete', array( 'WP_Theme_JSON_Resolver_Gutenberg', '_clean_cached_data_upon_upgrading' ) ); +add_action( 'upgrader_process_complete', array( 'WP_Theme_JSON_Resolver_Gutenberg', '_clean_cached_data_upon_upgrading', 10, 2 ) ); add_action( 'save_post_wp_global_styles', 'gutenberg_get_global_stylesheet_clean_cache' ); add_action( 'switch_theme', 'gutenberg_get_global_stylesheet_clean_cache' ); add_action( 'start_previewing_theme', 'gutenberg_get_global_stylesheet_clean_cache' ); add_action( 'activated_plugin', 'gutenberg_get_global_stylesheet_clean_cache' ); add_action( 'deactivated_plugin', 'gutenberg_get_global_stylesheet_clean_cache' ); -add_action( 'upgrader_process_complete', '_gutenberg_get_global_stylesheet_clean_cache_upon_upgrading' ); +add_action( 'upgrader_process_complete', '_gutenberg_get_global_stylesheet_clean_cache_upon_upgrading', 10, 2 ); From 2b163269b77617a597ce2b5e69c568f3742d3844 Mon Sep 17 00:00:00 2001 From: mmtr Date: Tue, 22 Nov 2022 16:20:28 +0100 Subject: [PATCH 21/21] Remove filter --- .../get-global-styles-and-settings.php | 17 ++++------------- 1 file changed, 4 insertions(+), 13 deletions(-) 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 79e5f956af9076..5d37d3182ccc85 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 @@ -91,19 +91,10 @@ function _wp_theme_has_theme_json_clean_cache_upon_upgrading_active_theme( $upgr * @return string Stylesheet. */ function gutenberg_get_global_stylesheet( $types = array() ) { - /** - * Filters whether the cached global stylesheet can be used. - * - * @param boolean $can_use_cached Whether the cached global stylesheet can be used. - */ - $can_use_cached = apply_filters( - 'wp_get_global_stylesheet_can_use_cache', - ( empty( $types ) ) && - // Ignore cache when `WP_DEBUG` is enabled, so it doesn't interfere with the theme developers workflow. - ( ! defined( 'WP_DEBUG' ) || ! WP_DEBUG ) - ); - $cache_key = 'gutenberg_get_global_stylesheet'; - $cache_group = 'theme_json'; + // Ignore cache when `WP_DEBUG` is enabled, so it doesn't interfere with the theme developers workflow. + $can_use_cached = empty( $types ) && ( ! defined( 'WP_DEBUG' ) || ! 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 ) {