PluginProbe
BerqWP – All-In-One Optimization for Core Web Vitals, Cache, CDN, Images, CSS & JavaScript / 4.0.19
BerqWP – All-In-One Optimization for Core Web Vitals, Cache, CDN, Images, CSS & JavaScript v4.0.19
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
searchpro / inc / cache / class-berqreverseproxy.php

class-berqreverseproxy.php in BerqWP – All-In-One Optimization for Core Web Vitals, Cache, CDN, Images, CSS & JavaScript 4.0.19, at inc/cache/class-berqreverseproxy.php

150 lines 4.6 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 class berqReverseProxyCache
4 {
5 public static function bypass()
6 {
7 add_action('send_headers', [self::class, 'handle_bypass']);
8 }
9
10 public static function handle_bypass()
11 {
12 if (headers_sent()) {
13 return;
14 }
15
16 header('Cache-Control: no-cache, no-store, must-revalidate');
17 header('Pragma: no-cache');
18 header('Expires: 0');
19 header('X-Bypass-Cache: ' . time());
20 header('Vary: X-Bypass-Cache');
21 }
22
23 public static function is_reverse_proxy_cache_enabled()
24 {
25 $custom_cache_header = get_option('berqwp_custom_cache_header');
26 return isset($_SERVER['HTTP_X_CACHE']) ||
27 isset($_SERVER['HTTP_X_VARNISH']) ||
28 isset($_SERVER['HTTP_CF_CACHE_STATUS']) ||
29 isset($_SERVER['HTTP_X_CACHE_STATUS']) ||
30 isset($_SERVER['HTTP_X_FASTCGI_CACHE']) ||
31 isset($_SERVER['HTTP_X_HCDN_CACHE_STATUS']) ||
32 isset($_SERVER['HTTP_X_CDN_CACHE_STATUS']) ||
33 isset($_SERVER['HTTP_X_PROXY_CACHE']) ||
34 ($custom_cache_header && isset($_SERVER[$custom_cache_header]));
35 }
36
37 public static function purge_varnish_cache($urls, $server_list = [], $purge_method = 'PURGE', $headers = [])
38 {
39
40 if (!is_array($urls)) {
41 $urls = [$urls];
42 }
43
44 foreach ((array) $urls as $url) {
45 $parsed = parse_url($url);
46 if (!$parsed)
47 continue;
48
49 // Build base URL components
50 $scheme = $parsed['scheme'] ?? 'http';
51 $host = $parsed['host'] ?? '';
52 $path = $parsed['path'] ?? '/';
53 $query = $parsed['query'] ?? '';
54
55 $targets = empty($server_list) ? [['host' => $host, 'url' => $url]] : [];
56
57 // Create server targets if server list exists
58 if (!empty($server_list)) {
59 foreach ($server_list as $server) {
60 $targets[] = [
61 'host' => $host,
62 'url' => "$scheme://$server$path" . ($query ? "?$query" : "")
63 ];
64 }
65 }
66
67 // Send purge requests
68 foreach ($targets as $target) {
69 $res = wp_remote_request($target['url'], [
70 'method' => $purge_method,
71 'headers' => array_merge(
72 ['Host' => $target['host']],
73 $headers
74 ),
75 // 'blocking' => false // Non-blocking for performance
76 ]);
77 // var_dump($res);
78 }
79 }
80 }
81
82 public static function flush_all()
83 {
84 if (!self::is_reverse_proxy_cache_enabled()) {
85 return false;
86 }
87
88 $results = [];
89
90 // Attempt full site purge using home URL with wildcard
91 $home_url = home_url('/*');
92
93 $results['ban'] = self::purge_varnish_cache(home_url('/.*'));
94
95 // Second method: Generic PURGE with regex
96 $results['purge'] = wp_remote_request($home_url, [
97 'method' => 'PURGE',
98 'headers' => [
99 'Host' => parse_url(home_url(), PHP_URL_HOST),
100 'X-Purge-Regex' => '.*'
101 ]
102 ]);
103
104 // Third method: CacheFlush-style header
105 $results['invalidate'] = wp_remote_request($home_url, [
106 'method' => 'INVALIDATE',
107 'headers' => [
108 'Host' => parse_url(home_url(), PHP_URL_HOST)
109 ]
110 ]);
111
112 // Return true if any method succeeded
113 return in_array(true, $results, true);
114 }
115
116 public static function purge_cache($url)
117 {
118 if (!self::is_reverse_proxy_cache_enabled()) {
119 return false;
120 }
121
122 $parsed_url = parse_url($url);
123 $host = $parsed_url['host'];
124
125 wp_remote_request(
126 $url,
127 array(
128 'method' => 'PURGE',
129 'headers' => array(
130 'Host' => $host
131 )
132 )
133 );
134
135 self::purge_varnish_cache($url);
136
137 if (strpos($url, '/.*') !== false) {
138 $url = str_replace('/.*', '/', $url);
139 }
140
141 // $response = wp_remote_get($url, array(
142 // 'headers' => array(
143 // 'Cookie' => 'wordpress_logged_in_',
144 // 'Cache-Control' => 'no-cache, must-revalidate, max-age=0',
145 // 'Pragma' => 'no-cache'
146 // )
147 // ));
148 }
149 }
150