Changeset 55148
- Timestamp:
- 01/26/2023 11:01:10 PM (21 months ago)
- Location:
- trunk
- Files:
-
- 5 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/wp-includes/global-styles-and-settings.php
r55144 r55148 86 86 */ 87 87 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'; 98 114 if ( $can_use_cached ) { 99 $cached = get_transient( $transient_name);115 $cached = wp_cache_get( $cache_key, $cache_group ); 100 116 if ( $cached ) { 101 117 return $cached; … … 153 169 154 170 $stylesheet = $styles_variables . $styles_rest; 155 156 171 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 ); 160 173 } 161 174 … … 304 317 */ 305 318 function wp_clean_theme_json_cache() { 319 wp_cache_delete( 'wp_get_global_stylesheet', 'theme_json' ); 306 320 WP_Theme_JSON_Resolver::clean_cached_data(); 307 321 } -
trunk/src/wp-includes/load.php
r55092 r55148 754 754 ); 755 755 756 wp_cache_add_non_persistent_groups( array( 'counts', 'plugins' ) );756 wp_cache_add_non_persistent_groups( array( 'counts', 'plugins', 'theme_json' ) ); 757 757 } 758 758 -
trunk/src/wp-includes/ms-blogs.php
r55123 r55148 576 576 } 577 577 578 wp_cache_add_non_persistent_groups( array( 'counts', 'plugins' ) );578 wp_cache_add_non_persistent_groups( array( 'counts', 'plugins', 'theme_json' ) ); 579 579 } 580 580 } … … 667 667 } 668 668 669 wp_cache_add_non_persistent_groups( array( 'counts', 'plugins' ) );669 wp_cache_add_non_persistent_groups( array( 'counts', 'plugins', 'theme_json' ) ); 670 670 } 671 671 } -
trunk/tests/phpunit/includes/abstract-testcase.php
r55092 r55148 402 402 ); 403 403 404 wp_cache_add_non_persistent_groups( array( 'counts', 'plugins' ) );404 wp_cache_add_non_persistent_groups( array( 'counts', 'plugins', 'theme_json' ) ); 405 405 } 406 406 -
trunk/tests/phpunit/tests/theme/wpGetGlobalStylesheet.php
r54214 r55148 271 271 ); 272 272 } 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 } 273 287 }
Note: See TracChangeset
for help on using the changeset viewer.