Changeset 55912
- Timestamp:
- 06/14/2023 07:40:50 AM (20 months ago)
- Location:
- trunk
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/wp-includes/class-wp-theme-json.php
r55907 r55912 590 590 $valid_block_names = array_keys( static::get_blocks_metadata() ); 591 591 $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 ); 594 601 595 602 // Internally, presets are keyed by origin. … … 669 676 * @param array $valid_block_names List of valid block names. 670 677 * @param array $valid_element_names List of valid element names. 678 * @param array $valid_variations List of valid variations per block. 671 679 * @return array The sanitized output. 672 680 */ 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 ) { 674 682 675 683 $output = array(); … … 729 737 if ( 730 738 ! 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 ] ) 732 741 ) { 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 ); 734 746 } 735 747 … … 2853 2865 $valid_block_names = array_keys( static::get_blocks_metadata() ); 2854 2866 $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 ); 2857 2876 2858 2877 $blocks_metadata = static::get_blocks_metadata(); -
trunk/tests/phpunit/tests/theme/wpThemeJson.php
r55468 r55912 3699 3699 } 3700 3700 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 3701 3749 /** 3702 3750 * @ticket 57583 … … 3733 3781 public function data_sanitize_for_block_with_style_variations() { 3734 3782 return array( 3735 '1 variation with 1 invalid property'=> array(3783 '1 variation with 1 valid property' => array( 3736 3784 'theme_json_variations' => array( 3737 3785 'variations' => array( … … 3783 3831 ), 3784 3832 ), 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 ),3823 3833 ); 3824 3834 } … … 3913 3923 ), 3914 3924 '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;}',3922 3925 ); 3923 3926 … … 3950 3953 'metadata_variation' => array( $plain['metadata'] ), 3951 3954 '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'],3997 3955 ), 3998 3956 );
Note: See TracChangeset
for help on using the changeset viewer.