PluginProbe
Powered Cache – Caching and Optimization for WordPress – Easily Improve PageSpeed & Web Vitals Score / 1.1.2
Powered Cache – Caching and Optimization for WordPress – Easily Improve PageSpeed & Web Vitals Score v1.1.2
trunk 1.0 1.0.1 1.1 1.1.1 1.1.2 1.2 1.2.1 1.2.2 1.2.3 1.2.4 1.2.5 1.2.6 1.2.7 1.2.8 2.0 2.0.1 2.0.2 2.0.3 2.0.4 2.1 2.1.1 2.1.2 2.2 2.2.1 All 69 releases
powered-cache / includes / class-powered-cache-config.php

class-powered-cache-config.php in Powered Cache – Caching and Optimization for WordPress – Easily Improve PageSpeed & Web Vitals Score 1.1.2, at includes/class-powered-cache-config.php

741 lines 24.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 if ( ! defined( 'ABSPATH' ) ) {
4 exit;
5 }
6
7 class Powered_Cache_Config {
8
9
10 /**
11 * Return an instance of the current class
12 *
13 * @since 1.0
14 * @since 1.1 initialize WP_Filesystem_Direct
15 * @return Powered_Cache_Config
16 */
17 public static function factory() {
18 static $instance = false;
19
20 if ( ! $instance ) {
21 $instance = new self();
22 global $powered_cache_fs;
23
24 if ( ! $powered_cache_fs ) {
25 include_once ABSPATH . 'wp-admin/includes/class-wp-filesystem-base.php';
26 include_once ABSPATH . 'wp-admin/includes/class-wp-filesystem-direct.php';
27
28 if ( ! defined( 'FS_CHMOD_DIR' ) ) {
29 define( 'FS_CHMOD_DIR', ( fileperms( ABSPATH ) & 0777 | 0755 ) );
30 }
31
32 if ( ! defined( 'FS_CHMOD_FILE' ) ) {
33 define( 'FS_CHMOD_FILE', ( fileperms( ABSPATH . 'index.php' ) & 0777 | 0644 ) );
34 }
35
36 $powered_cache_fs = new WP_Filesystem_Direct( new StdClass() );
37 }
38 }
39
40 return $instance;
41 }
42
43 /**
44 * placeholder
45 *
46 * @since 1.0
47 */
48 public function __construct() { }
49
50
51 /**
52 * Default options
53 *
54 * @since 1.0
55 * @return mixed|void
56 */
57 public function default_settings() {
58 $settings = array(
59 // basic options
60 'enable_page_caching' => false,
61 'object_cache' => 'off',
62 'cache_mobile' => true,
63 'cache_mobile_separate_file' => false,
64 'loggedin_user_cache' => false,
65 'ssl_cache' => false,
66 'gzip_compression' => false,
67 'cache_timeout' => 1440,
68 'cache_location' => powered_cache_get_cache_dir(),
69 // advanced options
70 'remove_query_string' => false,
71 'rejected_user_agents' => '',
72 'rejected_cookies' => '',
73 'rejected_uri' => '',
74 'accepted_query_strings' => '',
75 'purge_additional_pages' => '',
76 // cdn
77 'cdn_status' => false,
78 'cdn_hostname' => array(),
79 'cdn_zone' => array(),
80 'cdn_rejected_files' => '',
81 // skip extensions, we don't need default for them
82 // misc
83 'show_cache_message' => true,
84 );
85
86
87 return apply_filters( 'powered_cache_default_settings', $settings );
88 }
89
90 /**
91 * Generates advanced-cache.php
92 *
93 * @since 1.0
94 * @since 1.1 is_multisite control added
95 * @return bool
96 */
97 public function generate_advanced_cache_file() {
98 global $powered_cache_fs;
99
100 $file = untrailingslashit( WP_CONTENT_DIR ) . '/advanced-cache.php';
101
102
103 $file_string = '';
104
105 /**
106 * multisite setups should always have `advanced-cache.php` file
107 */
108 if ( true === powered_cache_get_option( 'enable_page_caching' ) || is_multisite() ) {
109 $file_string = $this->advanced_cache_file_content();
110 }
111
112 if ( ! $powered_cache_fs->put_contents( $file, $file_string, FS_CHMOD_FILE ) ) {
113 return false;
114 }
115
116 return true;
117 }
118
119
120 /**
121 * Setup object-cache.php
122 *
123 * @since 1.0
124 * @param string $backend
125 *
126 * @return bool
127 */
128 public function setup_object_cache( $backend = 'off' ) {
129 global $powered_cache_fs;
130
131 $file = untrailingslashit( WP_CONTENT_DIR ) . '/object-cache.php';
132
133 if ( is_multisite() && ! current_user_can( 'manage_network' ) ) {
134 return false;
135 }
136
137 if ( 'off' === $backend ) {
138 if ( $powered_cache_fs->exists( $file ) ) {
139 $powered_cache_fs->delete( $file ); // remove object cache file
140 }
141
142 return true;
143 }
144
145 $file_string = $this->object_cache_file_content( $backend );
146
147 if ( ! $powered_cache_fs->put_contents( $file, $file_string, FS_CHMOD_FILE ) ) {
148 return false;
149 }
150
151 return true;
152 }
153
154
155 /**
156 * Generate advanced-cache.php and define WP_CACHE
157 *
158 * @since 1.0
159 *
160 * @param $status
161 *
162 * @return bool
163 */
164 public function setup_page_cache( $status ) {
165
166 /**
167 * Forcing multisite settings always true
168 */
169 if ( is_multisite() ) {
170 $status = true;
171 }
172
173 Powered_Cache_Config::factory()->generate_advanced_cache_file();
174 Powered_Cache_Config::factory()->define_wp_cache( $status );
175 Powered_Cache_Config::factory()->configure_htaccess( $status );
176
177 return true;
178 }
179
180
181 /**
182 * object-cache.php contents
183 *
184 * @since 1.0
185 * @since 1.1 supports `POWERED_CACHE_OBJECT_CACHE_DROPIN`
186 * @param $backend
187 * @see Powered_Cache_Admin_Helper::object_cache_dropins
188 *
189 * @return mixed|void
190 */
191 public function object_cache_file_content( $backend ) {
192 $string = '<?php ' . "\n";
193 $string .= "defined( 'ABSPATH' ) || exit;" . PHP_EOL;
194 $string .= "define( 'POWERED_OBJECT_CACHE', true );" . PHP_EOL;
195 $string .= "if ( ! @file_exists( WP_CONTENT_DIR . '/pc-config/config-' . \$_SERVER['HTTP_HOST'] . '.php' ) ) { return; }" . PHP_EOL;
196
197 $object_caches = Powered_Cache_Admin_Helper::object_cache_dropins();
198
199 $string .= "\$GLOBALS['powered_cache_options'] = include( WP_CONTENT_DIR . '/pc-config/config-' . \$_SERVER['HTTP_HOST'] . '.php' );" . "\n\n";
200 $string .= 'if ( defined( \'POWERED_CACHE_OBJECT_CACHE_DROPIN\') && @file_exists( POWERED_CACHE_OBJECT_CACHE_DROPIN ) ) {' . PHP_EOL;
201 $string .= "\t" . 'include( POWERED_CACHE_OBJECT_CACHE_DROPIN );' . PHP_EOL;
202 $string .= '} elseif ( @file_exists( \'' . $object_caches[ $backend ] . '\' ) ) {' . PHP_EOL;
203 $string .= "\t" . 'include( \'' . $object_caches[ $backend ] . '\' );' . PHP_EOL;
204 $string .= '} else {' . PHP_EOL;
205 $string .= "\t" . 'define( \'POWERED_OBJECT_CACHE_HAS_PROBLEM\', true );' . PHP_EOL;
206 $string .= '}';
207
208 return apply_filters( 'powered_cache_object_cache_file_content', $string );
209 }
210
211
212 /**
213 * Prepare advanced-cache.php contents
214 *
215 * @since 1.0
216 * @since 1.1 supports `POWERED_CACHE_ADVANCED_CACHE_DROPIN`
217 * @return mixed|void
218 */
219 public function advanced_cache_file_content(){
220
221 $string = '<?php ' . PHP_EOL;
222 $string .= "defined( 'ABSPATH' ) || exit;" . PHP_EOL;
223 $string .= "define( 'POWERED_CACHE_PAGE_CACHING', true );" . PHP_EOL;
224
225 $string .= "\$config_file = WP_CONTENT_DIR . '/pc-config/config-' . \$_SERVER['HTTP_HOST'];" . PHP_EOL . PHP_EOL;
226
227 $string .= "if ( is_multisite() && ( defined( 'SUBDOMAIN_INSTALL' ) && false === SUBDOMAIN_INSTALL ) ) {" . PHP_EOL;
228 $string .= "\t" . "\$request_uri = explode( '/', ltrim( \$_SERVER['REQUEST_URI'], '/' ) );" . PHP_EOL;
229 $string .= "\t" . "\$sub_site_name = \$request_uri[0];" . PHP_EOL;
230 $string .= "\t" . "\$config_file .= '-'.\$sub_site_name;" . PHP_EOL;
231 $string .= "}" . PHP_EOL;
232 $string .= "\$config_file .= '.php';" . PHP_EOL;
233
234
235 $string .= "if ( ! @file_exists( \$config_file ) ) { return; }" . PHP_EOL;
236 // get config file
237 $string .= "\$GLOBALS['powered_cache_options'] = include( \$config_file );" . PHP_EOL . PHP_EOL;
238 // mobile cache varibales
239 $string .= '$powered_cache_mobile_browsers = ' . var_export( powered_cache_mobile_browsers(), true ) . ";" . PHP_EOL;
240 $string .= '$powered_cache_mobile_prefixes = ' . var_export( powered_cache_mobile_prefixes(), true ) . ";" . PHP_EOL;
241
242 $string .= 'if ( defined( \'POWERED_CACHE_ADVANCED_CACHE_DROPIN\') && @file_exists( POWERED_CACHE_ADVANCED_CACHE_DROPIN ) ) {' . PHP_EOL;
243 $string .= "\t" . 'include( POWERED_CACHE_ADVANCED_CACHE_DROPIN );' . PHP_EOL;
244 $string .= '} elseif ( @file_exists( \'' . POWERED_CACHE_DROPIN_DIR . 'page-cache.php' . '\' ) ) {' . PHP_EOL;
245 $string .= "\t" . 'include( \'' . POWERED_CACHE_DROPIN_DIR . 'page-cache.php' . '\' );' . PHP_EOL;
246 $string .= '} else {' . PHP_EOL;
247 $string .= "\t" . 'define( \'POWERED_CACHE_PAGE_CACHING_HAS_PROBLEM\', true );' . PHP_EOL;
248 $string .= '}';
249
250
251 return apply_filters( 'powered_cache_advanced_cache_file_content', $string );
252 }
253
254 /**
255 * Define WP_CACHE constant
256 *
257 * @since 1.0
258 * @param $status
259 *
260 * @return bool
261 */
262 public function define_wp_cache( $status ) {
263 global $powered_cache_fs;
264 $config_path = $this->find_wp_config_file();
265
266 if ( ! $config_path ) {
267 return false;
268 }
269
270
271 if ( defined( 'WP_CACHE' ) && WP_CACHE === $status ) {
272 return true;
273 }
274
275 $config_file_string = $powered_cache_fs->get_contents( $config_path );
276
277 // Config file is empty. Maybe couldn't read it?
278 if ( empty( $config_file_string ) ) {
279 return false;
280 }
281
282 $config_file = preg_split( "#(\r\n|\r|\n)#", $config_file_string );
283 $line_key = false;
284
285 foreach ( $config_file as $key => $line ) {
286 if ( ! preg_match( '/^\s*define\(\s*(\'|")([A-Z_]+)(\'|")(.*)/', $line, $match ) ) {
287 continue;
288 }
289
290 if ( $match[2] == 'WP_CACHE' ) {
291 $line_key = $key;
292 }
293 }
294
295 if ( $line_key !== false ) {
296 unset( $config_file[ $line_key ] );
297 }
298
299 $status_string = ( $status ) ? 'true' : 'false';
300
301 array_shift( $config_file );
302 array_unshift( $config_file, '<?php', "define( 'WP_CACHE', $status_string ); // Powered Cache" );
303
304 foreach ( $config_file as $key => $line ) {
305 if ( '' === $line ) {
306 unset( $config_file[$key] );
307 }
308 }
309
310 if ( ! $powered_cache_fs->put_contents( $config_path, implode( PHP_EOL, $config_file ), FS_CHMOD_FILE ) ) {
311 return false;
312 }
313
314 return true;
315 }
316
317
318 /**
319 * seeking wp-config file
320 *
321 * @since 1.0
322 *
323 * @return bool|string
324 */
325 public function find_wp_config_file() {
326 global $powered_cache_fs;
327
328 $file = '/wp-config.php';
329
330 for ( $i = 1; $i <= 3; $i ++ ) {
331 if ( $i > 1 ) {
332 $file = '/..' . $file;
333 }
334
335 if ( $powered_cache_fs->exists( untrailingslashit( ABSPATH ) . $file ) ) {
336 $config_path = untrailingslashit( ABSPATH ) . $file;
337 break;
338 }
339 }
340
341 if ( ! isset( $config_path ) ) {
342 return false;
343 }
344
345 return $config_path;
346 }
347
348 /**
349 * Save settings to file
350 *
351 * @since 1.0
352 * @param $configuration
353 *
354 * @return bool
355 */
356 public function save_to_file( $configuration ) {
357 global $powered_cache_fs;
358
359 $config_dir = WP_CONTENT_DIR . '/pc-config';
360
361 $site_url_parts = parse_url( site_url() );
362 $config_file_name = $site_url_parts['host'];
363
364 if ( is_multisite() && ( defined( 'SUBDOMAIN_INSTALL' ) && false === SUBDOMAIN_INSTALL ) ) {
365 if ( is_main_site( get_current_blog_id() ) ) {
366 $config_file_name .= '-blog';
367 } else {
368 $subdir_name = ltrim( parse_url( get_site_url( get_current_blog_id() ), PHP_URL_PATH ), '/' );
369 $config_file_name .= '-' . $subdir_name;
370 }
371 }
372
373
374 $config_file = $config_dir . '/config-' . $config_file_name . '.php';
375
376 $configuration['cache_location'] = powered_cache_get_cache_dir();
377
378 $powered_cache_fs->mkdir( $config_dir );
379
380 $config_file_string = '<?php' . PHP_EOL . "defined( 'ABSPATH' ) || exit;" . PHP_EOL . 'return ' . var_export( $configuration, true ) . ';' . PHP_EOL;
381
382 if ( ! $powered_cache_fs->put_contents( $config_file, $config_file_string, FS_CHMOD_FILE ) ) {
383 return false;
384 }
385
386 return true;
387 }
388
389 /**
390 * Create .htaccess file based on current setting preferences
391 *
392 * @since 1.0
393 * @return bool
394 */
395 public function configure_htaccess( $enable = true ) {
396 global $powered_cache_fs;
397 $htaccess_file = get_home_path() . '.htaccess';
398
399 /**
400 * Users might want to control .htaccess manually
401 *
402 * @since 1.1.1
403 */
404 if ( true !== apply_filters( 'powered_cache_auto_htaccess_update', true ) ) {
405 return false;
406 }
407
408 if ( $powered_cache_fs->is_writable( $htaccess_file ) ) {
409 $contents = $powered_cache_fs->get_contents( $htaccess_file );
410
411 //clean up
412 $contents = preg_replace( '/# BEGIN POWERED CACHE(.*)# END POWERED CACHE/is', '', $contents );
413
414 if ( false === $enable ) {
415 return $powered_cache_fs->put_contents( $htaccess_file, $contents, FS_CHMOD_FILE );
416 }
417
418 $rules = $this->htaccess_rules();
419 $contents = $rules . $contents;
420
421 // Update the .htacces file
422 if ( ! $powered_cache_fs->put_contents( $htaccess_file, $contents, FS_CHMOD_FILE ) ) {
423 return false;
424 }
425
426 return true;
427 }
428
429 return false;
430 }
431
432
433 /**
434 * Prepares .htaccess rules for the caching
435 *
436 * @since 1.1
437 * @return string $rules
438 */
439 public function htaccess_rules() {
440 $rules = '';
441
442 /**
443 * @since 1.1 $contents parameter removed
444 */
445 $rules .= apply_filters( 'powered_cache_pre_htaccess', '' );
446
447 $rules .= '# BEGIN POWERED CACHE' . PHP_EOL;
448
449 // todo set option here.
450 if ( apply_filters( 'powered_cache_browser_cache', true ) ) {
451 $wp_mime_types = wp_get_mime_types();
452 $mime_types = array_flip( $wp_mime_types );
453 //mimes
454 $rules .= '<IfModule mod_mime.c>' . PHP_EOL;
455 foreach ( $mime_types as $mime_type => $ext ) {
456 $ext_str = '.' . str_replace( '|', ' .', $ext );
457 $rules .= " AddType " . $mime_type . " " . $ext_str . PHP_EOL;
458 }
459 $rules .= "</IfModule>" . PHP_EOL;
460
461 // set expire time
462
463 $rules .= '<IfModule mod_expires.c>' . PHP_EOL;
464 $rules .= " ExpiresActive On" . PHP_EOL;
465 $rules .= ' ExpiresByType text/html "access plus 0 seconds"' . PHP_EOL;
466 $rules .= ' ExpiresByType text/richtext "access plus 0 seconds"' . PHP_EOL;
467 $rules .= ' ExpiresByType image/svg+xml "access plus 0 seconds"' . PHP_EOL;
468 $rules .= ' ExpiresByType text/plain "access plus 0 seconds"' . PHP_EOL;
469 $rules .= ' ExpiresByType text/xsd "access plus 0 seconds"' . PHP_EOL;
470 $rules .= ' ExpiresByType text/xsl "access plus 0 seconds"' . PHP_EOL;
471 $rules .= ' ExpiresByType text/xml "access plus 0 seconds"' . PHP_EOL;
472 $rules .= ' ExpiresByType text/cache-manifest "access plus 0 seconds"' . PHP_EOL;
473
474 foreach ( $mime_types as $mime_type => $ext ) {
475
476 if ( in_array( $mime_type, array( 'text/html', 'text/richtext', 'image/svg+xml', 'text/plain', 'text/xsd', 'text/xsl', 'text/xml', 'text/cache-manifest' ) ) ) {
477 continue;
478 }
479
480 /**
481 * Apache allow both format like A2592000 => "access plus 1 month"
482 * A => access, M => Modified
483 *
484 * @see http://httpd.apache.org/docs/current/mod/mod_expires.html
485 */
486 if ( in_array( $mime_type, array( 'text/css', 'application/javascript' ) ) ) {
487 $expiry_time = apply_filters( 'powered_cache_browser_cache_assets_lifespan', 'access plus 1 year' );
488 } else {
489 $expiry_time = apply_filters( 'powered_cache_browser_cache_default_lifespan', 'access plus 1 month' );
490 }
491 $rules .= ' ExpiresByType ' . $mime_type . ' "' . $expiry_time . '"' . PHP_EOL;
492 }
493 $rules .= "</IfModule>" . PHP_EOL;
494
495 }
496
497
498 // gzip
499 $rules .= '<IfModule mod_deflate.c>' . PHP_EOL;
500 $rules .= ' <IfModule mod_headers.c>' . PHP_EOL;
501 $rules .= ' Header append Vary User-Agent env=!dont-vary' . PHP_EOL;
502 $rules .= ' </IfModule>' . PHP_EOL;
503 $rules .= ' AddOutputFilterByType DEFLATE text/css text/x-component application/x-javascript application/javascript text/javascript text/x-js text/html text/richtext image/svg+xml text/plain text/xsd text/xsl text/xml image/bmp application/java application/msword application/vnd.ms-fontobject application/x-msdownload image/x-icon application/json application/vnd.ms-access application/vnd.ms-project application/x-font-otf application/vnd.ms-opentype application/vnd.oasis.opendocument.database application/vnd.oasis.opendocument.chart application/vnd.oasis.opendocument.formula application/vnd.oasis.opendocument.graphics application/vnd.oasis.opendocument.presentation application/vnd.oasis.opendocument.spreadsheet application/vnd.oasis.opendocument.text audio/ogg application/pdf application/vnd.ms-powerpoint application/x-shockwave-flash image/tiff application/x-font-ttf application/vnd.ms-opentype audio/wav application/vnd.ms-write application/font-woff application/font-woff2 application/vnd.ms-excel' . PHP_EOL;
504 $rules .= ' <IfModule mod_mime.c>' . PHP_EOL;
505 $rules .= ' AddOutputFilter DEFLATE js css htm html xml' . PHP_EOL;
506 $rules .= ' </IfModule>' . PHP_EOL;
507 $rules .= '</IfModule>' . PHP_EOL;
508
509
510 if ( true === powered_cache_get_option( 'gzip_compression' ) ) {
511 $rules .= '<IfModule mod_mime.c>' . PHP_EOL;
512 $rules .= ' AddType text/html .html_gzip' . PHP_EOL;
513 $rules .= ' AddEncoding gzip .html_gzip' . PHP_EOL;
514 $rules .= '</IfModule>' . PHP_EOL;
515 $rules .= '<IfModule mod_setenvif.c>' . PHP_EOL;
516 $rules .= ' SetEnvIfNoCase Request_URI \.html_gzip$ no-gzip' . PHP_EOL;
517 $rules .= '</IfModule>'.PHP_EOL;
518 }
519
520
521 // remove etag
522 $rules .= '<IfModule mod_headers.c>' . PHP_EOL;
523 $rules .= 'Header unset ETag' . PHP_EOL;
524 $rules .= '</IfModule>' . PHP_EOL . PHP_EOL;
525
526
527 // rewrite
528
529 $env_powered_cache_ua = '';
530 $env_powered_cache_ssl = '';
531 $env_powered_cache_enc = '';
532
533
534 $rewrite_base = parse_url( home_url() );
535 $rewrite_base = isset( $rewrite_base['path'] ) ? trailingslashit( $rewrite_base['path'] ) : '/';
536
537 $rules .= '<IfModule mod_rewrite.c>' . PHP_EOL;
538 $rules .= ' RewriteEngine On' . PHP_EOL;
539 $rules .= ' RewriteBase ' . $rewrite_base . PHP_EOL;
540 $rules .= ' AddDefaultCharset UTF-8 ' . PHP_EOL;
541
542
543 if ( true === powered_cache_get_option( 'cache_mobile' ) && true === powered_cache_get_option( 'cache_mobile_separate_file' ) ) {
544 $mobile_browsers = addcslashes( implode( '|', preg_split( '/[\s*,\s*]*,+[\s*,\s*]*/', powered_cache_mobile_browsers() ) ), ' ' );
545 $mobile_prefixes = addcslashes( implode( '|', preg_split( '/[\s*,\s*]*,+[\s*,\s*]*/', powered_cache_mobile_prefixes() ) ), ' ' );
546 // mobile env set
547 $rules .= " RewriteCond %{HTTP_USER_AGENT} (" . $mobile_browsers . ") [NC]" . PHP_EOL;
548 $rules .= " RewriteRule .* - [E=PC_UA:-mobile]" . PHP_EOL;
549 $rules .= " RewriteCond %{HTTP_USER_AGENT} ^(" . $mobile_prefixes . ") [NC]" . PHP_EOL;
550 $rules .= " RewriteRule .* - [E=PC_UA:-mobile]" . PHP_EOL;
551 $env_powered_cache_ua = '%{ENV:PC_UA}';
552 }
553
554 if ( true === powered_cache_get_option( 'ssl_cache' ) ) {
555 $rules .= " RewriteCond %{HTTPS} =on" . PHP_EOL;
556 $rules .= " RewriteRule .* - [E=PC_SSL:-https]" . PHP_EOL;
557 $rules .= " RewriteCond %{SERVER_PORT} =443" . PHP_EOL;
558 $rules .= " RewriteRule .* - [E=PC_SSL:-https]" . PHP_EOL;
559 $env_powered_cache_ssl = '%{ENV:PC_SSL}';
560 }
561
562 if ( true === powered_cache_get_option( 'gzip_compression' ) ) {
563 $rules .= " RewriteCond %{HTTP:Accept-Encoding} gzip" . PHP_EOL;
564 $rules .= " RewriteRule .* - [E=PC_ENC:_gzip]" . PHP_EOL;
565 $env_powered_cache_enc = '%{ENV:PC_ENC}';
566 }
567
568 $rules .= " RewriteCond %{REQUEST_METHOD} !=POST" . PHP_EOL;
569 $rules .= ' RewriteCond %{QUERY_STRING} =""' . PHP_EOL;
570
571 if ( substr( get_option( 'permalink_structure' ), - 1 ) == '/' ) {
572 $rules .= ' RewriteCond %{REQUEST_URI} \/$' . PHP_EOL;
573 }
574
575 // Get root base
576 $site_root = parse_url( site_url() );
577 $site_root = isset( $site_root['path'] ) ? trailingslashit( $site_root['path'] ) : '';
578
579
580 // reject user agent
581 if ( false !== powered_cache_get_option( 'rejected_user_agents' ) ) {
582 $rejected_user_agents = preg_split( '#(\r\n|\r|\n)#', powered_cache_get_option( 'rejected_user_agents' ) );
583 if ( ! empty( $rejected_user_agents ) ) {
584 $rules .= ' RewriteCond %{HTTP_USER_AGENT} !^(' . implode( '|', $rejected_user_agents ) . ').* [NC]' . PHP_EOL;
585 }
586 }
587
588
589 // ignore cookies
590 if ( false !== powered_cache_get_option( 'rejected_uri' ) ) {
591 $cookies = preg_split( '#(\r\n|\r|\n)#', powered_cache_get_option( 'rejected_uri' ) );
592 }
593 $wp_cookies = array( 'wordpressuser_', 'wordpresspass_', 'wordpress_sec_', 'wordpress_logged_in_' );
594 if ( ! empty( $cookies ) ) {
595 $wp_cookies = array_merge( $cookies, $wp_cookies );
596 }
597 $rules .= ' RewriteCond %{HTTP:Cookie} !(' . implode( '|', $wp_cookies ) . ') [NC]' . PHP_EOL;
598
599 // dont cache fbexternal
600 $rules .= ' RewriteCond %{HTTP_USER_AGENT} !^(facebookexternalhit).* [NC]' . PHP_EOL;
601
602
603 $cache_location = powered_cache_get_cache_dir();
604 $cache_location = untrailingslashit( $cache_location ) . '/powered-cache/';
605 if ( strpos( ABSPATH, $cache_location ) === false ) {
606 // clean doc root
607 $cache_path = str_replace( $_SERVER['DOCUMENT_ROOT'], '', $cache_location );
608 } else {
609 $cache_path = $site_root . str_replace( ABSPATH, '', $cache_location );
610 }
611
612
613 if ( apply_filters( 'powered_cache_maybe_1and1_hosting', ( 0 === strpos( $_SERVER['DOCUMENT_ROOT'], '/kunden/homepage/' ) ) ) ) {
614 $rules .= ' RewriteCond "' . str_replace( '/kunden/homepage/', '/', $cache_location ) . '%{HTTP_HOST}' . '%{REQUEST_URI}/index' . $env_powered_cache_ssl . $env_powered_cache_ua . '.html' . $env_powered_cache_enc . '" -f' . PHP_EOL;
615 } else {
616 $rules .= ' RewriteCond "%{DOCUMENT_ROOT}/' . ltrim( $cache_path, '/' ) . '%{HTTP_HOST}' . '%{REQUEST_URI}/index' . $env_powered_cache_ssl . $env_powered_cache_ua . '.html' . $env_powered_cache_enc . '" -f' . PHP_EOL;
617 }
618 $rules .= ' RewriteRule .* "' . $cache_path . '%{HTTP_HOST}' . '%{REQUEST_URI}/index' . $env_powered_cache_ssl . $env_powered_cache_ua . '.html' . $env_powered_cache_enc . '" [L]' . PHP_EOL;
619 $rules .= '</IfModule>' . PHP_EOL;
620 $rules .= '# END POWERED CACHE' . PHP_EOL;
621
622 return $rules;
623 }
624
625
626 /**
627 * Prepares nginx configuration
628 *
629 * @since 1.1
630 * @return string $contents nginx rules
631 */
632 public function nginx_rules() {
633 $contents = '';
634 $contents .= '##### POWERED CACHE CONF #####' . PHP_EOL;
635 $contents .= 'set $cache_uri $request_uri;' . PHP_EOL;
636 $contents .= 'set $pc_ssl "";' . PHP_EOL;
637 $contents .= 'set $pc_enc "";' . PHP_EOL;
638 $contents .= 'set $pc_ua "";' . PHP_EOL . PHP_EOL;
639
640 // post
641 $contents .= '# POST requests and urls with a query string should always go to PHP' . PHP_EOL;
642 $contents .= 'if ($request_method = POST) {' . PHP_EOL;
643 $contents .= ' set $cache_uri \'null cache\';' . PHP_EOL;
644 $contents .= '}' . PHP_EOL . PHP_EOL;
645
646 // query string
647 $contents .= 'if ($query_string != "") {' . PHP_EOL;
648 $contents .= ' set $cache_uri \'null cache\';' . PHP_EOL;
649 $contents .= '}' . PHP_EOL . PHP_EOL;
650
651 // https
652 $contents .= '# HTTPS' . PHP_EOL;
653 $contents .= 'if ($https = "on") {' . PHP_EOL;
654 $contents .= ' set $pc_ssl "-https";' . PHP_EOL;
655 $contents .= '}' . PHP_EOL . PHP_EOL;
656
657 $contents .= '# Don\'t cache uris containing the following segments' . PHP_EOL;
658 $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;
659 $contents .= ' set $cache_uri \'null cache\';' . PHP_EOL;
660 $contents .= '}' . PHP_EOL . PHP_EOL;
661
662 $contents .= '# Don\'t use the cache for logged in users or recent commenters' . PHP_EOL;
663 $contents .= 'if ($http_cookie ~* "comment_author|wordpress_[a-f0-9]+|wp-postpass|wordpress_logged_in|powered_cache_commented_posts") {' . PHP_EOL;
664 $contents .= ' set $cache_uri \'null cache\';' . PHP_EOL;
665 $contents .= '}' . PHP_EOL . PHP_EOL;
666
667 $contents .= 'if ($http_x_wap_profile) {' . PHP_EOL;
668 $contents .= ' set $pc_ua \'-mobile\';' . PHP_EOL;
669 $contents .= '}' . PHP_EOL . PHP_EOL;
670
671 if ( true === powered_cache_get_option( 'cache_mobile' ) && true === powered_cache_get_option( 'cache_mobile_separate_file' ) ) {
672 $mobile_browsers = addcslashes( implode( '|', preg_split( '/[\s*,\s*]*,+[\s*,\s*]*/', powered_cache_mobile_browsers() ) ), ' ' );
673 $mobile_prefixes = addcslashes( implode( '|', preg_split( '/[\s*,\s*]*,+[\s*,\s*]*/', powered_cache_mobile_prefixes() ) ), ' ' );
674
675 $contents .= 'if ($http_user_agent ~* (' . $mobile_browsers . '))' . PHP_EOL;
676 $contents .= ' set $pc_ua \'-mobile\';' . PHP_EOL;
677 $contents .= '}' . PHP_EOL . PHP_EOL;
678
679 $contents .= 'if ($http_user_agent ~* (' . $mobile_prefixes . '))' . PHP_EOL;
680 $contents .= ' set $pc_ua \'-mobile\';' . PHP_EOL;
681 $contents .= '}' . PHP_EOL . PHP_EOL;
682 }
683
684 $contents .= 'location = /favicon.ico { log_not_found off; access_log off; }' . PHP_EOL;
685 $contents .= 'location = /robots.txt { try_files $uri $uri/ /index.php?$args; log_not_found off; access_log off; }' . PHP_EOL . PHP_EOL;
686
687 $contents .= 'location ~* .(jpg|jpeg|png|gif|ico|css|js|woff|svg)$ {' . PHP_EOL;
688 $contents .= ' expires 1M;' . PHP_EOL;
689 $contents .= '}' . PHP_EOL . PHP_EOL;
690
691 $cache_suffix = 'html';
692 if ( true === powered_cache_get_option( 'gzip_compression' ) ) {
693 $cache_suffix .= '_gzip';
694 }
695
696
697 $contents .= 'location / {' . PHP_EOL;
698 $contents .= ' add_header X-Powered-Cache nginx;' . PHP_EOL;
699 $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;
700 $contents .= '}' . PHP_EOL . PHP_EOL;
701
702
703 return $contents;
704 }
705
706
707 /**
708 * Downloads configuration files
709 *
710 * @since 1.1
711 *
712 * @param string $server type supports apache and nginx
713 */
714 public function download_rewrite_rules( $server ) {
715
716 $rules = '';
717 $filename = 'conf';
718
719 if ( 'apache' === $server ) {
720 $rules = $this->htaccess_rules();
721 $filename = '.htaccess_powered_cache';
722 }
723
724 if ( 'nginx' === $server ) {
725 $rules = $this->nginx_rules();
726 $filename = 'poweredcache.conf';
727 }
728
729 nocache_headers();
730 @header( 'Content-Type: text/plain' );
731 @header( 'Content-Disposition: attachment; filename="' . $filename . '"' );
732 @header( 'Content-Transfer-Encoding: binary' );
733 @header( 'Content-Length: ' . strlen( $rules ) );
734 @header( 'Connection: close' );
735 echo $rules;
736 exit;
737 }
738
739
740 }
741