WordPress / gutenberg

The Block Editor project for WordPress and beyond. Plugin is available from the official repository.
https://wordpress.org/gutenberg/
Other
10.16k stars 4.05k forks source link

Replace deprecated `who` argument from user query #39986

Open grappler opened 2 years ago

grappler commented 2 years ago

What problem does this address?

In WordPress 5.9 the who argument has been deprecated. Trac Ticket

Gutenberg is still using the who argument and recommending it to be used. https://github.com/WordPress/gutenberg/blob/08cf4a96d1f14fcdf4c55886ba237fb294f9c465/packages/editor/src/components/post-author/constants.js#L2 https://github.com/WordPress/gutenberg/blob/23f11a18726bf69717595451ab3250cb67b426f6/packages/block-library/src/avatar/user-control.js#L11 https://github.com/WordPress/gutenberg/blob/08cf4a96d1f14fcdf4c55886ba237fb294f9c465/packages/block-library/src/query/edit/inspector-controls/author-control.js#L15 https://github.com/WordPress/gutenberg/blob/760815a731805fa05f6cbb234660b254ad1ca6c9/packages/block-library/src/post-author/edit.js#L43 https://github.com/WordPress/gutenberg/blob/711ea0eda4e3b4d28b9e0d70723ec04622dec278/packages/core-data/README.md?plain=1#L26

What is your proposed solution?

My suggestion would be to use the capability argument. The following code snippet fetches the edit_post capability for the current post type. This code is only compatible with WordPress 5.9. Either Gutenberg will need to increase the minimum supported version or include a check and a fallback with who: 'authors'.

const AUTHORS_QUERY = {
    per_page: 50,
    _fields: 'id,name',
    context: 'edit', // Allows non-admins to perform requests.
};
const currentPostType = wp.data.select('core/editor').getCurrentPostType();
const editPostCap = wp.data.select('core').getPostType( currentPostType ).capabilities.edit_post;
wp.data.select('core').getUsers( { ...AUTHORS_QUERY, capability: [ editPostCap ] } );

So that everyone does not have to figure out the edit_post capability, it might be best to create a helper function. Though in WordPress 5.9 the getAuthors function was deprecated https://github.com/WordPress/gutenberg/pull/33725

Related Issues: https://github.com/WordPress/gutenberg/issues/39035 & https://github.com/WordPress/gutenberg/issues/17364

ocean90 commented 2 years ago

Noting that the current request already generates a X-WP-DeprecatedParam header for this:

X-WP-DeprecatedParam: WP_User_Query (since 5.9.0; <code>who</code> is deprecated. Use <code>capability</code> instead.)

@TimothyBJacobs @spacedmonkey @youknowriad Do you have some ideas how we can detect whether who or capability should be used?

TimothyBJacobs commented 2 years ago

I'm not sure what the progress is on the client having knowledge of the REST API schemas, cc @youknowriad. But the best way would be to look at the schema for the endpoint and check for the presence of the capability property in the GET args.

grappler commented 2 years ago

The only reason that I can see why we would need to check for the support is to be able to run the Gutenberg plugin on WordPress 5.8. Depending on the time it takes for a patch, WordPress 6.0 will have been released.

It might make more sense to increase the minimum required WP version. https://github.com/WordPress/gutenberg/blob/956a836ce8fcdd61b186cf1c2d1047b48126898d/gutenberg.php#L6

ocean90 commented 1 year ago

As of #41306 the plugin now requires 5.9 so I guess we can switch the query to use capability now.