Skip to content

Commit 7bd0b73

Browse files
Block Supports: Only treat a non-empty string as a color preset slug.
1 parent 07a6415 commit 7bd0b73

4 files changed

Lines changed: 34 additions & 20 deletions

File tree

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -140,8 +140,9 @@ function wp_get_border_classes_and_styles( $block_attributes ) {
140140
$border_styles['width'] = is_numeric( $border['width'] ) ? "{$border['width']}px" : $border['width'];
141141
}
142142

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 );
143+
// Preset slugs are strings in theme.json and the editor.
144+
$preset_slug = $block_attributes['borderColor'] ?? null;
145+
$border_styles['color'] = is_string( $preset_slug ) && '' !== $preset_slug ? "var:preset|color|{$preset_slug}" : ( $border['color'] ?? null );
145146

146147
foreach ( array( 'top', 'right', 'bottom', 'left' ) as $side ) {
147148
if ( ! isset( $border[ $side ] ) || ! is_array( $border[ $side ] ) ) {

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

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -172,16 +172,9 @@ function wp_get_color_classes_and_styles( $block_attributes ) {
172172

173173
foreach ( $preset_attributes as $feature => list( $preset_attribute, $preset_type ) ) {
174174
$preset_slug = $block_attributes[ $preset_attribute ] ?? null;
175-
if ( true === $preset_slug ) {
176-
$preset_slug = 'true';
177-
}
178-
179-
// Mirrors JS truthiness, so '0' is a slug. Arrays cannot form a class name.
180-
$has_preset = is_string( $preset_slug )
181-
? '' !== $preset_slug
182-
: ( is_int( $preset_slug ) || is_float( $preset_slug ) ) && 0.0 !== (float) $preset_slug;
183175

184-
$preset_value = $has_preset ? "var:preset|{$preset_type}|{$preset_slug}" : null;
176+
// Preset slugs are strings in theme.json and the editor.
177+
$preset_value = is_string( $preset_slug ) && '' !== $preset_slug ? "var:preset|{$preset_type}|{$preset_slug}" : null;
185178
$custom_value = $block_attributes['style']['color'][ $feature ] ?? null;
186179
$color_block_styles[ $feature ] = $preset_value ?? $custom_value;
187180
}

‎tests/phpunit/tests/block-supports/wpGetBorderClassesAndStyles.php‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,20 @@ public function data_get_border_classes_and_styles() {
6868
'class' => 'has-border-color has-red-border-color',
6969
),
7070
),
71+
'non-string preset' => array(
72+
'block_attributes' => array(
73+
'borderColor' => 1,
74+
'style' => array(
75+
'border' => array(
76+
'color' => '#ff0000',
77+
),
78+
),
79+
),
80+
'expected' => array(
81+
'class' => 'has-border-color',
82+
'style' => 'border-color:#ff0000;',
83+
),
84+
),
7185
'numeric' => array(
7286
'block_attributes' => array(
7387
'style' => array(

‎tests/phpunit/tests/block-supports/wpGetColorClassesAndStyles.php‎

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ public function test_get_color_classes_and_styles( $block_attributes, $expected
2626
*/
2727
public function data_get_color_classes_and_styles() {
2828
return array(
29-
'custom' => array(
29+
'custom' => array(
3030
'block_attributes' => array(
3131
'style' => array(
3232
'color' => array(
@@ -41,7 +41,7 @@ public function data_get_color_classes_and_styles() {
4141
'style' => 'color:#d92828;background-color:#ffffff;background:linear-gradient(135deg,rgb(6,147,227) 0%,rgb(155,81,224) 100%);',
4242
),
4343
),
44-
'preset' => array(
44+
'preset' => array(
4545
'block_attributes' => array(
4646
'textColor' => 'contrast',
4747
'backgroundColor' => 'base',
@@ -51,7 +51,7 @@ public function data_get_color_classes_and_styles() {
5151
'class' => 'has-text-color has-contrast-color has-background has-base-background-color has-vivid-cyan-blue-to-vivid-purple-gradient-background',
5252
),
5353
),
54-
'preset wins over custom' => array(
54+
'preset wins over custom' => array(
5555
'block_attributes' => array(
5656
'textColor' => 'contrast',
5757
'style' => array(
@@ -64,7 +64,7 @@ public function data_get_color_classes_and_styles() {
6464
'class' => 'has-text-color has-contrast-color',
6565
),
6666
),
67-
'null preset uses custom text' => array(
67+
'null preset uses custom text' => array(
6868
'block_attributes' => array(
6969
'textColor' => null,
7070
'style' => array(
@@ -78,27 +78,33 @@ public function data_get_color_classes_and_styles() {
7878
'style' => 'color:#d92828;',
7979
),
8080
),
81-
'numeric preset' => array(
81+
'non-string preset uses custom' => array(
8282
'block_attributes' => array(
8383
'textColor' => 1,
84+
'style' => array(
85+
'color' => array(
86+
'text' => '#d92828',
87+
),
88+
),
8489
),
8590
'expected' => array(
86-
'class' => 'has-text-color has-1-color',
91+
'class' => 'has-text-color',
92+
'style' => 'color:#d92828;',
8793
),
8894
),
89-
'empty' => array(
95+
'empty' => array(
9096
'block_attributes' => array(),
9197
'expected' => array(),
9298
),
93-
'malformed color' => array(
99+
'malformed color' => array(
94100
'block_attributes' => array(
95101
'style' => array(
96102
'color' => '#d92828',
97103
),
98104
),
99105
'expected' => array(),
100106
),
101-
'malformed attributes' => array(
107+
'malformed attributes' => array(
102108
'block_attributes' => 'color',
103109
'expected' => array(),
104110
),

0 commit comments

Comments
 (0)