<!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>[55231] trunk: Block editor: Update WP_Theme_JSON_Resolver and improve its performance.</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/55231">55231</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/55231","name":"Review Commit"}}</script></dd>
<dt style="float: left; width: 6em; font-weight: bold">Author</dt> <dd>jorgefilipecosta</dd>
<dt style="float: left; width: 6em; font-weight: bold">Date</dt> <dd>2023-02-06 15:31:50 +0000 (Mon, 06 Feb 2023)</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'>Block editor: Update WP_Theme_JSON_Resolver and improve its performance.

This commit includes the latest updates WP_Theme_JSON_Resolver class made in the block editor. Some of these updates improve the performance of the class.

Props Mamaduka, hellofromTonya, flixos90, jorgefilipecosta, oandregal, spacedmonkey, audrasjb, costdev, scruffian.
Closes <a href="https://core.trac.wordpress.org/ticket/57545">#57545</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="#trunktestsphpunitdatathemedir1blockthemethemejson">trunk/tests/phpunit/data/themedir1/block-theme/theme.json</a></li>
<li><a href="#trunktestsphpunittestsrestapirestglobalstylescontrollerphp">trunk/tests/phpunit/tests/rest-api/rest-global-styles-controller.php</a></li>
<li><a href="#trunktestsphpunitteststhemewpThemeJsonResolverphp">trunk/tests/phpunit/tests/theme/wpThemeJsonResolver.php</a></li>
</ul>

<h3>Added Paths</h3>
<ul>
<li><a href="#trunktestsphpunitdatathemedir1blockthemestylesvariationbjson">trunk/tests/phpunit/data/themedir1/block-theme/styles/variation-b.json</a></li>
<li>trunk/tests/phpunit/data/themedir1/block-theme-child/styles/</li>
<li><a href="#trunktestsphpunitdatathemedir1blockthemechildstylesvariationbjson">trunk/tests/phpunit/data/themedir1/block-theme-child/styles/variation-b.json</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    2023-02-06 15:22:38 UTC (rev 55230)
+++ trunk/src/wp-includes/class-wp-theme-json-resolver.php      2023-02-06 15:31:50 UTC (rev 55231)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -427,14 +427,16 @@
</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="cx" style="display: block; padding: 0 10px">                $args             = array(
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                        'posts_per_page'      => 1,
-                       'orderby'             => 'date',
-                       'order'               => 'desc',
-                       'post_type'           => $post_type_filter,
-                       'post_status'         => $post_status_filter,
-                       'ignore_sticky_posts' => true,
-                       'no_found_rows'       => true,
-                       'tax_query'           => array(
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                 'posts_per_page'         => 1,
+                       'orderby'                => 'date',
+                       'order'                  => 'desc',
+                       'post_type'              => $post_type_filter,
+                       'post_status'            => $post_status_filter,
+                       'ignore_sticky_posts'    => true,
+                       'no_found_rows'          => true,
+                       'update_post_meta_cache' => false,
+                       'update_post_term_cache' => false,
+                       'tax_query'              => array(
</ins><span class="cx" style="display: block; padding: 0 10px">                                 array(
</span><span class="cx" style="display: block; padding: 0 10px">                                        'taxonomy' => 'wp_theme',
</span><span class="cx" style="display: block; padding: 0 10px">                                        'field'    => 'name',
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -446,7 +448,7 @@
</span><span class="cx" style="display: block; padding: 0 10px">                $global_style_query = new WP_Query();
</span><span class="cx" style="display: block; padding: 0 10px">                $recent_posts       = $global_style_query->query( $args );
</span><span class="cx" style="display: block; padding: 0 10px">                if ( count( $recent_posts ) === 1 ) {
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                        $user_cpt = get_post( $recent_posts[0], ARRAY_A );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                 $user_cpt = get_object_vars( $recent_posts[0] );
</ins><span class="cx" style="display: block; padding: 0 10px">                 } elseif ( $create_post ) {
</span><span class="cx" style="display: block; padding: 0 10px">                        $cpt_post_id = wp_insert_post(
</span><span class="cx" style="display: block; padding: 0 10px">                                array(
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -462,7 +464,7 @@
</span><span class="cx" style="display: block; padding: 0 10px">                                true
</span><span class="cx" style="display: block; padding: 0 10px">                        );
</span><span class="cx" style="display: block; padding: 0 10px">                        if ( ! is_wp_error( $cpt_post_id ) ) {
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                                $user_cpt = get_post( $cpt_post_id, ARRAY_A );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                         $user_cpt = get_object_vars( get_post( $cpt_post_id ) );
</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">@@ -525,10 +527,16 @@
</span><span class="cx" style="display: block; padding: 0 10px">        /**
</span><span class="cx" style="display: block; padding: 0 10px">         * Returns the data merged from multiple origins.
</span><span class="cx" style="display: block; padding: 0 10px">         *
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-         * There are three sources of data (origins) for a site:
-        * default, theme, and custom. The custom's has higher priority
-        * than the theme's, and the theme's higher than default's.
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+  * There are four sources of data (origins) for a site:
</ins><span class="cx" style="display: block; padding: 0 10px">          *
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         * - default => WordPress
+        * - blocks  => each one of the blocks provides data for itself
+        * - theme   => the active theme
+        * - custom  => data provided by the user
+        *
+        * The custom's has higher priority than the theme's, the theme's higher than blocks',
+        * and block's higher than default's.
+        *
</ins><span class="cx" style="display: block; padding: 0 10px">          * Unlike the getters
</span><span class="cx" style="display: block; padding: 0 10px">         * {@link https://developer.wordpress.org/reference/classes/wp_theme_json_resolver/get_core_data/ get_core_data},
</span><span class="cx" style="display: block; padding: 0 10px">         * {@link https://developer.wordpress.org/reference/classes/wp_theme_json_resolver/get_theme_data/ get_theme_data},
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -535,7 +543,7 @@
</span><span class="cx" style="display: block; padding: 0 10px">         * and {@link https://developer.wordpress.org/reference/classes/wp_theme_json_resolver/get_user_data/ get_user_data},
</span><span class="cx" style="display: block; padding: 0 10px">         * this method returns data after it has been merged with the previous origins.
</span><span class="cx" style="display: block; padding: 0 10px">         * This means that if the same piece of data is declared in different origins
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-         * (user, theme, and core), the last origin overrides the previous.
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+  * (default, blocks, theme, custom), the last origin overrides the previous.
</ins><span class="cx" style="display: block; padding: 0 10px">          *
</span><span class="cx" style="display: block; padding: 0 10px">         * For example, if the user has set a background color
</span><span class="cx" style="display: block; padding: 0 10px">         * for the paragraph block, and the theme has done it as well,
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -545,9 +553,10 @@
</span><span class="cx" style="display: block; padding: 0 10px">         * @since 5.9.0 Added user data, removed the `$settings` parameter,
</span><span class="cx" style="display: block; padding: 0 10px">         *              added the `$origin` parameter.
</span><span class="cx" style="display: block; padding: 0 10px">         * @since 6.1.0 Added block data and generation of spacingSizes array.
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         * @since 6.2.0 Changed ' $origin' parameter values to 'default', 'blocks', 'theme' or 'custom'.
</ins><span class="cx" style="display: block; padding: 0 10px">          *
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-         * @param string $origin Optional. To what level should we merge data.
-        *                       Valid values are 'theme' or 'custom'. Default 'custom'.
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+  * @param string $origin Optional. To what level should we merge data: 'default', 'blocks', 'theme' or 'custom'.
+        *                       'custom' is used as default value as well as fallback value if the origin is unknown.
</ins><span class="cx" style="display: block; padding: 0 10px">          * @return WP_Theme_JSON
</span><span class="cx" style="display: block; padding: 0 10px">         */
</span><span class="cx" style="display: block; padding: 0 10px">        public static function get_merged_data( $origin = 'custom' ) {
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -556,14 +565,23 @@
</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">                $result = static::get_core_data();
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                if ( 'default' === $origin ) {
+                       $result->set_spacing_sizes();
+                       return $result;
+               }
+
</ins><span class="cx" style="display: block; padding: 0 10px">                 $result->merge( static::get_block_data() );
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                if ( 'blocks' === $origin ) {
+                       return $result;
+               }
+
</ins><span class="cx" style="display: block; padding: 0 10px">                 $result->merge( static::get_theme_data() );
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-
-               if ( 'custom' === $origin ) {
-                       $result->merge( static::get_user_data() );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         if ( 'theme' === $origin ) {
+                       $result->set_spacing_sizes();
+                       return $result;
</ins><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">-                // Generate the default spacingSizes array based on the merged spacingScale settings.
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         $result->merge( static::get_user_data() );
</ins><span class="cx" style="display: block; padding: 0 10px">                 $result->set_spacing_sizes();
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">                return $result;
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -650,32 +668,60 @@
</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">+         * Returns an array of all nested JSON files within a given directory.
+        *
+        * @since 6.2.0
+        *
+        * @param string $dir The directory to recursively iterate and list files of.
+        * @return array The merged array.
+        */
+       private static function recursively_iterate_json( $dir ) {
+               $nested_files      = new RecursiveIteratorIterator( new RecursiveDirectoryIterator( $dir ) );
+               $nested_json_files = iterator_to_array( new RegexIterator( $nested_files, '/^.+\.json$/i', RecursiveRegexIterator::GET_MATCH ) );
+               return $nested_json_files;
+       }
+
+
+       /**
</ins><span class="cx" style="display: block; padding: 0 10px">          * Returns the style variations defined by the theme.
</span><span class="cx" style="display: block; padding: 0 10px">         *
</span><span class="cx" style="display: block; padding: 0 10px">         * @since 6.0.0
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         * @since 6.2.0 Returns parent theme variations if theme is a child.
</ins><span class="cx" style="display: block; padding: 0 10px">          *
</span><span class="cx" style="display: block; padding: 0 10px">         * @return array
</span><span class="cx" style="display: block; padding: 0 10px">         */
</span><span class="cx" style="display: block; padding: 0 10px">        public static function get_style_variations() {
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                $variations     = array();
-               $base_directory = get_stylesheet_directory() . '/styles';
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         $variation_files    = array();
+               $variations         = array();
+               $base_directory     = get_stylesheet_directory() . '/styles';
+               $template_directory = get_template_directory() . '/styles';
</ins><span class="cx" style="display: block; padding: 0 10px">                 if ( is_dir( $base_directory ) ) {
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                        $nested_files      = new RecursiveIteratorIterator( new RecursiveDirectoryIterator( $base_directory ) );
-                       $nested_html_files = iterator_to_array( new RegexIterator( $nested_files, '/^.+\.json$/i', RecursiveRegexIterator::GET_MATCH ) );
-                       ksort( $nested_html_files );
-                       foreach ( $nested_html_files as $path => $file ) {
-                               $decoded_file = wp_json_file_decode( $path, array( 'associative' => true ) );
-                               if ( is_array( $decoded_file ) ) {
-                                       $translated = static::translate( $decoded_file, wp_get_theme()->get( 'TextDomain' ) );
-                                       $variation  = ( new WP_Theme_JSON( $translated ) )->get_raw_data();
-                                       if ( empty( $variation['title'] ) ) {
-                                               $variation['title'] = basename( $path, '.json' );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                 $variation_files = static::recursively_iterate_json( $base_directory );
+               }
+               if ( is_dir( $template_directory ) && $template_directory !== $base_directory ) {
+                       $variation_files_parent = static::recursively_iterate_json( $template_directory );
+                       // If the child and parent variation file basename are the same, only include the child theme's.
+                       foreach ( $variation_files_parent as $parent_path => $parent ) {
+                               foreach ( $variation_files as $child_path => $child ) {
+                                       if ( basename( $parent_path ) === basename( $child_path ) ) {
+                                               unset( $variation_files_parent[ $parent_path ] );
</ins><span class="cx" style="display: block; padding: 0 10px">                                         }
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                                        $variations[] = $variation;
</del><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">+                        $variation_files = array_merge( $variation_files, $variation_files_parent );
</ins><span class="cx" style="display: block; padding: 0 10px">                 }
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                ksort( $variation_files );
+               foreach ( $variation_files as $path => $file ) {
+                       $decoded_file = wp_json_file_decode( $path, array( 'associative' => true ) );
+                       if ( is_array( $decoded_file ) ) {
+                               $translated = static::translate( $decoded_file, wp_get_theme()->get( 'TextDomain' ) );
+                               $variation  = ( new WP_Theme_JSON( $translated ) )->get_raw_data();
+                               if ( empty( $variation['title'] ) ) {
+                                       $variation['title'] = basename( $path, '.json' );
+                               }
+                               $variations[] = $variation;
+                       }
+               }
</ins><span class="cx" style="display: block; padding: 0 10px">                 return $variations;
</span><span class="cx" style="display: block; padding: 0 10px">        }
</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></span></pre></div>
<a id="trunktestsphpunitdatathemedir1blockthemestylesvariationbjson"></a>
<div class="addfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Added: trunk/tests/phpunit/data/themedir1/block-theme/styles/variation-b.json</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/tests/phpunit/data/themedir1/block-theme/styles/variation-b.json                            (rev 0)
+++ trunk/tests/phpunit/data/themedir1/block-theme/styles/variation-b.json      2023-02-06 15:31:50 UTC (rev 55231)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -0,0 +1,18 @@
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+{
+       "version": 2,
+       "settings": {
+               "blocks": {
+                       "core/post-title": {
+                               "color": {
+                                       "palette": [
+                                               {
+                                                       "slug": "light",
+                                                       "name": "Light",
+                                                       "color": "#f1f1f1"
+                                               }
+                                       ]
+                               }
+                       }
+               }
+       }
+}
</ins></span></pre></div>
<a id="trunktestsphpunitdatathemedir1blockthemethemejson"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/tests/phpunit/data/themedir1/block-theme/theme.json</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/tests/phpunit/data/themedir1/block-theme/theme.json 2023-02-06 15:22:38 UTC (rev 55230)
+++ trunk/tests/phpunit/data/themedir1/block-theme/theme.json   2023-02-06 15:31:50 UTC (rev 55231)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -124,4 +124,4 @@
</span><span class="cx" style="display: block; padding: 0 10px">                        "area": "header"
</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">-}
</del><span class="cx" style="display: block; padding: 0 10px">\ No newline at end of file
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+}
</ins></span></pre></div>
<a id="trunktestsphpunitdatathemedir1blockthemechildstylesvariationbjson"></a>
<div class="addfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Added: trunk/tests/phpunit/data/themedir1/block-theme-child/styles/variation-b.json</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/tests/phpunit/data/themedir1/block-theme-child/styles/variation-b.json                              (rev 0)
+++ trunk/tests/phpunit/data/themedir1/block-theme-child/styles/variation-b.json        2023-02-06 15:31:50 UTC (rev 55231)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -0,0 +1,18 @@
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+{
+       "version": 2,
+       "settings": {
+               "blocks": {
+                       "core/post-title": {
+                               "color": {
+                                       "palette": [
+                                               {
+                                                       "slug": "dark",
+                                                       "name": "Dark",
+                                                       "color": "#010101"
+                                               }
+                                       ]
+                               }
+                       }
+               }
+       }
+}
</ins></span></pre></div>
<a id="trunktestsphpunittestsrestapirestglobalstylescontrollerphp"></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/rest-api/rest-global-styles-controller.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/tests/phpunit/tests/rest-api/rest-global-styles-controller.php      2023-02-06 15:22:38 UTC (rev 55230)
+++ trunk/tests/phpunit/tests/rest-api/rest-global-styles-controller.php        2023-02-06 15:31:50 UTC (rev 55231)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -486,6 +486,27 @@
</span><span class="cx" style="display: block; padding: 0 10px">                $expected = array(
</span><span class="cx" style="display: block; padding: 0 10px">                        array(
</span><span class="cx" style="display: block; padding: 0 10px">                                'version'  => 2,
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                                'title'    => 'variation-b',
+                               'settings' => array(
+                                       'blocks' => array(
+                                               'core/post-title' => array(
+                                                       'color' => array(
+                                                               'palette' => array(
+                                                                       'theme' => array(
+                                                                               array(
+                                                                                       'slug'  => 'light',
+                                                                                       'name'  => 'Light',
+                                                                                       'color' => '#f1f1f1',
+                                                                               ),
+                                                                       ),
+                                                               ),
+                                                       ),
+                                               ),
+                                       ),
+                               ),
+                       ),
+                       array(
+                               'version'  => 2,
</ins><span class="cx" style="display: block; padding: 0 10px">                                 'title'    => 'Block theme variation',
</span><span class="cx" style="display: block; padding: 0 10px">                                'settings' => array(
</span><span class="cx" style="display: block; padding: 0 10px">                                        'color' => array(
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -511,7 +532,11 @@
</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><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                $this->assertSameSetsWithIndex( $data, $expected );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+
+               wp_recursive_ksort( $data );
+               wp_recursive_ksort( $expected );
+
+               $this->assertSameSets( $data, $expected );
</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="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   2023-02-06 15:22:38 UTC (rev 55230)
+++ trunk/tests/phpunit/tests/theme/wpThemeJsonResolver.php     2023-02-06 15:31:50 UTC (rev 55231)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -235,7 +235,7 @@
</span><span class="cx" style="display: block; padding: 0 10px">                );
</span><span class="cx" style="display: block; padding: 0 10px">                $this->assertSame(
</span><span class="cx" style="display: block; padding: 0 10px">                        'Wariant motywu blokowego',
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                        $style_variations[0]['title']
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                 $style_variations[1]['title']
</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">@@ -775,4 +775,209 @@
</span><span class="cx" style="display: block; padding: 0 10px">                $this->assertSame( $empty_theme_json, $theme_data->get_raw_data(), 'Theme data should be empty without theme support.' );
</span><span class="cx" style="display: block; padding: 0 10px">                $this->assertNull( $property->getValue(), 'Theme i18n schema should not have been loaded without theme support.' );
</span><span class="cx" style="display: block; padding: 0 10px">        }
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+
+       /**
+        * Tests that get_merged_data returns the data merged up to the proper origin.
+        *
+        * @ticket 57545
+        *
+        * @covers WP_Theme_JSON_Resolver::get_merged_data
+        *
+        * @dataProvider data_get_merged_data_returns_origin
+        *
+        * @param string $origin             What origin to get data from.
+        * @param bool   $core_palette       Whether the core palette is present.
+        * @param string $core_palette_text  Message.
+        * @param string $block_styles       Whether the block styles are present.
+        * @param string $block_styles_text  Message.
+        * @param bool   $theme_palette      Whether the theme palette is present.
+        * @param string $theme_palette_text Message.
+        * @param bool   $user_palette        Whether the user palette is present.
+        * @param string $user_palette_text   Message.
+        */
+       public function test_get_merged_data_returns_origin( $origin, $core_palette, $core_palette_text, $block_styles, $block_styles_text, $theme_palette, $theme_palette_text, $user_palette, $user_palette_text ) {
+               // Make sure there is data from the blocks origin.
+               register_block_type(
+                       'my/block-with-styles',
+                       array(
+                               'api_version' => 2,
+                               'attributes'  => array(
+                                       'borderColor' => array(
+                                               'type' => 'string',
+                                       ),
+                                       'style'       => array(
+                                               'type' => 'object',
+                                       ),
+                               ),
+                               'supports'    => array(
+                                       '__experimentalStyle' => array(
+                                               'typography' => array(
+                                                       'fontSize' => '42rem',
+                                               ),
+                                       ),
+                               ),
+                       )
+               );
+
+               // Make sure there is data from the theme origin.
+               switch_theme( 'block-theme' );
+
+               // Make sure there is data from the user origin.
+               wp_set_current_user( self::$administrator_id );
+               $user_cpt = WP_Theme_JSON_Resolver::get_user_data_from_wp_global_styles( wp_get_theme(), true );
+               $config   = json_decode( $user_cpt['post_content'], true );
+               $config['settings']['color']['palette']['custom'] = array(
+                       array(
+                               'color' => 'hotpink',
+                               'name'  => 'My color',
+                               'slug'  => 'my-color',
+                       ),
+               );
+               $user_cpt['post_content']                         = wp_json_encode( $config );
+               wp_update_post( $user_cpt, true, false );
+
+               $theme_json = WP_Theme_JSON_Resolver::get_merged_data( $origin );
+               $settings   = $theme_json->get_settings();
+               $styles     = $theme_json->get_styles_block_nodes();
+               $styles     = array_filter(
+                       $styles,
+                       static function( $element ) {
+                               return isset( $element['name'] ) && 'my/block-with-styles' === $element['name'];
+                       }
+               );
+               unregister_block_type( 'my/block-with-styles' );
+
+               $this->assertSame( $core_palette, isset( $settings['color']['palette']['default'] ), $core_palette_text );
+               $this->assertSame( $block_styles, count( $styles ) === 1, $block_styles_text );
+               $this->assertSame( $theme_palette, isset( $settings['color']['palette']['theme'] ), $theme_palette_text );
+               $this->assertSame( $user_palette, isset( $settings['color']['palette']['custom'] ), $user_palette_text );
+
+       }
+
+       /**
+        * Data provider.
+        *
+        * @return array[]
+        */
+       public function data_get_merged_data_returns_origin() {
+               return array(
+                       'origin_default' => array(
+                               'origin'             => 'default',
+                               'core_palette'       => true,
+                               'core_palette_text'  => 'Core palette must be present',
+                               'block_styles'       => false,
+                               'block_styles_text'  => 'Block styles should not be present',
+                               'theme_palette'      => false,
+                               'theme_palette_text' => 'Theme palette should not be present',
+                               'user_palette'       => false,
+                               'user_palette_text'  => 'User palette should not be present',
+                       ),
+                       'origin_blocks'  => array(
+                               'origin'             => 'blocks',
+                               'core_palette'       => true,
+                               'core_palette_text'  => 'Core palette must be present',
+                               'block_styles'       => true,
+                               'block_styles_text'  => 'Block styles must be present',
+                               'theme_palette'      => false,
+                               'theme_palette_text' => 'Theme palette should not be present',
+                               'user_palette'       => false,
+                               'user_palette_text'  => 'User palette should not be present',
+                       ),
+                       'origin_theme'   => array(
+                               'origin'             => 'theme',
+                               'core_palette'       => true,
+                               'core_palette_text'  => 'Core palette must be present',
+                               'block_styles'       => true,
+                               'block_styles_text'  => 'Block styles must be present',
+                               'theme_palette'      => true,
+                               'theme_palette_text' => 'Theme palette must be present',
+                               'user_palette'       => false,
+                               'user_palette_text'  => 'User palette should not be present',
+                       ),
+                       'origin_custom'  => array(
+                               'origin'             => 'custom',
+                               'core_palette'       => true,
+                               'core_palette_text'  => 'Core palette must be present',
+                               'block_styles'       => true,
+                               'block_styles_text'  => 'Block styles must be present',
+                               'theme_palette'      => true,
+                               'theme_palette_text' => 'Theme palette must be present',
+                               'user_palette'       => true,
+                               'user_palette_text'  => 'User palette must be present',
+                       ),
+               );
+       }
+
+       /**
+        * Tests that get_style_variations returns all variations, including parent theme variations if the theme is a child,
+        * and that the child variation overwrites the parent variation of the same name.
+        *
+        * @ticket 57545
+        *
+        * @covers WP_Theme_JSON_Resolver::get_style_variations
+        **/
+       public function test_get_style_variations_returns_all_variations() {
+               // Switch to a child theme.
+               switch_theme( 'block-theme-child' );
+               wp_set_current_user( self::$administrator_id );
+
+               $actual_settings   = WP_Theme_JSON_Resolver::get_style_variations();
+               $expected_settings = array(
+                       array(
+                               'version'  => 2,
+                               'title'    => 'variation-b',
+                               'settings' => array(
+                                       'blocks' => array(
+                                               'core/post-title' => array(
+                                                       'color' => array(
+                                                               'palette' => array(
+                                                                       'theme' => array(
+                                                                               array(
+                                                                                       'slug'  => 'dark',
+                                                                                       'name'  => 'Dark',
+                                                                                       'color' => '#010101',
+                                                                               ),
+                                                                       ),
+                                                               ),
+                                                       ),
+                                               ),
+                                       ),
+                               ),
+                       ),
+                       array(
+                               'version'  => 2,
+                               'title'    => 'Block theme variation',
+                               'settings' => array(
+                                       'color' => array(
+                                               'palette' => array(
+                                                       'theme' => array(
+                                                               array(
+                                                                       'slug'  => 'foreground',
+                                                                       'name'  => 'Foreground',
+                                                                       'color' => '#3F67C6',
+                                                               ),
+                                                       ),
+                                               ),
+                                       ),
+                               ),
+                               'styles'   => array(
+                                       'blocks' => array(
+                                               'core/post-title' => array(
+                                                       'typography' => array(
+                                                               'fontWeight' => '700',
+                                                       ),
+                                               ),
+                                       ),
+                               ),
+                       ),
+               );
+
+               wp_recursive_ksort( $actual_settings );
+               wp_recursive_ksort( $expected_settings );
+
+               $this->assertSame(
+                       $expected_settings,
+                       $actual_settings
+               );
+       }
</ins><span class="cx" style="display: block; padding: 0 10px"> }
</span></span></pre>
</div>
</div>

</body>
</html>