<!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>[55959] trunk: `wp_get_global_styles`: return the standard format for CSS Custom Properties.</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/55959">55959</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/55959","name":"Review Commit"}}</script></dd>
<dt style="float: left; width: 6em; font-weight: bold">Author</dt> <dd>oandregal</dd>
<dt style="float: left; width: 6em; font-weight: bold">Date</dt> <dd>2023-06-21 07:54:01 +0000 (Wed, 21 Jun 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'>`wp_get_global_styles`: return the standard format for CSS Custom Properties.

Props samnajian, hellofromtonya, isabel_brison.
Fixes <a href="https://core.trac.wordpress.org/ticket/58467">#58467</a>.</pre>

<h3>Modified Paths</h3>
<ul>
<li><a href="#trunksrcwpincludesclasswpthemejsonphp">trunk/src/wp-includes/class-wp-theme-json.php</a></li>
<li><a href="#trunksrcwpincludesglobalstylesandsettingsphp">trunk/src/wp-includes/global-styles-and-settings.php</a></li>
<li><a href="#trunktestsphpunitteststhemewpThemeJsonphp">trunk/tests/phpunit/tests/theme/wpThemeJson.php</a></li>
</ul>

</div>
<div id="patch">
<h3>Diff</h3>
<a id="trunksrcwpincludesclasswpthemejsonphp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/src/wp-includes/class-wp-theme-json.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/src/wp-includes/class-wp-theme-json.php     2023-06-21 06:56:18 UTC (rev 55958)
+++ trunk/src/wp-includes/class-wp-theme-json.php       2023-06-21 07:54:01 UTC (rev 55959)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -779,7 +779,7 @@
</span><span class="cx" style="display: block; padding: 0 10px">                        if ( empty( $result ) ) {
</span><span class="cx" style="display: block; padding: 0 10px">                                unset( $output[ $subtree ] );
</span><span class="cx" style="display: block; padding: 0 10px">                        } else {
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                                $output[ $subtree ] = $result;
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                         $output[ $subtree ] = static::resolve_custom_css_format( $result );
</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">@@ -1958,10 +1958,6 @@
</span><span class="cx" style="display: block; padding: 0 10px">        /**
</span><span class="cx" style="display: block; padding: 0 10px">         * Returns the style property for the given path.
</span><span class="cx" style="display: block; padding: 0 10px">         *
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-         * It also converts CSS Custom Property stored as
-        * "var:preset|color|secondary" to the form
-        * "--wp--preset--color--secondary".
-        *
</del><span class="cx" style="display: block; padding: 0 10px">          * It also converts references to a path to the value
</span><span class="cx" style="display: block; padding: 0 10px">         * stored at that location, e.g.
</span><span class="cx" style="display: block; padding: 0 10px">         * { "ref": "style.color.background" } => "#fff".
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -1969,6 +1965,10 @@
</span><span class="cx" style="display: block; padding: 0 10px">         * @since 5.8.0
</span><span class="cx" style="display: block; padding: 0 10px">         * @since 5.9.0 Added support for values of array type, which are returned as is.
</span><span class="cx" style="display: block; padding: 0 10px">         * @since 6.1.0 Added the `$theme_json` parameter.
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         * @since 6.3.0 It no longer converts the internal format "var:preset|color|secondary"
+        *              to the standard form "--wp--preset--color--secondary".
+        *              This is already done by the sanitize method,
+        *              so every property will be in the standard form.
</ins><span class="cx" style="display: block; padding: 0 10px">          *
</span><span class="cx" style="display: block; padding: 0 10px">         * @param array $styles Styles subtree.
</span><span class="cx" style="display: block; padding: 0 10px">         * @param array $path   Which property to process.
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -2018,20 +2018,6 @@
</span><span class="cx" style="display: block; padding: 0 10px">                        return $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">-                // Convert custom CSS properties.
-               $prefix     = 'var:';
-               $prefix_len = strlen( $prefix );
-               $token_in   = '|';
-               $token_out  = '--';
-               if ( 0 === strncmp( $value, $prefix, $prefix_len ) ) {
-                       $unwrapped_name = str_replace(
-                               $token_in,
-                               $token_out,
-                               substr( $value, $prefix_len )
-                       );
-                       $value          = "var(--wp--$unwrapped_name)";
-               }
-
</del><span class="cx" style="display: block; padding: 0 10px">                 return $value;
</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">@@ -3488,4 +3474,52 @@
</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, array( 'settings', 'spacing', 'spacingSizes', 'default' ), $spacing_sizes );
</span><span class="cx" style="display: block; padding: 0 10px">        }
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+
+       /**
+        * This is used to convert the internal representation of variables to the CSS representation.
+        * For example, `var:preset|color|vivid-green-cyan` becomes `var(--wp--preset--color--vivid-green-cyan)`.
+        *
+        * @since 6.3.0
+        * @param string $value The variable such as var:preset|color|vivid-green-cyan to convert.
+        * @return string The converted variable.
+        */
+       private static function convert_custom_properties( $value ) {
+               $prefix     = 'var:';
+               $prefix_len = strlen( $prefix );
+               $token_in   = '|';
+               $token_out  = '--';
+               if ( 0 === strpos( $value, $prefix ) ) {
+                       $unwrapped_name = str_replace(
+                               $token_in,
+                               $token_out,
+                               substr( $value, $prefix_len )
+                       );
+                       $value          = "var(--wp--$unwrapped_name)";
+               }
+
+               return $value;
+       }
+
+       /**
+        * Given a tree, converts the internal representation of variables to the CSS representation.
+        * It is recursive and modifies the input in-place.
+        *
+        * @since 6.3.0
+        * @param array $tree   Input to process.
+        * @return array The modified $tree.
+        */
+       private static function resolve_custom_css_format( $tree ) {
+               $prefix = 'var:';
+
+               foreach ( $tree as $key => $data ) {
+                       if ( is_string( $data ) && 0 === strpos( $data, $prefix ) ) {
+                               $tree[ $key ] = self::convert_custom_properties( $data );
+                       } elseif ( is_array( $data ) ) {
+                               $tree[ $key ] = self::resolve_custom_css_format( $data );
+                       }
+               }
+
+               return $tree;
+       }
+
</ins><span class="cx" style="display: block; padding: 0 10px"> }
</span></span></pre></div>
<a id="trunksrcwpincludesglobalstylesandsettingsphp"></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/global-styles-and-settings.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/src/wp-includes/global-styles-and-settings.php      2023-06-21 06:56:18 UTC (rev 55958)
+++ trunk/src/wp-includes/global-styles-and-settings.php        2023-06-21 07:54:01 UTC (rev 55959)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -92,6 +92,8 @@
</span><span class="cx" style="display: block; padding: 0 10px">  * Gets the styles resulting of merging core, theme, and user data.
</span><span class="cx" style="display: block; padding: 0 10px">  *
</span><span class="cx" style="display: block; padding: 0 10px">  * @since 5.9.0
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * @since 6.3.0 the internal format "var:preset|color|secondary" is always resolved
+ *              to the standard form "var(--wp--preset--font-size--small)".
</ins><span class="cx" style="display: block; padding: 0 10px">  *
</span><span class="cx" style="display: block; padding: 0 10px">  * @param array $path    Path to the specific style to retrieve. Optional.
</span><span class="cx" style="display: block; padding: 0 10px">  *                       If empty, will return all styles.
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -115,7 +117,6 @@
</span><span class="cx" style="display: block; padding: 0 10px">        if ( isset( $context['origin'] ) && 'base' === $context['origin'] ) {
</span><span class="cx" style="display: block; padding: 0 10px">                $origin = 'theme';
</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">         $styles = WP_Theme_JSON_Resolver::get_merged_data( $origin )->get_raw_data()['styles'];
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">        return _wp_array_get( $styles, $path, $styles );
</span></span></pre></div>
<a id="trunktestsphpunitteststhemewpThemeJsonphp"></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/wpThemeJson.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/tests/phpunit/tests/theme/wpThemeJson.php   2023-06-21 06:56:18 UTC (rev 55958)
+++ trunk/tests/phpunit/tests/theme/wpThemeJson.php     2023-06-21 07:54:01 UTC (rev 55959)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -1972,13 +1972,13 @@
</span><span class="cx" style="display: block; padding: 0 10px">                        'version' => WP_Theme_JSON::LATEST_SCHEMA,
</span><span class="cx" style="display: block; padding: 0 10px">                        'styles'  => array(
</span><span class="cx" style="display: block; padding: 0 10px">                                'color'    => array(
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                                        'text' => 'var:preset|color|dark-red',
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                                 'text' => 'var(--wp--preset--color--dark-red)',
</ins><span class="cx" style="display: block; padding: 0 10px">                                 ),
</span><span class="cx" style="display: block; padding: 0 10px">                                'elements' => array(
</span><span class="cx" style="display: block; padding: 0 10px">                                        'link' => array(
</span><span class="cx" style="display: block; padding: 0 10px">                                                'color' => array(
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                                                        'text'       => 'var:preset|color|dark-pink',
-                                                       'background' => 'var:preset|color|dark-red',
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                                                 'text'       => 'var(--wp--preset--color--dark-pink)',
+                                                       'background' => 'var(--wp--preset--color--dark-red)',
</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">@@ -1985,17 +1985,17 @@
</span><span class="cx" style="display: block; padding: 0 10px">                                'blocks'   => array(
</span><span class="cx" style="display: block; padding: 0 10px">                                        'core/image' => array(
</span><span class="cx" style="display: block; padding: 0 10px">                                                'filter' => array(
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                                                        'duotone' => 'var:preset|duotone|blue-red',
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                                                 'duotone' => 'var(--wp--preset--duotone--blue-red)',
</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">                                        'core/group' => array(
</span><span class="cx" style="display: block; padding: 0 10px">                                                'color'    => array(
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                                                        'text' => 'var:preset|color|dark-gray',
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                                                 'text' => 'var(--wp--preset--color--dark-gray)',
</ins><span class="cx" style="display: block; padding: 0 10px">                                                 ),
</span><span class="cx" style="display: block; padding: 0 10px">                                                'elements' => array(
</span><span class="cx" style="display: block; padding: 0 10px">                                                        'link' => array(
</span><span class="cx" style="display: block; padding: 0 10px">                                                                'color' => array(
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                                                                        'text' => 'var:preset|color|dark-pink',
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                                                                 'text' => 'var(--wp--preset--color--dark-pink)',
</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">@@ -4706,4 +4706,69 @@
</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">+
+       public function test_internal_syntax_is_converted_to_css_variables() {
+               $result = new WP_Theme_JSON(
+                       array(
+                               'version' => WP_Theme_JSON::LATEST_SCHEMA,
+                               'styles'  => array(
+                                       'color'    => array(
+                                               'background' => 'var:preset|color|primary',
+                                               'text'       => 'var(--wp--preset--color--secondary)',
+                                       ),
+                                       'elements' => array(
+                                               'link' => array(
+                                                       'color' => array(
+                                                               'background' => 'var:preset|color|pri',
+                                                               'text'       => 'var(--wp--preset--color--sec)',
+                                                       ),
+                                               ),
+                                       ),
+                                       'blocks'   => array(
+                                               'core/post-terms' => array(
+                                                       'typography' => array( 'fontSize' => 'var(--wp--preset--font-size--small)' ),
+                                                       'color'      => array( 'background' => 'var:preset|color|secondary' ),
+                                               ),
+                                               'core/navigation' => array(
+                                                       'elements' => array(
+                                                               'link' => array(
+                                                                       'color' => array(
+                                                                               'background' => 'var:preset|color|p',
+                                                                               'text'       => 'var(--wp--preset--color--s)',
+                                                                       ),
+                                                               ),
+                                                       ),
+                                               ),
+                                               'core/quote'      => array(
+                                                       'typography' => array( 'fontSize' => 'var(--wp--preset--font-size--d)' ),
+                                                       'color'      => array( 'background' => 'var:preset|color|d' ),
+                                                       'variations' => array(
+                                                               'plain' => array(
+                                                                       'typography' => array( 'fontSize' => 'var(--wp--preset--font-size--s)' ),
+                                                                       'color'      => array( 'background' => 'var:preset|color|s' ),
+                                                               ),
+                                                       ),
+                                               ),
+                                       ),
+                               ),
+                       )
+               );
+               $styles = $result->get_raw_data()['styles'];
+
+               $this->assertEquals( 'var(--wp--preset--color--primary)', $styles['color']['background'], 'Top level: Assert the originally correct values are still correct.' );
+               $this->assertEquals( 'var(--wp--preset--color--secondary)', $styles['color']['text'], 'Top level: Assert the originally correct values are still correct.' );
+
+               $this->assertEquals( 'var(--wp--preset--color--pri)', $styles['elements']['link']['color']['background'], 'Element top level: Assert the originally correct values are still correct.' );
+               $this->assertEquals( 'var(--wp--preset--color--sec)', $styles['elements']['link']['color']['text'], 'Element top level: Assert the originally correct values are still correct.' );
+
+               $this->assertEquals( 'var(--wp--preset--font-size--small)', $styles['blocks']['core/post-terms']['typography']['fontSize'], 'Top block level: Assert the originally correct values are still correct.' );
+               $this->assertEquals( 'var(--wp--preset--color--secondary)', $styles['blocks']['core/post-terms']['color']['background'], 'Top block level: Assert the internal variables are convert to CSS custom variables.' );
+
+               $this->assertEquals( 'var(--wp--preset--color--p)', $styles['blocks']['core/navigation']['elements']['link']['color']['background'], 'Elements block level: Assert the originally correct values are still correct.' );
+               $this->assertEquals( 'var(--wp--preset--color--s)', $styles['blocks']['core/navigation']['elements']['link']['color']['text'], 'Elements block level: Assert the originally correct values are still correct.' );
+
+               $this->assertEquals( 'var(--wp--preset--font-size--s)', $styles['blocks']['core/quote']['variations']['plain']['typography']['fontSize'], 'Style variations: Assert the originally correct values are still correct.' );
+               $this->assertEquals( 'var(--wp--preset--color--s)', $styles['blocks']['core/quote']['variations']['plain']['color']['background'], 'Style variations: Assert the internal variables are convert to CSS custom variables.' );
+
+       }
</ins><span class="cx" style="display: block; padding: 0 10px"> }
</span></span></pre>
</div>
</div>

</body>
</html>