| @@ -4,13 +4,16 @@ | ||
| 4 | 4 | class Cloudways extends berqIntegrations { |
| 5 | 5 | function __construct() { |
| 6 | 6 | add_action('berqwp_stored_page_cache', [$this, 'flush_page_cache']); |
| 7 | 7 | add_action('berqwp_flush_page_cache', [$this, 'flush_page_cache']); |
| 8 | - add_action('berqwp_flush_all_cache', [$this, 'flush_page_cache']); | |
| 8 | + add_action('berqwp_flush_all_cache', [$this, 'flush_all_page_cache']); | |
| 9 | 9 | } |
| 10 | 10 | |
| 11 | 11 | function flush_page_cache($slug = '/') { |
| 12 | - $page_url = trailingslashit( home_url() . $slug ); | |
| 12 | + if (empty($slug)) { | |
| 13 | + $slug = '/'; | |
| 14 | + } | |
| 15 | + $page_url = home_url($slug); | |
| 13 | 16 | $this->flush_cloudways_varnish($page_url); |
| 14 | 17 | } |
| 15 | 18 | |
| 16 | 19 | function flush_cloudways_varnish($url = '') { |
| @@ -18,12 +21,20 @@ | ||
| 18 | 21 | if (!$this->is_varnish_running()) { |
| 19 | 22 | return; |
| 20 | 23 | } |
| 21 | 24 | |
| 25 | + if (!$this->is_cloudways_hosting()) { | |
| 26 | + return; | |
| 27 | + } | |
| 28 | + | |
| 22 | 29 | if (empty($url)) { |
| 23 | 30 | $url = home_url(); |
| 24 | 31 | } |
| 25 | 32 | |
| 33 | + $host = !empty($_SERVER['HTTP_HOST']) ? sanitize_text_field(wp_unslash($_SERVER['HTTP_HOST'])) : ''; | |
| 34 | + berqReverseProxyCache::purge_varnish_cache($url, ['127.0.0.1', $host], 'URLPURGE'); | |
| 35 | + return; | |
| 36 | + | |
| 26 | 37 | $parse_url = parse_url($url); |
| 27 | 38 | $host = $parse_url['host']; |
| 28 | 39 | |
| 29 | 40 | // Setup the request to purge Varnish cache |
| @@ -41,8 +52,25 @@ | ||
| 41 | 52 | |
| 42 | 53 | return $httpCode === 200; |
| 43 | 54 | } |
| 44 | 55 | |
| 56 | + function flush_all_page_cache() { | |
| 57 | + | |
| 58 | + if (!$this->is_varnish_running()) { | |
| 59 | + return; | |
| 60 | + } | |
| 61 | + | |
| 62 | + if (!$this->is_cloudways_hosting()) { | |
| 63 | + return; | |
| 64 | + } | |
| 65 | + | |
| 66 | + $url = home_url('/.*'); | |
| 67 | + $host = !empty($_SERVER['HTTP_HOST']) ? sanitize_text_field(wp_unslash($_SERVER['HTTP_HOST'])) : ''; | |
| 68 | + berqReverseProxyCache::purge_varnish_cache($url, ['127.0.0.1', $host], 'PURGE'); | |
| 69 | + return; | |
| 70 | + | |
| 71 | + } | |
| 72 | + | |
| 45 | 73 | function is_varnish_running() { |
| 46 | 74 | // Check if the 'X-Varnish' or 'X-Cache' headers are present in the response headers |
| 47 | 75 | if (isset($_SERVER['HTTP_X_VARNISH']) || isset($_SERVER['HTTP_X_CACHE'])) { |
| 48 | 76 | return true; |
| @@ -51,9 +79,9 @@ | ||
| 51 | 79 | return false; |
| 52 | 80 | } |
| 53 | 81 | |
| 54 | 82 | function is_cloudways_hosting() { |
| 55 | - if (isset($_SERVER['cw_allowed_ip'])) { | |
| 83 | + if (isset($_SERVER['cw_allowed_ip']) || isset($_SERVER['HTTP_CF_IPCOUNTRY']) || strpos($_SERVER['DOCUMENT_ROOT'], '.cloudwaysapps.com') !== false) { | |
| 56 | 84 | return true; |
| 57 | 85 | } |
| 58 | 86 | |
| 59 | 87 | return false; |