-
Notifications
You must be signed in to change notification settings - Fork 4.2k
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
Update REST API to handle slug refs #44975
base: try/nav-block-use-slugs-for-ref
Are you sure you want to change the base?
Update REST API to handle slug refs #44975
Conversation
// Lists a single nav item based on the given id or slug. | ||
register_rest_route( | ||
$this->namespace, | ||
'/' . $this->rest_base . '/((?P<id>[\d]+)|(?P<slug>[\w\-]+))', |
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.
Both id and slug are needed in this request otherwise backwards compatibility is broken.
* See: https://github.com/WordPress/gutenberg/pull/43703. | ||
* | ||
* @param string $id the slug of the Navigation post. | ||
* @return WP_Post|null |
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.
Wrong return type.
Why can you using existing endpoints, like this?
|
I feel like I tried that (it was a while ago now). I will double check that in the main PR just in case before we look to merge this one. |
@getdave Works fine in my testing. https://spacedmonkey.com/wp-json/wp/v2/navigation?slug=navigation-2 The same of the response is different. Any array with a single object over one object. But it works just fine. |
The https://spacedmonkey.com/wp-json/wp/v2/navigation/434 Gutenberg's Core data has:
Conceptually what the block is trying to do is get the individual Navigation post assigned to it by a specific reference. This is different to fetching a collection of Navigation posts based on some query parameters. This is why we went down the route of changing the endpoint to accept slug as the "reference". This also brings it more into line with the way the Template Parts endpoint works. For example - if you want a single template part you pass a I'm diving back through the parent PR again because I"m sure I explored the route you are suggesting in more detail and it was decided against but I've not found the original conversation (yet). |
I think the way the REST API works is correct: the only call to return a single item is when we query by key. In general the key in WP is some kind of ID. The slug is a general query that can return a collection of one item if the slug is unique. With the caveat that I am not deep into the effort here yet, I think the client data library should work with this not the other way around. |
The logic in javascript should be simple. if is int Then just check the response is empty and get the 0 value of response. No need for a new endpoint. |
This needs to be explored in more detail but in principle I agree. @spacedmonkey and I discussed this in DMs on WP Slack and we came to the conclusion that
I'll report back with another PR. |
What?
Sub PR of #42809 containing changes to REST API needed to handle slugs as references for Navigation posts.
Why?
Foundational work for using slugs as references instead of IDs in the Nav block.
How?
Updates the REST API with a new endpoint which handles string-based slugs specifically and then routes that along the same handlers established for int based IDs.
Also adds test coverage for the endpoint.
Testing Instructions
Screenshots or screencast