-
Notifications
You must be signed in to change notification settings - Fork 37
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
Uncaught Exception: Serialization of 'Closure' is not allowed #10
Comments
I looked into this a bit and it seems that you can't serialize the set_transient( $key, $result->get_data(), $timeout ); then it works. But I'm not sure if the type returned will always be an instance of |
I created to pull request that fixes this. See #11. |
I did a bit of testing & prototyping related to this issue and the pull request (#11) I submitted. It turned out that it's quite hard to try to get around the closure serialization issue inside the WP_REST_Response. I couldn't find any sane way to do it. I tracked down where the closures were that were failing the serialization and the closures ended up being the validation and sanitation functions attached to the register_rest_route() function. Changing from using closures in those calls to using local named functions circumvents the serialization problem. Using the closures in that way wasn't even my idea but actually a recommended way of doing it while the WP API wasn't yet in the core. See f.ex. the validate_callback example here. I suggest that we add some note about not supporting closures inside the callback functions tied to the register_rest_route() function to the readme of this. Maybe it will save time from people who also have the same problem. What do you think? I'll also drop the PR related to this issue. |
Hi @sarukuku, Thanks |
Sorry about the delay. I'm using the latest WP version and the problem is repeatable with only the wp-rest-api-cache plugin installed and active. I'm using PHP 7 and a sample endpoint that causes the error could look f.ex. like this: add_action( 'rest_api_init', function () {
register_rest_route( 'myplugin/v1', '/author/(?P<id>\d+)', array(
'methods' => 'GET',
'callback' => 'my_awesome_func',
'args' => array(
'id' => array(
'validate_callback' => function($param, $request, $key) {
return is_numeric( $param );
}
),
),
) );
} ); The problem is that with PHP you can't serialize anonymous functions without tricks and the plugin relies on serialization on a level where an endpoint written like the one above would be serialized. As it includes an anonymous function it will throw an error when the plugin tries to serialize it for caching. The way around this is to give the function a name and only reference it by name from the endpoint. It's an easy fix but I think it should be stated in the readme and handled gracefully in inside the plugin. |
It looks like this is over a year old but did this get updated? I just tested the extension and got this error. I just disabled all plugins except REST API cache and I got this error:
|
@wrux Just don’t use anonymous/inline functions and you should be fine. |
This was using the default WP rest API on the latest version |
Same problem. |
Hans someone looked if the OTB WP REST API endpoints use inline functions in validation etc.? If this is the case this plugin will not work with them. At least that's my experience. |
I am also having same issues in my error log! |
I'm getting this kind of error just by installing & enabling the plugin:
Any idea what's wrong?
The text was updated successfully, but these errors were encountered: