<!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>[58422] trunk: Global styles: prevent duplicate CSS for block style variations.</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/58422">58422</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/58422","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>2024-06-17 09:42:57 +0000 (Mon, 17 Jun 2024)</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'>Global styles: prevent duplicate CSS for block style variations.
Props aaronrobertshaw, mukesh27, ramonopoly, isabel_brison, oandregal.
Fixes <a href="https://core.trac.wordpress.org/ticket/61443">#61443</a>.</pre>
<h3>Modified Paths</h3>
<ul>
<li><a href="#trunksrcwpincludesblocksupportsblockstylevariationsphp">trunk/src/wp-includes/block-supports/block-style-variations.php</a></li>
<li><a href="#trunksrcwpincludesclasswpthemejsonphp">trunk/src/wp-includes/class-wp-theme-json.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="trunksrcwpincludesblocksupportsblockstylevariationsphp"></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/block-supports/block-style-variations.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/src/wp-includes/block-supports/block-style-variations.php 2024-06-17 03:47:37 UTC (rev 58421)
+++ trunk/src/wp-includes/block-supports/block-style-variations.php 2024-06-17 09:42:57 UTC (rev 58422)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -136,8 +136,9 @@
</span><span class="cx" style="display: block; padding: 0 10px"> array( 'styles' ),
</span><span class="cx" style="display: block; padding: 0 10px"> array( 'custom' ),
</span><span class="cx" style="display: block; padding: 0 10px"> array(
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- 'skip_root_layout_styles' => true,
- 'scope' => ".$class_name",
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ 'include_block_style_variations' => true,
+ 'skip_root_layout_styles' => true,
+ 'scope' => ".$class_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></pre></div>
<a id="trunksrcwpincludesclasswpthemejsonphp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/src/wp-includes/class-wp-theme-json.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/src/wp-includes/class-wp-theme-json.php 2024-06-17 03:47:37 UTC (rev 58421)
+++ trunk/src/wp-includes/class-wp-theme-json.php 2024-06-17 09:42:57 UTC (rev 58422)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -750,7 +750,7 @@
</span><span class="cx" style="display: block; padding: 0 10px"> * @param string $origin Optional. What source of data this object represents.
</span><span class="cx" style="display: block; padding: 0 10px"> * One of 'blocks', 'default', 'theme', or 'custom'. Default '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">- public function __construct( $theme_json = array( 'version' => WP_Theme_JSON::LATEST_SCHEMA ), $origin = 'theme' ) {
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ public function __construct( $theme_json = array( 'version' => self::LATEST_SCHEMA ), $origin = 'theme' ) {
</ins><span class="cx" style="display: block; padding: 0 10px"> if ( ! in_array( $origin, static::VALID_ORIGINS, true ) ) {
</span><span class="cx" style="display: block; padding: 0 10px"> $origin = 'theme';
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -1247,7 +1247,8 @@
</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 Removed the `$type` parameter, added the `$types` and `$origins` parameters.
</span><span class="cx" style="display: block; padding: 0 10px"> * @since 6.3.0 Add fallback layout styles for Post Template when block gap support isn't available.
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- * @since 6.6.0 Added `skip_root_layout_styles` option to omit layout styles if desired.
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * @since 6.6.0 Added boolean `skip_root_layout_styles` and `include_block_style_variations` options
+ * to control styles output as desired.
</ins><span class="cx" style="display: block; padding: 0 10px"> *
</span><span class="cx" style="display: block; padding: 0 10px"> * @param string[] $types Types of styles to load. Will load all by default. It accepts:
</span><span class="cx" style="display: block; padding: 0 10px"> * - `variables`: only the CSS Custom Properties for presets & custom ones.
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -1257,9 +1258,10 @@
</span><span class="cx" style="display: block; padding: 0 10px"> * @param array $options {
</span><span class="cx" style="display: block; padding: 0 10px"> * Optional. An array of options for now used for internal purposes only (may change without notice).
</span><span class="cx" style="display: block; padding: 0 10px"> *
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- * @type string $scope Makes sure all style are scoped to a given selector
- * @type string $root_selector Overwrites and forces a given selector to be used on the root node
- * @type bool $skip_root_layout_styles Omits root layout styles from the generated stylesheet. Default false.
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * @type string $scope Makes sure all style are scoped to a given selector
+ * @type string $root_selector Overwrites and forces a given selector to be used on the root node
+ * @type bool $skip_root_layout_styles Omits root layout styles from the generated stylesheet. Default false.
+ * @type bool $include_block_style_variations Includes styles for block style variations in the generated stylesheet. Default false.
</ins><span class="cx" style="display: block; padding: 0 10px"> * }
</span><span class="cx" style="display: block; padding: 0 10px"> * @return string The resulting stylesheet.
</span><span class="cx" style="display: block; padding: 0 10px"> */
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -1281,7 +1283,7 @@
</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"> $blocks_metadata = static::get_blocks_metadata();
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- $style_nodes = static::get_style_nodes( $this->theme_json, $blocks_metadata );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $style_nodes = static::get_style_nodes( $this->theme_json, $blocks_metadata, $options );
</ins><span class="cx" style="display: block; padding: 0 10px"> $setting_nodes = static::get_setting_nodes( $this->theme_json, $blocks_metadata );
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> $root_style_key = array_search( static::ROOT_BLOCK_SELECTOR, array_column( $style_nodes, 'selector' ), true );
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -2446,12 +2448,18 @@
</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"> * @since 5.8.0
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * @since 6.6.0 Added options array for modifying generated nodes.
</ins><span class="cx" style="display: block; padding: 0 10px"> *
</span><span class="cx" style="display: block; padding: 0 10px"> * @param array $theme_json The tree to extract style nodes from.
</span><span class="cx" style="display: block; padding: 0 10px"> * @param array $selectors List of selectors per block.
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * @param array $options {
+ * Optional. An array of options for now used for internal purposes only (may change without notice).
+ *
+ * @type bool $include_block_style_variations Includes style nodes for block style variations. Default false.
+ * }
</ins><span class="cx" style="display: block; padding: 0 10px"> * @return array An array of style nodes metadata.
</span><span class="cx" style="display: block; padding: 0 10px"> */
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- protected static function get_style_nodes( $theme_json, $selectors = array() ) {
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ protected static function get_style_nodes( $theme_json, $selectors = array(), $options = array() ) {
</ins><span class="cx" style="display: block; padding: 0 10px"> $nodes = array();
</span><span class="cx" style="display: block; padding: 0 10px"> if ( ! isset( $theme_json['styles'] ) ) {
</span><span class="cx" style="display: block; padding: 0 10px"> return $nodes;
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -2493,7 +2501,7 @@
</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">- $block_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, $selectors, $options );
</ins><span class="cx" style="display: block; padding: 0 10px"> foreach ( $block_nodes as $block_node ) {
</span><span class="cx" style="display: block; padding: 0 10px"> $nodes[] = $block_node;
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -2564,12 +2572,19 @@
</span><span class="cx" style="display: block; padding: 0 10px"> *
</span><span class="cx" style="display: block; padding: 0 10px"> * @since 6.1.0
</span><span class="cx" style="display: block; padding: 0 10px"> * @since 6.3.0 Refactored and stabilized selectors API.
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * @since 6.6.0 Added optional selectors and options for generating block nodes.
</ins><span class="cx" style="display: block; padding: 0 10px"> *
</span><span class="cx" style="display: block; padding: 0 10px"> * @param array $theme_json The theme.json converted to an array.
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * @param array $selectors Optional list of selectors per block.
+ * @param array $options {
+ * Optional. An array of options for now used for internal purposes only (may change without notice).
+ *
+ * @type bool $include_block_style_variations Includes nodes for block style variations. Default false.
+ * }
</ins><span class="cx" style="display: block; padding: 0 10px"> * @return array The block nodes in theme.json.
</span><span class="cx" style="display: block; padding: 0 10px"> */
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- private static function get_block_nodes( $theme_json ) {
- $selectors = static::get_blocks_metadata();
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ private static function get_block_nodes( $theme_json, $selectors = array(), $options = array() ) {
+ $selectors = empty( $selectors ) ? static::get_blocks_metadata() : $selectors;
</ins><span class="cx" style="display: block; padding: 0 10px"> $nodes = array();
</span><span class="cx" style="display: block; padding: 0 10px"> if ( ! isset( $theme_json['styles'] ) ) {
</span><span class="cx" style="display: block; padding: 0 10px"> return $nodes;
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -2597,7 +2612,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"> $variation_selectors = array();
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- if ( isset( $node['variations'] ) ) {
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $include_variations = $options['include_block_style_variations'] ?? false;
+ if ( $include_variations && isset( $node['variations'] ) ) {
</ins><span class="cx" style="display: block; padding: 0 10px"> foreach ( $node['variations'] as $variation => $node ) {
</span><span class="cx" style="display: block; padding: 0 10px"> $variation_selectors[] = array(
</span><span class="cx" style="display: block; padding: 0 10px"> 'path' => array( 'styles', 'blocks', $name, 'variations', $variation ),
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -3280,7 +3296,8 @@
</span><span class="cx" style="display: block; padding: 0 10px"> $theme_json = static::sanitize( $theme_json, $valid_block_names, $valid_element_names, $valid_variations );
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> $blocks_metadata = static::get_blocks_metadata();
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- $style_nodes = static::get_style_nodes( $theme_json, $blocks_metadata );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $style_options = array( 'include_block_style_variations' => true ); // Allow variations data.
+ $style_nodes = static::get_style_nodes( $theme_json, $blocks_metadata, $style_options );
</ins><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> foreach ( $style_nodes as $metadata ) {
</span><span class="cx" style="display: block; padding: 0 10px"> $input = _wp_array_get( $theme_json, $metadata['path'], array() );
</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 2024-06-17 03:47:37 UTC (rev 58421)
+++ trunk/tests/phpunit/tests/theme/wpThemeJson.php 2024-06-17 09:42:57 UTC (rev 58422)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -5500,4 +5500,85 @@
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> $this->assertEquals( $expected, $actual );
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+
+ /**
+ * Block style variations styles aren't generated by default. This test covers
+ * the `get_block_nodes` does not include variations by default, preventing
+ * the inclusion of their styles.
+ *
+ * @ticket 61443
+ */
+ public function test_opt_out_of_block_style_variations_by_default() {
+ $theme_json = new ReflectionClass( 'WP_Theme_JSON' );
+
+ $func = $theme_json->getMethod( 'get_block_nodes' );
+ $func->setAccessible( true );
+
+ $theme_json = array(
+ 'version' => WP_Theme_JSON::LATEST_SCHEMA,
+ 'styles' => array(
+ 'blocks' => array(
+ 'core/button' => array(
+ 'variations' => array(
+ 'outline' => array(
+ 'color' => array(
+ 'background' => 'red',
+ ),
+ ),
+ ),
+ ),
+ ),
+ ),
+ );
+ $selectors = array();
+
+ $block_nodes = $func->invoke( null, $theme_json, $selectors );
+ $button_variations = $block_nodes[0]['variations'] ?? array();
+
+ $this->assertEquals( array(), $button_variations );
+ }
+
+ /**
+ * Block style variations styles aren't generated by default. This test ensures
+ * variations are included by `get_block_nodes` when requested.
+ *
+ * @ticket 61443
+ */
+ public function test_opt_in_to_block_style_variations() {
+ $theme_json = new ReflectionClass( 'WP_Theme_JSON' );
+
+ $func = $theme_json->getMethod( 'get_block_nodes' );
+ $func->setAccessible( true );
+
+ $theme_json = array(
+ 'version' => WP_Theme_JSON::LATEST_SCHEMA,
+ 'styles' => array(
+ 'blocks' => array(
+ 'core/button' => array(
+ 'variations' => array(
+ 'outline' => array(
+ 'color' => array(
+ 'background' => 'red',
+ ),
+ ),
+ ),
+ ),
+ ),
+ ),
+ );
+ $selectors = array();
+ $options = array( 'include_block_style_variations' => true );
+
+ $block_nodes = $func->invoke( null, $theme_json, $selectors, $options );
+ $button_variations = $block_nodes[0]['variations'] ?? array();
+
+ $expected = array(
+ array(
+ 'path' => array( 'styles', 'blocks', 'core/button', 'variations', 'outline' ),
+ 'selector' => '.wp-block-button.is-style-outline .wp-block-button__link',
+ ),
+ );
+
+ $this->assertEquals( $expected, $button_variations );
+ }
</ins><span class="cx" style="display: block; padding: 0 10px"> }
</span></span></pre>
</div>
</div>
</body>
</html>