From ef98ed44f17f3f4d5dc8454fd35dfe74f373d7e1 Mon Sep 17 00:00:00 2001 From: Aaron Robertshaw <60436221+aaronrobertshaw@users.noreply.github.com> Date: Thu, 5 Nov 2026 08:45:01 +0800 Subject: [PATCH 1/3] Block Supports: Add a PHP helper that returns every support's classes and styles 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. Avatar and Post Navigation Link now use it. --- lib/block-supports/utils.php | 61 ++++++++++++++ lib/load.php | 1 + packages/block-library/package.json | 1 + packages/block-library/src/avatar/index.php | 9 +-- .../src/post-navigation-link/index.php | 26 +++--- phpstan.neon.dist | 6 ++ phpunit/block-supports/utils-test.php | 79 +++++++++++++++++++ 7 files changed, 163 insertions(+), 20 deletions(-) create mode 100644 lib/block-supports/utils.php create mode 100644 phpunit/block-supports/utils-test.php diff --git a/lib/block-supports/utils.php b/lib/block-supports/utils.php new file mode 100644 index 00000000000000..86d708f035702d --- /dev/null +++ b/lib/block-supports/utils.php @@ -0,0 +1,61 @@ + 'gutenberg_get_background_classes_and_styles', + 'color' => 'gutenberg_get_color_classes_and_styles', + 'border' => 'gutenberg_get_border_classes_and_styles', + 'shadow' => 'gutenberg_get_shadow_classes_and_styles', + 'spacing' => 'gutenberg_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/lib/load.php b/lib/load.php index e3768f96450b76..5b444c2da34b33 100644 --- a/lib/load.php +++ b/lib/load.php @@ -247,6 +247,7 @@ function gutenberg_get_experiment_default( $name ) { require __DIR__ . '/block-supports/duotone.php'; require __DIR__ . '/block-supports/shadow.php'; require __DIR__ . '/block-supports/background.php'; +require __DIR__ . '/block-supports/utils.php'; require __DIR__ . '/block-supports/block-style-variations.php'; require __DIR__ . '/block-supports/aria-label.php'; require __DIR__ . '/block-supports/anchor.php'; diff --git a/packages/block-library/package.json b/packages/block-library/package.json index 1a386bceb00bc6..7a27f36cd893ec 100644 --- a/packages/block-library/package.json +++ b/packages/block-library/package.json @@ -78,6 +78,7 @@ "wp_get_spacing_classes_and_styles", "wp_get_global_styles", "wp_get_background_classes_and_styles", + "wp_get_block_supports_classes_and_styles", "wp_get_global_settings" ], "suffixClasses": [ diff --git a/packages/block-library/src/avatar/index.php b/packages/block-library/src/avatar/index.php index e8c8264f4f8096..4341bf0bf3f0f0 100644 --- a/packages/block-library/src/avatar/index.php +++ b/packages/block-library/src/avatar/index.php @@ -18,18 +18,17 @@ function render_block_core_avatar( $attributes, $content, $block ) { $size = $attributes['size'] ?? 96; $wrapper_attributes = get_block_wrapper_attributes(); - $border_attributes = get_block_core_avatar_border_attributes( $attributes ); - $shadow_attributes = wp_get_shadow_classes_and_styles( $attributes ); + $image_attributes = wp_get_block_supports_classes_and_styles( $attributes, array( 'border', 'shadow' ) ); // Class gets passed through `esc_attr` via `get_avatar`. - $image_classes = ! empty( $border_attributes['class'] ) - ? "wp-block-avatar__image {$border_attributes['class']}" + $image_classes = ! empty( $image_attributes['class'] ) + ? "wp-block-avatar__image {$image_attributes['class']}" : 'wp-block-avatar__image'; // Unlike class, `get_avatar` doesn't filter the styles via `esc_attr`. // The style engine does pass the border and shadow styles through // `safecss_filter_attr` however. - $image_css = ( $border_attributes['style'] ?? '' ) . ( $shadow_attributes['style'] ?? '' ); + $image_css = $image_attributes['style'] ?? ''; $image_styles = ! empty( $image_css ) ? sprintf( ' style="%s"', esc_attr( $image_css ) ) : ''; diff --git a/packages/block-library/src/post-navigation-link/index.php b/packages/block-library/src/post-navigation-link/index.php index 79ebc4c16025d7..f16b78d69f85cb 100644 --- a/packages/block-library/src/post-navigation-link/index.php +++ b/packages/block-library/src/post-navigation-link/index.php @@ -166,32 +166,28 @@ function render_block_core_post_navigation_link( $attributes, $content ) { * } */ function block_core_post_navigation_link_get_support_styles( $attributes ) { - $block_styles = $attributes['style'] ?? array(); - $background = $block_styles['background'] ?? null; + $support_attributes = $attributes; // Post Navigation Link has no clip support, so ignore a stale value. - if ( is_array( $background ) ) { - unset( $background['backgroundClip'] ); + if ( isset( $support_attributes['style']['background'] ) && is_array( $support_attributes['style']['background'] ) ) { + unset( $support_attributes['style']['background']['backgroundClip'] ); } - $background_styles = wp_get_background_classes_and_styles( array( 'style' => array( 'background' => $background ) ) ); - $border_styles = wp_get_border_classes_and_styles( $attributes ); - - $styles = wp_style_engine_get_styles( + // Dimensions sit between shadow and spacing in the style engine's order, so spacing is fetched separately. + $support_styles = wp_get_block_supports_classes_and_styles( $support_attributes, array( 'background', 'border', 'shadow' ) ); + $dimensions_styles = wp_style_engine_get_styles( array( 'dimensions' => array( - 'minHeight' => $block_styles['dimensions']['minHeight'] ?? null, - 'minWidth' => $block_styles['dimensions']['minWidth'] ?? null, + 'minHeight' => $attributes['style']['dimensions']['minHeight'] ?? null, + 'minWidth' => $attributes['style']['dimensions']['minWidth'] ?? null, ), - 'shadow' => $block_styles['shadow'] ?? null, ) ); - $spacing_styles = wp_get_spacing_classes_and_styles( $attributes ); + $spacing_styles = wp_get_spacing_classes_and_styles( $attributes ); - // The style engine outputs background before the other groups, so it leads here too. return array( - 'class' => implode( ' ', array_filter( array( $background_styles['class'] ?? '', $border_styles['class'] ?? '', $styles['classnames'] ?? '' ) ) ), - 'style' => ( $background_styles['style'] ?? '' ) . ( $border_styles['style'] ?? '' ) . ( $styles['css'] ?? '' ) . ( $spacing_styles['style'] ?? '' ), + 'class' => implode( ' ', array_filter( array( $support_styles['class'] ?? '', $dimensions_styles['classnames'] ?? '' ) ) ), + 'style' => ( $support_styles['style'] ?? '' ) . ( $dimensions_styles['css'] ?? '' ) . ( $spacing_styles['style'] ?? '' ), ); } diff --git a/phpstan.neon.dist b/phpstan.neon.dist index 9114e6ce8309ab..252655b53f9b3a 100644 --- a/phpstan.neon.dist +++ b/phpstan.neon.dist @@ -56,6 +56,12 @@ parameters: message: '#^Function wp_get_border_classes_and_styles not found\.$#' identifier: function.notFound + # wp_get_block_supports_classes_and_styles() is new in WordPress Core. + # Remove this entry once the WordPress stubs are generated from a Core revision that includes it. + - + message: '#^Function wp_get_block_supports_classes_and_styles not found\.$#' + identifier: function.notFound + # Level 3: # WP_REST_Request implements ArrayAccess, and the WordPress stubs declare its offsetSet() with a method-level # template (`@template TOffset of key-of`, `@param T[TOffset] $value`). PHPStan does not resolve that diff --git a/phpunit/block-supports/utils-test.php b/phpunit/block-supports/utils-test.php new file mode 100644 index 00000000000000..c6c99cf42b0e1d --- /dev/null +++ b/phpunit/block-supports/utils-test.php @@ -0,0 +1,79 @@ +assertSame( $expected, gutenberg_get_block_supports_classes_and_styles( $block_attributes, $supports ) ); + } + + /** + * Data provider. + * + * @return array + */ + public 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(), + ), + ); + } +} From 946d55799850f6461488b29b2d96dddc049af3f6 Mon Sep 17 00:00:00 2001 From: Aaron Robertshaw <60436221+aaronrobertshaw@users.noreply.github.com> Date: Thu, 5 Nov 2026 08:57:54 +0800 Subject: [PATCH 2/3] Add the backport changelog entry for the block supports helper --- backport-changelog/7.3/15485.md | 3 +++ phpstan.neon.dist | 2 +- 2 files changed, 4 insertions(+), 1 deletion(-) create mode 100644 backport-changelog/7.3/15485.md diff --git a/backport-changelog/7.3/15485.md b/backport-changelog/7.3/15485.md new file mode 100644 index 00000000000000..a9a6b40cb8c984 --- /dev/null +++ b/backport-changelog/7.3/15485.md @@ -0,0 +1,3 @@ +https://github.com/WordPress/wordpress-develop/pull/15485 + +* https://github.com/WordPress/gutenberg/pull/86337 diff --git a/phpstan.neon.dist b/phpstan.neon.dist index 252655b53f9b3a..a18277685db654 100644 --- a/phpstan.neon.dist +++ b/phpstan.neon.dist @@ -56,7 +56,7 @@ parameters: message: '#^Function wp_get_border_classes_and_styles not found\.$#' identifier: function.notFound - # wp_get_block_supports_classes_and_styles() is new in WordPress Core. + # wp_get_block_supports_classes_and_styles() is new in WordPress Core: https://github.com/WordPress/wordpress-develop/pull/15485 # Remove this entry once the WordPress stubs are generated from a Core revision that includes it. - message: '#^Function wp_get_block_supports_classes_and_styles not found\.$#' From 931f00c539924d445429b3cad000fa7025ef876c Mon Sep 17 00:00:00 2001 From: Aaron Robertshaw <60436221+aaronrobertshaw@users.noreply.github.com> Date: Fri, 6 Nov 2026 17:15:04 +0800 Subject: [PATCH 3/3] Narrow the block supports helper docblock types --- lib/block-supports/utils.php | 11 +++++++---- phpunit/block-supports/utils-test.php | 10 +++++----- 2 files changed, 12 insertions(+), 9 deletions(-) diff --git a/lib/block-supports/utils.php b/lib/block-supports/utils.php index 86d708f035702d..30d127ee8b668c 100644 --- a/lib/block-supports/utils.php +++ b/lib/block-supports/utils.php @@ -12,10 +12,13 @@ * * @since 7.3.0 * - * @param array $block_attributes Block attributes. - * @param array|null $supports Optional. Supports to include: any of `background`, `color`, - * `border`, `shadow` and `spacing`. Default null, for all of them. - * @return array Array with `class` and `style` keys, each present only when non-empty. + * @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 gutenberg_get_block_supports_classes_and_styles( $block_attributes, $supports = null ) { // Follows the style engine's group order. diff --git a/phpunit/block-supports/utils-test.php b/phpunit/block-supports/utils-test.php index c6c99cf42b0e1d..504ef8c0723f6b 100644 --- a/phpunit/block-supports/utils-test.php +++ b/phpunit/block-supports/utils-test.php @@ -12,18 +12,18 @@ class WP_Block_Supports_Utils_Test extends WP_UnitTestCase { * * @dataProvider data_get_block_supports_classes_and_styles * - * @param mixed $block_attributes Block attributes. - * @param array|null $supports Supports to include. - * @param array $expected Expected classes and styles. + * @param array|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, gutenberg_get_block_supports_classes_and_styles( $block_attributes, $supports ) ); } /** - * Data provider. + * Data provider for {@see self::test_get_block_supports_classes_and_styles()}. * - * @return array + * @return array|null, supports: string[]|null, expected: array{ class?: non-empty-string, style?: non-empty-string } }> */ public function data_get_block_supports_classes_and_styles() { $block_attributes = array(