From cfb332ba0c455ed74ae9e4f89718d9f0226515a6 Mon Sep 17 00:00:00 2001 From: ramon Date: Wed, 13 Nov 2024 14:14:27 +1100 Subject: [PATCH 1/4] Follow up to https://github.com/WordPress/gutenberg/pull/66002 Including variations in the nodes array when 'include_node_paths_only' => true --- lib/class-wp-theme-json-gutenberg.php | 14 ++++- phpunit/class-wp-theme-json-test.php | 79 ++++++++++++++++++++++++++- 2 files changed, 91 insertions(+), 2 deletions(-) diff --git a/lib/class-wp-theme-json-gutenberg.php b/lib/class-wp-theme-json-gutenberg.php index d505916450cafb..1a5cb0e708e0ac 100644 --- a/lib/class-wp-theme-json-gutenberg.php +++ b/lib/class-wp-theme-json-gutenberg.php @@ -2749,9 +2749,21 @@ private static function get_block_nodes( $theme_json, $selectors = array(), $opt foreach ( $theme_json['styles']['blocks'] as $name => $node ) { $node_path = array( 'styles', 'blocks', $name ); if ( $include_node_paths_only ) { - $nodes[] = array( + $variation_paths = array(); + if ( $include_variations && isset( $node['variations'] ) ) { + foreach ( $node['variations'] as $variation => $node ) { + $variation_paths[] = array( + 'path' => array( 'styles', 'blocks', $name, 'variations', $variation ), + ); + } + } + $node = array( 'path' => $node_path, ); + if ( ! empty( $variation_paths ) ) { + $node['variations'] = $variation_paths; + } + $nodes[] = $node; } else { $selector = null; if ( isset( $selectors[ $name ]['selector'] ) ) { diff --git a/phpunit/class-wp-theme-json-test.php b/phpunit/class-wp-theme-json-test.php index f77086cf0dd046..d79bd270aea3da 100644 --- a/phpunit/class-wp-theme-json-test.php +++ b/phpunit/class-wp-theme-json-test.php @@ -5927,7 +5927,7 @@ public function test_return_block_node_paths() { ), ), 'core/group' => array( - 'elements' => array( + 'elements' => array( 'link' => array( 'color' => array( 'background' => 'blue', @@ -5955,4 +5955,81 @@ public function test_return_block_node_paths() { $this->assertEquals( $expected, $block_nodes ); } + + /** + * This test covers `get_block_nodes` with the `$include_node_paths_only` + * and `include_block_style_variations` options. + */ + public function test_return_block_node_paths_with_variations() { + $theme_json = new ReflectionClass( 'WP_Theme_JSON_Gutenberg' ); + + $func = $theme_json->getMethod( 'get_block_nodes' ); + $func->setAccessible( true ); + + $theme_json = array( + 'version' => WP_Theme_JSON_Gutenberg::LATEST_SCHEMA, + 'styles' => array( + 'typography' => array( + 'fontSize' => '16px', + ), + 'blocks' => array( + 'core/button' => array( + 'color' => array( + 'background' => 'red', + ), + 'variations' => array( + 'cheese' => array( + 'color' => array( + 'background' => 'cheese', + ), + ), + ), + ), + 'core/group' => array( + 'color' => array( + 'background' => 'blue', + ), + 'variations' => array( + 'apricot' => array( + 'color' => array( + 'background' => 'apricot', + ), + ), + ), + ), + ), + ), + ); + + $block_nodes = $func->invoke( + null, + $theme_json, + array(), + array( + 'include_node_paths_only' => true, + 'include_block_style_variations' => true, + ) + ); + + $expected = array( + array( + 'path' => array( 'styles', 'blocks', 'core/button' ), + 'variations' => array( + array( + 'path' => array( 'styles', 'blocks', 'core/button', 'variations', 'cheese' ), + ), + ), + ), + array( + 'path' => array( 'styles', 'blocks', 'core/group' ), + 'variations' => array( + array( + 'path' => array( 'styles', 'blocks', 'core/group', 'variations', 'apricot' ), + ), + ), + ), + ); + + $this->assertEquals( $expected, $block_nodes ); + } } From e01800070bfb941f53fa432384dd20d69b2ce83c Mon Sep 17 00:00:00 2001 From: ramon Date: Wed, 13 Nov 2024 14:26:46 +1100 Subject: [PATCH 2/4] change inner loop var name --- lib/class-wp-theme-json-gutenberg.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/class-wp-theme-json-gutenberg.php b/lib/class-wp-theme-json-gutenberg.php index 1a5cb0e708e0ac..10f80c3f3cdb7b 100644 --- a/lib/class-wp-theme-json-gutenberg.php +++ b/lib/class-wp-theme-json-gutenberg.php @@ -2751,7 +2751,7 @@ private static function get_block_nodes( $theme_json, $selectors = array(), $opt if ( $include_node_paths_only ) { $variation_paths = array(); if ( $include_variations && isset( $node['variations'] ) ) { - foreach ( $node['variations'] as $variation => $node ) { + foreach ( $node['variations'] as $variation => $variation_node ) { $variation_paths[] = array( 'path' => array( 'styles', 'blocks', $name, 'variations', $variation ), ); From b4944e0f39446da8d46262597cbb4949d00e8f25 Mon Sep 17 00:00:00 2001 From: ramon Date: Wed, 13 Nov 2024 14:30:05 +1100 Subject: [PATCH 3/4] changelog --- backport-changelog/6.8/7784.md | 3 +++ 1 file changed, 3 insertions(+) create mode 100644 backport-changelog/6.8/7784.md diff --git a/backport-changelog/6.8/7784.md b/backport-changelog/6.8/7784.md new file mode 100644 index 00000000000000..b18dca88112a71 --- /dev/null +++ b/backport-changelog/6.8/7784.md @@ -0,0 +1,3 @@ +https://github.com/WordPress/wordpress-develop/pull/7784 + +* https://github.com/WordPress/gutenberg/pull/66948 From 5a5a99bf00ccc687f1951235eda6c6b5d454f162 Mon Sep 17 00:00:00 2001 From: ramon Date: Wed, 13 Nov 2024 14:49:13 +1100 Subject: [PATCH 4/4] linto --- phpunit/class-wp-theme-json-test.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/phpunit/class-wp-theme-json-test.php b/phpunit/class-wp-theme-json-test.php index d79bd270aea3da..1aacb92759e591 100644 --- a/phpunit/class-wp-theme-json-test.php +++ b/phpunit/class-wp-theme-json-test.php @@ -5927,7 +5927,7 @@ public function test_return_block_node_paths() { ), ), 'core/group' => array( - 'elements' => array( + 'elements' => array( 'link' => array( 'color' => array( 'background' => 'blue', @@ -6013,7 +6013,7 @@ public function test_return_block_node_paths_with_variations() { $expected = array( array( - 'path' => array( 'styles', 'blocks', 'core/button' ), + 'path' => array( 'styles', 'blocks', 'core/button' ), 'variations' => array( array( 'path' => array( 'styles', 'blocks', 'core/button', 'variations', 'cheese' ), @@ -6021,7 +6021,7 @@ public function test_return_block_node_paths_with_variations() { ), ), array( - 'path' => array( 'styles', 'blocks', 'core/group' ), + 'path' => array( 'styles', 'blocks', 'core/group' ), 'variations' => array( array( 'path' => array( 'styles', 'blocks', 'core/group', 'variations', 'apricot' ),