Skip to content

Commit dd33e32

Browse files
committed
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. git-svn-id: https://develop.svn.wordpress.org/trunk@55148 602fd350-edb4-49c9-b593-d223f7449a82
1 parent e2b5177 commit dd33e32

File tree

5 files changed

+47
-19
lines changed

5 files changed

+47
-19
lines changed

src/wp-includes/global-styles-and-settings.php

Lines changed: 29 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -85,18 +85,34 @@ function wp_get_global_styles( $path = array(), $context = array() ) {
8585
* @return string Stylesheet.
8686
*/
8787
function wp_get_global_stylesheet( $types = array() ) {
88-
// Return cached value if it can be used and exists.
89-
// It's cached by theme to make sure that theme switching clears the cache.
90-
$can_use_cached = (
91-
( empty( $types ) ) &&
92-
( ! defined( 'WP_DEBUG' ) || ! WP_DEBUG ) &&
93-
( ! defined( 'SCRIPT_DEBUG' ) || ! SCRIPT_DEBUG ) &&
94-
( ! defined( 'REST_REQUEST' ) || ! REST_REQUEST ) &&
95-
! is_admin()
96-
);
97-
$transient_name = 'global_styles_' . get_stylesheet();
88+
/*
89+
* Ignore cache when `WP_DEBUG` is enabled, so it doesn't interfere with the theme
90+
* developer's workflow.
91+
*
92+
* @todo Replace `WP_DEBUG` once an "in development mode" check is available in Core.
93+
*/
94+
$can_use_cached = empty( $types ) && ! WP_DEBUG;
95+
96+
/*
97+
* By using the 'theme_json' group, this data is marked to be non-persistent across requests.
98+
* @see `wp_cache_add_non_persistent_groups()`.
99+
*
100+
* The rationale for this is to make sure derived data from theme.json
101+
* is always fresh from the potential modifications done via hooks
102+
* that can use dynamic data (modify the stylesheet depending on some option,
103+
* settings depending on user permissions, etc.).
104+
* See some of the existing hooks to modify theme.json behavior:
105+
* @see https://make.wordpress.org/core/2022/10/10/filters-for-theme-json-data/
106+
*
107+
* A different alternative considered was to invalidate the cache upon certain
108+
* events such as options add/update/delete, user meta, etc.
109+
* It was judged not enough, hence this approach.
110+
* @see https://github.com/WordPress/gutenberg/pull/45372
111+
*/
112+
$cache_group = 'theme_json';
113+
$cache_key = 'wp_get_global_stylesheet';
98114
if ( $can_use_cached ) {
99-
$cached = get_transient( $transient_name );
115+
$cached = wp_cache_get( $cache_key, $cache_group );
100116
if ( $cached ) {
101117
return $cached;
102118
}
@@ -152,11 +168,8 @@ function wp_get_global_stylesheet( $types = array() ) {
152168
}
153169

154170
$stylesheet = $styles_variables . $styles_rest;
155-
156171
if ( $can_use_cached ) {
157-
// Cache for a minute.
158-
// This cache doesn't need to be any longer, we only want to avoid spikes on high-traffic sites.
159-
set_transient( $transient_name, $stylesheet, MINUTE_IN_SECONDS );
172+
wp_cache_set( $cache_key, $stylesheet, $cache_group );
160173
}
161174

162175
return $stylesheet;
@@ -303,5 +316,6 @@ function wp_theme_has_theme_json() {
303316
* @since 6.2.0
304317
*/
305318
function wp_clean_theme_json_cache() {
319+
wp_cache_delete( 'wp_get_global_stylesheet', 'theme_json' );
306320
WP_Theme_JSON_Resolver::clean_cached_data();
307321
}

src/wp-includes/load.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -753,7 +753,7 @@ function wp_start_object_cache() {
753753
)
754754
);
755755

756-
wp_cache_add_non_persistent_groups( array( 'counts', 'plugins' ) );
756+
wp_cache_add_non_persistent_groups( array( 'counts', 'plugins', 'theme_json' ) );
757757
}
758758

759759
$first_init = false;

src/wp-includes/ms-blogs.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -575,7 +575,7 @@ function switch_to_blog( $new_blog_id, $deprecated = null ) {
575575
);
576576
}
577577

578-
wp_cache_add_non_persistent_groups( array( 'counts', 'plugins' ) );
578+
wp_cache_add_non_persistent_groups( array( 'counts', 'plugins', 'theme_json' ) );
579579
}
580580
}
581581

@@ -666,7 +666,7 @@ function restore_current_blog() {
666666
);
667667
}
668668

669-
wp_cache_add_non_persistent_groups( array( 'counts', 'plugins' ) );
669+
wp_cache_add_non_persistent_groups( array( 'counts', 'plugins', 'theme_json' ) );
670670
}
671671
}
672672

tests/phpunit/includes/abstract-testcase.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -401,7 +401,7 @@ public static function flush_cache() {
401401
)
402402
);
403403

404-
wp_cache_add_non_persistent_groups( array( 'counts', 'plugins' ) );
404+
wp_cache_add_non_persistent_groups( array( 'counts', 'plugins', 'theme_json' ) );
405405
}
406406

407407
/**

tests/phpunit/tests/theme/wpGetGlobalStylesheet.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -270,4 +270,18 @@ public function test_should_enqueue_stored_styles() {
270270
'Registered styles with handle of "wp-style-engine-my-styles" do not match expected value from the Style Engine store.'
271271
);
272272
}
273+
/**
274+
* Tests that switching themes recalculates the stylesheet.
275+
*
276+
* @ticket 56970
277+
*/
278+
public function test_switching_themes_should_recalculate_stylesheet() {
279+
$stylesheet_for_default_theme = wp_get_global_stylesheet();
280+
switch_theme( 'block-theme' );
281+
$stylesheet_for_block_theme = wp_get_global_stylesheet();
282+
switch_theme( WP_DEFAULT_THEME );
283+
284+
$this->assertStringNotContainsString( '--wp--preset--font-size--custom: 100px;', $stylesheet_for_default_theme, 'custom font size (100px) not present for default theme' );
285+
$this->assertStringContainsString( '--wp--preset--font-size--custom: 100px;', $stylesheet_for_block_theme, 'custom font size (100px) is present for block theme' );
286+
}
273287
}

0 commit comments

Comments
 (0)