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/lib/block-supports/utils.php b/lib/block-supports/utils.php new file mode 100644 index 00000000000000..30d127ee8b668c --- /dev/null +++ b/lib/block-supports/utils.php @@ -0,0 +1,64 @@ + $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. + $getters = array( + 'background' => '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..a18277685db654 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: 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\.$#' + 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..504ef8c0723f6b --- /dev/null +++ b/phpunit/block-supports/utils-test.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, gutenberg_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 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(), + ), + ); + } +}