-
Notifications
You must be signed in to change notification settings - Fork 89
/
Copy pathExtension_AlwaysCached_Plugin.php
440 lines (385 loc) · 12.1 KB
/
Extension_AlwaysCached_Plugin.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
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
<?php
/**
* File: Extension_AlwaysCached_Plugin.php
*
* AlwaysCached plugin admin controller.
*
* @since 2.8.0
*
* @package W3TC
*/
namespace W3TC;
/**
* AlwaysCached Plugin.
*
* @since 2.8.0
*/
class Extension_AlwaysCached_Plugin {
/**
* Ahead generation extension data.
*
* @var array
*/
private $request_queue_item_extension = null;
/**
* Run method for AlwaysCached.
*
* @since 2.8.0
*
* @return void|null
*/
public function run() {
if ( ! self::is_enabled() ) {
return null;
}
add_action( 'init', array( $this, 'init' ) );
add_filter( 'w3tc_admin_bar_menu', array( $this, 'w3tc_admin_bar_menu' ) );
add_action( 'w3tc_pagecache_before_set', array( $this, 'w3tc_pagecache_before_set' ) );
add_filter( 'w3tc_pagecache_set', array( $this, 'w3tc_pagecache_set' ) );
add_filter( 'w3tc_pagecache_flush_url', array( $this, 'w3tc_pagecache_flush_url' ), 1000 );
add_filter( 'w3tc_pagecache_flush_all_groups', array( $this, 'w3tc_pagecache_flush_all_groups' ), 1000 );
add_filter( 'w3tc_pagecache_rules_apache_rewrite_cond', array( $this, 'w3tc_pagecache_rules_apache_rewrite_cond' ) );
// Cron job.
add_action( 'w3tc_alwayscached_wp_cron', array( $this, 'w3tc_alwayscached_wp_cron' ) );
/**
* This filter is documented in Generic_AdminActions_Default.php under the read_request method.
*/
add_filter( 'w3tc_config_key_descriptor', array( $this, 'w3tc_config_key_descriptor' ), 10, 2 );
}
/**
* Init for AlwaysCached.
*
* @since 2.8.0
*
* @return void
*/
public function init() {
$c = Dispatcher::config();
// phpcs:ignore WordPress.Security.NonceVerification.Recommended
if ( isset( $_REQUEST['w3tc_alwayscached'] ) ) {
Extension_AlwaysCached_Worker::run();
wp_die();
}
$enabled = $c->get_boolean( array( 'alwayscached', 'wp_cron' ) );
$time = $c->get_string( array( 'alwayscached', 'wp_cron_time' ) );
$interval = $c->get_string( array( 'alwayscached', 'wp_cron_interval' ) );
// Retrieve stored previous time and interval.
$prev_time = get_option( 'w3tc_alwayscached_wp_cron_time', '' );
$prev_interval = get_option( 'w3tc_alwayscached_wp_cron_interval', '' );
// Check if cron needs updating or scheduling.
if ( $enabled && ! empty( $time ) && ! empty( $interval ) ) {
// If no event is scheduled or the time/interval have changed, update the cron.
if ( ! wp_next_scheduled( 'w3tc_alwayscached_wp_cron' ) || $time !== $prev_time || $interval !== $prev_interval ) {
// Clear existing scheduled event.
wp_clear_scheduled_hook( 'w3tc_alwayscached_wp_cron' );
// Convert the time to a timestamp for scheduling.
$start_time = Util_Environment::get_cron_schedule_time( $time );
// Schedule the new event.
wp_schedule_event( $start_time, $interval, 'w3tc_alwayscached_wp_cron' );
// Store the new time and interval.
update_option( 'w3tc_alwayscached_wp_cron_time', $time );
update_option( 'w3tc_alwayscached_wp_cron_interval', $interval );
}
} elseif ( ! $enabled ) {
// Clear the cron job if it's disabled.
wp_clear_scheduled_hook( 'w3tc_alwayscached_wp_cron' );
// Remove the stored values.
delete_option( 'w3tc_alwayscached_wp_cron_time' );
delete_option( 'w3tc_alwayscached_wp_cron_interval' );
}
}
/**
* Adds admin bar menu links.
*
* @since 2.8.0
*
* @param array $menu_items Menu items.
*
* @return array
*/
public function w3tc_admin_bar_menu( $menu_items ) {
if ( ! is_admin() ) {
$menu_items['10025.always_cached'] = array(
'id' => 'w3tc_flush_current_page',
'parent' => 'w3tc',
'title' => __( 'Regenerate Current Page', 'w3-total-cache' ),
'href' => wp_nonce_url(
admin_url( 'admin.php?page=w3tc_dashboard&w3tc_alwayscached_regenerate&post_id=' . Util_Environment::detect_post_id() ),
'w3tc'
),
);
}
return $menu_items;
}
/**
* Adds AlwaysCached Apache rules.
*
* @since 2.8.0
*
* @param string $rewrite_conditions Apache rules buffer.
*
* @return string
*/
public function w3tc_pagecache_rules_apache_rewrite_cond( $rewrite_conditions ) {
$rewrite_conditions .= " RewriteCond %{HTTP:w3tcalwayscached} =\"\"\n";
return $rewrite_conditions;
}
/**
* ???
*
* @since 2.8.0
*
* @param array $o Page data.
*
* @return void
*/
public function w3tc_pagecache_before_set( $o ) {
if ( empty( $o['page_key_extension']['alwayscached'] ) ) {
return;
}
$url = ( empty( $o['page_key_extension']['encryption'] ) ? 'http://' : 'https://' ) .
$o['request_url_fragments']['host'] .
$o['request_url_fragments']['path'] .
$o['request_url_fragments']['querystring'];
$queue_item = Extension_AlwaysCached_Queue::get_by_url( $url );
if ( ! empty( $queue_item ) ) {
$this->request_queue_item_extension = @unserialize( $queue_item['extension'] ); // phpcs:ignore WordPress.PHP.NoSilencedErrors.Discouraged, WordPress.PHP.DiscouragedPHPFunctions.serialize_unserialize
header( 'w3tcalwayscached: ' . ( empty( $queue_item ) ? 'none' : $queue_item['key'] ) );
}
}
/**
* ???
*
* @since 2.8.0
*
* @param array $data Page data.
*
* @return array
*/
public function w3tc_pagecache_set( $data ) {
// in a case of alwayscached-regeneration request - apply cache's "ahead generation extension" data.
if ( ! empty( $this->request_queue_item_extension ) ) {
$keys_to_store = array( 'key_version', 'key_version_at_creation' );
foreach ( $keys_to_store as $k ) {
if ( isset( $this->request_queue_item_extension[ $k ] ) ) {
$data[ $k ] = $this->request_queue_item_extension[ $k ];
}
}
}
return $data;
}
/**
* Flush URL.
*
* Data format expected:
* array(
* 'url' =>
* 'cache' =>
* 'mobile_groups' =>
* 'referrer_groups' =>
* 'cookies' =>
* 'encryptions' =>
* 'compressions' =>
* 'group' =>
* 'parent' => object with _get_page_key method
* )
*
* @since 2.8.0
*
* @param array $data Data for flush request.
*
* @return array
*/
public function w3tc_pagecache_flush_url( $data ) {
// no support for mobile_groups, referrer_groups, cookies, group atm.
foreach ( $data['encryptions'] as $encryption ) {
$page_key_extension = array(
'useragent' => $data['mobile_groups'][0],
'referrer' => $data['referrer_groups'][0],
'cookie' => $data['cookies'][0],
'encryption' => $encryption,
'compression' => false,
'group' => $data['group'],
);
$page_key = $data['parent']->_get_page_key( $page_key_extension, $data['url'] );
// If the URL is excluded, store the data for later flushing.
if ( self::is_excluded( $data['url'] ) ) {
$excluded_data = $data;
continue;
}
// If cache key doesn't exist, skip to the next iteration.
if ( ! $data['cache']->exists( $page_key, $data['group'] ) ) {
continue;
}
// Queue the URL for later processing if it's not excluded and exists in cache.
Extension_AlwaysCached_Queue::add(
$data['url'],
array( 'group' => $data['group'] )
);
}
// Return the excluded URLs if any were found, so they can be flushed.
if ( ! empty( $excluded_data ) ) {
return $excluded_data;
}
return array();
}
/**
* Flush all groups.
*
* @since 2.8.0
*
* @param array $groups Groups.
*
* @return array
*/
public function w3tc_pagecache_flush_all_groups( $groups ) {
$c = Dispatcher::config();
$excluded_data = array();
// Flush all action will purge the queue as any queued changes will now be live.
if ( ! $c->get_boolean( array( 'alwayscached', 'flush_all' ) ) ) {
Extension_AlwaysCached_Queue::empty();
return $groups;
}
if ( in_array( '', $groups, true ) && $c->get_boolean( array( 'alwayscached', 'flush_all' ) ) ) {
$o = Dispatcher::component( 'PgCache_Flush' );
$extension = $o->get_ahead_generation_extension( '' );
$no_cache_vals = array( 'no-cache', 'no-store', 'must-revalidate', 'private' );
if ( $c->get_boolean( array( 'alwayscached', 'flush_all_home' ) ) ) {
$home_url = rtrim( home_url(), '/' ) . '/';
$response_headers = wp_remote_head( $home_url );
if ( ! is_wp_error( $response_headers ) ) {
$cache_control_vals = array_map( 'trim', explode( ',', wp_remote_retrieve_header( $response_headers, 'Cache-Control' ) ) );
if ( ! self::is_excluded( $home_url ) && ! array_intersect( $no_cache_vals, $cache_control_vals ) ) {
Extension_AlwaysCached_Queue::add( $home_url, $extension );
} else {
$o->flush_url( $home_url );
}
}
}
$posts_count = $c->get_integer( array( 'alwayscached', 'flush_all_posts_count' ) ) ?? 15;
if ( $posts_count > 0 ) {
$posts = get_posts(
array(
'post_type' => 'post',
'post_status' => 'publish',
'posts_per_page' => $posts_count,
'order' => 'DESC',
'orderby' => 'modified',
)
);
foreach ( $posts as $post ) {
$permalink = get_permalink( $post );
$response_headers = wp_remote_head( $permalink );
if ( is_wp_error( $response_headers ) ) {
continue;
}
$cache_control_vals = array_map( 'trim', explode( ',', wp_remote_retrieve_header( $response_headers, 'Cache-Control' ) ) );
if ( array_intersect( $no_cache_vals, $cache_control_vals ) ) {
continue;
}
if ( ! self::is_excluded( $permalink ) ) {
Extension_AlwaysCached_Queue::add( $permalink, $extension );
} else {
$o->flush_url( $permalink );
}
}
}
$pages_count = $c->get_integer( array( 'alwayscached', 'flush_all_pages_count' ) ) ?? 15;
if ( $pages_count > 0 ) {
$posts = get_posts(
array(
'post_type' => 'page',
'post_status' => 'publish',
'posts_per_page' => $pages_count,
'order' => 'DESC',
'orderby' => 'modified',
)
);
foreach ( $posts as $post ) {
$permalink = get_permalink( $post );
$response_headers = wp_remote_head( $permalink );
if ( is_wp_error( $response_headers ) ) {
continue;
}
$cache_control_vals = array_map( 'trim', explode( ',', wp_remote_retrieve_header( $response_headers, 'Cache-Control' ) ) );
if ( array_intersect( $no_cache_vals, $cache_control_vals ) ) {
continue;
}
if ( ! self::is_excluded( $permalink ) ) {
Extension_AlwaysCached_Queue::add( $permalink, $extension );
} else {
$o->flush_url( $permalink );
}
}
}
}
return array();
}
/**
* Gets the enabled status of the extension.
*
* @since 2.8.0
*
* @return bool
*/
public static function is_enabled() {
$config = Dispatcher::config();
$extensions_active = $config->get_array( 'extensions.active' );
return Util_Environment::is_w3tc_pro( $config ) && array_key_exists( 'alwayscached', $extensions_active );
}
/**
* Cron job for processing queue via WP cron.
*
* @since 2.8.0
*
* @return void
*/
public function w3tc_alwayscached_wp_cron() {
Extension_AlwaysCached_Worker::run();
}
/**
* Specify config key typing for fields that need it.
*
* @since 2.8.0
*
* @param mixed $descriptor Descriptor.
* @param mixed $key Compound key array.
*
* @return array
*/
public function w3tc_config_key_descriptor( $descriptor, $key ) {
if ( is_array( $key ) && 'alwayscached.exclusions' === implode( '.', $key ) ) {
$descriptor = array( 'type' => 'array' );
}
return $descriptor;
}
/**
* Checks if the given URL matches any exclusions.
*
* @since 2.8.0
*
* @param string $url URL.
*
* @return bool
*/
private function is_excluded( $url ) {
$c = Dispatcher::config();
$exclusions = $c->get_array( array( 'alwayscached', 'exclusions' ) );
// Normalize the URL to handle trailing slashes and parse the path.
$parsed_url = rtrim( wp_parse_url( $url, PHP_URL_PATH ), '/' );
$url_with_slash = $parsed_url . '/';
foreach ( $exclusions as $exclusion ) {
// Check both with and without trailing slash.
if ( fnmatch( $exclusion, $parsed_url ) || fnmatch( $exclusion, $url_with_slash ) ) {
return true;
}
}
return false;
}
}
$p = new Extension_AlwaysCached_Plugin();
$p->run();
if ( is_admin() ) {
$p = new Extension_AlwaysCached_Plugin_Admin();
$p->run();
}