<!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>[54805] branches/6.1/tests/phpunit/tests/theme/wpThemeJson.php: Editor: Improve performance of `WP_Theme_JSON` class by reducing usage of expensive array functions.</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/54805">54805</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/54805","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-11 02:05:56 +0000 (Fri, 11 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: Improve performance of `WP_Theme_JSON` class by reducing usage of expensive array functions.

In many scenarios array functions are more expensive than using simpler `for` or `foreach` loops.

This changeset results in roughly 4% faster `wp_head` execution time for both block themes and classic themes. While this may seem like a small win, it is a worthwhile enhancement and only one part of several other little performance tweaks which are being worked on to improve performance of `theme.json` parsing further.

Props aristath, desrosj, jrf, spacedmonkey.
Merges <a href="https://core.trac.wordpress.org/changeset/54804">[54804]</a> to the 6.1 branch.
Fixes <a href="https://core.trac.wordpress.org/ticket/56974">#56974</a>.
See <a href="https://core.trac.wordpress.org/ticket/57067">#57067</a>.</pre>

<h3>Modified Paths</h3>
<ul>
<li><a href="#branches61srcwpincludesclasswpthemejsonphp">branches/6.1/src/wp-includes/class-wp-theme-json.php</a></li>
<li><a href="#branches61testsphpunitteststhemewpThemeJsonphp">branches/6.1/tests/phpunit/tests/theme/wpThemeJson.php</a></li>
</ul>

<h3>Property Changed</h3>
<ul>
<li><a href="#branches61">branches/6.1/</a></li>
</ul>

</div>
<div id="patch">
<h3>Diff</h3>
<span class="cx" style="display: block; padding: 0 10px">Index: branches/6.1
</span><span class="cx" style="display: block; padding: 0 10px">===================================================================
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">--- branches/6.1 2022-11-11 01:48:05 UTC (rev 54804)
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+++ branches/6.1  2022-11-11 02:05:56 UTC (rev 54805)
</ins><a id="branches61"></a>
<div class="propset"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Property changes: branches/6.1</h4>
<pre class="diff"><span>
</span></pre></div>
<a id="svnmergeinfo"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: svn:mergeinfo</h4></div>
<span class="cx" style="display: block; padding: 0 10px"> /branches/5.0:43681-43682,43684-43688,43719-43720,43723,43726-43727,43729-43731,43734-43744,43747,43751-43754,43758,43760-43765,43767-43770,43772,43774-43781,43783,43785,43790-43806,43808-43821,43825,43828,43830-43834,43836-43843,43846-43863,43867-43889,43891-43894,43897-43905,43908-43909,43911-43929,43931-43942,43946-43947,43949-43956,43959-43964,43967-43969,43988,43994,44014,44017,44047,44183,44185,44187-44206,44208-44213,44231-44232,44235,44248,44284,44287-44288
</span><span class="cx" style="display: block; padding: 0 10px"> /branches/5.5:49373-49379,49381
</span><span class="cx" style="display: block; padding: 0 10px"> /branches/5.8:51889
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-/trunk:54645-54646,54649,54652,54660,54662,54669,54674,54678,54686,54690,54693,54698,54706,54724,54729-54730,54733,54745,54751,54766,54768-54771,54774,54777,54782,54785-54789,54797,54799
</del><span class="cx" style="display: block; padding: 0 10px">\ No newline at end of property
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+/trunk:54645-54646,54649,54652,54660,54662,54669,54674,54678,54686,54690,54693,54698,54706,54724,54729-54730,54733,54745,54751,54766,54768-54771,54774,54777,54782,54785-54789,54797,54799,54804
</ins><span class="cx" style="display: block; padding: 0 10px">\ No newline at end of property
</span><a id="branches61srcwpincludesclasswpthemejsonphp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: branches/6.1/src/wp-includes/class-wp-theme-json.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- branches/6.1/src/wp-includes/class-wp-theme-json.php      2022-11-11 01:48:05 UTC (rev 54804)
+++ branches/6.1/src/wp-includes/class-wp-theme-json.php        2022-11-11 02:05:56 UTC (rev 54805)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -461,6 +461,8 @@
</span><span class="cx" style="display: block; padding: 0 10px">        public static function get_element_class_name( $element ) {
</span><span class="cx" style="display: block; padding: 0 10px">                $class_name = '';
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                // TODO: Replace array_key_exists() with isset() check once WordPress drops
+               // support for PHP 5.6. See https://core.trac.wordpress.org/ticket/57067.
</ins><span class="cx" style="display: block; padding: 0 10px">                 if ( array_key_exists( $element, static::__EXPERIMENTAL_ELEMENT_CLASS_NAMES ) ) {
</span><span class="cx" style="display: block; padding: 0 10px">                        $class_name = static::__EXPERIMENTAL_ELEMENT_CLASS_NAMES[ $element ];
</span><span class="cx" style="display: block; padding: 0 10px">                }
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -519,7 +521,10 @@
</span><span class="cx" style="display: block; padding: 0 10px">                $nodes = static::get_setting_nodes( $this->theme_json );
</span><span class="cx" style="display: block; padding: 0 10px">                foreach ( $nodes as $node ) {
</span><span class="cx" style="display: block; padding: 0 10px">                        foreach ( static::PRESETS_METADATA as $preset_metadata ) {
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                                $path   = array_merge( $node['path'], $preset_metadata['path'] );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                         $path = $node['path'];
+                               foreach ( $preset_metadata['path'] as $subpath ) {
+                                       $path[] = $subpath;
+                               }
</ins><span class="cx" style="display: block; padding: 0 10px">                                 $preset = _wp_array_get( $this->theme_json, $path, null );
</span><span class="cx" style="display: block; padding: 0 10px">                                if ( null !== $preset ) {
</span><span class="cx" style="display: block; padding: 0 10px">                                        // If the preset is not already keyed by origin.
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -608,6 +613,7 @@
</span><span class="cx" style="display: block; padding: 0 10px">                 */
</span><span class="cx" style="display: block; padding: 0 10px">                $styles_non_top_level = static::VALID_STYLES;
</span><span class="cx" style="display: block; padding: 0 10px">                foreach ( array_keys( $styles_non_top_level ) as $section ) {
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                        // array_key_exists() needs to be used instead of isset() because the value can be null.
</ins><span class="cx" style="display: block; padding: 0 10px">                         if ( array_key_exists( $section, $styles_non_top_level ) && is_array( $styles_non_top_level[ $section ] ) ) {
</span><span class="cx" style="display: block; padding: 0 10px">                                foreach ( array_keys( $styles_non_top_level[ $section ] ) as $prop ) {
</span><span class="cx" style="display: block; padding: 0 10px">                                        if ( 'top' === $styles_non_top_level[ $section ][ $prop ] ) {
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -631,6 +637,8 @@
</span><span class="cx" style="display: block; padding: 0 10px">                foreach ( $valid_element_names as $element ) {
</span><span class="cx" style="display: block; padding: 0 10px">                        $schema_styles_elements[ $element ] = $styles_non_top_level;
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                        // TODO: Replace array_key_exists() with isset() check once WordPress drops
+                       // support for PHP 5.6. See https://core.trac.wordpress.org/ticket/57067.
</ins><span class="cx" style="display: block; padding: 0 10px">                         if ( array_key_exists( $element, static::VALID_ELEMENT_PSEUDO_SELECTORS ) ) {
</span><span class="cx" style="display: block; padding: 0 10px">                                foreach ( static::VALID_ELEMENT_PSEUDO_SELECTORS[ $element ] as $pseudo_selector ) {
</span><span class="cx" style="display: block; padding: 0 10px">                                        $schema_styles_elements[ $element ][ $pseudo_selector ] = $styles_non_top_level;
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -1273,8 +1281,12 @@
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">                        $selector = $metadata['selector'];
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                        $node         = _wp_array_get( $this->theme_json, $metadata['path'], array() );
-                       $declarations = array_merge( static::compute_preset_vars( $node, $origins ), static::compute_theme_vars( $node ) );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                 $node                    = _wp_array_get( $this->theme_json, $metadata['path'], array() );
+                       $declarations            = static::compute_preset_vars( $node, $origins );
+                       $theme_vars_declarations = static::compute_theme_vars( $node );
+                       foreach ( $theme_vars_declarations as $theme_vars_declaration ) {
+                               $declarations[] = $theme_vars_declaration;
+                       }
</ins><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">                        $stylesheet .= static::to_ruleset( $selector, $declarations );
</span><span class="cx" style="display: block; padding: 0 10px">                }
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -1601,11 +1613,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">                        if ( is_array( $value ) ) {
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                                $new_prefix = $new_key . $token;
-                               $result     = array_merge(
-                                       $result,
-                                       static::flatten_tree( $value, $new_prefix, $token )
-                               );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                         $new_prefix        = $new_key . $token;
+                               $flattened_subtree = static::flatten_tree( $value, $new_prefix, $token );
+                               foreach ( $flattened_subtree as $subtree_key => $subtree_value ) {
+                                       $result[ $subtree_key ] = $subtree_value;
+                               }
</ins><span class="cx" style="display: block; padding: 0 10px">                         } else {
</span><span class="cx" style="display: block; padding: 0 10px">                                $result[ $new_key ] = $value;
</span><span class="cx" style="display: block; padding: 0 10px">                        }
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -1667,6 +1679,8 @@
</span><span class="cx" style="display: block; padding: 0 10px">                        if ( is_array( $value_path ) ) {
</span><span class="cx" style="display: block; padding: 0 10px">                                $path_string = implode( '.', $value_path );
</span><span class="cx" style="display: block; padding: 0 10px">                                if (
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                                        // TODO: Replace array_key_exists() with isset() check once WordPress drops
+                                       // support for PHP 5.6. See https://core.trac.wordpress.org/ticket/57067.
</ins><span class="cx" style="display: block; padding: 0 10px">                                         array_key_exists( $path_string, static::PROTECTED_PROPERTIES ) &&
</span><span class="cx" style="display: block; padding: 0 10px">                                        _wp_array_get( $settings, static::PROTECTED_PROPERTIES[ $path_string ], null ) === null
</span><span class="cx" style="display: block; padding: 0 10px">                                ) {
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -1742,7 +1756,7 @@
</span><span class="cx" style="display: block; padding: 0 10px">                 * where the values is an array with a "ref" key, pointing to a path.
</span><span class="cx" style="display: block; padding: 0 10px">                 * For example: { "ref": "style.color.background" } => "#fff".
</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_array( $value ) && array_key_exists( 'ref', $value ) ) {
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         if ( is_array( $value ) && isset( $value['ref'] ) ) {
</ins><span class="cx" style="display: block; padding: 0 10px">                         $value_path = explode( '.', $value['ref'] );
</span><span class="cx" style="display: block; padding: 0 10px">                        $ref_value  = _wp_array_get( $theme_json, $value_path );
</span><span class="cx" style="display: block; padding: 0 10px">                        // Only use the ref value if we find anything.
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -1750,7 +1764,7 @@
</span><span class="cx" style="display: block; padding: 0 10px">                                $value = $ref_value;
</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">-                        if ( is_array( $ref_value ) && array_key_exists( 'ref', $ref_value ) ) {
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                 if ( is_array( $ref_value ) && isset( $ref_value['ref'] ) ) {
</ins><span class="cx" style="display: block; padding: 0 10px">                                 $path_string      = json_encode( $path );
</span><span class="cx" style="display: block; padding: 0 10px">                                $ref_value_string = json_encode( $ref_value );
</span><span class="cx" style="display: block; padding: 0 10px">                                _doing_it_wrong(
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -1886,6 +1900,8 @@
</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">                                // Handle any pseudo selectors for the element.
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                                // TODO: Replace array_key_exists() with isset() check once WordPress drops
+                               // support for PHP 5.6. See https://core.trac.wordpress.org/ticket/57067.
</ins><span class="cx" style="display: block; padding: 0 10px">                                 if ( array_key_exists( $element, static::VALID_ELEMENT_PSEUDO_SELECTORS ) ) {
</span><span class="cx" style="display: block; padding: 0 10px">                                        foreach ( static::VALID_ELEMENT_PSEUDO_SELECTORS[ $element ] as $pseudo_selector ) {
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -1905,7 +1921,10 @@
</span><span class="cx" style="display: block; padding: 0 10px">                        return $nodes;
</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">-                $nodes = array_merge( $nodes, static::get_block_nodes( $theme_json ) );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         $block_nodes = static::get_block_nodes( $theme_json );
+               foreach ( $block_nodes as $block_node ) {
+                       $nodes[] = $block_node;
+               }
</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 list of style nodes with metadata.
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -1982,6 +2001,8 @@
</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">                                        // Handle any pseudo selectors for the element.
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                                        // TODO: Replace array_key_exists() with isset() check once WordPress drops
+                                       // support for PHP 5.6. See https://core.trac.wordpress.org/ticket/57067.
</ins><span class="cx" style="display: block; padding: 0 10px">                                         if ( array_key_exists( $element, static::VALID_ELEMENT_PSEUDO_SELECTORS ) ) {
</span><span class="cx" style="display: block; padding: 0 10px">                                                foreach ( static::VALID_ELEMENT_PSEUDO_SELECTORS[ $element ] as $pseudo_selector ) {
</span><span class="cx" style="display: block; padding: 0 10px">                                                        if ( isset( $theme_json['styles']['blocks'][ $name ]['elements'][ $element ][ $pseudo_selector ] ) ) {
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -2035,7 +2056,9 @@
</span><span class="cx" style="display: block; padding: 0 10px">                                        // the feature selector. This may occur when multiple block
</span><span class="cx" style="display: block; padding: 0 10px">                                        // support features use the same custom selector.
</span><span class="cx" style="display: block; padding: 0 10px">                                        if ( isset( $feature_declarations[ $feature_selector ] ) ) {
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                                                $feature_declarations[ $feature_selector ] = array_merge( $feature_declarations[ $feature_selector ], $new_feature_declarations );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                                         foreach ( $new_feature_declarations as $new_feature_declaration ) {
+                                                       $feature_declarations[ $feature_selector ][] = $feature_declaration;
+                                               }
</ins><span class="cx" style="display: block; padding: 0 10px">                                         } else {
</span><span class="cx" style="display: block; padding: 0 10px">                                                $feature_declarations[ $feature_selector ] = $new_feature_declarations;
</span><span class="cx" style="display: block; padding: 0 10px">                                        }
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -2059,6 +2082,8 @@
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">                $element_pseudo_allowed = array();
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                // TODO: Replace array_key_exists() with isset() check once WordPress drops
+               // support for PHP 5.6. See https://core.trac.wordpress.org/ticket/57067.
</ins><span class="cx" style="display: block; padding: 0 10px">                 if ( array_key_exists( $current_element, static::VALID_ELEMENT_PSEUDO_SELECTORS ) ) {
</span><span class="cx" style="display: block; padding: 0 10px">                        $element_pseudo_allowed = static::VALID_ELEMENT_PSEUDO_SELECTORS[ $current_element ];
</span><span class="cx" style="display: block; padding: 0 10px">                }
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -2084,6 +2109,8 @@
</span><span class="cx" style="display: block; padding: 0 10px">                 * Otherwise just compute the styles for the default selector as normal.
</span><span class="cx" style="display: block; padding: 0 10px">                 */
</span><span class="cx" style="display: block; padding: 0 10px">                if ( $pseudo_selector && isset( $node[ $pseudo_selector ] ) &&
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                        // TODO: Replace array_key_exists() with isset() check once WordPress drops
+                       // support for PHP 5.6. See https://core.trac.wordpress.org/ticket/57067.
</ins><span class="cx" style="display: block; padding: 0 10px">                         array_key_exists( $current_element, static::VALID_ELEMENT_PSEUDO_SELECTORS )
</span><span class="cx" style="display: block; padding: 0 10px">                        && in_array( $pseudo_selector, static::VALID_ELEMENT_PSEUDO_SELECTORS[ $current_element ], true )
</span><span class="cx" style="display: block; padding: 0 10px">                ) {
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -2283,11 +2310,11 @@
</span><span class="cx" style="display: block; padding: 0 10px">                $nodes        = static::get_setting_nodes( $incoming_data );
</span><span class="cx" style="display: block; padding: 0 10px">                $slugs_global = static::get_default_slugs( $this->theme_json, array( 'settings' ) );
</span><span class="cx" style="display: block; padding: 0 10px">                foreach ( $nodes as $node ) {
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                        $slugs_node = static::get_default_slugs( $this->theme_json, $node['path'] );
-                       $slugs      = array_merge_recursive( $slugs_global, $slugs_node );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                 // Replace the spacing.units.
+                       $path   = $node['path'];
+                       $path[] = 'spacing';
+                       $path[] = 'units';
</ins><span class="cx" style="display: block; padding: 0 10px"> 
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                        // Replace the spacing.units.
-                       $path    = array_merge( $node['path'], array( 'spacing', 'units' ) );
</del><span class="cx" style="display: block; padding: 0 10px">                         $content = _wp_array_get( $incoming_data, $path, null );
</span><span class="cx" style="display: block; padding: 0 10px">                        if ( isset( $content ) ) {
</span><span class="cx" style="display: block; padding: 0 10px">                                _wp_array_set( $this->theme_json, $path, $content );
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -2298,19 +2325,25 @@
</span><span class="cx" style="display: block; padding: 0 10px">                                $override_preset = ! static::get_metadata_boolean( $this->theme_json['settings'], $preset['prevent_override'], true );
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">                                foreach ( static::VALID_ORIGINS as $origin ) {
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                                        $base_path = array_merge( $node['path'], $preset['path'] );
-                                       $path      = array_merge( $base_path, array( $origin ) );
-                                       $content   = _wp_array_get( $incoming_data, $path, null );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                                 $base_path = $node['path'];
+                                       foreach ( $preset['path'] as $leaf ) {
+                                               $base_path[] = $leaf;
+                                       }
+
+                                       $path   = $base_path;
+                                       $path[] = $origin;
+
+                                       $content = _wp_array_get( $incoming_data, $path, null );
</ins><span class="cx" style="display: block; padding: 0 10px">                                         if ( ! isset( $content ) ) {
</span><span class="cx" style="display: block; padding: 0 10px">                                                continue;
</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">                                        if ( 'theme' === $origin && $preset['use_default_names'] ) {
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                                                foreach ( $content as &$item ) {
-                                                       if ( ! array_key_exists( 'name', $item ) ) {
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                                         foreach ( $content as $key => $item ) {
+                                                       if ( ! isset( $item['name'] ) ) {
</ins><span class="cx" style="display: block; padding: 0 10px">                                                                 $name = static::get_name_from_defaults( $item['slug'], $base_path );
</span><span class="cx" style="display: block; padding: 0 10px">                                                                if ( null !== $name ) {
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                                                                        $item['name'] = $name;
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                                                                 $content[ $key ]['name'] = $name;
</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">@@ -2322,6 +2355,9 @@
</span><span class="cx" style="display: block; padding: 0 10px">                                        ) {
</span><span class="cx" style="display: block; padding: 0 10px">                                                _wp_array_set( $this->theme_json, $path, $content );
</span><span class="cx" style="display: block; padding: 0 10px">                                        } else {
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                                                $slugs_node = static::get_default_slugs( $this->theme_json, $node['path'] );
+                                               $slugs      = array_merge_recursive( $slugs_global, $slugs_node );
+
</ins><span class="cx" style="display: block; padding: 0 10px">                                                 $slugs_for_preset = _wp_array_get( $slugs, $preset['path'], array() );
</span><span class="cx" style="display: block; padding: 0 10px">                                                $content          = static::filter_slugs( $content, $slugs_for_preset );
</span><span class="cx" style="display: block; padding: 0 10px">                                                _wp_array_set( $this->theme_json, $path, $content );
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -2434,7 +2470,12 @@
</span><span class="cx" style="display: block; padding: 0 10px">                $slugs = array();
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">                foreach ( static::PRESETS_METADATA as $metadata ) {
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                        $path   = array_merge( $node_path, $metadata['path'], array( 'default' ) );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                 $path = $node_path;
+                       foreach ( $metadata['path'] as $leaf ) {
+                               $path[] = $leaf;
+                       }
+                       $path[] = 'default';
+
</ins><span class="cx" style="display: block; padding: 0 10px">                         $preset = _wp_array_get( $data, $path, null );
</span><span class="cx" style="display: block; padding: 0 10px">                        if ( ! isset( $preset ) ) {
</span><span class="cx" style="display: block; padding: 0 10px">                                continue;
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -2463,7 +2504,8 @@
</span><span class="cx" style="display: block; padding: 0 10px">         * @return string|null
</span><span class="cx" style="display: block; padding: 0 10px">         */
</span><span class="cx" style="display: block; padding: 0 10px">        protected function get_name_from_defaults( $slug, $base_path ) {
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                $path            = array_merge( $base_path, array( 'default' ) );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         $path            = $base_path;
+               $path[]          = 'default';
</ins><span class="cx" style="display: block; padding: 0 10px">                 $default_content = _wp_array_get( $this->theme_json, $path, null );
</span><span class="cx" style="display: block; padding: 0 10px">                if ( ! $default_content ) {
</span><span class="cx" style="display: block; padding: 0 10px">                        return null;
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -2539,6 +2581,8 @@
</span><span class="cx" style="display: block; padding: 0 10px">                         * $output is stripped of pseudo selectors. Re-add and process them
</span><span class="cx" style="display: block; padding: 0 10px">                         * or insecure styles here.
</span><span class="cx" style="display: block; padding: 0 10px">                         */
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                        // TODO: Replace array_key_exists() with isset() check once WordPress drops
+                       // support for PHP 5.6. See https://core.trac.wordpress.org/ticket/57067.
</ins><span class="cx" style="display: block; padding: 0 10px">                         if ( array_key_exists( $current_element, static::VALID_ELEMENT_PSEUDO_SELECTORS ) ) {
</span><span class="cx" style="display: block; padding: 0 10px">                                foreach ( static::VALID_ELEMENT_PSEUDO_SELECTORS[ $current_element ] as $pseudo_selector ) {
</span><span class="cx" style="display: block; padding: 0 10px">                                        if ( isset( $input[ $pseudo_selector ] ) ) {
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -2593,8 +2637,9 @@
</span><span class="cx" style="display: block; padding: 0 10px">                $output = array();
</span><span class="cx" style="display: block; padding: 0 10px">                foreach ( static::PRESETS_METADATA as $preset_metadata ) {
</span><span class="cx" style="display: block; padding: 0 10px">                        foreach ( static::VALID_ORIGINS as $origin ) {
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                                $path_with_origin = array_merge( $preset_metadata['path'], array( $origin ) );
-                               $presets          = _wp_array_get( $input, $path_with_origin, null );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                         $path_with_origin   = $preset_metadata['path'];
+                               $path_with_origin[] = $origin;
+                               $presets            = _wp_array_get( $input, $path_with_origin, null );
</ins><span class="cx" style="display: block; padding: 0 10px">                                 if ( null === $presets ) {
</span><span class="cx" style="display: block; padding: 0 10px">                                        continue;
</span><span class="cx" style="display: block; padding: 0 10px">                                }
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -2839,7 +2884,10 @@
</span><span class="cx" style="display: block; padding: 0 10px">                 */
</span><span class="cx" style="display: block; padding: 0 10px">                foreach ( $nodes as $node ) {
</span><span class="cx" style="display: block; padding: 0 10px">                        foreach ( static::PRESETS_METADATA as $preset_metadata ) {
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                                $path   = array_merge( $node['path'], $preset_metadata['path'] );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                         $path = $node['path'];
+                               foreach ( $preset_metadata['path'] as $preset_metadata_path ) {
+                                       $path[] = $preset_metadata_path;
+                               }
</ins><span class="cx" style="display: block; padding: 0 10px">                                 $preset = _wp_array_get( $output, $path, null );
</span><span class="cx" style="display: block; padding: 0 10px">                                if ( null === $preset ) {
</span><span class="cx" style="display: block; padding: 0 10px">                                        continue;
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -2873,7 +2921,10 @@
</span><span class="cx" style="display: block; padding: 0 10px">                foreach ( $nodes as $node ) {
</span><span class="cx" style="display: block; padding: 0 10px">                        $all_opt_ins_are_set = true;
</span><span class="cx" style="display: block; padding: 0 10px">                        foreach ( static::APPEARANCE_TOOLS_OPT_INS as $opt_in_path ) {
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                                $full_path = array_merge( $node['path'], $opt_in_path );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                         $full_path = $node['path'];
+                               foreach ( $opt_in_path as $opt_in_path_item ) {
+                                       $full_path[] = $opt_in_path_item;
+                               }
</ins><span class="cx" style="display: block; padding: 0 10px">                                 // Use "unset prop" as a marker instead of "null" because
</span><span class="cx" style="display: block; padding: 0 10px">                                // "null" can be a valid value for some props (e.g. blockGap).
</span><span class="cx" style="display: block; padding: 0 10px">                                $opt_in_value = _wp_array_get( $output, $full_path, 'unset prop' );
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -2884,9 +2935,14 @@
</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">                        if ( $all_opt_ins_are_set ) {
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                                _wp_array_set( $output, array_merge( $node['path'], array( 'appearanceTools' ) ), true );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                         $node_path_with_appearance_tools   = $node['path'];
+                               $node_path_with_appearance_tools[] = 'appearanceTools';
+                               _wp_array_set( $output, $node_path_with_appearance_tools, true );
</ins><span class="cx" style="display: block; padding: 0 10px">                                 foreach ( static::APPEARANCE_TOOLS_OPT_INS as $opt_in_path ) {
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                                        $full_path = array_merge( $node['path'], $opt_in_path );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                                 $full_path = $node['path'];
+                                       foreach ( $opt_in_path as $opt_in_path_item ) {
+                                               $full_path[] = $opt_in_path_item;
+                                       }
</ins><span class="cx" style="display: block; padding: 0 10px">                                         // Use "unset prop" as a marker instead of "null" because
</span><span class="cx" style="display: block; padding: 0 10px">                                        // "null" can be a valid value for some props (e.g. blockGap).
</span><span class="cx" style="display: block; padding: 0 10px">                                        $opt_in_value = _wp_array_get( $output, $full_path, 'unset prop' );
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -3037,7 +3093,10 @@
</span><span class="cx" style="display: block; padding: 0 10px">                        $slug += 10;
</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">-                $spacing_sizes = array_merge( $below_sizes, $above_sizes );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         $spacing_sizes = $below_sizes;
+               foreach ( $above_sizes as $above_sizes_item ) {
+                       $spacing_sizes[] = $above_sizes_item;
+               }
</ins><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">                // If there are 7 or less steps in the scale revert to numbers for labels instead of t-shirt sizes.
</span><span class="cx" style="display: block; padding: 0 10px">                if ( $spacing_scale['steps'] <= 7 ) {
</span></span></pre></div>
<a id="branches61testsphpunitteststhemewpThemeJsonphp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: branches/6.1/tests/phpunit/tests/theme/wpThemeJson.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- branches/6.1/tests/phpunit/tests/theme/wpThemeJson.php    2022-11-11 01:48:05 UTC (rev 54804)
+++ branches/6.1/tests/phpunit/tests/theme/wpThemeJson.php      2022-11-11 02:05:56 UTC (rev 54805)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -9,8 +9,9 @@
</span><span class="cx" style="display: block; padding: 0 10px">  * @since 5.8.0
</span><span class="cx" style="display: block; padding: 0 10px">  *
</span><span class="cx" style="display: block; padding: 0 10px">  * @group themes
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ *
+ * @covers WP_Theme_JSON
</ins><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"> class Tests_Theme_wpThemeJson extends WP_UnitTestCase {
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">        /**
</span></span></pre>
</div>
</div>

</body>
</html>