diff --git a/src/wp-includes/block-supports/utils.php b/src/wp-includes/block-supports/utils.php index c668586674de4..677690731a982 100644 --- a/src/wp-includes/block-supports/utils.php +++ b/src/wp-includes/block-supports/utils.php @@ -33,3 +33,61 @@ function wp_should_skip_block_supports_serialization( $block_type, $feature_set, return $skip_serialization; } + +/** + * Returns the combined classes and inline styles of the background, color, + * border, shadow and spacing block supports for block attributes. Does not + * check block support or skipped serialization. + * + * @since 7.3.0 + * + * @param array $block_attributes Block attributes. + * @param string[]|null $supports Optional. Supports to include: any of `background`, `color`, + * `border`, `shadow` and `spacing`. Unknown names are ignored. + * Default null, for all of them. + * @return array Array with `class` and `style` keys, each present only when non-empty. + * + * @phpstan-return array{ class?: non-empty-string, style?: non-empty-string } + */ +function wp_get_block_supports_classes_and_styles( $block_attributes, $supports = null ) { + // Follows the style engine's group order. + $getters = array( + 'background' => 'wp_get_background_classes_and_styles', + 'color' => 'wp_get_color_classes_and_styles', + 'border' => 'wp_get_border_classes_and_styles', + 'shadow' => 'wp_get_shadow_classes_and_styles', + 'spacing' => 'wp_get_spacing_classes_and_styles', + ); + + if ( is_array( $supports ) ) { + $getters = array_intersect_key( $getters, array_flip( $supports ) ); + } + + $classes = array(); + $styles = ''; + + foreach ( $getters as $getter ) { + $classes_and_styles = $getter( $block_attributes ); + + if ( ! empty( $classes_and_styles['class'] ) ) { + $classes[] = $classes_and_styles['class']; + } + + if ( ! empty( $classes_and_styles['style'] ) ) { + $styles .= $classes_and_styles['style']; + } + } + + $attributes = array(); + + if ( ! empty( $classes ) ) { + // Background and color can both add `has-background`. + $attributes['class'] = implode( ' ', array_unique( explode( ' ', implode( ' ', $classes ) ) ) ); + } + + if ( '' !== $styles ) { + $attributes['style'] = $styles; + } + + return $attributes; +} diff --git a/tests/phpunit/tests/block-supports/wpGetBlockSupportsClassesAndStyles.php b/tests/phpunit/tests/block-supports/wpGetBlockSupportsClassesAndStyles.php new file mode 100644 index 0000000000000..a60a9270d706e --- /dev/null +++ b/tests/phpunit/tests/block-supports/wpGetBlockSupportsClassesAndStyles.php @@ -0,0 +1,79 @@ +|null $block_attributes Block attributes, or null to check the guard. + * @param string[]|null $supports Supports to include. + * @param array $expected Expected classes and styles. + */ + public function test_get_block_supports_classes_and_styles( $block_attributes, $supports, $expected ) { + $this->assertSame( $expected, wp_get_block_supports_classes_and_styles( $block_attributes, $supports ) ); + } + + /** + * Data provider for {@see self::test_get_block_supports_classes_and_styles()}. + * + * @return array|null, supports: string[]|null, expected: array{ class?: non-empty-string, style?: non-empty-string } }> + */ + public static function data_get_block_supports_classes_and_styles() { + $block_attributes = array( + 'backgroundColor' => 'contrast', + 'borderColor' => 'accent-2', + 'style' => array( + 'background' => array( 'gradient' => 'linear-gradient(#000,#fff)' ), + 'border' => array( 'width' => '2px' ), + 'shadow' => '1px 1px 1px #000', + 'spacing' => array( 'padding' => array( 'top' => '10px' ) ), + ), + ); + + return array( + 'all supports' => array( + 'block_attributes' => $block_attributes, + 'supports' => null, + 'expected' => array( + 'class' => 'has-background has-contrast-background-color has-border-color has-accent-2-border-color', + 'style' => 'background-image:linear-gradient(#000,#fff);border-width:2px;box-shadow:1px 1px 1px #000;padding-top:10px;', + ), + ), + 'selected supports' => array( + 'block_attributes' => $block_attributes, + 'supports' => array( 'shadow', 'border' ), + 'expected' => array( + 'class' => 'has-border-color has-accent-2-border-color', + 'style' => 'border-width:2px;box-shadow:1px 1px 1px #000;', + ), + ), + 'unknown support' => array( + 'block_attributes' => $block_attributes, + 'supports' => array( 'typography' ), + 'expected' => array(), + ), + 'no supports' => array( + 'block_attributes' => $block_attributes, + 'supports' => array(), + 'expected' => array(), + ), + 'empty attributes' => array( + 'block_attributes' => array(), + 'supports' => null, + 'expected' => array(), + ), + 'non-array attributes' => array( + 'block_attributes' => null, + 'supports' => null, + 'expected' => array(), + ), + ); + } +}