Skip to content

Commit

Permalink
Editor: Display empty option when post author is missing (#64165)
Browse files Browse the repository at this point in the history
Co-authored-by: Mamaduka <[email protected]>
Co-authored-by: t-hamano <[email protected]>
Co-authored-by: jasmussen <[email protected]>
Co-authored-by: mossy2100 <[email protected]>
  • Loading branch information
5 people authored Aug 2, 2024
1 parent a9a0287 commit 9770494
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 4 deletions.
14 changes: 11 additions & 3 deletions packages/editor/src/components/post-author/hook.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
/**
* WordPress dependencies
*/
import { __ } from '@wordpress/i18n';
import { useMemo } from '@wordpress/element';
import { useSelect } from '@wordpress/data';
import { decodeEntities } from '@wordpress/html-entities';
Expand Down Expand Up @@ -46,17 +47,24 @@ export function useAuthorsQuery( search ) {
( { value } ) => postAuthor?.id === value
);

let currentAuthor = [];
if ( foundAuthor < 0 && postAuthor ) {
return [
currentAuthor = [
{
value: postAuthor.id,
label: decodeEntities( postAuthor.name ),
},
...fetchedAuthors,
];
} else if ( foundAuthor < 0 && ! postAuthor ) {
currentAuthor = [
{
value: 0,
label: __( '(No author)' ),
},
];
}

return fetchedAuthors;
return [ ...currentAuthor, ...fetchedAuthors ];
}, [ authors, postAuthor ] );

return { authorId, authorOptions, postAuthor };
Expand Down
4 changes: 3 additions & 1 deletion packages/editor/src/components/post-author/panel.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import { __, sprintf } from '@wordpress/i18n';
import { Button, Dropdown } from '@wordpress/components';
import { useState, useMemo } from '@wordpress/element';
import { decodeEntities } from '@wordpress/html-entities';
import { __experimentalInspectorPopoverHeader as InspectorPopoverHeader } from '@wordpress/block-editor';

/**
Expand All @@ -16,7 +17,8 @@ import { useAuthorsQuery } from './hook';

function PostAuthorToggle( { isOpen, onClick } ) {
const { postAuthor } = useAuthorsQuery();
const authorName = postAuthor?.name || '';
const authorName =
decodeEntities( postAuthor?.name ) || __( '(No author)' );
return (
<Button
size="compact"
Expand Down

0 comments on commit 9770494

Please sign in to comment.