Skip to content

Commit 90582b1

Browse files
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: #14094 Props westonruter, ramonopoly, andrewserong, talldanwp, sakibmd, testerrahul. Fixes #66269. git-svn-id: https://develop.svn.wordpress.org/trunk@65798 602fd350-edb4-49c9-b593-d223f7449a82
1 parent cd4fa0d commit 90582b1

14 files changed

Lines changed: 1030 additions & 148 deletions

‎src/wp-includes/block-supports/background.php‎

Lines changed: 84 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,67 @@ function wp_register_background_support( $block_type ) {
3434
}
3535
}
3636

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+
*/
50+
function 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;
96+
}
97+
3798
/**
3899
* Renders the background styles to the block wrapper.
39100
* This block support uses the `render_block` hook to ensure that
@@ -91,61 +152,47 @@ function wp_render_background_support( $block_content, $block ) {
91152
return $block_content;
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-
}
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+
);
110165
}
111-
}
112166

113-
if ( ! $skip_background_gradient ) {
114-
$background_styles['gradient'] = $block_attributes['style']['background']['gradient'] ?? null;
115-
}
167+
if ( $skip_background_gradient ) {
168+
unset( $block_attributes['style']['background']['gradient'] );
169+
}
116170

117-
if ( ! $skip_background_clip ) {
118-
$background_styles['backgroundClip'] = $block_attributes['style']['background']['backgroundClip'] ?? null;
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+
}
119175
}
120176

121-
$styles = wp_style_engine_get_styles( array( 'background' => $background_styles ) );
177+
$styles = wp_get_background_classes_and_styles( $block_attributes );
122178

123-
if ( ! empty( $styles['css'] ) ) {
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 );
126182

127183
if ( $tags->next_tag() ) {
128184
$existing_style = $tags->get_attribute( 'style' );
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
}
151198

‎src/wp-includes/block-supports/border.php‎

Lines changed: 96 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -52,76 +52,114 @@ function wp_apply_border_support( $block_type, $block_attributes ) {
5252
return array();
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-
}
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+
}
7072

71-
$border_block_styles['radius'] = $border_radius;
73+
if ( ! $has_border_style_support || $skip_style ) {
74+
unset( $block_attributes['style']['border']['style'] );
7275
}
7376

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'];
77+
if ( ! $has_border_width_support || $skip_width ) {
78+
unset( $block_attributes['style']['border']['width'] );
8179
}
8280

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'];
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+
}
90101

91-
// This check handles original unitless implementation.
92-
if ( is_numeric( $border_width ) ) {
93-
$border_width .= 'px';
102+
if ( $skip_style ) {
103+
unset( $block_attributes['style']['border'][ $side ]['style'] );
94104
}
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+
*/
123+
function 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();
95132

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;
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;
119153
}
154+
$border_styles[ $side ] = array(
155+
'width' => $border[ $side ]['width'] ?? null,
156+
'color' => $border[ $side ]['color'] ?? null,
157+
'style' => $border[ $side ]['style'] ?? null,
158+
);
120159
}
121160

122-
// Collect classes and styles.
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'] ) ) {
127165
$attributes['class'] = $styles['classnames'];

0 commit comments

Comments
 (0)