-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathajax.php
More file actions
53 lines (40 loc) · 1.03 KB
/
Copy pathajax.php
File metadata and controls
53 lines (40 loc) · 1.03 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
<?php
namespace tangible\api;
use tangible\api;
// AJAX
function ajax_callback() {
try {
if (!api\verify_nonce()) return api\error('Not allowed');
$request = isset($_POST['request']) ? json_decode(
stripslashes_deep($_POST['request']),
JSON_OBJECT_AS_ARRAY
) : [];
if ( !is_array($request) ) $request = [];
$name = $request['action'];
$data = $request['data'];
/**
* Action callback can:
*
* - Call api\send() or api\error(), which outputs JSON
* response and exits
* - Return a result to be sent
* - Throw an error
*
* All are standardized to response as { data } or { error }.
*/
$result = api\action( $name, $data );
return api\send( $result );
} catch (\Throwable $th) {
return api\error( $th->getMessage() );
}
}
// Logged-in users
add_action(
'wp_ajax_' . api::$state->action_key,
__NAMESPACE__ . '\\ajax_callback'
);
// Public
add_action(
'wp_ajax_nopriv_' . api::$state->action_key,
__NAMESPACE__ . '\\ajax_callback'
);