Skip to content

Commit 073766e

Browse files
committed
Port cache group management from PR 3712
1 parent 2726616 commit 073766e

5 files changed

Lines changed: 33 additions & 6 deletions

File tree

src/wp-includes/default-filters.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -346,8 +346,8 @@
346346
add_action( 'init', '_register_core_block_patterns_and_categories' );
347347
add_action( 'init', 'check_theme_switched', 99 );
348348
add_action( 'init', array( 'WP_Block_Supports', 'init' ), 22 );
349-
add_action( 'switch_theme', array( 'WP_Theme_JSON_Resolver', 'clean_cached_data' ) );
350-
add_action( 'start_previewing_theme', array( 'WP_Theme_JSON_Resolver', 'clean_cached_data' ) );
349+
add_action( 'switch_theme', '_wp_clean_theme_json_caches' );
350+
add_action( 'start_previewing_theme', '_wp_clean_theme_json_caches' );
351351
add_action( 'after_switch_theme', '_wp_menus_changed' );
352352
add_action( 'after_switch_theme', '_wp_sidebars_changed' );
353353
add_action( 'wp_print_styles', 'print_emoji_styles' );

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

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -264,6 +264,22 @@ function ( $item ) {
264264
* @return boolean
265265
*/
266266
function wp_theme_has_theme_json() {
267+
/**
268+
* By using the 'theme_json' group, this data is marked to be non-persistent across requests.
269+
* See `wp_cache_add_non_persistent_groups` in src/wp-includes/load.php and other places.
270+
*
271+
* The rationale for this is to make sure derived data from theme.json
272+
* is always fresh from the potential modifications done via hooks
273+
* that can use dynamic data (modify the stylesheet depending on some option,
274+
* settings depending on user permissions, etc.).
275+
* See some of the existing hooks to modify theme.json behaviour:
276+
* https://make.wordpress.org/core/2022/10/10/filters-for-theme-json-data/
277+
*
278+
* A different alternative considered was to invalidate the cache upon certain
279+
* events such as options add/update/delete, user meta, etc.
280+
* It was judged not enough, hence this approach.
281+
* See https://github.com/WordPress/gutenberg/pull/45372
282+
*/
267283
$cache_group = 'theme_json';
268284
$cache_key = 'wp_theme_has_theme_json';
269285
$theme_has_support = wp_cache_get( $cache_key, $cache_group );
@@ -295,3 +311,14 @@ function wp_theme_has_theme_json() {
295311

296312
return (bool) $theme_has_support;
297313
}
314+
315+
/**
316+
* Private function to clean the caches under the theme_json group.
317+
*
318+
* @since 6.1.2
319+
* @access private
320+
*/
321+
function _wp_clean_theme_json_caches() {
322+
wp_cache_delete( 'wp_theme_has_theme_json', 'theme_json' );
323+
WP_Theme_JSON_Resolver::clean_cached_data();
324+
}

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
/**

0 commit comments

Comments
 (0)