Skip to content

Commit b1f3806

Browse files
committed
Editor: Remove the object cache from wp_get_global_settings().
The resolver now caches the merged data, so this cache saved one array read per call. Read through to the resolver on every call and drop the two cache keys from wp_clean_theme_json_cache().
1 parent 0af082b commit b1f3806

2 files changed

Lines changed: 63 additions & 44 deletions

File tree

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

Lines changed: 3 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
* Gets the settings resulting of merging core, theme, and user data.
1010
*
1111
* @since 5.9.0
12+
* @since 7.2.0 Removed the per-request cache; WP_Theme_JSON_Resolver::get_merged_data() caches the merged data.
1213
*
1314
* @param array $path Path to the specific setting to retrieve. Optional.
1415
* If empty, will return all settings.
@@ -32,53 +33,13 @@ function wp_get_global_settings( $path = array(), $context = array() ) {
3233
$path = $new_path;
3334
}
3435

35-
/*
36-
* This is the default value when no origin is provided or when it is 'all'.
37-
*
38-
* The $origin is used as part of the cache key. Changes here need to account
39-
* for clearing the cache appropriately.
40-
*/
36+
// This is the default value when no origin is provided or when it is 'all'.
4137
$origin = 'custom';
4238
if ( isset( $context['origin'] ) && 'base' === $context['origin'] ) {
4339
$origin = 'theme';
4440
}
4541

46-
/*
47-
* By using the 'theme_json' group, this data is marked to be non-persistent across requests.
48-
* See `wp_cache_add_non_persistent_groups` in src/wp-includes/load.php and other places.
49-
*
50-
* The rationale for this is to make sure derived data from theme.json
51-
* is always fresh from the potential modifications done via hooks
52-
* that can use dynamic data (modify the stylesheet depending on some option,
53-
* settings depending on user permissions, etc.).
54-
* See some of the existing hooks to modify theme.json behavior:
55-
* https://make.wordpress.org/core/2022/10/10/filters-for-theme-json-data/
56-
*
57-
* A different alternative considered was to invalidate the cache upon certain
58-
* events such as options add/update/delete, user meta, etc.
59-
* It was judged not enough, hence this approach.
60-
* See https://github.com/WordPress/gutenberg/pull/45372
61-
*/
62-
$cache_group = 'theme_json';
63-
$cache_key = 'wp_get_global_settings_' . $origin;
64-
65-
/*
66-
* Ignore cache when the development mode is set to 'theme', so it doesn't interfere with the theme
67-
* developer's workflow.
68-
*/
69-
$can_use_cached = ! wp_is_development_mode( 'theme' );
70-
71-
$settings = false;
72-
if ( $can_use_cached ) {
73-
$settings = wp_cache_get( $cache_key, $cache_group );
74-
}
75-
76-
if ( false === $settings ) {
77-
$settings = WP_Theme_JSON_Resolver::get_merged_data( $origin )->get_settings();
78-
if ( $can_use_cached ) {
79-
wp_cache_set( $cache_key, $settings, $cache_group );
80-
}
81-
}
42+
$settings = WP_Theme_JSON_Resolver::get_merged_data( $origin )->get_settings();
8243

8344
return _wp_array_get( $settings, $path, $settings );
8445
}
@@ -452,8 +413,6 @@ function wp_theme_has_theme_json() {
452413
function wp_clean_theme_json_cache() {
453414
wp_cache_delete( 'wp_get_global_stylesheet', 'theme_json' );
454415
wp_cache_delete( 'wp_get_global_styles_svg_filters', 'theme_json' );
455-
wp_cache_delete( 'wp_get_global_settings_custom', 'theme_json' );
456-
wp_cache_delete( 'wp_get_global_settings_theme', 'theme_json' );
457416
wp_cache_delete( 'wp_get_global_styles_custom_css', 'theme_json' );
458417
wp_cache_delete( 'wp_get_theme_data_template_parts', 'theme_json' );
459418
WP_Theme_JSON_Resolver::clean_cached_data();
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
<?php
2+
3+
/**
4+
* Tests wp_get_global_settings().
5+
*
6+
* @group themes
7+
*
8+
* @covers ::wp_get_global_settings
9+
*/
10+
class Tests_Theme_wpGetGlobalSettings extends WP_UnitTestCase {
11+
12+
public function tear_down() {
13+
wp_clean_theme_json_cache();
14+
parent::tear_down();
15+
}
16+
17+
public function test_reads_through_to_the_resolver() {
18+
$contexts = array(
19+
'wp_get_global_settings_custom' => array(),
20+
'wp_get_global_settings_theme' => array( 'origin' => 'base' ),
21+
);
22+
23+
foreach ( $contexts as $context ) {
24+
wp_get_global_settings( array(), $context );
25+
}
26+
27+
foreach ( array_keys( $contexts ) as $cache_key ) {
28+
$this->assertFalse(
29+
wp_cache_get( $cache_key, 'theme_json' ),
30+
"The merged settings should not be cached under $cache_key."
31+
);
32+
}
33+
34+
// Block registration adds no settings, so inject one through the theme data
35+
// filter: registering a block refreshes the theme data, which reapplies the filter.
36+
$filter = static function ( $theme_json ) {
37+
return $theme_json->update_with(
38+
array(
39+
'version' => WP_Theme_JSON::LATEST_SCHEMA,
40+
'settings' => array(
41+
'custom' => array( 'cacheProbe' => 'fresh' ),
42+
),
43+
)
44+
);
45+
};
46+
add_filter( 'wp_theme_json_data_theme', $filter );
47+
register_block_type( 'test/block-settings' );
48+
49+
$settings = wp_get_global_settings();
50+
51+
unregister_block_type( 'test/block-settings' );
52+
remove_filter( 'wp_theme_json_data_theme', $filter );
53+
54+
$this->assertSame(
55+
'fresh',
56+
$settings['custom']['cacheProbe'] ?? null,
57+
'Settings changed after a block registration should be present on the next call.'
58+
);
59+
}
60+
}

0 commit comments

Comments
 (0)