PluginProbe
Autoptimize / trunk
Autoptimize vtrunk
3.1.16 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 All 108 releases
← All changes | classes/autoptimizeCache.php +47 -6 2.7.2 → trunk View file →
@@ -90,12 +90,17 @@
90 90 *
91 91 * @param string $data Data to cache.
92 92 * @param string $mime Mimetype.
93 93 *
94 - * @return void
94 + * @return void|bool
95 95 */
96 96 public function cache( $data, $mime )
97 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 +
98 103 // off by default; check if cachedirs exist every time before caching
99 104 //
100 105 // to be activated for users that experience these ugly errors;
101 106 // PHP Warning: file_put_contents failed to open stream: No such file or directory.
@@ -118,9 +123,9 @@
118 123 // save fallback .js or .css file if filter true (to be false by default) but not if snippet or single.
119 124 if ( self::do_fallback() && strpos( $this->filename, '_snippet_' ) === false && strpos( $this->filename, '_single_' ) === false ) {
120 125 $_extension = pathinfo( $this->filename, PATHINFO_EXTENSION );
121 126 $_fallback_file = AUTOPTIMIZE_CACHEFILE_PREFIX . 'fallback.' . $_extension;
122 - if ( ! file_exists( $this->cachedir . $_extension . '/' . $_fallback_file ) ) {
127 + if ( ( 'css' === $_extension || 'js' === $_extension ) && ! file_exists( $this->cachedir . $_extension . '/' . $_fallback_file ) ) {
123 128 file_put_contents( $this->cachedir . $_extension . '/' . $_fallback_file, $data );
124 129 }
125 130 }
126 131
@@ -376,9 +381,21 @@
376 381 * @return bool
377 382 */
378 383 public static function clearall( $propagate = true )
379 384 {
380 - if ( ! self::cacheavail() ) {
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 ( 'et_core_clear_wp_cache' === $caller ) {
390 + if ( apply_filters( 'autoptimize_filter_cache_divi_wrong_complain', true ) ) {
391 + _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 [email protected] to discuss.', 'Autoptimize 2.9.6' );
392 + }
393 + return false;
394 + }
395 + }
396 +
397 + if ( ! self::cacheavail() || true === apply_filters( 'autoptimize_filter_cache_clearall_disabled', false ) ) {
381 398 return false;
382 399 }
383 400
384 401 // TODO/FIXME: If cache is big, switch to advanced/new cache clearing automatically?
@@ -528,8 +545,13 @@
528 545 * @return bool
529 546 */
530 547 public static function cacheavail()
531 548 {
549 + // readonly FS explicitly OK'ed by dev, let's assume the cache dirs are there!
550 + if ( defined( 'AUTOPTIMIZE_CACHE_READONLY' ) ) {
551 + return true;
552 + }
553 +
532 554 if ( false === autoptimizeCache::check_and_create_dirs() ) {
533 555 return false;
534 556 }
535 557
@@ -621,12 +643,16 @@
621 643 $_fallback_filename = 'autoptimize_404_handler.php';
622 644 $_fallback_php = trailingslashit( WP_CONTENT_DIR ) . $_fallback_filename;
623 645 $_fallback_status = true;
624 646
625 - if ( ! file_exists( $_fallback_php ) ) {
647 + if ( ! file_exists( $_fallback_php ) && is_writable( WP_CONTENT_DIR ) ) {
626 648 $_fallback_php_contents = file_get_contents( AUTOPTIMIZE_PLUGIN_DIR . 'config/' . $_fallback_filename );
627 649 $_fallback_php_contents = str_replace( '<?php exit;', '<?php', $_fallback_php_contents );
628 650 $_fallback_php_contents = str_replace( '<!--ao-cache-dir-->', AUTOPTIMIZE_CACHE_DIR, $_fallback_php_contents );
651 + $_fallback_php_contents = str_replace( '<!--ao-cachefile-prefix-->', AUTOPTIMIZE_CACHEFILE_PREFIX, $_fallback_php_contents );
652 + if ( is_multisite() ) {
653 + $_fallback_php_contents = str_replace( '$multisite = false;', '$multisite = true;', $_fallback_php_contents );
654 + }
629 655 if ( apply_filters( 'autoptimize_filter_cache_fallback_log_errors', false ) ) {
630 656 $_fallback_php_contents = str_replace( '// error_log', 'error_log', $_fallback_php_contents );
631 657 }
632 658 $_fallback_status = file_put_contents( $_fallback_php, $_fallback_php_contents );
@@ -646,9 +672,9 @@
646 672 public static function do_fallback() {
647 673 static $_do_fallback = null;
648 674
649 675 if ( null === $_do_fallback ) {
650 - $_do_fallback = (bool) apply_filters( 'autoptimize_filter_cache_do_fallback', autoptimizeOptionWrapper::get_option( 'autoptimize_cache_fallback', '' ) );
676 + $_do_fallback = (bool) apply_filters( 'autoptimize_filter_cache_do_fallback', autoptimizeOptionWrapper::get_option( 'autoptimize_cache_fallback', '1' ) );
651 677 }
652 678
653 679 return $_do_fallback;
654 680 }
@@ -668,8 +694,13 @@
668 694 // set fallback path.
669 695 $js_or_css = pathinfo( $original_request, PATHINFO_EXTENSION );
670 696 $fallback_path = AUTOPTIMIZE_CACHE_DIR . $js_or_css . '/autoptimize_fallback.' . $js_or_css;
671 697
698 + // prepare for Shakeeb's Unused CSS files to be 404-handled as well.
699 + if ( strpos( $original_request, 'uucss/uucss-' ) !== false ) {
700 + $original_request = preg_replace( '/uucss\/uucss-[a-z0-9]{32}-/', 'css/', $original_request );
701 + }
702 +
672 703 // set fallback URL.
673 704 $fallback_target = preg_replace( '/(.*)_(?:[a-z0-9]{32})\.(js|css)$/', '${1}_fallback.${2}', $original_request );
674 705
675 706 // redirect to fallback if possible.
@@ -755,8 +786,12 @@
755 786 } elseif ( function_exists( 'w3tc_pgcache_flush' ) ) {
756 787 w3tc_pgcache_flush();
757 788 } elseif ( function_exists( 'wp_fast_cache_bulk_delete_all' ) ) {
758 789 wp_fast_cache_bulk_delete_all();
790 + } elseif ( function_exists( 'rapidcache_clear_cache' ) ) {
791 + rapidcache_clear_cache();
792 + } elseif ( class_exists( 'Swift_Performance_Cache' ) ) {
793 + Swift_Performance_Cache::clear_all_cache();
759 794 } elseif ( class_exists( 'WpFastestCache' ) ) {
760 795 $wpfc = new WpFastestCache();
761 796 $wpfc->deleteCache();
762 797 } elseif ( class_exists( 'c_ws_plugin__qcache_purging_routines' ) ) {
@@ -789,13 +824,16 @@
789 824 } elseif ( array_key_exists( 'KINSTA_CACHE_ZONE', $_SERVER ) ) {
790 825 $_kinsta_clear_cache_url = 'https://localhost/kinsta-clear-cache-all';
791 826 $_kinsta_response = wp_remote_get(
792 827 $_kinsta_clear_cache_url,
793 - array(
828 + array(
794 829 'sslverify' => false,
795 830 'timeout' => 5,
796 831 )
797 832 );
833 + } elseif ( class_exists( 'RaidboxesNginxCacheFunctions' ) ) {
834 + $rb_cache_helper = new RaidboxesNginxCacheFunctions();
835 + $rb_cache_helper->purge_cache();
798 836 } elseif ( defined('NGINX_HELPER_BASENAME') ) {
799 837 do_action( 'rt_nginx_helper_purge_all' );
800 838 } elseif ( file_exists( WP_CONTENT_DIR . '/wp-cache-config.php' ) && function_exists( 'prune_super_cache' ) ) {
801 839 // fallback for WP-Super-Cache
@@ -807,8 +845,11 @@
807 845 } else {
808 846 prune_super_cache( $cache_path . 'supercache/', true );
809 847 prune_super_cache( $cache_path, true );
810 848 }
849 + } elseif ( class_exists( 'NginxCache' ) ) {
850 + $nginx_cache = new NginxCache();
851 + $nginx_cache->purge_zone_once();
811 852 }
812 853 }
813 854 // @codingStandardsIgnoreEnd
814 855 }