Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 58 additions & 0 deletions src/wp-includes/block-supports/utils.php
Original file line number Diff line number Diff line change
Expand Up @@ -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<string, mixed> $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<string, string> Array with `class` and `style` keys, each present only when non-empty.
*
* @phpstan-return array{ class?: non-empty-string, style?: non-empty-string }

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Bonus:

Suggested change
* @phpstan-return array{ class?: non-empty-string, style?: non-empty-string }
* @phpstan-param array<'background'|'color'|'border'|'shadow'|'spacing'>|null $supports
* @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;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
<?php
/**
* @group block-supports
*
* @covers ::wp_get_block_supports_classes_and_styles
*/
class Tests_Block_Supports_WpGetBlockSupportsClassesAndStyles extends WP_UnitTestCase {
/**
* Tests that block support classes and styles are combined from block attributes.
*
* @ticket 66485
*
* @dataProvider data_get_block_supports_classes_and_styles
*
* @param array<string, mixed>|null $block_attributes Block attributes, or null to check the guard.
* @param string[]|null $supports Supports to include.
* @param array<string, string> $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<string, array{ block_attributes: array<string, mixed>|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(),
),
);
}
}
Loading