Skip to content

Instantly share code, notes, and snippets.

@adamsilverstein
Created October 29, 2020 20:19
Show Gist options
  • Save adamsilverstein/c108626a1a9ecbcbcdd3a3521b65371c to your computer and use it in GitHub Desktop.
Save adamsilverstein/c108626a1a9ecbcbcdd3a3521b65371c to your computer and use it in GitHub Desktop.
diff --git a/lib/rest-api.php b/lib/rest-api.php
index 8ad8653091..72944e6d79 100644
--- a/lib/rest-api.php
+++ b/lib/rest-api.php
@@ -52,6 +52,25 @@ function gutenberg_filter_oembed_result( $response, $handler, $request ) {
}
add_filter( 'rest_request_after_callbacks', 'gutenberg_filter_oembed_result', 10, 3 );
+add_action( 'rest_api_init', function() {
+ global $post;
+
+ if ( $post ) {
+ /**
+ * Ensure the post author name is included in the user request.
+ */
+ add_filter( "rest_prepare_{$post->post_type}", function( $response, $user, $request ) {
+ if ( $response->data['author'] ) {
+ $user = get_user_by( 'id', $response->data['author'] );
+ if ( $user ) {
+ $response->data[ 'author_name' ] = $user->data->display_name;
+ }
+ }
+ return $response;
+ }, 20, 3 );
+ }
+} );
+
/**
* Add fields required for site editing to the /themes endpoint.
*
diff --git a/packages/editor/src/components/post-author/index.js b/packages/editor/src/components/post-author/index.js
index 90f3119e19..854bda7794 100644
--- a/packages/editor/src/components/post-author/index.js
+++ b/packages/editor/src/components/post-author/index.js
@@ -19,22 +19,20 @@ import PostAuthorCheck from './check';
function PostAuthor() {
const [ fieldValue, setFieldValue ] = useState();
- const { authorId, isLoading, authors, postAuthor } = useSelect(
+ const { authorId, isLoading, authors, authorName } = useSelect(
( select ) => {
- const { __unstableGetAuthor, getAuthors, isResolving } = select(
+ const { getAuthors, isResolving } = select(
'core'
);
const { getEditedPostAttribute } = select( 'core/editor' );
- const author =
- postAuthor ||
- __unstableGetAuthor( getEditedPostAttribute( 'author' ) )[ 0 ];
+ const author = { id: authorId, name: authorName };
const query =
! fieldValue || '' === fieldValue || fieldValue === author.name
? {}
: { search: fieldValue };
return {
authorId: getEditedPostAttribute( 'author' ),
- postAuthor: author,
+ authorName: getEditedPostAttribute( 'author_name' ),
authors: getAuthors( query ),
isLoading: isResolving( 'core', 'getAuthors', [
{ search: fieldValue },
@@ -43,6 +41,7 @@ function PostAuthor() {
},
[ fieldValue ]
);
+ const postAuthor = { id: authorId, name: authorName };
const { editPost } = useDispatch( 'core/editor' );
const authorOptions = useMemo( () => {
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment