-
Notifications
You must be signed in to change notification settings - Fork 210
/
Subscriber.php
308 lines (266 loc) · 6.96 KB
/
Subscriber.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
<?php
declare(strict_types=1);
namespace WP_Rocket\Engine\Optimization\RUCSS\Cron;
use WP_Rocket\Event_Management\Subscriber_Interface;
use WP_Rocket\Engine\Common\Queue\RUCSSQueueRunner;
use WP_Rocket\Engine\Optimization\RUCSS\Admin\Database;
use WP_Rocket\Engine\Optimization\RUCSS\Controller\UsedCSS;
class Subscriber implements Subscriber_Interface {
/**
* UsedCss instance
*
* @var UsedCSS
*/
private $used_css;
/**
* Database instance
*
* @var Database
*/
private $database;
/**
* Instantiate the class
*
* @param UsedCSS $used_css UsedCSS instance.
* @param Database $database Database instance.
*/
public function __construct( UsedCSS $used_css, Database $database ) {
$this->used_css = $used_css;
$this->database = $database;
}
/**
* Return an array of events that this subscriber listens to.
*
* @return array
*/
public static function get_subscribed_events(): array {
return [
'rocket_rucss_pending_jobs' => 'process_pending_jobs',
'rocket_rucss_on_submit_jobs' => 'process_on_submit_jobs',
'rocket_rucss_job_check_status' => 'check_job_status',
'rocket_rucss_clean_rows_time_event' => 'cron_clean_rows',
'cron_schedules' => 'add_interval',
'rocket_deactivation' => 'on_deactivation',
'rocket_remove_rucss_failed_jobs' => 'cron_remove_failed_jobs',
'init' => [
[ 'schedule_clean_not_commonly_used_rows' ],
[ 'schedule_pending_jobs' ],
[ 'initialize_rucss_queue_runner' ],
[ 'schedule_removing_failed_jobs' ],
[ 'schedule_on_submit_jobs' ],
],
];
}
/**
* Schedules cron to clean not commonly used RUCSS rows.
*
* @since 3.9
*
* @return void
*/
public function schedule_clean_not_commonly_used_rows() {
if ( ! $this->used_css->is_enabled() ) {
return;
}
if ( wp_next_scheduled( 'rocket_rucss_clean_rows_time_event' ) ) {
return;
}
wp_schedule_event( time(), 'weekly', 'rocket_rucss_clean_rows_time_event' );
}
/**
* Initialize the queue runner for our RUCSS.
*
* @return void
*/
public function initialize_rucss_queue_runner() {
if ( ! $this->used_css->is_enabled() ) {
return;
}
RUCSSQueueRunner::instance()->init();
}
/**
* Process pending jobs with Cron iteration.
*
* @return void
*/
public function process_pending_jobs() {
$this->used_css->process_pending_jobs();
}
/**
* Process on submit jobs with Cron iteration.
*
* @return void
*/
public function process_on_submit_jobs() {
$this->used_css->process_on_submit_jobs();
}
/**
* Cron callback for deleting old rows in both table databases.
*
* @since 3.9
*
* @return void
*/
public function cron_clean_rows() {
if ( ! $this->is_deletion_enabled() ) {
return;
}
$this->database->delete_old_used_css();
}
/**
* Cron callback for removing failed jobs.
*
* @return void
*/
public function cron_remove_failed_jobs() {
$this->used_css->clear_failed_urls();
}
/**
* Handle job status by DB row ID.
*
* @param int $row_id DB Row ID.
*
* @return void
*/
public function check_job_status( int $row_id ) {
$this->used_css->check_job_status( $row_id );
}
/**
* Adds new interval for RUCSS pending jobs cron
*
* @since 3.11.3
*
* @param array[] $schedules An array of non-default cron schedule arrays.
*
* @return array
*/
public function add_interval( $schedules ) {
if ( ! $this->used_css->is_enabled() ) {
return $schedules;
}
/**
* Filters the cron interval.
*
* @since 3.11
*
* @param int $interval Interval in seconds.
*/
$interval = apply_filters( 'rocket_rucss_pending_jobs_cron_interval', 1 * rocket_get_constant( 'MINUTE_IN_SECONDS', 60 ) );
$schedules['rocket_rucss_pending_jobs'] = [
'interval' => $interval,
'display' => esc_html__( 'WP Rocket Remove Unused CSS pending jobs', 'rocket' ),
];
$default_interval = 3 * rocket_get_constant( 'DAY_IN_SECONDS', 86400 );
/**
* Filters the cron interval for clearing failed jobs.
*
* @param int $interval Interval in seconds.
*/
$interval = apply_filters( 'rocket_remove_rucss_failed_jobs_cron_interval', $default_interval );
$interval = (bool) $interval ? $interval : $default_interval;
$schedules['rocket_remove_rucss_failed_jobs'] = [
'interval' => $interval,
'display' => esc_html__( 'WP Rocket clear Remove Unused CSS failed jobs', 'rocket' ),
];
/**
* Filters the cron interval for processing on submit jobs.
*
* @param int $interval Interval in seconds.
*/
$interval = (int) apply_filters( 'rocket_remove_rucss_on_submit_jobs_cron_interval', 1 * rocket_get_constant( 'MINUTE_IN_SECONDS', 60 ) );
$schedules['rocket_rucss_on_submit_jobs'] = [
'interval' => $interval,
'display' => esc_html__( 'WP Rocket procees on submit jobs', 'rocket' ),
];
return $schedules;
}
/**
* Schedule on submit jobs.
*
* @return void
*/
public function schedule_on_submit_jobs() {
if (
! $this->used_css->is_enabled()
&&
wp_next_scheduled( 'rocket_rucss_on_submit_jobs' )
) {
wp_clear_scheduled_hook( 'rocket_rucss_on_submit_jobs' );
return;
}
if ( ! $this->used_css->is_enabled() ) {
return;
}
if ( wp_next_scheduled( 'rocket_rucss_on_submit_jobs' ) ) {
return;
}
wp_schedule_event( time(), 'rocket_rucss_on_submit_jobs', 'rocket_rucss_on_submit_jobs' );
}
/**
* Schedules cron to get RUCSS pendings jobs.
*
* @since 3.11.3
*
* @return void
*/
public function schedule_pending_jobs() {
if (
! $this->used_css->is_enabled()
&&
wp_next_scheduled( 'rocket_rucss_pending_jobs' )
) {
wp_clear_scheduled_hook( 'rocket_rucss_pending_jobs' );
return;
}
if ( ! $this->used_css->is_enabled() ) {
return;
}
if ( wp_next_scheduled( 'rocket_rucss_pending_jobs' ) ) {
return;
}
wp_schedule_event( time(), 'rocket_rucss_pending_jobs', 'rocket_rucss_pending_jobs' );
}
/**
* Schedules cron to remove failed jobs.
*
* @return void
*/
public function schedule_removing_failed_jobs() {
if (
! $this->used_css->is_enabled()
&&
wp_next_scheduled( 'rocket_remove_rucss_failed_jobs' )
) {
wp_clear_scheduled_hook( 'rocket_remove_rucss_failed_jobs' );
return;
}
if ( ! $this->used_css->is_enabled() ) {
return;
}
if ( wp_next_scheduled( 'rocket_remove_rucss_failed_jobs' ) ) {
return;
}
wp_schedule_event( time(), 'rocket_remove_rucss_failed_jobs', 'rocket_remove_rucss_failed_jobs' );
}
/**
* Clear schedule of RUCSS CRONs on deactivation.
*
* @return void
*/
public function on_deactivation() {
wp_clear_scheduled_hook( 'action_scheduler_run_queue_rucss', [ 'WP Cron' ] );
}
/**
* Checks if the RUCSS deletion is enabled.
*
* @return bool
*/
protected function is_deletion_enabled(): bool {
/**
* Filters the enable RUCSS deletion value
*
* @param bool $delete_rucss True to enable deletion, false otherwise.
*/
return (bool) apply_filters( 'rocket_rucss_deletion_enabled', true );
}
}