Make WordPress Core


Ignore:
Timestamp:
10/07/2026 10:53:31 PM (24 hours ago)
Author:
isabel_brison
Message:

Editor: normalize block gap values in layout and fix grid axial gap.

Ensures incorrect gap values don’t cause errors, fixes grid responsive column sizing when different vertical and horizontal gap values are present and ensures layouts that don’t support axial gap still get correct values.

Props isabel_brison, andrewserong.
Fixes #66263.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/block-supports/layout.php

    r63887 r64238  
    8181 * @since 7.1.0
    8282 *
    83  * @param string|array|null $gap_value Block gap value.
    84  * @return string|array|null Sanitized block gap value.
     83 * Numeric zero is converted to a string because it is valid CSS without a unit.
     84 * Other non-string values are rejected.
     85 *
     86 * @param mixed $gap_value Block gap value.
     87 * @return string|string[]|null Sanitized block gap value.
    8588 */
    8689function wp_sanitize_block_gap_value( $gap_value ) {
    8790        if ( is_array( $gap_value ) ) {
    8891                foreach ( $gap_value as $key => $value ) {
    89                         $gap_value[ $key ] = ! is_scalar( $value ) || ( $value && preg_match( '%[\\\(&=}]|/\*%', (string) $value ) ) ? null : $value;
    90                 }
    91 
    92                 return $gap_value;
     92                        $sanitized_value = wp_sanitize_block_gap_value( $value );
     93                        if ( ! is_string( $sanitized_value ) ) {
     94                                unset( $gap_value[ $key ] );
     95                                continue;
     96                        }
     97                        $gap_value[ $key ] = $sanitized_value;
     98                }
     99
     100                return empty( $gap_value ) ? null : $gap_value;
     101        }
     102
     103        if ( ( is_int( $gap_value ) || is_float( $gap_value ) ) && 0.0 === (float) $gap_value ) {
     104                return '0';
     105        }
     106
     107        if ( ! is_string( $gap_value ) ) {
     108                return null;
     109        }
     110
     111        if ( '' === trim( $gap_value ) ) {
     112                return null;
    93113        }
    94114
    … …  
    491511 * @access private
    492512 *
    493  * @param string               $selector                      CSS selector.
    494  * @param array                $layout                        Layout object. The one that is passed has already checked
    495  *                                                            the existence of default block layout.
    496  * @param bool                 $has_block_gap_support         Optional. Whether the theme has support for the block gap. Default false.
    497  * @param string|string[]|null $gap_value                     Optional. The block gap value to apply. Default null.
    498  * @param bool                 $should_skip_gap_serialization Optional. Whether to skip applying the user-defined value set in the editor. Default false.
    499  * @param string|array         $fallback_gap_value            Optional. The block gap value to apply. If it's an array expected properties are "top" and/or "left". Default '0.5em'.
    500  * @param array|null           $block_spacing                 Optional. Custom spacing set on the block. Default null.
    501  * @param array                $options                       {
     513 * @param string                         $selector                      CSS selector.
     514 * @param array                          $layout                        Layout object. The one that is passed has already checked
     515 *                                                                       the existence of default block layout.
     516 * @param bool                           $has_block_gap_support         Optional. Whether the theme has support for the block gap. Default false.
     517 * @param string|string[]|int|float|null $gap_value                     Optional. The block gap value to apply. Only zero is accepted as a
     518 *                                                                       numeric value. Default null.
     519 * @param bool                           $should_skip_gap_serialization Optional. Whether to skip applying the user-defined value set in the
     520 *                                                                       editor. Default false.
     521 * @param string|string[]|int|float|null $fallback_gap_value            Optional. The fallback block gap value to apply. Only zero is accepted
     522 *                                                                       as a numeric value. Default '0.5em'.
     523 * @param array|null                     $block_spacing                 Optional. Custom spacing set on the block. Default null.
     524 * @param array                          $options                       {
    502525 *     Optional. Extra options for internal callers. Default empty array.
    503526 *
    … …  
    510533 */
    511534function wp_get_layout_style( $selector, $layout, $has_block_gap_support = false, $gap_value = null, $should_skip_gap_serialization = false, $fallback_gap_value = '0.5em', $block_spacing = null, $options = array() ) {
     535        // Normalize here as well as at external data boundaries because this function has direct callers.
     536        $gap_value          = wp_sanitize_block_gap_value( $gap_value );
     537        $fallback_gap_value = wp_sanitize_block_gap_value( $fallback_gap_value ) ?? '0.5em';
     538
    512539        $base_layout             = is_array( $layout ) ? $layout : array();
    513540        $viewport_overrides      = $options['viewport_overrides'] ?? null;
    … …  
    858885
    859886                /*
    860                  * If the gap value is an array, we use the "left" value because it represents the vertical gap, which
     887                 * If the gap value is an array, we use the "left" value because it represents the horizontal gap, which
    861888                 * is the relevant one for computation of responsive grid columns.
    862889                 */
    … …  
    887914                                        $process_value   = "var(--wp--preset--spacing--$slug)";
    888915                                }
     916                                if ( ! is_array( $gap_value ) || 'left' === $gap_side ) {
     917                                        $responsive_gap_value = $process_value;
     918                                }
    889919                                $combined_gap_value .= "$process_value ";
    890920                        }
    891                         $gap_value            = trim( $combined_gap_value );
    892                         $responsive_gap_value = $gap_value;
     921                        $gap_value = trim( $combined_gap_value );
    893922                }
    894923
    … …  
    11971226
    11981227                $gap_value          = wp_sanitize_block_gap_value( $style_attr['spacing']['blockGap'] ?? null );
    1199                 $fallback_gap_value = $block_type->supports['spacing']['blockGap']['__experimentalDefault'] ?? '0.5em';
     1228                $fallback_gap_value = wp_sanitize_block_gap_value(
     1229                        $block_type->supports['spacing']['blockGap']['__experimentalDefault'] ?? null
     1230                ) ?? '0.5em';
    12001231                $block_spacing      = $style_attr['spacing'] ?? null;
    12011232
    … …  
    12311262                }
    12321263
    1233                 $global_block_gap_value = $variation_block_gap_value ?? $global_styles['blocks'][ $block_name ]['spacing']['blockGap'] ?? $global_styles['spacing']['blockGap'] ?? null;
     1264                $global_block_gap_candidates = array(
     1265                        $variation_block_gap_value,
     1266                        $global_styles['blocks'][ $block_name ]['spacing']['blockGap'] ?? null,
     1267                        $global_styles['spacing']['blockGap'] ?? null,
     1268                );
     1269                $global_block_gap_value      = null;
     1270                foreach ( $global_block_gap_candidates as $candidate_gap_value ) {
     1271                        $candidate_gap_value = wp_sanitize_block_gap_value( $candidate_gap_value );
     1272                        if ( null !== $candidate_gap_value ) {
     1273                                $global_block_gap_value = $candidate_gap_value;
     1274                                break;
     1275                        }
     1276                }
    12341277
    12351278                if ( null !== $global_block_gap_value ) {
Note: See TracChangeset for help on using the changeset viewer.