PluginProbe
BerqWP – All-In-One Optimization for Core Web Vitals, Cache, CDN, Images, CSS & JavaScript / 4.1.20
BerqWP – All-In-One Optimization for Core Web Vitals, Cache, CDN, Images, CSS & JavaScript v4.1.20
4.1.20 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 All 174 releases
← All changes | inc/cache/class-berqcache.php +153 -35 4.0.16 → 4.1.20 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');
@@ -364,8 +437,12 @@
364 437 if (!$can_flush_cache) {
365 438 return;
366 439 }
367 440
441 + if ($new_status == 'draft' && $old_status == 'draft') {
442 + return;
443 + }
444 +
368 445 if ($new_status == 'auto-draft') {
369 446 return;
370 447 }
371 448
@@ -415,12 +492,12 @@
415 492 function brust_cache_for_loggedin()
416 493 {
417 494 if (is_user_logged_in()) {
418 495 // 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");
496 + header('Vary: Accept-Encoding, Cookie');
497 + // header("Cache-Control: no-cache, no-store, must-revalidate");
498 + // header("Pragma: no-cache");
499 + // header("Expires: 0");
423 500 }
424 501 }
425 502
426 503 function handle_post_status_update($new_status, $old_status, $post)
@@ -463,14 +540,13 @@
463 540 }
464 541
465 542 public static function delete_page_cache_files($page_url)
466 543 {
467 - $cache_dir = bwp_get_cache_dir() . bwp_build_cache_path($page_url);
544 + $cache_file = bwp_get_cache_dir() . bwp_build_cache_path($page_url) . '/index.html.gz';
468 545
469 - if (is_dir($cache_dir)) {
470 - berqwp_unlink_recursive($cache_dir);
546 + if (file_exists($cache_file)) {
547 + unlink($cache_file);
471 548 }
472 -
473 549 }
474 550
475 551 public static function purge_page($page_url, $flush_criticalcss = false)
476 552 {
@@ -542,9 +618,9 @@
542 618 // $slug = '';
543 619 // }
544 620
545 621 $page_url = home_url($slug);
546 - $host = !empty($_SERVER['HTTP_HOST']) ? $_SERVER['HTTP_HOST'] : '';
622 + $host = !empty($_SERVER['HTTP_HOST']) ? sanitize_text_field(wp_unslash($_SERVER['HTTP_HOST'])) : '';
547 623 berqReverseProxyCache::purge_cache($page_url);
548 624 }
549 625
550 626 function ingore_tracking_params($tracking_params)
@@ -574,11 +650,13 @@
574 650 if (wp_is_post_revision($post_id)) {
575 651 return;
576 652 }
577 653
578 - if (!is_admin()) {
579 - return;
580 - }
654 + // prevents scheduled post public
655 + // via wp cron
656 + // if (!is_admin()) {
657 + // return;
658 + // }
581 659
582 660 $post_type = get_post_type($post_id);
583 661 $post_url = get_permalink($post_id);
584 662
@@ -820,26 +898,37 @@
820 898 if (!berqwp_can_use_cloud()) {
821 899 return false;
822 900 }
823 901
902 + $status_code = http_response_code();
903 +
904 + if ($status_code !== 200) {
905 + return false;
906 + }
907 +
824 908 // berqHeartbeat::add_queue($page_url);
825 909 warmup_cache_by_url($page_url);
826 910 }
827 911
912 + static function miss_cache_headers() {
913 + header('Cache-Control: no-store, no-cache, must-revalidate, max-age=0', true);
914 + header('CDN-Cache-Control: no-store', true); // Cloudflare
915 + header('Surrogate-Control: no-store', true); // Varnish
916 + header('X-Accel-Expires: 0', true); // Nginx FastCGI
917 + header("X-BerqWP-Cache: MISS");
918 + }
919 +
828 920 function html_cache()
829 921 {
830 922
831 923 // Bypass cache for Photon
832 924 if (!empty($_COOKIE['berqwpnocache'])) {
833 - header('Cache-Control: no-store, no-cache, must-revalidate, max-age=0', true);
834 - header('CDN-Cache-Control: no-store', true); // Cloudflare
835 - header('Surrogate-Control: no-store', true); // Varnish
836 - header('X-Accel-Expires: 0', true); // Nginx FastCGI
837 -
925 + self::miss_cache_headers();
838 926 return;
839 927 }
840 928
841 929 if (!self::should_cache()) {
930 + self::miss_cache_headers();
842 931 return;
843 932 }
844 933
845 934 $page_url = self::get_page_url();
@@ -844,20 +933,33 @@
844 933
845 934 $page_url = self::get_page_url();
846 935
847 936 if (empty($page_url)) {
937 + self::miss_cache_headers();
848 938 return;
849 939 }
850 940
851 941 // Disable cache for unknown query parameters
852 942 if (strpos($page_url, '?') !== false) {
943 + self::miss_cache_headers();
853 944 return;
854 945 }
855 946
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 +
856 957 if (is_singular()) {
857 958 $post_type = get_post_type();
858 959
859 960 if (empty($post_type) || !in_array($post_type, get_option('berqwp_optimize_post_types'))) {
961 + self::miss_cache_headers();
860 962 return;
861 963 }
862 964 } elseif (is_archive()) {
863 965 $queried_object = get_queried_object();
@@ -865,8 +967,9 @@
865 967 if ($queried_object instanceof WP_Term && !empty($queried_object->taxonomy)) {
866 968 $current_taxonomy = $queried_object->taxonomy;
867 969
868 970 if (!in_array($current_taxonomy, get_option('berqwp_optimize_taxonomies'))) {
971 + self::miss_cache_headers();
869 972 return;
870 973 }
871 974 }
872 975 }
@@ -874,8 +977,9 @@
874 977
875 978 $status_code = http_response_code();
876 979
877 980 if ($status_code !== 200) {
981 + self::miss_cache_headers();
878 982 return;
879 983 }
880 984
881 985 $berqconfigs = berqConfigs::getInstance();
@@ -895,21 +999,38 @@
895 999 header('Last-Modified: ' . gmdate('D, d M Y H:i:s', $lastModified) . ' GMT');
896 1000 header('X-File-Size: ' . filesize($cache_file), true);
897 1001 header('Content-Type: text/html; charset=utf-8');
898 1002 header("X-served: WP Hook");
899 - header('Vary: Cookie');
1003 + header('Vary: Accept-Encoding, Cookie');
1004 + header("X-BerqWP-Cache: HIT");
900 1005
901 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)) {
902 1007 // The client's cache is still valid based on Last-Modified, respond with a 304 Not Modified
903 1008 header('HTTP/1.1 304 Not Modified');
904 1009 // header('Expires: ' . gmdate('D, d M Y H:i:s') . ' GMT');
905 - header("Expires: 0");
906 - 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 +
907 1017 exit();
908 1018 }
909 1019
910 - header('Cache-Control: public, max-age=0, s-maxage=3600, must-revalidate', true);
911 - 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'");
912 1033 header('Content-Encoding: gzip', true);
913 1034 header('Content-Length: ' . filesize($cache_file), true);
914 1035 readfile($cache_file);
915 1036 exit();
@@ -914,12 +1035,9 @@
914 1035 readfile($cache_file);
915 1036 exit();
916 1037 }
917 1038
918 - header('Cache-Control: no-store, no-cache, must-revalidate, max-age=0');
919 - header('CDN-Cache-Control: no-store'); // Cloudflare
920 - header('Surrogate-Control: no-store'); // Varnish
921 - header('X-Accel-Expires: 0'); // Nginx FastCGI
1039 + self::miss_cache_headers();
922 1040
923 1041 self::purge_page($page_url);
924 1042
925 1043 if (!berqwp_can_use_cloud()) {