Make WordPress Core

Changeset 59418


Ignore:
Timestamp:
11/19/2024 04:42:17 AM (2 months ago)
Author:
ramonopoly
Message:

Theme JSON: include block style variations in path only output of get_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.

Location:
trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/class-wp-theme-json.php

    r59359 r59418  
    27232723            $node_path = array( 'styles', 'blocks', $name );
    27242724            if ( $include_node_paths_only ) {
    2725                 $nodes[] = array(
     2725                $variation_paths = array();
     2726                if ( $include_variations && isset( $node['variations'] ) ) {
     2727                    foreach ( $node['variations'] as $variation => $variation_node ) {
     2728                        $variation_paths[] = array(
     2729                            'path' => array( 'styles', 'blocks', $name, 'variations', $variation ),
     2730                        );
     2731                    }
     2732                }
     2733                $node = array(
    27262734                    'path' => $node_path,
    27272735                );
     2736                if ( ! empty( $variation_paths ) ) {
     2737                    $node['variations'] = $variation_paths;
     2738                }
     2739                $nodes[] = $node;
    27282740            } else {
    27292741                $selector = null;
  • trunk/tests/phpunit/tests/theme/wpThemeJson.php

    r59335 r59418  
    24972497            array(
    24982498                'path' => array( 'styles', 'blocks', 'core/group', 'elements', 'link' ),
     2499            ),
     2500        );
     2501
     2502        $this->assertEquals( $expected, $block_nodes );
     2503    }
     2504
     2505    /**
     2506     * This test covers `get_block_nodes` with the `$include_node_paths_only`
     2507     * and `include_block_style_variations` options.
     2508     *
     2509     * @ticket 62399
     2510     */
     2511    public function test_return_block_node_paths_with_variations() {
     2512        $theme_json = new ReflectionClass( 'WP_Theme_JSON' );
     2513
     2514        $func = $theme_json->getMethod( 'get_block_nodes' );
     2515        $func->setAccessible( true );
     2516
     2517        $theme_json = array(
     2518            'version' => WP_Theme_JSON::LATEST_SCHEMA,
     2519            'styles'  => array(
     2520                'typography' => array(
     2521                    'fontSize' => '16px',
     2522                ),
     2523                'blocks'     => array(
     2524                    'core/button' => array(
     2525                        'color'      => array(
     2526                            'background' => 'red',
     2527                        ),
     2528                        'variations' => array(
     2529                            'cheese' => array(
     2530                                'color' => array(
     2531                                    'background' => 'cheese',
     2532                                ),
     2533                            ),
     2534                        ),
     2535                    ),
     2536                    'core/group'  => array(
     2537                        'color'      => array(
     2538                            'background' => 'blue',
     2539                        ),
     2540                        'variations' => array(
     2541                            'apricot' => array(
     2542                                'color' => array(
     2543                                    'background' => 'apricot',
     2544                                ),
     2545                            ),
     2546                        ),
     2547                    ),
     2548                ),
     2549            ),
     2550        );
     2551
     2552        $block_nodes = $func->invoke(
     2553            null,
     2554            $theme_json,
     2555            array(),
     2556            array(
     2557                'include_node_paths_only'        => true,
     2558                'include_block_style_variations' => true,
     2559            )
     2560        );
     2561
     2562        $expected = array(
     2563            array(
     2564                'path'       => array( 'styles', 'blocks', 'core/button' ),
     2565                'variations' => array(
     2566                    array(
     2567                        'path' => array( 'styles', 'blocks', 'core/button', 'variations', 'cheese' ),
     2568                    ),
     2569                ),
     2570            ),
     2571            array(
     2572                'path'       => array( 'styles', 'blocks', 'core/group' ),
     2573                'variations' => array(
     2574                    array(
     2575                        'path' => array( 'styles', 'blocks', 'core/group', 'variations', 'apricot' ),
     2576                    ),
     2577                ),
    24992578            ),
    25002579        );
Note: See TracChangeset for help on using the changeset viewer.