Make WordPress Core


Ignore:
Timestamp:
01/18/2023 11:38:16 AM (20 months ago)
Author:
hellofromTonya
Message:

Themes: Introduce wp_theme_has_theme_json() for public consumption.

Adds wp_theme_has_theme_json() for public consumption, to replace the private internal Core-only WP_Theme_JSON_Resolver::theme_has_support() method. This new global function checks if a theme or its parent has a theme.json file.

For performance, results are cached as an integer 1 or 0 in the 'theme_json' group with 'wp_theme_has_theme_json' key. This is a non-persistent cache. Why? To make the derived data from theme.json is always fresh from the potential modifications done via hooks that can use dynamic data (modify the stylesheet depending on some option, settings depending on user permissions, etc.).

Also adds a new public function wp_clean_theme_json_cache() to clear the cache on 'switch_theme' and start_previewing_theme'.

References:

Follow-up to [54493], [53282], [52744], [52049], [50959].

Props oandregal, afragen, alexstine, aristath, azaozz, costdev, flixos90, hellofromTonya, mamaduka, mcsf, ocean90, spacedmonkey.
Fixes #56975.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/class-wp-theme-json-resolver.php

    r55067 r55086  
    5959
    6060    /**
    61      * Whether or not the theme supports theme.json.
    62      *
    63      * @since 5.8.0
    64      * @var bool
    65      */
    66     protected static $theme_has_support = null;
    67 
    68     /**
    6961     * Container for data coming from the user.
    7062     *
     
    296288         */
    297289        $theme_support_data = WP_Theme_JSON::get_from_editor_settings( get_default_block_editor_settings() );
    298         if ( ! static::theme_has_support() ) {
     290        if ( ! wp_theme_has_theme_json() ) {
    299291            if ( ! isset( $theme_support_data['settings']['color'] ) ) {
    300292                $theme_support_data['settings']['color'] = array();
     
    422414         * Bail early if the theme does not support a theme.json.
    423415         *
    424          * Since WP_Theme_JSON_Resolver::theme_has_support() only supports the active
     416         * Since wp_theme_has_theme_json() only supports the active
    425417         * theme, the extra condition for whether $theme is the active theme is
    426418         * present here.
    427419         */
    428         if ( $theme->get_stylesheet() === get_stylesheet() && ! static::theme_has_support() ) {
     420        if ( $theme->get_stylesheet() === get_stylesheet() && ! wp_theme_has_theme_json() ) {
    429421            return array();
    430422        }
     
    603595     * @since 5.8.0
    604596     * @since 5.9.0 Added a check in the parent theme.
     597     * @deprecated 6.2.0 Use wp_theme_has_theme_json() instead.
    605598     *
    606599     * @return bool
    607600     */
    608601    public static function theme_has_support() {
    609         if ( ! isset( static::$theme_has_support ) ) {
    610             static::$theme_has_support = (
    611                 static::get_file_path_from_theme( 'theme.json' ) !== '' ||
    612                 static::get_file_path_from_theme( 'theme.json', true ) !== ''
    613             );
    614         }
    615 
    616         return static::$theme_has_support;
     602        _deprecated_function( __METHOD__, '6.2.0', 'wp_theme_has_theme_json()' );
     603
     604        return wp_theme_has_theme_json();
    617605    }
    618606
     
    657645        static::$user                     = null;
    658646        static::$user_custom_post_type_id = null;
    659         static::$theme_has_support        = null;
    660647        static::$i18n_schema              = null;
    661648    }
Note: See TracChangeset for help on using the changeset viewer.