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
This commit is contained in:
spacedmonkey 2023-07-10 19:17:22 +00:00
parent fe75c1b12f
commit 7174e4e871
2 changed files with 9 additions and 12 deletions

View File

@ -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. * @return bool Returns true if theme or its parent has a theme.json file, false otherwise.
*/ */
function wp_theme_has_theme_json() { function wp_theme_has_theme_json() {
static $theme_has_support = null; static $theme_has_support = array();
$stylesheet = get_stylesheet();
if ( 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 * Ignore static cache when the development mode is set to 'theme', to avoid interfering with
* the theme developer's workflow. * the theme developer's workflow.
*/ */
wp_get_development_mode() !== 'theme' && 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 )
) { ) {
return $theme_has_support; return $theme_has_support[ $stylesheet ];
} }
$stylesheet_directory = get_stylesheet_directory(); $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 */ /** This filter is documented in wp-includes/link-template.php */
$path = apply_filters( 'theme_file_path', $path, 'theme.json' ); $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 ];
} }
/** /**

View File

@ -16,7 +16,7 @@
* *
* @global string $wp_version * @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. * Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.