Repository navigation
[ New block ] - Create a Generic Avatar block for Authors #38591
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
Changes from all commits
394660c
593811f
e6f0879
f4e6bae
20d5223
cdddba0
e6bd79b
3bcfbf7
5ca5366
24940d0
2d6fd8c
bb0734c
1e32235
1116795
f7aec54
d596593
574669e
6470851
5fdb17a
14fb5f6
fac3a75
b4929ae
80a752c
b02aa9b
0446e90
7b60292
d3a1016
38e4f6f
9fd357b
27476da
dc8b559
28bfe25
1a97441
587e66c
59e45bd
4caa746
4421c29
5fdb07b
e7f8b43
712845b
19e1ea0
b1cf494
1e95930
f7938f5
4173e6f
cb1f7a7
693c74e
01fc801
7adfbd5
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,53 @@ | ||
| { | ||
| "$schema": "https://schemas.wp.org/trunk/block.json", | ||
| "apiVersion": 2, | ||
| "name": "core/avatar", | ||
| "title": "Avatar", | ||
| "category": "theme", | ||
| "description": "Add a user's avatar.", | ||
| "textdomain": "default", | ||
| "attributes": { | ||
| "userId": { | ||
| "type": "number" | ||
| }, | ||
| "size": { | ||
| "type": "number", | ||
| "default": 96 | ||
| }, | ||
| "isLink": { | ||
| "type": "boolean", | ||
| "default": false | ||
| }, | ||
| "linkTarget": { | ||
| "type": "string", | ||
| "default": "_self" | ||
| } | ||
|
|
||
| }, | ||
| "usesContext": [ "postType", "postId", "commentId" ], | ||
| "supports": { | ||
| "html": false, | ||
| "align": true, | ||
| "alignWide": false, | ||
| "spacing": { | ||
| "margin": true | ||
| }, | ||
| "__experimentalBorder": { | ||
| "__experimentalSkipSerialization": true, | ||
| "radius": true, | ||
| "width": true, | ||
| "color": true, | ||
| "style": true, | ||
| "__experimentalDefaultControls": { | ||
| "radius": true | ||
| } | ||
| }, | ||
| "color": { | ||
| "text": false, | ||
| "background": false, | ||
| "__experimentalDuotone": "img" | ||
| } | ||
| }, | ||
| "editorStyle": "wp-block-avatar", | ||
| "style": "wp-block-avatar" | ||
| } |
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,222 @@ | ||||||||||||||||||||||||||||||||||||
| /** | ||||||||||||||||||||||||||||||||||||
| * External dependencies | ||||||||||||||||||||||||||||||||||||
| */ | ||||||||||||||||||||||||||||||||||||
| import classnames from 'classnames'; | ||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||
| /** | ||||||||||||||||||||||||||||||||||||
| * WordPress dependencies | ||||||||||||||||||||||||||||||||||||
| */ | ||||||||||||||||||||||||||||||||||||
| import { | ||||||||||||||||||||||||||||||||||||
| InspectorControls, | ||||||||||||||||||||||||||||||||||||
| useBlockProps, | ||||||||||||||||||||||||||||||||||||
| __experimentalUseBorderProps as useBorderProps, | ||||||||||||||||||||||||||||||||||||
| } from '@wordpress/block-editor'; | ||||||||||||||||||||||||||||||||||||
| import { | ||||||||||||||||||||||||||||||||||||
| PanelBody, | ||||||||||||||||||||||||||||||||||||
| RangeControl, | ||||||||||||||||||||||||||||||||||||
| ResizableBox, | ||||||||||||||||||||||||||||||||||||
| ToggleControl, | ||||||||||||||||||||||||||||||||||||
| } from '@wordpress/components'; | ||||||||||||||||||||||||||||||||||||
| import { __, isRTL } from '@wordpress/i18n'; | ||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||
| /** | ||||||||||||||||||||||||||||||||||||
| * Internal dependencies | ||||||||||||||||||||||||||||||||||||
| */ | ||||||||||||||||||||||||||||||||||||
| import { useUserAvatar, useCommentAvatar } from './hooks'; | ||||||||||||||||||||||||||||||||||||
| import UserControl from './user-control'; | ||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||
| const AvatarInspectorControls = ( { | ||||||||||||||||||||||||||||||||||||
| setAttributes, | ||||||||||||||||||||||||||||||||||||
| avatar, | ||||||||||||||||||||||||||||||||||||
| attributes, | ||||||||||||||||||||||||||||||||||||
| selectUser, | ||||||||||||||||||||||||||||||||||||
| } ) => ( | ||||||||||||||||||||||||||||||||||||
| <InspectorControls> | ||||||||||||||||||||||||||||||||||||
| <PanelBody title={ __( 'Avatar Settings' ) }> | ||||||||||||||||||||||||||||||||||||
| <RangeControl | ||||||||||||||||||||||||||||||||||||
| label={ __( 'Image size' ) } | ||||||||||||||||||||||||||||||||||||
| onChange={ ( newSize ) => | ||||||||||||||||||||||||||||||||||||
| setAttributes( { | ||||||||||||||||||||||||||||||||||||
| size: newSize, | ||||||||||||||||||||||||||||||||||||
| } ) | ||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||
| min={ avatar.minSize } | ||||||||||||||||||||||||||||||||||||
| max={ avatar.maxSize } | ||||||||||||||||||||||||||||||||||||
| initialPosition={ attributes?.size } | ||||||||||||||||||||||||||||||||||||
| value={ attributes?.size } | ||||||||||||||||||||||||||||||||||||
| /> | ||||||||||||||||||||||||||||||||||||
| { selectUser && ( | ||||||||||||||||||||||||||||||||||||
| <UserControl | ||||||||||||||||||||||||||||||||||||
| value={ attributes?.userId } | ||||||||||||||||||||||||||||||||||||
| onChange={ ( value ) => { | ||||||||||||||||||||||||||||||||||||
| setAttributes( { | ||||||||||||||||||||||||||||||||||||
| userId: value, | ||||||||||||||||||||||||||||||||||||
| } ); | ||||||||||||||||||||||||||||||||||||
| } } | ||||||||||||||||||||||||||||||||||||
| /> | ||||||||||||||||||||||||||||||||||||
| ) } | ||||||||||||||||||||||||||||||||||||
| <ToggleControl | ||||||||||||||||||||||||||||||||||||
| label={ __( 'Link to user profile' ) } | ||||||||||||||||||||||||||||||||||||
| onChange={ () => | ||||||||||||||||||||||||||||||||||||
| setAttributes( { isLink: ! attributes.isLink } ) | ||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||
| checked={ attributes.isLink } | ||||||||||||||||||||||||||||||||||||
| /> | ||||||||||||||||||||||||||||||||||||
| { attributes.isLink && ( | ||||||||||||||||||||||||||||||||||||
| <ToggleControl | ||||||||||||||||||||||||||||||||||||
| label={ __( 'Open in new tab' ) } | ||||||||||||||||||||||||||||||||||||
| onChange={ ( value ) => | ||||||||||||||||||||||||||||||||||||
| setAttributes( { | ||||||||||||||||||||||||||||||||||||
| linkTarget: value ? '_blank' : '_self', | ||||||||||||||||||||||||||||||||||||
| } ) | ||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||
| checked={ attributes.linkTarget === '_blank' } | ||||||||||||||||||||||||||||||||||||
| /> | ||||||||||||||||||||||||||||||||||||
| ) } | ||||||||||||||||||||||||||||||||||||
| </PanelBody> | ||||||||||||||||||||||||||||||||||||
| </InspectorControls> | ||||||||||||||||||||||||||||||||||||
| ); | ||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||
| const ResizableAvatar = ( { | ||||||||||||||||||||||||||||||||||||
| setAttributes, | ||||||||||||||||||||||||||||||||||||
| attributes, | ||||||||||||||||||||||||||||||||||||
| avatar, | ||||||||||||||||||||||||||||||||||||
| blockProps, | ||||||||||||||||||||||||||||||||||||
| isSelected, | ||||||||||||||||||||||||||||||||||||
| } ) => { | ||||||||||||||||||||||||||||||||||||
| const borderProps = useBorderProps( attributes ); | ||||||||||||||||||||||||||||||||||||
| return ( | ||||||||||||||||||||||||||||||||||||
| <div { ...blockProps }> | ||||||||||||||||||||||||||||||||||||
| <ResizableBox | ||||||||||||||||||||||||||||||||||||
| size={ { | ||||||||||||||||||||||||||||||||||||
| width: attributes.size, | ||||||||||||||||||||||||||||||||||||
| height: attributes.size, | ||||||||||||||||||||||||||||||||||||
| } } | ||||||||||||||||||||||||||||||||||||
| showHandle={ isSelected } | ||||||||||||||||||||||||||||||||||||
| onResizeStop={ ( event, direction, elt, delta ) => { | ||||||||||||||||||||||||||||||||||||
| setAttributes( { | ||||||||||||||||||||||||||||||||||||
| size: parseInt( | ||||||||||||||||||||||||||||||||||||
| attributes.size + ( delta.height || delta.width ), | ||||||||||||||||||||||||||||||||||||
| 10 | ||||||||||||||||||||||||||||||||||||
| ), | ||||||||||||||||||||||||||||||||||||
| } ); | ||||||||||||||||||||||||||||||||||||
| } } | ||||||||||||||||||||||||||||||||||||
| lockAspectRatio | ||||||||||||||||||||||||||||||||||||
| enable={ { | ||||||||||||||||||||||||||||||||||||
| top: false, | ||||||||||||||||||||||||||||||||||||
| right: ! isRTL(), | ||||||||||||||||||||||||||||||||||||
| bottom: true, | ||||||||||||||||||||||||||||||||||||
| left: isRTL(), | ||||||||||||||||||||||||||||||||||||
| } } | ||||||||||||||||||||||||||||||||||||
| minWidth={ avatar.minSize } | ||||||||||||||||||||||||||||||||||||
| maxWidth={ avatar.maxSize } | ||||||||||||||||||||||||||||||||||||
| > | ||||||||||||||||||||||||||||||||||||
| <img | ||||||||||||||||||||||||||||||||||||
| src={ avatar.src } | ||||||||||||||||||||||||||||||||||||
| alt={ avatar.alt } | ||||||||||||||||||||||||||||||||||||
| { ...borderProps } | ||||||||||||||||||||||||||||||||||||
| className={ classnames( | ||||||||||||||||||||||||||||||||||||
| 'avatar', | ||||||||||||||||||||||||||||||||||||
| 'avatar-' + attributes.size, | ||||||||||||||||||||||||||||||||||||
| 'photo', | ||||||||||||||||||||||||||||||||||||
| 'wp-block-avatar__image', | ||||||||||||||||||||||||||||||||||||
| borderProps.className | ||||||||||||||||||||||||||||||||||||
| ) } | ||||||||||||||||||||||||||||||||||||
| style={ { | ||||||||||||||||||||||||||||||||||||
| ...borderProps.style, // Border radius, width and style. | ||||||||||||||||||||||||||||||||||||
| } } | ||||||||||||||||||||||||||||||||||||
| /> | ||||||||||||||||||||||||||||||||||||
| </ResizableBox> | ||||||||||||||||||||||||||||||||||||
| </div> | ||||||||||||||||||||||||||||||||||||
| ); | ||||||||||||||||||||||||||||||||||||
| }; | ||||||||||||||||||||||||||||||||||||
| const CommentEdit = ( { attributes, context, setAttributes, isSelected } ) => { | ||||||||||||||||||||||||||||||||||||
| const { commentId } = context; | ||||||||||||||||||||||||||||||||||||
| const blockProps = useBlockProps(); | ||||||||||||||||||||||||||||||||||||
| const avatar = useCommentAvatar( { commentId } ); | ||||||||||||||||||||||||||||||||||||
| return ( | ||||||||||||||||||||||||||||||||||||
| <> | ||||||||||||||||||||||||||||||||||||
| <AvatarInspectorControls | ||||||||||||||||||||||||||||||||||||
| avatar={ avatar } | ||||||||||||||||||||||||||||||||||||
| setAttributes={ setAttributes } | ||||||||||||||||||||||||||||||||||||
| attributes={ attributes } | ||||||||||||||||||||||||||||||||||||
| selectUser={ false } | ||||||||||||||||||||||||||||||||||||
| /> | ||||||||||||||||||||||||||||||||||||
| { attributes.isLink ? ( | ||||||||||||||||||||||||||||||||||||
| <a | ||||||||||||||||||||||||||||||||||||
| href="#avatar-pseudo-link" | ||||||||||||||||||||||||||||||||||||
| className="wp-block-avatar__link" | ||||||||||||||||||||||||||||||||||||
| onClick={ ( event ) => event.preventDefault() } | ||||||||||||||||||||||||||||||||||||
| > | ||||||||||||||||||||||||||||||||||||
| <ResizableAvatar | ||||||||||||||||||||||||||||||||||||
| attributes={ attributes } | ||||||||||||||||||||||||||||||||||||
| avatar={ avatar } | ||||||||||||||||||||||||||||||||||||
| blockProps={ blockProps } | ||||||||||||||||||||||||||||||||||||
| isSelected={ isSelected } | ||||||||||||||||||||||||||||||||||||
| setAttributes={ setAttributes } | ||||||||||||||||||||||||||||||||||||
| /> | ||||||||||||||||||||||||||||||||||||
| </a> | ||||||||||||||||||||||||||||||||||||
| ) : ( | ||||||||||||||||||||||||||||||||||||
| <ResizableAvatar | ||||||||||||||||||||||||||||||||||||
| attributes={ attributes } | ||||||||||||||||||||||||||||||||||||
| avatar={ avatar } | ||||||||||||||||||||||||||||||||||||
| blockProps={ blockProps } | ||||||||||||||||||||||||||||||||||||
| isSelected={ isSelected } | ||||||||||||||||||||||||||||||||||||
| setAttributes={ setAttributes } | ||||||||||||||||||||||||||||||||||||
| /> | ||||||||||||||||||||||||||||||||||||
| ) } | ||||||||||||||||||||||||||||||||||||
| </> | ||||||||||||||||||||||||||||||||||||
| ); | ||||||||||||||||||||||||||||||||||||
| }; | ||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||
| const UserEdit = ( { attributes, context, setAttributes, isSelected } ) => { | ||||||||||||||||||||||||||||||||||||
| const { postId, postType } = context; | ||||||||||||||||||||||||||||||||||||
| const avatar = useUserAvatar( { | ||||||||||||||||||||||||||||||||||||
| userId: attributes?.userId, | ||||||||||||||||||||||||||||||||||||
| postId, | ||||||||||||||||||||||||||||||||||||
| postType, | ||||||||||||||||||||||||||||||||||||
| } ); | ||||||||||||||||||||||||||||||||||||
| const blockProps = useBlockProps(); | ||||||||||||||||||||||||||||||||||||
| return ( | ||||||||||||||||||||||||||||||||||||
| <> | ||||||||||||||||||||||||||||||||||||
| <AvatarInspectorControls | ||||||||||||||||||||||||||||||||||||
| selectUser={ true } | ||||||||||||||||||||||||||||||||||||
| attributes={ attributes } | ||||||||||||||||||||||||||||||||||||
| avatar={ avatar } | ||||||||||||||||||||||||||||||||||||
| setAttributes={ setAttributes } | ||||||||||||||||||||||||||||||||||||
| /> | ||||||||||||||||||||||||||||||||||||
| <div> | ||||||||||||||||||||||||||||||||||||
| { attributes.isLink ? ( | ||||||||||||||||||||||||||||||||||||
| <a | ||||||||||||||||||||||||||||||||||||
| href="#avatar-pseudo-link" | ||||||||||||||||||||||||||||||||||||
| className="wp-block-avatar__link" | ||||||||||||||||||||||||||||||||||||
| onClick={ ( event ) => event.preventDefault() } | ||||||||||||||||||||||||||||||||||||
| > | ||||||||||||||||||||||||||||||||||||
| <ResizableAvatar | ||||||||||||||||||||||||||||||||||||
| attributes={ attributes } | ||||||||||||||||||||||||||||||||||||
| avatar={ avatar } | ||||||||||||||||||||||||||||||||||||
| blockProps={ blockProps } | ||||||||||||||||||||||||||||||||||||
| isSelected={ isSelected } | ||||||||||||||||||||||||||||||||||||
| setAttributes={ setAttributes } | ||||||||||||||||||||||||||||||||||||
| /> | ||||||||||||||||||||||||||||||||||||
| </a> | ||||||||||||||||||||||||||||||||||||
| ) : ( | ||||||||||||||||||||||||||||||||||||
| <ResizableAvatar | ||||||||||||||||||||||||||||||||||||
| attributes={ attributes } | ||||||||||||||||||||||||||||||||||||
| avatar={ avatar } | ||||||||||||||||||||||||||||||||||||
| blockProps={ blockProps } | ||||||||||||||||||||||||||||||||||||
| isSelected={ isSelected } | ||||||||||||||||||||||||||||||||||||
| setAttributes={ setAttributes } | ||||||||||||||||||||||||||||||||||||
| /> | ||||||||||||||||||||||||||||||||||||
| ) } | ||||||||||||||||||||||||||||||||||||
| </div> | ||||||||||||||||||||||||||||||||||||
| </> | ||||||||||||||||||||||||||||||||||||
| ); | ||||||||||||||||||||||||||||||||||||
| }; | ||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||
| export default function Edit( props ) { | ||||||||||||||||||||||||||||||||||||
| if ( props?.context?.commentId ) { | ||||||||||||||||||||||||||||||||||||
| return <CommentEdit { ...props } />; | ||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||
| return <UserEdit { ...props } />; | ||||||||||||||||||||||||||||||||||||
|
Comment on lines
+218
to
+221
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
I feel that that represents a bit better which parts of the logic are shared by the comment and user avatar cases, and which parts we need to differentiate them. (I'd also consider making the
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Hey @ockham, for me that would be the ideal solution. 😄 However, I guess @c4rl0sbr4v0's decision of making two different components here was because React hooks cannot be used conditionally. 🤷 Another option that just came to my mind could be moving the conditional checking inside a
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| .wp-block-avatar__image img { | ||
| width: 100%; | ||
| } |

There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think that this text may be incorrect for the comments, where the link is often an external website?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I thought about it. But also, I changed the website on the user settings and could not see any website link.
Do we know from where WordPress fetch that information?
Could we rephrase to 'Link to user website or author archive'?