Make WordPress Core

Changeset 57716


Ignore:
Timestamp:
02/27/2024 10:13:13 AM (3 years ago)
Author:
youknowriad
Message:

Editor: Check for null values in Theme JSON to cater for blockGap.

When resolving theme.json preset variables, add a check to make sure the value is not empty before we run it through strpos() and preg_match_all().

Props ramonopoly, mukesh27, get_dave.
Fixes #60613.

Location:
trunk
Files:
2 edited

Legend:

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

    r57547 r57716  
    38583858         *
    38593859         * @since 6.3.0
     3860         * @since 6.5.0 Check for empty style before processing its value.
     3861         *
    38603862         * @param array $styles CSS declarations to convert.
    38613863         * @param array $values key => value pairs to use for replacement.
    … …  
    38643866        private static function convert_variables_to_value( $styles, $values ) {
    38653867                foreach ( $styles as $key => $style ) {
     3868                        if ( empty( $style ) ) {
     3869                                continue;
     3870                        }
     3871
    38663872                        if ( is_array( $style ) ) {
    38673873                                $styles[ $key ] = self::convert_variables_to_value( $style, $values );
  • trunk/tests/phpunit/tests/theme/wpThemeJson.php

    r57662 r57716  
    50935093        }
    50945094
     5095        /**
     5096         * Tests that the theme.json file is correctly parsed and the variables are resolved.
     5097         *
     5098         * @ticket 58588
     5099         * @ticket 60613
     5100         *
     5101         * @covers WP_Theme_JSON_Gutenberg::resolve_variables
     5102         * @covers WP_Theme_JSON_Gutenberg::convert_variables_to_value
     5103         */
    50955104        public function test_resolve_variables() {
    50965105                $primary_color   = '#9DFF20';
    … …  
    51005109                $large_font      = '18px';
    51015110                $small_font      = '12px';
     5111                $spacing         = 'clamp(1.5rem, 5vw, 2rem)';
    51025112                $theme_json      = new WP_Theme_JSON(
    51035113                        array(
    … …  
    51365146                                                                'name' => 'Font size large',
    51375147                                                                'slug' => 'large',
     5148                                                        ),
     5149                                                ),
     5150                                        ),
     5151                                        'spacing'    => array(
     5152                                                'spacingSizes' => array(
     5153                                                        array(
     5154                                                                'size' => $spacing,
     5155                                                                'name' => '100',
     5156                                                                'slug' => '100',
    51385157                                                        ),
    51395158                                                ),
    … …  
    52015220                                                        ),
    52025221                                                ),
     5222                                                'core/post-template'   => array(
     5223                                                        'spacing' => array(
     5224                                                                'blockGap' => null,
     5225                                                        ),
     5226                                                ),
     5227                                                'core/columns'         => array(
     5228                                                        'spacing' => array(
     5229                                                                'blockGap' => 'var(--wp--preset--spacing--100)',
     5230                                                        ),
     5231                                                ),
    52035232                                        ),
    52045233                                ),
    … …  
    52445273                $this->assertEquals( $small_font, $styles['blocks']['core/quote']['variations']['plain']['typography']['fontSize'], 'Block variations: font-size' );
    52455274                $this->assertEquals( $secondary_color, $styles['blocks']['core/quote']['variations']['plain']['color']['background'], 'Block variations: color' );
     5275                /*
     5276                 * As with wp_get_global_styles(), WP_Theme_JSON::resolve_variables may be called with merged data from
     5277                 * WP_Theme_JSON_Resolver. WP_Theme_JSON_Resolver::get_block_data() sets blockGap for supported blocks to `null` if the value is not defined.
     5278                 */
     5279                $this->assertNull(
     5280                        $styles['blocks']['core/post-template']['spacing']['blockGap'],
     5281                        'Blocks: Post Template spacing.blockGap should be null'
     5282                );
     5283                $this->assertEquals(
     5284                        $spacing,
     5285                        $styles['blocks']['core/columns']['spacing']['blockGap'],
     5286                        'Blocks: Columns spacing.blockGap should match'
     5287                );
    52465288        }
    52475289
Note: See TracChangeset for help on using the changeset viewer.