<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head><meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>[54799] trunk: Editor: Avoid running certain logic around `theme.json` parsing unnecessarily for classic themes.</title>
</head>
<body>

<style type="text/css"><!--
#msg dl.meta { border: 1px #006 solid; background: #369; padding: 6px; color: #fff; }
#msg dl.meta dt { float: left; width: 6em; font-weight: bold; }
#msg dt:after { content:':';}
#msg dl, #msg dt, #msg ul, #msg li, #header, #footer, #logmsg { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt;  }
#msg dl a { font-weight: bold}
#msg dl a:link    { color:#fc3; }
#msg dl a:active  { color:#ff0; }
#msg dl a:visited { color:#cc6; }
h3 { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt; font-weight: bold; }
#msg pre { white-space: pre-line; overflow: auto; background: #ffc; border: 1px #fa0 solid; padding: 6px; }
#logmsg { background: #ffc; border: 1px #fa0 solid; padding: 1em 1em 0 1em; }
#logmsg p, #logmsg pre, #logmsg blockquote { margin: 0 0 1em 0; }
#logmsg p, #logmsg li, #logmsg dt, #logmsg dd { line-height: 14pt; }
#logmsg h1, #logmsg h2, #logmsg h3, #logmsg h4, #logmsg h5, #logmsg h6 { margin: .5em 0; }
#logmsg h1:first-child, #logmsg h2:first-child, #logmsg h3:first-child, #logmsg h4:first-child, #logmsg h5:first-child, #logmsg h6:first-child { margin-top: 0; }
#logmsg ul, #logmsg ol { padding: 0; list-style-position: inside; margin: 0 0 0 1em; }
#logmsg ul { text-indent: -1em; padding-left: 1em; }#logmsg ol { text-indent: -1.5em; padding-left: 1.5em; }
#logmsg > ul, #logmsg > ol { margin: 0 0 1em 0; }
#logmsg pre { background: #eee; padding: 1em; }
#logmsg blockquote { border: 1px solid #fa0; border-left-width: 10px; padding: 1em 1em 0 1em; background: white;}
#logmsg dl { margin: 0; }
#logmsg dt { font-weight: bold; }
#logmsg dd { margin: 0; padding: 0 0 0.5em 0; }
#logmsg dd:before { content:'\00bb';}
#logmsg table { border-spacing: 0px; border-collapse: collapse; border-top: 4px solid #fa0; border-bottom: 1px solid #fa0; background: #fff; }
#logmsg table th { text-align: left; font-weight: normal; padding: 0.2em 0.5em; border-top: 1px dotted #fa0; }
#logmsg table td { text-align: right; border-top: 1px dotted #fa0; padding: 0.2em 0.5em; }
#logmsg table thead th { text-align: center; border-bottom: 1px solid #fa0; }
#logmsg table th.Corner { text-align: left; }
#logmsg hr { border: none 0; border-top: 2px dashed #fa0; height: 1px; }
#header, #footer { color: #fff; background: #636; border: 1px #300 solid; padding: 6px; }
#patch { width: 100%; }
#patch h4 {font-family: verdana,arial,helvetica,sans-serif;font-size:10pt;padding:8px;background:#369;color:#fff;margin:0;}
#patch .propset h4, #patch .binary h4 {margin:0;}
#patch pre {padding:0;line-height:1.2em;margin:0;}
#patch .diff {width:100%;background:#eee;padding: 0 0 10px 0;overflow:auto;}
#patch .propset .diff, #patch .binary .diff  {padding:10px 0;}
#patch span {display:block;padding:0 10px;}
#patch .modfile, #patch .addfile, #patch .delfile, #patch .propset, #patch .binary, #patch .copfile {border:1px solid #ccc;margin:10px 0;}
#patch ins {background:#dfd;text-decoration:none;display:block;padding:0 10px;}
#patch del {background:#fdd;text-decoration:none;display:block;padding:0 10px;}
#patch .lines, .info {color:#888;background:#fff;}
--></style>
<div id="msg">
<dl class="meta" style="font-size: 105%">
<dt style="float: left; width: 6em; font-weight: bold">Revision</dt> <dd><a style="font-weight: bold" href="https://core.trac.wordpress.org/changeset/54799">54799</a><script type="application/ld+json">{"@context":"http://schema.org","@type":"EmailMessage","description":"Review this Commit","action":{"@type":"ViewAction","url":"https://core.trac.wordpress.org/changeset/54799","name":"Review Commit"}}</script></dd>
<dt style="float: left; width: 6em; font-weight: bold">Author</dt> <dd>flixos90</dd>
<dt style="float: left; width: 6em; font-weight: bold">Date</dt> <dd>2022-11-10 22:14:53 +0000 (Thu, 10 Nov 2022)</dd>
</dl>

<pre style='padding-left: 1em; margin: 2em 0; border-left: 2px solid #ccc; line-height: 1.25; font-size: 105%; font-family: sans-serif'>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.
Fixes <a href="https://core.trac.wordpress.org/ticket/56945">#56945</a>.</pre>

<h3>Modified Paths</h3>
<ul>
<li><a href="#trunksrcwpincludesclasswpthemejsonresolverphp">trunk/src/wp-includes/class-wp-theme-json-resolver.php</a></li>
<li><a href="#trunksrcwpincludesclasswpthemejsonphp">trunk/src/wp-includes/class-wp-theme-json.php</a></li>
<li><a href="#trunktestsphpunitteststhemewpThemeJsonResolverphp">trunk/tests/phpunit/tests/theme/wpThemeJsonResolver.php</a></li>
</ul>

</div>
<div id="patch">
<h3>Diff</h3>
<a id="trunksrcwpincludesclasswpthemejsonresolverphp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/src/wp-includes/class-wp-theme-json-resolver.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/src/wp-includes/class-wp-theme-json-resolver.php    2022-11-10 19:42:58 UTC (rev 54798)
+++ trunk/src/wp-includes/class-wp-theme-json-resolver.php      2022-11-10 22:14:53 UTC (rev 54799)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -246,8 +246,13 @@
</span><span class="cx" style="display: block; padding: 0 10px">                $options = wp_parse_args( $options, array( 'with_supports' => true ) );
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">                if ( null === static::$theme || ! static::has_same_registered_blocks( 'theme' ) ) {
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                        $theme_json_data = static::read_json_file( static::get_file_path_from_theme( 'theme.json' ) );
-                       $theme_json_data = static::translate( $theme_json_data, wp_get_theme()->get( 'TextDomain' ) );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                 $theme_json_file = static::get_file_path_from_theme( 'theme.json' );
+                       if ( '' !== $theme_json_file ) {
+                               $theme_json_data = static::read_json_file( $theme_json_file );
+                               $theme_json_data = static::translate( $theme_json_data, wp_get_theme()->get( 'TextDomain' ) );
+                       } else {
+                               $theme_json_data = array();
+                       }
</ins><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">                        /**
</span><span class="cx" style="display: block; padding: 0 10px">                         * Filters the data provided by the theme for global styles and settings.
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -259,20 +264,23 @@
</span><span class="cx" style="display: block; padding: 0 10px">                        $theme_json      = apply_filters( 'wp_theme_json_data_theme', new WP_Theme_JSON_Data( $theme_json_data, 'theme' ) );
</span><span class="cx" style="display: block; padding: 0 10px">                        $theme_json_data = $theme_json->get_data();
</span><span class="cx" style="display: block; padding: 0 10px">                        static::$theme   = new WP_Theme_JSON( $theme_json_data );
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                }
</del><span class="cx" style="display: block; padding: 0 10px"> 
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                if ( wp_get_theme()->parent() ) {
-                       // Get parent theme.json.
-                       $parent_theme_json_data = static::read_json_file( static::get_file_path_from_theme( 'theme.json', true ) );
-                       $parent_theme_json_data = static::translate( $parent_theme_json_data, wp_get_theme()->parent()->get( 'TextDomain' ) );
-                       $parent_theme           = new WP_Theme_JSON( $parent_theme_json_data );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                 if ( wp_get_theme()->parent() ) {
+                               // Get parent theme.json.
+                               $parent_theme_json_file = static::get_file_path_from_theme( 'theme.json', true );
+                               if ( '' !== $parent_theme_json_file ) {
+                                       $parent_theme_json_data = static::read_json_file( $parent_theme_json_file );
+                                       $parent_theme_json_data = static::translate( $parent_theme_json_data, wp_get_theme()->parent()->get( 'TextDomain' ) );
+                                       $parent_theme           = new WP_Theme_JSON( $parent_theme_json_data );
</ins><span class="cx" style="display: block; padding: 0 10px"> 
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                        /*
-                        * Merge the child theme.json into the parent theme.json.
-                        * The child theme takes precedence over the parent.
-                        */
-                       $parent_theme->merge( static::$theme );
-                       static::$theme = $parent_theme;
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                                 /*
+                                        * Merge the child theme.json into the parent theme.json.
+                                        * The child theme takes precedence over the parent.
+                                        */
+                                       $parent_theme->merge( static::$theme );
+                                       static::$theme = $parent_theme;
+                               }
+                       }
</ins><span class="cx" style="display: block; padding: 0 10px">                 }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">                if ( ! $options['with_supports'] ) {
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -403,6 +411,18 @@
</span><span class="cx" style="display: block; padding: 0 10px">                if ( ! $theme instanceof WP_Theme ) {
</span><span class="cx" style="display: block; padding: 0 10px">                        $theme = wp_get_theme();
</span><span class="cx" style="display: block; padding: 0 10px">                }
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+
+               /*
+                * Bail early if the theme does not support a theme.json.
+                *
+                * Since WP_Theme_JSON_Resolver::theme_has_support() only supports the active
+                * theme, the extra condition for whether $theme is the active theme is
+                * present here.
+                */
+               if ( $theme->get_stylesheet() === get_stylesheet() && ! static::theme_has_support() ) {
+                       return array();
+               }
+
</ins><span class="cx" style="display: block; padding: 0 10px">                 $user_cpt         = array();
</span><span class="cx" style="display: block; padding: 0 10px">                $post_type_filter = 'wp_global_styles';
</span><span class="cx" style="display: block; padding: 0 10px">                $stylesheet       = $theme->get_stylesheet();
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -582,8 +602,8 @@
</span><span class="cx" style="display: block; padding: 0 10px">        public static function theme_has_support() {
</span><span class="cx" style="display: block; padding: 0 10px">                if ( ! isset( static::$theme_has_support ) ) {
</span><span class="cx" style="display: block; padding: 0 10px">                        static::$theme_has_support = (
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                                is_readable( static::get_file_path_from_theme( 'theme.json' ) ) ||
-                               is_readable( static::get_file_path_from_theme( 'theme.json', true ) )
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                         static::get_file_path_from_theme( 'theme.json' ) !== '' ||
+                               static::get_file_path_from_theme( 'theme.json', true ) !== ''
</ins><span class="cx" style="display: block; padding: 0 10px">                         );
</span><span class="cx" style="display: block; padding: 0 10px">                }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span></span></pre></div>
<a id="trunksrcwpincludesclasswpthemejsonphp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/src/wp-includes/class-wp-theme-json.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/src/wp-includes/class-wp-theme-json.php     2022-11-10 19:42:58 UTC (rev 54798)
+++ trunk/src/wp-includes/class-wp-theme-json.php       2022-11-10 22:14:53 UTC (rev 54799)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -2939,7 +2939,8 @@
</span><span class="cx" style="display: block; padding: 0 10px">        public function set_spacing_sizes() {
</span><span class="cx" style="display: block; padding: 0 10px">                $spacing_scale = _wp_array_get( $this->theme_json, array( 'settings', 'spacing', 'spacingScale' ), array() );
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                if ( ! is_numeric( $spacing_scale['steps'] )
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         if ( ! isset( $spacing_scale['steps'] )
+                       || ! is_numeric( $spacing_scale['steps'] )
</ins><span class="cx" style="display: block; padding: 0 10px">                         || ! isset( $spacing_scale['mediumStep'] )
</span><span class="cx" style="display: block; padding: 0 10px">                        || ! isset( $spacing_scale['unit'] )
</span><span class="cx" style="display: block; padding: 0 10px">                        || ! isset( $spacing_scale['operator'] )
</span></span></pre></div>
<a id="trunktestsphpunitteststhemewpThemeJsonResolverphp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/tests/phpunit/tests/theme/wpThemeJsonResolver.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/tests/phpunit/tests/theme/wpThemeJsonResolver.php   2022-11-10 19:42:58 UTC (rev 54798)
+++ trunk/tests/phpunit/tests/theme/wpThemeJsonResolver.php     2022-11-10 22:14:53 UTC (rev 54799)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -34,13 +34,6 @@
</span><span class="cx" style="display: block; padding: 0 10px">        private $orig_theme_dir;
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">        /**
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-         * Queries.
-        *
-        * @var array
-        */
-       private $queries = array();
-
-       /**
</del><span class="cx" style="display: block; padding: 0 10px">          * WP_Theme_JSON_Resolver::$blocks_cache property.
</span><span class="cx" style="display: block; padding: 0 10px">         *
</span><span class="cx" style="display: block; padding: 0 10px">         * @var ReflectionProperty
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -105,7 +98,7 @@
</span><span class="cx" style="display: block; padding: 0 10px">                add_filter( 'theme_root', array( $this, 'filter_set_theme_root' ) );
</span><span class="cx" style="display: block; padding: 0 10px">                add_filter( 'stylesheet_root', array( $this, 'filter_set_theme_root' ) );
</span><span class="cx" style="display: block; padding: 0 10px">                add_filter( 'template_root', array( $this, 'filter_set_theme_root' ) );
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                $this->queries = array();
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+
</ins><span class="cx" style="display: block; padding: 0 10px">                 // Clear caches.
</span><span class="cx" style="display: block; padding: 0 10px">                wp_clean_themes_cache();
</span><span class="cx" style="display: block; padding: 0 10px">                unset( $GLOBALS['wp_themes'] );
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -129,13 +122,6 @@
</span><span class="cx" style="display: block; padding: 0 10px">                return 'pl_PL';
</span><span class="cx" style="display: block; padding: 0 10px">        }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-        function filter_db_query( $query ) {
-               if ( preg_match( '#post_type = \'wp_global_styles\'#', $query ) ) {
-                       $this->queries[] = $query;
-               }
-               return $query;
-       }
-
</del><span class="cx" style="display: block; padding: 0 10px">         /**
</span><span class="cx" style="display: block; padding: 0 10px">         * @ticket 52991
</span><span class="cx" style="display: block; padding: 0 10px">         * @ticket 54336
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -634,17 +620,26 @@
</span><span class="cx" style="display: block; padding: 0 10px">         * @covers WP_Theme_JSON_Resolver::get_user_data_from_wp_global_styles
</span><span class="cx" style="display: block; padding: 0 10px">         */
</span><span class="cx" style="display: block; padding: 0 10px">        function test_get_user_data_from_wp_global_styles_does_not_use_uncached_queries() {
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                // Switch to a theme that does have support.
+               switch_theme( 'block-theme' );
</ins><span class="cx" style="display: block; padding: 0 10px">                 wp_set_current_user( self::$administrator_id );
</span><span class="cx" style="display: block; padding: 0 10px">                $theme = wp_get_theme();
</span><span class="cx" style="display: block; padding: 0 10px">                WP_Theme_JSON_Resolver::get_user_data_from_wp_global_styles( $theme );
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                add_filter( 'query', array( $this, 'filter_db_query' ) );
-               $query_count = count( $this->queries );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         $global_styles_query_count = 0;
+               add_filter(
+                       'query',
+                       function( $query ) use ( &$global_styles_query_count ) {
+                               if ( preg_match( '#post_type = \'wp_global_styles\'#', $query ) ) {
+                                       $global_styles_query_count++;
+                               }
+                               return $query;
+                       }
+               );
</ins><span class="cx" style="display: block; padding: 0 10px">                 for ( $i = 0; $i < 3; $i++ ) {
</span><span class="cx" style="display: block; padding: 0 10px">                        WP_Theme_JSON_Resolver::get_user_data_from_wp_global_styles( $theme );
</span><span class="cx" style="display: block; padding: 0 10px">                        WP_Theme_JSON_Resolver::clean_cached_data();
</span><span class="cx" style="display: block; padding: 0 10px">                }
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                $query_count = count( $this->queries ) - $query_count;
-               $this->assertSame( 0, $query_count, 'Unexpected SQL queries detected for the wp_global_style post type prior to creation.' );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         $this->assertSame( 0, $global_styles_query_count, 'Unexpected SQL queries detected for the wp_global_style post type prior to creation.' );
</ins><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">                $user_cpt = WP_Theme_JSON_Resolver::get_user_data_from_wp_global_styles( $theme );
</span><span class="cx" style="display: block; padding: 0 10px">                $this->assertEmpty( $user_cpt, 'User CPT is expected to be empty.' );
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -652,14 +647,13 @@
</span><span class="cx" style="display: block; padding: 0 10px">                $user_cpt = WP_Theme_JSON_Resolver::get_user_data_from_wp_global_styles( $theme, true );
</span><span class="cx" style="display: block; padding: 0 10px">                $this->assertNotEmpty( $user_cpt, 'User CPT is expected not to be empty.' );
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                $query_count = count( $this->queries );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         $global_styles_query_count = 0;
</ins><span class="cx" style="display: block; padding: 0 10px">                 for ( $i = 0; $i < 3; $i ++ ) {
</span><span class="cx" style="display: block; padding: 0 10px">                        $new_user_cpt = WP_Theme_JSON_Resolver::get_user_data_from_wp_global_styles( $theme );
</span><span class="cx" style="display: block; padding: 0 10px">                        WP_Theme_JSON_Resolver::clean_cached_data();
</span><span class="cx" style="display: block; padding: 0 10px">                        $this->assertSameSets( $user_cpt, $new_user_cpt, "User CPTs do not match on run {$i}." );
</span><span class="cx" style="display: block; padding: 0 10px">                }
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                $query_count = count( $this->queries ) - $query_count;
-               $this->assertSame( 1, $query_count, 'Unexpected SQL queries detected for the wp_global_style post type after creation.' );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         $this->assertSame( 1, $global_styles_query_count, 'Unexpected SQL queries detected for the wp_global_style post type after creation.' );
</ins><span class="cx" style="display: block; padding: 0 10px">         }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">        /**
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -666,15 +660,16 @@
</span><span class="cx" style="display: block; padding: 0 10px">         * @covers WP_Theme_JSON_Resolver::get_user_data_from_wp_global_styles
</span><span class="cx" style="display: block; padding: 0 10px">         */
</span><span class="cx" style="display: block; padding: 0 10px">        function test_get_user_data_from_wp_global_styles_does_not_use_uncached_queries_for_logged_out_users() {
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                // Switch to a theme that does have support.
+               switch_theme( 'block-theme' );
</ins><span class="cx" style="display: block; padding: 0 10px">                 $theme = wp_get_theme();
</span><span class="cx" style="display: block; padding: 0 10px">                WP_Theme_JSON_Resolver::get_user_data_from_wp_global_styles( $theme );
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                add_filter( 'query', array( $this, 'filter_db_query' ) );
-               $query_count = count( $this->queries );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         $query_count = get_num_queries();
</ins><span class="cx" style="display: block; padding: 0 10px">                 for ( $i = 0; $i < 3; $i++ ) {
</span><span class="cx" style="display: block; padding: 0 10px">                        WP_Theme_JSON_Resolver::get_user_data_from_wp_global_styles( $theme );
</span><span class="cx" style="display: block; padding: 0 10px">                        WP_Theme_JSON_Resolver::clean_cached_data();
</span><span class="cx" style="display: block; padding: 0 10px">                }
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                $query_count = count( $this->queries ) - $query_count;
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         $query_count = get_num_queries() - $query_count;
</ins><span class="cx" style="display: block; padding: 0 10px">                 $this->assertSame( 0, $query_count, 'Unexpected SQL queries detected for the wp_global_style post type prior to creation.' );
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">                $user_cpt = WP_Theme_JSON_Resolver::get_user_data_from_wp_global_styles( $theme );
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -682,10 +677,34 @@
</span><span class="cx" style="display: block; padding: 0 10px">        }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">        /**
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         * @ticket 56945
+        * @covers WP_Theme_JSON_Resolver::get_user_data_from_wp_global_styles
+        */
+       function test_get_user_data_from_wp_global_styles_does_not_run_for_theme_without_support() {
+               // The 'default' theme does not support theme.json.
+               switch_theme( 'default' );
+               wp_set_current_user( self::$administrator_id );
+               $theme = wp_get_theme();
+
+               $start_queries = get_num_queries();
+
+               // When theme.json is not supported, the method should not run a query and always return an empty result.
+               $user_cpt = WP_Theme_JSON_Resolver::get_user_data_from_wp_global_styles( $theme );
+               $this->assertEmpty( $user_cpt, 'User CPT is expected to be empty.' );
+               $this->assertSame( 0, get_num_queries() - $start_queries, 'Unexpected SQL query detected for theme without theme.json support.' );
+
+               $user_cpt = WP_Theme_JSON_Resolver::get_user_data_from_wp_global_styles( $theme, true );
+               $this->assertEmpty( $user_cpt, 'User CPT is expected to be empty.' );
+               $this->assertSame( 0, get_num_queries() - $start_queries, 'Unexpected SQL query detected for theme without theme.json support.' );
+       }
+
+       /**
</ins><span class="cx" style="display: block; padding: 0 10px">          * @ticket 55392
</span><span class="cx" style="display: block; padding: 0 10px">         * @covers WP_Theme_JSON_Resolver::get_user_data_from_wp_global_styles
</span><span class="cx" style="display: block; padding: 0 10px">         */
</span><span class="cx" style="display: block; padding: 0 10px">        function test_get_user_data_from_wp_global_styles_does_exist() {
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                // Switch to a theme that does have support.
+               switch_theme( 'block-theme' );
</ins><span class="cx" style="display: block; padding: 0 10px">                 $theme = wp_get_theme();
</span><span class="cx" style="display: block; padding: 0 10px">                $post1 = WP_Theme_JSON_Resolver::get_user_data_from_wp_global_styles( $theme, true );
</span><span class="cx" style="display: block; padding: 0 10px">                $this->assertIsArray( $post1 );
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -701,6 +720,8 @@
</span><span class="cx" style="display: block; padding: 0 10px">         * @covers WP_Theme_JSON_Resolver::get_user_data_from_wp_global_styles
</span><span class="cx" style="display: block; padding: 0 10px">         */
</span><span class="cx" style="display: block; padding: 0 10px">        function test_get_user_data_from_wp_global_styles_create_post() {
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                // Switch to a theme that does have support.
+               switch_theme( 'block-theme' );
</ins><span class="cx" style="display: block; padding: 0 10px">                 $theme = wp_get_theme( 'testing' );
</span><span class="cx" style="display: block; padding: 0 10px">                $post1 = WP_Theme_JSON_Resolver::get_user_data_from_wp_global_styles( $theme );
</span><span class="cx" style="display: block; padding: 0 10px">                $this->assertIsArray( $post1 );
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -718,6 +739,8 @@
</span><span class="cx" style="display: block; padding: 0 10px">         * @covers WP_Theme_JSON_Resolver::get_user_data_from_wp_global_styles
</span><span class="cx" style="display: block; padding: 0 10px">         */
</span><span class="cx" style="display: block; padding: 0 10px">        function test_get_user_data_from_wp_global_styles_filter_state() {
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                // Switch to a theme that does have support.
+               switch_theme( 'block-theme' );
</ins><span class="cx" style="display: block; padding: 0 10px">                 $theme = wp_get_theme( 'foo' );
</span><span class="cx" style="display: block; padding: 0 10px">                $post1 = WP_Theme_JSON_Resolver::get_user_data_from_wp_global_styles( $theme, true, array( 'publish' ) );
</span><span class="cx" style="display: block; padding: 0 10px">                $this->assertIsArray( $post1 );
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -749,4 +772,29 @@
</span><span class="cx" style="display: block; padding: 0 10px">                $line_height      = $current_settings['typography']['lineHeight'];
</span><span class="cx" style="display: block; padding: 0 10px">                $this->assertTrue( $line_height, 'lineHeight setting after add_theme_support() should be true.' );
</span><span class="cx" style="display: block; padding: 0 10px">        }
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+
+       /**
+        * @ticket 56945
+        * @covers WP_Theme_JSON_Resolver::get_theme_data
+        */
+       function test_get_theme_data_does_not_parse_theme_json_if_not_present() {
+               // The 'default' theme does not support theme.json.
+               switch_theme( 'default' );
+
+               $theme_json_resolver = new WP_Theme_JSON_Resolver();
+
+               // Force-unset $i18n_schema property to "unload" translation schema.
+               $property = new ReflectionProperty( $theme_json_resolver, 'i18n_schema' );
+               $property->setAccessible( true );
+               $property->setValue( null );
+
+               // A completely empty theme.json data set still has the 'version' key when parsed.
+               $empty_theme_json = array( 'version' => WP_Theme_JSON::LATEST_SCHEMA );
+
+               // Call using 'with_supports' set to false, so that the method only considers theme.json.
+               $theme_data = $theme_json_resolver->get_theme_data( array(), array( 'with_supports' => false ) );
+               $this->assertInstanceOf( 'WP_Theme_JSON', $theme_data, 'Theme data should be an instance of WP_Theme_JSON.' );
+               $this->assertSame( $empty_theme_json, $theme_data->get_raw_data(), 'Theme data should be empty without theme support.' );
+               $this->assertNull( $property->getValue(), 'Theme i18n schema should not have been loaded without theme support.' );
+       }
</ins><span class="cx" style="display: block; padding: 0 10px"> }
</span></span></pre>
</div>
</div>

</body>
</html>