Make WordPress Core

Changeset 56185


Ignore:
Timestamp:
07/10/2023 07:15:36 PM (11 months ago)
Author:
spacedmonkey
Message:

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.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/global-styles-and-settings.php

    r56101 r56185  
    351351 */
    352352function wp_theme_has_theme_json() {
    353     static $theme_has_support = null;
     353    static $theme_has_support = array();
     354
     355    $stylesheet = get_stylesheet();
    354356
    355357    if (
    356         null !== $theme_has_support &&
     358        isset( $theme_has_support[ $stylesheet ] ) &&
    357359        /*
    358360         * Ignore static cache when the development mode is set to 'theme', to avoid interfering with
    359361         * the theme developer's workflow.
    360362         */
    361         wp_get_development_mode() !== 'theme' &&
    362         /*
    363          * Ignore cache when automated test suites are running. Why? To ensure
    364          * the static cache is reset between each test.
    365          */
    366         ! ( defined( 'WP_RUN_CORE_TESTS' ) && WP_RUN_CORE_TESTS )
     363        wp_get_development_mode() !== 'theme'
    367364    ) {
    368         return $theme_has_support;
     365        return $theme_has_support[ $stylesheet ];
    369366    }
    370367
     
    382379    $path = apply_filters( 'theme_file_path', $path, 'theme.json' );
    383380
    384     $theme_has_support = file_exists( $path );
    385 
    386     return $theme_has_support;
     381    $theme_has_support[ $stylesheet ] = file_exists( $path );
     382
     383    return $theme_has_support[ $stylesheet ];
    387384}
    388385
Note: See TracChangeset for help on using the changeset viewer.