Make WordPress Core

Changeset 55912


Ignore:
Timestamp:
06/14/2023 07:40:50 AM (20 months ago)
Author:
oandregal
Message:

Ignore unregistered block style variations from theme.json.

This PR makes sure unregistered block style variations declared via theme.json are ignored. It fixes an issue by style variations don't work in the editor and CSS rules without a selector are output to the front-end.

Props isabel_brison.
Fixes #58462.

Location:
trunk
Files:
2 edited

Legend:

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

    r55907 r55912  
    590590        $valid_block_names   = array_keys( static::get_blocks_metadata() );
    591591        $valid_element_names = array_keys( static::ELEMENTS );
    592         $theme_json          = static::sanitize( $this->theme_json, $valid_block_names, $valid_element_names );
    593         $this->theme_json    = static::maybe_opt_in_into_settings( $theme_json );
     592        $valid_variations    = array();
     593        foreach ( self::get_blocks_metadata() as $block_name => $block_meta ) {
     594            if ( ! isset( $block_meta['styleVariations'] ) ) {
     595                continue;
     596            }
     597            $valid_variations[ $block_name ] = array_keys( $block_meta['styleVariations'] );
     598        }
     599        $theme_json       = static::sanitize( $this->theme_json, $valid_block_names, $valid_element_names, $valid_variations );
     600        $this->theme_json = static::maybe_opt_in_into_settings( $theme_json );
    594601
    595602        // Internally, presets are keyed by origin.
     
    669676     * @param array $valid_block_names   List of valid block names.
    670677     * @param array $valid_element_names List of valid element names.
     678     * @param array $valid_variations List of valid variations per block.
    671679     * @return array The sanitized output.
    672680     */
    673     protected static function sanitize( $input, $valid_block_names, $valid_element_names ) {
     681    protected static function sanitize( $input, $valid_block_names, $valid_element_names, $valid_variations ) {
    674682
    675683        $output = array();
     
    729737            if (
    730738                ! empty( $input['styles']['blocks'][ $block ]['variations'] ) &&
    731                 is_array( $input['styles']['blocks'][ $block ]['variations'] )
     739                is_array( $input['styles']['blocks'][ $block ]['variations'] ) &&
     740                isset( $valid_variations[ $block ] )
    732741            ) {
    733                 $style_variation_names = array_keys( $input['styles']['blocks'][ $block ]['variations'] );
     742                $style_variation_names = array_intersect(
     743                    array_keys( $input['styles']['blocks'][ $block ]['variations'] ),
     744                    $valid_variations[ $block ]
     745                );
    734746            }
    735747
     
    28532865        $valid_block_names   = array_keys( static::get_blocks_metadata() );
    28542866        $valid_element_names = array_keys( static::ELEMENTS );
    2855 
    2856         $theme_json = static::sanitize( $theme_json, $valid_block_names, $valid_element_names );
     2867        $valid_variations    = array();
     2868        foreach ( self::get_blocks_metadata() as $block_name => $block_meta ) {
     2869            if ( ! isset( $block_meta['styleVariations'] ) ) {
     2870                continue;
     2871            }
     2872            $valid_variations[ $block_name ] = array_keys( $block_meta['styleVariations'] );
     2873        }
     2874
     2875        $theme_json = static::sanitize( $theme_json, $valid_block_names, $valid_element_names, $valid_variations );
    28572876
    28582877        $blocks_metadata = static::get_blocks_metadata();
  • trunk/tests/phpunit/tests/theme/wpThemeJson.php

    r55468 r55912  
    36993699    }
    37003700
     3701    /*
     3702     * @ticket 58462
     3703     */
     3704    public function test_sanitize_for_unregistered_style_variations() {
     3705        $theme_json = new WP_Theme_JSON(
     3706            array(
     3707                'version' => 2,
     3708                'styles'  => array(
     3709                    'blocks' => array(
     3710                        'core/quote' => array(
     3711                            'variations' => array(
     3712                                'unregisteredVariation' => array(
     3713                                    'color' => array(
     3714                                        'background' => 'hotpink',
     3715                                    ),
     3716                                ),
     3717                                'plain'                 => array(
     3718                                    'color' => array(
     3719                                        'background' => 'hotpink',
     3720                                    ),
     3721                                ),
     3722                            ),
     3723                        ),
     3724                    ),
     3725                ),
     3726            )
     3727        );
     3728
     3729        $sanitized_theme_json = $theme_json->get_raw_data();
     3730        $expected             = array(
     3731            'version' => 2,
     3732            'styles'  => array(
     3733                'blocks' => array(
     3734                    'core/quote' => array(
     3735                        'variations' => array(
     3736                            'plain' => array(
     3737                                'color' => array(
     3738                                    'background' => 'hotpink',
     3739                                ),
     3740                            ),
     3741                        ),
     3742                    ),
     3743                ),
     3744            ),
     3745        );
     3746        $this->assertSameSetsWithIndex( $expected, $sanitized_theme_json, 'Sanitized theme.json styles does not match' );
     3747    }
     3748
    37013749    /**
    37023750     * @ticket 57583
     
    37333781    public function data_sanitize_for_block_with_style_variations() {
    37343782        return array(
    3735             '1 variation with 1 invalid property'   => array(
     3783            '1 variation with 1 valid property'     => array(
    37363784                'theme_json_variations' => array(
    37373785                    'variations' => array(
     
    37833831                ),
    37843832            ),
    3785             '2 variations with 1 invalid property'  => array(
    3786                 'theme_json_variations' => array(
    3787                     'variations' => array(
    3788                         'plain' => array(
    3789                             'color'            => array(
    3790                                 'background' => 'hotpink',
    3791                             ),
    3792                             'invalidProperty1' => 'value1',
    3793                         ),
    3794                         'basic' => array(
    3795                             'color' => array(
    3796                                 'background' => '#ffffff',
    3797                                 'text'       => '#000000',
    3798                             ),
    3799                             'foo'   => 'bar',
    3800                         ),
    3801                     ),
    3802                 ),
    3803                 'expected_sanitized'    => array(
    3804                     'blocks' => array(
    3805                         'core/quote' => array(
    3806                             'variations' => array(
    3807                                 'plain' => array(
    3808                                     'color' => array(
    3809                                         'background' => 'hotpink',
    3810                                     ),
    3811                                 ),
    3812                                 'basic' => array(
    3813                                     'color' => array(
    3814                                         'background' => '#ffffff',
    3815                                         'text'       => '#000000',
    3816                                     ),
    3817                                 ),
    3818                             ),
    3819                         ),
    3820                     ),
    3821                 ),
    3822             ),
    38233833        );
    38243834    }
     
    39133923            ),
    39143924            'styles'   => '.is-style-plain.is-style-plain.wp-block-quote{background-color: hotpink;}',
    3915         );
    3916         $basic = array(
    3917             'metadata' => array(
    3918                 'path'     => array( 'styles', 'blocks', 'core/quote', 'variations', 'basic' ),
    3919                 'selector' => '.is-style-basic.is-style-basic.wp-block-quote',
    3920             ),
    3921             'styles'   => '.is-style-basic.is-style-basic.wp-block-quote{background-color: #ffffff;color: #000000;}',
    39223925        );
    39233926
     
    39503953                'metadata_variation'    => array( $plain['metadata'] ),
    39513954                'expected'              => $plain['styles'],
    3952             ),
    3953             '2 variations with 1 invalid property'  => array(
    3954                 'theme_json_variations' => array(
    3955                     'variations' => array(
    3956                         'plain' => array(
    3957                             'color'            => array(
    3958                                 'background' => 'hotpink',
    3959                             ),
    3960                             'invalidProperty1' => 'value1',
    3961                         ),
    3962                         'basic' => array(
    3963                             'color' => array(
    3964                                 'background' => '#ffffff',
    3965                                 'text'       => '#000000',
    3966                             ),
    3967                             'foo'   => 'bar',
    3968                         ),
    3969                     ),
    3970                 ),
    3971                 'metadata_variation'    => array( $plain['metadata'], $basic['metadata'] ),
    3972                 'expected_styles'       => $plain['styles'] . $basic['styles'],
    3973             ),
    3974             '2 variations with multiple invalid properties' => array(
    3975                 'theme_json_variations' => array(
    3976                     'variations' => array(
    3977                         'plain' => array(
    3978                             'color'            => array(
    3979                                 'background' => 'hotpink',
    3980                             ),
    3981                             'invalidProperty1' => 'value1',
    3982                             'invalidProperty2' => 'value2',
    3983                         ),
    3984                         'basic' => array(
    3985                             'foo'   => 'foo',
    3986                             'color' => array(
    3987                                 'background' => '#ffffff',
    3988                                 'text'       => '#000000',
    3989                             ),
    3990                             'bar'   => 'bar',
    3991                             'baz'   => 'baz',
    3992                         ),
    3993                     ),
    3994                 ),
    3995                 'metadata_variation'    => array( $plain['metadata'], $basic['metadata'] ),
    3996                 'expected_styles'       => $plain['styles'] . $basic['styles'],
    39973955            ),
    39983956        );
Note: See TracChangeset for help on using the changeset viewer.