Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
Post Author Block: Add useDefaultAvatar fallback to display avatar pl…
…aceholder outside postId context
  • Loading branch information
theaminuli committed Sep 19, 2025
commit 95cd490025f4cb44ad763e4363dc91b10766a3df
15 changes: 5 additions & 10 deletions packages/block-library/src/avatar/hooks.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
/**
* 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';

/**
* Internal dependencies
*/
import { useDefaultAvatar } from '../utils/hooks';

function getAvatarSizes( sizes ) {
const minSize = sizes ? sizes[ 0 ] : 24;
const maxSize = sizes ? sizes[ sizes.length - 1 ] : 96;
Expand All @@ -16,15 +20,6 @@ function getAvatarSizes( sizes ) {
};
}

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',
Expand Down
21 changes: 14 additions & 7 deletions packages/block-library/src/post-author/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,19 @@ import {
__experimentalToolsPanelItem as ToolsPanelItem,
} from '@wordpress/components';
import { debounce } from '@wordpress/compose';
import { store as coreStore } from '@wordpress/core-data';
import { useDispatch, useSelect } from '@wordpress/data';
import { useMemo, useState } from '@wordpress/element';
import { useSelect, useDispatch } from '@wordpress/data';
import { __, sprintf } from '@wordpress/i18n';
import { decodeEntities } from '@wordpress/html-entities';
import { store as coreStore } from '@wordpress/core-data';
import { __, sprintf } from '@wordpress/i18n';

/**
* Internal dependencies
*/
import { useToolsPanelDropdownMenuProps } from '../utils/hooks';
import {
useDefaultAvatar,
useToolsPanelDropdownMenuProps,
} from '../utils/hooks';

const AUTHORS_QUERY = {
who: 'authors',
Expand Down Expand Up @@ -115,6 +118,7 @@ function PostAuthorEdit( {
} ) {
const isDescendentOfQueryLoop = Number.isFinite( queryId );
const dropdownMenuProps = useToolsPanelDropdownMenuProps();
const defaultAvatar = useDefaultAvatar();

const { authorDetails, canAssignAuthor, supportsAuthor } = useSelect(
( select ) => {
Expand Down Expand Up @@ -323,12 +327,15 @@ function PostAuthorEdit( {
</BlockControls>

<div { ...blockProps }>
{ showAvatar && authorDetails?.avatar_urls && (
{ showAvatar && (
<div className="wp-block-post-author__avatar">
<img
width={ avatarSize }
src={ authorDetails.avatar_urls[ avatarSize ] }
alt={ authorDetails.name }
src={
authorDetails?.avatar_urls?.[ avatarSize ] ||
defaultAvatar
}
alt={ authorDetails?.name || __( 'Post Author' ) }
/>
</div>
) }
Expand Down
9 changes: 9 additions & 0 deletions packages/block-library/src/utils/hooks.js
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,15 @@ export function useUploadMediaFromBlobURL( args = {} ) {
}, [ getSettings ] );
}

export function useDefaultAvatar() {
const { avatarURL: defaultAvatarUrl } = useSelect( ( select ) => {
const { getSettings } = select( blockEditorStore );
const { __experimentalDiscussionSettings } = getSettings();
return __experimentalDiscussionSettings;
} );
return defaultAvatarUrl;
}

export function useToolsPanelDropdownMenuProps() {
const isMobile = useViewportMatch( 'medium', '<' );
return ! isMobile
Expand Down
Loading