-
Notifications
You must be signed in to change notification settings - Fork 223
/
DataManagerSubscriber.php
318 lines (249 loc) · 7.11 KB
/
DataManagerSubscriber.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
<?php
namespace WP_Rocket\Engine\CDN\RocketCDN;
use WP_Rocket\Event_Management\Subscriber_Interface;
/**
* Subscriber for the RocketCDN integration in WP Rocket settings page
*
* @since 3.5
*/
class DataManagerSubscriber implements Subscriber_Interface {
const CRON_EVENT = 'rocketcdn_check_subscription_status_event';
/**
* RocketCDN API Client instance.
*
* @var APIClient
*/
private $api_client;
/**
* CDNOptionsManager instance.
*
* @var CDNOptionsManager
*/
private $cdn_options;
/**
* Constructor
*
* @param APIClient $api_client RocketCDN API Client instance.
* @param CDNOptionsManager $cdn_options CDNOptionsManager instance.
*/
public function __construct( APIClient $api_client, CDNOptionsManager $cdn_options ) {
$this->api_client = $api_client;
$this->cdn_options = $cdn_options;
}
/**
* {@inheritdoc}
*/
public static function get_subscribed_events() {
return [
'wp_ajax_save_rocketcdn_token' => 'update_user_token',
'wp_ajax_rocketcdn_enable' => 'enable',
'wp_ajax_rocketcdn_disable' => 'disable',
'wp_ajax_rocketcdn_process_set' => 'set_process_status',
'wp_ajax_rocketcdn_process_status' => 'get_process_status',
'wp_ajax_rocketcdn_validate_token_cname' => 'validate_token_cname',
self::CRON_EVENT => 'maybe_disable_cdn',
];
}
/**
* Updates the RocketCDN user token value
*
* @since 3.5
*
* @return void
*/
public function update_user_token() {
check_ajax_referer( 'rocket-ajax', 'nonce', true );
if ( ! current_user_can( 'rocket_manage_options' ) ) {
wp_send_json_error( 'unauthorized_user' );
}
if ( empty( $_POST['value'] ) ) {
delete_option( 'rocketcdn_user_token' );
wp_send_json_success( 'user_token_deleted' );
}
if ( ! is_string( $_POST['value'] ) ) {
wp_send_json_error( 'invalid_token' );
}
$token = sanitize_key( $_POST['value'] );
if ( 40 !== strlen( $token ) ) {
wp_send_json_error( 'invalid_token_length' );
}
update_option( 'rocketcdn_user_token', $token );
wp_send_json_success( 'user_token_saved' );
}
/**
* Ajax callback to enable RocketCDN
*
* @since 3.5
*
* @return void
*/
public function enable() {
check_ajax_referer( 'rocket-ajax', 'nonce', true );
$data = [
'process' => 'subscribe',
];
if ( ! current_user_can( 'rocket_manage_options' ) ) {
$data['message'] = 'unauthorized_user';
wp_send_json_error( $data );
}
if ( empty( $_POST['cdn_url'] ) ) {
$data['message'] = 'cdn_url_empty';
wp_send_json_error( $data );
}
$cdn_url = filter_var( wp_unslash( $_POST['cdn_url'] ), FILTER_VALIDATE_URL );
if ( ! $cdn_url ) {
$data['message'] = 'cdn_url_invalid_format';
wp_send_json_error( $data );
}
$this->cdn_options->enable( esc_url_raw( $cdn_url ) );
$subscription = $this->api_client->get_subscription_data();
$this->schedule_subscription_check( $subscription );
$this->delete_process();
$data['message'] = 'rocketcdn_enabled';
wp_send_json_success( $data );
}
/**
* AJAX callback to disable RocketCDN
*
* @since 3.5
*
* @return void
*/
public function disable() {
check_ajax_referer( 'rocket-ajax', 'nonce', true );
$data = [
'process' => 'unsubscribe',
];
if ( ! current_user_can( 'rocket_manage_options' ) ) {
$data['message'] = 'unauthorized_user';
wp_send_json_error( $data );
}
$this->cdn_options->disable();
$timestamp = wp_next_scheduled( self::CRON_EVENT );
if ( $timestamp ) {
wp_unschedule_event( $timestamp, self::CRON_EVENT );
}
$this->delete_process();
$data['message'] = 'rocketcdn_disabled';
wp_send_json_success( $data );
}
/**
* Delete the option tracking the RocketCDN process state
*
* @since 3.5
*
* @return void
*/
private function delete_process() {
delete_option( 'rocketcdn_process' );
}
/**
* Set the RocketCDN subscription process status
*
* @since 3.5
*
* @return void
*/
public function set_process_status() {
check_ajax_referer( 'rocket-ajax', 'nonce', true );
if ( ! current_user_can( 'rocket_manage_options' ) ) {
return;
}
if ( empty( $_POST['status'] ) ) {
return;
}
$status = filter_var( $_POST['status'], FILTER_VALIDATE_BOOLEAN ); // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.MissingUnslash -- Used as a boolean.
if ( false === $status ) {
delete_option( 'rocketcdn_process' );
return;
}
update_option( 'rocketcdn_process', $status );
}
/**
* Check for RocketCDN subscription process status
*
* @since 3.5
*
* @return void
*/
public function get_process_status() {
check_ajax_referer( 'rocket-ajax', 'nonce', true );
if ( ! current_user_can( 'rocket_manage_options' ) ) {
wp_send_json_error();
}
if ( get_option( 'rocketcdn_process' ) ) {
wp_send_json_success();
}
wp_send_json_error();
}
/**
* Cron job to disable CDN if the subscription expired
*
* @since 3.5
*
* @return void
*/
public function maybe_disable_cdn() {
delete_transient( 'rocketcdn_status' );
$subscription = $this->api_client->get_subscription_data();
if ( rocket_get_constant( 'WP_ROCKET_IS_TESTING', false ) ) {
$subscription = apply_filters( 'rocket_pre_get_subscription_data', $subscription );
}
if ( 'running' === $subscription['subscription_status'] ) {
$this->schedule_subscription_check( $subscription );
return;
}
$this->cdn_options->disable();
}
/**
* Validates and updates the token and cname from RocketCDN Iframe.
*
* @return void
*/
public function validate_token_cname() {
check_ajax_referer( 'rocket-ajax', 'nonce', true );
$data = [];
if ( ! current_user_can( 'rocket_manage_options' ) ) {
$data['message'] = 'unauthorized_user';
wp_send_json_error( $data );
}
if ( empty( $_POST['cdn_url'] ) || empty( $_POST['cdn_token'] ) ) {
$data['message'] = 'cdn_values_empty';
wp_send_json_error( $data );
}
$token = sanitize_key( $_POST['cdn_token'] );
$cdn_url = filter_var( wp_unslash( $_POST['cdn_url'] ), FILTER_VALIDATE_URL );
if ( ! $cdn_url ) {
$data['message'] = 'cdn_url_invalid_format';
wp_send_json_error( $data );
}
if ( 40 !== strlen( $token ) ) {
$data['message'] = 'invalid_token_length';
wp_send_json_error( $data );
}
$current_token = get_option( 'rocketcdn_user_token' );
$current_cname = $this->cdn_options->get_cdn_cnames();
if ( ! empty( $current_token ) ) {
$data['message'] = 'token_already_set';
wp_send_json_error( $data );
}
update_option( 'rocketcdn_user_token', $token );
$this->cdn_options->enable( esc_url_raw( $cdn_url ) );
$data['message'] = 'token_updated_successfully';
wp_send_json_success( $data );
}
/**
* Schedule the next cron subscription check
*
* @since 3.5
*
* @param array $subscription Array containing the subscription data.
* @return void
*/
private function schedule_subscription_check( $subscription ) {
$timestamp = strtotime( $subscription['subscription_next_date_update'] ) + strtotime( '+2 days' );
if ( ! wp_next_scheduled( self::CRON_EVENT ) ) {
wp_schedule_single_event( $timestamp, self::CRON_EVENT );
}
}
}