Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 11 additions & 2 deletions lib/class-wp-theme-json-gutenberg.php
Original file line number Diff line number Diff line change
Expand Up @@ -3879,8 +3879,17 @@ public function get_styles_for_block( $block_metadata ) {
// Only store if the variation has blockGap defined.
if ( isset( $style_variation_node['spacing']['blockGap'] ) ) {
// Append block selector to the variation selector for proper targeting.
$variation_metadata_with_selector = $style_variation;
$variation_metadata_with_selector['selector'] = $style_variation['selector'] . $block_metadata['css'];
$variation_metadata_with_selector = $style_variation;
$variation_metadata_with_selector['selector'] = $style_variation['selector'] . $block_metadata['css'];

/*
* `get_layout_styles()` reads `name` as a block name and returns early when
* that block has no layout support. A variation node's `name` is the variation
* slug, which is never a registered block, so leaving it here discards every
* variation gap rule.
*/
unset( $variation_metadata_with_selector['name'] );
Comment thread
Jiwoon-Kim marked this conversation as resolved.
Resolved

$style_variation_layout_metadata[ $style_variation['selector'] ] = array(
'metadata' => $variation_metadata_with_selector,
'node' => $style_variation_node,
Expand Down
57 changes: 57 additions & 0 deletions phpunit/class-wp-theme-json-test.php
Original file line number Diff line number Diff line change
Expand Up @@ -7770,6 +7770,63 @@ public function test_opt_in_to_block_style_variations() {
$this->assertEquals( $expected, $button_variations );
}

/**
* Tests that a block style variation declaring `spacing.blockGap` emits a layout
* gap rule scoped to the variation.
*
* The variation node carries the variation slug in its `name`, which
* `get_layout_styles()` reads as a block name. A slug is never a registered block,
* so without care the layout rules for every variation are dropped.
*
* @covers WP_Theme_JSON_Gutenberg::get_styles_for_block
*/
public function test_block_style_variation_with_block_gap_emits_layout_styles() {
$registry = WP_Block_Styles_Registry::get_instance();
$registry->register( 'core/group', array( 'name' => 'custom-group' ) );

$theme_json = new WP_Theme_JSON_Gutenberg(
array(
'version' => WP_Theme_JSON_Gutenberg::LATEST_SCHEMA,
'settings' => array(
'spacing' => array(
'blockGap' => true,
),
),
'styles' => array(
'blocks' => array(
'core/group' => array(
'variations' => array(
'custom-group' => array(
'spacing' => array(
'blockGap' => '3em',
),
),
),
),
),
),
),
'blocks'
);

$stylesheet = $theme_json->get_stylesheet(
array( 'styles' ),
array( 'custom' ),
array(
'include_block_style_variations' => true,
'skip_root_layout_styles' => true,
)
);

$registry->unregister( 'core/group', 'custom-group' );

$this->assertStringContainsString(
':root :where(.wp-block-group.is-style-custom-group.wp-block-group-is-layout-flex){gap: 3em;}',
$stylesheet,
'The variation should emit a gap rule scoped to itself.'
);
}

/**
* This test covers `get_block_nodes` with the `$include_node_paths_only` option.
* When `true`, `$include_node_paths_only` should return only the paths of the block nodes.
Expand Down
Loading