-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathclass-base-api.php
More file actions
207 lines (161 loc) · 4.29 KB
/
class-base-api.php
File metadata and controls
207 lines (161 loc) · 4.29 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
<?php
/**
* Class Base_API
*
* Sets up scaffold for creating ajax endpoints, allows cacheable GET requests.
*/
abstract class Base_API {
/**
* Rewrite url endpoint for api to answer to, can be overridden by implementation class.
*
* @protected
* @static
*
* @var string
*/
protected static $rewrite_endpoint = 'api';
/**
* Method names that follow $rewrite_endpoint ex. /api/$front_endpoint
* for unauthenticated use.
*
* @protected
* @static
*
* @var array
*/
protected static $front_endpoints = array();
/**
* Method names that follow $rewrite_endpoint ex. /api/$admin_endpoints
* for authenticated use.
*
* @protected
* @static
*
* @var array
*/
protected static $admin_endpoints = array();
/**
* Allowed origins for cross domain api access.
*
* @protected
*
* @var array
*/
protected $allowed_origins = array();
/**
* Base_API constructor.
*/
public function __construct() {
add_action( 'init', array( $this, 'init' ) );
add_action( 'template_redirect', array( $this, 'api_endpoint_template_redirect' ) );
}
/**
* Sets up rewrite endpoint
*/
public function init() {
// Add API Endpoint
add_rewrite_endpoint( static::$rewrite_endpoint, EP_ROOT );
}
/**
* Helper function for checking request type
*
* @return string
*/
public function request_type() {
return $_SERVER['REQUEST_METHOD'];
}
/**
* Helper function for checking authentication status
*
* @return array
*/
public function is_user_admin() {
$is_admin = false;
if ( is_user_logged_in() ) {
$user = wp_get_current_user();
$allowed_roles = array(
'editor',
'administrator',
'author'
);
/**
* Get the allowed roles for admin endpoint access.
*
* @param array $allowed_roles Allowed roles for admin access
*/
$allowed_roles = apply_filters( 'base_api_allowed_roles', $allowed_roles );
if ( is_super_admin( $user->ID ) ) {
$is_admin = true;
} else {
$intersection = array_intersect( $allowed_roles, $user->roles );
if ( $intersection ) {
$is_admin = true;
}
}
}
return $is_admin;
}
/**
* Handles template redirect requests. Checks if endpoint is valid and if
* user should be authenticated, routes request to proper handler function
*/
public function api_endpoint_template_redirect() {
/**
* @var $wp_query \WP_Query
*/
global $wp_query;
if ( empty( $wp_query->query_vars[ static::$rewrite_endpoint ] ) ) {
return;
}
// Allows use of DOING_AJAX content just like admin-ajax requests
if ( ! defined( 'DOING_AJAX' ) ) {
define( 'DOING_AJAX', true );
}
$api = explode( '/', $wp_query->query_vars[ static::$rewrite_endpoint ] );
$endpoint = array_shift( $api );
$endpoint_method = str_replace( '-', '_', $endpoint );
$is_front_endpoint = false;
$is_admin_endpoint = false;
if ( in_array( $endpoint, static::$front_endpoints ) ) {
$is_front_endpoint = true;
}
if ( in_array( $endpoint, static::$admin_endpoints ) ) {
$is_admin_endpoint = true;
}
if ( ( ! $is_front_endpoint && ! $is_admin_endpoint ) || ! method_exists( $this, $endpoint_method ) ) {
wp_send_json_error( __( 'This endpoint does not exist.', 'base-api' ) );
}
if ( $is_admin_endpoint && ! $this->is_user_admin() ) {
wp_send_json_error( __( 'This is an admin-only endpoint.', 'base-api' ) );
}
$data = call_user_func_array( array( $this, $endpoint_method ), $api );
// Check for WP_Error response
if ( is_wp_error( $data ) ) {
/**
* @var $data \WP_Error
*/
wp_send_json_error( $data->get_error_message() );
}
status_header( 200 );
$http_origin = $_SERVER['HTTP_ORIGIN'];
if ( in_array( $http_origin, $this->allowed_origins, true ) ) {
header( 'Access-Control-Allow-Credentials: true' );
header( 'Access-Control-Allow-Origin: ' . $http_origin );
}
header( 'Content-type: application/json' );
wp_send_json_success( $data );
}
/**
* With "/" delimited params there are times when defaults are needed in the url, but they are not needed
* once there are keys mapped to the values. This function combined with array_walk dumps default keys
* and values.
*
* @param string $item
* @param string $key
*/
public function empty_defaults( &$item, $key ) {
if ( 'default' === $item ) {
$item = null;
}
}
}