Skip to content

Commit cfb332b

Browse files
committed
Follow up to #66002
Including variations in the nodes array when 'include_node_paths_only' => true
1 parent 0a5e54a commit cfb332b

File tree

2 files changed

+91
-2
lines changed

2 files changed

+91
-2
lines changed

lib/class-wp-theme-json-gutenberg.php

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2749,9 +2749,21 @@ private static function get_block_nodes( $theme_json, $selectors = array(), $opt
27492749
foreach ( $theme_json['styles']['blocks'] as $name => $node ) {
27502750
$node_path = array( 'styles', 'blocks', $name );
27512751
if ( $include_node_paths_only ) {
2752-
$nodes[] = array(
2752+
$variation_paths = array();
2753+
if ( $include_variations && isset( $node['variations'] ) ) {
2754+
foreach ( $node['variations'] as $variation => $node ) {
2755+
$variation_paths[] = array(
2756+
'path' => array( 'styles', 'blocks', $name, 'variations', $variation ),
2757+
);
2758+
}
2759+
}
2760+
$node = array(
27532761
'path' => $node_path,
27542762
);
2763+
if ( ! empty( $variation_paths ) ) {
2764+
$node['variations'] = $variation_paths;
2765+
}
2766+
$nodes[] = $node;
27552767
} else {
27562768
$selector = null;
27572769
if ( isset( $selectors[ $name ]['selector'] ) ) {

phpunit/class-wp-theme-json-test.php

Lines changed: 78 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5927,7 +5927,7 @@ public function test_return_block_node_paths() {
59275927
),
59285928
),
59295929
'core/group' => array(
5930-
'elements' => array(
5930+
'elements' => array(
59315931
'link' => array(
59325932
'color' => array(
59335933
'background' => 'blue',
@@ -5955,4 +5955,81 @@ public function test_return_block_node_paths() {
59555955

59565956
$this->assertEquals( $expected, $block_nodes );
59575957
}
5958+
5959+
/**
5960+
* This test covers `get_block_nodes` with the `$include_node_paths_only`
5961+
* and `include_block_style_variations` options.
5962+
*/
5963+
public function test_return_block_node_paths_with_variations() {
5964+
$theme_json = new ReflectionClass( 'WP_Theme_JSON_Gutenberg' );
5965+
5966+
$func = $theme_json->getMethod( 'get_block_nodes' );
5967+
$func->setAccessible( true );
5968+
5969+
$theme_json = array(
5970+
'version' => WP_Theme_JSON_Gutenberg::LATEST_SCHEMA,
5971+
'styles' => array(
5972+
'typography' => array(
5973+
'fontSize' => '16px',
5974+
),
5975+
'blocks' => array(
5976+
'core/button' => array(
5977+
'color' => array(
5978+
'background' => 'red',
5979+
),
5980+
'variations' => array(
5981+
'cheese' => array(
5982+
'color' => array(
5983+
'background' => 'cheese',
5984+
),
5985+
),
5986+
),
5987+
),
5988+
'core/group' => array(
5989+
'color' => array(
5990+
'background' => 'blue',
5991+
),
5992+
'variations' => array(
5993+
'apricot' => array(
5994+
'color' => array(
5995+
'background' => 'apricot',
5996+
),
5997+
),
5998+
),
5999+
),
6000+
),
6001+
),
6002+
);
6003+
6004+
$block_nodes = $func->invoke(
6005+
null,
6006+
$theme_json,
6007+
array(),
6008+
array(
6009+
'include_node_paths_only' => true,
6010+
'include_block_style_variations' => true,
6011+
)
6012+
);
6013+
6014+
$expected = array(
6015+
array(
6016+
'path' => array( 'styles', 'blocks', 'core/button' ),
6017+
'variations' => array(
6018+
array(
6019+
'path' => array( 'styles', 'blocks', 'core/button', 'variations', 'cheese' ),
6020+
),
6021+
),
6022+
),
6023+
array(
6024+
'path' => array( 'styles', 'blocks', 'core/group' ),
6025+
'variations' => array(
6026+
array(
6027+
'path' => array( 'styles', 'blocks', 'core/group', 'variations', 'apricot' ),
6028+
),
6029+
),
6030+
),
6031+
);
6032+
6033+
$this->assertEquals( $expected, $block_nodes );
6034+
}
59586035
}

0 commit comments

Comments
 (0)