From 7174e4e871bd804332eb0bb8be8b8edc941df23c Mon Sep 17 00:00:00 2001 From: spacedmonkey Date: Mon, 10 Jul 2023 19:17:22 +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. Built from https://develop.svn.wordpress.org/trunk@56185 git-svn-id: http://core.svn.wordpress.org/trunk@55697 1a063a9b-81f0-0310-95a4-ce76da25c4cd --- wp-includes/global-styles-and-settings.php | 19 ++++++++----------- wp-includes/version.php | 2 +- 2 files changed, 9 insertions(+), 12 deletions(-) diff --git a/wp-includes/global-styles-and-settings.php b/wp-includes/global-styles-and-settings.php index c352f0d7c6..d674434313 100644 --- a/wp-includes/global-styles-and-settings.php +++ b/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 ]; } /** diff --git a/wp-includes/version.php b/wp-includes/version.php index 3811829d4f..742b09af5b 100644 --- a/wp-includes/version.php +++ b/wp-includes/version.php @@ -16,7 +16,7 @@ * * @global string $wp_version */ -$wp_version = '6.3-beta3-56184'; +$wp_version = '6.3-beta3-56185'; /** * Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.