| 1 |
<?php |
| 2 |
if (!defined('ABSPATH')) exit; |
| 3 |
|
| 4 |
class Cloudways extends berqIntegrations { |
| 5 |
function __construct() { |
| 6 |
add_action('berqwp_stored_page_cache', [$this, 'flush_page_cache']); |
| 7 |
add_action('berqwp_flush_page_cache', [$this, 'flush_page_cache']); |
| 8 |
add_action('berqwp_flush_all_cache', [$this, 'flush_page_cache']); |
| 9 |
} |
| 10 |
|
| 11 |
function flush_page_cache($slug = '/') { |
| 12 |
$page_url = trailingslashit( home_url() . $slug ); |
| 13 |
$this->flush_cloudways_varnish($page_url); |
| 14 |
} |
| 15 |
|
| 16 |
function flush_cloudways_varnish($url = '') { |
| 17 |
|
| 18 |
if (!$this->is_varnish_running()) { |
| 19 |
return; |
| 20 |
} |
| 21 |
|
| 22 |
if (empty($url)) { |
| 23 |
$url = home_url(); |
| 24 |
} |
| 25 |
|
| 26 |
$parse_url = parse_url($url); |
| 27 |
$host = $parse_url['host']; |
| 28 |
|
| 29 |
// Setup the request to purge Varnish cache |
| 30 |
$ch = curl_init(); |
| 31 |
curl_setopt($ch, CURLOPT_URL, $url); |
| 32 |
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'PURGE'); |
| 33 |
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); |
| 34 |
curl_setopt($ch, CURLOPT_HTTPHEADER, [ |
| 35 |
"Host: $host" |
| 36 |
]); |
| 37 |
|
| 38 |
$response = curl_exec($ch); |
| 39 |
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE); |
| 40 |
curl_close($ch); |
| 41 |
|
| 42 |
return $httpCode === 200; |
| 43 |
} |
| 44 |
|
| 45 |
function is_varnish_running() { |
| 46 |
// Check if the 'X-Varnish' or 'X-Cache' headers are present in the response headers |
| 47 |
if (isset($_SERVER['HTTP_X_VARNISH']) || isset($_SERVER['HTTP_X_CACHE'])) { |
| 48 |
return true; |
| 49 |
} |
| 50 |
|
| 51 |
return false; |
| 52 |
} |
| 53 |
|
| 54 |
function is_cloudways_hosting() { |
| 55 |
if (isset($_SERVER['cw_allowed_ip'])) { |
| 56 |
return true; |
| 57 |
} |
| 58 |
|
| 59 |
return false; |
| 60 |
} |
| 61 |
} |
| 62 |
|
| 63 |
new Cloudways(); |