Skip to content

Commit 482b786

Browse files
committed
Backporting the current state of WordPress/wordpress-develop#7486 and adding tests.
1 parent e86152b commit 482b786

File tree

2 files changed

+120
-39
lines changed

2 files changed

+120
-39
lines changed

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

Lines changed: 68 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -2719,15 +2719,17 @@ private static function update_separator_declarations( $declarations ) {
27192719
/**
27202720
* An internal method to get the block nodes from a theme.json file.
27212721
*
2722-
* @since 6.1.0
2723-
*
27242722
* @param array $theme_json The theme.json converted to an array.
27252723
* @param array $selectors Optional list of selectors per block.
2726-
* @param array $options An array of options to facilitate filtering node generation
2727-
* The options currently supported are:
2728-
* - `include_block_style_variations` which includes CSS for block style variations.
2724+
* @param array $options {
2725+
* Optional. An array of options for now used for internal purposes only (may change without notice).
2726+
*
2727+
* @type bool $include_block_style_variations Includes nodes for block style variations. Default false.
2728+
* @type bool $include_node_paths_only Return only block nodes node paths. Default false.
2729+
* }
27292730
* @return array The block nodes in theme.json.
27302731
*/
2732+
27312733
private static function get_block_nodes( $theme_json, $selectors = array(), $options = array() ) {
27322734
$selectors = empty( $selectors ) ? static::get_blocks_metadata() : $selectors;
27332735
$nodes = array();
@@ -2741,55 +2743,78 @@ private static function get_block_nodes( $theme_json, $selectors = array(), $opt
27412743
}
27422744

27432745
foreach ( $theme_json['styles']['blocks'] as $name => $node ) {
2744-
$selector = null;
2745-
if ( isset( $selectors[ $name ]['selector'] ) ) {
2746-
$selector = $selectors[ $name ]['selector'];
2747-
}
2748-
2749-
$duotone_selector = null;
2750-
if ( isset( $selectors[ $name ]['duotone'] ) ) {
2751-
$duotone_selector = $selectors[ $name ]['duotone'];
2752-
}
2746+
$include_node_paths_only = $options['include_node_paths_only'] ?? false;
2747+
$node_path = array( 'styles', 'blocks', $name );
2748+
if ( $include_node_paths_only ) {
2749+
$nodes[] = array(
2750+
'path' => $node_path,
2751+
);
2752+
} else {
2753+
$selector = null;
2754+
if ( isset( $selectors[ $name ]['selector'] ) ) {
2755+
$selector = $selectors[ $name ]['selector'];
2756+
}
27532757

2754-
$feature_selectors = null;
2755-
if ( isset( $selectors[ $name ]['selectors'] ) ) {
2756-
$feature_selectors = $selectors[ $name ]['selectors'];
2757-
}
2758+
$duotone_selector = null;
2759+
if ( isset( $selectors[ $name ]['duotone'] ) ) {
2760+
$duotone_selector = $selectors[ $name ]['duotone'];
2761+
}
27582762

2759-
$variation_selectors = array();
2760-
$include_variations = $options['include_block_style_variations'] ?? false;
2761-
if ( $include_variations && isset( $node['variations'] ) ) {
2762-
foreach ( $node['variations'] as $variation => $node ) {
2763-
$variation_selectors[] = array(
2764-
'path' => array( 'styles', 'blocks', $name, 'variations', $variation ),
2765-
'selector' => $selectors[ $name ]['styleVariations'][ $variation ],
2766-
);
2763+
$feature_selectors = null;
2764+
if ( isset( $selectors[ $name ]['selectors'] ) ) {
2765+
$feature_selectors = $selectors[ $name ]['selectors'];
27672766
}
2768-
}
27692767

2770-
$nodes[] = array(
2771-
'name' => $name,
2772-
'path' => array( 'styles', 'blocks', $name ),
2773-
'selector' => $selector,
2774-
'selectors' => $feature_selectors,
2775-
'duotone' => $duotone_selector,
2776-
'variations' => $variation_selectors,
2777-
'css' => $selector,
2778-
);
2768+
$variation_selectors = array();
2769+
$include_variations = $options['include_block_style_variations'] ?? false;
2770+
if ( $include_variations && isset( $node['variations'] ) ) {
2771+
foreach ( $node['variations'] as $variation => $node ) {
2772+
$variation_selectors[] = array(
2773+
'path' => array( 'styles', 'blocks', $name, 'variations', $variation ),
2774+
'selector' => $selectors[ $name ]['styleVariations'][ $variation ],
2775+
);
2776+
}
2777+
}
27792778

2779+
$nodes[] = array(
2780+
'name' => $name,
2781+
'path' => $node_path,
2782+
'selector' => $selector,
2783+
'selectors' => $feature_selectors,
2784+
'duotone' => $duotone_selector,
2785+
'variations' => $variation_selectors,
2786+
'css' => $selector,
2787+
);
2788+
}
27802789
if ( isset( $theme_json['styles']['blocks'][ $name ]['elements'] ) ) {
27812790
foreach ( $theme_json['styles']['blocks'][ $name ]['elements'] as $element => $node ) {
2791+
$node_path = array( 'styles', 'blocks', $name, 'elements', $element );
2792+
if ( $include_node_paths_only ) {
2793+
$nodes[] = array(
2794+
'path' => $node_path,
2795+
);
2796+
continue;
2797+
}
2798+
27822799
$nodes[] = array(
2783-
'path' => array( 'styles', 'blocks', $name, 'elements', $element ),
2800+
'path' => $node_path,
27842801
'selector' => $selectors[ $name ]['elements'][ $element ],
27852802
);
27862803

27872804
// Handle any pseudo selectors for the element.
27882805
if ( isset( static::VALID_ELEMENT_PSEUDO_SELECTORS[ $element ] ) ) {
27892806
foreach ( static::VALID_ELEMENT_PSEUDO_SELECTORS[ $element ] as $pseudo_selector ) {
27902807
if ( isset( $theme_json['styles']['blocks'][ $name ]['elements'][ $element ][ $pseudo_selector ] ) ) {
2808+
$node_path = array( 'styles', 'blocks', $name, 'elements', $element );
2809+
if ( $include_node_paths_only ) {
2810+
$nodes[] = array(
2811+
'path' => $node_path,
2812+
);
2813+
continue;
2814+
}
2815+
27912816
$nodes[] = array(
2792-
'path' => array( 'styles', 'blocks', $name, 'elements', $element ),
2817+
'path' => $node_path,
27932818
'selector' => static::append_to_selector( $selectors[ $name ]['elements'][ $element ], $pseudo_selector ),
27942819
);
27952820
}
@@ -3264,7 +3289,11 @@ public function merge( $incoming ) {
32643289
* some values provide exceptions, namely style values that are
32653290
* objects and represent unique definitions for the style.
32663291
*/
3267-
$style_nodes = static::get_styles_block_nodes();
3292+
$style_nodes = static::get_block_nodes(
3293+
$this->theme_json,
3294+
array(),
3295+
array( 'include_node_paths_only' => true )
3296+
);
32683297
foreach ( $style_nodes as $style_node ) {
32693298
$path = $style_node['path'];
32703299
/*

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

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5726,4 +5726,56 @@ public function test_opt_in_to_block_style_variations() {
57265726

57275727
$this->assertEquals( $expected, $button_variations );
57285728
}
5729+
5730+
/**
5731+
* This test covers `get_block_nodes` with the `$include_node_paths_only` option.
5732+
* When `true`, `$include_node_paths_only` should return only the paths of the block nodes.
5733+
*/
5734+
public function test_return_block_node_paths() {
5735+
$theme_json = new ReflectionClass( 'WP_Theme_JSON_Gutenberg' );
5736+
5737+
$func = $theme_json->getMethod( 'get_block_nodes' );
5738+
$func->setAccessible( true );
5739+
5740+
$theme_json = array(
5741+
'version' => WP_Theme_JSON_Gutenberg::LATEST_SCHEMA,
5742+
'styles' => array(
5743+
'typography' => array(
5744+
'fontSize' => '16px',
5745+
),
5746+
'blocks' => array(
5747+
'core/button' => array(
5748+
'color' => array(
5749+
'background' => 'red',
5750+
),
5751+
),
5752+
'core/group' => array(
5753+
'elements' => array(
5754+
'link' => array(
5755+
'color' => array(
5756+
'background' => 'blue',
5757+
),
5758+
),
5759+
),
5760+
),
5761+
),
5762+
),
5763+
);
5764+
5765+
$block_nodes = $func->invoke( null, $theme_json, array(), array( 'include_node_paths_only' => true ) );
5766+
5767+
$expected = array(
5768+
array(
5769+
'path' => array( 'styles', 'blocks', 'core/button' ),
5770+
),
5771+
array(
5772+
'path' => array( 'styles', 'blocks', 'core/group' ),
5773+
),
5774+
array(
5775+
'path' => array( 'styles', 'blocks', 'core/group', 'elements', 'link' ),
5776+
),
5777+
);
5778+
5779+
$this->assertEquals( $expected, $block_nodes );
5780+
}
57295781
}

0 commit comments

Comments
 (0)