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
How to differentiate between REST and POST requests of Gutenberg inside "save_post"? #12903
Comments
Or may be if it's possible to add some parameter to the REST request so as to use it in |
What are you doing on the If you only want to do something on REST requests, you could use the |
@TimothyBJacobs thank you for your contribution to WP REST API - Let me explain:
The main problem here is that how can I differentiate between 2(i) and 4. |
@manzoorwanijk I had the same question for WordPress to Buffer and WordPress to Hootsuite. Here's the current solution I came up with, abstracted as a class that might help: |
@n7studios, thank you for that gist. I already have such a workaround but not something to be relied upon. Although it should work in most cases but there are certain issues with your solution:
Lets see if there is a better solution |
@manzoorwanijk Agreed. I notice Posts through the Classic Editor define an Wondering if there's some way to use this to 'detect' whether the Post is truly Gutenberg or not? Also worth inspecting the request object when Gutenberg + non-Gutenberg requests are made through the REST API, to see if there are any differences that distinguish whether the Post was created in Gutenberg or not. |
I'm afraid I don't know enough to answer your questions directly, but I did want to say that I noticed a recent PR at #13718 which attempts to fix a preview race condition reported at #12617 and the discussion about the two types of requests you mentioned at the start ( I also know that Jetpack has some social media interactions and that plugin is open source and wondered if it would be a good suggestion to say to check out that code to see if they have done anything similar that could help you figure out your case by chance? |
As mentioned by @designsimply, it's worth pointing out the PR at #13718 which proposes to switch the order of the requests (there's no guarantees that will be merged.) That may cause an issue for code that relies on the existing order, though it might also reduce some of the complexity since I think you'd be able to switch to just relying on the REST hook. Also worth mentioning the second request only occurs when meta boxes are active. I'll make sure to make the point that plugins might have some dependencies on the save order in #13718. |
In encountering this issue in the wild, I believe this to be a fairly significant problem. Recapping the important bits:
If a plugin needs to run some process after a post is updated, for instance sync the "final" post data to some external resource, there's no longer a reliable way to do that. It used to be that one could pretty heavily rely on |
I believe there's a solution proposed here https://gist.github.com/n7studios/56fd05f19f5da26f19f6da0ccb57b144 And Gutenberg will always use two requests if you have meta boxes but there's no other way to support meta boxes otherwise. The meta boxes API is not deterministic enough to be able to run its saving hooks on the REST API call. |
This should not be closed. The proposed solution is not a sufficient workaround. Please review my comment above, this is a problem without a viable option as-is. At the most basic level, gutenberg should include a flag in the first request that there is going to be a second request. |
is it possible to check for existence of metaboxes on the server to figure that out? |
That would be a lot of work and I don’t think it would be reliable. Gutenberg knows if it would be sending a follow up request or not, right? Why not announce that intent? |
Let's reopen to reconsider that. |
Running into this as well. (I could copy a bunch of code over to Gutenberg and basically maintain a PHP and a JS codebase for the classic and the block editor, but that would, well, lead to other issues. So I'd much rather be able to detect Gutenberg requests reliably. Could we, in addition to To see if a request came from the admin or an entirely different client? (Even if they can be spoofed and whatnot, it'd still be better than assuming any REST request is a Gutenberg request, no?) Seems to work (unless of course some hosts unset refer(r)er URLs?):
Oh, I'm using this inside a |
Was also wondering if, in case the referrer is deemed unreliable, we couldn't check against the |
Case anyone's interested, still, I've been using this bit of code to detect whether a request is coming from Gutenberg (but not, e.g., a mobile app, for which
In combination with the presence of |
As we know that Gutenberg sends two requests when we hit the Publish/Update button:
post.php
to save/submit the metaboxessave_post
is triggered by both the requests. How do I know inside thesave_post
callback that the post is created/updated via Gutenberg, preferably in the second request? This is to avoid the double effect of the hook.I know some would suggest to add some meta value in the first request and use it to short-circuit the second callback or use a transient in a similar way. Filling up the meta table with useless data is not a good idea and transients don't look to be a better solution IMHO.
Is there a consistent query argument or a header that can be relied upon? I couldn't find any such thing in the two requests.
The text was updated successfully, but these errors were encountered: