Git Product home page Git Product logo

Comments (11)

sarukuku avatar sarukuku commented on June 22, 2024

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.

sarukuku avatar sarukuku commented on June 22, 2024

I created to pull request that fixes this. See #11.

from wp-rest-api-cache.

sarukuku avatar sarukuku commented on June 22, 2024

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.

airesvsg avatar airesvsg commented on June 22, 2024

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.

sarukuku avatar sarukuku commented on June 22, 2024

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.

wrux avatar wrux commented on June 22, 2024

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.

sarukuku avatar sarukuku commented on June 22, 2024

@wrux Just don’t use anonymous/inline functions and you should be fine.

from wp-rest-api-cache.

wrux avatar wrux commented on June 22, 2024

This was using the default WP rest API on the latest version

from wp-rest-api-cache.

XedinUnknown avatar XedinUnknown commented on June 22, 2024

Same problem.

from wp-rest-api-cache.

sarukuku avatar sarukuku commented on June 22, 2024

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.

ashishpatel1992 avatar ashishpatel1992 commented on June 22, 2024

I am also having same issues in my error log!

from wp-rest-api-cache.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    πŸ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❀️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.