-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclasses_Config.php.html
More file actions
844 lines (683 loc) · 37.6 KB
/
classes_Config.php.html
File metadata and controls
844 lines (683 loc) · 37.6 KB
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>PoweredCache Hooks: Source: classes/Config.php</title>
<script src="scripts/prettify/prettify.js"> </script>
<script src="scripts/prettify/lang-css.js"> </script>
<!--[if lt IE 9]>
<script src="//html5shiv.googlecode.com/svn/trunk/html5.js"></script>
<![endif]-->
<link type="text/css" rel="stylesheet" href="styles/prettify-tomorrow.css">
<link type="text/css" rel="stylesheet" href="styles/jsdoc-default.css">
</head>
<body>
<div id="main">
<h1 class="page-title">Source: classes/Config.php</h1>
<section>
<article>
<pre class="prettyprint source linenums"><code><?php
/**
* Configurator Class of the plugin
*
* @package PoweredCache
*/
namespace PoweredCache;
use function PoweredCache\Utils\can_configure_htaccess;
use function PoweredCache\Utils\can_configure_object_cache;
use function PoweredCache\Utils\get_cache_dir;
use function PoweredCache\Utils\get_object_cache_dropins;
use function PoweredCache\Utils\mobile_browsers;
use function PoweredCache\Utils\mobile_prefixes;
use function PoweredCache\Utils\permalink_structure_has_trailingslash;
use function PoweredCache\Utils\remove_dir;
if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly.
}
// phpcs:disable Generic.Strings.UnnecessaryStringConcat.Found
// phpcs:disable WordPress.WP.AlternativeFunctions.file_get_contents_file_get_contents
// phpcs:disable WordPress.PHP.DevelopmentFunctions.error_log_var_export
// phpcs:disable WordPress.PHP.NoSilencedErrors.Discouraged
// phpcs:disable WordPress.WP.AlternativeFunctions.file_system_read_file_put_contents
/**
* Class Config
*/
class Config {
/**
* placeholder
*
* @since 1.0
*/
public function __construct() {
}
/**
* Return an instance of the current class
*
* @return Config
* @since 2.0 removed WP_Filesystem_Direct deps
* @since 1.1 initialize WP_Filesystem_Direct
* @since 1.0
*/
public static function factory() {
static $instance = false;
if ( ! $instance ) {
$instance = new self();
}
return $instance;
}
/**
* Setup object-cache.php
*
* @param string $backend Persistent object cache backend. (memcached, redis etc..)
*
* @return bool
* @since 1.0
*/
public function setup_object_cache( $backend = 'off' ) {
$file = untrailingslashit( WP_CONTENT_DIR ) . '/object-cache.php';
/**
* Since object cache has impact on the entire network
* It only allowed by the network admin
*/
if ( is_multisite() && ! current_user_can( 'manage_network' ) ) {
return false;
}
if ( 'off' === $backend && file_exists( $file ) && false !== strpos( file_get_contents( $file ), 'POWERED_OBJECT_CACHE' ) ) {
/**
* Remove object-cache.php file only when the created file belongs to PoweredCache
*/
unlink( $file );
return true;
}
if ( 'off' === $backend ) {
return true;
}
$file_string = $this->object_cache_file_content( $backend );
if ( ! file_put_contents( $file, $file_string, LOCK_EX ) ) {
return false;
}
return true;
}
/**
* object-cache.php contents
*
* @param string $backend Persistent object cache backend
*
* @return mixed|void
* @since 1.0
* @see Powered_Cache_Admin_Helper::object_cache_dropins
*
* @since 1.1 supports `POWERED_CACHE_OBJECT_CACHE_DROPIN`
*/
public function object_cache_file_content( $backend ) {
$string = '<?php ' . "\n";
$string .= "defined( 'ABSPATH' ) || exit;" . PHP_EOL;
$string .= "define( 'POWERED_OBJECT_CACHE', true );" . PHP_EOL;
$string .= "if ( ! defined( 'WP_CACHE_KEY_SALT' ) && defined( 'DB_NAME') ) {" . PHP_EOL;
$string .= "\t" . "define( 'WP_CACHE_KEY_SALT', DB_NAME );" . PHP_EOL;
$string .= '}' . PHP_EOL;
$object_caches = get_object_cache_dropins();
$string .= 'if ( defined( \'POWERED_CACHE_OBJECT_CACHE_DROPIN\') && @file_exists( POWERED_CACHE_OBJECT_CACHE_DROPIN ) ) {' . PHP_EOL;
$string .= "\t" . 'include( POWERED_CACHE_OBJECT_CACHE_DROPIN );' . PHP_EOL;
$string .= '} elseif ( @file_exists( \'' . $object_caches[ $backend ] . '\' ) ) {' . PHP_EOL;
$string .= "\t" . 'include( \'' . $object_caches[ $backend ] . '\' );' . PHP_EOL;
$string .= '} else {' . PHP_EOL;
$string .= "\t" . 'define( \'POWERED_OBJECT_CACHE_HAS_PROBLEM\', true );' . PHP_EOL;
$string .= '}' . PHP_EOL;
/**
* Filters object-cache.php file contents.
*
* @hook powered_cache_object_cache_file_content
*
* @param {string} $string The content of the object-cache.php file
*
* @return {string} New value.
*
* @since 1.0
*/
return apply_filters( 'powered_cache_object_cache_file_content', $string );
}
/**
* Generate advanced-cache.php and define WP_CACHE
*
* @param bool $status status of the page caching
*
* @return bool
* @since 1.0
*/
public function setup_page_cache( $status ) {
/**
* Forcing multisite settings always true
*/
if ( is_multisite() && ! POWERED_CACHE_IS_NETWORK ) {
$status = true;
}
$this->generate_advanced_cache_file();
$this->define_wp_cache( $status );
if ( can_configure_htaccess() ) {
$this->configure_htaccess( $status );
}
$this->protect_cache_dir();
return true;
}
/**
* Generates advanced-cache.php
*
* @return bool
* @since 1.1 is_multisite control added
* @since 1.0
*/
public function generate_advanced_cache_file() {
$file = untrailingslashit( WP_CONTENT_DIR ) . '/advanced-cache.php';
$settings = \PoweredCache\Utils\get_settings();
$file_string = '';
/**
* multisite setups should always have `advanced-cache.php` file
*/
if ( true === $settings['enable_page_cache'] || is_multisite() ) {
$file_string = $this->advanced_cache_file_content();
}
if ( ! file_put_contents( $file, $file_string ) ) {
return false;
}
return true;
}
/**
* Prepare advanced-cache.php contents
*
* @return mixed|void
* @since 1.1 supports `POWERED_CACHE_ADVANCED_CACHE_DROPIN`
* @since 1.0
*/
public function advanced_cache_file_content() {
$string = '<?php ' . PHP_EOL;
$string .= "defined( 'ABSPATH' ) || exit;" . PHP_EOL;
$string .= "define( 'POWERED_CACHE_PAGE_CACHING', true );" . PHP_EOL . PHP_EOL;
// lookup order 1) network-wide , 2) subdomain specific (if any) 3) domain specific
$string .= "\$config_locations[] = WP_CONTENT_DIR . '/pc-config/config-network.php';" . PHP_EOL;
$string .= "\$host = isset( \$_SERVER['HTTP_HOST'] ) ? \$_SERVER['HTTP_HOST'] : '';" . PHP_EOL . PHP_EOL;
$string .= "if ( is_multisite() && defined( 'SUBDOMAIN_INSTALL' ) && ! SUBDOMAIN_INSTALL ) {" . PHP_EOL;
$string .= "\t" . "\$request_uri = explode( '/', ltrim( \$_SERVER['REQUEST_URI'], '/' ) );" . PHP_EOL;
$string .= "\t" . 'if ( ! empty( $request_uri[0] ) ) {' . PHP_EOL;
$string .= "\t" . "\t" . "\$config_locations[] = WP_CONTENT_DIR . '/pc-config/config-' . \$host . '-' . \$request_uri[0] . '.php';" . PHP_EOL;
$string .= "\t" . '}' . PHP_EOL;
$string .= '}' . PHP_EOL;
$string .= "\$config_locations[] = WP_CONTENT_DIR . '/pc-config/config-' . \$host . '.php';" . PHP_EOL . PHP_EOL;
$string .= 'foreach ( $config_locations as $config_file ) {' . PHP_EOL;
$string .= "\t" . 'if ( @file_exists( $config_file ) ) {' . PHP_EOL;
$string .= "\t" . "\t" . 'include( $config_file );' . PHP_EOL;
$string .= "\t" . "\t" . 'break;' . PHP_EOL;
$string .= "\t" . '}' . PHP_EOL;
$string .= '}' . PHP_EOL . PHP_EOL;
$string .= "if ( ! isset( \$GLOBALS['powered_cache_options'] ) ) {" . PHP_EOL;
$string .= "\t" . 'return;' . PHP_EOL;
$string .= '}' . PHP_EOL . PHP_EOL;
$string .= 'if ( defined( \'POWERED_CACHE_ADVANCED_CACHE_DROPIN\') && @file_exists( POWERED_CACHE_ADVANCED_CACHE_DROPIN ) ) {' . PHP_EOL;
$string .= "\t" . 'include( POWERED_CACHE_ADVANCED_CACHE_DROPIN );' . PHP_EOL;
$string .= '} elseif ( @file_exists( \'' . POWERED_CACHE_DROPIN_DIR . 'page-cache.php' . '\' ) ) {' . PHP_EOL;
$string .= "\t" . 'include( \'' . POWERED_CACHE_DROPIN_DIR . 'page-cache.php' . '\' );' . PHP_EOL;
$string .= '} else {' . PHP_EOL;
$string .= "\t" . 'define( \'POWERED_CACHE_PAGE_CACHING_HAS_PROBLEM\', true );' . PHP_EOL;
$string .= '}';
/**
* Filters advanced-cache.php file contents.
*
* @hook powered_cache_advanced_cache_file_content
*
* @param {string} $string The content of the advanced-cache.php file
*
* @return {string} New value.
*
* @since 1.0
*/
return apply_filters( 'powered_cache_advanced_cache_file_content', $string );
}
/**
* Define WP_CACHE constant
*
* @param bool $status The status of the caching
*
* @return bool
* @since 1.0
*/
public function define_wp_cache( $status ) {
$config_path = $this->find_wp_config_file();
if ( ! $config_path ) {
return false;
}
if ( defined( 'WP_CACHE' ) && WP_CACHE === $status ) {
return true;
}
$config_file_string = file_get_contents( $config_path );
// Config file is empty. Maybe couldn't read it?
if ( empty( $config_file_string ) ) {
return false;
}
$config_file = preg_split( "#(\r\n|\r|\n)#", $config_file_string );
$line_key = false;
foreach ( $config_file as $key => $line ) {
if ( ! preg_match( '/^\s*define\(\s*(\'|")([A-Z_]+)(\'|")(.*)/', $line, $match ) ) {
continue;
}
if ( 'WP_CACHE' === $match[2] ) {
$line_key = $key;
}
}
if ( false !== $line_key ) {
unset( $config_file[ $line_key ] );
}
$status_string = ( $status ) ? 'true' : 'false';
array_shift( $config_file );
array_unshift( $config_file, '<?php', "define( 'WP_CACHE', $status_string ); // Powered Cache" );
if ( ! @file_put_contents( $config_path, implode( PHP_EOL, $config_file ) ) ) { // phpcs:ignore
return false;
}
return true;
}
/**
* seeking wp-config file
*
* @return bool|string
* @since 1.0
*/
public function find_wp_config_file() {
$file = '/wp-config.php';
for ( $i = 1; $i <= 3; $i ++ ) {
if ( $i > 1 ) {
$file = '/..' . $file;
}
if ( file_exists( untrailingslashit( ABSPATH ) . $file ) ) {
$config_path = untrailingslashit( ABSPATH ) . $file;
break;
}
}
if ( ! isset( $config_path ) ) {
return false;
}
return $config_path;
}
/**
* Create .htaccess file based on current setting preferences
*
* @param bool $enable Configure .htaccess automatically when it's true
*
* @return bool
* @since 1.0
*/
public function configure_htaccess( $enable = true ) {
if ( ! function_exists( '\get_home_path' ) ) {
require_once ABSPATH . '/wp-admin/includes/file.php';
}
$htaccess_file = get_home_path() . '.htaccess';
$settings = \PoweredCache\Utils\get_settings();
/**
* Filters whether automatically update or not update .htaccess file
*
* @hook powered_cache_auto_htaccess_update
*
* @param {boolean} true to automatic update.
*
* @return {boolean} New value.
*
* @since 1.1.1
*/
if ( true !== apply_filters( 'powered_cache_auto_htaccess_update', true ) ) {
return false;
}
/**
* Apache users can control automatic configuration
*
* @since 1.2
*/
$automatic_configuration = $settings['auto_configure_htaccess'];
if ( is_multisite() && ! POWERED_CACHE_IS_NETWORK ) {
$automatic_configuration = false; // individual sites shouldn't use .htaccess on multisite
}
if ( ! $automatic_configuration ) {
return false;
}
if ( is_writable( $htaccess_file ) ) {
$contents = file_get_contents( $htaccess_file );
// clean up
$contents = preg_replace( '/# BEGIN POWERED CACHE(.*)# END POWERED CACHE\s*?/isU', '', $contents );
if ( false === $enable ) {
return file_put_contents( $htaccess_file, $contents );
}
$rules = Htaccess::factory()->htaccess_rules();
$contents = $rules . $contents;
// Update the .htacces file
if ( ! file_put_contents( $htaccess_file, $contents ) ) {
return false;
}
return true;
}
return false;
}
/**
* Prepares .htaccess rules for the caching
*
* @return string $rules
* @since 1.1
* @deprecated 2.5.0 Use the {@see '\PoweredCache\Htaccess::factory()->htaccess_rules()'} instead.
*/
public function htaccess_rules() {
$rules = Htaccess::factory()->htaccess_rules();
return $rules;
}
/**
* Make sure directory listing disabled
*
* @return bool
* @since 1.2
*/
public function protect_cache_dir() {
if ( ! file_exists( get_cache_dir() ) ) {
mkdir( get_cache_dir() );
}
$file = get_cache_dir() . '.htaccess';
$file_string = '<IfModule mod_autoindex.c>' . PHP_EOL;
$file_string .= ' Options -Indexes' . PHP_EOL;
$file_string .= '</IfModule>' . PHP_EOL . PHP_EOL;
$file_string .= '<FilesMatch "^.*-user_.*\.(html|html\.gz)$">' . PHP_EOL;
$file_string .= ' Order Allow,Deny' . PHP_EOL;
$file_string .= ' Deny from all' . PHP_EOL;
$file_string .= '</FilesMatch>' . PHP_EOL;
/**
* Filters the content of the .htaccess file in the cache directory.
*
* @param {string} $file_string Default configuration
*
* @hook powered_cache_cache_dir_htaccess_file_content
* @since 3.5.3
*/
$file_string = apply_filters( 'powered_cache_cache_dir_htaccess_file_content', $file_string );
if ( ! file_put_contents( $file, $file_string ) ) {
return false;
}
return true;
}
/**
* Prepare config name
*
* @param bool $is_network Whether network-wide config name or not
*
* @return string configuration file name
* @since 2.0
*/
public function get_config_filename( $is_network ) {
if ( $is_network ) {
return 'config-network.php';
} else {
$url_parts = wp_parse_url( home_url() );
$config_name = 'config-' . $url_parts['host'];
if ( is_multisite() && ! is_subdomain_install() ) {
if ( ! is_main_site( get_current_blog_id() ) && ! empty( $url_parts['path'] ) ) {
$subdir_name = ltrim( $url_parts['path'], '/' );
$config_name .= '-' . $subdir_name;
}
}
$config_name .= '.php';
return $config_name;
}
}
/**
* Save settings to file
*
* @param array $configuration plugin settings
* @param bool $network_wide Whether network-wide configuration or not
*
* @return bool
* @since 1.0
*/
public function save_to_file( $configuration, $network_wide = false ) {
$config_dir = WP_CONTENT_DIR . '/pc-config';
$config_file_name = $this->get_config_filename( $network_wide );
if ( ! file_exists( $config_dir ) ) {
mkdir( $config_dir );
}
$config_file = trailingslashit( $config_dir ) . $config_file_name;
if ( ! file_exists( $config_file ) ) {
touch( $config_file );
}
$configuration['cache_location'] = get_cache_dir();
$config_file_string = '<?php' . PHP_EOL . "defined( 'ABSPATH' ) || exit;" . PHP_EOL . PHP_EOL;
$config_file_string .= "\$GLOBALS['powered_cache_options'] = " . var_export( $configuration, true ) . ';' . PHP_EOL . PHP_EOL;
// mobile cache variables
$config_file_string .= '$powered_cache_mobile_browsers = ' . var_export( mobile_browsers(), true ) . ';' . PHP_EOL;
$config_file_string .= '$powered_cache_mobile_prefixes = ' . var_export( mobile_prefixes(), true ) . ';' . PHP_EOL;
$config_file_string .= '$powered_cache_rejected_user_agents = ' . var_export( AdvancedCache::get_rejected_user_agents(), true ) . ';' . PHP_EOL;
$config_file_string .= '$powered_cache_rejected_cookies = ' . var_export( AdvancedCache::get_rejected_cookies(), true ) . ';' . PHP_EOL;
$config_file_string .= '$powered_cache_rejected_referrers = ' . var_export( AdvancedCache::get_rejected_referrers(), true ) . ';' . PHP_EOL;
$config_file_string .= '$powered_cache_vary_cookies = ' . var_export( AdvancedCache::get_vary_cookies(), true ) . ';' . PHP_EOL;
$config_file_string .= '$powered_cache_rejected_uri = ' . var_export( AdvancedCache::get_rejected_uri(), true ) . ';' . PHP_EOL;
$config_file_string .= '$powered_cache_ignored_query_strings = ' . var_export( AdvancedCache::get_ignored_query_strings(), true ) . ';' . PHP_EOL;
$config_file_string .= '$powered_cache_cache_query_strings = ' . var_export( AdvancedCache::get_cache_query_string(), true ) . ';' . PHP_EOL;
if ( permalink_structure_has_trailingslash() ) {
$config_file_string .= '$powered_cache_slash_check = true;' . PHP_EOL;
} else {
$config_file_string .= '$powered_cache_slash_check = false;' . PHP_EOL;
}
/**
* Fires before writing configuration file.
*
* @hook powered_cache_create_config_file
*
* @param {string} $config_file The path of the configuration file.
* @param {string} $config_file_string The contents of the configurations.
* @param {bool} $network_wide Whether network-wide configuration or not.
*
* @since 2.0
*/
do_action( 'powered_cache_create_config_file', $config_file, $config_file_string, $network_wide );
if ( ! file_put_contents( $config_file, $config_file_string ) ) {
return false;
}
// OPCache invalidate
if ( function_exists( 'opcache_invalidate' ) ) {
opcache_invalidate( $config_file, true );
}
return true;
}
/**
* Prepares nginx configuration
*
* @return string $contents nginx rules
* @since 1.2 trailingslash rule added
*
* @since 1.1
*/
public function nginx_rules() {
$settings = \PoweredCache\Utils\get_settings();
$contents = '';
$contents .= '##### POWERED CACHE CONF #####' . PHP_EOL;
$contents .= 'set $cache_uri $request_uri;' . PHP_EOL;
$contents .= 'set $pc_ssl "";' . PHP_EOL;
$contents .= 'set $pc_enc "";' . PHP_EOL;
$contents .= 'set $pc_ua "";' . PHP_EOL . PHP_EOL;
// post
$contents .= '# POST requests and urls with a query string should always go to PHP' . PHP_EOL;
$contents .= 'if ($request_method = POST) {' . PHP_EOL;
$contents .= ' set $cache_uri \'null cache\';' . PHP_EOL;
$contents .= '}' . PHP_EOL . PHP_EOL;
$contents .= 'location = /favicon.ico { log_not_found off; access_log off; }' . PHP_EOL;
$contents .= 'location = /robots.txt { try_files $uri $uri/ /index.php?$args; log_not_found off; access_log off; }' . PHP_EOL . PHP_EOL;
// query string
$contents .= 'if ($query_string != "") {' . PHP_EOL;
$contents .= ' set $cache_uri \'null cache\';' . PHP_EOL;
$contents .= '}' . PHP_EOL . PHP_EOL;
// Add CORS rules
if ( $settings['enable_cdn'] ) {
$contents .= 'location ~* .(jpg|jpeg|png|gif|ico|css|js|svg|eot|woff|woff2|ttf|otf)$ {' . PHP_EOL;
$contents .= 'add_header Access-Control-Allow-Origin *;' . PHP_EOL;
$contents .= '}' . PHP_EOL . PHP_EOL;
}
/**
* Documented in htaccess config
*/
if ( apply_filters( 'powered_cache_browser_cache', true ) ) {
$contents .= 'location ~* .(js|jpg|jpeg|gif|png|css|tgz|gz|rar|bz2|doc|pdf|ppt|tar|wav|bmp|rtf|swf|ico|flv|txt|woff|woff2|svg|webp|avif)$ {' . PHP_EOL;
$contents .= ' expires 6M;' . PHP_EOL;
$contents .= '}' . PHP_EOL . PHP_EOL;
}
// file optimizer rewrite rule
if ( $settings['rewrite_file_optimizer'] ) {
$file_optimizer_path = \PoweredCache\Optimizer\Helper::get_file_optimizer_relative_path();
$contents .= 'location /_static/ {' . PHP_EOL;
$contents .= ' fastcgi_pass unix:/var/run/fastcgi.sock;' . PHP_EOL;
$contents .= ' include /etc/nginx/fastcgi_params;' . PHP_EOL;
$contents .= ' fastcgi_param SCRIPT_FILENAME $document_root/' . $file_optimizer_path . ';' . PHP_EOL;
$contents .= '}' . PHP_EOL . PHP_EOL;
}
// https
$contents .= '# HTTPS' . PHP_EOL;
$contents .= 'if ($https = "on") {' . PHP_EOL;
$contents .= ' set $pc_ssl "-https";' . PHP_EOL;
$contents .= '}' . PHP_EOL . PHP_EOL;
$contents .= '# Don\'t cache uris containing the following segments' . PHP_EOL;
$contents .= 'if ($request_uri ~* "(/wp-admin/|/xmlrpc.php|/wp-(app|cron|login|register|mail).php|wp-.*.php|/feed/|index.php|wp-comments-popup.php|wp-links-opml.php|wp-locations.php|sitemap(_index)?.xml|[a-z0-9_-]+-sitemap([0-9]+)?.xml)") {' . PHP_EOL;
$contents .= ' set $cache_uri \'null cache\';' . PHP_EOL;
$contents .= '}' . PHP_EOL . PHP_EOL;
$rejected_user_agents = (array) AdvancedCache::get_rejected_user_agents();
$contents .= '# Don\'t use the cache for rejected agents' . PHP_EOL;
$contents .= 'if ($http_user_agent ~* "(' . implode( '|', $rejected_user_agents ) . ')") {' . PHP_EOL;
$contents .= ' set $cache_uri \'null cache\';' . PHP_EOL;
$contents .= '}' . PHP_EOL . PHP_EOL;
$rejected_cookies = (array) AdvancedCache::get_rejected_cookies();
$contents .= '# Don\'t use the cache for logged in users or recent commenters' . PHP_EOL;
$contents .= 'if ($http_cookie ~* "wordpress_[a-f0-9]+|' . implode( '|', $rejected_cookies ) . '") {' . PHP_EOL;
$contents .= ' set $cache_uri \'null cache\';' . PHP_EOL;
$contents .= '}' . PHP_EOL . PHP_EOL;
$contents .= 'if ($http_x_wap_profile) {' . PHP_EOL;
$contents .= ' set $pc_ua \'-mobile\';' . PHP_EOL;
$contents .= '}' . PHP_EOL . PHP_EOL;
if ( true === $settings['cache_mobile'] && true === $settings['cache_mobile_separate_file'] ) {
$mobile_browsers = addcslashes( implode( '|', preg_split( '/[\s*,\s*]*,+[\s*,\s*]*/', mobile_browsers() ) ), ' ' );
$mobile_prefixes = addcslashes( implode( '|', preg_split( '/[\s*,\s*]*,+[\s*,\s*]*/', mobile_prefixes() ) ), ' ' );
$contents .= 'if ($http_user_agent ~* (' . $mobile_browsers . ')) {' . PHP_EOL;
$contents .= ' set $pc_ua \'-mobile\';' . PHP_EOL;
$contents .= '}' . PHP_EOL . PHP_EOL;
$contents .= 'if ($http_user_agent ~* (' . $mobile_prefixes . ')) {' . PHP_EOL;
$contents .= ' set $pc_ua \'-mobile\';' . PHP_EOL;
$contents .= '}' . PHP_EOL . PHP_EOL;
}
$cache_suffix = 'html';
if ( true === $settings['gzip_compression'] ) {
$cache_suffix .= '.gz';
}
$contents .= 'location / {' . PHP_EOL;
/**
* Documented in htaccess config
*/
if ( apply_filters( 'powered_cache_mod_rewrite', true ) ) { // rewrite
$contents .= ' add_header X-Powered-Cache nginx;' . PHP_EOL;
$contents .= ' try_files /wp-content/cache/powered-cache/$http_host/$cache_uri/index${pc_ssl}${pc_ua}.' . $cache_suffix . ' $uri $uri/ /index.php?$args;' . PHP_EOL;
} else {
$contents .= ' try_files $uri $uri/ /index.php?$args;' . PHP_EOL;
}
$contents .= '}' . PHP_EOL . PHP_EOL;
if ( permalink_structure_has_trailingslash() ) {
$contents .= '# add trailingslash rule' . PHP_EOL;
$contents .= 'rewrite ^([^.]*[^/])$ $1/ permanent;' . PHP_EOL;
}
return $contents;
}
/**
* Downloads configuration files
*
* @param string $server type supports apache and nginx
*
* @since 1.1
*/
public function download_rewrite_rules( $server ) {
$rules = '';
$filename = 'conf';
if ( 'apache' === $server ) {
$rules = Htaccess::factory()->htaccess_rules();
$filename = '.htaccess_powered_cache';
}
if ( 'nginx' === $server ) {
$rules = $this->nginx_rules();
$filename = 'poweredcache.conf';
}
nocache_headers();
@header( 'Content-Type: text/plain' );
@header( 'Content-Disposition: attachment; filename="' . $filename . '"' );
@header( 'Content-Transfer-Encoding: binary' );
@header( 'Content-Length: ' . strlen( $rules ) );
@header( 'Connection: close' );
echo $rules; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
exit;
}
/**
* Make the caching configurations with given options
*
* @param array $settings Plugin settings
* @param bool $network_wide Whether network-wide configuration or not
*
* @since 2.0
*/
public function save_configuration( $settings, $network_wide = false ) {
if ( can_configure_object_cache() ) {
if ( ! empty( $settings['dev_mode'] ) ) {
$this->setup_object_cache(); // dev mode, no object cache
} else {
$this->setup_object_cache( $settings['object_cache'] );
}
}
$this->setup_page_cache( $settings['enable_page_cache'] );
$private_settings = [ 'cloudflare_email', 'cloudflare_api_key', 'cloudflare_api_token', 'cloudflare_zone' ];
foreach ( $private_settings as $setting_key ) {
unset( $settings[ $setting_key ] );
}
$this->save_to_file( $settings, $network_wide );
}
/**
* Clean-up all the configurations and cache related footprints
*/
public function clean_up() {
$object_cache_dropin = untrailingslashit( WP_CONTENT_DIR ) . '/object-cache.php';
if ( file_exists( $object_cache_dropin ) && false !== strpos( file_get_contents( $object_cache_dropin ), 'POWERED_OBJECT_CACHE' ) ) {
unlink( $object_cache_dropin );
}
$advanced_cache_dropin = untrailingslashit( WP_CONTENT_DIR ) . '/advanced-cache.php';
if ( file_exists( $advanced_cache_dropin ) ) {
unlink( $advanced_cache_dropin );
}
$this->define_wp_cache( false );
$this->configure_htaccess( false );
$this->protect_cache_dir();
if ( ! file_exists( get_cache_dir() ) ) {
remove_dir( get_cache_dir() );
}
$config_dir = WP_CONTENT_DIR . '/pc-config';
if ( is_multisite() ) {
$config_file_name = $this->get_config_filename( POWERED_CACHE_IS_NETWORK );
$config_file = trailingslashit( $config_dir ) . $config_file_name;
if ( file_exists( $config_file ) ) {
unlink( $config_file );
}
} else { // remove entire configuration directory on single site setup
remove_dir( $config_dir );
}
/**
* Fires after cleanup all configurations
*
* @hook powered_cache_after_clean_up
*
* @since 2.0
*/
do_action( 'powered_cache_after_clean_up' );
}
}
</code></pre>
</article>
</section>
</div>
<nav>
<h2><a href="index.html">Home</a></h2><h3>Actions</h3><ul><li><a href="powered_cache_admin_page_after_settings_nav.html">powered_cache_admin_page_after_settings_nav</a></li><li><a href="powered_cache_admin_page_before_file_optimization.html">powered_cache_admin_page_before_file_optimization</a></li><li><a href="powered_cache_advanced_cache_purge_on_comment_update.html">powered_cache_advanced_cache_purge_on_comment_update</a></li><li><a href="powered_cache_advanced_cache_purge_post.html">powered_cache_advanced_cache_purge_post</a></li><li><a href="powered_cache_after_clean_up.html">powered_cache_after_clean_up</a></li><li><a href="powered_cache_cache_dir_htaccess_file_content.html">powered_cache_cache_dir_htaccess_file_content</a></li><li><a href="powered_cache_cdn_setup.html">powered_cache_cdn_setup</a></li><li><a href="powered_cache_clean_site_cache_dir.html">powered_cache_clean_site_cache_dir</a></li><li><a href="powered_cache_create_config_file.html">powered_cache_create_config_file</a></li><li><a href="powered_cache_flushed.html">powered_cache_flushed</a></li><li><a href="powered_cache_fo_js_concat_did_items.html">powered_cache_fo_js_concat_did_items</a></li><li><a href="powered_cache_get_url_dir.html">powered_cache_get_url_dir</a></li><li><a href="powered_cache_init.html">powered_cache_init</a></li><li><a href="powered_cache_lazy_load_compat.html">powered_cache_lazy_load_compat</a></li><li><a href="powered_cache_lazy_load_filter.html">powered_cache_lazy_load_filter</a></li><li><a href="powered_cache_lazy_load_placeholder_url.html">powered_cache_lazy_load_placeholder_url</a></li><li><a href="powered_cache_loaded.html">powered_cache_loaded</a></li><li><a href="powered_cache_mobile_browsers.html">powered_cache_mobile_browsers</a></li><li><a href="powered_cache_mobile_prefixes.html">powered_cache_mobile_prefixes</a></li><li><a href="powered_cache_page_cache_enable.html">powered_cache_page_cache_enable</a></li><li><a href="powered_cache_page_cached.html">powered_cache_page_cached</a></li><li><a href="powered_cache_preload_http_request.html">powered_cache_preload_http_request</a></li><li><a href="powered_cache_purge_all_cache.html">powered_cache_purge_all_cache</a></li><li><a href="powered_cache_settings_saved.html">powered_cache_settings_saved</a></li><li><a href="powered_cache_updated.html">powered_cache_updated</a></li></ul><h3>Filters</h3><ul><li><a href="populate_preload_queue_urls.html">populate_preload_queue_urls</a></li><li><a href="powered_cache_accepted_query_strings.html">powered_cache_accepted_query_strings</a></li><li><a href="powered_cache_advanced_cache_file_content.html">powered_cache_advanced_cache_file_content</a></li><li><a href="powered_cache_advanced_cache_purge_urls.html">powered_cache_advanced_cache_purge_urls</a></li><li><a href="powered_cache_after_htaccess.html">powered_cache_after_htaccess</a></li><li><a href="powered_cache_auto_htaccess_update.html">powered_cache_auto_htaccess_update</a></li><li><a href="powered_cache_browser_cache.html">powered_cache_browser_cache</a></li><li><a href="powered_cache_browser_cache_assets_lifespan.html">powered_cache_browser_cache_assets_lifespan</a></li><li><a href="powered_cache_browser_cache_default_lifespan.html">powered_cache_browser_cache_default_lifespan</a></li><li><a href="powered_cache_browser_cache_lifespan.html">powered_cache_browser_cache_lifespan</a></li><li><a href="powered_cache_cache_purge_interval.html">powered_cache_cache_purge_interval</a></li><li><a href="powered_cache_cache_query_strings.html">powered_cache_cache_query_strings</a></li><li><a href="powered_cache_cdn_addresses.html">powered_cache_cdn_addresses</a></li><li><a href="powered_cache_cdn_disable.html">powered_cache_cdn_disable</a></li><li><a href="powered_cache_cdn_extensions.html">powered_cache_cdn_extensions</a></li><li><a href="powered_cache_cdn_image_extensions.html">powered_cache_cdn_image_extensions</a></li><li><a href="powered_cache_cdn_rewrite_relative_urls.html">powered_cache_cdn_rewrite_relative_urls</a></li><li><a href="powered_cache_cdn_site_hostnames.html">powered_cache_cdn_site_hostnames</a></li><li><a href="powered_cache_cdn_zones.html">powered_cache_cdn_zones</a></li><li><a href="powered_cache_default_settings.html">powered_cache_default_settings</a></li><li><a href="powered_cache_defer_exclusions.html">powered_cache_defer_exclusions</a></li><li><a href="powered_cache_delay_exclusions.html">powered_cache_delay_exclusions</a></li><li><a href="powered_cache_delayed_js_script_content.html">powered_cache_delayed_js_script_content</a></li><li><a href="powered_cache_delayed_js_script_loader.html">powered_cache_delayed_js_script_loader</a></li><li><a href="powered_cache_delayed_js_skip.html">powered_cache_delayed_js_skip</a></li><li><a href="powered_cache_delayed_js_timeout.html">powered_cache_delayed_js_timeout</a></li><li><a href="powered_cache_disable_advanced_cache_notices.html">powered_cache_disable_advanced_cache_notices</a></li><li><a href="powered_cache_disable_js_defer.html">powered_cache_disable_js_defer</a></li><li><a href="powered_cache_disable_js_defer_inline.html">powered_cache_disable_js_defer_inline</a></li><li><a href="powered_cache_disable_native_lazyload.html">powered_cache_disable_native_lazyload</a></li><li><a href="powered_cache_fo_allow_gzip_compression.html">powered_cache_fo_allow_gzip_compression</a></li><li><a href="powered_cache_fo_cache_ttl.html">powered_cache_fo_cache_ttl</a></li><li><a href="powered_cache_fo_css_do_concat.html">powered_cache_fo_css_do_concat</a></li><li><a href="powered_cache_fo_dashboard.html">powered_cache_fo_dashboard</a></li><li><a href="powered_cache_fo_disable.html">powered_cache_fo_disable</a></li><li><a href="powered_cache_fo_disable_css_combine.html">powered_cache_fo_disable_css_combine</a></li><li><a href="powered_cache_fo_disable_css_minify.html">powered_cache_fo_disable_css_minify</a></li><li><a href="powered_cache_fo_disable_html_minify.html">powered_cache_fo_disable_html_minify</a></li><li><a href="powered_cache_fo_disable_js_combine.html">powered_cache_fo_disable_js_combine</a></li><li><a href="powered_cache_fo_disable_js_minify.html">powered_cache_fo_disable_js_minify</a></li><li><a href="powered_cache_fo_excluded_css_files.html">powered_cache_fo_excluded_css_files</a></li><li><a href="powered_cache_fo_excluded_js_files.html">powered_cache_fo_excluded_js_files</a></li><li><a href="powered_cache_fo_google_font_display.html">powered_cache_fo_google_font_display</a></li><li><a href="powered_cache_fo_google_fonts_domain.html">powered_cache_fo_google_fonts_domain</a></li><li><a href="powered_cache_fo_js_do_concat.html">powered_cache_fo_js_do_concat</a></li><li><a href="powered_cache_fo_relative_path.html">powered_cache_fo_relative_path</a></li><li><a href="powered_cache_fo_script_loader_tag.html">powered_cache_fo_script_loader_tag</a></li><li><a href="powered_cache_fo_site_url.html">powered_cache_fo_site_url</a></li><li><a href="powered_cache_fo_style_loader_tag.html">powered_cache_fo_style_loader_tag</a></li><li><a href="powered_cache_get_page_cache_dir.html">powered_cache_get_page_cache_dir</a></li><li><a href="powered_cache_htaccess_add_cors.html">powered_cache_htaccess_add_cors</a></li><li><a href="powered_cache_htaccess_cache_control_rules.html">powered_cache_htaccess_cache_control_rules</a></li><li><a href="powered_cache_htaccess_enable_gzip_compression.html">powered_cache_htaccess_enable_gzip_compression</a></li><li><a href="powered_cache_ignored_query_strings.html">powered_cache_ignored_query_strings</a></li><li><a href="powered_cache_js_execution_methods.html">powered_cache_js_execution_methods</a></li><li><a href="powered_cache_known_headers.html">powered_cache_known_headers</a></li><li><a href="powered_cache_lazy_load_avatar.html">powered_cache_lazy_load_avatar</a></li><li><a href="powered_cache_lazy_load_enabled.html">powered_cache_lazy_load_enabled</a></li><li><a href="powered_cache_lazy_load_exclusions.html">powered_cache_lazy_load_exclusions</a></li><li><a href="powered_cache_lazy_load_html.html">powered_cache_lazy_load_html</a></li><li><a href="powered_cache_lazy_load_iframes.html">powered_cache_lazy_load_iframes</a></li><li><a href="powered_cache_lazy_load_images.html">powered_cache_lazy_load_images</a></li><li><a href="powered_cache_lazy_load_post_content.html">powered_cache_lazy_load_post_content</a></li><li><a href="powered_cache_lazy_load_post_thumbnail.html">powered_cache_lazy_load_post_thumbnail</a></li><li><a href="powered_cache_lazy_load_run_filter.html">powered_cache_lazy_load_run_filter</a></li><li><a href="powered_cache_lazy_load_skip_classes.html">powered_cache_lazy_load_skip_classes</a></li><li><a href="powered_cache_lazy_load_skip_first_nth_img.html">powered_cache_lazy_load_skip_first_nth_img</a></li><li><a href="powered_cache_lazy_load_threshold.html">powered_cache_lazy_load_threshold</a></li><li><a href="powered_cache_lazy_load_widget_text.html">powered_cache_lazy_load_widget_text</a></li><li><a href="powered_cache_lazy_load_youtube.html">powered_cache_lazy_load_youtube</a></li><li><a href="powered_cache_lazy_load_youtube_css.html">powered_cache_lazy_load_youtube_css</a></li><li><a href="powered_cache_log_destination.html">powered_cache_log_destination</a></li><li><a href="powered_cache_log_message.html">powered_cache_log_message</a></li><li><a href="powered_cache_log_message_type.html">powered_cache_log_message_type</a></li><li><a href="powered_cache_maybe_1and1_hosting.html">powered_cache_maybe_1and1_hosting</a></li><li><a href="powered_cache_mod_rewrite.html">powered_cache_mod_rewrite</a></li><li><a href="powered_cache_object_cache_dropins.html">powered_cache_object_cache_dropins</a></li><li><a href="powered_cache_object_cache_file_content.html">powered_cache_object_cache_file_content</a></li><li><a href="powered_cache_page_cache_meta_info.html">powered_cache_page_cache_meta_info</a></li><li><a href="powered_cache_page_cache_meta_params.html">powered_cache_page_cache_meta_params</a></li><li><a href="powered_cache_page_caching_buffer.html">powered_cache_page_caching_buffer</a></li><li><a href="powered_cache_post_related_urls.html">powered_cache_post_related_urls</a></li><li><a href="powered_cache_pre_htaccess.html">powered_cache_pre_htaccess</a></li><li><a href="powered_cache_preconnect_resource.html">powered_cache_preconnect_resource</a></li><li><a href="powered_cache_prefetch_dns.html">powered_cache_prefetch_dns</a></li><li><a href="powered_cache_preload_add_url_to_queue.html">powered_cache_preload_add_url_to_queue</a></li><li><a href="powered_cache_preload_expired_urls.html">powered_cache_preload_expired_urls</a></li><li><a href="powered_cache_preload_mobile_user_agent.html">powered_cache_preload_mobile_user_agent</a></li><li><a href="powered_cache_preload_post_limit.html">powered_cache_preload_post_limit</a></li><li><a href="powered_cache_preload_post_types.html">powered_cache_preload_post_types</a></li><li><a href="powered_cache_preload_public_posts_offset.html">powered_cache_preload_public_posts_offset</a></li><li><a href="powered_cache_preload_public_taxonomies_offset.html">powered_cache_preload_public_taxonomies_offset</a></li><li><a href="powered_cache_preload_request_interval.html">powered_cache_preload_request_interval</a></li><li><a href="powered_cache_preload_tax_term_args.html">powered_cache_preload_tax_term_args</a></li><li><a href="powered_cache_preload_taxonomies.html">powered_cache_preload_taxonomies</a></li><li><a href="powered_cache_preload_term_limit.html">powered_cache_preload_term_limit</a></li><li><a href="powered_cache_preload_url_request_args.html">powered_cache_preload_url_request_args</a></li><li><a href="powered_cache_preloader_max_server_load.html">powered_cache_preloader_max_server_load</a></li><li><a href="powered_cache_preloader_should_continue.html">powered_cache_preloader_should_continue</a></li><li><a href="powered_cache_rejected_cookies.html">powered_cache_rejected_cookies</a></li><li><a href="powered_cache_rejected_referrers.html">powered_cache_rejected_referrers</a></li><li><a href="powered_cache_rejected_uri_list.html">powered_cache_rejected_uri_list</a></li><li><a href="powered_cache_rejected_user_agents.html">powered_cache_rejected_user_agents</a></li><li><a href="powered_cache_sanitized_options.html">powered_cache_sanitized_options</a></li><li><a href="powered_cache_scheduled_cleanup_frequency_options.html">powered_cache_scheduled_cleanup_frequency_options</a></li><li><a href="powered_cache_site_cache_dir.html">powered_cache_site_cache_dir</a></li><li><a href="powered_cache_vary_cookies.html">powered_cache_vary_cookies</a></li></ul>
</nav>
<br class="clear">
<script> prettyPrint(); </script>
<script src="scripts/linenumber.js"> </script>
</body>
</html>