| @@ -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 | |
| @@ -92,11 +92,13 @@ | ||
| 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 | 95 | add_action('berqwp_deactivate_plugin', 'bwp_remove_htaccess_rules'); |
| 96 | + | |
| 96 | 97 | if (bwp_is_openlitespeed_server()) { |
| 97 | 98 | add_action('berqwp_activate_plugin', 'bwp_remove_htaccess_rules'); |
| 98 | 99 | } |
| 100 | + | |
| 99 | 101 | add_action('berqwp_on_update_sandbox_mode', 'bwp_sync_htaccess_on_sandbox_change'); |
| 100 | 102 | |
| 101 | 103 | // Clear queue list on cloud |
| 102 | 104 | add_action('berqwp_deactivate_plugin', 'berqwp_clear_cache_queue'); |
| @@ -111,8 +113,15 @@ | ||
| 111 | 113 | } |
| 112 | 114 | |
| 113 | 115 | // detect when theme/plugin is installed or updated |
| 114 | 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 | + | |
| 115 | 124 | } |
| 116 | 125 | |
| 117 | 126 | static function getInstance() { |
| 118 | 127 | |
| @@ -124,8 +133,45 @@ | ||
| 124 | 133 | |
| 125 | 134 | return self::$instance; |
| 126 | 135 | } |
| 127 | 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 | + | |
| 128 | 174 | function process_updates( WP_Upgrader $upgrader, array $hook_extra ) { |
| 129 | 175 | |
| 130 | 176 | if (!berqwp_can_use_cloud()) { |
| 131 | 177 | return; |
| @@ -130,21 +176,33 @@ | ||
| 130 | 176 | if (!berqwp_can_use_cloud()) { |
| 131 | 177 | return; |
| 132 | 178 | } |
| 133 | 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 | + | |
| 134 | 196 | $berqwp = new BerqWP(berqwp_get_license_key(), null, null); |
| 135 | 197 | $parsed_url = wp_parse_url(home_url()); |
| 136 | 198 | $domain = $parsed_url['host']; |
| 137 | - | |
| 138 | - global $berq_log; | |
| 139 | - $berq_log->info("Processing updates"); | |
| 140 | - | |
| 199 | + | |
| 141 | 200 | // mark cdn assets stale |
| 142 | 201 | $berqwp->cdn_stale_assets($domain); |
| 143 | 202 | |
| 144 | 203 | // flush all critical css cache |
| 145 | 204 | $berqwp->purge_critilclcss($domain); |
| 146 | - | |
| 147 | 205 | } |
| 148 | 206 | |
| 149 | 207 | function process_migration() { |
| 150 | 208 | $raw_home = get_option('home'); |
| @@ -370,8 +428,12 @@ | ||
| 370 | 428 | if (!$can_flush_cache) { |
| 371 | 429 | return; |
| 372 | 430 | } |
| 373 | 431 | |
| 432 | + if ($new_status == 'draft' && $old_status == 'draft') { | |
| 433 | + return; | |
| 434 | + } | |
| 435 | + | |
| 374 | 436 | if ($new_status == 'auto-draft') { |
| 375 | 437 | return; |
| 376 | 438 | } |
| 377 | 439 | |
| @@ -421,12 +483,12 @@ | ||
| 421 | 483 | function brust_cache_for_loggedin() |
| 422 | 484 | { |
| 423 | 485 | if (is_user_logged_in()) { |
| 424 | 486 | // 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"); | |
| 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"); | |
| 429 | 491 | } |
| 430 | 492 | } |
| 431 | 493 | |
| 432 | 494 | function handle_post_status_update($new_status, $old_status, $post) |
| @@ -547,9 +609,9 @@ | ||
| 547 | 609 | // $slug = ''; |
| 548 | 610 | // } |
| 549 | 611 | |
| 550 | 612 | $page_url = home_url($slug); |
| 551 | - $host = !empty($_SERVER['HTTP_HOST']) ? $_SERVER['HTTP_HOST'] : ''; | |
| 613 | + $host = !empty($_SERVER['HTTP_HOST']) ? sanitize_text_field(wp_unslash($_SERVER['HTTP_HOST'])) : ''; | |
| 552 | 614 | berqReverseProxyCache::purge_cache($page_url); |
| 553 | 615 | } |
| 554 | 616 | |
| 555 | 617 | function ingore_tracking_params($tracking_params) |
| @@ -579,11 +641,13 @@ | ||
| 579 | 641 | if (wp_is_post_revision($post_id)) { |
| 580 | 642 | return; |
| 581 | 643 | } |
| 582 | 644 | |
| 583 | - if (!is_admin()) { | |
| 584 | - return; | |
| 585 | - } | |
| 645 | + // prevents scheduled post public | |
| 646 | + // via wp cron | |
| 647 | + // if (!is_admin()) { | |
| 648 | + // return; | |
| 649 | + // } | |
| 586 | 650 | |
| 587 | 651 | $post_type = get_post_type($post_id); |
| 588 | 652 | $post_url = get_permalink($post_id); |
| 589 | 653 | |
| @@ -835,22 +899,27 @@ | ||
| 835 | 899 | // berqHeartbeat::add_queue($page_url); |
| 836 | 900 | warmup_cache_by_url($page_url); |
| 837 | 901 | } |
| 838 | 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 | + | |
| 839 | 911 | function html_cache() |
| 840 | 912 | { |
| 841 | 913 | |
| 842 | 914 | // Bypass cache for Photon |
| 843 | 915 | 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 | - | |
| 916 | + self::miss_cache_headers(); | |
| 849 | 917 | return; |
| 850 | 918 | } |
| 851 | 919 | |
| 852 | 920 | if (!self::should_cache()) { |
| 921 | + self::miss_cache_headers(); | |
| 853 | 922 | return; |
| 854 | 923 | } |
| 855 | 924 | |
| 856 | 925 | $page_url = self::get_page_url(); |
| @@ -855,13 +924,15 @@ | ||
| 855 | 924 | |
| 856 | 925 | $page_url = self::get_page_url(); |
| 857 | 926 | |
| 858 | 927 | if (empty($page_url)) { |
| 928 | + self::miss_cache_headers(); | |
| 859 | 929 | return; |
| 860 | 930 | } |
| 861 | 931 | |
| 862 | 932 | // Disable cache for unknown query parameters |
| 863 | 933 | if (strpos($page_url, '?') !== false) { |
| 934 | + self::miss_cache_headers(); | |
| 864 | 935 | return; |
| 865 | 936 | } |
| 866 | 937 | |
| 867 | 938 | if (is_singular()) { |
| @@ -867,8 +938,9 @@ | ||
| 867 | 938 | if (is_singular()) { |
| 868 | 939 | $post_type = get_post_type(); |
| 869 | 940 | |
| 870 | 941 | if (empty($post_type) || !in_array($post_type, get_option('berqwp_optimize_post_types'))) { |
| 942 | + self::miss_cache_headers(); | |
| 871 | 943 | return; |
| 872 | 944 | } |
| 873 | 945 | } elseif (is_archive()) { |
| 874 | 946 | $queried_object = get_queried_object(); |
| @@ -876,8 +948,9 @@ | ||
| 876 | 948 | if ($queried_object instanceof WP_Term && !empty($queried_object->taxonomy)) { |
| 877 | 949 | $current_taxonomy = $queried_object->taxonomy; |
| 878 | 950 | |
| 879 | 951 | if (!in_array($current_taxonomy, get_option('berqwp_optimize_taxonomies'))) { |
| 952 | + self::miss_cache_headers(); | |
| 880 | 953 | return; |
| 881 | 954 | } |
| 882 | 955 | } |
| 883 | 956 | } |
| @@ -885,8 +958,9 @@ | ||
| 885 | 958 | |
| 886 | 959 | $status_code = http_response_code(); |
| 887 | 960 | |
| 888 | 961 | if ($status_code !== 200) { |
| 962 | + self::miss_cache_headers(); | |
| 889 | 963 | return; |
| 890 | 964 | } |
| 891 | 965 | |
| 892 | 966 | $berqconfigs = berqConfigs::getInstance(); |
| @@ -906,21 +980,38 @@ | ||
| 906 | 980 | header('Last-Modified: ' . gmdate('D, d M Y H:i:s', $lastModified) . ' GMT'); |
| 907 | 981 | header('X-File-Size: ' . filesize($cache_file), true); |
| 908 | 982 | header('Content-Type: text/html; charset=utf-8'); |
| 909 | 983 | header("X-served: WP Hook"); |
| 910 | - header('Vary: Cookie'); | |
| 984 | + header('Vary: Accept-Encoding, Cookie'); | |
| 985 | + header("X-BerqWP-Cache: HIT"); | |
| 911 | 986 | |
| 912 | 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)) { |
| 913 | 988 | // The client's cache is still valid based on Last-Modified, respond with a 304 Not Modified |
| 914 | 989 | header('HTTP/1.1 304 Not Modified'); |
| 915 | 990 | // header('Expires: ' . gmdate('D, d M Y H:i:s') . ' GMT'); |
| 916 | - header("Expires: 0"); | |
| 917 | - 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 | + | |
| 918 | 998 | exit(); |
| 919 | 999 | } |
| 920 | 1000 | |
| 921 | - header('Cache-Control: public, max-age=0, s-maxage=3600, must-revalidate', true); | |
| 922 | - 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'"); | |
| 923 | 1014 | header('Content-Encoding: gzip', true); |
| 924 | 1015 | header('Content-Length: ' . filesize($cache_file), true); |
| 925 | 1016 | readfile($cache_file); |
| 926 | 1017 | exit(); |
| @@ -925,12 +1016,9 @@ | ||
| 925 | 1016 | readfile($cache_file); |
| 926 | 1017 | exit(); |
| 927 | 1018 | } |
| 928 | 1019 | |
| 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 | |
| 1020 | + self::miss_cache_headers(); | |
| 933 | 1021 | |
| 934 | 1022 | self::purge_page($page_url); |
| 935 | 1023 | |
| 936 | 1024 | if (!berqwp_can_use_cloud()) { |