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 +64 -329 2.0trunk 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;
@@ -392,15 +387,15 @@
392 387 if ( is_writable( $htaccess_file ) ) {
393 388 $contents = file_get_contents( $htaccess_file );
394 389
395 390 // clean up
396 - $contents = preg_replace( '/# BEGIN POWERED CACHE(.*)# END POWERED CACHE/is', '', $contents );
391 + $contents = preg_replace( '/# BEGIN POWERED CACHE(.*)# END POWERED CACHE\s*?/isU', '', $contents );
397 392
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,247 +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 - // gzip
506 - $rules .= '<IfModule mod_deflate.c>' . PHP_EOL;
507 - $rules .= ' <IfModule mod_headers.c>' . PHP_EOL;
508 - $rules .= ' Header set X-Powered-By "Powered Cache"' . PHP_EOL;
509 - $rules .= ' Header append Vary User-Agent env=!dont-vary' . PHP_EOL;
510 - $rules .= ' </IfModule>' . PHP_EOL;
511 - $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'
512 - . PHP_EOL; // phpcs:ignore
513 - $rules .= ' <IfModule mod_mime.c>' . PHP_EOL;
514 - $rules .= ' AddOutputFilter DEFLATE js css htm html xml' . PHP_EOL;
515 - $rules .= ' </IfModule>' . PHP_EOL;
516 -
517 - $rules .= '</IfModule>' . PHP_EOL;
518 -
519 - // remove etag
520 - $rules .= '<IfModule mod_headers.c>' . PHP_EOL;
521 - $rules .= 'Header unset ETag' . PHP_EOL;
522 - $rules .= '</IfModule>' . PHP_EOL . PHP_EOL;
523 -
524 - /**
525 - * Filters whether doing the configuration for htaccess rewrite cache or not
526 - *
527 - * @hook powered_cache_mod_rewrite
528 - *
529 - * @param {boolean} true for creating .htaccess rewrite rules.
530 - *
531 - * @return {boolean} New value.
532 - *
533 - * @since 2.0
534 - */
535 - if ( apply_filters( 'powered_cache_mod_rewrite', true ) ) { // rewrite
536 -
537 - // add gzip type for .html.gz format
538 - if ( $settings['gzip_compression'] ) {
539 - $rules .= '<IfModule mod_mime.c>' . PHP_EOL;
540 - $rules .= ' AddType text/html .html.gz' . PHP_EOL;
541 - $rules .= ' AddEncoding gzip .gz' . PHP_EOL;
542 - $rules .= '</IfModule>' . PHP_EOL;
543 - $rules .= '<IfModule mod_setenvif.c>' . PHP_EOL;
544 - $rules .= ' SetEnvIfNoCase Request_URI \.html.gz$ no-gzip' . PHP_EOL;
545 - $rules .= '</IfModule>' . PHP_EOL;
546 - }
547 -
548 - $env_powered_cache_ua = '';
549 - $env_powered_cache_ssl = '';
550 - $env_powered_cache_enc = '';
551 -
552 - $rewrite_base = wp_parse_url( home_url() );
553 - $rewrite_base = isset( $rewrite_base['path'] ) ? trailingslashit( $rewrite_base['path'] ) : '/';
554 -
555 - $rules .= '<IfModule mod_rewrite.c>' . PHP_EOL;
556 - $rules .= ' RewriteEngine On' . PHP_EOL;
557 - $rules .= ' RewriteBase ' . $rewrite_base . PHP_EOL;
558 - $rules .= ' AddDefaultCharset UTF-8 ' . PHP_EOL;
559 -
560 - if ( true === $settings['cache_mobile'] && true === $settings['cache_mobile_separate_file'] ) {
561 - $mobile_browsers = addcslashes( implode( '|', preg_split( '/[\s*,\s*]*,+[\s*,\s*]*/', mobile_browsers() ) ), ' ' );
562 - $mobile_prefixes = addcslashes( implode( '|', preg_split( '/[\s*,\s*]*,+[\s*,\s*]*/', mobile_prefixes() ) ), ' ' );
563 - // mobile env set
564 - $rules .= ' RewriteCond %{HTTP_USER_AGENT} (' . $mobile_browsers . ') [NC]' . PHP_EOL;
565 - $rules .= ' RewriteRule .* - [E=PC_UA:-mobile]' . PHP_EOL;
566 - $rules .= ' RewriteCond %{HTTP_USER_AGENT} ^(' . $mobile_prefixes . ') [NC]' . PHP_EOL;
567 - $rules .= ' RewriteRule .* - [E=PC_UA:-mobile]' . PHP_EOL;
568 - $env_powered_cache_ua = '%{ENV:PC_UA}';
569 - }
570 -
571 - $rules .= ' RewriteCond %{HTTPS} on [OR]' . PHP_EOL;
572 - $rules .= ' RewriteCond %{SERVER_PORT} ^443$ [OR]' . PHP_EOL;
573 - $rules .= ' RewriteCond %{HTTP:X-Forwarded-Proto} https' . PHP_EOL;
574 - $rules .= ' RewriteRule .* - [E=PC_SSL:-https]' . PHP_EOL;
575 - $env_powered_cache_ssl = '%{ENV:PC_SSL}';
576 -
577 - if ( true === $settings['gzip_compression'] ) {
578 - $rules .= ' RewriteCond %{HTTP:Accept-Encoding} gzip' . PHP_EOL;
579 - $rules .= ' RewriteRule .* - [E=PC_ENC:.gz]' . PHP_EOL;
580 - $env_powered_cache_enc = '%{ENV:PC_ENC}';
581 - }
582 -
583 - $rules .= ' RewriteCond %{REQUEST_METHOD} !=POST' . PHP_EOL;
584 - $rules .= ' RewriteCond %{QUERY_STRING} =""' . PHP_EOL;
585 -
586 - if ( permalink_structure_has_trailingslash() ) {
587 - $rules .= ' RewriteCond %{REQUEST_URI} !^.*[^/]$' . PHP_EOL;
588 - $rules .= ' RewriteCond %{REQUEST_URI} !^.*//.*$' . PHP_EOL;
589 - }
590 -
591 - // Get root base
592 - $site_root = wp_parse_url( site_url() );
593 - $site_root = isset( $site_root['path'] ) ? trailingslashit( $site_root['path'] ) : '';
594 -
595 - // reject user agent
596 - $rejected_user_agents = (array) AdvancedCache::get_rejected_user_agents();
597 - if ( ! empty( $rejected_user_agents ) ) {
598 - $rules .= ' RewriteCond %{HTTP_USER_AGENT} !^(' . implode( '|', $rejected_user_agents ) . ').* [NC]' . PHP_EOL;
599 - }
600 -
601 - // rejected cookies
602 - $rejected_cookies = AdvancedCache::get_rejected_cookies();
603 - if ( ! empty( $rejected_cookies ) ) {
604 - $rules .= ' RewriteCond %{HTTP:Cookie} !(' . implode( '|', $rejected_cookies ) . ') [NC]' . PHP_EOL;
605 - }
606 -
607 - // dont cache fbexternal
608 - $rules .= ' RewriteCond %{HTTP_USER_AGENT} !^(facebookexternalhit).* [NC]' . PHP_EOL;
609 -
610 - $cache_location = get_cache_dir();
611 - $cache_location = untrailingslashit( $cache_location ) . '/powered-cache/';
612 - if ( strpos( ABSPATH, $cache_location ) === false ) {
613 - $cache_path = str_replace( $_SERVER['DOCUMENT_ROOT'], '', $cache_location ); // clean doc root
614 - } else {
615 - $cache_path = $site_root . str_replace( ABSPATH, '', $cache_location );
616 - }
617 -
618 - /**
619 - * Filters whether running on 1and1_hosting or not
620 - *
621 - * @hook powered_cache_maybe_1and1_hosting
622 - *
623 - * @param {boolean} $status true if /kunden/homepage directory exists
624 - *
625 - * @return {boolean} New value.
626 - *
627 - * @since 1.1
628 - */
629 - if ( apply_filters( 'powered_cache_maybe_1and1_hosting', ( 0 === strpos( $_SERVER['DOCUMENT_ROOT'], '/kunden/homepage/' ) ) ) ) {
630 - $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;
631 - } else {
632 - $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;
633 - }
634 - $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;
635 -
636 - if ( $settings['gzip_compression'] ) {
637 - $rules .= ' # prevent mod_deflate double gzip' . PHP_EOL;
638 - $rules .= ' RewriteRule \.html\.gz$ - [T=text/html,E=no-gzip:1]' . PHP_EOL;
639 - }
640 -
641 - $rules .= '</IfModule>' . PHP_EOL;
642 - }
643 -
644 - /**
645 - * Filters post htaccess rules
646 - *
647 - * @hook powered_cache_after_htaccess
648 - *
649 - * @param {string} empty htaccesss rules by default
650 - *
651 - * @return {boolean} New value.
652 - *
653 - * @since 2.0
654 - */
655 - $rules .= apply_filters( 'powered_cache_after_htaccess', '' );
656 -
657 - $rules .= '# END POWERED CACHE';
658 -
659 421 return $rules;
660 422 }
661 423
662 424 /**
@@ -672,10 +434,26 @@
672 434 }
673 435
674 436 $file = get_cache_dir() . '.htaccess';
675 437
676 - $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;
677 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 +
678 456 if ( ! file_put_contents( $file, $file_string ) ) {
679 457 return false;
680 458 }
681 459
@@ -738,16 +516,18 @@
738 516 $config_file_string = '<?php' . PHP_EOL . "defined( 'ABSPATH' ) || exit;" . PHP_EOL . PHP_EOL;
739 517
740 518 $config_file_string .= "\$GLOBALS['powered_cache_options'] = " . var_export( $configuration, true ) . ';' . PHP_EOL . PHP_EOL;
741 519
742 - // mobile cache varibales
520 + // mobile cache variables
743 521 $config_file_string .= '$powered_cache_mobile_browsers = ' . var_export( mobile_browsers(), true ) . ';' . PHP_EOL;
744 522 $config_file_string .= '$powered_cache_mobile_prefixes = ' . var_export( mobile_prefixes(), true ) . ';' . PHP_EOL;
745 523 $config_file_string .= '$powered_cache_rejected_user_agents = ' . var_export( AdvancedCache::get_rejected_user_agents(), true ) . ';' . PHP_EOL;
746 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;
747 526 $config_file_string .= '$powered_cache_vary_cookies = ' . var_export( AdvancedCache::get_vary_cookies(), true ) . ';' . PHP_EOL;
748 527 $config_file_string .= '$powered_cache_rejected_uri = ' . var_export( AdvancedCache::get_rejected_uri(), true ) . ';' . PHP_EOL;
749 - $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;
750 530
751 531 if ( permalink_structure_has_trailingslash() ) {
752 532 $config_file_string .= '$powered_cache_slash_check = true;' . PHP_EOL;
753 533 } else {
@@ -770,8 +550,13 @@
770 550 if ( ! file_put_contents( $config_file, $config_file_string ) ) {
771 551 return false;
772 552 }
773 553
554 + // OPCache invalidate
555 + if ( function_exists( 'opcache_invalidate' ) ) {
556 + opcache_invalidate( $config_file, true );
557 + }
558 +
774 559 return true;
775 560 }
776 561
777 562
@@ -806,17 +591,35 @@
806 591 $contents .= 'if ($query_string != "") {' . PHP_EOL;
807 592 $contents .= ' set $cache_uri \'null cache\';' . PHP_EOL;
808 593 $contents .= '}' . PHP_EOL . PHP_EOL;
809 594
595 + // Add CORS rules
596 + if ( $settings['enable_cdn'] ) {
597 + $contents .= 'location ~* .(jpg|jpeg|png|gif|ico|css|js|svg|eot|woff|woff2|ttf|otf)$ {' . PHP_EOL;
598 + $contents .= 'add_header Access-Control-Allow-Origin *;' . PHP_EOL;
599 + $contents .= '}' . PHP_EOL . PHP_EOL;
600 + }
601 +
810 602 /**
811 603 * Documented in htaccess config
812 604 */
813 605 if ( apply_filters( 'powered_cache_browser_cache', true ) ) {
814 - $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;
815 607 $contents .= ' expires 6M;' . PHP_EOL;
816 608 $contents .= '}' . PHP_EOL . PHP_EOL;
817 609 }
818 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 +
819 622 // https
820 623 $contents .= '# HTTPS' . PHP_EOL;
821 624 $contents .= 'if ($https = "on") {' . PHP_EOL;
822 625 $contents .= ' set $pc_ssl "-https";' . PHP_EOL;
@@ -829,9 +632,9 @@
829 632
830 633 $rejected_user_agents = (array) AdvancedCache::get_rejected_user_agents();
831 634
832 635 $contents .= '# Don\'t use the cache for rejected agents' . PHP_EOL;
833 - $contents .= 'if ($http_user_agent ~* "(' . implode( '|', $rejected_user_agents ) . '")) {' . PHP_EOL;
636 + $contents .= 'if ($http_user_agent ~* "(' . implode( '|', $rejected_user_agents ) . ')") {' . PHP_EOL;
834 637 $contents .= ' set $cache_uri \'null cache\';' . PHP_EOL;
835 638 $contents .= '}' . PHP_EOL . PHP_EOL;
836 639
837 640 $rejected_cookies = (array) AdvancedCache::get_rejected_cookies();
@@ -898,9 +701,9 @@
898 701 $rules = '';
899 702 $filename = 'conf';
900 703
901 704 if ( 'apache' === $server ) {
902 - $rules = $this->htaccess_rules();
705 + $rules = Htaccess::factory()->htaccess_rules();
903 706 $filename = '.htaccess_powered_cache';
904 707 }
905 708
906 709 if ( 'nginx' === $server ) {
@@ -928,13 +731,17 @@
928 731 */
929 732 public function save_configuration( $settings, $network_wide = false ) {
930 733
931 734 if ( can_configure_object_cache() ) {
932 - $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 + }
933 740 }
934 741
935 742 $this->setup_page_cache( $settings['enable_page_cache'] );
936 - $private_settings = [ 'cloudflare_email', 'cloudflare_api_key', 'cloudflare_zone' ];
743 + $private_settings = [ 'cloudflare_email', 'cloudflare_api_key', 'cloudflare_api_token', 'cloudflare_zone' ];
937 744
938 745 foreach ( $private_settings as $setting_key ) {
939 746 unset( $settings[ $setting_key ] );
940 747 }
@@ -985,78 +792,6 @@
985 792 */
986 793 do_action( 'powered_cache_after_clean_up' );
987 794
988 795 }
989 -
990 - /**
991 - * Apache allows both format like A2592000 => "access plus 1 month"
992 - * A => access, M => Modified
993 - *
994 - * @param string $mime_type Mimetype
995 - *
996 - * @return string cache lifespan for apache
997 - * @see http://httpd.apache.org/docs/current/mod/mod_expires.html
998 - * @since 2.0
999 - */
1000 - public function get_browser_cache_lifespan( $mime_type ) {
1001 - switch ( $mime_type ) {
1002 - case 'text/css':
1003 - case 'application/javascript':
1004 - /**
1005 - * Filters TTL for CSS/JS files.
1006 - *
1007 - * @hook powered_cache_browser_cache_assets_lifespan
1008 - *
1009 - * @param {string} $expiry_time .htaccess lifespan
1010 - *
1011 - * @return {string} New value.
1012 - *
1013 - * @since 1.1
1014 - */
1015 - $expiry_time = apply_filters( 'powered_cache_browser_cache_assets_lifespan', 'access plus 1 year' );
1016 - break;
1017 - case 'image/jpeg':
1018 - case 'image/gif':
1019 - case 'image/png':
1020 - case 'image/bmp':
1021 - case 'image/tiff':
1022 - case 'image/webp':
1023 - case 'image/heic':
1024 - $expiry_time = 'access plus 6 months';
1025 - break;
1026 - case 'image/x-icon':
1027 - $expiry_time = 'access plus 1 week'; // favicon
1028 - break;
1029 - default:
1030 - /**
1031 - * Filters default TTL for browser cache lifespan.
1032 - *
1033 - * @hook powered_cache_browser_cache_default_lifespan
1034 - *
1035 - * @param {string} $expiry_time .htaccess lifespan
1036 - *
1037 - * @return {string} New value.
1038 - *
1039 - * @since 1.1
1040 - */
1041 - $expiry_time = apply_filters( 'powered_cache_browser_cache_default_lifespan', 'access plus 1 month' );
1042 - }
1043 -
1044 - /**
1045 - * Filters TTL for browser cache lifespan.
1046 - *
1047 - * @hook powered_cache_browser_cache_lifespan
1048 - *
1049 - * @param {string} $expiry_time .htaccess lifespan
1050 - * @param {string} $mime_type mime type
1051 - *
1052 - * @return {string} New value.
1053 - *
1054 - * @since 2.0
1055 - */
1056 - $expiry_time = apply_filters( 'powered_cache_browser_cache_lifespan', $expiry_time, $mime_type );
1057 -
1058 - return $expiry_time;
1059 - }
1060 -
1061 796
1062 797 }