Make WordPress Core


Ignore:
Timestamp:
06/14/2023 07:40:50 AM (3 years 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.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • 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.