|
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
|
|
|
|
|
40
|
|
|
//region SAMPLE ACTIONS. |
|
41
|
|
|
add_action( 'admin_post_wl_hello_world', array( |
|
42
|
|
|
$this, |
|
43
|
|
|
'hello_world', |
|
44
|
|
|
) ); |
|
45
|
|
|
add_action( 'admin_post_nopriv_wl_hello_world', array( |
|
46
|
|
|
$this, |
|
47
|
|
|
'nopriv_hello_world', |
|
48
|
|
|
) ); |
|
49
|
|
|
//endregion |
|
50
|
|
|
|
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* Add the `wl-api` rewrite end-point. |
|
55
|
|
|
* |
|
56
|
|
|
* @since 3.15.3 |
|
57
|
|
|
*/ |
|
58
|
|
|
public function add_rewrite_endpoint() { |
|
59
|
|
|
|
|
60
|
|
|
add_rewrite_endpoint( 'wl-api', EP_ROOT ); |
|
61
|
|
|
$this->ensure_rewrite_rules_are_flushed(); |
|
62
|
|
|
|
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
/** |
|
66
|
|
|
* Handle `template_redirect` hooks. |
|
67
|
|
|
* |
|
68
|
|
|
* @since 3.15.3 |
|
69
|
|
|
*/ |
|
70
|
|
|
public function template_redirect() { |
|
71
|
|
|
|
|
72
|
|
|
global $wp_query; |
|
|
|
|
|
|
73
|
|
|
|
|
74
|
|
|
if ( ! isset( $wp_query->query_vars['wl-api'] ) ) { |
|
75
|
|
|
$this->log->trace( 'Skipping, not a `wl-api` call.' ); |
|
76
|
|
|
|
|
77
|
|
|
return; |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
$this->do_action( $_REQUEST['action'] ); |
|
81
|
|
|
|
|
82
|
|
|
exit; |
|
83
|
|
|
|
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
/** |
|
87
|
|
|
* Do the requested action. |
|
88
|
|
|
* |
|
89
|
|
|
* @since 3.15.3 |
|
90
|
|
|
* |
|
91
|
|
|
* @param string $action The action to execute. |
|
92
|
|
|
*/ |
|
93
|
|
|
private function do_action( $action ) { |
|
94
|
|
|
|
|
95
|
|
|
if ( empty( $action ) ) { |
|
96
|
|
|
return; |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
if ( ! wp_validate_auth_cookie( '', 'logged_in' ) ) { |
|
100
|
|
|
/** |
|
101
|
|
|
* Fires on a non-authenticated admin post request for the given action. |
|
102
|
|
|
* |
|
103
|
|
|
* The dynamic portion of the hook name, `$action`, refers to the given |
|
104
|
|
|
* request action. |
|
105
|
|
|
* |
|
106
|
|
|
* @since 2.6.0 |
|
107
|
|
|
*/ |
|
108
|
|
|
do_action( "admin_post_nopriv_{$action}" ); |
|
109
|
|
|
} else { |
|
110
|
|
|
/** |
|
111
|
|
|
* Fires on an authenticated admin post request for the given action. |
|
112
|
|
|
* |
|
113
|
|
|
* The dynamic portion of the hook name, `$action`, refers to the given |
|
114
|
|
|
* request action. |
|
115
|
|
|
* |
|
116
|
|
|
* @since 2.6.0 |
|
117
|
|
|
*/ |
|
118
|
|
|
do_action( "admin_post_{$action}" ); |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
|
|
/** |
|
124
|
|
|
* Test function, anonymous. |
|
125
|
|
|
* |
|
126
|
|
|
* @since 3.15.3 |
|
127
|
|
|
*/ |
|
128
|
|
|
public function nopriv_hello_world() { |
|
129
|
|
|
|
|
130
|
|
|
wp_die( 'Hello World! (from anonymous)' ); |
|
131
|
|
|
|
|
132
|
|
|
} |
|
133
|
|
|
|
|
134
|
|
|
/** |
|
135
|
|
|
* Test function, authenticated. |
|
136
|
|
|
* |
|
137
|
|
|
* @since 3.15.3 |
|
138
|
|
|
*/ |
|
139
|
|
|
public function hello_world() { |
|
140
|
|
|
|
|
141
|
|
|
wp_die( 'Hello World! (from authenticated)' ); |
|
142
|
|
|
|
|
143
|
|
|
} |
|
144
|
|
|
|
|
145
|
|
|
/** |
|
146
|
|
|
* Ensure that the rewrite rules are flushed the first time. |
|
147
|
|
|
* |
|
148
|
|
|
* @since 3.16.0 changed the value from 1 to `yes` to avoid type juggling issues. |
|
149
|
|
|
* @since 3.15.3 |
|
150
|
|
|
*/ |
|
151
|
|
|
public static function ensure_rewrite_rules_are_flushed() { |
|
152
|
|
|
|
|
153
|
|
|
// See https://github.com/insideout10/wordlift-plugin/issues/698. |
|
154
|
|
|
if ( 'yes' !== get_option( 'wl_http_api' ) ) { |
|
155
|
|
|
update_option( 'wl_http_api', 'yes' ); |
|
156
|
|
|
add_action( 'wp_loaded', function () { |
|
157
|
|
|
flush_rewrite_rules(); |
|
158
|
|
|
} ); |
|
159
|
|
|
} |
|
160
|
|
|
|
|
161
|
|
|
} |
|
162
|
|
|
|
|
163
|
|
|
/** |
|
164
|
|
|
* Called by {@see activate_wordlift}, resets the `wl_http_api` option flag in order to force WordLift to |
|
165
|
|
|
* reinitialize the `wl-api` route. |
|
166
|
|
|
* |
|
167
|
|
|
* @see https://github.com/insideout10/wordlift-plugin/issues/820 related issue. |
|
168
|
|
|
* |
|
169
|
|
|
* @since 3.19.2 |
|
170
|
|
|
*/ |
|
171
|
|
|
public static function activate() { |
|
172
|
|
|
|
|
173
|
|
|
// Force the plugin to reinitialize the rewrite rules. |
|
174
|
|
|
update_option( 'wl_http_api', 'no' ); |
|
175
|
|
|
|
|
176
|
|
|
} |
|
177
|
|
|
|
|
178
|
|
|
/** |
|
179
|
|
|
* Delete the option when the plugin is deactivated. |
|
180
|
|
|
* |
|
181
|
|
|
* @since 3.19.4 |
|
182
|
|
|
* |
|
183
|
|
|
* @see https://github.com/insideout10/wordlift-plugin/issues/846 |
|
184
|
|
|
*/ |
|
185
|
|
|
public static function deactivate() { |
|
186
|
|
|
|
|
187
|
|
|
delete_option( 'wl_http_api' ); |
|
188
|
|
|
|
|
189
|
|
|
} |
|
190
|
|
|
|
|
191
|
|
|
} |
|
192
|
|
|
|
Instead of relying on
globalstate, we recommend one of these alternatives:1. Pass all data via parameters
2. Create a class that maintains your state