Comments (11)
I looked into this a bit and it seems that you can't serialize the WP_REST_Response
object. If I change the line 63 to
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 WP_REST_Response
object so I can't really say if this is a viable option as a fix.
from wp-rest-api-cache.
I created to pull request that fixes this. See #11.
from wp-rest-api-cache.
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.
from wp-rest-api-cache.
Hi @sarukuku,
Thanks for your time.
Please, send me more information about your environment like WP version, plugins, PHP version and endpoints.
Thanks
from wp-rest-api-cache.
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.
from wp-rest-api-cache.
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:
[Mon Apr 23 04:32:28.782774 2018] [:error] [pid 19449] [client 115.84.94.242:50308] PHP Fatal error: Uncaught Exception: Serialization of 'Closure' is not allowed in /var/www/site/wordpress/wp-includes/functions.php:440\nStack trace:\n#0 /var/www/site/wordpress/wp-includes/functions.php(440): serialize(Object(WP_REST_Response))\n#1 /var/www/site/wordpress/wp-includes/option.php(463): maybe_serialize(Object(WP_REST_Response))\n#2 /var/www/site/wordpress/wp-includes/option.php(766): add_option('_transient_rest...', Object(WP_REST_Response), '', 'no')\n#3 /var/www/site/wordpress/wp-content/plugins/wp-rest-api-cache-master/class-wp-rest-cache.php(63): set_transient('rest_cache_/wp-...', Object(WP_REST_Response), 604800)\n#4 /var/www/site/wordpress/wp-includes/class-wp-hook.php(286): WP_REST_Cache::pre_dispatch(Object(WP_REST_Response), Object(WP_REST_Server), Object(WP_REST_Request))\n#5 /var/www/site/wordpress/wp-includes/plugin.php(203): WP_Hook->apply_filters(NULL, Array)\n#6 /var/www/site/wordpress/wp-includes/rest-api/class-wp-rest-server.php(817): apply_filters('rest_pre_dispat...', NULL, Object(WP_REST_Se in /var/www/site/wordpress/wp-includes/functions.php on line 440
from wp-rest-api-cache.
@wrux Just donβt use anonymous/inline functions and you should be fine.
from wp-rest-api-cache.
This was using the default WP rest API on the latest version
from wp-rest-api-cache.
Same problem.
from wp-rest-api-cache.
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.
from wp-rest-api-cache.
I am also having same issues in my error log!
from wp-rest-api-cache.
Related Issues (20)
- Clear cache on post update HOT 1
- Cache is not getting Updated after post publish HOT 3
- Cache producing wrong result when caching Custom Post Types with _embed option HOT 3
- Object Cache support missing
- Caching Custom Endpoints HOT 2
- POST requests have cache ! HOT 4
- Setting Content-Length HOT 1
- Request: Add cache cleared callback hook
- Last-Modified or ETag header
- Incompatible with Gutenberg HOT 4
- Core's return true function
- Doesn't work as expected with WooCommerce enabled HOT 2
- cache woocommerce rest api v3 with products updated weekly: how to? HOT 1
- Is this just browser-level caching? HOT 1
- not working with WP REST API V2 HOT 6
- not work for endpoint with _embed HOT 3
- Enabled but no time difference HOT 1
- Clear cache HOT 1
- Embedded Media is not cached properly HOT 19
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
π Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. πππ
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google β€οΈ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from wp-rest-api-cache.