-
Notifications
You must be signed in to change notification settings - Fork 223
/
DynamicLists.php
305 lines (260 loc) · 7.12 KB
/
DynamicLists.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
<?php
declare(strict_types=1);
namespace WP_Rocket\Engine\Optimization\DynamicLists;
use WP_Rocket\Abstract_Render;
use WP_Rocket\Engine\Admin\Beacon\Beacon;
use WP_Rocket\Engine\License\API\User;
use WP_REST_Response;
use WP_Error;
class DynamicLists extends Abstract_Render {
/**
* Providers array.
* Array of objects with keys: api_client and data_manager.
*
* @var array
*/
private $providers;
/**
* User instance
*
* @var User
*/
private $user;
/**
* Beacon instance
*
* @var Beacon
*/
private $beacon;
/**
* Route Rest API namespace.
*/
const ROUTE_NAMESPACE = 'wp-rocket/v1';
/**
* Instantiate the class.
*
* @param array $providers Lists providers.
* @param User $user User instance.
* @param string $template_path Path to views.
* @param Beacon $beacon Beacon instance.
*/
public function __construct( array $providers, User $user, $template_path, Beacon $beacon ) {
parent::__construct( $template_path );
$this->providers = $providers;
$this->user = $user;
$this->beacon = $beacon;
}
/**
* Registers the dynamic lists update route
*
* @return void
*/
public function register_rest_route() {
register_rest_route(
self::ROUTE_NAMESPACE,
'dynamic_lists/update',
[
'methods' => 'PUT',
'callback' => [ $this, 'rest_update_response' ],
'permission_callback' => [ $this, 'check_permissions' ],
]
);
}
/**
* Checks user's permissions. This is a callback registered to REST route's "permission_callback" parameter.
*
* @return bool true if the user has permission; else false.
*/
public function check_permissions() {
return current_user_can( 'rocket_manage_options' );
}
/**
* Returns the update response
*
* @return WP_REST_Response|WP_Error
*/
public function rest_update_response() {
return rest_ensure_response( $this->update_lists_from_remote() );
}
/**
* Updates the lists from remote
*
* @return array
*/
public function update_lists_from_remote() {
if ( $this->user->is_license_expired() ) {
return [
'success' => false,
'data' => '',
'message' => __( 'You need an active license to get the latest version of the lists from our server.', 'rocket' ),
];
}
$response = [];
$success = false;
$should_purge = false;
foreach ( $this->providers as $provider ) {
$result = $provider->api_client->get_exclusions_list( $provider->data_manager->get_lists_hash() );
if ( empty( $result['code'] ) || empty( $result['body'] ) ) {
$response[ $provider->title ] = [
'success' => false,
'data' => '',
'message' => __( 'Could not get updated lists from server.', 'rocket' ),
];
continue;
}
if ( 206 === $result['code'] ) {
$response[ $provider->title ] = [
'success' => true,
'data' => '',
'message' => __( 'Lists are up to date.', 'rocket' ),
];
continue;
}
if ( ! $provider->data_manager->save_dynamic_lists( $result['body'] ) ) {
$response[ $provider->title ] = [
'success' => false,
'data' => '',
'message' => __( 'Could not update lists.', 'rocket' ),
];
continue;
}
$success = true;
$response[ $provider->title ] = [
'success' => true,
'data' => '',
'message' => __( 'Lists are successfully updated.', 'rocket' ),
];
$should_purge |= $provider->clear_cache ?? true;
}
if ( $success ) {
/**
* Fires after saving all dynamic lists files.
*
* @since 3.12.1
*
* @param bool $should_purge Should purge status based on the updated providers.
*/
do_action( 'rocket_after_save_dynamic_lists', $should_purge );
}
return $response;
}
/**
* Schedule cron to update dynamic lists weekly.
*
* @return void
*/
public function schedule_lists_update() {
if ( ! wp_next_scheduled( 'rocket_update_dynamic_lists' ) ) {
wp_schedule_event( time(), 'weekly', 'rocket_update_dynamic_lists' );
}
}
/**
* Clear dynamic lists update event.
*/
public function clear_schedule_lists_update() {
wp_clear_scheduled_hook( 'rocket_update_dynamic_lists' );
}
/**
* Displays the dynamic lists update section on tools tab
*
* @return void
*/
public function display_update_lists_section() {
if ( ! current_user_can( 'rocket_manage_options' ) ) {
return;
}
$data = [
'beacon' => $this->beacon->get_suggest( 'dynamic_lists' ),
];
echo $this->generate( 'settings/dynamic-lists-update', $data ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
}
/**
* Get the cached ignored parameters
*
* @return array
*/
public function get_cache_ignored_parameters(): array {
$lists = $this->providers['defaultlists']->data_manager->get_lists();
return isset( $lists->cache_ignored_parameters ) ? array_flip( $lists->cache_ignored_parameters ) : [];
}
/**
* Get the JS minify excluded external paths
*
* @return array
*/
public function get_js_minify_excluded_external(): array {
$lists = $this->providers['defaultlists']->data_manager->get_lists();
return isset( $lists->js_minify_external ) ? $lists->js_minify_external : [];
}
/**
* Get the patterns to move after the combine JS file
*
* @return array
*/
public function get_js_move_after_combine(): array {
$lists = $this->providers['defaultlists']->data_manager->get_lists();
return isset( $lists->js_move_after_combine ) ? $lists->js_move_after_combine : [];
}
/**
* Get the inline JS excluded from combine JS
*
* @return array
*/
public function get_combine_js_excluded_inline(): array {
$lists = $this->providers['defaultlists']->data_manager->get_lists();
return isset( $lists->js_excluded_inline ) ? $lists->js_excluded_inline : [];
}
/**
* Get the preload exclusions
*
* @return array
*/
public function get_preload_exclusions(): array {
$lists = $this->providers['defaultlists']->data_manager->get_lists();
return isset( $lists->preload_exclusions ) ? $lists->preload_exclusions : [];
}
/**
* Get Delay JS dynamic list.
*
* @return object
*/
public function get_delayjs_list() {
return $this->providers['delayjslists']->data_manager->get_lists();
}
/**
* Get the JS minify excluded files
*
* @return array
*/
public function get_js_exclude_files(): array {
$lists = $this->providers['defaultlists']->data_manager->get_lists();
return isset( $lists->exclude_js_files ) ? $lists->exclude_js_files : [];
}
/**
* Get the incompatible plugins list
*
* @return array
*/
public function get_incompatible_plugins() {
$lists = $this->providers['incompatible_plugins']->data_manager->get_plugins_list();
return isset( $lists ) ? $lists : [];
}
/**
* Get the staging list
*
* @return array
*/
public function get_stagings(): array {
$lists = $this->providers['defaultlists']->data_manager->get_lists();
return isset( $lists->staging_domains ) ? $lists->staging_domains : [];
}
/**
* Get the JS minify excluded templates
*
* @return array
*/
public function get_exclude_js_templates(): array {
$lists = $this->providers['defaultlists']->data_manager->get_lists();
return $lists->exclude_js_template ?? [];
}
}