diff --git a/docs/reference-guides/core-blocks.md b/docs/reference-guides/core-blocks.md index eee9d488f3c77a..01c7aaade1a850 100644 --- a/docs/reference-guides/core-blocks.md +++ b/docs/reference-guides/core-blocks.md @@ -26,6 +26,15 @@ Embed a simple audio player. ([Source](https://github.com/WordPress/gutenberg/tr - **Supports:** align, anchor - **Attributes:** autoplay, caption, id, loop, preload, src +## Avatar + +Add a user's avatar. ([Source](https://github.com/WordPress/gutenberg/tree/trunk/packages/block-library/src/avatar)) + +- **Name:** core/avatar +- **Category:** theme +- **Supports:** align, color (~~background~~, ~~text~~), spacing (margin), ~~alignWide~~, ~~html~~ +- **Attributes:** isLink, linkTarget, size, userId + ## Reusable block Create and save content to reuse across your site. Update the block, and the changes apply everywhere it’s used. ([Source](https://github.com/WordPress/gutenberg/tree/trunk/packages/block-library/src/block)) @@ -104,7 +113,7 @@ Add the avatar of this comment's author. ([Source](https://github.com/WordPress/ - **Name:** core/comment-author-avatar - **Category:** theme -- **Supports:** color (background, ~~text~~), spacing (margin, padding), ~~html~~ +- **Supports:** color (background, ~~text~~), spacing (margin, padding), ~~html~~, ~~inserter~~ - **Attributes:** height, width ## Comment Author Name diff --git a/lib/blocks.php b/lib/blocks.php index 4dda3aacfa4e26..5ff1cc5d907281 100644 --- a/lib/blocks.php +++ b/lib/blocks.php @@ -53,6 +53,7 @@ function gutenberg_reregister_core_block_types() { ), 'block_names' => array( 'archives.php' => 'core/archives', + 'avatar.php' => 'core/avatar', 'block.php' => 'core/block', 'calendar.php' => 'core/calendar', 'categories.php' => 'core/categories', diff --git a/packages/block-library/src/avatar/block.json b/packages/block-library/src/avatar/block.json new file mode 100644 index 00000000000000..537e21422b41d4 --- /dev/null +++ b/packages/block-library/src/avatar/block.json @@ -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" +} diff --git a/packages/block-library/src/avatar/edit.js b/packages/block-library/src/avatar/edit.js new file mode 100644 index 00000000000000..8a14e153b295f2 --- /dev/null +++ b/packages/block-library/src/avatar/edit.js @@ -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, +} ) => ( + + + + setAttributes( { + size: newSize, + } ) + } + min={ avatar.minSize } + max={ avatar.maxSize } + initialPosition={ attributes?.size } + value={ attributes?.size } + /> + { selectUser && ( + { + setAttributes( { + userId: value, + } ); + } } + /> + ) } + + setAttributes( { isLink: ! attributes.isLink } ) + } + checked={ attributes.isLink } + /> + { attributes.isLink && ( + + setAttributes( { + linkTarget: value ? '_blank' : '_self', + } ) + } + checked={ attributes.linkTarget === '_blank' } + /> + ) } + + +); + +const ResizableAvatar = ( { + setAttributes, + attributes, + avatar, + blockProps, + isSelected, +} ) => { + const borderProps = useBorderProps( attributes ); + return ( +
+ { + 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 } + > + { + +
+ ); +}; +const CommentEdit = ( { attributes, context, setAttributes, isSelected } ) => { + const { commentId } = context; + const blockProps = useBlockProps(); + const avatar = useCommentAvatar( { commentId } ); + return ( + <> + + { attributes.isLink ? ( + event.preventDefault() } + > + + + ) : ( + + ) } + + ); +}; + +const UserEdit = ( { attributes, context, setAttributes, isSelected } ) => { + const { postId, postType } = context; + const avatar = useUserAvatar( { + userId: attributes?.userId, + postId, + postType, + } ); + const blockProps = useBlockProps(); + return ( + <> + +
+ { attributes.isLink ? ( + event.preventDefault() } + > + + + ) : ( + + ) } +
+ + ); +}; + +export default function Edit( props ) { + if ( props?.context?.commentId ) { + return ; + } + return ; +} diff --git a/packages/block-library/src/avatar/editor.scss b/packages/block-library/src/avatar/editor.scss new file mode 100644 index 00000000000000..d88962199fe3cf --- /dev/null +++ b/packages/block-library/src/avatar/editor.scss @@ -0,0 +1,3 @@ +.wp-block-avatar__image img { + width: 100%; +} diff --git a/packages/block-library/src/avatar/hooks.js b/packages/block-library/src/avatar/hooks.js new file mode 100644 index 00000000000000..fcc7090547b14f --- /dev/null +++ b/packages/block-library/src/avatar/hooks.js @@ -0,0 +1,96 @@ +/** + * WordPress dependencies + */ +import { store as blockEditorStore } from '@wordpress/block-editor'; +import { store as coreStore, useEntityProp } from '@wordpress/core-data'; +import { __, sprintf } from '@wordpress/i18n'; +import { useSelect } from '@wordpress/data'; + +function getAvatarSizes( sizes ) { + const minSize = sizes ? sizes[ 0 ] : 24; + const maxSize = sizes ? sizes[ sizes.length - 1 ] : 96; + const maxSizeBuffer = Math.floor( maxSize * 2.5 ); + return { + minSize, + maxSize: maxSizeBuffer, + }; +} + +function useDefaultAvatar() { + const { avatarURL: defaultAvatarUrl } = useSelect( ( select ) => { + const { getSettings } = select( blockEditorStore ); + const { __experimentalDiscussionSettings } = getSettings(); + return __experimentalDiscussionSettings; + } ); + return defaultAvatarUrl; +} + +export function useCommentAvatar( { commentId } ) { + const [ avatars ] = useEntityProp( + 'root', + 'comment', + 'author_avatar_urls', + commentId + ); + + const [ authorName ] = useEntityProp( + 'root', + 'comment', + 'author_name', + commentId + ); + const avatarUrls = avatars ? Object.values( avatars ) : null; + const sizes = avatars ? Object.keys( avatars ) : null; + const { minSize, maxSize } = getAvatarSizes( sizes ); + const defaultAvatar = useDefaultAvatar(); + return { + src: avatarUrls ? avatarUrls[ avatarUrls.length - 1 ] : defaultAvatar, + minSize, + maxSize, + // translators: %s is the Author name. + alt: authorName + ? // translators: %s is the Author name. + sprintf( __( '%s Avatar' ), authorName ) + : __( 'Default Avatar' ), + }; +} + +export function useUserAvatar( { userId, postId, postType } ) { + const { authorDetails } = useSelect( + ( select ) => { + const { getEditedEntityRecord, getUser } = select( coreStore ); + if ( userId ) { + return { + authorDetails: getUser( userId ), + }; + } + const _authorId = getEditedEntityRecord( + 'postType', + postType, + postId + )?.author; + + return { + authorDetails: _authorId ? getUser( _authorId ) : null, + }; + }, + [ postType, postId, userId ] + ); + const avatarUrls = authorDetails + ? Object.values( authorDetails.avatar_urls ) + : null; + const sizes = authorDetails + ? Object.keys( authorDetails.avatar_urls ) + : null; + const { minSize, maxSize } = getAvatarSizes( sizes ); + const defaultAvatar = useDefaultAvatar(); + return { + src: avatarUrls ? avatarUrls[ avatarUrls.length - 1 ] : defaultAvatar, + minSize, + maxSize, + alt: authorDetails + ? // translators: %s is the Author name. + sprintf( __( '%s Avatar' ), authorDetails?.name ) + : __( 'Default Avatar' ), + }; +} diff --git a/packages/block-library/src/avatar/index.js b/packages/block-library/src/avatar/index.js new file mode 100644 index 00000000000000..ef71f2751459a0 --- /dev/null +++ b/packages/block-library/src/avatar/index.js @@ -0,0 +1,18 @@ +/** + * WordPress dependencies + */ +import { commentAuthorAvatar as icon } from '@wordpress/icons'; + +/** + * Internal dependencies + */ +import metadata from './block.json'; +import edit from './edit'; + +const { name } = metadata; +export { metadata, name }; + +export const settings = { + icon, + edit, +}; diff --git a/packages/block-library/src/avatar/index.php b/packages/block-library/src/avatar/index.php new file mode 100644 index 00000000000000..aaddbe77fb044b --- /dev/null +++ b/packages/block-library/src/avatar/index.php @@ -0,0 +1,146 @@ + $value ) { + if ( null !== $value ) { + $name = _wp_to_kebab_case( $key ); + // Add shared styles for individual border radii. + $border_style = sprintf( + 'border-%s-radius: %s;', + esc_attr( $name ), + esc_attr( $value ) + ); + $image_styles[] = $border_style; + } + } + } else { + $border_style = sprintf( 'border-radius: %s;', esc_attr( $border_radius ) ); + $image_styles[] = $border_style; + } + } + + // Add border color styles. + $has_border_color = ! empty( $attributes['style']['border']['color'] ); + + if ( $has_border_color ) { + $border_color = $attributes['style']['border']['color']; + $image_styles[] = sprintf( 'border-color: %s;', esc_attr( $border_color ) ); + } + + // Add border style (solid, dashed, dotted ). + $has_border_style = ! empty( $attributes['style']['border']['style'] ); + + if ( $has_border_style ) { + $border_style = $attributes['style']['border']['style']; + $image_styles[] = sprintf( 'border-style: %s;', esc_attr( $border_style ) ); + } + + // Add border classes to the avatar image for both custom colors and palette colors. + $image_classes = ''; + if ( $has_border_color || isset( $attributes['borderColor'] ) ) { + $image_classes .= 'has-border-color'; + } + if ( isset( $attributes['borderColor'] ) ) { + $image_classes .= ' has-' . $attributes['borderColor'] . '-border-color'; + } + + if ( ! isset( $block->context['commentId'] ) ) { + $author_id = isset( $attributes['userId'] ) ? $attributes['userId'] : get_post_field( 'post_author', $block->context['postId'] ); + $author_name = get_the_author_meta( 'display_name', $author_id ); + // translators: %s is the Author name. + $alt = sprintf( __( '%s Avatar' ), $author_name ); + $avatar_block = get_avatar( + $author_id, + $size, + '', + $alt, + array( + 'extra_attr' => isset( $image_styles ) ? sprintf( ' style="%s"', safecss_filter_attr( implode( ' ', $image_styles ) ) ) : '', + 'class' => "wp-block-avatar__image $image_classes ", + ) + ); + if ( isset( $attributes['isLink'] ) && $attributes['isLink'] ) { + $label = ''; + if ( '_blank' === $attributes['linkTarget'] ) { + // translators: %s is the Author name. + $label = 'aria-label="' . sprintf( esc_attr__( '( %s author archive, opens in a new tab)' ), $author_name ) . '"'; + } + // translators: %1$s: Author archive link. %2$s: Link target. %3$s Aria label. %4$s Avatar image. + $avatar_block = sprintf( '%4$s', get_author_posts_url( $author_id ), esc_attr( $attributes['linkTarget'] ), $label, $avatar_block ); + } + return sprintf( '
%2s
', $wrapper_attributes, $avatar_block ); + } + $comment = get_comment( $block->context['commentId'] ); + /* translators: %s is the Comment Author name */ + $alt = sprintf( __( '%s Avatar' ), $comment->comment_author ); + if ( ! $comment ) { + return ''; + } + $avatar_block = get_avatar( + $comment, + $size, + '', + $alt, + array( + 'extra_attr' => isset( $image_styles ) ? sprintf( ' style="%s"', safecss_filter_attr( implode( ' ', $image_styles ) ) ) : '', + 'class' => "wp-block-avatar__image $image_classes", + ) + ); + if ( isset( $attributes['isLink'] ) && $attributes['isLink'] && isset( $comment->comment_author_url ) && '' !== $comment->comment_author_url ) { + $label = ''; + if ( '_blank' === $attributes['linkTarget'] ) { + // translators: %s is the Comment Author name. + $label = 'aria-label="' . sprintf( esc_attr__( '( %s website link, opens in a new tab)' ), $comment->comment_author ) . '"'; + } + // translators: %1$s: Comment Author website link. %2$s: Link target. %3$s Aria label. %4$s Avatar image. + $avatar_block = sprintf( '%4$s', $comment->comment_author_url, esc_attr( $attributes['linkTarget'] ), $label, $avatar_block ); + } + return sprintf( '
%2s
', $wrapper_attributes, $avatar_block ); +} + +/** + * Registers the `core/avatar` block on the server. + */ +function register_block_core_avatar() { + register_block_type_from_metadata( + __DIR__ . '/avatar', + array( + 'render_callback' => 'render_block_core_avatar', + ) + ); +} +add_action( 'init', 'register_block_core_avatar' ); diff --git a/packages/block-library/src/avatar/user-control.js b/packages/block-library/src/avatar/user-control.js new file mode 100644 index 00000000000000..49370b39716eff --- /dev/null +++ b/packages/block-library/src/avatar/user-control.js @@ -0,0 +1,56 @@ +/** + * WordPress dependencies + */ +import { __ } from '@wordpress/i18n'; +import { ComboboxControl } from '@wordpress/components'; +import { useSelect } from '@wordpress/data'; +import { store as coreStore } from '@wordpress/core-data'; +import { useState } from '@wordpress/element'; + +const AUTHORS_QUERY = { + who: 'authors', + per_page: -1, + _fields: 'id,name', + context: 'view', +}; + +function UserControl( { value, onChange } ) { + const [ filteredAuthorsList, setFilteredAuthorsList ] = useState(); + const authorsList = useSelect( ( select ) => { + const { getUsers } = select( coreStore ); + return getUsers( AUTHORS_QUERY ); + }, [] ); + if ( ! authorsList ) { + return null; + } + + const options = authorsList.map( ( author ) => { + return { + label: author.name, + value: author.id, + }; + } ); + + return ( + + setFilteredAuthorsList( + options.filter( ( option ) => + option.label + .toLowerCase() + .startsWith( inputValue.toLowerCase() ) + ) + ) + } + /> + ); +} + +export default UserControl; diff --git a/packages/block-library/src/comment-author-avatar/block.json b/packages/block-library/src/comment-author-avatar/block.json index a255d1f7075a32..3177a812cb825c 100644 --- a/packages/block-library/src/comment-author-avatar/block.json +++ b/packages/block-library/src/comment-author-avatar/block.json @@ -20,6 +20,7 @@ "usesContext": [ "commentId" ], "supports": { "html": false, + "inserter": false, "__experimentalBorder": { "radius": true, "width": true, diff --git a/packages/block-library/src/comment-template/edit.js b/packages/block-library/src/comment-template/edit.js index ef06d38d9b99f1..ec26d965354fe7 100644 --- a/packages/block-library/src/comment-template/edit.js +++ b/packages/block-library/src/comment-template/edit.js @@ -20,7 +20,7 @@ import { store as coreStore } from '@wordpress/core-data'; import { useCommentQueryArgs, useCommentTree } from './hooks'; const TEMPLATE = [ - [ 'core/comment-author-avatar' ], + [ 'core/avatar' ], [ 'core/comment-author-name' ], [ 'core/comment-date' ], [ 'core/comment-content' ], diff --git a/packages/block-library/src/editor.scss b/packages/block-library/src/editor.scss index 10a098bb2492d6..59c1f993a51e1c 100644 --- a/packages/block-library/src/editor.scss +++ b/packages/block-library/src/editor.scss @@ -1,5 +1,6 @@ @import "./archives/editor.scss"; @import "./audio/editor.scss"; +@import "./avatar/editor.scss"; @import "./block/editor.scss"; @import "./button/editor.scss"; @import "./buttons/editor.scss"; diff --git a/packages/block-library/src/index.js b/packages/block-library/src/index.js index 54bdde6c36a69c..321a0505eaf52c 100644 --- a/packages/block-library/src/index.js +++ b/packages/block-library/src/index.js @@ -13,6 +13,7 @@ import { * Internal dependencies */ import * as archives from './archives'; +import * as avatar from './avatar'; import * as audio from './audio'; import * as button from './button'; import * as buttons from './buttons'; @@ -251,10 +252,10 @@ export const __experimentalRegisterExperimentalCoreBlocks = process.env ? ( { enableFSEBlocks } = {} ) => { [ // Experimental blocks. + avatar, homeLink, postAuthorName, queryNoResults, - // Full Site Editing blocks. ...( enableFSEBlocks ? [ diff --git a/test/integration/fixtures/blocks/core__avatar.html b/test/integration/fixtures/blocks/core__avatar.html new file mode 100644 index 00000000000000..725d4e80341455 --- /dev/null +++ b/test/integration/fixtures/blocks/core__avatar.html @@ -0,0 +1 @@ + diff --git a/test/integration/fixtures/blocks/core__avatar.json b/test/integration/fixtures/blocks/core__avatar.json new file mode 100644 index 00000000000000..6d21f8bebfc7b0 --- /dev/null +++ b/test/integration/fixtures/blocks/core__avatar.json @@ -0,0 +1,29 @@ +[ + { + "name": "core/avatar", + "isValid": true, + "attributes": { + "userId": 4, + "size": 85, + "isLink": true, + "linkTarget": "_self", + "align": "right", + "borderColor": "vivid-red", + "style": { + "spacing": { + "margin": { + "bottom": "40px" + } + }, + "border": { + "radius": "47px", + "width": "3px" + }, + "color": { + "duotone": [ "#000000", "#ffe2c7" ] + } + } + }, + "innerBlocks": [] + } +] diff --git a/test/integration/fixtures/blocks/core__avatar.parsed.json b/test/integration/fixtures/blocks/core__avatar.parsed.json new file mode 100644 index 00000000000000..1f695d74659d73 --- /dev/null +++ b/test/integration/fixtures/blocks/core__avatar.parsed.json @@ -0,0 +1,29 @@ +[ + { + "blockName": "core/avatar", + "attrs": { + "userId": 4, + "size": 85, + "isLink": true, + "align": "right", + "style": { + "spacing": { + "margin": { + "bottom": "40px" + } + }, + "border": { + "radius": "47px", + "width": "3px" + }, + "color": { + "duotone": [ "#000000", "#ffe2c7" ] + } + }, + "borderColor": "vivid-red" + }, + "innerBlocks": [], + "innerHTML": "", + "innerContent": [] + } +] diff --git a/test/integration/fixtures/blocks/core__avatar.serialized.html b/test/integration/fixtures/blocks/core__avatar.serialized.html new file mode 100644 index 00000000000000..6965019bf96dbe --- /dev/null +++ b/test/integration/fixtures/blocks/core__avatar.serialized.html @@ -0,0 +1 @@ +