Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve caching in wp_get_global_styles_svg_filters #3926

Closed
wants to merge 7 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 12 additions & 12 deletions src/wp-includes/global-styles-and-settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -233,17 +233,17 @@ function wp_get_global_stylesheet( $types = array() ) {
* @return string
*/
function wp_get_global_styles_svg_filters() {
// Return cached value if it can be used and exists.
// It's cached by theme to make sure that theme switching clears the cache.
$can_use_cached = (
( ! defined( 'WP_DEBUG' ) || ! WP_DEBUG ) &&
( ! defined( 'SCRIPT_DEBUG' ) || ! SCRIPT_DEBUG ) &&
( ! defined( 'REST_REQUEST' ) || ! REST_REQUEST ) &&
! is_admin()
);
$transient_name = 'global_styles_svg_filters_' . get_stylesheet();
/*
* Ignore cache when `WP_DEBUG` is enabled, so it doesn't interfere with the theme
* developer's workflow.
*
* @todo Replace `WP_DEBUG` once an "in development mode" check is available in Core.
*/
$can_use_cached = ! WP_DEBUG;
$cache_group = 'theme_json';
$cache_key = 'wp_get_global_styles_svg_filters';
if ( $can_use_cached ) {
$cached = get_transient( $transient_name );
$cached = wp_cache_get( $cache_key, $cache_group );
if ( $cached ) {
return $cached;
}
Expand All @@ -260,8 +260,7 @@ function wp_get_global_styles_svg_filters() {
$svgs = $tree->get_svg_filters( $origins );

if ( $can_use_cached ) {
// Cache for a minute, same as wp_get_global_stylesheet.
set_transient( $transient_name, $svgs, MINUTE_IN_SECONDS );
wp_cache_set( $cache_key, $svgs, $cache_group );
}

return $svgs;
Expand Down Expand Up @@ -367,6 +366,7 @@ function wp_theme_has_theme_json() {
*/
function wp_clean_theme_json_cache() {
wp_cache_delete( 'wp_get_global_stylesheet', 'theme_json' );
wp_cache_delete( 'wp_get_global_styles_svg_filters', 'theme_json' );
spacedmonkey marked this conversation as resolved.
Show resolved Hide resolved
wp_cache_delete( 'wp_get_global_settings_custom', 'theme_json' );
wp_cache_delete( 'wp_get_global_settings_theme', 'theme_json' );
WP_Theme_JSON_Resolver::clean_cached_data();
Expand Down
37 changes: 37 additions & 0 deletions tests/phpunit/tests/theme/wpGetGlobalStylesSvgFilters.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php
/**
* Tests wp_get_global_styles_svg_filters().
*
* @group themes
*/
class Tests_Theme_wpGetGlobalStylesSvgFilters extends WP_UnitTestCase {
public function set_up() {
parent::set_up();
// Clear caches.
wp_clean_themes_cache();
}

public function tear_down() {
wp_clean_themes_cache();

parent::tear_down();
}

/**
* Tests that switching themes recalculates the svgs.
*
* @covers ::wp_get_global_styles_svg_filters
*
* @ticket 57568
*/
public function test_switching_themes_should_recalculate_svg() {
$svg_for_default_theme = wp_get_global_styles_svg_filters();
switch_theme( 'block-theme' );
$svg_for_block_theme = wp_get_global_styles_svg_filters();
switch_theme( WP_DEFAULT_THEME );

$this->assertStringContainsString( '<svg', $svg_for_default_theme, 'Default theme should contain SVG' );
$this->assertStringContainsString( '<svg', $svg_for_default_theme, 'Block theme should contain SVG' );
$this->assertNotSame( $svg_for_default_theme, $svg_for_block_theme, 'Cache value should have changed' );
}
}