|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Service: Http Api. |
|
4
|
|
|
* |
|
5
|
|
|
* Handle calls to `/wl-api`. |
|
6
|
|
|
* |
|
7
|
|
|
* See https://make.wordpress.org/plugins/2012/06/07/rewrite-endpoints-api/. |
|
8
|
|
|
* |
|
9
|
|
|
* @since 3.15.3 |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
/** |
|
13
|
|
|
* Define the {@link Wordlift_Http_Api} class. |
|
14
|
|
|
* |
|
15
|
|
|
* @since 3.15.3 |
|
16
|
|
|
*/ |
|
17
|
|
|
class Wordlift_Http_Api { |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* A {@link Wordlift_Log_Service} instance. |
|
21
|
|
|
* |
|
22
|
|
|
* @since 3.15.3 |
|
23
|
|
|
* |
|
24
|
|
|
* @var \Wordlift_Log_Service $log A {@link Wordlift_Log_Service} instance. |
|
25
|
|
|
*/ |
|
26
|
|
|
private $log; |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* Create a {@link Wordlift_End_Point} instance. |
|
30
|
|
|
* |
|
31
|
|
|
* @since 3.15.3 |
|
32
|
|
|
*/ |
|
33
|
|
|
public function __construct() { |
|
34
|
|
|
|
|
35
|
|
|
$this->log = Wordlift_Log_Service::get_logger( get_class() ); |
|
36
|
|
|
|
|
37
|
|
|
add_action( 'init', array( $this, 'add_rewrite_endpoint' ) ); |
|
38
|
|
|
add_action( 'template_redirect', array( $this, 'template_redirect' ) ); |
|
39
|
|
|
add_action( 'admin_post_wl_hello_world', array( $this, 'hello_world' ) ); |
|
40
|
|
|
add_action( 'admin_post_nopriv_wl_hello_world', array( $this, 'nopriv_hello_world' ) ); |
|
41
|
|
|
|
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* Add the `wl-api` rewrite end-point. |
|
46
|
|
|
* |
|
47
|
|
|
* @since 3.15.3 |
|
48
|
|
|
*/ |
|
49
|
|
|
public function add_rewrite_endpoint() { |
|
50
|
|
|
|
|
51
|
|
|
add_rewrite_endpoint( 'wl-api', EP_ROOT ); |
|
52
|
|
|
$this->ensure_rewrite_rules_are_flushed(); |
|
53
|
|
|
|
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* Handle `template_redirect` hooks. |
|
58
|
|
|
* |
|
59
|
|
|
* @since 3.15.3 |
|
60
|
|
|
*/ |
|
61
|
|
|
public function template_redirect() { |
|
|
|
|
|
|
62
|
|
|
|
|
63
|
|
|
global $wp_query; |
|
|
|
|
|
|
64
|
|
|
|
|
65
|
|
|
if ( ! isset( $wp_query->query_vars['wl-api'] ) ) { |
|
66
|
|
|
$this->log->trace( 'Skipping, not a `wl-api` call.' ); |
|
67
|
|
|
|
|
68
|
|
|
return; |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
$this->do_action( $_REQUEST['action'] ); |
|
72
|
|
|
|
|
73
|
|
|
exit; |
|
|
|
|
|
|
74
|
|
|
|
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
/** |
|
78
|
|
|
* Do the requested action. |
|
79
|
|
|
* |
|
80
|
|
|
* @since 3.15.3 |
|
81
|
|
|
* |
|
82
|
|
|
* @param string $action The action to execute. |
|
83
|
|
|
*/ |
|
84
|
|
|
private function do_action( $action ) { |
|
85
|
|
|
|
|
86
|
|
|
if ( empty( $action ) ) { |
|
87
|
|
|
return; |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
if ( ! wp_validate_auth_cookie( '', 'logged_in' ) ) { |
|
91
|
|
|
/** |
|
92
|
|
|
* Fires on a non-authenticated admin post request for the given action. |
|
93
|
|
|
* |
|
94
|
|
|
* The dynamic portion of the hook name, `$action`, refers to the given |
|
95
|
|
|
* request action. |
|
96
|
|
|
* |
|
97
|
|
|
* @since 2.6.0 |
|
98
|
|
|
*/ |
|
99
|
|
|
do_action( "admin_post_nopriv_{$action}" ); |
|
100
|
|
|
} else { |
|
101
|
|
|
/** |
|
102
|
|
|
* Fires on an authenticated admin post request for the given action. |
|
103
|
|
|
* |
|
104
|
|
|
* The dynamic portion of the hook name, `$action`, refers to the given |
|
105
|
|
|
* request action. |
|
106
|
|
|
* |
|
107
|
|
|
* @since 2.6.0 |
|
108
|
|
|
*/ |
|
109
|
|
|
do_action( "admin_post_{$action}" ); |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
/** |
|
115
|
|
|
* Test function, anonymous. |
|
116
|
|
|
* |
|
117
|
|
|
* @since 3.15.3 |
|
118
|
|
|
*/ |
|
119
|
|
|
public function nopriv_hello_world() { |
|
120
|
|
|
|
|
121
|
|
|
wp_die( 'Hello World! (from anonymous)' ); |
|
122
|
|
|
|
|
123
|
|
|
} |
|
124
|
|
|
|
|
125
|
|
|
/** |
|
126
|
|
|
* Test function, authenticated. |
|
127
|
|
|
* |
|
128
|
|
|
* @since 3.15.3 |
|
129
|
|
|
*/ |
|
130
|
|
|
public function hello_world() { |
|
131
|
|
|
|
|
132
|
|
|
wp_die( 'Hello World! (from authenticated)' ); |
|
133
|
|
|
|
|
134
|
|
|
} |
|
135
|
|
|
|
|
136
|
|
|
/** |
|
137
|
|
|
* Ensure that the rewrite rules are flushed the first time. |
|
138
|
|
|
* |
|
139
|
|
|
* @since 3.15.3 |
|
140
|
|
|
*/ |
|
141
|
|
|
public static function ensure_rewrite_rules_are_flushed() { |
|
142
|
|
|
|
|
143
|
|
|
if ( 1 !== get_option( 'wl_http_api' ) ) { |
|
144
|
|
|
flush_rewrite_rules(); |
|
145
|
|
|
add_option( 'wl_http_api', 1 ); |
|
146
|
|
|
} |
|
147
|
|
|
|
|
148
|
|
|
} |
|
149
|
|
|
|
|
150
|
|
|
} |
|
151
|
|
|
|
Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable: