-
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
Fix retrieving autosaves when using a custom rest_namespace #41542
Conversation
When specifying `rest_namespace` in a custom post type, a hardcoded `wp/v2` in `getAutosaves` results in a 404
Thanks for the PR @tomjn! The test failures do look related. I'm guessing that the post types response is mocked somewhere and that mock is missing a |
Good catch @tomjn - your code probably just needs a fallback if namespace is undefined to get tests passing. I did notice we have some other non-test instances of
|
I'll be honest, I wrote this in the github UI then took a flight so not surprised it failed a check or two, so this is my first chance to respond. I do remember seeing the namespace referenced like this elsewhere: const namespace = postType?.rest_namespace ?? 'wp/v2'; |
@adamsilverstein @TimothyBJacobs I've addressed the linter issue, took me a bit more time than expected to get the dependencies/npm setup locally, I'm unsure on the best way to implement const type = await resolveSelect.getPostType( postType );
const base = type.rest_base;
const namespace = type?.rest_namespace ?? 'wp/v2';
const autosaves = await apiFetch( {
path: `/${ namespace }/${ base }/${ postId }/autosaves?context=edit`,
} ); I expect there'll be pushback over the naming of |
Updating the test mock data and adding new tests is beyond my bandwidth/knowledge for the project at the moment though, so I'll need help or someone else to pick it up |
You should be able to assign a default while destructuring. const { rest_namespace: restNamespace = 'wp/v2' } = resolveSelect.getPostType( postType ); |
🥳 thanks @TimothyBJacobs that has CI passing! |
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.
Looks good! Would be nice to add tests to validate and fix any other hard coded namespaces in a future PR.
Nice work @tomjn - approved! I'd love to see a follow up adding the tests and fixing any other hard coded namespace instances. Could you please create an issue to track this (even if you don't have time to work on the PR)? |
Is there a way to disable the autosave feature until this PR is merged? |
@adamsilverstein I no longer have the bandwidth to work on this, I don't have the capacity to push it forward, if you or someone else can create an issue for it I'd appreciate it, this was mostly me trying to help out someone on stack exchange and doesn't impact me personally. I also see there are now conflicts resulting from formatting changes made elsewhere |
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.
This looks good to go to me!
Related: #41950 |
Hi, @tomjn It looks like static analysis checks are failing. Do you mind rebasing this branch and running the linter on the changed file? |
I don’t have the time to work on this, this will need a new owner to
champion it and push it forward
|
Thanks for the quick reply, @tomjn. Happy to take over to PR and see this merged. |
Co-authored-by: Jonny Harris <[email protected]>
@Mamaduka I think we are good to merge now. |
Thanks for the fix, @spacedmonkey! |
What?
When specifying
rest_namespace
in a custom post type, a hardcodedwp/v2
ingetAutosaves
results in a 404So instead, use the rest namespace from the type, not just the base
Why?
Because bugs are bad and CPT's with
rest_namespace
deserve autosaves too!How?
By not hardcoding
/wp/v2
Testing Instructions
test
rest_namespace
and set it to something such astesting/v99
wp/v2/test?autosaves....
in the browser error console