PluginProbe
BerqWP – All-In-One Optimization for Core Web Vitals, Cache, CDN, Images, CSS & JavaScript / trunk
BerqWP – All-In-One Optimization for Core Web Vitals, Cache, CDN, Images, CSS & JavaScript vtrunk
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 1.9.4 1.9.5 1.9.6 All 170 releases
← All changes | inc/cache/class-berqcache.php +134 -35 4.0.16trunk 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)
@@ -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)
@@ -463,14 +531,13 @@
463 531 }
464 532
465 533 public static function delete_page_cache_files($page_url)
466 534 {
467 - $cache_dir = bwp_get_cache_dir() . bwp_build_cache_path($page_url);
535 + $cache_file = bwp_get_cache_dir() . bwp_build_cache_path($page_url) . '/index.html.gz';
468 536
469 - if (is_dir($cache_dir)) {
470 - berqwp_unlink_recursive($cache_dir);
537 + if (file_exists($cache_file)) {
538 + unlink($cache_file);
471 539 }
472 -
473 540 }
474 541
475 542 public static function purge_page($page_url, $flush_criticalcss = false)
476 543 {
@@ -542,9 +609,9 @@
542 609 // $slug = '';
543 610 // }
544 611
545 612 $page_url = home_url($slug);
546 - $host = !empty($_SERVER['HTTP_HOST']) ? $_SERVER['HTTP_HOST'] : '';
613 + $host = !empty($_SERVER['HTTP_HOST']) ? sanitize_text_field(wp_unslash($_SERVER['HTTP_HOST'])) : '';
547 614 berqReverseProxyCache::purge_cache($page_url);
548 615 }
549 616
550 617 function ingore_tracking_params($tracking_params)
@@ -574,11 +641,13 @@
574 641 if (wp_is_post_revision($post_id)) {
575 642 return;
576 643 }
577 644
578 - if (!is_admin()) {
579 - return;
580 - }
645 + // prevents scheduled post public
646 + // via wp cron
647 + // if (!is_admin()) {
648 + // return;
649 + // }
581 650
582 651 $post_type = get_post_type($post_id);
583 652 $post_url = get_permalink($post_id);
584 653
@@ -820,26 +889,37 @@
820 889 if (!berqwp_can_use_cloud()) {
821 890 return false;
822 891 }
823 892
893 + $status_code = http_response_code();
894 +
895 + if ($status_code !== 200) {
896 + return false;
897 + }
898 +
824 899 // berqHeartbeat::add_queue($page_url);
825 900 warmup_cache_by_url($page_url);
826 901 }
827 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 +
828 911 function html_cache()
829 912 {
830 913
831 914 // Bypass cache for Photon
832 915 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 -
916 + self::miss_cache_headers();
838 917 return;
839 918 }
840 919
841 920 if (!self::should_cache()) {
921 + self::miss_cache_headers();
842 922 return;
843 923 }
844 924
845 925 $page_url = self::get_page_url();
@@ -844,13 +924,15 @@
844 924
845 925 $page_url = self::get_page_url();
846 926
847 927 if (empty($page_url)) {
928 + self::miss_cache_headers();
848 929 return;
849 930 }
850 931
851 932 // Disable cache for unknown query parameters
852 933 if (strpos($page_url, '?') !== false) {
934 + self::miss_cache_headers();
853 935 return;
854 936 }
855 937
856 938 if (is_singular()) {
@@ -856,8 +938,9 @@
856 938 if (is_singular()) {
857 939 $post_type = get_post_type();
858 940
859 941 if (empty($post_type) || !in_array($post_type, get_option('berqwp_optimize_post_types'))) {
942 + self::miss_cache_headers();
860 943 return;
861 944 }
862 945 } elseif (is_archive()) {
863 946 $queried_object = get_queried_object();
@@ -865,8 +948,9 @@
865 948 if ($queried_object instanceof WP_Term && !empty($queried_object->taxonomy)) {
866 949 $current_taxonomy = $queried_object->taxonomy;
867 950
868 951 if (!in_array($current_taxonomy, get_option('berqwp_optimize_taxonomies'))) {
952 + self::miss_cache_headers();
869 953 return;
870 954 }
871 955 }
872 956 }
@@ -874,8 +958,9 @@
874 958
875 959 $status_code = http_response_code();
876 960
877 961 if ($status_code !== 200) {
962 + self::miss_cache_headers();
878 963 return;
879 964 }
880 965
881 966 $berqconfigs = berqConfigs::getInstance();
@@ -895,21 +980,38 @@
895 980 header('Last-Modified: ' . gmdate('D, d M Y H:i:s', $lastModified) . ' GMT');
896 981 header('X-File-Size: ' . filesize($cache_file), true);
897 982 header('Content-Type: text/html; charset=utf-8');
898 983 header("X-served: WP Hook");
899 - header('Vary: Cookie');
984 + header('Vary: Accept-Encoding, Cookie');
985 + header("X-BerqWP-Cache: HIT");
900 986
901 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)) {
902 988 // The client's cache is still valid based on Last-Modified, respond with a 304 Not Modified
903 989 header('HTTP/1.1 304 Not Modified');
904 990 // header('Expires: ' . gmdate('D, d M Y H:i:s') . ' GMT');
905 - header("Expires: 0");
906 - 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 +
907 998 exit();
908 999 }
909 1000
910 - header('Cache-Control: public, max-age=0, s-maxage=3600, must-revalidate', true);
911 - 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'");
912 1014 header('Content-Encoding: gzip', true);
913 1015 header('Content-Length: ' . filesize($cache_file), true);
914 1016 readfile($cache_file);
915 1017 exit();
@@ -914,12 +1016,9 @@
914 1016 readfile($cache_file);
915 1017 exit();
916 1018 }
917 1019
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
1020 + self::miss_cache_headers();
922 1021
923 1022 self::purge_page($page_url);
924 1023
925 1024 if (!berqwp_can_use_cloud()) {