From 6ed1ed2e3f031e3c3f38130cd736ef80c25db5e7 Mon Sep 17 00:00:00 2001 From: ramon Date: Wed, 13 Nov 2024 14:27:44 +1100 Subject: [PATCH 1/3] Including variations in the nodes array when 'include_node_paths_only' => true --- src/wp-includes/class-wp-theme-json.php | 14 ++++- tests/phpunit/tests/theme/wpThemeJson.php | 77 +++++++++++++++++++++++ 2 files changed, 90 insertions(+), 1 deletion(-) diff --git a/src/wp-includes/class-wp-theme-json.php b/src/wp-includes/class-wp-theme-json.php index f40fde9cf82b3..7885f27b420c5 100644 --- a/src/wp-includes/class-wp-theme-json.php +++ b/src/wp-includes/class-wp-theme-json.php @@ -2722,9 +2722,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 => $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/tests/phpunit/tests/theme/wpThemeJson.php b/tests/phpunit/tests/theme/wpThemeJson.php index 5b0653c6849a3..ab51a49e91e54 100644 --- a/tests/phpunit/tests/theme/wpThemeJson.php +++ b/tests/phpunit/tests/theme/wpThemeJson.php @@ -2502,6 +2502,83 @@ 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' ); + + $func = $theme_json->getMethod( 'get_block_nodes' ); + $func->setAccessible( true ); + + $theme_json = array( + 'version' => WP_Theme_JSON::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 ); + } + /** * @ticket 54336 */ From ab8e76da5cde68352a0ba86a03ef7b9d1c242316 Mon Sep 17 00:00:00 2001 From: Ramon Date: Tue, 19 Nov 2024 13:25:30 +1100 Subject: [PATCH 2/3] Update tests/phpunit/tests/theme/wpThemeJson.php Co-authored-by: Aaron Robertshaw <60436221+aaronrobertshaw@users.noreply.github.com> --- tests/phpunit/tests/theme/wpThemeJson.php | 2 ++ 1 file changed, 2 insertions(+) diff --git a/tests/phpunit/tests/theme/wpThemeJson.php b/tests/phpunit/tests/theme/wpThemeJson.php index ab51a49e91e54..884b14df23a1f 100644 --- a/tests/phpunit/tests/theme/wpThemeJson.php +++ b/tests/phpunit/tests/theme/wpThemeJson.php @@ -2505,6 +2505,8 @@ public function test_return_block_node_paths() { /** * This test covers `get_block_nodes` with the `$include_node_paths_only` * and `include_block_style_variations` options. + * + * @ticket 62399 */ public function test_return_block_node_paths_with_variations() { $theme_json = new ReflectionClass( 'WP_Theme_JSON' ); From 2b2934a46ca4a3aa4b63b8e88a941dd6bba9c5e1 Mon Sep 17 00:00:00 2001 From: Ramon Date: Tue, 19 Nov 2024 14:06:07 +1100 Subject: [PATCH 3/3] Update tests/phpunit/tests/theme/wpThemeJson.php Tabby cat --- tests/phpunit/tests/theme/wpThemeJson.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/phpunit/tests/theme/wpThemeJson.php b/tests/phpunit/tests/theme/wpThemeJson.php index 884b14df23a1f..8fe8c7d5d0d65 100644 --- a/tests/phpunit/tests/theme/wpThemeJson.php +++ b/tests/phpunit/tests/theme/wpThemeJson.php @@ -2506,7 +2506,7 @@ public function test_return_block_node_paths() { * This test covers `get_block_nodes` with the `$include_node_paths_only` * and `include_block_style_variations` options. * - * @ticket 62399 + * @ticket 62399 */ public function test_return_block_node_paths_with_variations() { $theme_json = new ReflectionClass( 'WP_Theme_JSON' );