Skip to content

Commit fee7818

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

14 files changed

Lines changed: 1211 additions & 139 deletions

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

Lines changed: 77 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,47 @@ 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+
// The helper ignores a non-array background, so only an array needs filtering.
133+
if ( is_array( $block_attributes['style']['background'] ) ) {
134+
if ( $skip_background_image ) {
135+
unset(
136+
$block_attributes['style']['background']['backgroundImage'],
137+
$block_attributes['style']['background']['backgroundSize'],
138+
$block_attributes['style']['background']['backgroundPosition'],
139+
$block_attributes['style']['background']['backgroundRepeat'],
140+
$block_attributes['style']['background']['backgroundAttachment']
141+
);
94142
}
95-
}
96143

97-
if ( ! $skip_background_gradient ) {
98-
$background_styles['gradient'] = $block_attributes['style']['background']['gradient'] ?? null;
99-
}
144+
if ( $skip_background_gradient ) {
145+
unset( $block_attributes['style']['background']['gradient'] );
146+
}
100147

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

105-
$styles = wp_style_engine_get_styles( array( 'background' => $background_styles ) );
154+
$styles = wp_get_background_classes_and_styles( $block_attributes );
106155

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

111160
if ( $tags->next_tag() ) {
112161
$existing_style = $tags->get_attribute( 'style' );
113162
if ( is_string( $existing_style ) && '' !== $existing_style ) {
114163
$separator = str_ends_with( $existing_style, ';' ) ? '' : ';';
115-
$updated_style = "{$existing_style}{$separator}{$styles['css']}";
164+
$updated_style = "{$existing_style}{$separator}{$styles['style']}";
116165
} else {
117-
$updated_style = $styles['css'];
166+
$updated_style = $styles['style'];
118167
}
119168

120169
$tags->set_attribute( 'style', $updated_style );
121170

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' );
171+
if ( ! empty( $styles['class'] ) ) {
172+
$tags->add_class( $styles['class'] );
133173
}
134174
}
135175

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

Lines changed: 92 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -52,76 +52,110 @@ 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 helper reads only `style.border`, so a non-array value counts as no border styles.
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+
// Sides are filtered by skipped serialization only, not by feature support.
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+
}
9097

91-
// This check handles original unitless implementation.
92-
if ( is_numeric( $border_width ) ) {
93-
$border_width .= 'px';
98+
if ( $skip_color ) {
99+
unset( $block_attributes['style']['border'][ $side ]['color'] );
94100
}
95101

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;
102+
if ( $skip_style ) {
103+
unset( $block_attributes['style']['border'][ $side ]['style'] );
119104
}
120105
}
121106

122-
// Collect classes and styles.
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.2.0
116+
*
117+
* @param array $block_attributes Block attributes.
118+
* @return array Array with `class` and `style` keys, each present only when non-empty.
119+
*/
120+
function wp_get_border_classes_and_styles( $block_attributes ) {
121+
if ( ! is_array( $block_attributes ) ) {
122+
return array();
123+
}
124+
125+
$border = isset( $block_attributes['style']['border'] ) && is_array( $block_attributes['style']['border'] )
126+
? $block_attributes['style']['border']
127+
: array();
128+
$border_styles = array();
129+
130+
// Unitless radius and width are from the original implementation.
131+
if ( isset( $border['radius'] ) ) {
132+
$border_styles['radius'] = is_numeric( $border['radius'] ) ? "{$border['radius']}px" : $border['radius'];
133+
}
134+
135+
if ( isset( $border['style'] ) ) {
136+
$border_styles['style'] = $border['style'];
137+
}
138+
139+
if ( isset( $border['width'] ) ) {
140+
$border_styles['width'] = is_numeric( $border['width'] ) ? "{$border['width']}px" : $border['width'];
141+
}
142+
143+
$preset_border_color = array_key_exists( 'borderColor', $block_attributes ) ? "var:preset|color|{$block_attributes['borderColor']}" : null;
144+
$border_styles['color'] = $preset_border_color ? $preset_border_color : ( $border['color'] ?? null );
145+
146+
foreach ( array( 'top', 'right', 'bottom', 'left' ) as $side ) {
147+
if ( ! isset( $border[ $side ] ) || ! is_array( $border[ $side ] ) ) {
148+
continue;
149+
}
150+
$border_styles[ $side ] = array(
151+
'width' => $border[ $side ]['width'] ?? null,
152+
'color' => $border[ $side ]['color'] ?? null,
153+
'style' => $border[ $side ]['style'] ?? null,
154+
);
155+
}
156+
123157
$attributes = array();
124-
$styles = wp_style_engine_get_styles( array( 'border' => $border_block_styles ) );
158+
$styles = wp_style_engine_get_styles( array( 'border' => $border_styles ) );
125159

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

0 commit comments

Comments
 (0)