Skip to content

Commit a4d85e7

Browse files
Block Supports: Add PHP helpers for block support classes and styles.
1 parent 6d8ee2a commit a4d85e7

14 files changed

Lines changed: 1209 additions & 134 deletions

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

Lines changed: 79 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,60 @@ 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.2.0
43+
*
44+
* @param array $block_attributes Block attributes.
45+
* @return array Array with `class` and `style` keys, each present only when non-empty.
46+
*/
47+
function wp_get_background_classes_and_styles( $block_attributes ) {
48+
$background = $block_attributes['style']['background'] ?? null;
49+
50+
if ( ! is_array( $background ) ) {
51+
return array();
52+
}
53+
54+
$background_styles = array(
55+
'backgroundImage' => $background['backgroundImage'] ?? null,
56+
'backgroundSize' => $background['backgroundSize'] ?? null,
57+
'backgroundPosition' => $background['backgroundPosition'] ?? null,
58+
'backgroundRepeat' => $background['backgroundRepeat'] ?? null,
59+
'backgroundAttachment' => $background['backgroundAttachment'] ?? null,
60+
'gradient' => $background['gradient'] ?? null,
61+
'backgroundClip' => $background['backgroundClip'] ?? null,
62+
);
63+
64+
if ( ! empty( $background_styles['backgroundImage'] ) ) {
65+
$background_styles['backgroundSize'] ??= 'cover';
66+
if ( 'contain' === $background_styles['backgroundSize'] && ! $background_styles['backgroundPosition'] ) {
67+
$background_styles['backgroundPosition'] = '50% 50%';
68+
}
69+
}
70+
71+
$styles = wp_style_engine_get_styles( array( 'background' => $background_styles ) );
72+
73+
if ( empty( $styles['css'] ) ) {
74+
return array();
75+
}
76+
77+
$classes_and_styles = array();
78+
79+
$has_background = ! empty( $background_styles['backgroundImage'] ) || ! empty( $background_styles['gradient'] );
80+
81+
// A text clip paints the glyphs, not the box, so there is no block background.
82+
if ( $has_background && 'text' !== $background_styles['backgroundClip'] ) {
83+
$classes_and_styles['class'] = 'has-background';
84+
}
85+
86+
$classes_and_styles['style'] = $styles['css'];
87+
88+
return $classes_and_styles;
89+
}
90+
3791
/**
3892
* Renders the background styles to the block wrapper.
3993
* This block support uses the `render_block` hook to ensure that
@@ -75,61 +129,49 @@ function wp_render_background_support( $block_content, $block ) {
75129
return $block_content;
76130
}
77131

78-
$background_styles = array();
79-
80-
if ( ! $skip_background_image ) {
81-
$background_styles['backgroundImage'] = $block_attributes['style']['background']['backgroundImage'] ?? null;
82-
$background_styles['backgroundSize'] = $block_attributes['style']['background']['backgroundSize'] ?? null;
83-
$background_styles['backgroundPosition'] = $block_attributes['style']['background']['backgroundPosition'] ?? null;
84-
$background_styles['backgroundRepeat'] = $block_attributes['style']['background']['backgroundRepeat'] ?? null;
85-
$background_styles['backgroundAttachment'] = $block_attributes['style']['background']['backgroundAttachment'] ?? null;
86-
87-
if ( ! empty( $background_styles['backgroundImage'] ) ) {
88-
$background_styles['backgroundSize'] ??= 'cover';
89-
90-
// If the background size is set to `contain` and no position is set, set the position to `center`.
91-
if ( 'contain' === $background_styles['backgroundSize'] && ! $background_styles['backgroundPosition'] ) {
92-
$background_styles['backgroundPosition'] = '50% 50%';
93-
}
132+
$attributes = $block_attributes;
133+
134+
// The helper returns nothing for a non-array background, so only an array needs filtering.
135+
if ( is_array( $attributes['style']['background'] ) ) {
136+
if ( $skip_background_image ) {
137+
unset(
138+
$attributes['style']['background']['backgroundImage'],
139+
$attributes['style']['background']['backgroundSize'],
140+
$attributes['style']['background']['backgroundPosition'],
141+
$attributes['style']['background']['backgroundRepeat'],
142+
$attributes['style']['background']['backgroundAttachment']
143+
);
94144
}
95-
}
96145

97-
if ( ! $skip_background_gradient ) {
98-
$background_styles['gradient'] = $block_attributes['style']['background']['gradient'] ?? null;
99-
}
146+
if ( $skip_background_gradient ) {
147+
unset( $attributes['style']['background']['gradient'] );
148+
}
100149

101-
if ( ! $skip_background_clip ) {
102-
$background_styles['backgroundClip'] = $block_attributes['style']['background']['backgroundClip'] ?? null;
150+
// Serialized values only, so a skipped clip cannot drop `has-background`.
151+
if ( $skip_background_clip ) {
152+
unset( $attributes['style']['background']['backgroundClip'] );
153+
}
103154
}
104155

105-
$styles = wp_style_engine_get_styles( array( 'background' => $background_styles ) );
156+
$styles = wp_get_background_classes_and_styles( $attributes );
106157

107-
if ( ! empty( $styles['css'] ) ) {
158+
if ( ! empty( $styles['style'] ) ) {
108159
// Inject background styles to the first element, presuming it's the wrapper, if it exists.
109160
$tags = new WP_HTML_Tag_Processor( $block_content );
110161

111162
if ( $tags->next_tag() ) {
112163
$existing_style = $tags->get_attribute( 'style' );
113164
if ( is_string( $existing_style ) && '' !== $existing_style ) {
114165
$separator = str_ends_with( $existing_style, ';' ) ? '' : ';';
115-
$updated_style = "{$existing_style}{$separator}{$styles['css']}";
166+
$updated_style = "{$existing_style}{$separator}{$styles['style']}";
116167
} else {
117-
$updated_style = $styles['css'];
168+
$updated_style = $styles['style'];
118169
}
119170

120171
$tags->set_attribute( 'style', $updated_style );
121172

122-
/*
123-
* A background clipped to the text paints the glyphs rather than the
124-
* block's box, so there is no block background to announce. Read the
125-
* clip from the serialized styles so an unsupported or skipped value
126-
* left in the block's attributes cannot suppress the class.
127-
*/
128-
$background_clip = $background_styles['backgroundClip'] ?? null;
129-
$has_background = ! empty( $background_styles['backgroundImage'] ) || ! empty( $background_styles['gradient'] );
130-
131-
if ( $has_background && 'text' !== $background_clip ) {
132-
$tags->add_class( 'has-background' );
173+
if ( ! empty( $styles['class'] ) ) {
174+
$tags->add_class( $styles['class'] );
133175
}
134176
}
135177

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

Lines changed: 78 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -52,76 +52,104 @@ function wp_apply_border_support( $block_type, $block_attributes ) {
5252
return array();
5353
}
5454

55-
$border_block_styles = array();
5655
$has_border_color_support = wp_has_border_feature_support( $block_type, 'color' );
5756
$has_border_width_support = wp_has_border_feature_support( $block_type, 'width' );
57+
$attributes = $block_attributes;
5858

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'];
59+
if ( ! isset( $attributes['style']['border'] ) || ! is_array( $attributes['style']['border'] ) ) {
60+
unset( $attributes['style'] );
61+
}
6662

67-
if ( is_numeric( $border_radius ) ) {
68-
$border_radius .= 'px';
63+
foreach ( array( 'radius', 'style', 'width', 'color' ) as $feature ) {
64+
if (
65+
! wp_has_border_feature_support( $block_type, $feature ) ||
66+
wp_should_skip_block_supports_serialization( $block_type, '__experimentalBorder', $feature )
67+
) {
68+
unset( $attributes['style']['border'][ $feature ] );
6969
}
70-
71-
$border_block_styles['radius'] = $border_radius;
7270
}
7371

74-
// Border style.
7572
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' )
73+
! $has_border_color_support ||
74+
wp_should_skip_block_supports_serialization( $block_type, '__experimentalBorder', 'color' )
7975
) {
80-
$border_block_styles['style'] = $block_attributes['style']['border']['style'];
76+
unset( $attributes['borderColor'] );
8177
}
8278

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'];
79+
// Sides are filtered by skipped serialization only, not by feature support.
80+
$skipped_side_features = array();
81+
foreach ( array( 'width', 'color', 'style' ) as $feature ) {
82+
if ( wp_should_skip_block_supports_serialization( $block_type, '__experimentalBorder', $feature ) ) {
83+
$skipped_side_features[] = $feature;
84+
}
85+
}
9086

91-
// This check handles original unitless implementation.
92-
if ( is_numeric( $border_width ) ) {
93-
$border_width .= 'px';
87+
foreach ( array( 'top', 'right', 'bottom', 'left' ) as $side ) {
88+
if (
89+
! ( $has_border_color_support || $has_border_width_support ) ||
90+
! isset( $attributes['style']['border'][ $side ] ) ||
91+
! is_array( $attributes['style']['border'][ $side ] )
92+
) {
93+
unset( $attributes['style']['border'][ $side ] );
94+
continue;
95+
}
96+
foreach ( $skipped_side_features as $feature ) {
97+
unset( $attributes['style']['border'][ $side ][ $feature ] );
9498
}
99+
}
100+
101+
return wp_get_border_classes_and_styles( $attributes );
102+
}
103+
104+
/**
105+
* Returns border classes and inline styles for block attributes, like the JS
106+
* `getBorderClassesAndStyles()`. Does not check block support or skipped
107+
* serialization.
108+
*
109+
* @since 7.2.0
110+
*
111+
* @param array $block_attributes Block attributes.
112+
* @return array Array with `class` and `style` keys, each present only when non-empty.
113+
*/
114+
function wp_get_border_classes_and_styles( $block_attributes ) {
115+
if ( ! is_array( $block_attributes ) ) {
116+
return array();
117+
}
118+
119+
$border = isset( $block_attributes['style']['border'] ) && is_array( $block_attributes['style']['border'] )
120+
? $block_attributes['style']['border']
121+
: array();
122+
$border_styles = array();
95123

96-
$border_block_styles['width'] = $border_width;
124+
// Unitless radius and width are from the original implementation.
125+
if ( isset( $border['radius'] ) ) {
126+
$border_styles['radius'] = is_numeric( $border['radius'] ) ? "{$border['radius']}px" : $border['radius'];
97127
}
98128

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;
129+
if ( isset( $border['style'] ) ) {
130+
$border_styles['style'] = $border['style'];
131+
}
132+
133+
if ( isset( $border['width'] ) ) {
134+
$border_styles['width'] = is_numeric( $border['width'] ) ? "{$border['width']}px" : $border['width'];
135+
}
136+
137+
$preset_border_color = array_key_exists( 'borderColor', $block_attributes ) ? "var:preset|color|{$block_attributes['borderColor']}" : null;
138+
$border_styles['color'] = $preset_border_color ? $preset_border_color : ( $border['color'] ?? null );
139+
140+
foreach ( array( 'top', 'right', 'bottom', 'left' ) as $side ) {
141+
if ( ! isset( $border[ $side ] ) || ! is_array( $border[ $side ] ) ) {
142+
continue;
119143
}
144+
$border_styles[ $side ] = array(
145+
'width' => $border[ $side ]['width'] ?? null,
146+
'color' => $border[ $side ]['color'] ?? null,
147+
'style' => $border[ $side ]['style'] ?? null,
148+
);
120149
}
121150

122-
// Collect classes and styles.
123151
$attributes = array();
124-
$styles = wp_style_engine_get_styles( array( 'border' => $border_block_styles ) );
152+
$styles = wp_style_engine_get_styles( array( 'border' => $border_styles ) );
125153

126154
if ( ! empty( $styles['classnames'] ) ) {
127155
$attributes['class'] = $styles['classnames'];

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

Lines changed: 52 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -97,37 +97,63 @@ function wp_apply_colors_support( $block_type, $block_attributes ) {
9797
( isset( $color_support['background'] ) && $color_support['background'] ) ||
9898
( is_array( $color_support ) && ! isset( $color_support['background'] ) );
9999
$has_gradients_support = $color_support['gradients'] ?? false;
100-
$color_block_styles = array();
101100

102-
// Text colors.
103-
if ( $has_text_colors_support && ! wp_should_skip_block_supports_serialization( $block_type, 'color', 'text' ) ) {
104-
$preset_text_color = array_key_exists( 'textColor', $block_attributes ) ? "var:preset|color|{$block_attributes['textColor']}" : null;
105-
$custom_text_color = $block_attributes['style']['color']['text'] ?? null;
106-
$color_block_styles['text'] = $preset_text_color ? $preset_text_color : $custom_text_color;
107-
}
101+
// background.php owns the CSS when a background gradient is set, so skip color.gradient.
102+
$has_background_gradient_support = block_has_support( $block_type, array( 'background', 'gradient' ), false );
103+
$has_background_gradient_value = ! empty( $block_attributes['style']['background']['gradient'] );
108104

109-
// Background colors.
110-
if ( $has_background_colors_support && ! wp_should_skip_block_supports_serialization( $block_type, 'color', 'background' ) ) {
111-
$preset_background_color = array_key_exists( 'backgroundColor', $block_attributes ) ? "var:preset|color|{$block_attributes['backgroundColor']}" : null;
112-
$custom_background_color = $block_attributes['style']['color']['background'] ?? null;
113-
$color_block_styles['background'] = $preset_background_color ? $preset_background_color : $custom_background_color;
105+
$features = array(
106+
'text' => array( 'textColor', $has_text_colors_support && ! wp_should_skip_block_supports_serialization( $block_type, 'color', 'text' ) ),
107+
'background' => array( 'backgroundColor', $has_background_colors_support && ! wp_should_skip_block_supports_serialization( $block_type, 'color', 'background' ) ),
108+
'gradient' => array(
109+
'gradient',
110+
$has_gradients_support &&
111+
! wp_should_skip_block_supports_serialization( $block_type, 'color', 'gradients' ) &&
112+
! ( $has_background_gradient_support && $has_background_gradient_value ),
113+
),
114+
);
115+
$attributes = $block_attributes;
116+
$has_color_styles = isset( $attributes['style']['color'] ) && is_array( $attributes['style']['color'] );
117+
118+
foreach ( $features as $feature => list( $preset_attribute, $is_allowed ) ) {
119+
if ( $is_allowed ) {
120+
continue;
121+
}
122+
unset( $attributes[ $preset_attribute ] );
123+
if ( $has_color_styles ) {
124+
unset( $attributes['style']['color'][ $feature ] );
125+
}
114126
}
115127

116-
// Gradients.
117-
// Suppress color.gradient CSS when background.gradient is supported and
118-
// explicitly set. background.php owns CSS generation in that case, and
119-
// emitting the background shorthand here would conflict with it.
120-
$has_background_gradient_support = block_has_support( $block_type, array( 'background', 'gradient' ), false );
121-
$has_background_gradient_value = ! empty( $block_attributes['style']['background']['gradient'] );
128+
return wp_get_color_classes_and_styles( $attributes );
129+
}
122130

123-
if (
124-
$has_gradients_support &&
125-
! wp_should_skip_block_supports_serialization( $block_type, 'color', 'gradients' ) &&
126-
! ( $has_background_gradient_support && $has_background_gradient_value )
127-
) {
128-
$preset_gradient_color = array_key_exists( 'gradient', $block_attributes ) ? "var:preset|gradient|{$block_attributes['gradient']}" : null;
129-
$custom_gradient_color = $block_attributes['style']['color']['gradient'] ?? null;
130-
$color_block_styles['gradient'] = $preset_gradient_color ? $preset_gradient_color : $custom_gradient_color;
131+
/**
132+
* Returns color classes and inline styles for block attributes, like the JS
133+
* `getColorClassesAndStyles()`. Does not check block support or skipped
134+
* serialization.
135+
*
136+
* @since 7.2.0
137+
*
138+
* @param array $block_attributes Block attributes.
139+
* @return array Array with `class` and `style` keys, each present only when non-empty.
140+
*/
141+
function wp_get_color_classes_and_styles( $block_attributes ) {
142+
if ( ! is_array( $block_attributes ) ) {
143+
return array();
144+
}
145+
146+
$color_block_styles = array();
147+
$preset_attributes = array(
148+
'text' => array( 'textColor', 'color' ),
149+
'background' => array( 'backgroundColor', 'color' ),
150+
'gradient' => array( 'gradient', 'gradient' ),
151+
);
152+
153+
foreach ( $preset_attributes as $feature => list( $preset_attribute, $preset_type ) ) {
154+
$preset_value = array_key_exists( $preset_attribute, $block_attributes ) ? "var:preset|{$preset_type}|{$block_attributes[ $preset_attribute ]}" : null;
155+
$custom_value = $block_attributes['style']['color'][ $feature ] ?? null;
156+
$color_block_styles[ $feature ] = $preset_value ?? $custom_value;
131157
}
132158

133159
$attributes = array();

0 commit comments

Comments
 (0)