-
Notifications
You must be signed in to change notification settings - Fork 7
/
AjaxWrapper.php
457 lines (393 loc) · 12.2 KB
/
AjaxWrapper.php
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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
<?php
if (!class_exists('Ajaw_v1_ActionBuilder', false)):
class Ajaw_v1_ActionBuilder {
private $action;
private $callback = '__return_null';
private $params = array();
private $httpMethod = null;
private $capability = null;
private $permissionCheckCallback = null;
private $mustBeLoggedIn = true;
private $checkNonce = true;
public function __construct($action) {
$this->action = $action;
}
/**
* @param callable $callback
* @return $this
*/
public function handler($callback) {
$this->callback = $callback;
return $this;
}
public function requiredParam($name, $type = null, $validateCallback = null) {
return $this->addParameter($name, $type, true, null, $validateCallback);
}
public function optionalParam($name, $defaultValue = null, $type = null, $validateCallback = null) {
return $this->addParameter($name, $type, false, $defaultValue, $validateCallback);
}
private function addParameter($name, $type, $required, $defaultValue, $validateCallback) {
if (isset($type) && !isset(Ajaw_v1_Action::$defaultValidators[$type])) {
throw new LogicException(sprintf(
'Unknown parameter type "%s". Supported types are: %s.',
$type,
implode(', ', array_keys(Ajaw_v1_Action::$defaultValidators[$type]))
));
}
$this->params[$name] = array(
'required' => $required,
'defaultValue' => $defaultValue,
'type' => $type,
'validateCallback' => $validateCallback,
);
return $this;
}
public function method($httpMethod) {
$this->httpMethod = strtoupper($httpMethod);
return $this;
}
public function requiredCap($capability) {
$this->capability = $capability;
return $this;
}
public function permissionCallback($callback) {
$this->permissionCheckCallback = $callback;
return $this;
}
public function allowUnprivilegedUsers() {
$this->mustBeLoggedIn = false;
return $this;
}
public function withoutNonce() {
$this->checkNonce = false;
return $this;
}
public function build() {
$instance = new Ajaw_v1_Action($this->action, $this->callback, $this->params);
$instance->mustBeLoggedIn = $this->mustBeLoggedIn;
$instance->requiredCap = $this->capability;
$instance->nonceCheckEnabled = $this->checkNonce;
$instance->method = $this->httpMethod;
$instance->permissionCallback = $this->permissionCheckCallback;
return $instance;
}
public function register() {
$instance = $this->build();
$instance->register();
return $instance;
}
}
endif;
if (!class_exists('Ajaw_v1_Action', false)):
class Ajaw_v1_Action {
public $action;
public $callback;
public $params = array();
public $method = null;
public $requiredCap = null;
public $mustBeLoggedIn = false;
public $nonceCheckEnabled = true;
public $permissionCallback = null;
private $isScriptRegistered = false;
public static $defaultValidators = array(
'int' => array(__CLASS__, 'validateInt'),
'float' => array(__CLASS__, 'validateFloat'),
'boolean' => array(__CLASS__, 'validateBoolean'),
'string' => array(__CLASS__, 'validateString'),
);
public function __construct($action, $callback, $params) {
$this->action = $action;
$this->callback = $callback;
$this->params = $params;
if (empty($this->action)) {
throw new LogicException(sprintf(
'AJAX action name is missing. You must either pass it to the %1$s constructor '
. 'or give the %1$s::$action property a valid default value.',
get_class($this)
));
}
}
/**
* Set up hooks for AJAX and helper scripts.
*/
public function register() {
//Register the AJAX handler(s).
$hookNames = array('wp_ajax_' . $this->action);
if (!$this->mustBeLoggedIn) {
$hookNames[] = 'wp_ajax_nopriv_' . $this->action;
}
foreach($hookNames as $hook) {
if (has_action($hook)) {
throw new RuntimeException(sprintf('The action name "%s" is already in use.', $this->action));
}
add_action($hook, array($this, 'processAjaxRequest'));
}
//Register the utility JS library after WP is fully loaded.
if (did_action('wp_loaded')) {
$this->registerScript();
} else {
add_action('wp_loaded', array($this, 'registerScript'), 2);
}
}
/**
* @access protected
*/
public function processAjaxRequest() {
$result = $this->handleAction();
if (is_wp_error($result)) {
$statusCode = $result->get_error_data();
if (isset($statusCode) && is_int($statusCode) ) {
status_header($statusCode);
}
$errorResponse = array(
'error' => array(
'message' => $result->get_error_message(),
'code' => $result->get_error_code()
)
);
$result = $errorResponse;
}
if (isset($result)) {
$this->outputJSON($result);
}
exit;
}
protected function handleAction() {
$method = $this->getRequestMethod();
if (isset($this->method) && ($method !== $this->method)) {
return new WP_Error(
'http_method_not_allowed',
'The HTTP method is not supported by the request handler.',
405
);
}
$isAuthorized = $this->checkAuthorization();
if ($isAuthorized !== true) {
return $isAuthorized;
}
$params = $this->parseParameters();
if ($params instanceof WP_Error) {
return $params;
}
//Call the user-specified action handler.
if (is_callable($this->callback)) {
return call_user_func($this->callback, $params);
} else {
return new WP_Error(
'missing_ajax_handler',
sprintf(
'There is no request handler assigned to the "%1$s" action. '
. 'Either pass a valid callback to $builder->request() or override the %2$s::%3$s method.',
$this->action,
__CLASS__,
__METHOD__
),
500
);
}
}
/**
* Check if the current user is authorized to perform this action.
*
* @return bool|WP_Error
*/
protected function checkAuthorization() {
if ($this->mustBeLoggedIn && !is_user_logged_in()) {
return new WP_Error('login_required', 'You must be logged in to perform this action.', 403);
}
if (isset($this->requiredCap) && !current_user_can($this->requiredCap)) {
return new WP_Error('capability_missing', 'You don\'t have permission to perform this action.', 403);
}
if ($this->nonceCheckEnabled && !check_ajax_referer($this->action, false, false)) {
return new WP_Error('nonce_check_failed', 'Invalid or missing nonce.', 403);
}
if (isset($this->permissionCallback)) {
$result = call_user_func($this->permissionCallback);
if ($result === false) {
return new WP_Error(
'permission_callback_failed',
'You don\'t have permission to perform this action.',
403
);
} else if (is_wp_error($result)) {
return $result;
}
}
return true;
}
protected function getRequestMethod() {
return strtoupper(filter_input(
INPUT_SERVER,
'REQUEST_METHOD',
FILTER_VALIDATE_REGEXP,
array('options' => array('regexp' => '/^[a-z]{3,20}$/i'))
));
}
protected function parseParameters() {
$method = $this->getRequestMethod();
// phpcs:disable WordPress.Security.NonceVerification -- checkAuthorization() is where nonce verification happens.
//Retrieve request parameters.
if ($method === 'GET') {
$rawParams = $_GET;
} else if ($method === 'POST') {
$rawParams = $_POST;
} else {
$rawParams = $_REQUEST;
}
// phpcs:enable
//Remove magic quotes. WordPress applies them in wp-settings.php.
//There's no hook for wp_magic_quotes, so we use one that's closest in execution order.
if (did_action('sanitize_comment_cookies') && function_exists('wp_magic_quotes')) {
$rawParams = wp_unslash($rawParams);
}
//Validate all parameters.
$inputParams = $rawParams;
foreach($this->params as $name => $settings) {
//Verify that all the required parameters are present.
//Empty strings are treated as missing parameters.
if (isset($inputParams[$name]) && ($inputParams[$name] !== '')) {
$value = $this->validateParameter($settings, $inputParams[$name], $name);
if (is_wp_error($value)) {
return $value;
} else {
$inputParams[$name] = $value;
}
} else if (empty($settings['required'])) {
//It's an optional parameter. Use the default value.
$inputParams[$name] = $settings['defaultValue'];
} else {
return new WP_Error(
'missing_required_parameter',
sprintf('Required parameter is missing or empty: "%s".', $name),
400
);
}
}
return $inputParams;
}
protected function validateParameter($settings, $value, $name) {
if (isset($settings['type'])) {
$value = call_user_func(self::$defaultValidators[$settings['type']], $value, $name);
if (is_wp_error($value)) {
return $value;
}
}
if (isset($settings['validateCallback'])) {
$success = call_user_func($settings['validateCallback'], $value);
if (is_wp_error($success)) {
return $success;
} else if ($success === false) {
return new WP_Error(
'invalid_parameter_value',
sprintf('The value of the parameter "%s" is invalid.', $name),
400
);
}
}
return $value;
}
private static function validateInt($value, $name) {
$result = filter_var($value, FILTER_VALIDATE_INT);
if ($result === false) {
return new WP_Error(
'invalid_parameter_value',
sprintf('The value of the parameter "%s" is invalid. It must be an integer.', $name),
400
);
}
return $result;
}
private static function validateFloat($value, $name) {
$result = filter_var($value, FILTER_VALIDATE_FLOAT);
if ($result === false) {
return new WP_Error(
'invalid_parameter_value',
sprintf('The value of the parameter "%s" is invalid. It must be a float.', $name),
400
);
}
return $result;
}
private static function validateBoolean($value, $name) {
$result = filter_var($value, FILTER_VALIDATE_BOOLEAN, array('flags' => FILTER_NULL_ON_FAILURE));
if ($result === null) {
return new WP_Error(
'invalid_parameter_value',
sprintf('The value of the parameter "%s" is invalid. It must be a boolean.', $name),
400
);
}
return $result;
}
private static function validateString($value, $name) {
if (!is_string($value)) {
return new WP_Error(
'invalid_parameter_value',
sprintf('The value of the parameter "%s" is invalid. It must be a string.', $name),
400
);
}
return $value;
}
protected function outputJSON($response) {
@header('Content-Type: application/json; charset=' . get_option('blog_charset'));
echo wp_json_encode($response);
}
public function registerScript() {
if ($this->isScriptRegistered) {
return;
}
$this->isScriptRegistered = true;
//There could be multiple instances of this class, but we only need to register the script once.
$handle = $this->getScriptHandle();
if (!wp_script_is($handle, 'registered')) {
wp_register_script(
$handle,
plugins_url('ajax-action-wrapper.js', __FILE__),
array('jquery'),
'20161105'
);
}
//Pass the action to the script.
if (function_exists('wp_add_inline_script')) {
wp_add_inline_script($handle, $this->generateActionJs(), 'after'); //WP 4.5+
} else {
add_filter('script_loader_tag', array($this, 'addRegistrationScript'), 10, 2); //WP 4.1+
}
}
/**
* Backwards compatibility for older versions of WP that don't have wp_add_inline_script().
* @internal
*
* @param string $tag
* @param string $handle
* @return string
*/
public function addRegistrationScript($tag, $handle) {
if ($handle === $this->getScriptHandle()) {
$tag .= '<script type="text/javascript">' . $this->generateActionJs() . '</script>';
}
return $tag;
}
protected function generateActionJs() {
$properties = array(
'ajaxUrl' => admin_url('admin-ajax.php'),
'method' => $this->method,
'nonce' => $this->nonceCheckEnabled ? wp_create_nonce($this->action) : null,
);
return sprintf(
'AjawV1.actionRegistry.add("%s", %s);' . "\n",
esc_js($this->action),
wp_json_encode($properties)
);
}
public function getScriptHandle() {
return 'ajaw-v1-ajax-action-wrapper';
}
}
endif;
if (!function_exists('ajaw_v1_CreateAction')) {
function ajaw_v1_CreateAction($action) {
return new Ajaw_v1_ActionBuilder($action);
}
}