From 47833e43571dd9c2eb0560cd8a998451a4b9c8fc Mon Sep 17 00:00:00 2001 From: Carlos Bravo Date: Mon, 8 Nov 2021 17:28:55 +0100 Subject: [PATCH 1/2] Add spacing attributes to comment author avatar --- .../src/comment-author-avatar/block.json | 5 ++++ .../src/comment-author-avatar/edit.js | 9 +++++-- .../src/comment-author-avatar/index.php | 24 +++++++++++++++++-- 3 files changed, 34 insertions(+), 4 deletions(-) diff --git a/packages/block-library/src/comment-author-avatar/block.json b/packages/block-library/src/comment-author-avatar/block.json index e1ddf516d453..d4a1a735a178 100644 --- a/packages/block-library/src/comment-author-avatar/block.json +++ b/packages/block-library/src/comment-author-avatar/block.json @@ -30,6 +30,11 @@ "background": true, "text": false, "links": false + }, + "spacing": { + "__experimentalSkipSerialization": true, + "margin": true, + "padding": true } } } diff --git a/packages/block-library/src/comment-author-avatar/edit.js b/packages/block-library/src/comment-author-avatar/edit.js index 430d7779a977..e93aec55ec8e 100644 --- a/packages/block-library/src/comment-author-avatar/edit.js +++ b/packages/block-library/src/comment-author-avatar/edit.js @@ -1,7 +1,11 @@ /** * WordPress dependencies */ -import { InspectorControls, useBlockProps } from '@wordpress/block-editor'; +import { + InspectorControls, + useBlockProps, + __experimentalGetSpacingClassesAndStyles as useSpacingProps, +} from '@wordpress/block-editor'; import { PanelBody, ResizableBox, RangeControl } from '@wordpress/components'; import { useEntityProp } from '@wordpress/core-data'; import { __, _x, isRTL } from '@wordpress/i18n'; @@ -31,6 +35,7 @@ export default function Edit( { const minSize = sizes ? sizes[ 0 ] : 24; const maxSize = sizes ? sizes[ sizes.length - 1 ] : 96; const blockProps = useBlockProps(); + const spacingProps = useSpacingProps( attributes ); const maxSizeBuffer = Math.floor( maxSize * 2.5 ); const inspectorControls = ( @@ -90,7 +95,7 @@ export default function Edit( { return ( <> { inspectorControls } -
{ displayAvatar }
+
{ displayAvatar }
); } diff --git a/packages/block-library/src/comment-author-avatar/index.php b/packages/block-library/src/comment-author-avatar/index.php index 0aa7f9aeb8e1..204ec14475fc 100644 --- a/packages/block-library/src/comment-author-avatar/index.php +++ b/packages/block-library/src/comment-author-avatar/index.php @@ -25,6 +25,23 @@ function render_block_core_comment_author_avatar( $attributes, $content, $block // This is the only way to retreive 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 = isset($attributes['style']['spacing']) ? $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 = isset( $attributes['width'] ) ? $attributes['width'] : 96; $height = isset( $attributes['height'] ) ? $attributes['height'] : 96; $styles = isset( $wrapper_attributes['style'] ) ? $wrapper_attributes['style'] : ''; @@ -33,7 +50,7 @@ function render_block_core_comment_author_avatar( $attributes, $content, $block /* translators: %s is the Comment Author name */ $alt = sprintf( __( '%s Avatar' ), $comment->comment_author ); - $avatar_string = get_avatar( + $avatar_block = get_avatar( $comment, null, '', @@ -45,7 +62,10 @@ function render_block_core_comment_author_avatar( $attributes, $content, $block 'class' => $classes, ) ); - return $avatar_string; + if (isset($spacing_attributes)) { + return sprintf('
%2s
', $spacing_string, $avatar_block); + } + return sprintf('
%1s
', $avatar_block); } /** From 691c0a98e4b868ba1f4a34771a8bf158a8fe9580 Mon Sep 17 00:00:00 2001 From: Carlos Bravo Date: Wed, 10 Nov 2021 16:26:21 +0100 Subject: [PATCH 2/2] Fix php format --- .../src/comment-author-avatar/index.php | 33 +++++++++---------- 1 file changed, 16 insertions(+), 17 deletions(-) diff --git a/packages/block-library/src/comment-author-avatar/index.php b/packages/block-library/src/comment-author-avatar/index.php index 204ec14475fc..2010ce2ed764 100644 --- a/packages/block-library/src/comment-author-avatar/index.php +++ b/packages/block-library/src/comment-author-avatar/index.php @@ -29,23 +29,22 @@ function render_block_core_comment_author_avatar( $attributes, $content, $block /** * 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 = isset($attributes['style']['spacing']) ? $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_attributes = isset( $attributes['style']['spacing'] ) ? $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 ); + $spacing_string = implode( ';', $spacing_array ); } - $width = isset( $attributes['width'] ) ? $attributes['width'] : 96; - $height = isset( $attributes['height'] ) ? $attributes['height'] : 96; - $styles = isset( $wrapper_attributes['style'] ) ? $wrapper_attributes['style'] : ''; - $classes = isset( $wrapper_attributes['class'] ) ? $wrapper_attributes['class'] : ''; + $width = isset( $attributes['width'] ) ? $attributes['width'] : 96; + $height = isset( $attributes['height'] ) ? $attributes['height'] : 96; + $styles = isset( $wrapper_attributes['style'] ) ? $wrapper_attributes['style'] : ''; + $classes = isset( $wrapper_attributes['class'] ) ? $wrapper_attributes['class'] : ''; /* translators: %s is the Comment Author name */ $alt = sprintf( __( '%s Avatar' ), $comment->comment_author ); @@ -62,10 +61,10 @@ function render_block_core_comment_author_avatar( $attributes, $content, $block 'class' => $classes, ) ); - if (isset($spacing_attributes)) { - return sprintf('
%2s
', $spacing_string, $avatar_block); + if ( isset( $spacing_attributes ) ) { + return sprintf( '
%2s
', $spacing_string, $avatar_block ); } - return sprintf('
%1s
', $avatar_block); + return sprintf( '
%1s
', $avatar_block ); } /**