-
Notifications
You must be signed in to change notification settings - Fork 4.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Switch post-author
component to use /wp/v2/users/?who=authors
#6515
Merged
Merged
Changes from 4 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
f112000
Switch `post-author` component to use `/wp/v2/users/?who=authors`
danielbachhuber 80d0500
Ensure state is always an array, properly
danielbachhuber e2c7f4e
Update tests for 80d0500b2cca564303d5c14d891572420c0d4f1a
danielbachhuber ee7ac14
Remove unused variable
danielbachhuber de5b7f0
Remove unnecessary prop pass-through
danielbachhuber 97a7a35
Ensure no duplicate authors are listed
danielbachhuber 1e3205b
Use a "users" reducer combined with a "queries" sub state to map auth…
youknowriad File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,7 @@ | ||
/** | ||
* External dependencies | ||
*/ | ||
import { filter, get } from 'lodash'; | ||
import { get } from 'lodash'; | ||
|
||
/** | ||
* WordPress dependencies | ||
|
@@ -15,8 +15,7 @@ import { withSelect } from '@wordpress/data'; | |
*/ | ||
import PostTypeSupportCheck from '../post-type-support-check'; | ||
|
||
export function PostAuthorCheck( { user, users, children } ) { | ||
const authors = filter( users.data, ( { capabilities } ) => get( capabilities, [ 'level_1' ], false ) ); | ||
export function PostAuthorCheck( { user, authors, children } ) { | ||
const userCanPublishPosts = get( user.data, [ 'post_type_capabilities', 'publish_posts' ], false ); | ||
|
||
if ( ! userCanPublishPosts || authors.length < 2 ) { | ||
|
@@ -30,13 +29,14 @@ export default compose( [ | |
withSelect( ( select ) => { | ||
return { | ||
postType: select( 'core/editor' ).getCurrentPostType(), | ||
authors: select( 'core' ).getAuthors(), | ||
}; | ||
} ), | ||
withAPIData( ( props ) => { | ||
const { postType } = props; | ||
const { postType, authors } = props; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Passing the props down is useless, it's done automatically in There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
|
||
return { | ||
users: '/wp/v2/users?context=edit&per_page=100', | ||
authors, | ||
user: `/wp/v2/users/me?post_type=${ postType }&context=edit`, | ||
}; | ||
} ), | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I believe this would create duplicate authors if we trigger the request twice (it's not happening at the moment though :)). Maybe we can store them by Id :).
Also, why do we have an
authors
reducer instead of ausers
reducer. I believe we should have ausers
reducer and use thegetAuthors
selector to filter the data according to the user's object. This way we won't be duplicating data when we introduce theusers
fetching.This looks more complex at first sight, but it's the way to go. Just imagine how you'd update the authors list if you create a new author from the JS side for instance? If it's all handled in a generic way in a
users
reducer, the selector will update its changes without any specific behavior to trigger the request again.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed in 97a7a35
Because of #6361. With this approach, we keep the definition of "which user is an author" server-side.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Well! Can I suggest that it's not a good compromise going forward. This means that our data module would consider
authors
as a separate entity thanusers
. While this may be ok shortterm, I don't think it's good because we have to think of thecore-data
module as a generic module to handle client data in any WP Admin context. It means another page could create users, which could also be "authors" but since this knowledge is only available server-side, the UI won't update once those users are created.How can we move this knowledge client-side? what property do we need in the
user
's object to say that it's an author or not?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Would you like me to move it to editor data then?
We'd need to run a second query client-side for every user collection request. First, a
WP_User_Query( [ 'who' => 'authors' ] )
to get all authors, and the secondWP_User_Query
for the full query. When preparing the results for return, we'd inspect whether the user is a part of the first query and include an author attribute correspondingly.My preference is to keep this current implementation.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In fact, this current implementation can be progressively enhanced to a better implementation in the future, should one arise.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So we discussed this with @aduth and I believe there's a compromise to find here if we can't move the "author" knowledge to the client.
we can track the
?who=authors
response as a set of IDs pointing to the canonical users state.Also this relates to #6395 helper since
?who=authors
can be considered just onequery
on theuser
model.I'm fine using a compromise for now and generalizing this as a query in #6395 or similar.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you clarify what changes you'd like me to make to this PR? Or, open a PR against this PR with your suggested changes?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sure, give me some minutes
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Here #6522 It's not tested etc... just to get the idea.