Skip to content

Commit

Permalink
Theme JSON: include block style variations in path only output of get…
Browse files Browse the repository at this point in the history
…_block_nodes

An `$include_node_paths_only` option to `get_block_nodes()` was introduced to improve performance.

When set to `true`, this option tells the function to only return paths, and not selectors, for consumers that only needed paths to style values.

For one of the conditional blocks, block style variations wasn't included.

This commit adds them to the array of paths following the existing model `$node[]['path' => [], 'variations' => ['path' => []]]`.

Follow-up to [61858].

Props aaronrobertshaw, ramonopoly.
Fixes #62399.




git-svn-id: https://develop.svn.wordpress.org/trunk@59418 602fd350-edb4-49c9-b593-d223f7449a82
  • Loading branch information
ramonjd committed Nov 19, 2024
1 parent af003c4 commit dec5c90
Show file tree
Hide file tree
Showing 2 changed files with 92 additions and 1 deletion.
14 changes: 13 additions & 1 deletion src/wp-includes/class-wp-theme-json.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'] ) ) {
Expand Down
79 changes: 79 additions & 0 deletions tests/phpunit/tests/theme/wpThemeJson.php
Original file line number Diff line number Diff line change
Expand Up @@ -2502,6 +2502,85 @@ 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.
*
* @ticket 62399
*/
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
*/
Expand Down

0 comments on commit dec5c90

Please sign in to comment.