Skip to content

Commit 2bb0144

Browse files
Block Supports: Add a helper that returns every support's classes and styles.
Adds wp_get_block_supports_classes_and_styles(), which combines the background, color, border, shadow and spacing helpers in the style engine's order. An optional list limits it to the supports a block applies to one element.
1 parent 99a9486 commit 2bb0144

2 files changed

Lines changed: 134 additions & 0 deletions

File tree

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

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,3 +33,58 @@ function wp_should_skip_block_supports_serialization( $block_type, $feature_set,
3333

3434
return $skip_serialization;
3535
}
36+
37+
/**
38+
* Returns the combined classes and inline styles of the background, color,
39+
* border, shadow and spacing block supports for block attributes. Does not
40+
* check block support or skipped serialization.
41+
*
42+
* @since 7.3.0
43+
*
44+
* @param array $block_attributes Block attributes.
45+
* @param array|null $supports Optional. Supports to include: any of `background`, `color`,
46+
* `border`, `shadow` and `spacing`. Default null, for all of them.
47+
* @return array Array with `class` and `style` keys, each present only when non-empty.
48+
*/
49+
function wp_get_block_supports_classes_and_styles( $block_attributes, $supports = null ) {
50+
// Follows the style engine's group order.
51+
$getters = array(
52+
'background' => 'wp_get_background_classes_and_styles',
53+
'color' => 'wp_get_color_classes_and_styles',
54+
'border' => 'wp_get_border_classes_and_styles',
55+
'shadow' => 'wp_get_shadow_classes_and_styles',
56+
'spacing' => 'wp_get_spacing_classes_and_styles',
57+
);
58+
59+
if ( is_array( $supports ) ) {
60+
$getters = array_intersect_key( $getters, array_flip( $supports ) );
61+
}
62+
63+
$classes = array();
64+
$styles = '';
65+
66+
foreach ( $getters as $getter ) {
67+
$classes_and_styles = $getter( $block_attributes );
68+
69+
if ( ! empty( $classes_and_styles['class'] ) ) {
70+
$classes[] = $classes_and_styles['class'];
71+
}
72+
73+
if ( ! empty( $classes_and_styles['style'] ) ) {
74+
$styles .= $classes_and_styles['style'];
75+
}
76+
}
77+
78+
$attributes = array();
79+
80+
if ( ! empty( $classes ) ) {
81+
// Background and color can both add `has-background`.
82+
$attributes['class'] = implode( ' ', array_unique( explode( ' ', implode( ' ', $classes ) ) ) );
83+
}
84+
85+
if ( '' !== $styles ) {
86+
$attributes['style'] = $styles;
87+
}
88+
89+
return $attributes;
90+
}
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
<?php
2+
/**
3+
* @group block-supports
4+
*
5+
* @covers ::wp_get_block_supports_classes_and_styles
6+
*/
7+
class Tests_Block_Supports_WpGetBlockSupportsClassesAndStyles extends WP_UnitTestCase {
8+
/**
9+
* Tests that block support classes and styles are combined from block attributes.
10+
*
11+
* @ticket XXXXX
12+
*
13+
* @dataProvider data_get_block_supports_classes_and_styles
14+
*
15+
* @param mixed $block_attributes Block attributes.
16+
* @param array|null $supports Supports to include.
17+
* @param array $expected Expected classes and styles.
18+
*/
19+
public function test_get_block_supports_classes_and_styles( $block_attributes, $supports, $expected ) {
20+
$this->assertSame( $expected, wp_get_block_supports_classes_and_styles( $block_attributes, $supports ) );
21+
}
22+
23+
/**
24+
* Data provider.
25+
*
26+
* @return array[]
27+
*/
28+
public static function data_get_block_supports_classes_and_styles() {
29+
$block_attributes = array(
30+
'backgroundColor' => 'contrast',
31+
'borderColor' => 'accent-2',
32+
'style' => array(
33+
'background' => array( 'gradient' => 'linear-gradient(#000,#fff)' ),
34+
'border' => array( 'width' => '2px' ),
35+
'shadow' => '1px 1px 1px #000',
36+
'spacing' => array( 'padding' => array( 'top' => '10px' ) ),
37+
),
38+
);
39+
40+
return array(
41+
'all supports' => array(
42+
'block_attributes' => $block_attributes,
43+
'supports' => null,
44+
'expected' => array(
45+
'class' => 'has-background has-contrast-background-color has-border-color has-accent-2-border-color',
46+
'style' => 'background-image:linear-gradient(#000,#fff);border-width:2px;box-shadow:1px 1px 1px #000;padding-top:10px;',
47+
),
48+
),
49+
'selected supports' => array(
50+
'block_attributes' => $block_attributes,
51+
'supports' => array( 'shadow', 'border' ),
52+
'expected' => array(
53+
'class' => 'has-border-color has-accent-2-border-color',
54+
'style' => 'border-width:2px;box-shadow:1px 1px 1px #000;',
55+
),
56+
),
57+
'unknown support' => array(
58+
'block_attributes' => $block_attributes,
59+
'supports' => array( 'typography' ),
60+
'expected' => array(),
61+
),
62+
'no supports' => array(
63+
'block_attributes' => $block_attributes,
64+
'supports' => array(),
65+
'expected' => array(),
66+
),
67+
'empty attributes' => array(
68+
'block_attributes' => array(),
69+
'supports' => null,
70+
'expected' => array(),
71+
),
72+
'non-array attributes' => array(
73+
'block_attributes' => null,
74+
'supports' => null,
75+
'expected' => array(),
76+
),
77+
);
78+
}
79+
}

0 commit comments

Comments
 (0)