From 8a2859cfa7e13e76c91367ca64b9dedb347bc34e Mon Sep 17 00:00:00 2001 From: Aaron Robertshaw <60436221+aaronrobertshaw@users.noreply.github.com> Date: Tue, 11 Jun 2024 16:25:41 +1000 Subject: [PATCH] Global Styles: Prevent duplicate block style variations CSS --- lib/block-supports/block-style-variations.php | 1 + lib/class-wp-theme-json-gutenberg.php | 20 +++++++++++++------ 2 files changed, 15 insertions(+), 6 deletions(-) diff --git a/lib/block-supports/block-style-variations.php b/lib/block-supports/block-style-variations.php index f2bc6af92e9dec..ebe1c4fc8888ec 100644 --- a/lib/block-supports/block-style-variations.php +++ b/lib/block-supports/block-style-variations.php @@ -133,6 +133,7 @@ function gutenberg_render_block_style_variation_support_styles( $parsed_block ) array( 'styles' ), array( 'custom' ), array( + 'block_style_variations' => true, 'skip_root_layout_styles' => true, 'scope' => ".$class_name", ) diff --git a/lib/class-wp-theme-json-gutenberg.php b/lib/class-wp-theme-json-gutenberg.php index db54757da68e75..b6462f9e0580dc 100644 --- a/lib/class-wp-theme-json-gutenberg.php +++ b/lib/class-wp-theme-json-gutenberg.php @@ -736,7 +736,7 @@ public static function get_element_class_name( $element ) { * @param string $origin Optional. What source of data this object represents. * One of 'blocks', 'default', 'theme', or 'custom'. Default 'theme'. */ - public function __construct( $theme_json = array( 'version' => WP_Theme_JSON_Gutenberg::LATEST_SCHEMA ), $origin = 'theme' ) { + public function __construct( $theme_json = array( 'version' => self::LATEST_SCHEMA ), $origin = 'theme' ) { if ( ! in_array( $origin, static::VALID_ORIGINS, true ) ) { $origin = 'theme'; } @@ -1259,6 +1259,7 @@ public function get_settings() { * - 'scope' that makes sure all style are scoped to a given selector * - `root_selector` which overwrites and forces a given selector to be used on the root node * - `skip_root_layout_styles` which omits root layout styles from the generated stylesheet. + * - `block_style_variations` which includes CSS for block style variations. * @return string The resulting stylesheet. */ public function get_stylesheet( $types = array( 'variables', 'styles', 'presets' ), $origins = null, $options = array() ) { @@ -1279,7 +1280,7 @@ public function get_stylesheet( $types = array( 'variables', 'styles', 'presets' } $blocks_metadata = static::get_blocks_metadata(); - $style_nodes = static::get_style_nodes( $this->theme_json, $blocks_metadata ); + $style_nodes = static::get_style_nodes( $this->theme_json, $blocks_metadata, $options ); $setting_nodes = static::get_setting_nodes( $this->theme_json, $blocks_metadata ); $root_style_key = array_search( static::ROOT_BLOCK_SELECTOR, array_column( $style_nodes, 'selector' ), true ); @@ -2486,9 +2487,12 @@ protected static function get_setting_nodes( $theme_json, $selectors = array() ) * * @param array $theme_json The tree to extract style nodes from. * @param array $selectors List of selectors per block. + * @param array $options An array of options to facilitate filtering style node generation + * The options currently supported are: + * - `block_style_variations` which includes CSS for block style variations. * @return array An array of style nodes metadata. */ - protected static function get_style_nodes( $theme_json, $selectors = array() ) { + protected static function get_style_nodes( $theme_json, $selectors = array(), $options = array() ) { $nodes = array(); if ( ! isset( $theme_json['styles'] ) ) { return $nodes; @@ -2532,7 +2536,7 @@ protected static function get_style_nodes( $theme_json, $selectors = array() ) { return $nodes; } - $block_nodes = static::get_block_nodes( $theme_json, $selectors ); + $block_nodes = static::get_block_nodes( $theme_json, $selectors, $options ); foreach ( $block_nodes as $block_node ) { $nodes[] = $block_node; } @@ -2607,9 +2611,12 @@ private static function update_separator_declarations( $declarations ) { * * @param array $theme_json The theme.json converted to an array. * @param array $selectors Optional list of selectors per block. + * @param array $options An array of options to facilitate filtering node generation + * The options currently supported are: + * - `block_style_variations` which includes CSS for block style variations. * @return array The block nodes in theme.json. */ - private static function get_block_nodes( $theme_json, $selectors = array() ) { + private static function get_block_nodes( $theme_json, $selectors = array(), $options = array() ) { $selectors = empty( $selectors ) ? static::get_blocks_metadata() : $selectors; $nodes = array(); if ( ! isset( $theme_json['styles'] ) ) { @@ -2638,7 +2645,8 @@ private static function get_block_nodes( $theme_json, $selectors = array() ) { } $variation_selectors = array(); - if ( isset( $node['variations'] ) ) { + $include_variations = $options['block_style_variations'] ?? false; + if ( $include_variations && isset( $node['variations'] ) ) { foreach ( $node['variations'] as $variation => $node ) { $variation_selectors[] = array( 'path' => array( 'styles', 'blocks', $name, 'variations', $variation ),