Make WordPress Core

Changeset 54399


Ignore:
Timestamp:
10/06/2022 06:00:21 PM (4 years ago)
Author:
davidbaumwald
Message:

Editor: Ensure block styles in theme.json are rendered.

This change removes the caching of theme data in WP_Theme_JSON_Resolver::get_theme_data(), instead freshly compiling theme data on each call.

Also, to prevent any performance degradation by the removal, the file contents of theme.json files are now cached in WP_Theme_JSON_Resolver::read_json_file(), preventing multiple filesystem reads.

Follow-up to [54385].

Props ndiego, bph, mikachan, andrewserong, oandregal, cbravobernal, bernhard-reiter, aristath.
Fixes #56736.

File:
1 edited

Legend:

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

    r54298 r54399  
    7272
    7373        /**
     74         * `theme.json` file cache.
     75         *
     76         * @since 6.1.0
     77         * @var array
     78         */
     79        protected static $theme_json_file_cache = array();
     80
     81        /**
    7482         * Processes a file that adheres to the theme.json schema
    7583         * and returns an array with its contents, or a void array if none found.
    7684         *
    7785         * @since 5.8.0
     86         * @since 6.1.0 Added caching.
    7887         *
    7988         * @param string $file_path Path to file. Empty if no file.
     
    8190         */
    8291        protected static function read_json_file( $file_path ) {
    83                 $config = array();
    8492                if ( $file_path ) {
     93                        if ( array_key_exists( $file_path, static::$theme_json_file_cache ) ) {
     94                                return static::$theme_json_file_cache[ $file_path ];
     95                        }
     96
    8597                        $decoded_file = wp_json_file_decode( $file_path, array( 'associative' => true ) );
    8698                        if ( is_array( $decoded_file ) ) {
    87                                 $config = $decoded_file;
    88                         }
    89                 }
    90                 return $config;
     99                                static::$theme_json_file_cache[ $file_path ] = $decoded_file;
     100                                return static::$theme_json_file_cache[ $file_path ];
     101                        }
     102                }
     103
     104                return array();
    91105        }
    92106
     
    132146         */
    133147        public static function get_core_data() {
    134                 if ( null !== static::$core ) {
    135                         return static::$core;
    136                 }
    137 
    138148                $config = static::read_json_file( __DIR__ . '/theme.json' );
    139149                $config = static::translate( $config );
     150
    140151                /**
    141152                 * Filters the default data provided by WordPress for global styles & settings.
     
    179190                $options = wp_parse_args( $options, array( 'with_supports' => true ) );
    180191
    181                 if ( null === static::$theme ) {
    182                         $theme_json_data = static::read_json_file( static::get_file_path_from_theme( 'theme.json' ) );
    183                         $theme_json_data = static::translate( $theme_json_data, wp_get_theme()->get( 'TextDomain' ) );
    184                         /**
    185                          * Filters the data provided by the theme for global styles & settings.
    186                          *
    187                          * @since 6.1.0
    188                          *
    189                          * @param WP_Theme_JSON_Data Class to access and update the underlying data.
     192                $theme_json_data = static::read_json_file( static::get_file_path_from_theme( 'theme.json' ) );
     193                $theme_json_data = static::translate( $theme_json_data, wp_get_theme()->get( 'TextDomain' ) );
     194
     195                /**
     196                 * Filters the data provided by the theme for global styles and settings.
     197                 *
     198                 * @since 6.1.0
     199                 *
     200                 * @param WP_Theme_JSON_Data Class to access and update the underlying data.
     201                 */
     202                $theme_json      = apply_filters( 'theme_json_theme', new WP_Theme_JSON_Data( $theme_json_data, 'theme' ) );
     203                $theme_json_data = $theme_json->get_data();
     204                static::$theme   = new WP_Theme_JSON( $theme_json_data );
     205
     206                if ( wp_get_theme()->parent() ) {
     207                        // Get parent theme.json.
     208                        $parent_theme_json_data = static::read_json_file( static::get_file_path_from_theme( 'theme.json', true ) );
     209                        $parent_theme_json_data = static::translate( $parent_theme_json_data, wp_get_theme()->parent()->get( 'TextDomain' ) );
     210                        $parent_theme           = new WP_Theme_JSON( $parent_theme_json_data );
     211
     212                        /*
     213                         * Merge the child theme.json into the parent theme.json.
     214                         * The child theme takes precedence over the parent.
    190215                         */
    191                         $theme_json      = apply_filters( 'theme_json_theme', new WP_Theme_JSON_Data( $theme_json_data, 'theme' ) );
    192                         $theme_json_data = $theme_json->get_data();
    193                         static::$theme   = new WP_Theme_JSON( $theme_json_data );
    194 
    195                         if ( wp_get_theme()->parent() ) {
    196                                 // Get parent theme.json.
    197                                 $parent_theme_json_data = static::read_json_file( static::get_file_path_from_theme( 'theme.json', true ) );
    198                                 $parent_theme_json_data = static::translate( $parent_theme_json_data, wp_get_theme()->parent()->get( 'TextDomain' ) );
    199                                 $parent_theme           = new WP_Theme_JSON( $parent_theme_json_data );
    200 
    201                                 // Merge the child theme.json into the parent theme.json.
    202                                 // The child theme takes precedence over the parent.
    203                                 $parent_theme->merge( static::$theme );
    204                                 static::$theme = $parent_theme;
    205                         }
     216                        $parent_theme->merge( static::$theme );
     217                        static::$theme = $parent_theme;
    206218                }
    207219
     
    398410         */
    399411        public static function get_user_data() {
    400                 if ( null !== static::$user ) {
    401                         return static::$user;
    402                 }
    403 
    404412                $config   = array();
    405413                $user_cpt = static::get_user_data_from_wp_global_styles( wp_get_theme() );
Note: See TracChangeset for help on using the changeset viewer.