Make WordPress Core


Ignore:
Timestamp:
01/26/2023 11:01:10 PM (4 years ago)
Author:
flixos90
Message:

Editor: Use a non-persistent object cache instead of transient in wp_get_global_stylesheet().

This changeset is part of a greater effort to enhance the caching strategy for theme.json based data. Similar to [55138], the cache is currently ignored when WP_DEBUG is on to avoid interrupting the theme developer's workflow.

Props oandregal, spacedmonkey, hellofromtonya, flixos90, ironprogrammer, azaozz, aristath, costdev, mcsf.
Fixes #56910.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/global-styles-and-settings.php

    r55144 r55148  
    8686 */
    8787function 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';
    98114        if ( $can_use_cached ) {
    99                 $cached = get_transient( $transient_name );
     115                $cached = wp_cache_get( $cache_key, $cache_group );
    100116                if ( $cached ) {
    101117                        return $cached;
    … …  
    153169
    154170        $stylesheet = $styles_variables . $styles_rest;
    155 
    156171        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 );
    160173        }
    161174
    … …  
    304317 */
    305318function wp_clean_theme_json_cache() {
     319        wp_cache_delete( 'wp_get_global_stylesheet', 'theme_json' );
    306320        WP_Theme_JSON_Resolver::clean_cached_data();
    307321}
Note: See TracChangeset for help on using the changeset viewer.