PluginProbe
Powered Cache – Caching and Optimization for WordPress – Easily Improve PageSpeed & Web Vitals Score / trunk
Powered Cache – Caching and Optimization for WordPress – Easily Improve PageSpeed & Web Vitals Score vtrunk
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
← All changes | includes/classes/Config.php +56 -354 2.1trunk View file →
@@ -112,9 +112,9 @@
112 112 public function object_cache_file_content( $backend ) {
113 113 $string = '<?php ' . "\n";
114 114 $string .= "defined( 'ABSPATH' ) || exit;" . PHP_EOL;
115 115 $string .= "define( 'POWERED_OBJECT_CACHE', true );" . PHP_EOL;
116 - $string .= "if ( ! defined( 'WP_CACHE_KEY_SALT' ) ) {" . PHP_EOL;
116 + $string .= "if ( ! defined( 'WP_CACHE_KEY_SALT' ) && defined( 'DB_NAME') ) {" . PHP_EOL;
117 117 $string .= "\t" . "define( 'WP_CACHE_KEY_SALT', DB_NAME );" . PHP_EOL;
118 118 $string .= '}' . PHP_EOL;
119 119
120 120 $object_caches = get_object_cache_dropins();
@@ -124,9 +124,9 @@
124 124 $string .= '} elseif ( @file_exists( \'' . $object_caches[ $backend ] . '\' ) ) {' . PHP_EOL;
125 125 $string .= "\t" . 'include( \'' . $object_caches[ $backend ] . '\' );' . PHP_EOL;
126 126 $string .= '} else {' . PHP_EOL;
127 127 $string .= "\t" . 'define( \'POWERED_OBJECT_CACHE_HAS_PROBLEM\', true );' . PHP_EOL;
128 - $string .= '}';
128 + $string .= '}' . PHP_EOL;
129 129
130 130 /**
131 131 * Filters object-cache.php file contents.
132 132 *
@@ -211,16 +211,17 @@
211 211 $string .= "defined( 'ABSPATH' ) || exit;" . PHP_EOL;
212 212 $string .= "define( 'POWERED_CACHE_PAGE_CACHING', true );" . PHP_EOL . PHP_EOL;
213 213 // lookup order 1) network-wide , 2) subdomain specific (if any) 3) domain specific
214 214 $string .= "\$config_locations[] = WP_CONTENT_DIR . '/pc-config/config-network.php';" . PHP_EOL;
215 + $string .= "\$host = isset( \$_SERVER['HTTP_HOST'] ) ? \$_SERVER['HTTP_HOST'] : '';" . PHP_EOL . PHP_EOL;
215 216 $string .= "if ( is_multisite() && defined( 'SUBDOMAIN_INSTALL' ) && ! SUBDOMAIN_INSTALL ) {" . PHP_EOL;
216 217 $string .= "\t" . "\$request_uri = explode( '/', ltrim( \$_SERVER['REQUEST_URI'], '/' ) );" . PHP_EOL;
217 218 $string .= "\t" . 'if ( ! empty( $request_uri[0] ) ) {' . PHP_EOL;
218 - $string .= "\t" . "\t" . "\$config_locations[] = WP_CONTENT_DIR . '/pc-config/config-' . \$_SERVER['HTTP_HOST'] . '-' . \$request_uri[0] . '.php';" . PHP_EOL;
219 + $string .= "\t" . "\t" . "\$config_locations[] = WP_CONTENT_DIR . '/pc-config/config-' . \$host . '-' . \$request_uri[0] . '.php';" . PHP_EOL;
219 220 $string .= "\t" . '}' . PHP_EOL;
220 221 $string .= '}' . PHP_EOL;
221 222
222 - $string .= "\$config_locations[] = WP_CONTENT_DIR . '/pc-config/config-' . \$_SERVER['HTTP_HOST'] . '.php';" . PHP_EOL . PHP_EOL;
223 + $string .= "\$config_locations[] = WP_CONTENT_DIR . '/pc-config/config-' . \$host . '.php';" . PHP_EOL . PHP_EOL;
223 224
224 225 $string .= 'foreach ( $config_locations as $config_file ) {' . PHP_EOL;
225 226 $string .= "\t" . 'if ( @file_exists( $config_file ) ) {' . PHP_EOL;
226 227 $string .= "\t" . "\t" . 'include( $config_file );' . PHP_EOL;
@@ -302,15 +303,9 @@
302 303
303 304 array_shift( $config_file );
304 305 array_unshift( $config_file, '<?php', "define( 'WP_CACHE', $status_string ); // Powered Cache" );
305 306
306 - foreach ( $config_file as $key => $line ) {
307 - if ( '' === $line ) {
308 - unset( $config_file[ $key ] );
309 - }
310 - }
311 -
312 - if ( ! file_put_contents( $config_path, implode( PHP_EOL, $config_file ) ) ) {
307 + if ( ! @file_put_contents( $config_path, implode( PHP_EOL, $config_file ) ) ) { // phpcs:ignore
313 308 return false;
314 309 }
315 310
316 311 return true;
@@ -398,9 +393,9 @@
398 393 if ( false === $enable ) {
399 394 return file_put_contents( $htaccess_file, $contents );
400 395 }
401 396
402 - $rules = $this->htaccess_rules();
397 + $rules = Htaccess::factory()->htaccess_rules();
403 398 $contents = $rules . $contents;
404 399
405 400 // Update the .htacces file
406 401 if ( ! file_put_contents( $htaccess_file, $contents ) ) {
@@ -416,273 +411,14 @@
416 411 /**
417 412 * Prepares .htaccess rules for the caching
418 413 *
419 414 * @return string $rules
420 - * @since 1.1
415 + * @since 1.1
416 + * @deprecated 2.5.0 Use the {@see '\PoweredCache\Htaccess::factory()->htaccess_rules()'} instead.
421 417 */
422 418 public function htaccess_rules() {
423 - $rules = '';
424 - $settings = \PoweredCache\Utils\get_settings();
419 + $rules = Htaccess::factory()->htaccess_rules();
425 420
426 - /**
427 - * Filters base htaccess rules
428 - *
429 - * @hook powered_cache_pre_htaccess
430 - *
431 - * @param {string} empty htaccesss rules by default
432 - *
433 - * @return {boolean} New value.
434 - *
435 - * @since 1.1
436 - */
437 - $rules .= apply_filters( 'powered_cache_pre_htaccess', '' );
438 -
439 - $rules .= '# BEGIN POWERED CACHE' . PHP_EOL;
440 -
441 - /**
442 - * Filters whether doing the configuration for browser cache or not
443 - *
444 - * @hook powered_cache_browser_cache
445 - *
446 - * @param {boolean} true for creating .htaccess rules for browser cache
447 - *
448 - * @return {boolean} New value.
449 - *
450 - * @since 1.1
451 - */
452 - if ( apply_filters( 'powered_cache_browser_cache', true ) ) {
453 - $wp_mime_types = wp_get_mime_types();
454 - $mime_types = array_flip( $wp_mime_types );
455 - // mimes
456 - $rules .= '<IfModule mod_mime.c>' . PHP_EOL;
457 - foreach ( $mime_types as $mime_type => $ext ) {
458 - $ext_str = '.' . str_replace( '|', ' .', $ext );
459 - $rules .= ' AddType ' . $mime_type . ' ' . $ext_str . PHP_EOL;
460 - }
461 - $rules .= '</IfModule>' . PHP_EOL;
462 -
463 - // set expire time
464 -
465 - $rules .= '<IfModule mod_expires.c>' . PHP_EOL;
466 - $rules .= ' ExpiresActive On' . PHP_EOL;
467 - $rules .= ' ExpiresByType text/html "access plus 0 seconds"' . PHP_EOL;
468 - $rules .= ' ExpiresByType text/richtext "access plus 0 seconds"' . PHP_EOL;
469 - $rules .= ' ExpiresByType text/plain "access plus 0 seconds"' . PHP_EOL;
470 - $rules .= ' ExpiresByType text/xsd "access plus 0 seconds"' . PHP_EOL;
471 - $rules .= ' ExpiresByType text/xsl "access plus 0 seconds"' . PHP_EOL;
472 - $rules .= ' ExpiresByType text/xml "access plus 0 seconds"' . PHP_EOL;
473 - $rules .= ' ExpiresByType text/cache-manifest "access plus 0 seconds"' . PHP_EOL;
474 -
475 - foreach ( $mime_types as $mime_type => $ext ) {
476 -
477 - if ( in_array( $mime_type, array( 'text/html', 'text/richtext', 'text/plain', 'text/xsd', 'text/xsl', 'text/xml', 'text/cache-manifest' ), true ) ) {
478 - continue;
479 - }
480 -
481 - $expiry_time = $this->get_browser_cache_lifespan( $mime_type );
482 -
483 - $rules .= ' ExpiresByType ' . $mime_type . ' "' . $expiry_time . '"' . PHP_EOL;
484 - }
485 -
486 - $font_types = [
487 - 'application/x-font-ttf',
488 - 'application/x-font-woff ',
489 - 'application/x-font-woff2',
490 - 'font/opentype',
491 - 'application/vnd.ms-fontobject',
492 - 'application/font-sfnt',
493 - 'image/svg+xml',
494 - ];
495 -
496 - foreach ( $font_types as $mime_type ) {
497 - $expiry_time = $this->get_browser_cache_lifespan( $mime_type );
498 -
499 - $rules .= ' ExpiresByType ' . $mime_type . ' "' . $expiry_time . '"' . PHP_EOL;
500 - }
501 -
502 - $rules .= '</IfModule>' . PHP_EOL;
503 - }
504 -
505 - // Add cors rules
506 - if ( $settings['enable_cdn'] ) {
507 -
508 - /**
509 - * Add CORS configuration
510 - *
511 - * @link https://developer.mozilla.org/en-US/docs/Web/HTML/CORS_enabled_image
512 - * @since 2.1
513 - */
514 - $rules .= '<IfModule mod_setenvif.c>' . PHP_EOL;
515 - $rules .= ' <IfModule mod_headers.c>' . PHP_EOL;
516 - $rules .= ' <FilesMatch "\.(avifs?|bmp|cur|gif|ico|jpe?g|jxl|a?png|svgz?|webp)$">' . PHP_EOL;
517 - $rules .= ' SetEnvIf Origin ":" IS_CORS' . PHP_EOL;
518 - $rules .= ' Header set Access-Control-Allow-Origin "*" env=IS_CORS' . PHP_EOL;
519 - $rules .= ' </FilesMatch>' . PHP_EOL;
520 - $rules .= ' </IfModule>' . PHP_EOL;
521 - $rules .= '</IfModule>' . PHP_EOL . PHP_EOL;
522 -
523 - // configure fonts
524 - $rules .= '<FilesMatch "\.(ttf|ttc|otf|eot|woff|woff2|font.css)$">' . PHP_EOL;
525 - $rules .= ' <IfModule mod_headers.c>' . PHP_EOL;
526 - $rules .= ' Header set Access-Control-Allow-Origin "*"' . PHP_EOL;
527 - $rules .= ' </IfModule>' . PHP_EOL;
528 - $rules .= '</FilesMatch>' . PHP_EOL . PHP_EOL;
529 - }
530 -
531 - // gzip
532 - $rules .= '<IfModule mod_deflate.c>' . PHP_EOL;
533 - $rules .= ' <IfModule mod_headers.c>' . PHP_EOL;
534 - $rules .= ' Header set X-Powered-By "Powered Cache"' . PHP_EOL;
535 - $rules .= ' Header append Vary User-Agent env=!dont-vary' . PHP_EOL;
536 - $rules .= ' </IfModule>' . PHP_EOL;
537 - $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'
538 - . PHP_EOL; // phpcs:ignore
539 - $rules .= ' <IfModule mod_mime.c>' . PHP_EOL;
540 - $rules .= ' AddOutputFilter DEFLATE js css htm html xml' . PHP_EOL;
541 - $rules .= ' </IfModule>' . PHP_EOL;
542 -
543 - $rules .= '</IfModule>' . PHP_EOL;
544 -
545 - // remove etag
546 - $rules .= '<IfModule mod_headers.c>' . PHP_EOL;
547 - $rules .= 'Header unset ETag' . PHP_EOL;
548 - $rules .= '</IfModule>' . PHP_EOL . PHP_EOL;
549 -
550 - /**
551 - * Filters whether doing the configuration for htaccess rewrite cache or not
552 - *
553 - * @hook powered_cache_mod_rewrite
554 - *
555 - * @param {boolean} true for creating .htaccess rewrite rules.
556 - *
557 - * @return {boolean} New value.
558 - *
559 - * @since 2.0
560 - */
561 - if ( apply_filters( 'powered_cache_mod_rewrite', true ) ) { // rewrite
562 -
563 - // add gzip type for .html.gz format
564 - if ( $settings['gzip_compression'] ) {
565 - $rules .= '<IfModule mod_mime.c>' . PHP_EOL;
566 - $rules .= ' AddType text/html .html.gz' . PHP_EOL;
567 - $rules .= ' AddEncoding gzip .gz' . PHP_EOL;
568 - $rules .= '</IfModule>' . PHP_EOL;
569 - $rules .= '<IfModule mod_setenvif.c>' . PHP_EOL;
570 - $rules .= ' SetEnvIfNoCase Request_URI \.html.gz$ no-gzip' . PHP_EOL;
571 - $rules .= '</IfModule>' . PHP_EOL;
572 - }
573 -
574 - $env_powered_cache_ua = '';
575 - $env_powered_cache_ssl = '';
576 - $env_powered_cache_enc = '';
577 -
578 - $rewrite_base = wp_parse_url( home_url() );
579 - $rewrite_base = isset( $rewrite_base['path'] ) ? trailingslashit( $rewrite_base['path'] ) : '/';
580 -
581 - $rules .= '<IfModule mod_rewrite.c>' . PHP_EOL;
582 - $rules .= ' RewriteEngine On' . PHP_EOL;
583 - $rules .= ' RewriteBase ' . $rewrite_base . PHP_EOL;
584 - $rules .= ' AddDefaultCharset UTF-8 ' . PHP_EOL;
585 -
586 - if ( true === $settings['cache_mobile'] && true === $settings['cache_mobile_separate_file'] ) {
587 - $mobile_browsers = addcslashes( implode( '|', preg_split( '/[\s*,\s*]*,+[\s*,\s*]*/', mobile_browsers() ) ), ' ' );
588 - $mobile_prefixes = addcslashes( implode( '|', preg_split( '/[\s*,\s*]*,+[\s*,\s*]*/', mobile_prefixes() ) ), ' ' );
589 - // mobile env set
590 - $rules .= ' RewriteCond %{HTTP_USER_AGENT} (' . $mobile_browsers . ') [NC]' . PHP_EOL;
591 - $rules .= ' RewriteRule .* - [E=PC_UA:-mobile]' . PHP_EOL;
592 - $rules .= ' RewriteCond %{HTTP_USER_AGENT} ^(' . $mobile_prefixes . ') [NC]' . PHP_EOL;
593 - $rules .= ' RewriteRule .* - [E=PC_UA:-mobile]' . PHP_EOL;
594 - $env_powered_cache_ua = '%{ENV:PC_UA}';
595 - }
596 -
597 - $rules .= ' RewriteCond %{HTTPS} on [OR]' . PHP_EOL;
598 - $rules .= ' RewriteCond %{SERVER_PORT} ^443$ [OR]' . PHP_EOL;
599 - $rules .= ' RewriteCond %{HTTP:X-Forwarded-Proto} https' . PHP_EOL;
600 - $rules .= ' RewriteRule .* - [E=PC_SSL:-https]' . PHP_EOL;
601 - $env_powered_cache_ssl = '%{ENV:PC_SSL}';
602 -
603 - if ( true === $settings['gzip_compression'] ) {
604 - $rules .= ' RewriteCond %{HTTP:Accept-Encoding} gzip' . PHP_EOL;
605 - $rules .= ' RewriteRule .* - [E=PC_ENC:.gz]' . PHP_EOL;
606 - $env_powered_cache_enc = '%{ENV:PC_ENC}';
607 - }
608 -
609 - $rules .= ' RewriteCond %{REQUEST_METHOD} !=POST' . PHP_EOL;
610 - $rules .= ' RewriteCond %{QUERY_STRING} =""' . PHP_EOL;
611 -
612 - if ( permalink_structure_has_trailingslash() ) {
613 - $rules .= ' RewriteCond %{REQUEST_URI} !^.*[^/]$' . PHP_EOL;
614 - $rules .= ' RewriteCond %{REQUEST_URI} !^.*//.*$' . PHP_EOL;
615 - }
616 -
617 - // Get root base
618 - $site_root = wp_parse_url( site_url() );
619 - $site_root = isset( $site_root['path'] ) ? trailingslashit( $site_root['path'] ) : '';
620 -
621 - // reject user agent
622 - $rejected_user_agents = (array) AdvancedCache::get_rejected_user_agents();
623 - if ( ! empty( $rejected_user_agents ) ) {
624 - $rules .= ' RewriteCond %{HTTP_USER_AGENT} !^(' . implode( '|', $rejected_user_agents ) . ').* [NC]' . PHP_EOL;
625 - }
626 -
627 - // rejected cookies
628 - $rejected_cookies = AdvancedCache::get_rejected_cookies();
629 - if ( ! empty( $rejected_cookies ) ) {
630 - $rules .= ' RewriteCond %{HTTP:Cookie} !(' . implode( '|', $rejected_cookies ) . ') [NC]' . PHP_EOL;
631 - }
632 -
633 - // dont cache fbexternal
634 - $rules .= ' RewriteCond %{HTTP_USER_AGENT} !^(facebookexternalhit).* [NC]' . PHP_EOL;
635 -
636 - $cache_location = get_cache_dir();
637 - $cache_location = untrailingslashit( $cache_location ) . '/powered-cache/';
638 - if ( strpos( ABSPATH, $cache_location ) === false ) {
639 - $cache_path = str_replace( $_SERVER['DOCUMENT_ROOT'], '', $cache_location ); // clean doc root
640 - } else {
641 - $cache_path = $site_root . str_replace( ABSPATH, '', $cache_location );
642 - }
643 -
644 - /**
645 - * Filters whether running on 1and1_hosting or not
646 - *
647 - * @hook powered_cache_maybe_1and1_hosting
648 - *
649 - * @param {boolean} $status true if /kunden/homepage directory exists
650 - *
651 - * @return {boolean} New value.
652 - *
653 - * @since 1.1
654 - */
655 - if ( apply_filters( 'powered_cache_maybe_1and1_hosting', ( 0 === strpos( $_SERVER['DOCUMENT_ROOT'], '/kunden/homepage/' ) ) ) ) {
656 - $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;
657 - } else {
658 - $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;
659 - }
660 - $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;
661 -
662 - if ( $settings['gzip_compression'] ) {
663 - $rules .= ' # prevent mod_deflate double gzip' . PHP_EOL;
664 - $rules .= ' RewriteRule \.html\.gz$ - [T=text/html,E=no-gzip:1]' . PHP_EOL;
665 - }
666 -
667 - $rules .= '</IfModule>' . PHP_EOL;
668 - }
669 -
670 - /**
671 - * Filters post htaccess rules
672 - *
673 - * @hook powered_cache_after_htaccess
674 - *
675 - * @param {string} empty htaccesss rules by default
676 - *
677 - * @return {boolean} New value.
678 - *
679 - * @since 2.0
680 - */
681 - $rules .= apply_filters( 'powered_cache_after_htaccess', '' );
682 -
683 - $rules .= '# END POWERED CACHE' . PHP_EOL;
684 -
685 421 return $rules;
686 422 }
687 423
688 424 /**
@@ -698,10 +434,26 @@
698 434 }
699 435
700 436 $file = get_cache_dir() . '.htaccess';
701 437
702 - $file_string = 'Options -Indexes';
438 + $file_string = '<IfModule mod_autoindex.c>' . PHP_EOL;
439 + $file_string .= ' Options -Indexes' . PHP_EOL;
440 + $file_string .= '</IfModule>' . PHP_EOL . PHP_EOL;
441 + $file_string .= '<FilesMatch "^.*-user_.*\.(html|html\.gz)$">' . PHP_EOL;
442 + $file_string .= ' Order Allow,Deny' . PHP_EOL;
443 + $file_string .= ' Deny from all' . PHP_EOL;
444 + $file_string .= '</FilesMatch>' . PHP_EOL;
703 445
446 + /**
447 + * Filters the content of the .htaccess file in the cache directory.
448 + *
449 + * @param {string} $file_string Default configuration
450 + *
451 + * @hook powered_cache_cache_dir_htaccess_file_content
452 + * @since 3.5.3
453 + */
454 + $file_string = apply_filters( 'powered_cache_cache_dir_htaccess_file_content', $file_string );
455 +
704 456 if ( ! file_put_contents( $file, $file_string ) ) {
705 457 return false;
706 458 }
707 459
@@ -764,16 +516,18 @@
764 516 $config_file_string = '<?php' . PHP_EOL . "defined( 'ABSPATH' ) || exit;" . PHP_EOL . PHP_EOL;
765 517
766 518 $config_file_string .= "\$GLOBALS['powered_cache_options'] = " . var_export( $configuration, true ) . ';' . PHP_EOL . PHP_EOL;
767 519
768 - // mobile cache varibales
520 + // mobile cache variables
769 521 $config_file_string .= '$powered_cache_mobile_browsers = ' . var_export( mobile_browsers(), true ) . ';' . PHP_EOL;
770 522 $config_file_string .= '$powered_cache_mobile_prefixes = ' . var_export( mobile_prefixes(), true ) . ';' . PHP_EOL;
771 523 $config_file_string .= '$powered_cache_rejected_user_agents = ' . var_export( AdvancedCache::get_rejected_user_agents(), true ) . ';' . PHP_EOL;
772 524 $config_file_string .= '$powered_cache_rejected_cookies = ' . var_export( AdvancedCache::get_rejected_cookies(), true ) . ';' . PHP_EOL;
525 + $config_file_string .= '$powered_cache_rejected_referrers = ' . var_export( AdvancedCache::get_rejected_referrers(), true ) . ';' . PHP_EOL;
773 526 $config_file_string .= '$powered_cache_vary_cookies = ' . var_export( AdvancedCache::get_vary_cookies(), true ) . ';' . PHP_EOL;
774 527 $config_file_string .= '$powered_cache_rejected_uri = ' . var_export( AdvancedCache::get_rejected_uri(), true ) . ';' . PHP_EOL;
775 - $config_file_string .= '$powered_cache_accepted_query_strings = ' . var_export( AdvancedCache::get_accepted_query_strings(), true ) . ';' . PHP_EOL;
528 + $config_file_string .= '$powered_cache_ignored_query_strings = ' . var_export( AdvancedCache::get_ignored_query_strings(), true ) . ';' . PHP_EOL;
529 + $config_file_string .= '$powered_cache_cache_query_strings = ' . var_export( AdvancedCache::get_cache_query_string(), true ) . ';' . PHP_EOL;
776 530
777 531 if ( permalink_structure_has_trailingslash() ) {
778 532 $config_file_string .= '$powered_cache_slash_check = true;' . PHP_EOL;
779 533 } else {
@@ -796,8 +550,13 @@
796 550 if ( ! file_put_contents( $config_file, $config_file_string ) ) {
797 551 return false;
798 552 }
799 553
554 + // OPCache invalidate
555 + if ( function_exists( 'opcache_invalidate' ) ) {
556 + opcache_invalidate( $config_file, true );
557 + }
558 +
800 559 return true;
801 560 }
802 561
803 562
@@ -843,13 +602,24 @@
843 602 /**
844 603 * Documented in htaccess config
845 604 */
846 605 if ( apply_filters( 'powered_cache_browser_cache', true ) ) {
847 - $contents .= 'location ~* .(jpg|jpeg|png|gif|ico|css|js|svg|eot|woff|woff2|ttf|otf)$ {' . PHP_EOL;
606 + $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;
848 607 $contents .= ' expires 6M;' . PHP_EOL;
849 608 $contents .= '}' . PHP_EOL . PHP_EOL;
850 609 }
851 610
611 + // file optimizer rewrite rule
612 + if ( $settings['rewrite_file_optimizer'] ) {
613 + $file_optimizer_path = \PoweredCache\Optimizer\Helper::get_file_optimizer_relative_path();
614 +
615 + $contents .= 'location /_static/ {' . PHP_EOL;
616 + $contents .= ' fastcgi_pass unix:/var/run/fastcgi.sock;' . PHP_EOL;
617 + $contents .= ' include /etc/nginx/fastcgi_params;' . PHP_EOL;
618 + $contents .= ' fastcgi_param SCRIPT_FILENAME $document_root/' . $file_optimizer_path . ';' . PHP_EOL;
619 + $contents .= '}' . PHP_EOL . PHP_EOL;
620 + }
621 +
852 622 // https
853 623 $contents .= '# HTTPS' . PHP_EOL;
854 624 $contents .= 'if ($https = "on") {' . PHP_EOL;
855 625 $contents .= ' set $pc_ssl "-https";' . PHP_EOL;
@@ -862,9 +632,9 @@
862 632
863 633 $rejected_user_agents = (array) AdvancedCache::get_rejected_user_agents();
864 634
865 635 $contents .= '# Don\'t use the cache for rejected agents' . PHP_EOL;
866 - $contents .= 'if ($http_user_agent ~* "(' . implode( '|', $rejected_user_agents ) . '")) {' . PHP_EOL;
636 + $contents .= 'if ($http_user_agent ~* "(' . implode( '|', $rejected_user_agents ) . ')") {' . PHP_EOL;
867 637 $contents .= ' set $cache_uri \'null cache\';' . PHP_EOL;
868 638 $contents .= '}' . PHP_EOL . PHP_EOL;
869 639
870 640 $rejected_cookies = (array) AdvancedCache::get_rejected_cookies();
@@ -931,9 +701,9 @@
931 701 $rules = '';
932 702 $filename = 'conf';
933 703
934 704 if ( 'apache' === $server ) {
935 - $rules = $this->htaccess_rules();
705 + $rules = Htaccess::factory()->htaccess_rules();
936 706 $filename = '.htaccess_powered_cache';
937 707 }
938 708
939 709 if ( 'nginx' === $server ) {
@@ -961,13 +731,17 @@
961 731 */
962 732 public function save_configuration( $settings, $network_wide = false ) {
963 733
964 734 if ( can_configure_object_cache() ) {
965 - $this->setup_object_cache( $settings['object_cache'] );
735 + if ( ! empty( $settings['dev_mode'] ) ) {
736 + $this->setup_object_cache(); // dev mode, no object cache
737 + } else {
738 + $this->setup_object_cache( $settings['object_cache'] );
739 + }
966 740 }
967 741
968 742 $this->setup_page_cache( $settings['enable_page_cache'] );
969 - $private_settings = [ 'cloudflare_email', 'cloudflare_api_key', 'cloudflare_zone' ];
743 + $private_settings = [ 'cloudflare_email', 'cloudflare_api_key', 'cloudflare_api_token', 'cloudflare_zone' ];
970 744
971 745 foreach ( $private_settings as $setting_key ) {
972 746 unset( $settings[ $setting_key ] );
973 747 }
@@ -1018,78 +792,6 @@
1018 792 */
1019 793 do_action( 'powered_cache_after_clean_up' );
1020 794
1021 795 }
1022 -
1023 - /**
1024 - * Apache allows both format like A2592000 => "access plus 1 month"
1025 - * A => access, M => Modified
1026 - *
1027 - * @param string $mime_type Mimetype
1028 - *
1029 - * @return string cache lifespan for apache
1030 - * @see http://httpd.apache.org/docs/current/mod/mod_expires.html
1031 - * @since 2.0
1032 - */
1033 - public function get_browser_cache_lifespan( $mime_type ) {
1034 - switch ( $mime_type ) {
1035 - case 'text/css':
1036 - case 'application/javascript':
1037 - /**
1038 - * Filters TTL for CSS/JS files.
1039 - *
1040 - * @hook powered_cache_browser_cache_assets_lifespan
1041 - *
1042 - * @param {string} $expiry_time .htaccess lifespan
1043 - *
1044 - * @return {string} New value.
1045 - *
1046 - * @since 1.1
1047 - */
1048 - $expiry_time = apply_filters( 'powered_cache_browser_cache_assets_lifespan', 'access plus 1 year' );
1049 - break;
1050 - case 'image/jpeg':
1051 - case 'image/gif':
1052 - case 'image/png':
1053 - case 'image/bmp':
1054 - case 'image/tiff':
1055 - case 'image/webp':
1056 - case 'image/heic':
1057 - $expiry_time = 'access plus 6 months';
1058 - break;
1059 - case 'image/x-icon':
1060 - $expiry_time = 'access plus 1 week'; // favicon
1061 - break;
1062 - default:
1063 - /**
1064 - * Filters default TTL for browser cache lifespan.
1065 - *
1066 - * @hook powered_cache_browser_cache_default_lifespan
1067 - *
1068 - * @param {string} $expiry_time .htaccess lifespan
1069 - *
1070 - * @return {string} New value.
1071 - *
1072 - * @since 1.1
1073 - */
1074 - $expiry_time = apply_filters( 'powered_cache_browser_cache_default_lifespan', 'access plus 1 month' );
1075 - }
1076 -
1077 - /**
1078 - * Filters TTL for browser cache lifespan.
1079 - *
1080 - * @hook powered_cache_browser_cache_lifespan
1081 - *
1082 - * @param {string} $expiry_time .htaccess lifespan
1083 - * @param {string} $mime_type mime type
1084 - *
1085 - * @return {string} New value.
1086 - *
1087 - * @since 2.0
1088 - */
1089 - $expiry_time = apply_filters( 'powered_cache_browser_cache_lifespan', $expiry_time, $mime_type );
1090 -
1091 - return $expiry_time;
1092 - }
1093 -
1094 796
1095 797 }