| @@ -51,9 +51,9 @@ | ||
| 51 | 51 | add_action('berqwp_flush_page_cache', 'bwp_remove_recently_optimized_entry', 10, 1); |
| 52 | 52 | |
| 53 | 53 | add_action('init', [$this, 'bypass_cache']); |
| 54 | 54 | |
| 55 | - add_action('init', [$this, 'bypass_cache']); | |
| 55 | + // add_action('init', [$this, 'bypass_cache']); | |
| 56 | 56 | |
| 57 | 57 | // Flush cache when there's a new comment |
| 58 | 58 | add_action('wp_set_comment_status', [$this, 'handle_new_comment'], 10, 2); |
| 59 | 59 | |
| @@ -91,12 +91,20 @@ | ||
| 91 | 91 | |
| 92 | 92 | // Apache htaccess cache rules (skip on LiteSpeed — remove any existing rules) |
| 93 | 93 | add_action('berqwp_activate_plugin', 'bwp_write_htaccess_rules'); |
| 94 | 94 | add_action('berqwp_flush_all_cache', 'bwp_write_htaccess_rules'); |
| 95 | + add_action('berqwp_flush_all_cache', 'bwp_delete_php_files_in_cache'); | |
| 95 | 96 | add_action('berqwp_deactivate_plugin', 'bwp_remove_htaccess_rules'); |
| 97 | + | |
| 98 | + // Keep the dropin's cached permalink convention in sync with WordPress | |
| 99 | + add_action('init', 'bwp_sync_permalink_config'); | |
| 100 | + add_action('update_option_permalink_structure', 'bwp_sync_permalink_config'); | |
| 101 | + add_action('berqwp_activate_plugin', 'bwp_sync_permalink_config'); | |
| 102 | + | |
| 96 | 103 | if (bwp_is_openlitespeed_server()) { |
| 97 | 104 | add_action('berqwp_activate_plugin', 'bwp_remove_htaccess_rules'); |
| 98 | 105 | } |
| 106 | + | |
| 99 | 107 | add_action('berqwp_on_update_sandbox_mode', 'bwp_sync_htaccess_on_sandbox_change'); |
| 100 | 108 | |
| 101 | 109 | // Clear queue list on cloud |
| 102 | 110 | add_action('berqwp_deactivate_plugin', 'berqwp_clear_cache_queue'); |
| @@ -111,8 +119,15 @@ | ||
| 111 | 119 | } |
| 112 | 120 | |
| 113 | 121 | // detect when theme/plugin is installed or updated |
| 114 | 122 | add_action( 'upgrader_process_complete', [$this, 'process_updates'], 10, 2 ); |
| 123 | + | |
| 124 | + // handle menu update | |
| 125 | + add_action( 'wp_update_nav_menu', [$this, 'handle_menu_update'], 10, 2 ); | |
| 126 | + | |
| 127 | + // handle customizer save | |
| 128 | + add_action( 'customize_save', [$this, 'handle_customizer_update'] ); | |
| 129 | + | |
| 115 | 130 | } |
| 116 | 131 | |
| 117 | 132 | static function getInstance() { |
| 118 | 133 | |
| @@ -124,8 +139,45 @@ | ||
| 124 | 139 | |
| 125 | 140 | return self::$instance; |
| 126 | 141 | } |
| 127 | 142 | |
| 143 | + function handle_customizer_update( $wp_customize ) { | |
| 144 | + | |
| 145 | + global $berq_log; | |
| 146 | + $berq_log->info("Customizer updated"); | |
| 147 | + | |
| 148 | + $this->flush_all(); | |
| 149 | + | |
| 150 | + } | |
| 151 | + | |
| 152 | + function handle_menu_update( $menu_id, $menu_data = null ) { | |
| 153 | + | |
| 154 | + global $berq_log; | |
| 155 | + $berq_log->info("Site menu updated"); | |
| 156 | + | |
| 157 | + $this->flush_all(); | |
| 158 | + | |
| 159 | + } | |
| 160 | + | |
| 161 | + function flush_all() { | |
| 162 | + | |
| 163 | + global $berq_log; | |
| 164 | + $berq_log->info("Flushing everything"); | |
| 165 | + | |
| 166 | + if (berqwp_can_use_cloud()) { | |
| 167 | + | |
| 168 | + // flush critical css | |
| 169 | + $this->purge_critical_css_cache(); | |
| 170 | + | |
| 171 | + // stale cdn | |
| 172 | + self::stale_cloud_assets(); | |
| 173 | + | |
| 174 | + } | |
| 175 | + | |
| 176 | + // clear cache | |
| 177 | + $this->delete_cache_files(); | |
| 178 | + } | |
| 179 | + | |
| 128 | 180 | function process_updates( WP_Upgrader $upgrader, array $hook_extra ) { |
| 129 | 181 | |
| 130 | 182 | if (!berqwp_can_use_cloud()) { |
| 131 | 183 | return; |
| @@ -130,21 +182,36 @@ | ||
| 130 | 182 | if (!berqwp_can_use_cloud()) { |
| 131 | 183 | return; |
| 132 | 184 | } |
| 133 | 185 | |
| 186 | + global $berq_log; | |
| 187 | + $berq_log->info("Processing updates"); | |
| 188 | + | |
| 189 | + self::stale_cloud_assets(); | |
| 190 | + $this->purge_critical_css_cache(); | |
| 191 | + | |
| 192 | + // trigger cache warmup | |
| 193 | + do_action('berqwp_cache_warmup'); | |
| 194 | + | |
| 195 | + } | |
| 196 | + | |
| 197 | + static function stale_cloud_assets() { | |
| 198 | + if (!berqwp_can_use_cloud()) { | |
| 199 | + return; | |
| 200 | + } | |
| 201 | + | |
| 202 | + global $berq_log; | |
| 203 | + $berq_log->info("Marking cloud assets stale"); | |
| 204 | + | |
| 134 | 205 | $berqwp = new BerqWP(berqwp_get_license_key(), null, null); |
| 135 | 206 | $parsed_url = wp_parse_url(home_url()); |
| 136 | 207 | $domain = $parsed_url['host']; |
| 137 | - | |
| 138 | - global $berq_log; | |
| 139 | - $berq_log->info("Processing updates"); | |
| 140 | - | |
| 208 | + | |
| 141 | 209 | // mark cdn assets stale |
| 142 | 210 | $berqwp->cdn_stale_assets($domain); |
| 143 | 211 | |
| 144 | 212 | // flush all critical css cache |
| 145 | 213 | $berqwp->purge_critilclcss($domain); |
| 146 | - | |
| 147 | 214 | } |
| 148 | 215 | |
| 149 | 216 | function process_migration() { |
| 150 | 217 | $raw_home = get_option('home'); |
| @@ -183,8 +250,9 @@ | ||
| 183 | 250 | |
| 184 | 251 | update_option('berqwp_raw_home', $raw_home); |
| 185 | 252 | delete_transient('berq_lic_response_cache'); |
| 186 | 253 | delete_transient('berqwp_lic_response_cache'); |
| 254 | + delete_transient('berqwp_warmup_running'); | |
| 187 | 255 | } |
| 188 | 256 | } |
| 189 | 257 | |
| 190 | 258 | function post_type_action_links($actions, $post) |
| @@ -370,8 +438,12 @@ | ||
| 370 | 438 | if (!$can_flush_cache) { |
| 371 | 439 | return; |
| 372 | 440 | } |
| 373 | 441 | |
| 442 | + if ($new_status == 'draft' && $old_status == 'draft') { | |
| 443 | + return; | |
| 444 | + } | |
| 445 | + | |
| 374 | 446 | if ($new_status == 'auto-draft') { |
| 375 | 447 | return; |
| 376 | 448 | } |
| 377 | 449 | |
| @@ -421,12 +493,12 @@ | ||
| 421 | 493 | function brust_cache_for_loggedin() |
| 422 | 494 | { |
| 423 | 495 | if (is_user_logged_in()) { |
| 424 | 496 | // Add Vary: Cookie header to differentiate cached versions for logged-in users |
| 425 | - header('Vary: Cookie'); | |
| 426 | - header("Cache-Control: no-cache, no-store, must-revalidate"); | |
| 427 | - header("Pragma: no-cache"); | |
| 428 | - header("Expires: 0"); | |
| 497 | + header('Vary: Accept-Encoding, Cookie'); | |
| 498 | + // header("Cache-Control: no-cache, no-store, must-revalidate"); | |
| 499 | + // header("Pragma: no-cache"); | |
| 500 | + // header("Expires: 0"); | |
| 429 | 501 | } |
| 430 | 502 | } |
| 431 | 503 | |
| 432 | 504 | function handle_post_status_update($new_status, $old_status, $post) |
| @@ -547,9 +619,9 @@ | ||
| 547 | 619 | // $slug = ''; |
| 548 | 620 | // } |
| 549 | 621 | |
| 550 | 622 | $page_url = home_url($slug); |
| 551 | - $host = !empty($_SERVER['HTTP_HOST']) ? $_SERVER['HTTP_HOST'] : ''; | |
| 623 | + $host = !empty($_SERVER['HTTP_HOST']) ? sanitize_text_field(wp_unslash($_SERVER['HTTP_HOST'])) : ''; | |
| 552 | 624 | berqReverseProxyCache::purge_cache($page_url); |
| 553 | 625 | } |
| 554 | 626 | |
| 555 | 627 | function ingore_tracking_params($tracking_params) |
| @@ -579,11 +651,13 @@ | ||
| 579 | 651 | if (wp_is_post_revision($post_id)) { |
| 580 | 652 | return; |
| 581 | 653 | } |
| 582 | 654 | |
| 583 | - if (!is_admin()) { | |
| 584 | - return; | |
| 585 | - } | |
| 655 | + // prevents scheduled post public | |
| 656 | + // via wp cron | |
| 657 | + // if (!is_admin()) { | |
| 658 | + // return; | |
| 659 | + // } | |
| 586 | 660 | |
| 587 | 661 | $post_type = get_post_type($post_id); |
| 588 | 662 | $post_url = get_permalink($post_id); |
| 589 | 663 | |
| @@ -835,22 +909,27 @@ | ||
| 835 | 909 | // berqHeartbeat::add_queue($page_url); |
| 836 | 910 | warmup_cache_by_url($page_url); |
| 837 | 911 | } |
| 838 | 912 | |
| 913 | + static function miss_cache_headers() { | |
| 914 | + header('Cache-Control: no-store, no-cache, must-revalidate, max-age=0', true); | |
| 915 | + header('CDN-Cache-Control: no-store', true); // Cloudflare | |
| 916 | + header('Surrogate-Control: no-store', true); // Varnish | |
| 917 | + header('X-Accel-Expires: 0', true); // Nginx FastCGI | |
| 918 | + header("X-BerqWP-Cache: MISS"); | |
| 919 | + } | |
| 920 | + | |
| 839 | 921 | function html_cache() |
| 840 | 922 | { |
| 841 | 923 | |
| 842 | 924 | // Bypass cache for Photon |
| 843 | 925 | if (!empty($_COOKIE['berqwpnocache'])) { |
| 844 | - header('Cache-Control: no-store, no-cache, must-revalidate, max-age=0', true); | |
| 845 | - header('CDN-Cache-Control: no-store', true); // Cloudflare | |
| 846 | - header('Surrogate-Control: no-store', true); // Varnish | |
| 847 | - header('X-Accel-Expires: 0', true); // Nginx FastCGI | |
| 848 | - | |
| 926 | + self::miss_cache_headers(); | |
| 849 | 927 | return; |
| 850 | 928 | } |
| 851 | 929 | |
| 852 | 930 | if (!self::should_cache()) { |
| 931 | + self::miss_cache_headers(); | |
| 853 | 932 | return; |
| 854 | 933 | } |
| 855 | 934 | |
| 856 | 935 | $page_url = self::get_page_url(); |
| @@ -855,20 +934,32 @@ | ||
| 855 | 934 | |
| 856 | 935 | $page_url = self::get_page_url(); |
| 857 | 936 | |
| 858 | 937 | if (empty($page_url)) { |
| 938 | + self::miss_cache_headers(); | |
| 859 | 939 | return; |
| 860 | 940 | } |
| 861 | 941 | |
| 862 | 942 | // Disable cache for unknown query parameters |
| 863 | 943 | if (strpos($page_url, '?') !== false) { |
| 944 | + self::miss_cache_headers(); | |
| 864 | 945 | return; |
| 865 | 946 | } |
| 866 | 947 | |
| 948 | + // Redirect non-canonical URLs (wrong trailing slash) the same way WordPress would, | |
| 949 | + // before ever touching the cache — works for both trailing-slash and non-trailing-slash | |
| 950 | + // permalink structures. | |
| 951 | + $canonical_url = bwp_canonicalize_page_url($page_url); | |
| 952 | + if ($canonical_url !== $page_url) { | |
| 953 | + wp_safe_redirect($canonical_url, 301); | |
| 954 | + exit(); | |
| 955 | + } | |
| 956 | + | |
| 867 | 957 | if (is_singular()) { |
| 868 | 958 | $post_type = get_post_type(); |
| 869 | 959 | |
| 870 | 960 | if (empty($post_type) || !in_array($post_type, get_option('berqwp_optimize_post_types'))) { |
| 961 | + self::miss_cache_headers(); | |
| 871 | 962 | return; |
| 872 | 963 | } |
| 873 | 964 | } elseif (is_archive()) { |
| 874 | 965 | $queried_object = get_queried_object(); |
| @@ -876,8 +967,9 @@ | ||
| 876 | 967 | if ($queried_object instanceof WP_Term && !empty($queried_object->taxonomy)) { |
| 877 | 968 | $current_taxonomy = $queried_object->taxonomy; |
| 878 | 969 | |
| 879 | 970 | if (!in_array($current_taxonomy, get_option('berqwp_optimize_taxonomies'))) { |
| 971 | + self::miss_cache_headers(); | |
| 880 | 972 | return; |
| 881 | 973 | } |
| 882 | 974 | } |
| 883 | 975 | } |
| @@ -885,8 +977,9 @@ | ||
| 885 | 977 | |
| 886 | 978 | $status_code = http_response_code(); |
| 887 | 979 | |
| 888 | 980 | if ($status_code !== 200) { |
| 981 | + self::miss_cache_headers(); | |
| 889 | 982 | return; |
| 890 | 983 | } |
| 891 | 984 | |
| 892 | 985 | $berqconfigs = berqConfigs::getInstance(); |
| @@ -906,21 +999,38 @@ | ||
| 906 | 999 | header('Last-Modified: ' . gmdate('D, d M Y H:i:s', $lastModified) . ' GMT'); |
| 907 | 1000 | header('X-File-Size: ' . filesize($cache_file), true); |
| 908 | 1001 | header('Content-Type: text/html; charset=utf-8'); |
| 909 | 1002 | header("X-served: WP Hook"); |
| 910 | - header('Vary: Cookie'); | |
| 1003 | + header('Vary: Accept-Encoding, Cookie'); | |
| 1004 | + header("X-BerqWP-Cache: HIT"); | |
| 911 | 1005 | |
| 912 | 1006 | 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)) { |
| 913 | 1007 | // The client's cache is still valid based on Last-Modified, respond with a 304 Not Modified |
| 914 | 1008 | header('HTTP/1.1 304 Not Modified'); |
| 915 | 1009 | // header('Expires: ' . gmdate('D, d M Y H:i:s') . ' GMT'); |
| 916 | - header("Expires: 0"); | |
| 917 | - header('Cache-Control: no-cache, must-revalidate'); | |
| 1010 | + // header("Expires: 0"); | |
| 1011 | + // header('Cache-Control: no-cache, must-revalidate'); | |
| 1012 | + | |
| 1013 | + header('Cache-Control: public, max-age=600, s-maxage=2592000, stale-while-revalidate=86400', true); | |
| 1014 | + header('cdn-cache-control: max-age=2592000'); | |
| 1015 | + header('Cache-Tag: ' . wp_parse_url(home_url())['host']); | |
| 1016 | + | |
| 918 | 1017 | exit(); |
| 919 | 1018 | } |
| 920 | 1019 | |
| 921 | - header('Cache-Control: public, max-age=0, s-maxage=3600, must-revalidate', true); | |
| 922 | - header('Vary: Accept-Encoding, Cookie'); | |
| 1020 | + // Prevent PHP/Apache from re-compressing the already-gzipped cache file | |
| 1021 | + if (ini_get('zlib.output_compression')) { | |
| 1022 | + ini_set('zlib.output_compression', 'Off'); | |
| 1023 | + } | |
| 1024 | + | |
| 1025 | + while (ob_get_level()) { | |
| 1026 | + ob_end_clean(); | |
| 1027 | + } | |
| 1028 | + | |
| 1029 | + header('Cache-Control: public, max-age=600, s-maxage=2592000, stale-while-revalidate=86400', true); | |
| 1030 | + header('cdn-cache-control: max-age=2592000'); | |
| 1031 | + header('Cache-Tag: ' . wp_parse_url(home_url())['host']); | |
| 1032 | + // header("Content-Security-Policy: script-src 'self' blob: 'unsafe-inline'"); | |
| 923 | 1033 | header('Content-Encoding: gzip', true); |
| 924 | 1034 | header('Content-Length: ' . filesize($cache_file), true); |
| 925 | 1035 | readfile($cache_file); |
| 926 | 1036 | exit(); |
| @@ -925,12 +1035,9 @@ | ||
| 925 | 1035 | readfile($cache_file); |
| 926 | 1036 | exit(); |
| 927 | 1037 | } |
| 928 | 1038 | |
| 929 | - header('Cache-Control: no-store, no-cache, must-revalidate, max-age=0'); | |
| 930 | - header('CDN-Cache-Control: no-store'); // Cloudflare | |
| 931 | - header('Surrogate-Control: no-store'); // Varnish | |
| 932 | - header('X-Accel-Expires: 0'); // Nginx FastCGI | |
| 1039 | + self::miss_cache_headers(); | |
| 933 | 1040 | |
| 934 | 1041 | self::purge_page($page_url); |
| 935 | 1042 | |
| 936 | 1043 | if (!berqwp_can_use_cloud()) { |