From a92f7bf8cb52b4d00554b5888d39b6a6440f7824 Mon Sep 17 00:00:00 2001 From: Jonny Harris Date: Mon, 10 Jul 2023 19:15:36 +0000 Subject: [PATCH] Themes: Improved caching in `wp_theme_has_theme_json()`. Improve logic in `wp_theme_has_theme_json()` to improve caching soo it takes into account that a theme can be switched. Before this function would overly cache if the current theme has a theme.json file. Now it use the value of `get_stylesheet()` as a key to an array. This way, if the value of stylesheet is changed, by either a filter or in a unit test, a new check to see if the file exists is run. For this reason, the workaround to check if unit tests are running by checking if `WP_RUN_CORE_TESTS` constant exists can be removed. Props westonruter, spacedmonkey, flixos90. Fixes #58758. git-svn-id: https://develop.svn.wordpress.org/trunk@56185 602fd350-edb4-49c9-b593-d223f7449a82 --- .../global-styles-and-settings.php | 19 ++++++++----------- 1 file changed, 8 insertions(+), 11 deletions(-) diff --git a/src/wp-includes/global-styles-and-settings.php b/src/wp-includes/global-styles-and-settings.php index c352f0d7c6..d674434313 100644 --- a/src/wp-includes/global-styles-and-settings.php +++ b/src/wp-includes/global-styles-and-settings.php @@ -350,22 +350,19 @@ function wp_add_global_styles_for_blocks() { * @return bool Returns true if theme or its parent has a theme.json file, false otherwise. */ function wp_theme_has_theme_json() { - static $theme_has_support = null; + static $theme_has_support = array(); + + $stylesheet = get_stylesheet(); if ( - null !== $theme_has_support && + isset( $theme_has_support[ $stylesheet ] ) && /* * Ignore static cache when the development mode is set to 'theme', to avoid interfering with * the theme developer's workflow. */ - wp_get_development_mode() !== 'theme' && - /* - * Ignore cache when automated test suites are running. Why? To ensure - * the static cache is reset between each test. - */ - ! ( defined( 'WP_RUN_CORE_TESTS' ) && WP_RUN_CORE_TESTS ) + wp_get_development_mode() !== 'theme' ) { - return $theme_has_support; + return $theme_has_support[ $stylesheet ]; } $stylesheet_directory = get_stylesheet_directory(); @@ -381,9 +378,9 @@ function wp_theme_has_theme_json() { /** This filter is documented in wp-includes/link-template.php */ $path = apply_filters( 'theme_file_path', $path, 'theme.json' ); - $theme_has_support = file_exists( $path ); + $theme_has_support[ $stylesheet ] = file_exists( $path ); - return $theme_has_support; + return $theme_has_support[ $stylesheet ]; } /**