Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add spacing attributes to comment author avatar #36322

Merged
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 5 additions & 0 deletions packages/block-library/src/comment-author-avatar/block.json
Expand Up @@ -30,6 +30,11 @@
"background": true,
"text": false,
"links": false
},
"spacing": {
"__experimentalSkipSerialization": true,
"margin": true,
"padding": true
}
}
}
9 changes: 7 additions & 2 deletions 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';
Expand Down Expand Up @@ -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 = (
Expand Down Expand Up @@ -90,7 +95,7 @@ export default function Edit( {
return (
<>
{ inspectorControls }
<div>{ displayAvatar }</div>
<div { ...spacingProps }>{ displayAvatar }</div>
</>
);
}
31 changes: 25 additions & 6 deletions packages/block-library/src/comment-author-avatar/index.php
Expand Up @@ -25,15 +25,31 @@ 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();
$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'] : '';

/**
* 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'] : '';
$classes = isset( $wrapper_attributes['class'] ) ? $wrapper_attributes['class'] : '';

/* translators: %s is the Comment Author name */
$alt = sprintf( __( '%s Avatar' ), $comment->comment_author );

$avatar_string = get_avatar(
$avatar_block = get_avatar(
$comment,
null,
'',
Expand All @@ -45,7 +61,10 @@ function render_block_core_comment_author_avatar( $attributes, $content, $block
'class' => $classes,
)
);
return $avatar_string;
if ( isset( $spacing_attributes ) ) {
return sprintf( '<div style="%1s">%2s</div>', $spacing_string, $avatar_block );
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One thing that came up to my mind. Should we sanitize the computed string for the style?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep, seems a good idea. Should I create a new PR, right?

Copy link
Member

@gziolo gziolo Nov 29, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, that's the best way to move forward. It's also why a proper API would be better as we would have a central place to address this type of concers 😄

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in #36988, thank you!

}
return sprintf( '<div>%1s</div>', $avatar_block );
}

/**
Expand Down