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

* https://github.com/WordPress/gutenberg/pull/84205
* https://github.com/WordPress/gutenberg/pull/84206
* https://github.com/WordPress/gutenberg/pull/84207
* https://github.com/WordPress/gutenberg/pull/84208
* https://github.com/WordPress/gutenberg/pull/84209
27 changes: 23 additions & 4 deletions lib/block-supports/shadow.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,13 +54,32 @@ function gutenberg_apply_shadow_support( $block_type, $block_attributes ) {
return array();
}

$shadow_block_styles = array();
return gutenberg_get_shadow_classes_and_styles( $block_attributes );
}

/**
* Returns shadow classes and inline styles for block attributes, like the JS
* `getShadowClassesAndStyles()`. Does not check block support or skipped
* serialization.
*
* @since 7.3.0
*
* @param array $block_attributes Block attributes.
* @return array Array with `class` and `style` keys, each present only when non-empty.
*/
function gutenberg_get_shadow_classes_and_styles( $block_attributes ) {
if ( ! is_array( $block_attributes ) ) {
return array();
}

$custom_shadow = $block_attributes['style']['shadow'] ?? null;
$shadow_block_styles['shadow'] = $custom_shadow;
$shadow = $block_attributes['style']['shadow'] ?? null;

$attributes = array();
$styles = gutenberg_style_engine_get_styles( $shadow_block_styles );
$styles = gutenberg_style_engine_get_styles( array( 'shadow' => $shadow ) );

if ( ! empty( $styles['classnames'] ) ) {
Comment thread
aaronrobertshaw marked this conversation as resolved.
Resolved
$attributes['class'] = $styles['classnames'];
}

if ( ! empty( $styles['css'] ) ) {
$attributes['style'] = $styles['css'];
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 @@ -71,6 +71,7 @@
"wp_apply_colors_support",
"wp_enqueue_block_support_styles",
"wp_get_typography_font_size_value",
"wp_get_shadow_classes_and_styles",
"wp_style_engine_get_styles",
"wp_get_global_styles",
"wp_get_global_settings"
Expand Down
4 changes: 2 additions & 2 deletions packages/block-library/src/avatar/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ 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_styles = wp_style_engine_get_styles( array( 'shadow' => $attributes['style']['shadow'] ?? null ) );
$shadow_attributes = wp_get_shadow_classes_and_styles( $attributes );

// Class gets passed through `esc_attr` via `get_avatar`.
$image_classes = ! empty( $border_attributes['class'] )
Expand All @@ -29,7 +29,7 @@ function render_block_core_avatar( $attributes, $content, $block ) {
// 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_styles['css'] ?? '' );
$image_css = ( $border_attributes['style'] ?? '' ) . ( $shadow_attributes['style'] ?? '' );
$image_styles = ! empty( $image_css )
? sprintf( ' style="%s"', esc_attr( $image_css ) )
: '';
Expand Down
8 changes: 3 additions & 5 deletions packages/block-library/src/navigation-submenu/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -277,11 +277,9 @@ function render_block_core_navigation_submenu( $attributes, $content, $block ) {
* Shadow serialization is skipped for this block so the shadow lands on the
* dropdown panel, the floating layer it is meant for, and not the menu item.
*/
if ( ! empty( $attributes['style']['shadow'] ) ) {
$shadow_styles = wp_style_engine_get_styles( array( 'shadow' => $attributes['style']['shadow'] ) );
if ( ! empty( $shadow_styles['css'] ) ) {
$style_attribute .= $shadow_styles['css'];
}
$shadow_attributes = wp_get_shadow_classes_and_styles( $attributes );
if ( ! empty( $shadow_attributes['style'] ) ) {
$style_attribute .= $shadow_attributes['style'];
}

if ( strpos( $inner_blocks_html, 'current-menu-item' ) ) {
Expand Down
9 changes: 3 additions & 6 deletions packages/block-library/src/post-featured-image/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,12 +64,9 @@ function render_block_core_post_featured_image( $attributes, $content, $block )
if ( ! empty( $attributes['scale'] ) ) {
$extra_styles .= esc_attr( safecss_filter_attr( 'object-fit:' . $attributes['scale'] ) ) . ';';
}
if ( ! empty( $attributes['style']['shadow'] ) ) {
$shadow_styles = wp_style_engine_get_styles( array( 'shadow' => $attributes['style']['shadow'] ) );

if ( ! empty( $shadow_styles['css'] ) ) {
$extra_styles .= $shadow_styles['css'];
}
$shadow_attributes = wp_get_shadow_classes_and_styles( $attributes );
if ( ! empty( $shadow_attributes['style'] ) ) {
$extra_styles .= $shadow_attributes['style'];
}

if ( ! empty( $extra_styles ) ) {
Expand Down
17 changes: 7 additions & 10 deletions packages/block-library/src/search/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -472,16 +472,13 @@ function styles_for_block_core_search( $attributes ) {
}

// Shadow lands on the same elements as the border.
if ( ! empty( $attributes['style']['shadow'] ) && is_string( $attributes['style']['shadow'] ) ) {
$shadow_styles = wp_style_engine_get_styles( array( 'shadow' => $attributes['style']['shadow'] ) );

if ( ! empty( $shadow_styles['css'] ) ) {
if ( $is_button_inside ) {
$wrapper_styles[] = $shadow_styles['css'];
} else {
$button_styles[] = $shadow_styles['css'];
$input_styles[] = $shadow_styles['css'];
}
$shadow_attributes = wp_get_shadow_classes_and_styles( $attributes );
if ( ! empty( $shadow_attributes['style'] ) ) {
if ( $is_button_inside ) {
$wrapper_styles[] = $shadow_attributes['style'];
} else {
$button_styles[] = $shadow_attributes['style'];
$input_styles[] = $shadow_attributes['style'];
}
}

Expand Down
6 changes: 6 additions & 0 deletions phpstan.neon.dist
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,12 @@ parameters:
message: '#^Action callback returns .+ but should not return anything\.$#'
identifier: return.void

# wp_get_shadow_classes_and_styles() is new in WordPress Core: https://github.com/WordPress/wordpress-develop/pull/14094
# Remove this entry once the WordPress stubs are generated from a Core revision that includes it.
-
message: '#^Function wp_get_shadow_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
50 changes: 49 additions & 1 deletion phpunit/block-supports/shadow-test.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@ public function set_up() {
}

public function tear_down() {
unregister_block_type( $this->test_block_name );
if ( $this->test_block_name ) {
unregister_block_type( $this->test_block_name );
}
$this->test_block_name = null;
parent::tear_down();
}
Expand Down Expand Up @@ -108,4 +110,50 @@ public function data_generate_shadow_fixtures() {
),
);
}

/**
* @covers ::gutenberg_get_shadow_classes_and_styles
*
* @dataProvider data_get_shadow_classes_and_styles
*
* @param mixed $block_attributes Block attributes.
* @param array $expected Expected classes and styles.
*/
public function test_get_shadow_classes_and_styles( $block_attributes, $expected ) {
$this->assertSame( $expected, gutenberg_get_shadow_classes_and_styles( $block_attributes ) );
}

/**
* Data provider.
*
* @return array
*/
public function data_get_shadow_classes_and_styles() {
return array(
'custom shadow' => array(
'block_attributes' => array( 'style' => array( 'shadow' => '1px 1px 1px #000' ) ),
'expected' => array( 'style' => 'box-shadow:1px 1px 1px #000;' ),
),
'preset shadow' => array(
'block_attributes' => array( 'style' => array( 'shadow' => 'var:preset|shadow|natural' ) ),
'expected' => array( 'style' => 'box-shadow:var(--wp--preset--shadow--natural);' ),
),
'empty shadow' => array(
'block_attributes' => array( 'style' => array( 'shadow' => '' ) ),
'expected' => array(),
),
'non-array style' => array(
'block_attributes' => array( 'style' => 'box-shadow:1px 1px 1px #000' ),
'expected' => array(),
),
'non-array attributes' => array(
'block_attributes' => null,
'expected' => array(),
),
'object attributes' => array(
'block_attributes' => (object) array( 'style' => array( 'shadow' => '1px 1px 1px #000' ) ),
'expected' => array(),
),
);
}
}
67 changes: 67 additions & 0 deletions phpunit/blocks/render-block-avatar-test.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
<?php
/**
* Avatar block rendering tests.
*
* @package WordPress
* @subpackage Blocks
*/

/**
* Tests for the Avatar block.
*
* @group blocks
*
* @covers ::gutenberg_render_block_core_avatar
*/
class Render_Block_Avatar_Test extends WP_UnitTestCase {

/**
* @var int
*/
private static $user_id;

public static function wpSetUpBeforeClass( $factory ) {
self::$user_id = $factory->user->create( array( 'display_name' => 'Avatar Author' ) );
}

/**
* Renders an Avatar block and returns the image's class and style attributes.
*
* @param array $style Style attribute.
* @return array Class and style of the image.
*/
private function render_image_attributes( $style ) {
$block = new WP_Block(
array(
'blockName' => 'core/avatar',
'attrs' => array(
'userId' => self::$user_id,
'style' => $style,
),
)
);

$processor = new WP_HTML_Tag_Processor( $block->render() );
$this->assertTrue( $processor->next_tag( 'img' ) );

return array(
'class' => $processor->get_attribute( 'class' ),
'style' => $processor->get_attribute( 'style' ),
);
}

public function test_shadow_follows_border_on_the_image() {
$actual = $this->render_image_attributes(
array(
'border' => array(
'radius' => '10px',
'color' => '#ff0000',
),
'shadow' => '1px 1px 1px #000',
)
);

$this->assertSame( 'avatar avatar-96 photo wp-block-avatar__image has-border-color', $actual['class'] );
$this->assertSame( 'border-color:#ff0000;border-radius:10px;box-shadow:1px 1px 1px #000;', $actual['style'] );
}
}
20 changes: 20 additions & 0 deletions phpunit/blocks/render-block-icon-test.php
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,26 @@ public function test_preserves_intrinsic_svg_style_when_applying_rotation() {
$this->assertLessThan( strpos( $style, 'rotate' ), strpos( $style, 'fill' ) );
}

public function test_shadow_keeps_the_style_engine_order() {
$output = gutenberg_render_block_core_icon(
array(
'icon' => 'core/info',
'style' => array(
'color' => array( 'text' => '#000' ),
'border' => array( 'width' => '2px' ),
'shadow' => 'var:preset|shadow|natural',
'spacing' => array( 'padding' => '4px' ),
'dimensions' => array( 'width' => '48px' ),
),
)
);

$processor = new WP_HTML_Tag_Processor( $output );
$this->assertTrue( $processor->next_tag( 'svg' ) );
$this->assertSame( 'has-text-color', $processor->get_attribute( 'class' ) );
$this->assertSame( 'fill: none; color:#000;border-width:2px;box-shadow:var(--wp--preset--shadow--natural);width:48px;padding:4px;', $processor->get_attribute( 'style' ) );
}

public function test_renders_only_icons_in_public_collections() {
// Renders public core icon.
$processor = new WP_HTML_Tag_Processor( gutenberg_render_block_core_icon( array( 'icon' => 'core/caution' ) ) );
Expand Down
32 changes: 32 additions & 0 deletions phpunit/blocks/render-block-navigation-submenu-test.php
Original file line number Diff line number Diff line change
Expand Up @@ -276,4 +276,36 @@ public function test_should_apply_open_on_click_for_legacy_openSubmenusOnClick_a
'Submenu should not have "open-on-hover-click" class when legacy openSubmenusOnClick was true'
);
}

/**
* @covers ::gutenberg_render_block_core_navigation_submenu
*/
public function test_should_apply_shadow_to_the_submenu_container() {
$page_id = self::$page->ID;

$parsed_blocks = parse_blocks(
'<!-- wp:navigation-submenu {"label":"Submenu Label","type":"page","id":' . $page_id . ',"url":"http://localhost:8888/?page_id=' . $page_id . '","kind":"post-type","style":{"shadow":"var:preset|shadow|natural"}} -->
<!-- wp:navigation-link {"label":"Submenu Item Link Label","type":"page","id":' . $page_id . ',"url":"http://localhost:8888/?page_id=' . $page_id . '","kind":"post-type"} /-->
<!-- /wp:navigation-submenu -->'
);

$navigation_submenu_block = new WP_Block( $parsed_blocks[0], array( 'overlayTextColor' => 'purple' ) );

$tags = new WP_HTML_Tag_Processor(
gutenberg_render_block_core_navigation_submenu(
$navigation_submenu_block->attributes,
array(),
$navigation_submenu_block
)
);
$this->assertTrue(
$tags->next_tag(
array(
'tag_name' => 'ul',
'class_name' => 'wp-block-navigation__submenu-container',
)
)
);
$this->assertSame( 'box-shadow:var(--wp--preset--shadow--natural)', $tags->get_attribute( 'style' ) );
}
}
Loading
Loading