From 24bcc488b045058fdef4d2dac80576f857d89391 Mon Sep 17 00:00:00 2001 From: Felix Arntz Date: Thu, 26 Jan 2023 23:03:14 +0000 Subject: [PATCH] Editor: Use a non-persistent object cache instead of transient in `wp_get_global_stylesheet()`. This changeset is part of a greater effort to enhance the caching strategy for `theme.json` based data. Similar to [55138], the cache is currently ignored when `WP_DEBUG` is on to avoid interrupting the theme developer's workflow. Props oandregal, spacedmonkey, hellofromtonya, flixos90, ironprogrammer, azaozz, aristath, costdev, mcsf. Fixes #56910. Built from https://develop.svn.wordpress.org/trunk@55148 git-svn-id: http://core.svn.wordpress.org/trunk@54681 1a063a9b-81f0-0310-95a4-ce76da25c4cd --- wp-includes/global-styles-and-settings.php | 44 ++++++++++++++-------- wp-includes/load.php | 2 +- wp-includes/ms-blogs.php | 4 +- wp-includes/version.php | 2 +- 4 files changed, 33 insertions(+), 19 deletions(-) diff --git a/wp-includes/global-styles-and-settings.php b/wp-includes/global-styles-and-settings.php index ac73c889e5..a814b5fe3b 100644 --- a/wp-includes/global-styles-and-settings.php +++ b/wp-includes/global-styles-and-settings.php @@ -85,18 +85,34 @@ function wp_get_global_styles( $path = array(), $context = array() ) { * @return string Stylesheet. */ function wp_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 = 'global_styles_' . get_stylesheet(); + /* + * Ignore cache when `WP_DEBUG` is enabled, so it doesn't interfere with the theme + * developer's workflow. + * + * @todo Replace `WP_DEBUG` once an "in development mode" check is available in Core. + */ + $can_use_cached = empty( $types ) && ! WP_DEBUG; + + /* + * 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_group = 'theme_json'; + $cache_key = 'wp_get_global_stylesheet'; if ( $can_use_cached ) { - $cached = get_transient( $transient_name ); + $cached = wp_cache_get( $cache_key, $cache_group ); if ( $cached ) { return $cached; } @@ -152,11 +168,8 @@ function wp_get_global_stylesheet( $types = array() ) { } $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 ); + wp_cache_set( $cache_key, $stylesheet, $cache_group ); } return $stylesheet; @@ -303,5 +316,6 @@ function wp_theme_has_theme_json() { * @since 6.2.0 */ function wp_clean_theme_json_cache() { + wp_cache_delete( 'wp_get_global_stylesheet', 'theme_json' ); WP_Theme_JSON_Resolver::clean_cached_data(); } diff --git a/wp-includes/load.php b/wp-includes/load.php index 4c212086df..c2450e582f 100644 --- a/wp-includes/load.php +++ b/wp-includes/load.php @@ -753,7 +753,7 @@ function wp_start_object_cache() { ) ); - wp_cache_add_non_persistent_groups( array( 'counts', 'plugins' ) ); + wp_cache_add_non_persistent_groups( array( 'counts', 'plugins', 'theme_json' ) ); } $first_init = false; diff --git a/wp-includes/ms-blogs.php b/wp-includes/ms-blogs.php index c5a988693c..9f2916d7f9 100644 --- a/wp-includes/ms-blogs.php +++ b/wp-includes/ms-blogs.php @@ -575,7 +575,7 @@ function switch_to_blog( $new_blog_id, $deprecated = null ) { ); } - wp_cache_add_non_persistent_groups( array( 'counts', 'plugins' ) ); + wp_cache_add_non_persistent_groups( array( 'counts', 'plugins', 'theme_json' ) ); } } @@ -666,7 +666,7 @@ function restore_current_blog() { ); } - wp_cache_add_non_persistent_groups( array( 'counts', 'plugins' ) ); + wp_cache_add_non_persistent_groups( array( 'counts', 'plugins', 'theme_json' ) ); } } diff --git a/wp-includes/version.php b/wp-includes/version.php index 5857623b05..38a8e8528e 100644 --- a/wp-includes/version.php +++ b/wp-includes/version.php @@ -16,7 +16,7 @@ * * @global string $wp_version */ -$wp_version = '6.2-alpha-55147'; +$wp_version = '6.2-alpha-55148'; /** * Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.