PluginProbe
BerqWP – All-In-One Optimization for Core Web Vitals, Cache, CDN, Images, CSS & JavaScript / 4.1.18
BerqWP – All-In-One Optimization for Core Web Vitals, Cache, CDN, Images, CSS & JavaScript v4.1.18
4.1.19 4.1.18 4.1.17 4.1.16 4.1.15 4.1.14 4.1.13 4.1.12 4.1.11 4.1.10 4.0.30 4.0.29 4.0.28 4.0.27 4.0.26 4.0.24 4.0.25 4.0.23 4.0.22 4.0.21 4.0.19 4.0.18 4.0.17 4.0.16 1.9.3 All 173 releases
← All changes | inc/cache/class-berqcache.php +144 -31 4.0.17 → 4.1.18 View file →
@@ -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)
@@ -85,14 +91,22 @@
85 91
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');
95 + add_action('berqwp_flush_all_cache', 'bwp_delete_php_files_in_cache');
89 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 +
90 103 if (bwp_is_openlitespeed_server()) {
91 104 add_action('berqwp_activate_plugin', 'bwp_remove_htaccess_rules');
92 105 }
93 - add_action('berqwp_update_sandbox_mode', 'bwp_sync_htaccess_on_sandbox_change');
94 106
107 + add_action('berqwp_on_update_sandbox_mode', 'bwp_sync_htaccess_on_sandbox_change');
108 +
95 109 // Clear queue list on cloud
96 110 add_action('berqwp_deactivate_plugin', 'berqwp_clear_cache_queue');
97 111
98 112 // Edit post type list custom links
@@ -105,8 +119,15 @@
105 119 }
106 120
107 121 // detect when theme/plugin is installed or updated
108 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 +
109 130 }
110 131
111 132 static function getInstance() {
112 133
@@ -118,8 +139,45 @@
118 139
119 140 return self::$instance;
120 141 }
121 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 +
122 180 function process_updates( WP_Upgrader $upgrader, array $hook_extra ) {
123 181
124 182 if (!berqwp_can_use_cloud()) {
125 183 return;
@@ -124,21 +182,36 @@
124 182 if (!berqwp_can_use_cloud()) {
125 183 return;
126 184 }
127 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 +
128 205 $berqwp = new BerqWP(berqwp_get_license_key(), null, null);
129 206 $parsed_url = wp_parse_url(home_url());
130 207 $domain = $parsed_url['host'];
131 -
132 - global $berq_log;
133 - $berq_log->info("Processing updates");
134 -
208 +
135 209 // mark cdn assets stale
136 210 $berqwp->cdn_stale_assets($domain);
137 211
138 212 // flush all critical css cache
139 213 $berqwp->purge_critilclcss($domain);
140 -
141 214 }
142 215
143 216 function process_migration() {
144 217 $raw_home = get_option('home');
@@ -177,8 +250,9 @@
177 250
178 251 update_option('berqwp_raw_home', $raw_home);
179 252 delete_transient('berq_lic_response_cache');
180 253 delete_transient('berqwp_lic_response_cache');
254 + delete_transient('berqwp_warmup_running');
181 255 }
182 256 }
183 257
184 258 function post_type_action_links($actions, $post)
@@ -364,8 +438,12 @@
364 438 if (!$can_flush_cache) {
365 439 return;
366 440 }
367 441
442 + if ($new_status == 'draft' && $old_status == 'draft') {
443 + return;
444 + }
445 +
368 446 if ($new_status == 'auto-draft') {
369 447 return;
370 448 }
371 449
@@ -415,12 +493,12 @@
415 493 function brust_cache_for_loggedin()
416 494 {
417 495 if (is_user_logged_in()) {
418 496 // 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");
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");
423 501 }
424 502 }
425 503
426 504 function handle_post_status_update($new_status, $old_status, $post)
@@ -541,9 +619,9 @@
541 619 // $slug = '';
542 620 // }
543 621
544 622 $page_url = home_url($slug);
545 - $host = !empty($_SERVER['HTTP_HOST']) ? $_SERVER['HTTP_HOST'] : '';
623 + $host = !empty($_SERVER['HTTP_HOST']) ? sanitize_text_field(wp_unslash($_SERVER['HTTP_HOST'])) : '';
546 624 berqReverseProxyCache::purge_cache($page_url);
547 625 }
548 626
549 627 function ingore_tracking_params($tracking_params)
@@ -573,11 +651,13 @@
573 651 if (wp_is_post_revision($post_id)) {
574 652 return;
575 653 }
576 654
577 - if (!is_admin()) {
578 - return;
579 - }
655 + // prevents scheduled post public
656 + // via wp cron
657 + // if (!is_admin()) {
658 + // return;
659 + // }
580 660
581 661 $post_type = get_post_type($post_id);
582 662 $post_url = get_permalink($post_id);
583 663
@@ -829,22 +909,27 @@
829 909 // berqHeartbeat::add_queue($page_url);
830 910 warmup_cache_by_url($page_url);
831 911 }
832 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 +
833 921 function html_cache()
834 922 {
835 923
836 924 // Bypass cache for Photon
837 925 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 -
926 + self::miss_cache_headers();
843 927 return;
844 928 }
845 929
846 930 if (!self::should_cache()) {
931 + self::miss_cache_headers();
847 932 return;
848 933 }
849 934
850 935 $page_url = self::get_page_url();
@@ -849,20 +934,32 @@
849 934
850 935 $page_url = self::get_page_url();
851 936
852 937 if (empty($page_url)) {
938 + self::miss_cache_headers();
853 939 return;
854 940 }
855 941
856 942 // Disable cache for unknown query parameters
857 943 if (strpos($page_url, '?') !== false) {
944 + self::miss_cache_headers();
858 945 return;
859 946 }
860 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 +
861 957 if (is_singular()) {
862 958 $post_type = get_post_type();
863 959
864 960 if (empty($post_type) || !in_array($post_type, get_option('berqwp_optimize_post_types'))) {
961 + self::miss_cache_headers();
865 962 return;
866 963 }
867 964 } elseif (is_archive()) {
868 965 $queried_object = get_queried_object();
@@ -870,8 +967,9 @@
870 967 if ($queried_object instanceof WP_Term && !empty($queried_object->taxonomy)) {
871 968 $current_taxonomy = $queried_object->taxonomy;
872 969
873 970 if (!in_array($current_taxonomy, get_option('berqwp_optimize_taxonomies'))) {
971 + self::miss_cache_headers();
874 972 return;
875 973 }
876 974 }
877 975 }
@@ -879,8 +977,9 @@
879 977
880 978 $status_code = http_response_code();
881 979
882 980 if ($status_code !== 200) {
981 + self::miss_cache_headers();
883 982 return;
884 983 }
885 984
886 985 $berqconfigs = berqConfigs::getInstance();
@@ -900,21 +999,38 @@
900 999 header('Last-Modified: ' . gmdate('D, d M Y H:i:s', $lastModified) . ' GMT');
901 1000 header('X-File-Size: ' . filesize($cache_file), true);
902 1001 header('Content-Type: text/html; charset=utf-8');
903 1002 header("X-served: WP Hook");
904 - header('Vary: Cookie');
1003 + header('Vary: Accept-Encoding, Cookie');
1004 + header("X-BerqWP-Cache: HIT");
905 1005
906 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)) {
907 1007 // The client's cache is still valid based on Last-Modified, respond with a 304 Not Modified
908 1008 header('HTTP/1.1 304 Not Modified');
909 1009 // header('Expires: ' . gmdate('D, d M Y H:i:s') . ' GMT');
910 - header("Expires: 0");
911 - 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 +
912 1017 exit();
913 1018 }
914 1019
915 - header('Cache-Control: public, max-age=0, s-maxage=3600, must-revalidate', true);
916 - 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'");
917 1033 header('Content-Encoding: gzip', true);
918 1034 header('Content-Length: ' . filesize($cache_file), true);
919 1035 readfile($cache_file);
920 1036 exit();
@@ -919,12 +1035,9 @@
919 1035 readfile($cache_file);
920 1036 exit();
921 1037 }
922 1038
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
1039 + self::miss_cache_headers();
927 1040
928 1041 self::purge_page($page_url);
929 1042
930 1043 if (!berqwp_can_use_cloud()) {