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
61 changes: 46 additions & 15 deletions lib/block-supports/spacing.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,28 +43,59 @@ function gutenberg_apply_spacing_support( $block_type, $block_attributes ) {
return array();
}

$attributes = array();
if ( ! isset( $block_attributes['style']['spacing'] ) || ! is_array( $block_attributes['style']['spacing'] ) ) {
return array();
}

$has_padding_support = block_has_support( $block_type, array( 'spacing', 'padding' ), false );
$has_margin_support = block_has_support( $block_type, array( 'spacing', 'margin' ), false );
$block_styles = $block_attributes['style'] ?? null;
$skip_padding = wp_should_skip_block_supports_serialization( $block_type, 'spacing', 'padding' );
$skip_margin = wp_should_skip_block_supports_serialization( $block_type, 'spacing', 'margin' );

if ( ! $block_styles ) {
return $attributes;
if ( ! $has_padding_support || $skip_padding ) {
unset( $block_attributes['style']['spacing']['padding'] );
}

$skip_padding = wp_should_skip_block_supports_serialization( $block_type, 'spacing', 'padding' );
$skip_margin = wp_should_skip_block_supports_serialization( $block_type, 'spacing', 'margin' );
$spacing_block_styles = array(
'padding' => null,
'margin' => null,
);
if ( $has_padding_support && ! $skip_padding ) {
$spacing_block_styles['padding'] = $block_styles['spacing']['padding'] ?? null;
if ( ! $has_margin_support || $skip_margin ) {
unset( $block_attributes['style']['spacing']['margin'] );
}
if ( $has_margin_support && ! $skip_margin ) {
$spacing_block_styles['margin'] = $block_styles['spacing']['margin'] ?? null;

return gutenberg_get_spacing_classes_and_styles( $block_attributes );
}

/**
* Returns spacing classes and inline styles for block attributes, like the JS
* `getSpacingClassesAndStyles()`. 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_spacing_classes_and_styles( $block_attributes ) {
if ( ! is_array( $block_attributes ) ) {
return array();
}

$spacing = $block_attributes['style']['spacing'] ?? null;
if ( ! is_array( $spacing ) ) {
return array();
}

$styles = gutenberg_style_engine_get_styles(
array(
'spacing' => array(
'padding' => $spacing['padding'] ?? null,
'margin' => $spacing['margin'] ?? null,
),
)
);
$attributes = array();

if ( ! empty( $styles['classnames'] ) ) {
$attributes['class'] = $styles['classnames'];
}
$styles = gutenberg_style_engine_get_styles( array( 'spacing' => $spacing_block_styles ) );

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 @@ -74,6 +74,7 @@
"wp_get_typography_font_size_value",
"wp_get_shadow_classes_and_styles",
"wp_style_engine_get_styles",
"wp_get_spacing_classes_and_styles",
"wp_get_global_styles",
"wp_get_global_settings"
],
Expand Down
22 changes: 5 additions & 17 deletions packages/block-library/src/comment-author-avatar/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,21 +26,6 @@ function render_block_core_comment_author_avatar( $attributes, $content, $block
// This is the only way to retrieve style and classes on different instances.
$wrapper_attributes = WP_Block_Supports::get_instance()->apply_block_supports();

/**
* We get the spacing attributes and transform the array provided into a string formatted for being applied as a style html tag.
* Good candidate to be moved to a separate function in core.
*/
$spacing_attributes = $attributes['style']['spacing'] ?? null;
if ( isset( $spacing_attributes ) && ! empty( $spacing_attributes ) ) {
$spacing_array = array();
foreach ( $spacing_attributes as $spacing_attribute_key => $spacing_attribute_value ) {
foreach ( $spacing_attribute_value as $position_key => $position_value ) {
$spacing_array[] = $spacing_attribute_key . '-' . $position_key . ': ' . $position_value;
}
}
$spacing_string = implode( ';', $spacing_array );
}

$width = $attributes['width'] ?? 96;
$height = $attributes['height'] ?? 96;
$styles = $wrapper_attributes['style'] ?? '';
Expand All @@ -61,8 +46,11 @@ function render_block_core_comment_author_avatar( $attributes, $content, $block
'class' => $classes,
)
);
if ( isset( $spacing_attributes ) ) {
return sprintf( '<div style="%1$s">%2$s</div>', esc_attr( $spacing_string ), $avatar_block );

// Spacing skips serialization, so it goes on the outer div, not the image.
$spacing_styles = wp_get_spacing_classes_and_styles( $attributes );
if ( ! empty( $spacing_styles['style'] ) ) {
return sprintf( '<div style="%1$s">%2$s</div>', esc_attr( $spacing_styles['style'] ), $avatar_block );
}
return sprintf( '<div>%s</div>', $avatar_block );
}
Expand Down
19 changes: 11 additions & 8 deletions packages/block-library/src/icon/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -86,12 +86,6 @@ function render_block_core_icon( $attributes ) {
// Shadow.
$shadow_styles = $attributes['style']['shadow'] ?? null;

// Spacing (Padding).
$spacing_styles = array();
if ( isset( $attributes['style']['spacing']['padding'] ) ) {
$spacing_styles['padding'] = $attributes['style']['spacing']['padding'];
}

// Dimensions (Width, Min width, Min height).
$dimensions_styles = array();
foreach ( array( 'width', 'minWidth', 'minHeight' ) as $dimension ) {
Expand All @@ -101,16 +95,25 @@ function render_block_core_icon( $attributes ) {
}

// Generate styles and classes.
$styles = wp_style_engine_get_styles(
$styles = wp_style_engine_get_styles(
array(
'color' => $color_styles,
'background' => $background_styles,
'border' => $border_styles,
'shadow' => $shadow_styles,
'spacing' => $spacing_styles,
'dimensions' => $dimensions_styles,
),
);
$spacing_styles = wp_get_spacing_classes_and_styles(
array(
'style' => array(
'spacing' => array(
'padding' => $attributes['style']['spacing']['padding'] ?? null,
),
),
)
);
$styles['css'] = ( $styles['css'] ?? '' ) . ( $spacing_styles['style'] ?? '' );

$svg = wp_get_icon(
$attributes['icon'],
Expand Down
9 changes: 3 additions & 6 deletions packages/block-library/src/post-navigation-link/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,7 @@ function block_core_post_navigation_link_get_support_styles( $attributes ) {
}
}

$styles = wp_style_engine_get_styles(
$styles = wp_style_engine_get_styles(
array(
'background' => $background_styles,
'border' => $border_styles,
Expand All @@ -224,12 +224,9 @@ function block_core_post_navigation_link_get_support_styles( $attributes ) {
'minWidth' => $block_styles['dimensions']['minWidth'] ?? null,
),
'shadow' => $block_styles['shadow'] ?? null,
'spacing' => array(
'margin' => $block_styles['spacing']['margin'] ?? null,
'padding' => $block_styles['spacing']['padding'] ?? null,
),
)
);
$spacing_styles = wp_get_spacing_classes_and_styles( $attributes );

$class_names = $styles['classnames'] ?? '';

Expand All @@ -240,7 +237,7 @@ function block_core_post_navigation_link_get_support_styles( $attributes ) {

return array(
'class' => $class_names,
'style' => $styles['css'] ?? '',
'style' => ( $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 @@ -38,6 +38,12 @@ parameters:
message: '#^Function wp_get_shadow_classes_and_styles not found\.$#'
identifier: function.notFound

# wp_get_spacing_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_spacing_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
100 changes: 100 additions & 0 deletions phpunit/block-supports/spacing-test.php
Original file line number Diff line number Diff line change
Expand Up @@ -157,4 +157,104 @@ public function test_margin_with_individual_skipped_serialization_block_supports

$this->assertSame( $expected, $actual );
}

public function test_unsupported_padding_is_not_applied() {
$this->test_block_name = 'test/unsupported-padding-is-not-applied';
register_block_type(
$this->test_block_name,
array(
'api_version' => 3,
'attributes' => array(
'style' => array(
'type' => 'object',
),
),
'supports' => array(
'spacing' => array(
'margin' => true,
),
),
)
);
$registry = WP_Block_Type_Registry::get_instance();
$block_type = $registry->get_registered( $this->test_block_name );
$block_atts = array(
'style' => array(
'spacing' => array(
'margin' => '1px',
'padding' => '2px',
),
),
);

$actual = gutenberg_apply_spacing_support( $block_type, $block_atts );
$expected = array(
'style' => 'margin:1px;',
);

$this->assertSame( $expected, $actual );
}

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

/**
* Data provider.
*
* @return array
*/
public function data_get_spacing_classes_and_styles() {
return array(
'custom values' => array(
'block_attributes' => array(
'style' => array(
'spacing' => array(
'padding' => array(
'top' => '1px',
'left' => '2em',
),
'margin' => '3px',
),
),
),
'expected' => array(
'style' => 'padding-top:1px;padding-left:2em;margin:3px;',
),
),
'presets' => array(
'block_attributes' => array(
'style' => array(
'spacing' => array(
'padding' => 'var:preset|spacing|40',
'margin' => array( 'bottom' => 'var:preset|spacing|20' ),
),
),
),
'expected' => array(
'style' => 'padding:var(--wp--preset--spacing--40);margin-bottom:var(--wp--preset--spacing--20);',
),
),
'malformed spacing' => array(
'block_attributes' => array(
'style' => array(
'spacing' => '10px',
),
),
'expected' => array(),
),
'object attributes' => array(
'block_attributes' => (object) array( 'style' => array( 'spacing' => array( 'margin' => '3px' ) ) ),
'expected' => array(),
),
);
}
}
87 changes: 87 additions & 0 deletions phpunit/blocks/render-block-comment-author-avatar-test.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
<?php
/**
* Comment Author Avatar block rendering tests.
*
* @package WordPress
* @subpackage Blocks
*/

/**
* Tests for the Comment Author Avatar block.
*
* @group blocks
*
* @covers ::gutenberg_render_block_core_comment_author_avatar
*/
class Tests_Blocks_Render_Comment_Author_Avatar extends WP_UnitTestCase {

/**
* Comment to render the avatar for.
*
* @var int
*/
private static $comment_id;

public static function wpSetUpBeforeClass( $factory ) {
$post_id = $factory->post->create();
self::$comment_id = $factory->comment->create(
array(
'comment_post_ID' => $post_id,
'comment_author' => 'Test',
'comment_author_email' => 'test@example.org',
)
);
}

/**
* Renders a Comment Author Avatar block and returns its wrapper style.
*
* @param array $attributes Block attributes.
* @return string|null Wrapper `style` attribute.
*/
private function get_wrapper_style( $attributes ) {
$parsed_block = parse_blocks( '<!-- wp:comment-author-avatar ' . wp_json_encode( (object) $attributes ) . ' /-->' )[0];
$block = new WP_Block( $parsed_block, array( 'commentId' => self::$comment_id ) );

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

return $processor->get_attribute( 'style' );
}

/**
* @dataProvider data_spacing_styles
*
* @param array $spacing Spacing attribute.
* @param string $expected_style Expected wrapper style.
*/
public function test_wrapper_has_spacing_styles( $spacing, $expected_style ) {
$this->assertSame( $expected_style, $this->get_wrapper_style( array( 'style' => array( 'spacing' => $spacing ) ) ) );
}

/**
* Data provider.
*
* @return array
*/
public function data_spacing_styles() {
return array(
'preset' => array(
'spacing' => array( 'padding' => array( 'top' => 'var:preset|spacing|40' ) ),
'expected_style' => 'padding-top:var(--wp--preset--spacing--40);',
),
'padding and margin' => array(
'spacing' => array(
'margin' => array( 'top' => '5px' ),
'padding' => array( 'left' => '10px' ),
),
'expected_style' => 'padding-left:10px;margin-top:5px;',
),
);
}

public function test_wrapper_has_no_style_without_spacing() {
$this->assertNull( $this->get_wrapper_style( array() ) );
$this->assertNull( $this->get_wrapper_style( array( 'style' => array( 'spacing' => array() ) ) ) );
}
}
Loading
Loading