Make WordPress Core


Ignore:
Timestamp:
11/10/2022 10:40:59 PM (2 years ago)
Author:
flixos90
Message:

Editor: Avoid running certain logic around theme.json parsing unnecessarily for classic themes.

Here's what it does:

  • Do not load and parse theme-i18n.json schema if the theme does not have a theme.json file.
  • Fix the variable caching layer around the theme's theme.json parsing so that a parent's theme theme.json is cached as well.
  • Do not run a WP_Query for global styles for a user when the theme does not have a theme.json.

In a basic WordPress setup, this changeset improves wp_head execution time for classic themes by 10%, and overall response time for both block themes and classic themes by 4%. This may seem like a small win, but 4% reduced overall response time is actually quite a bit for one change, and it is worth mentioning that this is just one of several other little performance tweaks which are being worked on to improve performance of theme.json parsing further.

Props flixos90, manuilov, oandregal, peterwilsoncc, spacedmonkey.
Merges [54799] to the 6.1 branch.
Fixes #56945.

Location:
branches/6.1
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • branches/6.1

  • branches/6.1/src/wp-includes/class-wp-theme-json-resolver.php

    r54779 r54800  
    247247
    248248        if ( null === static::$theme || ! static::has_same_registered_blocks( 'theme' ) ) {
    249             $theme_json_data = static::read_json_file( static::get_file_path_from_theme( 'theme.json' ) );
    250             $theme_json_data = static::translate( $theme_json_data, wp_get_theme()->get( 'TextDomain' ) );
     249            $theme_json_file = static::get_file_path_from_theme( 'theme.json' );
     250            if ( '' !== $theme_json_file ) {
     251                $theme_json_data = static::read_json_file( $theme_json_file );
     252                $theme_json_data = static::translate( $theme_json_data, wp_get_theme()->get( 'TextDomain' ) );
     253            } else {
     254                $theme_json_data = array();
     255            }
    251256
    252257            /**
     
    260265            $theme_json_data = $theme_json->get_data();
    261266            static::$theme   = new WP_Theme_JSON( $theme_json_data );
    262         }
    263 
    264         if ( wp_get_theme()->parent() ) {
    265             // Get parent theme.json.
    266             $parent_theme_json_data = static::read_json_file( static::get_file_path_from_theme( 'theme.json', true ) );
    267             $parent_theme_json_data = static::translate( $parent_theme_json_data, wp_get_theme()->parent()->get( 'TextDomain' ) );
    268             $parent_theme           = new WP_Theme_JSON( $parent_theme_json_data );
    269 
    270             /*
    271              * Merge the child theme.json into the parent theme.json.
    272              * The child theme takes precedence over the parent.
    273              */
    274             $parent_theme->merge( static::$theme );
    275             static::$theme = $parent_theme;
     267
     268            if ( wp_get_theme()->parent() ) {
     269                // Get parent theme.json.
     270                $parent_theme_json_file = static::get_file_path_from_theme( 'theme.json', true );
     271                if ( '' !== $parent_theme_json_file ) {
     272                    $parent_theme_json_data = static::read_json_file( $parent_theme_json_file );
     273                    $parent_theme_json_data = static::translate( $parent_theme_json_data, wp_get_theme()->parent()->get( 'TextDomain' ) );
     274                    $parent_theme           = new WP_Theme_JSON( $parent_theme_json_data );
     275
     276                    /*
     277                     * Merge the child theme.json into the parent theme.json.
     278                     * The child theme takes precedence over the parent.
     279                     */
     280                    $parent_theme->merge( static::$theme );
     281                    static::$theme = $parent_theme;
     282                }
     283            }
    276284        }
    277285
     
    404412            $theme = wp_get_theme();
    405413        }
     414
     415        /*
     416         * Bail early if the theme does not support a theme.json.
     417         *
     418         * Since WP_Theme_JSON_Resolver::theme_has_support() only supports the active
     419         * theme, the extra condition for whether $theme is the active theme is
     420         * present here.
     421         */
     422        if ( $theme->get_stylesheet() === get_stylesheet() && ! static::theme_has_support() ) {
     423            return array();
     424        }
     425
    406426        $user_cpt         = array();
    407427        $post_type_filter = 'wp_global_styles';
     
    583603        if ( ! isset( static::$theme_has_support ) ) {
    584604            static::$theme_has_support = (
    585                 is_readable( static::get_file_path_from_theme( 'theme.json' ) ) ||
    586                 is_readable( static::get_file_path_from_theme( 'theme.json', true ) )
     605                static::get_file_path_from_theme( 'theme.json' ) !== '' ||
     606                static::get_file_path_from_theme( 'theme.json', true ) !== ''
    587607            );
    588608        }
Note: See TracChangeset for help on using the changeset viewer.