diff --git a/backport-changelog/7.3/14094.md b/backport-changelog/7.3/14094.md new file mode 100644 index 00000000000000..af2f1e32bd6b82 --- /dev/null +++ b/backport-changelog/7.3/14094.md @@ -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 diff --git a/lib/block-supports/shadow.php b/lib/block-supports/shadow.php index 3a6b2ebee3de0a..42579926ce7f14 100644 --- a/lib/block-supports/shadow.php +++ b/lib/block-supports/shadow.php @@ -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'] ) ) { + $attributes['class'] = $styles['classnames']; + } if ( ! empty( $styles['css'] ) ) { $attributes['style'] = $styles['css']; diff --git a/packages/block-library/package.json b/packages/block-library/package.json index e6f461b3dcbcf0..292f1cebc69789 100644 --- a/packages/block-library/package.json +++ b/packages/block-library/package.json @@ -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" diff --git a/packages/block-library/src/avatar/index.php b/packages/block-library/src/avatar/index.php index c2571754326851..0f56ce461a08f6 100644 --- a/packages/block-library/src/avatar/index.php +++ b/packages/block-library/src/avatar/index.php @@ -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'] ) @@ -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 ) ) : ''; diff --git a/packages/block-library/src/navigation-submenu/index.php b/packages/block-library/src/navigation-submenu/index.php index 687a6d8e7545b1..3ca728cc83d251 100644 --- a/packages/block-library/src/navigation-submenu/index.php +++ b/packages/block-library/src/navigation-submenu/index.php @@ -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' ) ) { diff --git a/packages/block-library/src/post-featured-image/index.php b/packages/block-library/src/post-featured-image/index.php index 593003f10e1c51..08ac73adff0705 100644 --- a/packages/block-library/src/post-featured-image/index.php +++ b/packages/block-library/src/post-featured-image/index.php @@ -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 ) ) { diff --git a/packages/block-library/src/search/index.php b/packages/block-library/src/search/index.php index 29850a5cc2a550..00c912706e5d0a 100644 --- a/packages/block-library/src/search/index.php +++ b/packages/block-library/src/search/index.php @@ -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']; } } diff --git a/phpstan.neon.dist b/phpstan.neon.dist index 9ca9711515a713..087504b5b6c81b 100644 --- a/phpstan.neon.dist +++ b/phpstan.neon.dist @@ -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`, `@param T[TOffset] $value`). PHPStan does not resolve that diff --git a/phpunit/block-supports/shadow-test.php b/phpunit/block-supports/shadow-test.php index edcd1ff62061e5..4b3095a5b8d117 100644 --- a/phpunit/block-supports/shadow-test.php +++ b/phpunit/block-supports/shadow-test.php @@ -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(); } @@ -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(), + ), + ); + } } diff --git a/phpunit/blocks/render-block-avatar-test.php b/phpunit/blocks/render-block-avatar-test.php new file mode 100644 index 00000000000000..02bf59ae98a297 --- /dev/null +++ b/phpunit/blocks/render-block-avatar-test.php @@ -0,0 +1,67 @@ +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'] ); + } +} diff --git a/phpunit/blocks/render-block-icon-test.php b/phpunit/blocks/render-block-icon-test.php index fc382221767621..c24f7605911683 100644 --- a/phpunit/blocks/render-block-icon-test.php +++ b/phpunit/blocks/render-block-icon-test.php @@ -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' ) ) ); diff --git a/phpunit/blocks/render-block-navigation-submenu-test.php b/phpunit/blocks/render-block-navigation-submenu-test.php index c0e3352ecc82c6..12c2f5663cb37c 100644 --- a/phpunit/blocks/render-block-navigation-submenu-test.php +++ b/phpunit/blocks/render-block-navigation-submenu-test.php @@ -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( + ' + + ' + ); + + $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' ) ); + } } diff --git a/phpunit/blocks/render-block-post-featured-image-test.php b/phpunit/blocks/render-block-post-featured-image-test.php new file mode 100644 index 00000000000000..f24209acc2d340 --- /dev/null +++ b/phpunit/blocks/render-block-post-featured-image-test.php @@ -0,0 +1,78 @@ +post->create(); + self::$attachment_id = $factory->attachment->create_upload_object( DIR_TESTDATA . '/images/canola.jpg', self::$post_id ); + set_post_thumbnail( self::$post_id, self::$attachment_id ); + } + + public static function wpTearDownAfterClass() { + wp_delete_post( self::$attachment_id, true ); + } + + /** + * Renders a Post Featured Image block and returns the image's class and style attributes. + * + * @param array $attributes Block attributes. + * @return array Class and style of the image. + */ + private function render_image_attributes( $attributes ) { + $block = new WP_Block( + array( + 'blockName' => 'core/post-featured-image', + 'attrs' => $attributes, + ), + array( 'postId' => self::$post_id ) + ); + + $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( + 'style' => array( + 'border' => array( + 'radius' => '10px', + 'color' => '#ff0000', + ), + 'shadow' => '1px 1px 1px #000', + ), + ) + ); + + $this->assertSame( 'has-border-color wp-post-image', $actual['class'] ); + $this->assertSame( 'border-color:#ff0000;border-radius:10px;object-fit:cover;box-shadow:1px 1px 1px #000;', $actual['style'] ); + } +} diff --git a/phpunit/blocks/render-block-post-navigation-link-test.php b/phpunit/blocks/render-block-post-navigation-link-test.php index 76071e23857678..3acba75ea1c37e 100644 --- a/phpunit/blocks/render-block-post-navigation-link-test.php +++ b/phpunit/blocks/render-block-post-navigation-link-test.php @@ -227,4 +227,20 @@ public function test_null_from_the_post_link_filter_omits_the_styles() { $this->assertStringNotContainsString( 'style=', $output ); $this->assertStringNotContainsString( 'has-border-color', $output ); } + + public function test_rendered_link_full_support_classes_and_styles() { + $output = $this->render_block( self::$second_post_id, $this->styled_attributes() ); + + $processor = new WP_HTML_Tag_Processor( $output ); + $processor->next_tag( 'div' ); + + $this->assertSame( + 'post-navigation-link-previous has-border-color has-accent-2-border-color wp-block-post-navigation-link', + $processor->get_attribute( 'class' ) + ); + $this->assertSame( + 'border-style:dashed;border-width:3px;box-shadow:10px 10px 5px #000000;min-height:100px;min-width:200px;padding-top:20px;margin-top:30px', + $processor->get_attribute( 'style' ) + ); + } } diff --git a/phpunit/blocks/render-block-search-test.php b/phpunit/blocks/render-block-search-test.php new file mode 100644 index 00000000000000..57ef338125895d --- /dev/null +++ b/phpunit/blocks/render-block-search-test.php @@ -0,0 +1,54 @@ +original_block_to_render = WP_Block_Supports::$block_to_render; + WP_Block_Supports::$block_to_render = array( + 'blockName' => 'core/search', + 'attrs' => array(), + ); + } + + public function tear_down() { + WP_Block_Supports::$block_to_render = $this->original_block_to_render; + + parent::tear_down(); + } + + public function test_shadow_follows_border_on_the_input_and_button() { + $output = gutenberg_render_block_core_search( + array( + 'label' => 'Search', + 'buttonText' => 'Search', + 'buttonPosition' => 'button-outside', + 'placeholder' => '', + 'style' => array( + 'border' => array( 'width' => '2px' ), + 'shadow' => 'var:preset|shadow|natural', + ), + ) + ); + + $processor = new WP_HTML_Tag_Processor( $output ); + $this->assertTrue( $processor->next_tag( 'input' ) ); + $this->assertSame( 'border-width: 2px;box-shadow:var(--wp--preset--shadow--natural)', $processor->get_attribute( 'style' ) ); + $this->assertTrue( $processor->next_tag( 'button' ) ); + $this->assertSame( 'border-width: 2px;box-shadow:var(--wp--preset--shadow--natural)', $processor->get_attribute( 'style' ) ); + } +}