From d6c118a7603b7c5c00b9c25aff53a5cf74935815 Mon Sep 17 00:00:00 2001 From: Peter Hartree Date: Thu, 17 Jul 2014 13:35:25 +0000 Subject: [PATCH 1/2] Add `previous_post_title` and `next_post_title` properties to the `get_post` response object --- controllers/core.php | 2 ++ 1 file changed, 2 insertions(+) diff --git a/controllers/core.php b/controllers/core.php index a3dda64..12003e4 100644 --- a/controllers/core.php +++ b/controllers/core.php @@ -72,9 +72,11 @@ public function get_post() { ); if ($previous) { $response['previous_url'] = get_permalink($previous->ID); + $response['previous_post_title'] = $previous->post_title; } if ($next) { $response['next_url'] = get_permalink($next->ID); + $response['next_post_title'] = $next->post_title; } return $response; } else { From 03485da44f8444ebc3bc007ba97cc2305f68c75f Mon Sep 17 00:00:00 2001 From: Peter Hartree Date: Sat, 26 Jul 2014 19:20:53 +0000 Subject: [PATCH 2/2] Add simple caching mechanism Use the WordPress Transients API to cache API requests for non-logged in users. This will significantly speed up API requests and reduce server load. You might want to tweak the cache time on line 84 - for high traffic sites, a cache duration of just a few minutes will be enough to achieve significant performance gains. --- singletons/api.php | 35 +++++++++++++++++++++++++++++++++-- 1 file changed, 33 insertions(+), 2 deletions(-) diff --git a/singletons/api.php b/singletons/api.php index baeb506..e555d13 100644 --- a/singletons/api.php +++ b/singletons/api.php @@ -50,8 +50,39 @@ function template_redirect() { $this->error('Not found'); } - // Run the method - $result = $this->controller->$method(); + // Build transient slug using method (and query params if set) + $transient_slug = 'json-api-'; + $transient_slug .= $method; + + $url = parse_url($_SERVER['REQUEST_URI']); + + if($url['query'] !== null): + $args = wp_parse_args($url['query']); + $transient_slug .= '-' . implode('-', array_keys($args)); + $transient_slug .= '-' . implode('-', $args); + endif; + + // Transient slug must be less than 45 characters (http://codex.wordpress.org/Transients_API) + if(strlen($transient_slug) > 40): + $transient_slug = 'json-api-' . md5($transient_slug); + endif; + + if ( is_user_logged_in() ): + // Never show cached content to a logged-in user + $cached = false; + else: + // Check for cached value + $cached = get_transient($transient_slug); + endif; + + if($cached !== false): + // Use the cached result + $result = $cached; + else: + // Run the method and cache result for 24 hours + $result = $this->controller->$method(); + set_transient( $transient_slug, $result, 24 * 3600 ); + endif; // Handle the result $this->response->respond($result);