| 1 |
<?php |
| 2 |
if (!defined('ABSPATH')) exit; |
| 3 |
|
| 4 |
class Siteground 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_cache']); |
| 9 |
} |
| 10 |
|
| 11 |
function flush_cache() { |
| 12 |
$this->purge_siteground_url(trailingslashit( home_url()."/(.*)" )); |
| 13 |
} |
| 14 |
|
| 15 |
function flush_page_cache($slug) { |
| 16 |
$page_url = home_url() . $slug; |
| 17 |
$this->purge_siteground_url($page_url); |
| 18 |
} |
| 19 |
|
| 20 |
function purge_siteground_url($url) { |
| 21 |
// Check if the site is hosted on SiteGround |
| 22 |
if (!$this->is_siteground()) { |
| 23 |
return false; |
| 24 |
} |
| 25 |
|
| 26 |
// Construct the URL object to parse host and path |
| 27 |
$urlObj = parse_url($url); |
| 28 |
$host = preg_replace("/^www\./", "", $urlObj['host']); |
| 29 |
$path = isset($urlObj['path']) ? $urlObj['path'] : '/'; |
| 30 |
|
| 31 |
// If there's a query string, append it to the path |
| 32 |
if (!empty($urlObj['query'])) { |
| 33 |
// $path .= '?' . $urlObj['query']; |
| 34 |
$path .= "(.*)"; |
| 35 |
} |
| 36 |
|
| 37 |
try { |
| 38 |
$sock_path = '/chroot/tmp/site-tools.sock'; // Adjust the socket path as per your SiteGround setup |
| 39 |
|
| 40 |
// Check if the socket file exists |
| 41 |
if (!file_exists($sock_path)) { |
| 42 |
return false; |
| 43 |
} |
| 44 |
|
| 45 |
// Open a socket connection |
| 46 |
$sock = stream_socket_client('unix://' . $sock_path, $errno, $errstr, 5); |
| 47 |
|
| 48 |
// Check if the connection was successful |
| 49 |
if (false === $sock) { |
| 50 |
return false; |
| 51 |
} |
| 52 |
|
| 53 |
// Construct the purge request |
| 54 |
$req = array( |
| 55 |
'api' => 'domain-all', |
| 56 |
'cmd' => 'update', |
| 57 |
'settings' => array('json' => 1), |
| 58 |
'params' => array( |
| 59 |
'flush_cache' => '1', |
| 60 |
'id' => $host, |
| 61 |
'path' => $path, |
| 62 |
), |
| 63 |
); |
| 64 |
|
| 65 |
// Send the request to the socket |
| 66 |
fwrite($sock, json_encode($req, JSON_FORCE_OBJECT) . "\n"); |
| 67 |
|
| 68 |
// Read the response from the socket |
| 69 |
$response = fgets($sock, 32 * 1024); |
| 70 |
|
| 71 |
// Close the socket connection |
| 72 |
fclose($sock); |
| 73 |
|
| 74 |
// Decode the response JSON |
| 75 |
$result = @json_decode($response, true); |
| 76 |
|
| 77 |
// Check for errors in the response |
| 78 |
if (false === $result || isset($result['err_code'])) { |
| 79 |
return false; |
| 80 |
} |
| 81 |
} catch (Exception $e) { |
| 82 |
// Handle any exceptions that occur during the process |
| 83 |
return false; |
| 84 |
} |
| 85 |
|
| 86 |
// Return true on successful cache purge |
| 87 |
return true; |
| 88 |
} |
| 89 |
|
| 90 |
function is_siteground() { |
| 91 |
// Check by hostname or wp-config.php content |
| 92 |
if (strpos(gethostname(), "siteground.eu") !== false) { |
| 93 |
return true; |
| 94 |
} |
| 95 |
|
| 96 |
$configFilePath = ABSPATH . 'wp-config.php'; |
| 97 |
if (!file_exists($configFilePath)) { |
| 98 |
return false; |
| 99 |
} |
| 100 |
|
| 101 |
// Check for SiteGround-specific marker in wp-config.php |
| 102 |
$configContent = file_get_contents($configFilePath); |
| 103 |
return strpos($configContent, 'Added by SiteGround WordPress management system') !== false; |
| 104 |
} |
| 105 |
} |
| 106 |
|
| 107 |
new Siteground(); |