Skip to content

Commit f8e168a

Browse files
Block Supports: Match JS truthiness for color preset attributes.
1 parent 37c88ca commit f8e168a

2 files changed

Lines changed: 19 additions & 2 deletions

File tree

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

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -171,8 +171,17 @@ function wp_get_color_classes_and_styles( $block_attributes ) {
171171
);
172172

173173
foreach ( $preset_attributes as $feature => list( $preset_attribute, $preset_type ) ) {
174-
$preset_slug = $block_attributes[ $preset_attribute ] ?? null;
175-
$preset_value = is_string( $preset_slug ) && '' !== $preset_slug ? "var:preset|{$preset_type}|{$preset_slug}" : null;
174+
$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;
183+
184+
$preset_value = $has_preset ? "var:preset|{$preset_type}|{$preset_slug}" : null;
176185
$custom_value = $block_attributes['style']['color'][ $feature ] ?? null;
177186
$color_block_styles[ $feature ] = $preset_value ?? $custom_value;
178187
}

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,14 @@ public function data_get_color_classes_and_styles() {
7878
'style' => 'color:#d92828;',
7979
),
8080
),
81+
'numeric preset' => array(
82+
'block_attributes' => array(
83+
'textColor' => 1,
84+
),
85+
'expected' => array(
86+
'class' => 'has-text-color has-1-color',
87+
),
88+
),
8189
'empty' => array(
8290
'block_attributes' => array(),
8391
'expected' => array(),

0 commit comments

Comments
 (0)