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/common-functions.php +109 -7 4.0.19 → 4.1.18 View file →
@@ -24,12 +24,49 @@
24 24
25 25 return true;
26 26 }
27 27
28 +function bwp_request_is_https() {
29 + if (!empty($_SERVER['HTTPS']) && strtolower($_SERVER['HTTPS']) !== 'off') {
30 + return true;
31 + }
32 +
33 + // Reverse proxy / load balancer terminating TLS upstream
34 + if (!empty($_SERVER['HTTP_X_FORWARDED_PROTO'])) {
35 + // may be a comma-separated chain: "https,http" — first hop is the client
36 + $proto = strtolower(trim(explode(',', $_SERVER['HTTP_X_FORWARDED_PROTO'])[0]));
37 + if ($proto === 'https') {
38 + return true;
39 + }
40 + }
41 +
42 + if (!empty($_SERVER['HTTP_X_FORWARDED_SSL']) && strtolower($_SERVER['HTTP_X_FORWARDED_SSL']) === 'on') {
43 + return true;
44 + }
45 +
46 + if (!empty($_SERVER['HTTP_FRONT_END_HTTPS']) && strtolower($_SERVER['HTTP_FRONT_END_HTTPS']) === 'on') {
47 + return true;
48 + }
49 +
50 + if (!empty($_SERVER['SERVER_PORT']) && (int) $_SERVER['SERVER_PORT'] === 443) {
51 + return true;
52 + }
53 +
54 + return false;
55 +}
56 +
28 57 function bwp_get_request_url() {
29 - $scheme = (!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off') ? 'https://' : 'http://';
30 - $host = $_SERVER['HTTP_HOST'] ?? 'localhost'; // Fallback to 'localhost' if unavailable
31 - $uri = $_SERVER['REQUEST_URI'] ?? '/';
58 +
59 + $scheme = 'http://';
60 +
61 + if (bwp_request_is_https()) {
62 + $scheme = 'https://';
63 + } elseif (function_exists('wp_parse_url') && function_exists('get_option')) {
64 + $scheme = (wp_parse_url(get_option('home'), PHP_URL_SCHEME) === 'https') ? 'https://' : 'http://';
65 + }
66 +
67 + $host = isset($_SERVER['HTTP_HOST']) ? strip_tags(stripslashes($_SERVER['HTTP_HOST'])) : 'localhost';
68 + $uri = isset($_SERVER['REQUEST_URI']) ? strip_tags(stripslashes($_SERVER['REQUEST_URI'])) : '/';
32 69 $url = $scheme . $host . $uri;
33 70 return strtolower($url);
34 71 }
35 72
@@ -91,10 +128,49 @@
91 128
92 129 return $host . $path . '/q-' . $query_hash;
93 130 }
94 131
132 +/**
133 + * Redirect a mismatched-trailing-slash request to its canonical form, the same
134 + * way WordPress's redirect_canonical() would — but without needing WordPress
135 + * to boot first. Used by the advanced-cache.php dropin, which runs before the
136 + * DB connection is available.
137 + */
138 +function bwp_redirect_to_canonical_slash($wants_trailing_slash) {
139 + $uri = isset($_SERVER['REQUEST_URI']) ? $_SERVER['REQUEST_URI'] : '/';
140 + $parts = explode('?', $uri, 2);
141 + $path = $parts[0];
142 + $query = isset($parts[1]) ? '?' . $parts[1] : '';
143 +
144 + if ($wants_trailing_slash) {
145 + $path = rtrim($path, '/') . '/';
146 + } else {
147 + $path = rtrim($path, '/');
148 + if ($path === '') {
149 + $path = '/';
150 + }
151 + }
152 +
153 + $scheme = bwp_request_is_https() ? 'https://' : 'http://';
154 + $host = isset($_SERVER['HTTP_HOST']) ? strip_tags(stripslashes($_SERVER['HTTP_HOST'])) : '';
155 +
156 + header('Location: ' . $scheme . $host . $path . $query, true, 301);
157 + exit();
158 +}
159 +
95 160 function bwp_serve_advanced_cache($serve_from = 'plugin') {
161 +
162 + if (php_sapi_name() === 'cli' || (defined('WP_CLI') && WP_CLI)) {
163 + return;
164 + }
165 +
166 + if ( strpos( $_SERVER["REQUEST_URI"], "berqwp-ping" ) !== false ) {
167 + echo "PONG";
168 + exit();
169 + }
170 +
96 171 if (isset($_SERVER['REQUEST_METHOD']) && $_SERVER['REQUEST_METHOD'] === 'GET' && !bwp_is_user_logged_in() && !bwp_is_ajax() && !berqDetectCrawler::is_crawler() && bwp_pass_cookie_requirement()) {
172 +
97 173 $berqconfigs = berqConfigs::getInstance();
98 174 $configs = $berqconfigs->get_configs();
99 175 $url = bwp_get_request_url();
100 176 $url = @dropin_remove_ignore_params($url);
@@ -112,13 +188,22 @@
112 188 // $compression_enabled = $configs['page_compression'] === true;
113 189 $compression_enabled = true;
114 190 $accept_encoding = $_SERVER['HTTP_ACCEPT_ENCODING'] ?? '';
115 191 $supports_gzip = strpos($accept_encoding, 'gzip') !== false;
192 + $domain = isset($_SERVER['HTTP_HOST']) ? strip_tags(stripslashes($_SERVER['HTTP_HOST'])) : '';
116 193
117 194 if (berqwp_dropin_is_page_url_excluded($url)) {
118 195 return;
119 196 }
120 197
198 + $request_path = parse_url($url, PHP_URL_PATH) ?? '/';
199 + if ($request_path !== '/' && isset($configs['permalink_trailing_slash']) && $configs['permalink_trailing_slash'] !== null) {
200 + $has_trailing_slash = substr($request_path, -1) === '/';
201 + if ($configs['permalink_trailing_slash'] !== $has_trailing_slash) {
202 + bwp_redirect_to_canonical_slash($configs['permalink_trailing_slash']);
203 + }
204 + }
205 +
121 206 if (file_exists($cache_file) && $cache_max_life > time()) {
122 207 $file_content = @file_get_contents($cache_file);
123 208
124 209 if (!isset($_GET['creating_cache']) && file_exists($cache_file)) {
@@ -140,9 +225,10 @@
140 225 header('Last-Modified: ' . gmdate('D, d M Y H:i:s', $lastModified) . ' GMT');
141 226 header('X-File-Size: ' . filesize($cache_file), true);
142 227 header('Content-Type: text/html; charset=utf-8');
143 228 header("X-served: $serve_from");
144 - header('Vary: Cookie');
229 + header("X-BerqWP-Cache: HIT");
230 + // header('Vary: Cookie');
145 231
146 232 // Check if the client has a cached copy and if it's still valid using Last-Modified
147 233 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)) {
148 234 // The client's cache is still valid based on Last-Modified, respond with a 304 Not Modified
@@ -147,16 +233,32 @@
147 233 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)) {
148 234 // The client's cache is still valid based on Last-Modified, respond with a 304 Not Modified
149 235 header('HTTP/1.1 304 Not Modified');
150 236 // header('Expires: ' . gmdate('D, d M Y H:i:s') . ' GMT');
151 - header("Expires: 0");
152 - header('Cache-Control: no-cache, must-revalidate');
237 + // header("Expires: 0");
238 + // header('Cache-Control: no-cache, must-revalidate');
239 + header('Cache-Control: public, max-age=600, s-maxage=2592000, stale-while-revalidate=86400', true);
240 + header('cdn-cache-control: max-age=2592000');
241 + header("Cache-Tag: $domain");
153 242 exit();
154 243
155 244 }
156 245
246 + // Prevent PHP/Apache from re-compressing the already-gzipped cache file
247 + if (ini_get('zlib.output_compression')) {
248 + ini_set('zlib.output_compression', 'Off');
249 + }
250 +
251 + while (ob_get_level()) {
252 + ob_end_clean();
253 + }
254 +
157 255 // header('Cache-Control: public, max-age=0, s-maxage=3600, must-revalidate', true);
158 - header('Cache-Control: public, max-age=60, s-maxage=3600, stale-while-revalidate=60, must-revalidate', true);
256 + // header('Cache-Control: public, max-age=60, s-maxage=3600, stale-while-revalidate=60, must-revalidate', true);
257 + header('Cache-Control: public, max-age=600, s-maxage=2592000, stale-while-revalidate=86400', true);
258 + header('cdn-cache-control: max-age=2592000');
259 + header("Cache-Tag: $domain");
260 + // header("Content-Security-Policy: script-src 'self' blob: 'unsafe-inline'");
159 261 header('Vary: Accept-Encoding, Cookie');
160 262 header('Content-Encoding: gzip', true);
161 263 header('Content-Length: ' . filesize($cache_file), true);
162 264 readfile($cache_file);