-
Notifications
You must be signed in to change notification settings - Fork 89
/
Copy pathPgCache_ContentGrabber.php
2533 lines (2166 loc) · 71 KB
/
PgCache_ContentGrabber.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
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
/**
* File: PgCache_ConfigLabels.php
*
* @package W3TC
*/
namespace W3TC;
// To support legacy updates with old add-ins.
if ( class_exists( 'PgCache_ContentGrabber' ) ) {
return;
}
/**
* Class PgCache_ContentGrabber
*
* phpcs:disable PSR2.Classes.PropertyDeclaration.Underscore
* phpcs:disable PSR2.Methods.MethodDeclaration.Underscore
* phpcs:disable WordPress.PHP.NoSilencedErrors.Discouraged
* phpcs:disable WordPress.WP.AlternativeFunctions
* phpcs:disable WordPress.Security.ValidatedSanitizedInput
*/
class PgCache_ContentGrabber {
/**
* Advanced cache config
*
* @var Config
*/
protected $_config = null;
/**
* Mobile object
*
* @var W3_Mobile
*/
protected $_mobile = null;
/**
* Referrer object
*
* @var W3_Referrer
*/
protected $_referrer = null;
/**
* Caching flag
*
* @var boolean
*/
private $_caching = false;
/**
* Time start
*
* @var double
*/
private $_time_start = 0;
/**
* Lifetime
*
* @var integer
*/
private $_lifetime = 0;
/**
* Enhanced mode flag
*
* @var boolean
*/
private $_enhanced_mode = false;
/**
* Debug flag
*
* @var boolean
*/
private $_debug = false;
/**
* Request URI
*
* @var string
*/
private $_request_uri;
/**
* Request URL fragments
*
* Filled by _preprocess_request_uri
* - ['host' => 'path' => , 'querystring' => ]
*
* @var array
*/
private $_request_url_fragments;
/**
* Page key
*
* @var string
*/
private $_page_key = '';
/**
* Page key extension
*
* @var string
*/
private $_page_key_extension;
/**
* Shutdown buffer
*
* @var string
*/
private $_shutdown_buffer = '';
/**
* Cache reject reason
*
* @var string
*/
private $cache_reject_reason = '';
/**
* Process status
*
* @var string
*/
private $process_status = '';
/**
* Output size
*
* @var int
*/
private $output_size = 0;
/**
* Late init flag
*
* @var bool If cached page should be displayed after init
*/
private $_late_init = false;
/**
* Late caching flag
*
* @var bool late caching
*/
private $_late_caching = false;
/**
* Cached data
*
* @var array
*/
private $_cached_data = null;
/**
* Old exists flag
*
* @var bool
*/
private $_old_exists = false;
/**
* Nginx/Memcached flag
*
* @var bool Nginx memcached flag
*/
private $_nginx_memcached = false;
/**
* Page group
*
* @var string
*/
private $_page_group;
/**
* Constructs the PgCache_ContentGrabber instance.
*
* Initializes configuration, debug settings, and request URL fragments.
*
* @return void
*/
public function __construct() {
$this->_config = Dispatcher::config();
$this->_debug = $this->_config->get_boolean( 'pgcache.debug' );
$this->_request_url_fragments = array(
'host' => Util_Environment::host_port(),
);
$this->_request_uri = isset( $_SERVER['REQUEST_URI'] ) ? filter_var( $_SERVER['REQUEST_URI'], FILTER_SANITIZE_URL ) : '';
$this->_lifetime = $this->_config->get_integer( 'pgcache.lifetime' );
$this->_late_init = $this->_config->get_boolean( 'pgcache.late_init' );
$this->_late_caching = $this->_config->get_boolean( 'pgcache.late_caching' );
$engine = $this->_config->get_string( 'pgcache.engine' );
$this->_enhanced_mode = 'file_generic' === $engine;
$this->_nginx_memcached = 'nginx_memcached' === $engine;
if ( $this->_config->get_boolean( 'mobile.enabled' ) ) {
$this->_mobile = Dispatcher::component( 'Mobile_UserAgent' );
}
if ( $this->_config->get_boolean( 'referrer.enabled' ) ) {
$this->_referrer = Dispatcher::component( 'Mobile_Referrer' );
}
}
/**
* Processes the page cache logic.
*
* Handles caching based on conditions and outputs the cached or generated content.
*
* @return void
*/
public function process() {
$this->run_extensions_dropin();
// Skip caching for some pages.
switch ( true ) {
case defined( 'DONOTCACHEPAGE' ):
$this->process_status = 'miss_third_party';
$this->cache_reject_reason = 'DONOTCACHEPAGE defined';
if ( $this->_debug ) {
self::log( 'skip processing because of DONOTCACHEPAGE constant' );
}
return;
case defined( 'DOING_AJAX' ):
$this->process_status = 'miss_ajax';
$this->cache_reject_reason = 'AJAX request';
if ( $this->_debug ) {
self::log( 'skip processing because of AJAX constant' );
}
return;
case defined( 'APP_REQUEST' ):
case defined( 'XMLRPC_REQUEST' ):
$this->cache_reject_reason = 'API call constant defined';
$this->process_status = 'miss_api_call';
if ( $this->_debug ) {
self::log( 'skip processing because of API call constant' );
}
return;
case defined( 'DOING_CRON' ):
case defined( 'WP_ADMIN' ):
case ( defined( 'SHORTINIT' ) && SHORTINIT ):
$this->cache_reject_reason = 'WP_ADMIN defined';
$this->process_status = 'miss_wp_admin';
if ( $this->_debug ) {
self::log( 'skip processing because of generic constant' );
}
return;
}
// Do page cache logic.
if ( $this->_debug ) {
$this->_time_start = Util_Debug::microtime();
}
// TODO: call modifies object state, rename method at least.
$this->_caching = $this->_can_read_cache();
global $w3_late_init;
if ( $this->_debug ) {
self::log( 'start, can_cache: ' . ( $this->_caching ? 'true' : 'false' ) . ', reject reason: ' . $this->cache_reject_reason );
}
$this->_page_key_extension = $this->_get_key_extension();
if ( ! $this->_page_key_extension['cache'] ) {
$this->_caching = false;
$this->cache_reject_reason = $this->_page_key_extension['cache_reject_reason'];
}
if ( ! empty( $_SERVER['HTTP_W3TCALWAYSCACHED'] ) ) {
$this->_page_key_extension['alwayscached'] = true;
}
if ( $this->_caching && ! $this->_late_caching ) {
$this->_cached_data = $this->_extract_cached_page( false );
if ( $this->_cached_data ) {
if ( $this->_late_init ) {
$w3_late_init = true;
return;
} else {
$this->process_status = 'hit';
$this->process_cached_page_and_exit( $this->_cached_data );
// if is passes here - exit is not possible now and will happen on init.
return;
}
} else {
$this->_late_init = false;
}
} else {
$this->_late_init = false;
}
$w3_late_init = $this->_late_init;
// Start output buffering.
Util_Bus::add_ob_callback( 'pagecache', array( $this, 'ob_callback' ) );
}
/**
* Executes the extensions drop-in logic.
*
* Includes active extensions defined in the configuration.
*
* @return void
*/
private function run_extensions_dropin() {
$c = $this->_config;
$extensions = $c->get_array( 'extensions.active' );
$dropin = $c->get_array( 'extensions.active_dropin' );
foreach ( $dropin as $extension => $nothing ) {
if ( isset( $extensions[ $extension ] ) ) {
$path = $extensions[ $extension ];
$filename = W3TC_EXTENSION_DIR . '/' .
str_replace( '..', '', trim( $path, '/' ) );
if ( file_exists( $filename ) ) {
include_once $filename;
}
}
}
}
/**
* Extracts a cached page from storage.
*
* @param bool $with_filter Whether to apply filters to the cache keys.
*
* @return array|null An array of cached page data or null if not found.
*/
public function _extract_cached_page( $with_filter ) {
if ( ! empty( $this->_page_key_extension['alwayscached'] ) ) {
return null;
}
$cache = $this->_get_cache( $this->_page_key_extension['group'] );
$mobile_group = $this->_page_key_extension['useragent'];
$referrer_group = $this->_page_key_extension['referrer'];
$encryption = $this->_page_key_extension['encryption'];
$compression = $this->_page_key_extension['compression'];
// Check if page is cached.
if ( ! $this->_set_extract_page_key( $this->_page_key_extension, $with_filter ) ) {
$data = null;
} else {
$data = $cache->get_with_old( $this->_page_key, $this->_page_group );
list( $data, $this->_old_exists ) = $data;
}
// Try to get uncompressed version of cache.
if ( $compression && ! $data ) {
if (
! $this->_set_extract_page_key(
array_merge(
$this->_page_key_extension,
array( 'compression' => '' )
),
$with_filter
)
) {
$data = null;
} else {
$data = $cache->get_with_old( $this->_page_key, $this->_page_group );
list( $data, $this->_old_exists ) = $data;
$compression = false;
}
}
if ( ! $data ) {
if ( $this->_debug ) {
self::log( 'no cache entry for ' . $this->_request_url_fragments['host'] . $this->_request_uri . ' ' . $this->_page_key );
}
return null;
}
$data['compression'] = $compression;
return $data;
}
/**
* Sets the page key and group for cache extraction.
*
* @param array $page_key_extension {
* Cache key extension data.
*
* @type string $group The cache group.
* @type string $useragent The user agent string.
* @type string $referrer The referrer URL.
* @type string $encryption Encryption type.
* @type string $compression Compression type.
* @type string $content_type The content type.
* }
* @param bool $with_filter Whether to apply filters to the cache keys.
*
* @return bool True if the page key was set successfully, false otherwise.
*/
private function _set_extract_page_key( $page_key_extension, $with_filter ) {
// set page group.
$this->_page_group = $page_key_extension['group'];
if ( $with_filter ) {
// return empty value if caching should not happen.
$this->_page_group = apply_filters(
'w3tc_page_extract_group',
$page_key_extension['group'],
$this->_request_url_fragments['host'] . $this->_request_uri,
$page_key_extension
);
$page_key_extension['group'] = $this->_page_group;
}
// set page key.
$this->_page_key = $this->_get_page_key( $page_key_extension );
if ( $with_filter ) {
// return empty value if caching should not happen.
$this->_page_key = apply_filters(
'w3tc_page_extract_key',
$this->_page_key,
$page_key_extension['useragent'],
$page_key_extension['referrer'],
$page_key_extension['encryption'],
$page_key_extension['compression'],
$page_key_extension['content_type'],
$this->_request_url_fragments['host'] . $this->_request_uri,
$page_key_extension
);
}
if ( ! empty( $this->_page_key ) ) {
return true;
}
$this->caching = false;
$this->cache_reject_reason = 'w3tc_page_extract_key filter result forced not to cache';
return false;
}
/**
* Processes the cached page and terminates execution.
*
* @param array $data {
* Cached page data.
*
* @type bool $404 Whether the page is a 404 response. Defaults to false.
* @type array $headers Headers to be sent with the response.
* @type string $content Cached page content.
* @type bool $has_dynamic Whether the page contains dynamic content.
* @type int $time Timestamp of when the page was cached.
* @type string $compression Compression type used for the cached page.
* }
*
* @return void
*/
private function process_cached_page_and_exit( $data ) {
// Do Bad Behavior check.
$this->_bad_behavior();
$is_404 = isset( $data['404'] ) ? $data['404'] : false;
$headers = isset( $data['headers'] ) ? $data['headers'] : array();
$content = $data['content'];
$has_dynamic = isset( $data['has_dynamic'] ) && $data['has_dynamic'];
$etag = md5( $content );
if ( $has_dynamic ) {
// its last modification date is now, and any compression browser wants cant be used, since its compressed now.
$time = time();
$compression = $this->_page_key_extension['compression'];
} else {
$time = isset( $data['time'] ) ? $data['time'] : time();
$compression = $data['compression'];
}
// Send headers.
$this->_send_headers( $is_404, $time, $etag, $compression, $headers );
if ( isset( $_SERVER['REQUEST_METHOD'] ) && 'HEAD' === $_SERVER['REQUEST_METHOD'] ) {
return;
}
// parse dynamic content and compress if it's dynamic page with mfuncs.
if ( $has_dynamic ) {
$content = $this->_parse_dynamic( $content );
$content = $this->_compress( $content, $compression );
}
echo $content; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
Dispatcher::usage_statistics_apply_before_init_and_exit(
array(
$this,
'w3tc_usage_statistics_of_request',
)
);
}
/**
* Output buffering callback for caching.
*
* @param string $buffer The current output buffer.
*
* @return string Processed buffer to be output.
*/
public function ob_callback( $buffer ) {
$this->output_size = strlen( $buffer );
if ( ! $this->_is_cacheable_content_type() ) {
if ( $this->_debug ) {
self::log( 'storing cached page - not a cached content' );
}
return $buffer;
}
$compression = false;
$has_dynamic = $this->_has_dynamic( $buffer );
$response_headers = $this->_get_response_headers();
// TODO: call modifies object state, rename method at least.
$original_can_cache = $this->_can_write_cache( $buffer, $response_headers );
$can_cache = apply_filters( 'w3tc_can_cache', $original_can_cache, $this, $buffer );
if ( $can_cache !== $original_can_cache ) {
$this->cache_reject_reason = 'Third-party plugin has modified caching activity';
}
if ( $this->_debug ) {
self::log(
'storing cached page: ' . ( $can_cache ? 'true' : 'false' ) . ' original ' .
( $this->_caching ? ' true' : 'false' ) . ' reason ' . $this->cache_reject_reason
);
}
$buffer = str_replace(
'{w3tc_pagecache_reject_reason}',
( '' !== $this->cache_reject_reason ? sprintf( ' (%s)', $this->cache_reject_reason ) : '' ),
$buffer
);
if ( $can_cache ) {
$buffer = $this->_maybe_save_cached_result( $buffer, $response_headers, $has_dynamic );
} else {
if ( $has_dynamic ) {
// send common headers since output will be compressed.
$compression_header = $this->_page_key_extension['compression'];
if ( defined( 'W3TC_PAGECACHE_OUTPUT_COMPRESSION_OFF' ) ) {
$compression_header = false;
}
$headers = $this->_get_common_headers( $compression_header );
$this->_headers( $headers );
}
// remove cached entries if its not cached anymore.
if ( $this->cache_reject_reason ) {
if ( $this->_old_exists ) {
$cache = $this->_get_cache( $this->_page_key_extension['group'] );
$compressions_to_store = $this->_get_compressions();
foreach ( $compressions_to_store as $_compression ) {
$_page_key = $this->_get_page_key(
array_merge(
$this->_page_key_extension,
array( 'compression' => $_compression )
)
);
$cache->hard_delete( $_page_key );
}
}
}
}
// We can't capture output in ob_callback so we use shutdown function.
if ( $has_dynamic ) {
$this->_shutdown_buffer = $buffer;
$buffer = '';
register_shutdown_function(
array(
$this,
'shutdown',
)
);
}
return $buffer;
}
/**
* Handles the shutdown process for compressing and outputting the page buffer.
*
* @return void
*/
public function shutdown() {
$compression = $this->_page_key_extension['compression'];
// Parse dynamic content.
$buffer = $this->_parse_dynamic( $this->_shutdown_buffer );
// Compress page according to headers already set.
$compressed_buffer = $this->_compress( $buffer, $compression );
echo $compressed_buffer; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
}
/**
* Determines if the cache can be read for the current request.
*
* @return bool True if cache can be read, false otherwise.
*/
private function _can_read_cache() {
// Don't cache in console mode.
if ( PHP_SAPI === 'cli' ) {
$this->cache_reject_reason = 'Console mode';
return false;
}
// Skip if session defined.
if ( defined( 'SID' ) && ! empty( SID ) ) {
$this->cache_reject_reason = 'Session started';
return false;
}
if ( ! $this->_config->get_boolean( 'pgcache.cache.ssl' ) && Util_Environment::is_https() ) {
$this->cache_reject_reason = 'SSL caching disabled';
$this->process_status = 'miss_configuration';
return false;
}
// Skip if posting.
$request_method = isset( $_SERVER['REQUEST_METHOD'] ) ? htmlspecialchars( stripslashes( $_SERVER['REQUEST_METHOD'] ) ) : '';
if ( in_array( strtoupper( $request_method ), array( 'DELETE', 'PUT', 'OPTIONS', 'TRACE', 'CONNECT', 'POST' ), true ) ) {
$this->cache_reject_reason = sprintf( 'Requested method is %s', $request_method );
return false;
}
// Skip if HEAD request..
if (
isset( $_SERVER['REQUEST_METHOD'] ) &&
strtoupper( $_SERVER['REQUEST_METHOD'] ) === 'HEAD' &&
( $this->_enhanced_mode || $this->_config->get_boolean( 'pgcache.reject.request_head' ) )
) {
$this->cache_reject_reason = 'Requested method is HEAD';
return false;
}
// Skip if there is query in the request uri.
$this->_preprocess_request_uri();
if ( ! empty( $this->_request_url_fragments['querystring'] ) ) {
$should_reject_qs = (
! $this->_config->get_boolean( 'pgcache.cache.query' ) ||
'file_generic' === $this->_config->get_string( 'pgcache.engine' )
);
if (
$should_reject_qs &&
'cache' === $this->_config->get_string( 'pgcache.rest' ) &&
Util_Environment::is_rest_request( $this->_request_uri )
) {
$should_reject_qs = false;
}
if ( $should_reject_qs ) {
$this->cache_reject_reason = 'Requested URI contains query';
$this->process_status = 'miss_query_string';
return false;
}
}
// Check request URI.
if ( ! $this->_passed_accept_files() && ! $this->_passed_reject_uri() ) {
$this->cache_reject_reason = 'Requested URI is rejected';
$this->process_status = 'miss_configuration';
return false;
}
// Check User Agent.
if ( ! $this->_check_ua() ) {
$this->cache_reject_reason = 'User agent is rejected';
if ( ! empty( Util_Request::get_string( 'w3tc_rewrite_test' ) ) ) {
// special common case - w3tc_rewrite_test check request.
$this->process_status = 'miss_wp_admin';
} else {
$this->process_status = 'miss_configuration';
}
return false;
}
// Check WordPress cookies.
if ( ! $this->_check_cookies() ) {
$this->cache_reject_reason = 'Cookie is rejected';
$this->process_status = 'miss_configuration';
return false;
}
// Skip if user is logged in or user role is logged in.
if ( $this->_config->get_boolean( 'pgcache.reject.logged' ) ) {
if ( ! $this->_check_logged_in() ) {
$this->cache_reject_reason = 'User is logged in';
$this->process_status = 'miss_logged_in';
return false;
}
} elseif ( ! $this->_check_logged_in_role_allowed() ) {
$this->cache_reject_reason = 'Rejected user role is logged in';
$this->process_status = 'miss_logged_in';
return false;
}
return true;
}
/**
* Determines if the cache can be written for the given buffer and response headers.
*
* @param string $buffer The content buffer to potentially cache.
* @param array $response_headers {
* Response headers from the current request.
*
* @type array $kv Key-value pairs of response headers.
* }
*
* @return bool True if cache can be written, false otherwise.
*/
private function _can_write_cache( $buffer, $response_headers ) {
// Skip if caching is disabled.
if ( ! $this->_caching ) {
return false;
}
// Check for DONOTCACHEPAGE constant.
if ( defined( 'DONOTCACHEPAGE' ) && DONOTCACHEPAGE ) {
$this->cache_reject_reason = 'DONOTCACHEPAGE constant is defined';
$this->process_status = 'miss_third_party';
return false;
}
if ( 'cache' !== $this->_config->get_string( 'pgcache.rest' ) ) {
if ( defined( 'REST_REQUEST' ) && REST_REQUEST ) {
$this->cache_reject_reason = 'REST request';
$this->process_status = 'miss_api_call';
return false;
}
}
// Don't cache 404 pages.
if ( ! $this->_config->get_boolean( 'pgcache.cache.404' ) && function_exists( 'is_404' ) && is_404() ) {
$this->cache_reject_reason = 'Page is 404';
$this->process_status = 'miss_404';
return false;
}
// Don't cache homepage.
if ( ! $this->_config->get_boolean( 'pgcache.cache.home' ) && function_exists( 'is_home' ) && is_home() ) {
$this->cache_reject_reason = is_front_page() && is_home() ? 'Page is front page' : 'Page is posts page';
$this->process_status = 'miss_configuration';
return false;
}
// Don't cache front page.
if ( $this->_config->get_boolean( 'pgcache.reject.front_page' ) && function_exists( 'is_front_page' ) && is_front_page() && ! is_home() ) {
$this->cache_reject_reason = 'Page is front page';
$this->process_status = 'miss_configuration';
return false;
}
// Don't cache feed.
if ( ! $this->_config->get_boolean( 'pgcache.cache.feed' ) && function_exists( 'is_feed' ) && is_feed() ) {
$this->cache_reject_reason = 'Page is feed';
$this->process_status = 'miss_configuration';
return false;
}
// Check if page contains dynamic tags.
if ( $this->_enhanced_mode && $this->_has_dynamic( $buffer ) ) {
$this->cache_reject_reason = 'Page contains dynamic tags (mfunc or mclude) can not be cached in enhanced mode';
$this->process_status = 'miss_mfunc';
return false;
}
if ( ! $this->_passed_accept_files() ) {
if ( is_single() ) {
// Don't cache pages associated with categories.
if ( $this->_passed_reject_categories() ) {
$this->cache_reject_reason = 'Page associated with a rejected category';
$this->process_status = 'miss_configuration';
return false;
}
// Don't cache pages that use tags.
if ( $this->_passed_reject_tags() ) {
$this->cache_reject_reason = 'Page using a rejected tag';
$this->process_status = 'miss_configuration';
return false;
}
}
// Don't cache pages by these authors.
if ( $this->_passed_reject_authors() ) {
$this->cache_reject_reason = 'Page written by a rejected author';
$this->process_status = 'miss_configuration';
return false;
}
// Don't cache pages using custom fields.
if ( $this->_passed_reject_custom_fields() ) {
$this->cache_reject_reason = 'Page using a rejected custom field';
$this->process_status = 'miss_configuration';
return false;
}
}
if ( ! empty( $response_headers['kv']['content-encoding'] ) ) {
$this->cache_reject_reason = 'Response is compressed';
$this->process_status = 'miss_compressed';
return false;
}
if ( empty( $buffer ) && empty( $response_headers['kv']['location'] ) ) {
$this->cache_reject_reason = 'Empty response';
$this->process_status = 'miss_empty_response';
return false;
}
if ( isset( $response_headers['kv']['location'] ) ) {
// dont cache query-string normalization redirects (e.g. from wp core) when cache key is normalized,
// since that cause redirect loop.
if (
$this->_get_page_key( $this->_page_key_extension ) === $this->_get_page_key(
$this->_page_key_extension,
$response_headers['kv']['location']
)
) {
$this->cache_reject_reason = 'Normalization redirect';
$this->process_status = 'miss_normalization_redirect';
return false;
}
}
if ( isset( $_SERVER['REQUEST_METHOD'] ) && 'HEAD' === $_SERVER['REQUEST_METHOD'] ) {
$this->cache_reject_reason = 'HEAD request';
$this->process_status = 'miss_request_method';
return;
}
return true;
}
/**
* Retrieves the size of cache statistics.
*
* @param int $timeout_time Timeout in seconds for the operation.
*
* @return int|null The size of the cache statistics, or null if unavailable.
*/
public function get_cache_stats_size( $timeout_time ) {
$cache = $this->_get_cache();
if ( method_exists( $cache, 'get_stats_size' ) ) {
return $cache->get_stats_size( $timeout_time );
}
return null;
}
/**
* Retrieves usage statistics and configuration for the cache engine.
*
* @return array Configuration data for the cache engine.
*/
public function get_usage_statistics_cache_config() {
$engine = $this->_config->get_string( 'pgcache.engine' );
switch ( $engine ) {
case 'memcached':
case 'nginx_memcached':
$engine_config = array(
'servers' => $this->_config->get_array( 'pgcache.memcached.servers' ),
'persistent' => $this->_config->get_boolean( 'pgcache.memcached.persistent' ),
'aws_autodiscovery' => $this->_config->get_boolean( 'pgcache.memcached.aws_autodiscovery' ),
'username' => $this->_config->get_string( 'pgcache.memcached.username' ),
'password' => $this->_config->get_string( 'pgcache.memcached.password' ),
'binary_protocol' => $this->_config->get_boolean( 'pgcache.memcached.binary_protocol' ),
);
break;
case 'redis':
$engine_config = array(
'servers' => $this->_config->get_array( 'pgcache.redis.servers' ),
'verify_tls_certificates' => $this->_config->get_boolean( 'pgcache.redis.verify_tls_certificates' ),
'persistent' => $this->_config->get_boolean( 'pgcache.redis.persistent' ),
'timeout' => $this->_config->get_integer( 'pgcache.redis.timeout' ),
'retry_interval' => $this->_config->get_integer( 'pgcache.redis.retry_interval' ),
'read_timeout' => $this->_config->get_integer( 'pgcache.redis.read_timeout' ),
'dbid' => $this->_config->get_integer( 'pgcache.redis.dbid' ),
'password' => $this->_config->get_string( 'pgcache.redis.password' ),
);
break;
case 'file_generic':
$engine = 'file';
break;
default:
$engine_config = array();
}
$engine_config['engine'] = $engine;
return $engine_config;
}
/**
* Retrieves the cache instance for a specific group.
*
* @param string $group Cache group name. Defaults to '*'.
*
* @return mixed Cache instance.
*/
public function _get_cache( $group = '*' ) {
static $caches = array();
if ( empty( $group ) ) {
$group = '*';
}
if ( empty( $caches[ $group ] ) ) {
$engine = $this->_config->get_string( 'pgcache.engine' );
switch ( $engine ) {
case 'memcached':
case 'nginx_memcached':
$engine_config = array(
'servers' => $this->_config->get_array( 'pgcache.memcached.servers' ),
'persistent' => $this->_config->get_boolean( 'pgcache.memcached.persistent' ),
'aws_autodiscovery' => $this->_config->get_boolean( 'pgcache.memcached.aws_autodiscovery' ),
'username' => $this->_config->get_string( 'pgcache.memcached.username' ),
'password' => $this->_config->get_string( 'pgcache.memcached.password' ),
'binary_protocol' => $this->_config->get_boolean( 'pgcache.memcached.binary_protocol' ),
'host' => Util_Environment::host(),
);
break;
case 'redis':
$engine_config = array(
'servers' => $this->_config->get_array( 'pgcache.redis.servers' ),
'verify_tls_certificates' => $this->_config->get_boolean( 'pgcache.redis.verify_tls_certificates' ),
'persistent' => $this->_config->get_boolean( 'pgcache.redis.persistent' ),
'timeout' => $this->_config->get_integer( 'pgcache.redis.timeout' ),
'retry_interval' => $this->_config->get_integer( 'pgcache.redis.retry_interval' ),
'read_timeout' => $this->_config->get_integer( 'pgcache.redis.read_timeout' ),
'dbid' => $this->_config->get_integer( 'pgcache.redis.dbid' ),
'password' => $this->_config->get_string( 'pgcache.redis.password' ),
);
break;
case 'file':
$engine_config = array(
'section' => 'page',
'flush_parent' => ( Util_Environment::blog_id() === 0 ),
'locking' => $this->_config->get_boolean( 'pgcache.file.locking' ),
'flush_timelimit' => $this->_config->get_integer( 'timelimit.cache_flush' ),
);
break;
case 'file_generic':
if ( '*' !== $group ) {
$engine = 'file';
$engine_config = array(
'section' => 'page',
'cache_dir' => W3TC_CACHE_PAGE_ENHANCED_DIR . DIRECTORY_SEPARATOR . Util_Environment::host_port(),
'flush_parent' => ( Util_Environment::blog_id() === 0 ),
'locking' => $this->_config->get_boolean( 'pgcache.file.locking' ),
'flush_timelimit' => $this->_config->get_integer( 'timelimit.cache_flush' ),
);
break;
}
if ( 0 === Util_Environment::blog_id() ) {
$flush_dir = W3TC_CACHE_PAGE_ENHANCED_DIR;