| @@ -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,19 @@ | ||
| 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 | + _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 | + | |
| 395 | + if ( ! self::cacheavail() || true === apply_filters( 'autoptimize_filter_cache_clearall_disabled', false ) ) { | |
| 381 | 396 | return false; |
| 382 | 397 | } |
| 383 | 398 | |
| 384 | 399 | // TODO/FIXME: If cache is big, switch to advanced/new cache clearing automatically? |
| @@ -528,8 +543,13 @@ | ||
| 528 | 543 | * @return bool |
| 529 | 544 | */ |
| 530 | 545 | public static function cacheavail() |
| 531 | 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 | + | |
| 532 | 552 | if ( false === autoptimizeCache::check_and_create_dirs() ) { |
| 533 | 553 | return false; |
| 534 | 554 | } |
| 535 | 555 | |
| @@ -621,12 +641,16 @@ | ||
| 621 | 641 | $_fallback_filename = 'autoptimize_404_handler.php'; |
| 622 | 642 | $_fallback_php = trailingslashit( WP_CONTENT_DIR ) . $_fallback_filename; |
| 623 | 643 | $_fallback_status = true; |
| 624 | 644 | |
| 625 | - if ( ! file_exists( $_fallback_php ) ) { | |
| 645 | + if ( ! file_exists( $_fallback_php ) && is_writable( WP_CONTENT_DIR ) ) { | |
| 626 | 646 | $_fallback_php_contents = file_get_contents( AUTOPTIMIZE_PLUGIN_DIR . 'config/' . $_fallback_filename ); |
| 627 | 647 | $_fallback_php_contents = str_replace( '<?php exit;', '<?php', $_fallback_php_contents ); |
| 628 | 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 | + } | |
| 629 | 653 | if ( apply_filters( 'autoptimize_filter_cache_fallback_log_errors', false ) ) { |
| 630 | 654 | $_fallback_php_contents = str_replace( '// error_log', 'error_log', $_fallback_php_contents ); |
| 631 | 655 | } |
| 632 | 656 | $_fallback_status = file_put_contents( $_fallback_php, $_fallback_php_contents ); |
| @@ -646,9 +670,9 @@ | ||
| 646 | 670 | public static function do_fallback() { |
| 647 | 671 | static $_do_fallback = null; |
| 648 | 672 | |
| 649 | 673 | if ( null === $_do_fallback ) { |
| 650 | - $_do_fallback = (bool) apply_filters( 'autoptimize_filter_cache_do_fallback', autoptimizeOptionWrapper::get_option( 'autoptimize_cache_fallback', '' ) ); | |
| 674 | + $_do_fallback = (bool) apply_filters( 'autoptimize_filter_cache_do_fallback', autoptimizeOptionWrapper::get_option( 'autoptimize_cache_fallback', '1' ) ); | |
| 651 | 675 | } |
| 652 | 676 | |
| 653 | 677 | return $_do_fallback; |
| 654 | 678 | } |
| @@ -668,8 +692,13 @@ | ||
| 668 | 692 | // set fallback path. |
| 669 | 693 | $js_or_css = pathinfo( $original_request, PATHINFO_EXTENSION ); |
| 670 | 694 | $fallback_path = AUTOPTIMIZE_CACHE_DIR . $js_or_css . '/autoptimize_fallback.' . $js_or_css; |
| 671 | 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 | + | |
| 672 | 701 | // set fallback URL. |
| 673 | 702 | $fallback_target = preg_replace( '/(.*)_(?:[a-z0-9]{32})\.(js|css)$/', '${1}_fallback.${2}', $original_request ); |
| 674 | 703 | |
| 675 | 704 | // redirect to fallback if possible. |
| @@ -755,8 +784,12 @@ | ||
| 755 | 784 | } elseif ( function_exists( 'w3tc_pgcache_flush' ) ) { |
| 756 | 785 | w3tc_pgcache_flush(); |
| 757 | 786 | } elseif ( function_exists( 'wp_fast_cache_bulk_delete_all' ) ) { |
| 758 | 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(); | |
| 759 | 792 | } elseif ( class_exists( 'WpFastestCache' ) ) { |
| 760 | 793 | $wpfc = new WpFastestCache(); |
| 761 | 794 | $wpfc->deleteCache(); |
| 762 | 795 | } elseif ( class_exists( 'c_ws_plugin__qcache_purging_routines' ) ) { |
| @@ -789,13 +822,16 @@ | ||
| 789 | 822 | } elseif ( array_key_exists( 'KINSTA_CACHE_ZONE', $_SERVER ) ) { |
| 790 | 823 | $_kinsta_clear_cache_url = 'https://localhost/kinsta-clear-cache-all'; |
| 791 | 824 | $_kinsta_response = wp_remote_get( |
| 792 | 825 | $_kinsta_clear_cache_url, |
| 793 | - array( | |
| 826 | + array( | |
| 794 | 827 | 'sslverify' => false, |
| 795 | 828 | 'timeout' => 5, |
| 796 | 829 | ) |
| 797 | 830 | ); |
| 831 | + } elseif ( class_exists( 'RaidboxesNginxCacheFunctions' ) ) { | |
| 832 | + $rb_cache_helper = new RaidboxesNginxCacheFunctions(); | |
| 833 | + $rb_cache_helper->purge_cache(); | |
| 798 | 834 | } elseif ( defined('NGINX_HELPER_BASENAME') ) { |
| 799 | 835 | do_action( 'rt_nginx_helper_purge_all' ); |
| 800 | 836 | } elseif ( file_exists( WP_CONTENT_DIR . '/wp-cache-config.php' ) && function_exists( 'prune_super_cache' ) ) { |
| 801 | 837 | // fallback for WP-Super-Cache |
| @@ -807,8 +843,11 @@ | ||
| 807 | 843 | } else { |
| 808 | 844 | prune_super_cache( $cache_path . 'supercache/', true ); |
| 809 | 845 | prune_super_cache( $cache_path, true ); |
| 810 | 846 | } |
| 847 | + } elseif ( class_exists( 'NginxCache' ) ) { | |
| 848 | + $nginx_cache = new NginxCache(); | |
| 849 | + $nginx_cache->purge_zone_once(); | |
| 811 | 850 | } |
| 812 | 851 | } |
| 813 | 852 | // @codingStandardsIgnoreEnd |
| 814 | 853 | } |