Make WordPress Core

Changeset 65798


Ignore:
Timestamp:
11/26/2026 06:51:16 AM (less than one hour ago)
Author:
aaronrobertshaw
Message:

Editor: Add PHP helpers for block support classes and styles.

Adds wp_get_border_classes_and_styles(), wp_get_shadow_classes_and_styles(), wp_get_spacing_classes_and_styles(), wp_get_color_classes_and_styles() and wp_get_background_classes_and_styles(). Each takes a block's attributes and returns the classes and styles for that block support, similar to the JavaScript get*ClassesAndStyles() functions.

Blocks that skip serialization of a block support can call these helpers instead of rebuilding the classes and styles by hand. The border, shadow, spacing, color and background block supports now build their output with them.

The block support output is unchanged with one exception. A null, false, empty or zero preset color now falls back to the custom color, as it does in the editor.

Developed in: ​https://github.com/WordPress/wordpress-develop/pull/14094

Props westonruter, ramonopoly, andrewserong, talldanwp, sakibmd, testerrahul.
Fixes #66269.

Location:
trunk
Files:
5 added
9 edited

Legend:

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

    r64590 r65798  
    3333                );
    3434        }
     35}
     36
     37/**
     38 * Returns background classes and inline styles for block attributes, like the JS
     39 * `getBackgroundClassesAndStyles()`. Does not check block support or skipped
     40 * serialization.
     41 *
     42 * @since 7.3.0
     43 *
     44 * @param array<string, mixed> $block_attributes Block attributes.
     45 * @return array<string, string> Array with `class` and `style` keys, each present only when non-empty.
     46 *
     47 * @phpstan-param array{ style?: array<string, mixed>, ...<string, mixed> } $block_attributes
     48 * @phpstan-return array{ class?: 'has-background', style?: non-empty-string }
     49 */
     50function wp_get_background_classes_and_styles( $block_attributes ) {
     51        if ( ! is_array( $block_attributes ) ) {
     52                return array();
     53        }
     54
     55        $background = $block_attributes['style']['background'] ?? null;
     56
     57        if ( ! is_array( $background ) ) {
     58                return array();
     59        }
     60
     61        $background_styles = array(
     62                'backgroundImage'      => $background['backgroundImage'] ?? null,
     63                'backgroundSize'       => $background['backgroundSize'] ?? null,
     64                'backgroundPosition'   => $background['backgroundPosition'] ?? null,
     65                'backgroundRepeat'     => $background['backgroundRepeat'] ?? null,
     66                'backgroundAttachment' => $background['backgroundAttachment'] ?? null,
     67                'gradient'             => $background['gradient'] ?? null,
     68                'backgroundClip'       => $background['backgroundClip'] ?? null,
     69        );
     70
     71        if ( ! empty( $background_styles['backgroundImage'] ) ) {
     72                $background_styles['backgroundSize'] ??= 'cover';
     73                if ( 'contain' === $background_styles['backgroundSize'] && ! $background_styles['backgroundPosition'] ) {
     74                        $background_styles['backgroundPosition'] = '50% 50%';
     75                }
     76        }
     77
     78        $styles = wp_style_engine_get_styles( array( 'background' => $background_styles ) );
     79
     80        if ( empty( $styles['css'] ) ) {
     81                return array();
     82        }
     83
     84        $attributes = array();
     85
     86        $has_background = ! empty( $background_styles['backgroundImage'] ) || ! empty( $background_styles['gradient'] );
     87
     88        // A text clip paints the glyphs, not the box, so there is no block background.
     89        if ( $has_background && 'text' !== $background_styles['backgroundClip'] ) {
     90                $attributes['class'] = 'has-background';
     91        }
     92
     93        $attributes['style'] = $styles['css'];
     94
     95        return $attributes;
    3596}
    3697
    … …  
    92153        }
    93154
    94         $background_styles = array();
    95 
    96         if ( ! $skip_background_image ) {
    97                 $background_styles['backgroundImage']      = $block_attributes['style']['background']['backgroundImage'] ?? null;
    98                 $background_styles['backgroundSize']       = $block_attributes['style']['background']['backgroundSize'] ?? null;
    99                 $background_styles['backgroundPosition']   = $block_attributes['style']['background']['backgroundPosition'] ?? null;
    100                 $background_styles['backgroundRepeat']     = $block_attributes['style']['background']['backgroundRepeat'] ?? null;
    101                 $background_styles['backgroundAttachment'] = $block_attributes['style']['background']['backgroundAttachment'] ?? null;
    102 
    103                 if ( ! empty( $background_styles['backgroundImage'] ) ) {
    104                         $background_styles['backgroundSize'] ??= 'cover';
    105 
    106                         // If the background size is set to `contain` and no position is set, set the position to `center`.
    107                         if ( 'contain' === $background_styles['backgroundSize'] && ! $background_styles['backgroundPosition'] ) {
    108                                 $background_styles['backgroundPosition'] = '50% 50%';
    109                         }
    110                 }
    111         }
    112 
    113         if ( ! $skip_background_gradient ) {
    114                 $background_styles['gradient'] = $block_attributes['style']['background']['gradient'] ?? null;
    115         }
    116 
    117         if ( ! $skip_background_clip ) {
    118                 $background_styles['backgroundClip'] = $block_attributes['style']['background']['backgroundClip'] ?? null;
    119         }
    120 
    121         $styles = wp_style_engine_get_styles( array( 'background' => $background_styles ) );
    122 
    123         if ( ! empty( $styles['css'] ) ) {
     155        // unset() fatals on a string, and the helper ignores one anyway.
     156        if ( is_array( $block_attributes['style']['background'] ) ) {
     157                if ( $skip_background_image ) {
     158                        unset(
     159                                $block_attributes['style']['background']['backgroundImage'],
     160                                $block_attributes['style']['background']['backgroundSize'],
     161                                $block_attributes['style']['background']['backgroundPosition'],
     162                                $block_attributes['style']['background']['backgroundRepeat'],
     163                                $block_attributes['style']['background']['backgroundAttachment']
     164                        );
     165                }
     166
     167                if ( $skip_background_gradient ) {
     168                        unset( $block_attributes['style']['background']['gradient'] );
     169                }
     170
     171                // Serialized values only, so a skipped clip cannot drop `has-background`.
     172                if ( $skip_background_clip ) {
     173                        unset( $block_attributes['style']['background']['backgroundClip'] );
     174                }
     175        }
     176
     177        $styles = wp_get_background_classes_and_styles( $block_attributes );
     178
     179        if ( ! empty( $styles['style'] ) ) {
    124180                // Inject background styles to the first element, presuming it's the wrapper, if it exists.
    125181                $tags = new WP_HTML_Tag_Processor( $block_content );
    … …  
    129185                        if ( is_string( $existing_style ) && '' !== $existing_style ) {
    130186                                $separator     = str_ends_with( $existing_style, ';' ) ? '' : ';';
    131                                 $updated_style = "{$existing_style}{$separator}{$styles['css']}";
     187                                $updated_style = "{$existing_style}{$separator}{$styles['style']}";
    132188                        } else {
    133                                 $updated_style = $styles['css'];
     189                                $updated_style = $styles['style'];
    134190                        }
    135191
    136192                        $tags->set_attribute( 'style', $updated_style );
    137193
    138                         /*
    139                          * A background clipped to the text paints the glyphs rather than the
    140                          * block's box, so there is no block background to announce. Read the
    141                          * clip from the serialized styles so an unsupported or skipped value
    142                          * left in the block's attributes cannot suppress the class.
    143                          */
    144                         $background_clip = $background_styles['backgroundClip'] ?? null;
    145                         $has_background  = ! empty( $background_styles['backgroundImage'] ) || ! empty( $background_styles['gradient'] );
    146 
    147                         if ( $has_background && 'text' !== $background_clip ) {
    148                                 $tags->add_class( 'has-background' );
     194                        if ( ! empty( $styles['class'] ) ) {
     195                                $tags->add_class( $styles['class'] );
    149196                        }
    150197                }
  • trunk/src/wp-includes/block-supports/border.php

    r61430 r65798  
    5353        }
    5454
    55         $border_block_styles      = array();
    56         $has_border_color_support = wp_has_border_feature_support( $block_type, 'color' );
    57         $has_border_width_support = wp_has_border_feature_support( $block_type, 'width' );
    58 
    59         // Border radius.
    60         if (
    61                 wp_has_border_feature_support( $block_type, 'radius' ) &&
    62                 isset( $block_attributes['style']['border']['radius'] ) &&
    63                 ! wp_should_skip_block_supports_serialization( $block_type, '__experimentalBorder', 'radius' )
    64         ) {
    65                 $border_radius = $block_attributes['style']['border']['radius'];
    66 
    67                 if ( is_numeric( $border_radius ) ) {
    68                         $border_radius .= 'px';
    69                 }
    70 
    71                 $border_block_styles['radius'] = $border_radius;
    72         }
    73 
    74         // Border style.
    75         if (
    76                 wp_has_border_feature_support( $block_type, 'style' ) &&
    77                 isset( $block_attributes['style']['border']['style'] ) &&
    78                 ! wp_should_skip_block_supports_serialization( $block_type, '__experimentalBorder', 'style' )
    79         ) {
    80                 $border_block_styles['style'] = $block_attributes['style']['border']['style'];
    81         }
    82 
    83         // Border width.
    84         if (
    85                 $has_border_width_support &&
    86                 isset( $block_attributes['style']['border']['width'] ) &&
    87                 ! wp_should_skip_block_supports_serialization( $block_type, '__experimentalBorder', 'width' )
    88         ) {
    89                 $border_width = $block_attributes['style']['border']['width'];
    90 
    91                 // This check handles original unitless implementation.
    92                 if ( is_numeric( $border_width ) ) {
    93                         $border_width .= 'px';
    94                 }
    95 
    96                 $border_block_styles['width'] = $border_width;
    97         }
    98 
    99         // Border color.
    100         if (
    101                 $has_border_color_support &&
    102                 ! wp_should_skip_block_supports_serialization( $block_type, '__experimentalBorder', 'color' )
    103         ) {
    104                 $preset_border_color          = array_key_exists( 'borderColor', $block_attributes ) ? "var:preset|color|{$block_attributes['borderColor']}" : null;
    105                 $custom_border_color          = $block_attributes['style']['border']['color'] ?? null;
    106                 $border_block_styles['color'] = $preset_border_color ? $preset_border_color : $custom_border_color;
    107         }
    108 
    109         // Generates styles for individual border sides.
    110         if ( $has_border_color_support || $has_border_width_support ) {
    111                 foreach ( array( 'top', 'right', 'bottom', 'left' ) as $side ) {
    112                         $border                       = $block_attributes['style']['border'][ $side ] ?? null;
    113                         $border_side_values           = array(
    114                                 'width' => isset( $border['width'] ) && ! wp_should_skip_block_supports_serialization( $block_type, '__experimentalBorder', 'width' ) ? $border['width'] : null,
    115                                 'color' => isset( $border['color'] ) && ! wp_should_skip_block_supports_serialization( $block_type, '__experimentalBorder', 'color' ) ? $border['color'] : null,
    116                                 'style' => isset( $border['style'] ) && ! wp_should_skip_block_supports_serialization( $block_type, '__experimentalBorder', 'style' ) ? $border['style'] : null,
    117                         );
    118                         $border_block_styles[ $side ] = $border_side_values;
    119                 }
    120         }
    121 
    122         // Collect classes and styles.
     55        $has_border_color_support  = wp_has_border_feature_support( $block_type, 'color' );
     56        $has_border_radius_support = wp_has_border_feature_support( $block_type, 'radius' );
     57        $has_border_style_support  = wp_has_border_feature_support( $block_type, 'style' );
     58        $has_border_width_support  = wp_has_border_feature_support( $block_type, 'width' );
     59        $skip_color                = wp_should_skip_block_supports_serialization( $block_type, '__experimentalBorder', 'color' );
     60        $skip_radius               = wp_should_skip_block_supports_serialization( $block_type, '__experimentalBorder', 'radius' );
     61        $skip_style                = wp_should_skip_block_supports_serialization( $block_type, '__experimentalBorder', 'style' );
     62        $skip_width                = wp_should_skip_block_supports_serialization( $block_type, '__experimentalBorder', 'width' );
     63
     64        // The unset() calls below fail on a non-array style or border.
     65        if ( ! isset( $block_attributes['style']['border'] ) || ! is_array( $block_attributes['style']['border'] ) ) {
     66                unset( $block_attributes['style'] );
     67        }
     68
     69        if ( ! $has_border_radius_support || $skip_radius ) {
     70                unset( $block_attributes['style']['border']['radius'] );
     71        }
     72
     73        if ( ! $has_border_style_support || $skip_style ) {
     74                unset( $block_attributes['style']['border']['style'] );
     75        }
     76
     77        if ( ! $has_border_width_support || $skip_width ) {
     78                unset( $block_attributes['style']['border']['width'] );
     79        }
     80
     81        if ( ! $has_border_color_support || $skip_color ) {
     82                unset( $block_attributes['style']['border']['color'], $block_attributes['borderColor'] );
     83        }
     84
     85        // Side values follow skipped serialization only, as on trunk.
     86        foreach ( array( 'top', 'right', 'bottom', 'left' ) as $side ) {
     87                $has_side = isset( $block_attributes['style']['border'][ $side ] ) && is_array( $block_attributes['style']['border'][ $side ] );
     88
     89                if ( ! $has_side || ( ! $has_border_color_support && ! $has_border_width_support ) ) {
     90                        unset( $block_attributes['style']['border'][ $side ] );
     91                        continue;
     92                }
     93
     94                if ( $skip_width ) {
     95                        unset( $block_attributes['style']['border'][ $side ]['width'] );
     96                }
     97
     98                if ( $skip_color ) {
     99                        unset( $block_attributes['style']['border'][ $side ]['color'] );
     100                }
     101
     102                if ( $skip_style ) {
     103                        unset( $block_attributes['style']['border'][ $side ]['style'] );
     104                }
     105        }
     106
     107        return wp_get_border_classes_and_styles( $block_attributes );
     108}
     109
     110/**
     111 * Returns border classes and inline styles for block attributes, like the JS
     112 * `getBorderClassesAndStyles()`. Does not check block support or skipped
     113 * serialization.
     114 *
     115 * @since 7.3.0
     116 *
     117 * @param array<string, mixed> $block_attributes Block attributes.
     118 * @return array<string, string> Array with `class` and `style` keys, each present only when non-empty.
     119 *
     120 * @phpstan-param array{ style?: array<string, mixed>, ...<string, mixed> } $block_attributes
     121 * @phpstan-return array{ class?: non-empty-string, style?: non-empty-string }
     122 */
     123function wp_get_border_classes_and_styles( $block_attributes ) {
     124        if ( ! is_array( $block_attributes ) ) {
     125                return array();
     126        }
     127
     128        $border        = isset( $block_attributes['style']['border'] ) && is_array( $block_attributes['style']['border'] )
     129                ? $block_attributes['style']['border']
     130                : array();
     131        $border_styles = array();
     132
     133        // Unitless radius and width are from the original implementation.
     134        if ( isset( $border['radius'] ) ) {
     135                $border_styles['radius'] = is_numeric( $border['radius'] ) ? "{$border['radius']}px" : $border['radius'];
     136        }
     137
     138        if ( isset( $border['style'] ) ) {
     139                $border_styles['style'] = $border['style'];
     140        }
     141
     142        if ( isset( $border['width'] ) ) {
     143                $border_styles['width'] = is_numeric( $border['width'] ) ? "{$border['width']}px" : $border['width'];
     144        }
     145
     146        // Preset slugs are strings in theme.json and the editor.
     147        $preset_slug            = $block_attributes['borderColor'] ?? null;
     148        $border_styles['color'] = is_string( $preset_slug ) && '' !== $preset_slug ? "var:preset|color|{$preset_slug}" : ( $border['color'] ?? null );
     149
     150        foreach ( array( 'top', 'right', 'bottom', 'left' ) as $side ) {
     151                if ( ! isset( $border[ $side ] ) || ! is_array( $border[ $side ] ) ) {
     152                        continue;
     153                }
     154                $border_styles[ $side ] = array(
     155                        'width' => $border[ $side ]['width'] ?? null,
     156                        'color' => $border[ $side ]['color'] ?? null,
     157                        'style' => $border[ $side ]['style'] ?? null,
     158                );
     159        }
     160
    123161        $attributes = array();
    124         $styles     = wp_style_engine_get_styles( array( 'border' => $border_block_styles ) );
     162        $styles     = wp_style_engine_get_styles( array( 'border' => $border_styles ) );
    125163
    126164        if ( ! empty( $styles['classnames'] ) ) {
  • trunk/src/wp-includes/block-supports/colors.php

    r64590 r65798  
    9999                ( is_array( $color_support ) && ! isset( $color_support['background'] ) );
    100100        $has_gradients_support         = $color_support['gradients'] ?? false;
    101         $color_block_styles            = array();
    102 
    103         // Text colors.
    104         if ( $has_text_colors_support && ! wp_should_skip_block_supports_serialization( $block_type, 'color', 'text' ) ) {
    105                 $preset_text_color          = array_key_exists( 'textColor', $block_attributes ) ? "var:preset|color|{$block_attributes['textColor']}" : null;
    106                 $custom_text_color          = $block_attributes['style']['color']['text'] ?? null;
    107                 $color_block_styles['text'] = $preset_text_color ? $preset_text_color : $custom_text_color;
    108         }
    109 
    110         // Background colors.
    111         if ( $has_background_colors_support && ! wp_should_skip_block_supports_serialization( $block_type, 'color', 'background' ) ) {
    112                 $preset_background_color          = array_key_exists( 'backgroundColor', $block_attributes ) ? "var:preset|color|{$block_attributes['backgroundColor']}" : null;
    113                 $custom_background_color          = $block_attributes['style']['color']['background'] ?? null;
    114                 $color_block_styles['background'] = $preset_background_color ? $preset_background_color : $custom_background_color;
    115         }
    116 
    117         // Gradients.
    118         // Suppress color.gradient CSS when background.gradient is supported and
    119         // explicitly set. background.php owns CSS generation in that case, and
    120         // emitting the background shorthand here would conflict with it.
     101        $skip_text                     = wp_should_skip_block_supports_serialization( $block_type, 'color', 'text' );
     102        $skip_background               = wp_should_skip_block_supports_serialization( $block_type, 'color', 'background' );
     103        $skip_gradients                = wp_should_skip_block_supports_serialization( $block_type, 'color', 'gradients' );
     104
     105        // background.php owns the CSS when a background gradient is set, so skip color.gradient.
    121106        $has_background_gradient_support = block_has_support( $block_type, array( 'background', 'gradient' ), false );
    122107        $has_background_gradient_value   = ! empty( $block_attributes['style']['background']['gradient'] );
    123 
    124         if (
    125                 $has_gradients_support &&
    126                 ! wp_should_skip_block_supports_serialization( $block_type, 'color', 'gradients' ) &&
    127                 ! ( $has_background_gradient_support && $has_background_gradient_value )
    128         ) {
    129                 $preset_gradient_color          = array_key_exists( 'gradient', $block_attributes ) ? "var:preset|gradient|{$block_attributes['gradient']}" : null;
    130                 $custom_gradient_color          = $block_attributes['style']['color']['gradient'] ?? null;
    131                 $color_block_styles['gradient'] = $preset_gradient_color ? $preset_gradient_color : $custom_gradient_color;
    132         }
    133 
    134         $attributes = array();
    135         $styles     = wp_style_engine_get_styles( array( 'color' => $color_block_styles ), array( 'convert_vars_to_classnames' => true ) );
     108        $has_background_gradient         = $has_background_gradient_support && $has_background_gradient_value;
    136109
    137110        /*
    138111         * Mirror of the background support: a color clipped to the text paints the
    139112         * glyphs, not the box, so only a gradient still adds `has-background`.
     113         * Read before the attributes are filtered for the helper.
    140114         */
    141115        $clips_to_text = 'text' === ( $block_attributes['style']['background']['backgroundClip'] ?? null ) &&
    … …  
    143117                ! wp_should_skip_block_supports_serialization( $block_type, 'background', 'backgroundClip' );
    144118
    145         if ( $clips_to_text && ! empty( $styles['classnames'] ) && empty( $color_block_styles['gradient'] ) ) {
    146                 $styles['classnames'] = implode( ' ', array_diff( explode( ' ', $styles['classnames'] ), array( 'has-background' ) ) );
    147         }
     119        // The helper reads only `style.color`, so a non-array value counts as no color styles.
     120        if ( ! isset( $block_attributes['style']['color'] ) || ! is_array( $block_attributes['style']['color'] ) ) {
     121                unset( $block_attributes['style'] );
     122        }
     123
     124        if ( ! $has_text_colors_support || $skip_text ) {
     125                unset( $block_attributes['textColor'], $block_attributes['style']['color']['text'] );
     126        }
     127
     128        if ( ! $has_background_colors_support || $skip_background ) {
     129                unset( $block_attributes['backgroundColor'], $block_attributes['style']['color']['background'] );
     130        }
     131
     132        if ( ! $has_gradients_support || $skip_gradients || $has_background_gradient ) {
     133                unset( $block_attributes['gradient'], $block_attributes['style']['color']['gradient'] );
     134        }
     135
     136        $classes_and_styles = wp_get_color_classes_and_styles( $block_attributes );
     137        $has_gradient       = ! empty( $block_attributes['gradient'] ) || ! empty( $block_attributes['style']['color']['gradient'] );
     138
     139        if ( $clips_to_text && ! $has_gradient && ! empty( $classes_and_styles['class'] ) ) {
     140                $classes = array_diff( explode( ' ', $classes_and_styles['class'] ), array( 'has-background' ) );
     141                if ( empty( $classes ) ) {
     142                        unset( $classes_and_styles['class'] );
     143                } else {
     144                        $classes_and_styles['class'] = implode( ' ', $classes );
     145                }
     146        }
     147
     148        return $classes_and_styles;
     149}
     150
     151/**
     152 * Returns color classes and inline styles for block attributes, like the JS
     153 * `getColorClassesAndStyles()`. Does not check block support or skipped
     154 * serialization.
     155 *
     156 * @since 7.3.0
     157 *
     158 * @param array<string, mixed> $block_attributes Block attributes.
     159 * @return array<string, string> Array with `class` and `style` keys, each present only when non-empty.
     160 *
     161 * @phpstan-param array{ style?: array{ color?: array<string, mixed>, ...<string, mixed> }, ...<string, mixed> } $block_attributes
     162 * @phpstan-return array{ class?: non-empty-string, style?: non-empty-string }
     163 */
     164function wp_get_color_classes_and_styles( $block_attributes ) {
     165        if ( ! is_array( $block_attributes ) ) {
     166                return array();
     167        }
     168
     169        $color_block_styles = array();
     170        $preset_attributes  = array(
     171                'text'       => array( 'textColor', 'color' ),
     172                'background' => array( 'backgroundColor', 'color' ),
     173                'gradient'   => array( 'gradient', 'gradient' ),
     174        );
     175
     176        foreach ( $preset_attributes as $feature => list( $preset_attribute, $preset_type ) ) {
     177                $preset_slug = $block_attributes[ $preset_attribute ] ?? null;
     178
     179                // Preset slugs are strings in theme.json and the editor.
     180                $preset_value                   = is_string( $preset_slug ) && '' !== $preset_slug ? "var:preset|{$preset_type}|{$preset_slug}" : null;
     181                $custom_value                   = $block_attributes['style']['color'][ $feature ] ?? null;
     182                $color_block_styles[ $feature ] = $preset_value ?? $custom_value;
     183        }
     184
     185        $attributes = array();
     186        $styles     = wp_style_engine_get_styles( array( 'color' => $color_block_styles ), array( 'convert_vars_to_classnames' => true ) );
    148187
    149188        if ( ! empty( $styles['classnames'] ) ) {
  • trunk/src/wp-includes/block-supports/shadow.php

    r64727 r65798  
    6161        }
    6262
    63         $shadow_block_styles = array();
     63        return wp_get_shadow_classes_and_styles( $block_attributes );
     64}
    6465
    65         $custom_shadow                 = $block_attributes['style']['shadow'] ?? null;
    66         $shadow_block_styles['shadow'] = $custom_shadow;
     66/**
     67 * Returns shadow classes and inline styles for block attributes, like the JS
     68 * `getShadowClassesAndStyles()`. Does not check block support or skipped
     69 * serialization.
     70 *
     71 * @since 7.3.0
     72 *
     73 * @param array<string, mixed> $block_attributes Block attributes.
     74 * @return array<string, string> Array with `class` and `style` keys, each present only when non-empty.
     75 *
     76 * @phpstan-param array{ style?: array<string, mixed>, ...<string, mixed> } $block_attributes
     77 * @phpstan-return array{ class?: non-empty-string, style?: non-empty-string }
     78 */
     79function wp_get_shadow_classes_and_styles( $block_attributes ) {
     80        if ( ! is_array( $block_attributes ) ) {
     81                return array();
     82        }
     83
     84        $shadow = $block_attributes['style']['shadow'] ?? null;
    6785
    6886        $attributes = array();
    69         $styles     = wp_style_engine_get_styles( $shadow_block_styles );
     87        $styles     = wp_style_engine_get_styles( array( 'shadow' => $shadow ) );
     88
     89        if ( ! empty( $styles['classnames'] ) ) {
     90                $attributes['class'] = $styles['classnames'];
     91        }
    7092
    7193        if ( ! empty( $styles['css'] ) ) {
  • trunk/src/wp-includes/block-supports/spacing.php

    r61430 r65798  
    5050        }
    5151
    52         $attributes          = array();
     52        if ( ! isset( $block_attributes['style']['spacing'] ) || ! is_array( $block_attributes['style']['spacing'] ) ) {
     53                return array();
     54        }
     55
    5356        $has_padding_support = block_has_support( $block_type, array( 'spacing', 'padding' ), false );
    5457        $has_margin_support  = block_has_support( $block_type, array( 'spacing', 'margin' ), false );
    55         $block_styles        = $block_attributes['style'] ?? null;
     58        $skip_padding        = wp_should_skip_block_supports_serialization( $block_type, 'spacing', 'padding' );
     59        $skip_margin         = wp_should_skip_block_supports_serialization( $block_type, 'spacing', 'margin' );
    5660
    57         if ( ! $block_styles ) {
    58                 return $attributes;
     61        if ( ! $has_padding_support || $skip_padding ) {
     62                unset( $block_attributes['style']['spacing']['padding'] );
    5963        }
    6064
    61         $skip_padding         = wp_should_skip_block_supports_serialization( $block_type, 'spacing', 'padding' );
    62         $skip_margin          = wp_should_skip_block_supports_serialization( $block_type, 'spacing', 'margin' );
    63         $spacing_block_styles = array(
    64                 'padding' => null,
    65                 'margin'  => null,
     65        if ( ! $has_margin_support || $skip_margin ) {
     66                unset( $block_attributes['style']['spacing']['margin'] );
     67        }
     68
     69        return wp_get_spacing_classes_and_styles( $block_attributes );
     70}
     71
     72/**
     73 * Returns spacing classes and inline styles for block attributes, like the JS
     74 * `getSpacingClassesAndStyles()`. Does not check block support or skipped
     75 * serialization.
     76 *
     77 * @since 7.3.0
     78 *
     79 * @param array<string, mixed> $block_attributes Block attributes.
     80 * @return array<string, string> Array with `class` and `style` keys, each present only when non-empty.
     81 *
     82 * @phpstan-param array{ style?: array<string, mixed>, ...<string, mixed> } $block_attributes
     83 * @phpstan-return array{ class?: non-empty-string, style?: non-empty-string }
     84 */
     85function wp_get_spacing_classes_and_styles( $block_attributes ) {
     86        if ( ! is_array( $block_attributes ) ) {
     87                return array();
     88        }
     89
     90        $spacing = $block_attributes['style']['spacing'] ?? null;
     91        if ( ! is_array( $spacing ) ) {
     92                return array();
     93        }
     94
     95        $styles     = wp_style_engine_get_styles(
     96                array(
     97                        'spacing' => array(
     98                                'padding' => $spacing['padding'] ?? null,
     99                                'margin'  => $spacing['margin'] ?? null,
     100                        ),
     101                )
    66102        );
    67         if ( $has_padding_support && ! $skip_padding ) {
    68                 $spacing_block_styles['padding'] = $block_styles['spacing']['padding'] ?? null;
     103        $attributes = array();
     104
     105        if ( ! empty( $styles['classnames'] ) ) {
     106                $attributes['class'] = $styles['classnames'];
    69107        }
    70         if ( $has_margin_support && ! $skip_margin ) {
    71                 $spacing_block_styles['margin'] = $block_styles['spacing']['margin'] ?? null;
    72         }
    73         $styles = wp_style_engine_get_styles( array( 'spacing' => $spacing_block_styles ) );
    74108
    75109        if ( ! empty( $styles['css'] ) ) {
  • trunk/tests/phpunit/tests/block-supports/border.php

    r58181 r65798  
    2323
    2424        /**
     25         * Registers a new block for testing border support.
     26         *
     27         * @param string $block_name Name for the test block.
     28         * @param array  $supports   Array defining block support configuration.
     29         * @return WP_Block_Type The block type for the newly registered test block.
     30         */
     31        private function register_bordered_block_with_support( $block_name, $supports = array() ) {
     32                $this->test_block_name = $block_name;
     33                register_block_type(
     34                        $this->test_block_name,
     35                        array(
     36                                'api_version' => 3,
     37                                'attributes'  => array(
     38                                        'borderColor' => array(
     39                                                'type' => 'string',
     40                                        ),
     41                                        'style'       => array(
     42                                                'type' => 'object',
     43                                        ),
     44                                ),
     45                                'supports'    => $supports,
     46                        )
     47                );
     48                $registry = WP_Block_Type_Registry::get_instance();
     49
     50                return $registry->get_registered( $this->test_block_name );
     51        }
     52
     53        /**
    2554         * @ticket 55505
    2655         */
    … …  
    160189                $this->assertSame( $expected, $actual );
    161190        }
     191
     192        /**
     193         * @ticket 66269
     194         */
     195        public function test_border_color_preset_with_skipped_color_serialization() {
     196                $block_type  = $this->register_bordered_block_with_support(
     197                        'test/border-color-preset-with-skipped-color-serialization',
     198                        array(
     199                                '__experimentalBorder' => array(
     200                                        'color'                           => true,
     201                                        'width'                           => true,
     202                                        '__experimentalSkipSerialization' => array( 'color' ),
     203                                ),
     204                        )
     205                );
     206                $block_attrs = array(
     207                        'borderColor' => 'red',
     208                        'style'       => array( 'border' => array( 'width' => '1px' ) ),
     209                );
     210
     211                $this->assertSame(
     212                        array( 'style' => 'border-width:1px;' ),
     213                        wp_apply_border_support( $block_type, $block_attrs )
     214                );
     215        }
     216
     217        /**
     218         * @ticket 66269
     219         */
     220        public function test_split_borders_without_color_or_width_support_are_dropped() {
     221                $block_type  = $this->register_bordered_block_with_support(
     222                        'test/split-borders-without-color-or-width-support',
     223                        array(
     224                                '__experimentalBorder' => array(
     225                                        'radius' => true,
     226                                        'style'  => true,
     227                                ),
     228                        )
     229                );
     230                $block_attrs = array(
     231                        'style' => array(
     232                                'border' => array(
     233                                        'radius' => '5px',
     234                                        'top'    => array(
     235                                                'style' => 'dashed',
     236                                                'width' => '2px',
     237                                        ),
     238                                ),
     239                        ),
     240                );
     241                $actual      = wp_apply_border_support( $block_type, $block_attrs );
     242                $expected    = array(
     243                        'style' => 'border-radius:5px;',
     244                );
     245
     246                $this->assertSame( $expected, $actual );
     247        }
     248
     249        /**
     250         * @ticket 66269
     251         */
     252        public function test_non_array_border_and_sides_are_ignored() {
     253                $block_type = $this->register_bordered_block_with_support(
     254                        'test/non-array-border-and-sides',
     255                        array(
     256                                '__experimentalBorder' => true,
     257                        )
     258                );
     259
     260                $this->assertSame( array(), wp_apply_border_support( $block_type, array( 'style' => 'invalid' ) ) );
     261                $this->assertSame( array(), wp_apply_border_support( $block_type, array( 'style' => array( 'border' => 'invalid' ) ) ) );
     262                $this->assertSame(
     263                        array( 'style' => 'border-bottom-width:1px;' ),
     264                        wp_apply_border_support(
     265                                $block_type,
     266                                array(
     267                                        'style' => array(
     268                                                'border' => array(
     269                                                        'top'    => 'invalid',
     270                                                        'bottom' => array( 'width' => '1px' ),
     271                                                ),
     272                                        ),
     273                                )
     274                        )
     275                );
     276        }
    162277}
  • trunk/tests/phpunit/tests/block-supports/colors.php

    r64741 r65798  
    408408                );
    409409        }
     410
     411        /**
     412         * @ticket 66269
     413         */
     414        public function test_color_support_true_applies_text_and_background_colors() {
     415                $this->test_block_name = 'test/color-support-true';
     416                register_block_type(
     417                        $this->test_block_name,
     418                        array(
     419                                'api_version' => 3,
     420                                'attributes'  => array(
     421                                        'textColor'       => array(
     422                                                'type' => 'string',
     423                                        ),
     424                                        'backgroundColor' => array(
     425                                                'type' => 'string',
     426                                        ),
     427                                ),
     428                                'supports'    => array(
     429                                        'color' => true,
     430                                ),
     431                        )
     432                );
     433
     434                $registry   = WP_Block_Type_Registry::get_instance();
     435                $block_type = $registry->get_registered( $this->test_block_name );
     436
     437                $block_atts = array(
     438                        'textColor'       => 'contrast',
     439                        'backgroundColor' => 'base',
     440                );
     441
     442                $actual   = wp_apply_colors_support( $block_type, $block_atts );
     443                $expected = array(
     444                        'class' => 'has-text-color has-contrast-color has-background has-base-background-color',
     445                );
     446
     447                $this->assertSame( $expected, $actual );
     448        }
     449
     450        /**
     451         * @ticket 66269
     452         */
     453        public function test_color_text_support_disabled_skips_text_color() {
     454                $this->test_block_name = 'test/color-text-support-disabled';
     455                register_block_type(
     456                        $this->test_block_name,
     457                        array(
     458                                'api_version' => 3,
     459                                'attributes'  => array(
     460                                        'textColor' => array(
     461                                                'type' => 'string',
     462                                        ),
     463                                ),
     464                                'supports'    => array(
     465                                        'color' => array(
     466                                                'text' => false,
     467                                        ),
     468                                ),
     469                        )
     470                );
     471
     472                $registry   = WP_Block_Type_Registry::get_instance();
     473                $block_type = $registry->get_registered( $this->test_block_name );
     474
     475                $block_atts = array(
     476                        'textColor' => 'contrast',
     477                );
     478
     479                $actual   = wp_apply_colors_support( $block_type, $block_atts );
     480                $expected = array();
     481
     482                $this->assertSame( $expected, $actual );
     483        }
    410484}
  • trunk/tests/phpunit/tests/block-supports/spacing.php

    r58181 r65798  
    165165                $this->assertSame( $expected, $actual );
    166166        }
     167
     168        /**
     169         * @ticket 66269
     170         */
     171        public function test_unsupported_padding_is_not_applied() {
     172                $this->test_block_name = 'test/unsupported-padding-is-not-applied';
     173                register_block_type(
     174                        $this->test_block_name,
     175                        array(
     176                                'api_version' => 3,
     177                                'attributes'  => array(
     178                                        'style' => array(
     179                                                'type' => 'object',
     180                                        ),
     181                                ),
     182                                'supports'    => array(
     183                                        'spacing' => array(
     184                                                'margin' => true,
     185                                        ),
     186                                ),
     187                        )
     188                );
     189                $registry   = WP_Block_Type_Registry::get_instance();
     190                $block_type = $registry->get_registered( $this->test_block_name );
     191                $block_atts = array(
     192                        'style' => array(
     193                                'spacing' => array(
     194                                        'margin'  => '1px',
     195                                        'padding' => '2px',
     196                                ),
     197                        ),
     198                );
     199
     200                $actual   = wp_apply_spacing_support( $block_type, $block_atts );
     201                $expected = array(
     202                        'style' => 'margin:1px;',
     203                );
     204
     205                $this->assertSame( $expected, $actual );
     206        }
    167207}
  • trunk/tests/phpunit/tests/block-supports/wpRenderBackgroundSupport.php

    r64816 r65798  
    8787         * @ticket 64974
    8888         * @ticket 66094
     89         * @ticket 66269
    8990         *
    9091         * @covers ::wp_render_background_support
    … …  
    285286                                'wrapper'             => '<div>Content</div>',
    286287                        ),
     288                        'background gradient is applied without the image when the image skips serialization' => array(
     289                                'theme_name'          => 'block-theme-child-with-fluid-typography',
     290                                'block_name'          => 'test/background-image-skipped-gradient-applied',
     291                                'background_settings' => array(
     292                                        'backgroundImage'                 => true,
     293                                        'gradient'                        => true,
     294                                        '__experimentalSkipSerialization' => array( 'backgroundImage' ),
     295                                ),
     296                                'background_style'    => array(
     297                                        'backgroundImage' => array(
     298                                                'url' => 'https://example.com/image.jpg',
     299                                        ),
     300                                        'gradient'        => 'linear-gradient(135deg,rgb(255,0,0) 0%,rgb(0,0,255) 100%)',
     301                                ),
     302                                'expected_wrapper'    => '<div class="has-background" style="background-image:linear-gradient(135deg,rgb(255,0,0) 0%,rgb(0,0,255) 100%);">Content</div>',
     303                                'wrapper'             => '<div>Content</div>',
     304                        ),
    287305                        'background image style is not applied if the block does not support background image' => array(
    288306                                'theme_name'          => 'block-theme-child-with-fluid-typography',
Note: See TracChangeset for help on using the changeset viewer.