Make WordPress Core


Ignore:
Timestamp:
01/25/2023 07:36:58 PM (21 months ago)
Author:
hellofromTonya
Message:

Themes: Add static cache variable to wp_theme_has_theme_json().

For performance, a static variable is added to wp_theme_has_theme_json() to cache the boolean result of determining if a theme (or its parent) has a theme.json file.

This cache avoids the overhead of calling get_stylesheet_directory() and get_template_directory() each time wp_theme_has_theme_json() is invoked.

The cache is lean, non-persistent, and encapsulated within the function (i.e. function scope and not available externally).

The cache is ignored when:

  • WP_DEBUG is on to avoid interrupting theme developer's workflow and for extender automated test suites.
  • WP_RUN_CORE_TESTS is on to ensure each Core test exercises the checking code.

Follow-up to [55092], [55086].

Props oandregal, azaozz, costdev, dmsnell, flixos90, hellofromTonya, Otto42, spacedmonkey.
Fixes #56975.

File:
1 edited

Legend:

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

    r55092 r55138  
    265265 */
    266266function wp_theme_has_theme_json() {
     267    static $theme_has_support = null;
     268
     269    if (
     270        null !== $theme_has_support &&
     271        /*
     272         * Ignore static cache when `WP_DEBUG` is enabled. Why? To avoid interfering with
     273         * the theme developer's workflow.
     274         *
     275         * @todo Replace `WP_DEBUG` once an "in development mode" check is available in Core.
     276         */
     277        ! WP_DEBUG &&
     278        /*
     279         * Ignore cache when automated test suites are running. Why? To ensure
     280         * the static cache is reset between each test.
     281         */
     282        ! ( defined( 'WP_RUN_CORE_TESTS' ) && WP_RUN_CORE_TESTS )
     283    ) {
     284        return $theme_has_support;
     285    }
     286
    267287    // Does the theme have its own theme.json?
    268288    $theme_has_support = is_readable( get_stylesheet_directory() . '/theme.json' );
Note: See TracChangeset for help on using the changeset viewer.