|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
use Wordlift\Cache\Ttl_Cache; |
|
4
|
|
|
|
|
5
|
|
|
/** |
|
6
|
|
|
* A base abstract class for shortcode REST backend which does |
|
7
|
|
|
* some common tasks. |
|
8
|
|
|
* |
|
9
|
|
|
* @since 3.5.4 |
|
10
|
|
|
*/ |
|
11
|
|
|
abstract class Wordlift_Shortcode_REST { |
|
12
|
|
|
|
|
13
|
|
|
/** |
|
14
|
|
|
* The cache_ttl, set by extending classes. |
|
15
|
|
|
*/ |
|
16
|
|
|
const CACHE_TTL = 86400; // 24 hours |
|
17
|
|
|
|
|
18
|
|
|
public function __construct( $endpoint, $args ) { |
|
19
|
|
|
|
|
20
|
|
|
$scope = $this; |
|
21
|
|
|
$this->endpoint = $endpoint; |
|
|
|
|
|
|
22
|
|
|
$this->args = $args; |
|
|
|
|
|
|
23
|
|
|
|
|
24
|
|
|
// Register rest route with callback |
|
25
|
|
|
add_action( 'rest_api_init', function () use ( $scope ) { |
|
26
|
|
|
register_rest_route( WL_REST_ROUTE_DEFAULT_NAMESPACE, $scope->endpoint, array( |
|
27
|
|
|
'methods' => WP_REST_Server::READABLE, |
|
28
|
|
|
'callback' => array( $scope, 'rest_callback' ), |
|
29
|
|
|
'args' => $scope->args |
|
30
|
|
|
) ); |
|
31
|
|
|
} ); |
|
32
|
|
|
|
|
33
|
|
|
// Optimizations: disable unneeded plugins on this specific REST call. WPSeo is slowing down the responses quite a bit. |
|
34
|
|
View Code Duplication |
add_action( 'plugins_loaded', function () use ( $scope ) { |
|
|
|
|
|
|
35
|
|
|
|
|
36
|
|
|
if ( ! defined( 'REST_REQUEST' ) || ! REST_REQUEST || ! $scope->is_endpoint() ) { |
|
37
|
|
|
return; |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
remove_action( 'plugins_loaded', 'rocket_init' ); |
|
41
|
|
|
remove_action( 'plugins_loaded', 'wpseo_premium_init', 14 ); |
|
42
|
|
|
remove_action( 'plugins_loaded', 'wpseo_init', 14 ); |
|
43
|
|
|
}, 0 ); |
|
44
|
|
|
|
|
45
|
|
View Code Duplication |
add_action( 'init', function () use ( $scope ) { |
|
|
|
|
|
|
46
|
|
|
|
|
47
|
|
|
if ( ! defined( 'REST_REQUEST' ) || ! REST_REQUEST || ! $scope->is_endpoint() ) { |
|
48
|
|
|
return; |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
remove_action( 'init', 'wp_widgets_init', 1 ); |
|
52
|
|
|
remove_action( 'init', 'gglcptch_init' ); |
|
53
|
|
|
}, 0 ); |
|
54
|
|
|
|
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
public abstract function get_data( $request ); |
|
58
|
|
|
|
|
59
|
|
|
public function rest_callback( WP_REST_Request $request ) { |
|
60
|
|
|
|
|
61
|
|
|
// Respond from origin if TTL is 0 |
|
62
|
|
|
if ( static::CACHE_TTL == 0 ) { |
|
63
|
|
|
|
|
64
|
|
|
$data = $this->get_data( $request ); |
|
65
|
|
|
$response = rest_ensure_response( $data ); |
|
66
|
|
|
if ( is_wp_error( $data ) ) { |
|
67
|
|
|
return $response; |
|
68
|
|
|
} |
|
69
|
|
|
$response->header( 'X-WordLift-Cache', 'MISS' ); |
|
70
|
|
|
|
|
71
|
|
|
return $response; |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
// Create the cache key. |
|
75
|
|
|
$cache_key_params = $request->get_params(); |
|
76
|
|
|
unset( $cache_key_params['uniqid'] ); |
|
77
|
|
|
unset( $cache_key_params['rest_route'] ); |
|
78
|
|
|
$cache_key = array( 'request_params' => $cache_key_params ); |
|
79
|
|
|
|
|
80
|
|
|
// Create the TTL cache and try to get the results. |
|
81
|
|
|
$cache = new Ttl_Cache( $this->endpoint, static::CACHE_TTL ); |
|
82
|
|
|
$cache_results = $cache->get( $cache_key ); |
|
83
|
|
|
|
|
84
|
|
|
if ( isset( $cache_results ) ) { |
|
85
|
|
|
|
|
86
|
|
|
$response = rest_ensure_response( $cache_results ); |
|
87
|
|
|
$response->header( 'X-WordLift-Cache', 'HIT' ); |
|
88
|
|
|
|
|
89
|
|
|
return $response; |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
$data = $this->get_data( $request ); |
|
93
|
|
|
$response = rest_ensure_response( $data ); |
|
94
|
|
|
if ( is_wp_error( $data ) ) { |
|
95
|
|
|
return $response; |
|
96
|
|
|
} |
|
97
|
|
|
$response->header( 'X-WordLift-Cache', 'MISS' ); |
|
98
|
|
|
|
|
99
|
|
|
// Put the result before sending the json to the client, since sending the json will terminate us. |
|
100
|
|
|
$cache->put( $cache_key, $data ); |
|
101
|
|
|
|
|
102
|
|
|
return $response; |
|
103
|
|
|
|
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
private function is_endpoint() { |
|
107
|
|
|
$compare_route = WL_REST_ROUTE_DEFAULT_NAMESPACE . $this->endpoint; |
|
108
|
|
|
|
|
109
|
|
|
// Directly accessing $_SERVER['REQUEST_URI'] or $_GET['rest_route'] here as it's too early to use global $wp reliably |
|
110
|
|
|
|
|
111
|
|
|
if ( $_SERVER['REQUEST_URI'] && strpos( $_SERVER['REQUEST_URI'], $compare_route ) ) { |
|
112
|
|
|
return true; |
|
113
|
|
|
} |
|
114
|
|
|
if ( ! empty( $_GET['rest_route'] ) && strpos( $_GET['rest_route'], $compare_route ) ) { |
|
115
|
|
|
return true; |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
|
|
return false; |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
|
|
} |
|
122
|
|
|
|
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: