<!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>[55986] trunk: `wp_get_global_styles`: allow transforming the CSS Custom Properties into the values they represent.</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/55986">55986</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/55986","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-22 08:42:42 +0000 (Thu, 22 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`: allow transforming the CSS Custom Properties into the values they represent.
Props samnajian, ramonopoly, isabel_brison.
Fixes <a href="https://core.trac.wordpress.org/ticket/58588">#58588</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-22 08:16:05 UTC (rev 55985)
+++ trunk/src/wp-includes/class-wp-theme-json.php 2023-06-22 08:42:42 UTC (rev 55986)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -3529,4 +3529,77 @@
</span><span class="cx" style="display: block; padding: 0 10px"> return $tree;
</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">+ /**
+ * Replaces CSS variables with their values in place.
+ *
+ * @since 6.3.0
+ * @param array $styles CSS declarations to convert.
+ * @param array $values key => value pairs to use for replacement.
+ * @return array
+ */
+ private static function convert_variables_to_value( $styles, $values ) {
+ foreach ( $styles as $key => $style ) {
+ if ( is_array( $style ) ) {
+ $styles[ $key ] = self::convert_variables_to_value( $style, $values );
+ continue;
+ }
+
+ if ( 0 <= strpos( $style, 'var(' ) ) {
+ // find all the variables in the string in the form of var(--variable-name, fallback), with fallback in the second capture group.
+
+ $has_matches = preg_match_all( '/var\(([^),]+)?,?\s?(\S+)?\)/', $style, $var_parts );
+
+ if ( $has_matches ) {
+ $resolved_style = $styles[ $key ];
+ foreach ( $var_parts[1] as $index => $var_part ) {
+ $key_in_values = 'var(' . $var_part . ')';
+ $rule_to_replace = $var_parts[0][ $index ]; // the css rule to replace e.g. var(--wp--preset--color--vivid-green-cyan).
+ $fallback = $var_parts[2][ $index ]; // the fallback value.
+ $resolved_style = str_replace(
+ array(
+ $rule_to_replace,
+ $fallback,
+ ),
+ array(
+ isset( $values[ $key_in_values ] ) ? $values[ $key_in_values ] : $rule_to_replace,
+ isset( $values[ $fallback ] ) ? $values[ $fallback ] : $fallback,
+ ),
+ $resolved_style
+ );
+ }
+ $styles[ $key ] = $resolved_style;
+ }
+ }
+ }
+
+ return $styles;
+ }
+
+ /**
+ * Resolves the values of CSS variables in the given styles.
+ *
+ * @since 6.3.0
+ * @param WP_Theme_JSON $theme_json The theme json resolver.
+ *
+ * @return WP_Theme_JSON The $theme_json with resolved variables.
+ */
+ public static function resolve_variables( $theme_json ) {
+ $settings = $theme_json->get_settings();
+ $styles = $theme_json->get_raw_data()['styles'];
+ $preset_vars = static::compute_preset_vars( $settings, static::VALID_ORIGINS );
+ $theme_vars = static::compute_theme_vars( $settings );
+ $vars = array_reduce(
+ array_merge( $preset_vars, $theme_vars ),
+ function( $carry, $item ) {
+ $name = $item['name'];
+ $carry[ "var({$name})" ] = $item['value'];
+ return $carry;
+ },
+ array()
+ );
+
+ $theme_json->theme_json['styles'] = self::convert_variables_to_value( $styles, $vars );
+ return $theme_json;
+ }
+
</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-22 08:16:05 UTC (rev 55985)
+++ trunk/src/wp-includes/global-styles-and-settings.php 2023-06-22 08:42:42 UTC (rev 55986)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -92,8 +92,10 @@
</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><del style="background-color: #fdd; 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)".
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * @since 6.3.0 the internal link format "var:preset|color|secondary" is resolved
+ * to "var(--wp--preset--font-size--small)" so consumers don't have to.
+ * @since 6.3.0 `transforms` is now usable in the `context` parameter. In case [`transforms`]['resolve_variables']
+ * is defined, variables are resolved to their value in the styles.
</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">@@ -105,6 +107,9 @@
</span><span class="cx" style="display: block; padding: 0 10px"> * @type string $origin Which origin to take data from.
</span><span class="cx" style="display: block; padding: 0 10px"> * Valid values are 'all' (core, theme, and user) or 'base' (core and theme).
</span><span class="cx" style="display: block; padding: 0 10px"> * If empty or unknown, 'all' is used.
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * @type array $transforms Which transformation(s) to apply.
+ * Valid value is array( 'resolve-variables' ).
+ * If defined, variables are resolved to their value in the styles.
</ins><span class="cx" style="display: block; padding: 0 10px"> * }
</span><span class="cx" style="display: block; padding: 0 10px"> * @return mixed The styles array or individual style value to retrieve.
</span><span class="cx" style="display: block; padding: 0 10px"> */
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -117,11 +122,20 @@
</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">- $styles = WP_Theme_JSON_Resolver::get_merged_data( $origin )->get_raw_data()['styles'];
</del><span class="cx" style="display: block; padding: 0 10px">
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $resolve_variables = isset( $context['transforms'] )
+ && is_array( $context['transforms'] )
+ && in_array( 'resolve-variables', $context['transforms'], true );
+
+ $merged_data = WP_Theme_JSON_Resolver::get_merged_data( $origin );
+ if ( $resolve_variables ) {
+ $merged_data = WP_Theme_JSON::resolve_variables( $merged_data );
+ }
+ $styles = $merged_data->get_raw_data()['styles'];
</ins><span class="cx" style="display: block; padding: 0 10px"> return _wp_array_get( $styles, $path, $styles );
</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">+
</ins><span class="cx" style="display: block; padding: 0 10px"> /**
</span><span class="cx" style="display: block; padding: 0 10px"> * Returns the stylesheet resulting of merging core, theme, and user data.
</span><span class="cx" style="display: block; padding: 0 10px"> *
</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-22 08:16:05 UTC (rev 55985)
+++ trunk/tests/phpunit/tests/theme/wpThemeJson.php 2023-06-22 08:42:42 UTC (rev 55986)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -4777,4 +4777,158 @@
</span><span class="cx" style="display: block; padding: 0 10px"> $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.' );
</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_resolve_variables() {
+ $primary_color = '#9DFF20';
+ $secondary_color = '#9DFF21';
+ $contrast_color = '#000';
+ $raw_color_value = '#efefef';
+ $large_font = '18px';
+ $small_font = '12px';
+ $theme_json = new WP_Theme_JSON(
+ array(
+ 'version' => WP_Theme_JSON::LATEST_SCHEMA,
+ 'settings' => array(
+ 'color' => array(
+ 'palette' => array(
+ 'theme' => array(
+ array(
+ 'color' => $primary_color,
+ 'name' => 'Primary',
+ 'slug' => 'primary',
+ ),
+ array(
+ 'color' => $secondary_color,
+ 'name' => 'Secondary',
+ 'slug' => 'secondary',
+ ),
+ array(
+ 'color' => $contrast_color,
+ 'name' => 'Contrast',
+ 'slug' => 'contrast',
+ ),
+ ),
+ ),
+ ),
+ 'typography' => array(
+ 'fontSizes' => array(
+ array(
+ 'size' => $small_font,
+ 'name' => 'Font size small',
+ 'slug' => 'small',
+ ),
+ array(
+ 'size' => $large_font,
+ 'name' => 'Font size large',
+ 'slug' => 'large',
+ ),
+ ),
+ ),
+ ),
+ 'styles' => array(
+ 'color' => array(
+ 'background' => 'var(--wp--preset--color--primary)',
+ 'text' => $raw_color_value,
+ ),
+ 'elements' => array(
+ 'button' => array(
+ 'color' => array(
+ 'text' => 'var(--wp--preset--color--contrast)',
+ ),
+ 'typography' => array(
+ 'fontSize' => 'var(--wp--preset--font-size--small)',
+ ),
+ ),
+ ),
+ 'blocks' => array(
+ 'core/post-terms' => array(
+ 'typography' => array( 'fontSize' => 'var(--wp--preset--font-size--small)' ),
+ 'color' => array( 'background' => $raw_color_value ),
+ ),
+ 'core/more' => array(
+ 'typography' => array( 'fontSize' => 'var(--undefined--font-size--small)' ),
+ 'color' => array( 'background' => 'linear-gradient(90deg, var(--wp--preset--color--primary) 0%, var(--wp--preset--color--secondary) 35%, var(--wp--undefined--color--secondary) 100%)' ),
+ ),
+ 'core/comment-content' => array(
+ 'typography' => array( 'fontSize' => 'calc(var(--wp--preset--font-size--small, 12px) + 20px)' ),
+ 'color' => array(
+ 'text' => 'var(--wp--preset--color--primary, red)',
+ 'background' => 'var(--wp--preset--color--primary, var(--wp--preset--font-size--secondary))',
+ 'link' => 'var(--undefined--color--primary, var(--wp--preset--font-size--secondary))',
+ ),
+ ),
+ 'core/comments' => array(
+ 'color' => array(
+ 'text' => 'var(--undefined--color--primary, var(--wp--preset--font-size--small))',
+ 'background' => 'var(--wp--preset--color--primary, var(--undefined--color--primary))',
+ ),
+ ),
+ 'core/navigation' => array(
+ 'elements' => array(
+ 'link' => array(
+ 'color' => array(
+ 'background' => 'var(--wp--preset--color--primary)',
+ 'text' => 'var(--wp--preset--color--secondary)',
+ ),
+ 'typography' => array(
+ 'fontSize' => 'var(--wp--preset--font-size--large)',
+ ),
+ ),
+ ),
+ ),
+ 'core/quote' => array(
+ 'typography' => array( 'fontSize' => 'var(--wp--preset--font-size--large)' ),
+ 'color' => array( 'background' => 'var(--wp--preset--color--primary)' ),
+ 'variations' => array(
+ 'plain' => array(
+ 'typography' => array( 'fontSize' => 'var(--wp--preset--font-size--small)' ),
+ 'color' => array( 'background' => 'var(--wp--preset--color--secondary)' ),
+ ),
+ ),
+ ),
+ ),
+ ),
+ )
+ );
+
+ $styles = $theme_json::resolve_variables( $theme_json )->get_raw_data()['styles'];
+
+ $this->assertEquals( $primary_color, $styles['color']['background'], 'Top level: Assert values are converted' );
+ $this->assertEquals( $raw_color_value, $styles['color']['text'], 'Top level: Assert raw values stay intact' );
+
+ $this->assertEquals( $contrast_color, $styles['elements']['button']['color']['text'], 'Elements: color' );
+ $this->assertEquals( $small_font, $styles['elements']['button']['typography']['fontSize'], 'Elements: font-size' );
+
+ $this->assertEquals( $large_font, $styles['blocks']['core/quote']['typography']['fontSize'], 'Blocks: font-size' );
+ $this->assertEquals( $primary_color, $styles['blocks']['core/quote']['color']['background'], 'Blocks: color' );
+ $this->assertEquals( $raw_color_value, $styles['blocks']['core/post-terms']['color']['background'], 'Blocks: Raw color value stays intact' );
+ $this->assertEquals( $small_font, $styles['blocks']['core/post-terms']['typography']['fontSize'], 'Block core/post-terms: font-size' );
+ $this->assertEquals(
+ "linear-gradient(90deg, $primary_color 0%, $secondary_color 35%, var(--wp--undefined--color--secondary) 100%)",
+ $styles['blocks']['core/more']['color']['background'],
+ 'Blocks: multiple colors and undefined color'
+ );
+ $this->assertEquals( 'var(--undefined--font-size--small)', $styles['blocks']['core/more']['typography']['fontSize'], 'Blocks: undefined font-size ' );
+ $this->assertEquals( "calc($small_font + 20px)", $styles['blocks']['core/comment-content']['typography']['fontSize'], 'Blocks: font-size in random place' );
+ $this->assertEquals( $primary_color, $styles['blocks']['core/comment-content']['color']['text'], 'Blocks: text color with fallback' );
+ $this->assertEquals( $primary_color, $styles['blocks']['core/comment-content']['color']['background'], 'Blocks: background color with var as fallback' );
+ $this->assertEquals( $primary_color, $styles['blocks']['core/navigation']['elements']['link']['color']['background'], 'Block element: background color' );
+ $this->assertEquals( $secondary_color, $styles['blocks']['core/navigation']['elements']['link']['color']['text'], 'Block element: text color' );
+ $this->assertEquals( $large_font, $styles['blocks']['core/navigation']['elements']['link']['typography']['fontSize'], 'Block element: font-size' );
+
+ $this->assertEquals(
+ "var(--undefined--color--primary, $small_font)",
+ $styles['blocks']['core/comments']['color']['text'],
+ 'Blocks: text color with undefined var and fallback'
+ );
+ $this->assertEquals(
+ $primary_color,
+ $styles['blocks']['core/comments']['color']['background'],
+ 'Blocks: background color with variable and undefined fallback'
+ );
+
+ $this->assertEquals( $small_font, $styles['blocks']['core/quote']['variations']['plain']['typography']['fontSize'], 'Block variations: font-size' );
+ $this->assertEquals( $secondary_color, $styles['blocks']['core/quote']['variations']['plain']['color']['background'], 'Block variations: color' );
+ }
+
</ins><span class="cx" style="display: block; padding: 0 10px"> }
</span></span></pre>
</div>
</div>
</body>
</html>