Skip to content
Merged
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
3 changes: 3 additions & 0 deletions backport-changelog/7.3/15485.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
https://github.com/WordPress/wordpress-develop/pull/15485

* https://github.com/WordPress/gutenberg/pull/86337
64 changes: 64 additions & 0 deletions lib/block-supports/utils.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
<?php
/**
* Block support utility functions.
*
* @package gutenberg
*/

/**
* 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 }
*/
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;
}
1 change: 1 addition & 0 deletions lib/load.php
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down
1 change: 1 addition & 0 deletions packages/block-library/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": [
Expand Down
9 changes: 4 additions & 5 deletions packages/block-library/src/avatar/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 ) )
: '';
Expand Down
26 changes: 11 additions & 15 deletions packages/block-library/src/post-navigation-link/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'] ?? '' ),
);
}

Expand Down
6 changes: 6 additions & 0 deletions phpstan.neon.dist
Original file line number Diff line number Diff line change
Expand Up @@ -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<T>`, `@param T[TOffset] $value`). PHPStan does not resolve that
Expand Down
79 changes: 79 additions & 0 deletions phpunit/block-supports/utils-test.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
<?php

/**
* Test the block support utility functions.
*
* @package gutenberg
*/

class WP_Block_Supports_Utils_Test extends WP_UnitTestCase {
/**
* @covers ::gutenberg_get_block_supports_classes_and_styles
*
* @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, gutenberg_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 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