Make WordPress Core

Changeset 55148


Ignore:
Timestamp:
01/26/2023 11:01:10 PM (13 months 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.

Location:
trunk
Files:
5 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}
  • trunk/src/wp-includes/load.php

    r55092 r55148  
    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
  • trunk/src/wp-includes/ms-blogs.php

    r55123 r55148  
    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    }
     
    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    }
  • trunk/tests/phpunit/includes/abstract-testcase.php

    r55092 r55148  
    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
  • trunk/tests/phpunit/tests/theme/wpGetGlobalStylesheet.php

    r54214 r55148  
    271271        );
    272272    }
     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    }
    273287}
Note: See TracChangeset for help on using the changeset viewer.