Skip to content

Commit

Permalink
Make theme_json as non-persistent at load
Browse files Browse the repository at this point in the history
  • Loading branch information
oandregal committed Dec 13, 2022
1 parent ff7fd1e commit 45969f1
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 30 deletions.
1 change: 0 additions & 1 deletion src/wp-includes/default-filters.php
Original file line number Diff line number Diff line change
Expand Up @@ -352,7 +352,6 @@
add_action( 'after_switch_theme', '_wp_sidebars_changed' );
add_action( 'wp_print_styles', 'print_emoji_styles' );
add_action( 'plugins_loaded', '_wp_theme_json_webfonts_handler' );
add_action( 'plugins_loaded', '_wp_add_non_persistent_theme_json_cache_group' );

if ( isset( $_GET['replytocom'] ) ) {
add_filter( 'wp_robots', 'wp_robots_no_robots' );
Expand Down
45 changes: 17 additions & 28 deletions src/wp-includes/global-styles-and-settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -85,8 +85,24 @@ function wp_get_global_styles( $path = array(), $context = array() ) {
function wp_get_global_stylesheet( $types = array() ) {
// Ignore cache when `WP_DEBUG` is enabled, so it doesn't interfere with the theme developers workflow.
$can_use_cached = empty( $types ) && ! WP_DEBUG;
$cache_key = 'wp_get_global_stylesheet';
/**
* By using the 'theme_json' group, this data is marked to be non-persistent across requests.
* See `wp_cache_add_non_persistent_groups` in src/wp-includes/load.php
*
* 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 behaviour:
* 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 = wp_cache_get( $cache_key, $cache_group );
if ( $cached ) {
Expand Down Expand Up @@ -253,30 +269,3 @@ function _wp_clean_theme_json_caches() {
wp_cache_delete( 'wp_get_global_stylesheet', 'theme_json' );
WP_Theme_JSON_Resolver::clean_cached_data();
}

/**
* Tell the cache mechanisms not to persist theme.json data across requests.
* The data stored under this cache group:
*
* - wp_get_global_stylesheet
*
* There is some hooks consumers can use to modify parts
* of the theme.json logic.
* See https://make.wordpress.org/core/2022/10/10/filters-for-theme-json-data/
*
* The rationale to make this cache group non persistent 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,
* or settings depending on user permissions, etc.).
*
* 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
*
* @since 6.1.2
* @access private
*/
function _wp_add_non_persistent_theme_json_cache_group() {
wp_cache_add_non_persistent_groups( 'theme_json' );
}
2 changes: 1 addition & 1 deletion src/wp-includes/load.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit 45969f1

Please sign in to comment.