From 1db82436a3147d92a4e0ffb3855875a5e3a14c0f Mon Sep 17 00:00:00 2001 From: Adam Silverstein Date: Thu, 19 Nov 2020 01:27:33 -0700 Subject: [PATCH] Fix the Post author selector for contributors (#26554) Co-authored-by: Riad Benguella --- .../developers/data/data-core.md | 1 + packages/core-data/README.md | 1 + packages/core-data/src/resolvers.js | 26 +++++++++++++++---- packages/core-data/src/selectors.js | 25 ++++++++++++++---- .../src/components/post-author/check.js | 4 +-- .../src/components/post-author/index.js | 14 +++++----- 6 files changed, 53 insertions(+), 18 deletions(-) diff --git a/docs/designers-developers/developers/data/data-core.md b/docs/designers-developers/developers/data/data-core.md index 6484e914f2eef..37c4ad7d5f858 100644 --- a/docs/designers-developers/developers/data/data-core.md +++ b/docs/designers-developers/developers/data/data-core.md @@ -34,6 +34,7 @@ Returns all available authors. _Parameters_ - _state_ `Object`: Data state. +- _query_ `(Object|undefined)`: Optional object of query parameters to include with request. _Returns_ diff --git a/packages/core-data/README.md b/packages/core-data/README.md index fdb7e87043ac2..bc887ad571c7c 100644 --- a/packages/core-data/README.md +++ b/packages/core-data/README.md @@ -271,6 +271,7 @@ Returns all available authors. _Parameters_ - _state_ `Object`: Data state. +- _query_ `(Object|undefined)`: Optional object of query parameters to include with request. _Returns_ diff --git a/packages/core-data/src/resolvers.js b/packages/core-data/src/resolvers.js index b9eca1bee7e86..9a1db4022b558 100644 --- a/packages/core-data/src/resolvers.js +++ b/packages/core-data/src/resolvers.js @@ -33,12 +33,28 @@ import { /** * Requests authors from the REST API. + * + * @param {Object|undefined} query Optional object of query parameters to + * include with request. */ -export function* getAuthors() { - const users = yield apiFetch( { - path: '/wp/v2/users/?who=authors&per_page=-1', - } ); - yield receiveUserQuery( 'authors', users ); +export function* getAuthors( query ) { + const path = addQueryArgs( + '/wp/v2/users/?who=authors&per_page=100', + query + ); + const users = yield apiFetch( { path } ); + yield receiveUserQuery( path, users ); +} + +/** + * Temporary approach to resolving editor access to author queries. + * + * @param {number} id The author id. + */ +export function* __unstableGetAuthor( id ) { + const path = `/wp/v2/users?who=authors&include=${ id }`; + const users = yield apiFetch( { path } ); + yield receiveUserQuery( 'author', users ); } /** diff --git a/packages/core-data/src/selectors.js b/packages/core-data/src/selectors.js index 42f442809c952..d00356fa3483e 100644 --- a/packages/core-data/src/selectors.js +++ b/packages/core-data/src/selectors.js @@ -9,6 +9,7 @@ import { set, map, find, get, filter, compact, defaultTo } from 'lodash'; */ import { createRegistrySelector } from '@wordpress/data'; import deprecated from '@wordpress/deprecated'; +import { addQueryArgs } from '@wordpress/url'; /** * Internal dependencies @@ -48,18 +49,32 @@ export const isRequestingEmbedPreview = createRegistrySelector( } ); +/** + * Returns all available authors. + * + * @param {Object} state Data state. + * @param {Object|undefined} query Optional object of query parameters to + * include with request. + * @return {Array} Authors list. + */ +export function getAuthors( state, query ) { + const path = addQueryArgs( + '/wp/v2/users/?who=authors&per_page=100', + query + ); + return getUserQueryResults( state, path ); +} + /** * Returns all available authors. * * @param {Object} state Data state. + * @param {number} id The author id. * * @return {Array} Authors list. */ -export function getAuthors( state ) { - deprecated( "select( 'core' ).getAuthors()", { - alternative: "select( 'core' ).getUsers({ who: 'authors' })", - } ); - return getUserQueryResults( state, 'authors' ); +export function __unstableGetAuthor( state, id ) { + return get( state, [ 'users', 'byId', id ], null ); } /** diff --git a/packages/editor/src/components/post-author/check.js b/packages/editor/src/components/post-author/check.js index 8ee69ff4826c9..1826da16a76ef 100644 --- a/packages/editor/src/components/post-author/check.js +++ b/packages/editor/src/components/post-author/check.js @@ -19,7 +19,7 @@ export function PostAuthorCheck( { authors, children, } ) { - if ( ! hasAssignAuthorAction || ! authors || authors.length < 2 ) { + if ( ! hasAssignAuthorAction || ! authors || 1 >= authors.length ) { return null; } @@ -40,7 +40,7 @@ export default compose( [ false ), postType: select( 'core/editor' ).getCurrentPostType(), - authors: select( 'core' ).getUsers( { who: 'authors' } ), + authors: select( 'core' ).getAuthors(), }; } ), withInstanceId, diff --git a/packages/editor/src/components/post-author/index.js b/packages/editor/src/components/post-author/index.js index 889f5dcf0c74e..ea0e8909ab2fd 100644 --- a/packages/editor/src/components/post-author/index.js +++ b/packages/editor/src/components/post-author/index.js @@ -21,18 +21,20 @@ function PostAuthor() { const { authorId, isLoading, authors, postAuthor } = useSelect( ( select ) => { - const { getUser, getUsers, isResolving } = select( 'core' ); + const { __unstableGetAuthor, getAuthors, isResolving } = select( + 'core' + ); const { getEditedPostAttribute } = select( 'core/editor' ); - const author = getUser( getEditedPostAttribute( 'author' ) ); + const author = __unstableGetAuthor( + getEditedPostAttribute( 'author' ) + ); const query = ! fieldValue || '' === fieldValue ? {} : { search: fieldValue }; return { authorId: getEditedPostAttribute( 'author' ), postAuthor: author, - authors: getUsers( { who: 'authors', ...query } ), - isLoading: isResolving( 'core', 'getUsers', [ - { search: fieldValue, who: 'authors' }, - ] ), + authors: getAuthors( query ), + isLoading: isResolving( 'core', 'getAuthors', [ query ] ), }; }, [ fieldValue ]