Diff: STRATO-apps/wordpress_03/app/wp-content/plugins/wp-rocket/inc/Engine/Preload/Cron/Subscriber.php
Keine Baseline-Datei – Diff nur gegen leer.
1
-
1
+
<?php
2
+
3
+
namespace WP_Rocket\Engine\Preload\Cron;
4
+
5
+
use WP_Rocket\Engine\Common\Queue\Cleaner;
6
+
use WP_Rocket\Engine\Preload\Admin\Settings;
7
+
use WP_Rocket\Engine\Preload\Controller\PreloadUrl;
8
+
use WP_Rocket\Engine\Preload\Database\Queries\Cache;
9
+
use WP_Rocket\Event_Management\Subscriber_Interface;
10
+
11
+
class Subscriber implements Subscriber_Interface {
12
+
13
+
/**
14
+
* Preload settings.
15
+
*
16
+
* @var Settings
17
+
*/
18
+
protected $settings;
19
+
20
+
/**
21
+
* Db query.
22
+
*
23
+
* @var Cache
24
+
*/
25
+
protected $query;
26
+
27
+
/**
28
+
* Preload url controller.
29
+
*
30
+
* @var PreloadUrl
31
+
*/
32
+
protected $preload_controller;
33
+
34
+
/**
35
+
* Creates an instance of the class.
36
+
*
37
+
* @param Settings $settings Preload settings.
38
+
* @param Cache $query Db query.
39
+
* @param PreloadUrl $preload_controller Preload url controller.
40
+
*/
41
+
public function __construct( Settings $settings, Cache $query, PreloadUrl $preload_controller ) {
42
+
$this->settings = $settings;
43
+
$this->query = $query;
44
+
$this->preload_controller = $preload_controller;
45
+
}
46
+
47
+
/**
48
+
* Return an array of events that this subscriber listens to.
49
+
*
50
+
* @return array
51
+
*/
52
+
public static function get_subscribed_events() {
53
+
return [
54
+
'rocket_preload_clean_rows_time_event' => 'remove_old_rows',
55
+
'rocket_preload_process_pending' => [
56
+
[ 'process_pending_urls' ],
57
+
[ 'clean_preload_jobs' ],
58
+
],
59
+
'rocket_preload_revert_old_failed_rows' => 'revert_old_failed_rows',
60
+
'cron_schedules' => [
61
+
[ 'add_interval' ],
62
+
[ 'add_revert_old_failed_interval' ],
63
+
],
64
+
'init' => [
65
+
[ 'schedule_clean_not_commonly_used_rows' ],
66
+
[ 'schedule_pending_jobs' ],
67
+
[ 'schedule_revert_old_failed_rows' ],
68
+
],
69
+
];
70
+
}
71
+
72
+
/**
73
+
* Schedule clean from removing of old urls.
74
+
*
75
+
* @return void
76
+
*/
77
+
public function schedule_clean_not_commonly_used_rows() {
78
+
/**
79
+
* Delay before the not accessed row is deleted.
80
+
*
81
+
* @param string $delay delay before the not accessed row is deleted.
82
+
*/
83
+
$delay = (string) apply_filters( 'rocket_preload_delay_delete_non_accessed', '1 month' );
84
+
85
+
if ( '' === $delay || wp_next_scheduled( 'rocket_preload_clean_rows_time_event' ) ) {
86
+
return;
87
+
}
88
+
89
+
wp_schedule_event( time() + 10 * MINUTE_IN_SECONDS, 'weekly', 'rocket_preload_clean_rows_time_event' );
90
+
}
91
+
92
+
/**
93
+
* Preload Url jobs.
94
+
*
95
+
* @return void
96
+
*/
97
+
public function process_pending_urls() {
98
+
if ( ! $this->settings->is_enabled() ) {
99
+
return;
100
+
}
101
+
102
+
$this->preload_controller->process_pending_jobs();
103
+
}
104
+
105
+
/**
106
+
* Clean Action Scheduler jobs for preload.
107
+
*
108
+
* @return void
109
+
*/
110
+
public function clean_preload_jobs() {
111
+
$clean_batch_size = (int) apply_filters( 'rocket_action_scheduler_clean_batch_size', 100, 'rocket-preload' );
112
+
$cleaner = new Cleaner( null, $clean_batch_size, 'rocket-preload' );
113
+
$cleaner->clean();
114
+
}
115
+
116
+
/**
117
+
* Add the interval for the cron.
118
+
*
119
+
* @param array $schedules Cron schedules.
120
+
* @return mixed
121
+
*/
122
+
public function add_interval( $schedules ) {
123
+
if ( ! $this->settings->is_enabled() ) {
124
+
return $schedules;
125
+
}
126
+
127
+
/**
128
+
* Filters the cron interval.
129
+
*
130
+
* @since 3.11
131
+
*
132
+
* @param int $interval Interval in seconds.
133
+
*/
134
+
$interval = apply_filters( 'rocket_preload_pending_jobs_cron_interval', 1 * rocket_get_constant( 'MINUTE_IN_SECONDS', 60 ) );
135
+
136
+
$schedules['rocket_preload_process_pending'] = [
137
+
'interval' => $interval,
138
+
'display' => esc_html__( 'WP Rocket Preload pending jobs', 'rocket' ),
139
+
];
140
+
141
+
return $schedules;
142
+
}
143
+
144
+
/**
145
+
* Add the interval for the cron.
146
+
*
147
+
* @param array $schedules Cron schedules.
148
+
* @return mixed
149
+
*/
150
+
public function add_revert_old_failed_interval( $schedules ) {
151
+
if ( ! $this->settings->is_enabled() ) {
152
+
return $schedules;
153
+
}
154
+
155
+
/**
156
+
* Filters the cron interval.
157
+
*
158
+
* @since 3.11
159
+
*
160
+
* @param int $interval Interval in seconds.
161
+
*/
162
+
$interval = apply_filters( 'rocket_preload_revert_old_failed_rows_cron_interval', 12 * rocket_get_constant( 'HOUR_IN_SECONDS', 60 * 60 ) );
163
+
164
+
$schedules['rocket_revert_old_failed_rows'] = [
165
+
'interval' => $interval,
166
+
'display' => esc_html__( 'WP Rocket Preload revert stuck failed jobs', 'rocket' ),
167
+
];
168
+
169
+
return $schedules;
170
+
}
171
+
172
+
/**
173
+
* Schedule pending preload urls.
174
+
*
175
+
* @return void
176
+
*/
177
+
public function schedule_pending_jobs() {
178
+
179
+
if (
180
+
! $this->settings->is_enabled()
181
+
&&
182
+
wp_next_scheduled( 'rocket_preload_process_pending' )
183
+
) {
184
+
wp_clear_scheduled_hook( 'rocket_preload_process_pending' );
185
+
186
+
return;
187
+
}
188
+
189
+
if ( ! $this->settings->is_enabled() ) {
190
+
return;
191
+
}
192
+
193
+
if ( wp_next_scheduled( 'rocket_preload_process_pending' ) ) {
194
+
return;
195
+
}
196
+
197
+
wp_schedule_event( time() + MINUTE_IN_SECONDS, 'rocket_preload_process_pending', 'rocket_preload_process_pending' );
198
+
}
199
+
200
+
/**
201
+
* Schedule revert stuck failed row cron.
202
+
*
203
+
* @return void
204
+
*/
205
+
public function schedule_revert_old_failed_rows() {
206
+
if (
207
+
! $this->settings->is_enabled()
208
+
&&
209
+
wp_next_scheduled( 'rocket_preload_revert_old_failed_rows' )
210
+
) {
211
+
wp_clear_scheduled_hook( 'rocket_preload_revert_old_failed_rows' );
212
+
213
+
return;
214
+
}
215
+
216
+
if ( ! $this->settings->is_enabled() ) {
217
+
return;
218
+
}
219
+
220
+
if ( wp_next_scheduled( 'rocket_preload_revert_old_failed_rows' ) ) {
221
+
return;
222
+
}
223
+
224
+
wp_schedule_event( time() + MINUTE_IN_SECONDS, 'rocket_revert_old_failed_rows', 'rocket_preload_revert_old_failed_rows' );
225
+
}
226
+
227
+
/**
228
+
* Remove old urls.
229
+
*
230
+
* @return void
231
+
*/
232
+
public function remove_old_rows() {
233
+
/**
234
+
* Delay before the not accessed row is deleted.
235
+
*
236
+
* @param string $delay delay before the not accessed row is deleted.
237
+
*/
238
+
$delay = (string) apply_filters( 'rocket_preload_delay_delete_non_accessed', '1 month' );
239
+
240
+
$parts = explode( ' ', $delay );
241
+
242
+
if ( '' === $delay || '0' === $delay ) {
243
+
return;
244
+
}
245
+
246
+
$value = 1;
247
+
$unit = 'month';
248
+
249
+
if ( count( $parts ) === 2 && $parts[0] >= 0 ) {
250
+
$value = $parts[0];
251
+
$unit = $parts[1];
252
+
}
253
+
254
+
$this->query->remove_all_not_accessed_rows( $value, $unit );
255
+
}
256
+
257
+
/**
258
+
* Remove old failed urls.
259
+
*
260
+
* @return void
261
+
*/
262
+
public function revert_old_failed_rows() {
263
+
$this->query->revert_old_failed();
264
+
}
265
+
}
266
+