Skip to content

Commit 45969f1

Browse files
committed
Make theme_json as non-persistent at load
1 parent ff7fd1e commit 45969f1

3 files changed

Lines changed: 18 additions & 30 deletions

File tree

src/wp-includes/default-filters.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -352,7 +352,6 @@
352352
add_action( 'after_switch_theme', '_wp_sidebars_changed' );
353353
add_action( 'wp_print_styles', 'print_emoji_styles' );
354354
add_action( 'plugins_loaded', '_wp_theme_json_webfonts_handler' );
355-
add_action( 'plugins_loaded', '_wp_add_non_persistent_theme_json_cache_group' );
356355

357356
if ( isset( $_GET['replytocom'] ) ) {
358357
add_filter( 'wp_robots', 'wp_robots_no_robots' );

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

Lines changed: 17 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -85,8 +85,24 @@ function wp_get_global_styles( $path = array(), $context = array() ) {
8585
function wp_get_global_stylesheet( $types = array() ) {
8686
// Ignore cache when `WP_DEBUG` is enabled, so it doesn't interfere with the theme developers workflow.
8787
$can_use_cached = empty( $types ) && ! WP_DEBUG;
88-
$cache_key = 'wp_get_global_stylesheet';
88+
/**
89+
* By using the 'theme_json' group, this data is marked to be non-persistent across requests.
90+
* See `wp_cache_add_non_persistent_groups` in src/wp-includes/load.php
91+
*
92+
* The rationale for this is to make sure derived data from theme.json
93+
* is always fresh from the potential modifications done via hooks
94+
* that can use dynamic data (modify the stylesheet depending on some option,
95+
* settings depending on user permissions, etc.).
96+
* See some of the existing hooks to modify theme.json behaviour:
97+
* https://make.wordpress.org/core/2022/10/10/filters-for-theme-json-data/
98+
*
99+
* A different alternative considered was to invalidate the cache upon certain
100+
* events such as options add/update/delete, user meta, etc.
101+
* It was judged not enough, hence this approach.
102+
* See https://github.com/WordPress/gutenberg/pull/45372
103+
*/
89104
$cache_group = 'theme_json';
105+
$cache_key = 'wp_get_global_stylesheet';
90106
if ( $can_use_cached ) {
91107
$cached = wp_cache_get( $cache_key, $cache_group );
92108
if ( $cached ) {
@@ -253,30 +269,3 @@ function _wp_clean_theme_json_caches() {
253269
wp_cache_delete( 'wp_get_global_stylesheet', 'theme_json' );
254270
WP_Theme_JSON_Resolver::clean_cached_data();
255271
}
256-
257-
/**
258-
* Tell the cache mechanisms not to persist theme.json data across requests.
259-
* The data stored under this cache group:
260-
*
261-
* - wp_get_global_stylesheet
262-
*
263-
* There is some hooks consumers can use to modify parts
264-
* of the theme.json logic.
265-
* See https://make.wordpress.org/core/2022/10/10/filters-for-theme-json-data/
266-
*
267-
* The rationale to make this cache group non persistent is to make sure derived data
268-
* from theme.json is always fresh from the potential modifications done via hooks
269-
* that can use dynamic data (modify the stylesheet depending on some option,
270-
* or settings depending on user permissions, etc.).
271-
*
272-
* A different alternative considered was to invalidate the cache upon certain
273-
* events such as options add/update/delete, user meta, etc.
274-
* It was judged not enough, hence this approach.
275-
* See https://github.com/WordPress/gutenberg/pull/45372
276-
*
277-
* @since 6.1.2
278-
* @access private
279-
*/
280-
function _wp_add_non_persistent_theme_json_cache_group() {
281-
wp_cache_add_non_persistent_groups( 'theme_json' );
282-
}

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;

0 commit comments

Comments
 (0)