| @@ -33,9 +33,10 @@ | ||
| 33 | 33 | // add_action('berqwp_flush_all_cache', [$this, 'request_warmup_cache']); |
| 34 | 34 | // add_action('berqwp_cache_warmup', [$this, 'request_warmup_cache']); |
| 35 | 35 | |
| 36 | 36 | // Delete cache files when the mode changes |
| 37 | - add_action('berqwp_before_update_optimization_mode', [$this, 'delete_cache_files']); | |
| 37 | + // add_action('berqwp_before_update_optimization_mode', [$this, 'delete_cache_files']); | |
| 38 | + add_action('berqwp_on_update_optimization_mode', [$this, 'delete_cache_files']); | |
| 38 | 39 | |
| 39 | 40 | // Reverse proxy cache support |
| 40 | 41 | add_action('berqwp_stored_page_cache', [$this, 'flush_reverse_proxy_cache']); |
| 41 | 42 | add_action('berqwp_flush_all_cache', ['berqReverseProxyCache', 'flush_all']); |
| @@ -43,11 +44,16 @@ | ||
| 43 | 44 | |
| 44 | 45 | // Clear cache warmup lock after storing the cache |
| 45 | 46 | add_action('berqwp_stored_page_cache', 'bwp_clear_warmup_lock'); |
| 46 | 47 | |
| 48 | + // Log recently optimized pages | |
| 49 | + add_action('berqwp_stored_page_cache', 'bwp_log_recently_optimized_page', 20, 1); | |
| 50 | + add_action('berqwp_flush_all_cache', 'bwp_clear_recently_optimized_log'); | |
| 51 | + add_action('berqwp_flush_page_cache', 'bwp_remove_recently_optimized_entry', 10, 1); | |
| 52 | + | |
| 47 | 53 | add_action('init', [$this, 'bypass_cache']); |
| 48 | 54 | |
| 49 | - add_action('init', [$this, 'bypass_cache']); | |
| 55 | + // add_action('init', [$this, 'bypass_cache']); | |
| 50 | 56 | |
| 51 | 57 | // Flush cache when there's a new comment |
| 52 | 58 | add_action('wp_set_comment_status', [$this, 'handle_new_comment'], 10, 2); |
| 53 | 59 | |
| @@ -78,9 +84,9 @@ | ||
| 78 | 84 | // flush cloudflare edge cache |
| 79 | 85 | add_action('berqwp_stored_page_cache', [$this, 'flush_cf_page']); |
| 80 | 86 | add_action('berqwp_flush_page_cache', [$this, 'flush_cf_page']); |
| 81 | 87 | add_action('berqwp_flush_all_cache', 'bwp_cf_flush_all'); |
| 82 | - add_action('berqwp_update_sandbox_mode', 'bwp_cf_flush_all'); | |
| 88 | + add_action('berqwp_on_update_sandbox_mode', 'bwp_cf_flush_all'); | |
| 83 | 89 | add_action('berqwp_deactivate_plugin', 'bwp_cf_delete_rules'); |
| 84 | 90 | add_action('berqwp_activate_plugin', [$this, 'check_cf_rules']); |
| 85 | 91 | |
| 86 | 92 | // Apache htaccess cache rules (skip on LiteSpeed — remove any existing rules) |
| @@ -86,13 +92,15 @@ | ||
| 86 | 92 | // Apache htaccess cache rules (skip on LiteSpeed — remove any existing rules) |
| 87 | 93 | add_action('berqwp_activate_plugin', 'bwp_write_htaccess_rules'); |
| 88 | 94 | add_action('berqwp_flush_all_cache', 'bwp_write_htaccess_rules'); |
| 89 | 95 | add_action('berqwp_deactivate_plugin', 'bwp_remove_htaccess_rules'); |
| 96 | + | |
| 90 | 97 | if (bwp_is_openlitespeed_server()) { |
| 91 | 98 | add_action('berqwp_activate_plugin', 'bwp_remove_htaccess_rules'); |
| 92 | 99 | } |
| 93 | - add_action('berqwp_update_sandbox_mode', 'bwp_sync_htaccess_on_sandbox_change'); | |
| 94 | 100 | |
| 101 | + add_action('berqwp_on_update_sandbox_mode', 'bwp_sync_htaccess_on_sandbox_change'); | |
| 102 | + | |
| 95 | 103 | // Clear queue list on cloud |
| 96 | 104 | add_action('berqwp_deactivate_plugin', 'berqwp_clear_cache_queue'); |
| 97 | 105 | |
| 98 | 106 | // Edit post type list custom links |
| @@ -105,8 +113,15 @@ | ||
| 105 | 113 | } |
| 106 | 114 | |
| 107 | 115 | // detect when theme/plugin is installed or updated |
| 108 | 116 | add_action( 'upgrader_process_complete', [$this, 'process_updates'], 10, 2 ); |
| 117 | + | |
| 118 | + // handle menu update | |
| 119 | + add_action( 'wp_update_nav_menu', [$this, 'handle_menu_update'], 10, 2 ); | |
| 120 | + | |
| 121 | + // handle customizer save | |
| 122 | + add_action( 'customize_save', [$this, 'handle_customizer_update'] ); | |
| 123 | + | |
| 109 | 124 | } |
| 110 | 125 | |
| 111 | 126 | static function getInstance() { |
| 112 | 127 | |
| @@ -118,8 +133,45 @@ | ||
| 118 | 133 | |
| 119 | 134 | return self::$instance; |
| 120 | 135 | } |
| 121 | 136 | |
| 137 | + function handle_customizer_update( $wp_customize ) { | |
| 138 | + | |
| 139 | + global $berq_log; | |
| 140 | + $berq_log->info("Customizer updated"); | |
| 141 | + | |
| 142 | + $this->flush_all(); | |
| 143 | + | |
| 144 | + } | |
| 145 | + | |
| 146 | + function handle_menu_update( $menu_id, $menu_data = null ) { | |
| 147 | + | |
| 148 | + global $berq_log; | |
| 149 | + $berq_log->info("Site menu updated"); | |
| 150 | + | |
| 151 | + $this->flush_all(); | |
| 152 | + | |
| 153 | + } | |
| 154 | + | |
| 155 | + function flush_all() { | |
| 156 | + | |
| 157 | + global $berq_log; | |
| 158 | + $berq_log->info("Flushing everything"); | |
| 159 | + | |
| 160 | + if (berqwp_can_use_cloud()) { | |
| 161 | + | |
| 162 | + // flush critical css | |
| 163 | + $this->purge_critical_css_cache(); | |
| 164 | + | |
| 165 | + // stale cdn | |
| 166 | + self::stale_cloud_assets(); | |
| 167 | + | |
| 168 | + } | |
| 169 | + | |
| 170 | + // clear cache | |
| 171 | + $this->delete_cache_files(); | |
| 172 | + } | |
| 173 | + | |
| 122 | 174 | function process_updates( WP_Upgrader $upgrader, array $hook_extra ) { |
| 123 | 175 | |
| 124 | 176 | if (!berqwp_can_use_cloud()) { |
| 125 | 177 | return; |
| @@ -124,21 +176,33 @@ | ||
| 124 | 176 | if (!berqwp_can_use_cloud()) { |
| 125 | 177 | return; |
| 126 | 178 | } |
| 127 | 179 | |
| 180 | + global $berq_log; | |
| 181 | + $berq_log->info("Processing updates"); | |
| 182 | + | |
| 183 | + self::stale_cloud_assets(); | |
| 184 | + $this->purge_critical_css_cache(); | |
| 185 | + | |
| 186 | + } | |
| 187 | + | |
| 188 | + static function stale_cloud_assets() { | |
| 189 | + if (!berqwp_can_use_cloud()) { | |
| 190 | + return; | |
| 191 | + } | |
| 192 | + | |
| 193 | + global $berq_log; | |
| 194 | + $berq_log->info("Marking cloud assets stale"); | |
| 195 | + | |
| 128 | 196 | $berqwp = new BerqWP(berqwp_get_license_key(), null, null); |
| 129 | 197 | $parsed_url = wp_parse_url(home_url()); |
| 130 | 198 | $domain = $parsed_url['host']; |
| 131 | - | |
| 132 | - global $berq_log; | |
| 133 | - $berq_log->info("Processing updates"); | |
| 134 | - | |
| 199 | + | |
| 135 | 200 | // mark cdn assets stale |
| 136 | 201 | $berqwp->cdn_stale_assets($domain); |
| 137 | 202 | |
| 138 | 203 | // flush all critical css cache |
| 139 | 204 | $berqwp->purge_critilclcss($domain); |
| 140 | - | |
| 141 | 205 | } |
| 142 | 206 | |
| 143 | 207 | function process_migration() { |
| 144 | 208 | $raw_home = get_option('home'); |
| @@ -364,8 +428,12 @@ | ||
| 364 | 428 | if (!$can_flush_cache) { |
| 365 | 429 | return; |
| 366 | 430 | } |
| 367 | 431 | |
| 432 | + if ($new_status == 'draft' && $old_status == 'draft') { | |
| 433 | + return; | |
| 434 | + } | |
| 435 | + | |
| 368 | 436 | if ($new_status == 'auto-draft') { |
| 369 | 437 | return; |
| 370 | 438 | } |
| 371 | 439 | |
| @@ -415,12 +483,12 @@ | ||
| 415 | 483 | function brust_cache_for_loggedin() |
| 416 | 484 | { |
| 417 | 485 | if (is_user_logged_in()) { |
| 418 | 486 | // Add Vary: Cookie header to differentiate cached versions for logged-in users |
| 419 | - header('Vary: Cookie'); | |
| 420 | - header("Cache-Control: no-cache, no-store, must-revalidate"); | |
| 421 | - header("Pragma: no-cache"); | |
| 422 | - header("Expires: 0"); | |
| 487 | + header('Vary: Accept-Encoding, Cookie'); | |
| 488 | + // header("Cache-Control: no-cache, no-store, must-revalidate"); | |
| 489 | + // header("Pragma: no-cache"); | |
| 490 | + // header("Expires: 0"); | |
| 423 | 491 | } |
| 424 | 492 | } |
| 425 | 493 | |
| 426 | 494 | function handle_post_status_update($new_status, $old_status, $post) |
| @@ -541,9 +609,9 @@ | ||
| 541 | 609 | // $slug = ''; |
| 542 | 610 | // } |
| 543 | 611 | |
| 544 | 612 | $page_url = home_url($slug); |
| 545 | - $host = !empty($_SERVER['HTTP_HOST']) ? $_SERVER['HTTP_HOST'] : ''; | |
| 613 | + $host = !empty($_SERVER['HTTP_HOST']) ? sanitize_text_field(wp_unslash($_SERVER['HTTP_HOST'])) : ''; | |
| 546 | 614 | berqReverseProxyCache::purge_cache($page_url); |
| 547 | 615 | } |
| 548 | 616 | |
| 549 | 617 | function ingore_tracking_params($tracking_params) |
| @@ -573,11 +641,13 @@ | ||
| 573 | 641 | if (wp_is_post_revision($post_id)) { |
| 574 | 642 | return; |
| 575 | 643 | } |
| 576 | 644 | |
| 577 | - if (!is_admin()) { | |
| 578 | - return; | |
| 579 | - } | |
| 645 | + // prevents scheduled post public | |
| 646 | + // via wp cron | |
| 647 | + // if (!is_admin()) { | |
| 648 | + // return; | |
| 649 | + // } | |
| 580 | 650 | |
| 581 | 651 | $post_type = get_post_type($post_id); |
| 582 | 652 | $post_url = get_permalink($post_id); |
| 583 | 653 | |
| @@ -829,22 +899,27 @@ | ||
| 829 | 899 | // berqHeartbeat::add_queue($page_url); |
| 830 | 900 | warmup_cache_by_url($page_url); |
| 831 | 901 | } |
| 832 | 902 | |
| 903 | + static function miss_cache_headers() { | |
| 904 | + header('Cache-Control: no-store, no-cache, must-revalidate, max-age=0', true); | |
| 905 | + header('CDN-Cache-Control: no-store', true); // Cloudflare | |
| 906 | + header('Surrogate-Control: no-store', true); // Varnish | |
| 907 | + header('X-Accel-Expires: 0', true); // Nginx FastCGI | |
| 908 | + header("X-BerqWP-Cache: MISS"); | |
| 909 | + } | |
| 910 | + | |
| 833 | 911 | function html_cache() |
| 834 | 912 | { |
| 835 | 913 | |
| 836 | 914 | // Bypass cache for Photon |
| 837 | 915 | if (!empty($_COOKIE['berqwpnocache'])) { |
| 838 | - header('Cache-Control: no-store, no-cache, must-revalidate, max-age=0', true); | |
| 839 | - header('CDN-Cache-Control: no-store', true); // Cloudflare | |
| 840 | - header('Surrogate-Control: no-store', true); // Varnish | |
| 841 | - header('X-Accel-Expires: 0', true); // Nginx FastCGI | |
| 842 | - | |
| 916 | + self::miss_cache_headers(); | |
| 843 | 917 | return; |
| 844 | 918 | } |
| 845 | 919 | |
| 846 | 920 | if (!self::should_cache()) { |
| 921 | + self::miss_cache_headers(); | |
| 847 | 922 | return; |
| 848 | 923 | } |
| 849 | 924 | |
| 850 | 925 | $page_url = self::get_page_url(); |
| @@ -849,13 +924,15 @@ | ||
| 849 | 924 | |
| 850 | 925 | $page_url = self::get_page_url(); |
| 851 | 926 | |
| 852 | 927 | if (empty($page_url)) { |
| 928 | + self::miss_cache_headers(); | |
| 853 | 929 | return; |
| 854 | 930 | } |
| 855 | 931 | |
| 856 | 932 | // Disable cache for unknown query parameters |
| 857 | 933 | if (strpos($page_url, '?') !== false) { |
| 934 | + self::miss_cache_headers(); | |
| 858 | 935 | return; |
| 859 | 936 | } |
| 860 | 937 | |
| 861 | 938 | if (is_singular()) { |
| @@ -861,8 +938,9 @@ | ||
| 861 | 938 | if (is_singular()) { |
| 862 | 939 | $post_type = get_post_type(); |
| 863 | 940 | |
| 864 | 941 | if (empty($post_type) || !in_array($post_type, get_option('berqwp_optimize_post_types'))) { |
| 942 | + self::miss_cache_headers(); | |
| 865 | 943 | return; |
| 866 | 944 | } |
| 867 | 945 | } elseif (is_archive()) { |
| 868 | 946 | $queried_object = get_queried_object(); |
| @@ -870,8 +948,9 @@ | ||
| 870 | 948 | if ($queried_object instanceof WP_Term && !empty($queried_object->taxonomy)) { |
| 871 | 949 | $current_taxonomy = $queried_object->taxonomy; |
| 872 | 950 | |
| 873 | 951 | if (!in_array($current_taxonomy, get_option('berqwp_optimize_taxonomies'))) { |
| 952 | + self::miss_cache_headers(); | |
| 874 | 953 | return; |
| 875 | 954 | } |
| 876 | 955 | } |
| 877 | 956 | } |
| @@ -879,8 +958,9 @@ | ||
| 879 | 958 | |
| 880 | 959 | $status_code = http_response_code(); |
| 881 | 960 | |
| 882 | 961 | if ($status_code !== 200) { |
| 962 | + self::miss_cache_headers(); | |
| 883 | 963 | return; |
| 884 | 964 | } |
| 885 | 965 | |
| 886 | 966 | $berqconfigs = berqConfigs::getInstance(); |
| @@ -900,21 +980,38 @@ | ||
| 900 | 980 | header('Last-Modified: ' . gmdate('D, d M Y H:i:s', $lastModified) . ' GMT'); |
| 901 | 981 | header('X-File-Size: ' . filesize($cache_file), true); |
| 902 | 982 | header('Content-Type: text/html; charset=utf-8'); |
| 903 | 983 | header("X-served: WP Hook"); |
| 904 | - header('Vary: Cookie'); | |
| 984 | + header('Vary: Accept-Encoding, Cookie'); | |
| 985 | + header("X-BerqWP-Cache: HIT"); | |
| 905 | 986 | |
| 906 | 987 | if ((isset($_SERVER['HTTP_IF_MODIFIED_SINCE']) && strtotime($_SERVER['HTTP_IF_MODIFIED_SINCE']) >= $lastModified) || (isset($_SERVER['HTTP_IF_NONE_MATCH']) && $_SERVER['HTTP_IF_NONE_MATCH'] === $etag)) { |
| 907 | 988 | // The client's cache is still valid based on Last-Modified, respond with a 304 Not Modified |
| 908 | 989 | header('HTTP/1.1 304 Not Modified'); |
| 909 | 990 | // header('Expires: ' . gmdate('D, d M Y H:i:s') . ' GMT'); |
| 910 | - header("Expires: 0"); | |
| 911 | - header('Cache-Control: no-cache, must-revalidate'); | |
| 991 | + // header("Expires: 0"); | |
| 992 | + // header('Cache-Control: no-cache, must-revalidate'); | |
| 993 | + | |
| 994 | + header('Cache-Control: public, max-age=600, s-maxage=2592000, stale-while-revalidate=86400', true); | |
| 995 | + header('cdn-cache-control: max-age=2592000'); | |
| 996 | + header('Cache-Tag: ' . wp_parse_url(home_url())['host']); | |
| 997 | + | |
| 912 | 998 | exit(); |
| 913 | 999 | } |
| 914 | 1000 | |
| 915 | - header('Cache-Control: public, max-age=0, s-maxage=3600, must-revalidate', true); | |
| 916 | - header('Vary: Accept-Encoding, Cookie'); | |
| 1001 | + // Prevent PHP/Apache from re-compressing the already-gzipped cache file | |
| 1002 | + if (ini_get('zlib.output_compression')) { | |
| 1003 | + ini_set('zlib.output_compression', 'Off'); | |
| 1004 | + } | |
| 1005 | + | |
| 1006 | + while (ob_get_level()) { | |
| 1007 | + ob_end_clean(); | |
| 1008 | + } | |
| 1009 | + | |
| 1010 | + header('Cache-Control: public, max-age=600, s-maxage=2592000, stale-while-revalidate=86400', true); | |
| 1011 | + header('cdn-cache-control: max-age=2592000'); | |
| 1012 | + header('Cache-Tag: ' . wp_parse_url(home_url())['host']); | |
| 1013 | + // header("Content-Security-Policy: script-src 'self' blob: 'unsafe-inline'"); | |
| 917 | 1014 | header('Content-Encoding: gzip', true); |
| 918 | 1015 | header('Content-Length: ' . filesize($cache_file), true); |
| 919 | 1016 | readfile($cache_file); |
| 920 | 1017 | exit(); |
| @@ -919,12 +1016,9 @@ | ||
| 919 | 1016 | readfile($cache_file); |
| 920 | 1017 | exit(); |
| 921 | 1018 | } |
| 922 | 1019 | |
| 923 | - header('Cache-Control: no-store, no-cache, must-revalidate, max-age=0'); | |
| 924 | - header('CDN-Cache-Control: no-store'); // Cloudflare | |
| 925 | - header('Surrogate-Control: no-store'); // Varnish | |
| 926 | - header('X-Accel-Expires: 0'); // Nginx FastCGI | |
| 1020 | + self::miss_cache_headers(); | |
| 927 | 1021 | |
| 928 | 1022 | self::purge_page($page_url); |
| 929 | 1023 | |
| 930 | 1024 | if (!berqwp_can_use_cloud()) { |