PluginProbe
Autoptimize / 3.0.3
Autoptimize v3.0.3
2.2.2 2.3.0 2.3.1 2.3.2 2.3.3 2.3.4 2.4.0 2.4.1 2.4.2 2.4.3 2.4.4 2.5.0 2.5.1 2.6.0 2.6.1 2.6.2 2.7.0 2.7.1 2.7.2 2.7.3 2.7.4 2.7.5 2.7.6 2.7.7 2.7.8 All 107 releases
← All changes | classes/autoptimizeCache.php +94 -8 2.6.03.0.3 View file →
@@ -39,17 +39,22 @@
39 39 * @param string $ext Extension.
40 40 */
41 41 public function __construct( $md5, $ext = 'php' )
42 42 {
43 + $_min_ext = '';
44 + if ( apply_filters( 'autoptimize_filter_cache_url_add_min_ext', false ) ) {
45 + $_min_ext = '.min';
46 + }
47 +
43 48 $this->cachedir = AUTOPTIMIZE_CACHE_DIR;
44 49 $this->nogzip = AUTOPTIMIZE_CACHE_NOGZIP;
45 50 if ( ! $this->nogzip ) {
46 - $this->filename = AUTOPTIMIZE_CACHEFILE_PREFIX . $md5 . '.php';
51 + $this->filename = AUTOPTIMIZE_CACHEFILE_PREFIX . $md5 . $_min_ext . '.php';
47 52 } else {
48 53 if ( in_array( $ext, array( 'js', 'css' ) ) ) {
49 - $this->filename = $ext . '/' . AUTOPTIMIZE_CACHEFILE_PREFIX . $md5 . '.' . $ext;
54 + $this->filename = $ext . '/' . AUTOPTIMIZE_CACHEFILE_PREFIX . $md5 . $_min_ext . '.' . $ext;
50 55 } else {
51 - $this->filename = AUTOPTIMIZE_CACHEFILE_PREFIX . $md5 . '.' . $ext;
56 + $this->filename = AUTOPTIMIZE_CACHEFILE_PREFIX . $md5 . $_min_ext . '.' . $ext;
52 57 }
53 58 }
54 59 }
55 60
@@ -89,8 +94,13 @@
89 94 * @return void
90 95 */
91 96 public function cache( $data, $mime )
92 97 {
98 + // readonly FS explicitly OK'ed by developer, so just pretend all is OK.
99 + if ( defined( 'AUTOPTIMIZE_CACHE_READONLY' ) ) {
100 + return true;
101 + }
102 +
93 103 // off by default; check if cachedirs exist every time before caching
94 104 //
95 105 // to be activated for users that experience these ugly errors;
96 106 // PHP Warning: file_put_contents failed to open stream: No such file or directory.
@@ -113,9 +123,9 @@
113 123 // save fallback .js or .css file if filter true (to be false by default) but not if snippet or single.
114 124 if ( self::do_fallback() && strpos( $this->filename, '_snippet_' ) === false && strpos( $this->filename, '_single_' ) === false ) {
115 125 $_extension = pathinfo( $this->filename, PATHINFO_EXTENSION );
116 126 $_fallback_file = AUTOPTIMIZE_CACHEFILE_PREFIX . 'fallback.' . $_extension;
117 - if ( ! file_exists( $this->cachedir . $_extension . '/' . $_fallback_file ) ) {
127 + if ( ( 'css' === $_extension || 'js' === $_extension ) && ! file_exists( $this->cachedir . $_extension . '/' . $_fallback_file ) ) {
118 128 file_put_contents( $this->cachedir . $_extension . '/' . $_fallback_file, $data );
119 129 }
120 130 }
121 131
@@ -127,8 +137,12 @@
127 137 file_put_contents( $this->cachedir . $this->filename . '.br', brotli_compress( $data, 11, BROTLI_GENERIC ) );
128 138 }
129 139 }
130 140 }
141 +
142 + // Provide 3rd party action hook for every cache file that is created.
143 + // This hook can for example be used to inject a copy of the created cache file to a other domain.
144 + do_action( 'autoptimize_action_cache_file_created', $this->cachedir . $this->filename );
131 145 }
132 146
133 147 /**
134 148 * Get cache filename.
@@ -367,8 +381,18 @@
367 381 * @return bool
368 382 */
369 383 public static function clearall( $propagate = true )
370 384 {
385 + if ( defined( 'ET_CORE_VERSION' ) && 'Divi' === get_template() ) {
386 + // see https://blog.futtta.be/2018/11/17/warning-divi-purging-autoptimizes-cache/ .
387 + $dbt = debug_backtrace( DEBUG_BACKTRACE_IGNORE_ARGS, 2 );
388 + $caller = isset( $dbt[1]['function'] ) ? $dbt[1]['function'] : null;
389 + if ( $caller === 'et_core_clear_wp_cache' ) {
390 + _doing_it_wrong( 'autoptimizeCache::clearall', 'Divi devs: please don\'t clear Autoptimize\'s cache, it is unneeded and can break sites. You can contact me at futtta@gmail.com to discuss.', 'Autoptimize 2.9.6' );
391 + return false;
392 + }
393 + }
394 +
371 395 if ( ! self::cacheavail() ) {
372 396 return false;
373 397 }
374 398
@@ -519,8 +543,13 @@
519 543 * @return bool
520 544 */
521 545 public static function cacheavail()
522 546 {
547 + // readonly FS explicitly OK'ed by dev, let's assume the cache dirs are there!
548 + if ( defined( 'AUTOPTIMIZE_CACHE_READONLY' ) ) {
549 + return true;
550 + }
551 +
523 552 if ( false === autoptimizeCache::check_and_create_dirs() ) {
524 553 return false;
525 554 }
526 555
@@ -588,9 +617,9 @@
588 617 </Files>
589 618 </IfModule>';
590 619 }
591 620
592 - if ( self::do_fallback() ) {
621 + if ( self::do_fallback() === true ) {
593 622 $content .= "\nErrorDocument 404 " . trailingslashit( parse_url( content_url(), PHP_URL_PATH ) ) . 'autoptimize_404_handler.php';
594 623 }
595 624 @file_put_contents( $htaccess, $content ); // @codingStandardsIgnoreLine
596 625 }
@@ -612,12 +641,16 @@
612 641 $_fallback_filename = 'autoptimize_404_handler.php';
613 642 $_fallback_php = trailingslashit( WP_CONTENT_DIR ) . $_fallback_filename;
614 643 $_fallback_status = true;
615 644
616 - if ( ! file_exists( $_fallback_php ) ) {
645 + if ( ! file_exists( $_fallback_php ) && is_writable( WP_CONTENT_DIR ) ) {
617 646 $_fallback_php_contents = file_get_contents( AUTOPTIMIZE_PLUGIN_DIR . 'config/' . $_fallback_filename );
618 647 $_fallback_php_contents = str_replace( '<?php exit;', '<?php', $_fallback_php_contents );
619 648 $_fallback_php_contents = str_replace( '<!--ao-cache-dir-->', AUTOPTIMIZE_CACHE_DIR, $_fallback_php_contents );
649 + $_fallback_php_contents = str_replace( '<!--ao-cachefile-prefix-->', AUTOPTIMIZE_CACHEFILE_PREFIX, $_fallback_php_contents );
650 + if ( is_multisite() ) {
651 + $_fallback_php_contents = str_replace( '$multisite = false;', '$multisite = true;', $_fallback_php_contents );
652 + }
620 653 if ( apply_filters( 'autoptimize_filter_cache_fallback_log_errors', false ) ) {
621 654 $_fallback_php_contents = str_replace( '// error_log', 'error_log', $_fallback_php_contents );
622 655 }
623 656 $_fallback_status = file_put_contents( $_fallback_php, $_fallback_php_contents );
@@ -627,17 +660,60 @@
627 660 }
628 661
629 662 /**
630 663 * Tells if AO should try to avoid 404's by creating fallback filesize
631 - * and create a php 404 handler and tell .htaccess to redirect to said handler.
664 + * and create a php 404 handler and tell .htaccess to redirect to said handler
665 + * and hook into WordPress to redirect 404 to said handler as well. NGINX users
666 + * are smart enough to get this working, no? ;-)
632 667 *
633 668 * Return bool
634 669 */
635 670 public static function do_fallback() {
636 - return apply_filters( 'autoptimize_filter_cache_do_fallback', false );
671 + static $_do_fallback = null;
672 +
673 + if ( null === $_do_fallback ) {
674 + $_do_fallback = (bool) apply_filters( 'autoptimize_filter_cache_do_fallback', autoptimizeOptionWrapper::get_option( 'autoptimize_cache_fallback', '1' ) );
675 + }
676 +
677 + return $_do_fallback;
637 678 }
638 679
639 680 /**
681 + * Hooks into template_redirect, will act on 404-ing requests for
682 + * Autoptimized files and redirects to the fallback CSS/ JS if available
683 + * and 410'ing ("Gone") if fallback not available.
684 + */
685 + public static function wordpress_notfound_fallback() {
686 + $original_request = strtok( $_SERVER['REQUEST_URI'], '?' );
687 + if ( strpos( $original_request, wp_basename( WP_CONTENT_DIR ) . AUTOPTIMIZE_CACHE_CHILD_DIR ) !== false && is_404() ) {
688 + // make sure this is not considered a 404.
689 + global $wp_query;
690 + $wp_query->is_404 = false;
691 +
692 + // set fallback path.
693 + $js_or_css = pathinfo( $original_request, PATHINFO_EXTENSION );
694 + $fallback_path = AUTOPTIMIZE_CACHE_DIR . $js_or_css . '/autoptimize_fallback.' . $js_or_css;
695 +
696 + // prepare for Shakeeb's Unused CSS files to be 404-handled as well.
697 + if ( strpos( $original_request, 'uucss/uucss-' ) !== false ) {
698 + $original_request = preg_replace( '/uucss\/uucss-[a-z0-9]{32}-/', 'css/', $original_request );
699 + }
700 +
701 + // set fallback URL.
702 + $fallback_target = preg_replace( '/(.*)_(?:[a-z0-9]{32})\.(js|css)$/', '${1}_fallback.${2}', $original_request );
703 +
704 + // redirect to fallback if possible.
705 + if ( $original_request !== $fallback_target && file_exists( $fallback_path ) ) {
706 + // redirect to fallback.
707 + wp_redirect( $fallback_target, 302 );
708 + } else {
709 + // return HTTP 410 (gone) reponse.
710 + status_header( 410 );
711 + }
712 + }
713 + }
714 +
715 + /**
640 716 * Checks if cache dirs exist and create if not.
641 717 * Returns false if not succesful.
642 718 *
643 719 * @return bool
@@ -708,8 +784,12 @@
708 784 } elseif ( function_exists( 'w3tc_pgcache_flush' ) ) {
709 785 w3tc_pgcache_flush();
710 786 } elseif ( function_exists( 'wp_fast_cache_bulk_delete_all' ) ) {
711 787 wp_fast_cache_bulk_delete_all();
788 + } elseif ( function_exists( 'rapidcache_clear_cache' ) ) {
789 + rapidcache_clear_cache();
790 + } elseif ( class_exists( 'Swift_Performance_Cache' ) ) {
791 + Swift_Performance_Cache::clear_all_cache();
712 792 } elseif ( class_exists( 'WpFastestCache' ) ) {
713 793 $wpfc = new WpFastestCache();
714 794 $wpfc->deleteCache();
715 795 } elseif ( class_exists( 'c_ws_plugin__qcache_purging_routines' ) ) {
@@ -747,8 +827,11 @@
747 827 'sslverify' => false,
748 828 'timeout' => 5,
749 829 )
750 830 );
831 + } elseif ( class_exists( 'RaidboxesNginxCacheFunctions' ) ) {
832 + $rb_cache_helper = new RaidboxesNginxCacheFunctions();
833 + $rb_cache_helper->purge_cache();
751 834 } elseif ( defined('NGINX_HELPER_BASENAME') ) {
752 835 do_action( 'rt_nginx_helper_purge_all' );
753 836 } elseif ( file_exists( WP_CONTENT_DIR . '/wp-cache-config.php' ) && function_exists( 'prune_super_cache' ) ) {
754 837 // fallback for WP-Super-Cache
@@ -760,8 +843,11 @@
760 843 } else {
761 844 prune_super_cache( $cache_path . 'supercache/', true );
762 845 prune_super_cache( $cache_path, true );
763 846 }
847 + } elseif ( class_exists( 'NginxCache' ) ) {
848 + $nginx_cache = new NginxCache();
849 + $nginx_cache->purge_zone_once();
764 850 }
765 851 }
766 852 // @codingStandardsIgnoreEnd
767 853 }