Skip to content
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

Add user pattern categories to the pattern categories API response #5621

Conversation

glendaviesnz
Copy link

@glendaviesnz glendaviesnz commented Nov 6, 2023

Currently just a draft PR to get some feedback on adding user pattern categories into the block-patterns/categories endpoint to check that we are doing this in a standard and backward compatible way.

This PR adds the user pattern categories only if the source query param is specified and includes user.

Also added is a new id field as this is needed to map the user categories back to the taxonomy terms in certain parts of the UI. This field is only returned if id is explicitly request in the _fields param.

So to see the added data the endpoint has to be called with params _fields: 'name,label,description, id' and source: [ 'core', 'user' ],

Trac ticket: https://core.trac.wordpress.org/ticket/59903


This Pull Request is for code review only. Please keep all other discussion in the Trac ticket. Do not merge this Pull Request. See GitHub Pull Requests for Code Review in the Core Handbook for more details.

@glendaviesnz
Copy link
Author

@TimothyBJacobs do you have any thoughts about whether this is an appropriate/backwards compat way to add additional data and fields to the block-patterns/categories endpoint?

@glendaviesnz glendaviesnz changed the title [WIP] Add user pattern categories to the pattern categories API response Add user pattern categories to the pattern categories API response Nov 13, 2023
@glendaviesnz
Copy link
Author

There will be a bit of work in adding additional unit tests for this as the taxonomy terms will need to be mocked, so I will wait until this approach is approved and then add additional tests to cover it.

Comment on lines +85 to +88
$valid_query_args = array(
'source' => true,
);
$query_args = array_intersect_key( $request->get_params(), $valid_query_args );
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What's the purpose of this code?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This approach was copied from here, and I assumed it was a way of ensuring that any non-valid query args where dropped 🤔

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right, but in that case it's because we are forwarding those query args wholesale to another API. Here, we are just consuming a single query parameter. So we can just do $request['source'].

Comment on lines +149 to +151
if ( rest_is_field_included( 'id', $fields ) && isset( $item['id'] ) ) {
$data['id'] = $item['id'];
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Generally, introducing a new field would not be seen as a BC break. Is there a specific issue we are trying to work around here?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok, good to know, I thought additional fields would be seen as breaking change, I can remove this if not.

'description' => __( 'Limit result set to specific sources, `core` and/or `user`' ),
'type' => 'array',
'items' => array(
'type' => 'string',
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can specify the allowed sources using the enum keyword.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok, will add that.

* @return array Collection parameters.
*/
public function get_collection_params() {
$query_params = parent::get_collection_params();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should unset the collection parameters we don't support, search, page and per_page. The default value for the context should also be set to view.

*
* @return array Collection parameters.
*/
public function get_collection_params() {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The result of this method should be called in args in register_rest_route.

@TimothyBJacobs
Copy link
Member

It seems a bit less than ideal that we are now making a database call, but this endpoint isn't paginated. What would it look like to support pagination?

It's also unfortunate that this API shape does not match the terms controller which it is now returning data for, the name property is backward.

@TimothyBJacobs
Copy link
Member

Have you considered what this would look like as a search controller type via WP_REST_Search_Handler with a subtype for user and core?

That would solve the different shape issues and is the more canonical way of handling similar but disparate data types in a single REST API request.

@glendaviesnz
Copy link
Author

Thanks for the pointer to WP_REST_Search_Handler @TimothyBJacobs, I will take a look at that and see if that is a better option.

@glendaviesnz
Copy link
Author

Going to close this for now - not sure if I will get it sorted before 6.5

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants