| 1 |
<?php |
| 2 |
|
| 3 |
class RabbitLoader_21_TP |
| 4 |
{ |
| 5 |
|
| 6 |
const PURGE_ALL = 1; |
| 7 |
const PURGE_ID = 2; |
| 8 |
const PURGE_URL = 3; |
| 9 |
private static $rl_wp_options = []; |
| 10 |
|
| 11 |
public static function purge_all(&$success_count) |
| 12 |
{ |
| 13 |
self::call_tpvs(self::PURGE_ALL, '', $success_count); |
| 14 |
} |
| 15 |
|
| 16 |
public static function purge_url($url, &$success_count) |
| 17 |
{ |
| 18 |
self::call_tpvs(self::PURGE_URL, $url, $success_count); |
| 19 |
} |
| 20 |
|
| 21 |
public static function purge_post_id($post_id, &$success_count) |
| 22 |
{ |
| 23 |
self::call_tpvs(self::PURGE_ID, $post_id, $success_count); |
| 24 |
} |
| 25 |
|
| 26 |
private static function call_tpvs($purge_type, $url_id, &$success_count) |
| 27 |
{ |
| 28 |
RabbitLoader_21_Core::getWpOption(RabbitLoader_21_TP::$rl_wp_options); |
| 29 |
$class_methods = get_class_methods('RabbitLoader_21_TP'); |
| 30 |
foreach ($class_methods as $method_name) { |
| 31 |
if (substr($method_name, 0, 3) == 'tpv') { |
| 32 |
try { |
| 33 |
if (self::$method_name($purge_type, $url_id)) { |
| 34 |
++$success_count; |
| 35 |
} |
| 36 |
} catch (\Throwable $e) { |
| 37 |
RabbitLoader_21_Core::on_exception($e); |
| 38 |
} |
| 39 |
} |
| 40 |
} |
| 41 |
} |
| 42 |
|
| 43 |
private static function tpv_wp_core($purge_type, $url_id) |
| 44 |
{ |
| 45 |
if (function_exists('wp_cache_flush')) { |
| 46 |
wp_cache_flush(); |
| 47 |
} |
| 48 |
|
| 49 |
if (function_exists('opcache_reset')) { |
| 50 |
opcache_reset(); |
| 51 |
} |
| 52 |
|
| 53 |
if (function_exists('apc_clear_cache')) { |
| 54 |
apc_clear_cache(); |
| 55 |
} |
| 56 |
} |
| 57 |
|
| 58 |
private static function tpv_litespeed($purge_type, $url_id) |
| 59 |
{ |
| 60 |
$isLS = !empty($_SERVER["X-LSCACHE"]) || (!empty($_SERVER["SERVER_SOFTWARE"]) && "litespeed" == strtolower($_SERVER["SERVER_SOFTWARE"])) || RabbitLoader_21_Util_Core::isDev(); |
| 61 |
if (!$isLS) { |
| 62 |
return false; |
| 63 |
} |
| 64 |
if ($purge_type == self::PURGE_ALL) { |
| 65 |
RabbitLoader\SDK\Util::sendHeader("X-LiteSpeed-Purge: *", false); |
| 66 |
} elseif ($purge_type == self::PURGE_URL) { |
| 67 |
$path = parse_url($url_id, PHP_URL_PATH); |
| 68 |
if (empty($path)) { |
| 69 |
$path = '/'; |
| 70 |
} |
| 71 |
RabbitLoader\SDK\Util::sendHeader("X-LiteSpeed-Purge: " . $path, false); |
| 72 |
} |
| 73 |
return true; |
| 74 |
} |
| 75 |
|
| 76 |
/** |
| 77 |
* https://github.com/pantheon-systems/pantheon-advanced-page-cache/blob/master/pantheon-advanced-page-cache.php |
| 78 |
*/ |
| 79 |
private static function tpv_pantheon($purge_type, $url_id) |
| 80 |
{ |
| 81 |
if (!function_exists('pantheon_wp_clear_edge_all')) { |
| 82 |
return false; |
| 83 |
} |
| 84 |
if ($purge_type == self::PURGE_ALL) { |
| 85 |
pantheon_wp_clear_edge_all(); |
| 86 |
} elseif ($purge_type == self::PURGE_ID) { |
| 87 |
do_action('clean_post_cache', $url_id); |
| 88 |
} |
| 89 |
return true; |
| 90 |
} |
| 91 |
|
| 92 |
private static function tpv_nginx($purge_type, $url_id) |
| 93 |
{ |
| 94 |
global $nginx_purger; |
| 95 |
if (empty($nginx_purger)) { |
| 96 |
return false; |
| 97 |
} |
| 98 |
if ($purge_type == self::PURGE_ALL) { |
| 99 |
$nginx_purger->purge_all(); |
| 100 |
} elseif ($purge_type == self::PURGE_URL) { |
| 101 |
$nginx_purger->purge_url($url_id); |
| 102 |
} |
| 103 |
return true; |
| 104 |
} |
| 105 |
|
| 106 |
/** |
| 107 |
* https://github.com/Savvii/warpdrive/ |
| 108 |
*/ |
| 109 |
private static function tpv_savii($purge_type, $url_id) |
| 110 |
{ |
| 111 |
if (!defined('\Savvii\CacheFlusherPlugin::NAME_DOMAINFLUSH_NOW')) { |
| 112 |
return false; |
| 113 |
} |
| 114 |
do_action('warpdrive_domain_flush'); |
| 115 |
return true; |
| 116 |
} |
| 117 |
|
| 118 |
private static function tpv_ninukis($purge_type, $url_id) |
| 119 |
{ |
| 120 |
if (!defined('WP_NINUKIS_WP_NAME') || !class_exists('Ninukis_Plugin')) { |
| 121 |
return false; |
| 122 |
} |
| 123 |
if ($purge_type == self::PURGE_ALL) { |
| 124 |
$purge_pressidum = Ninukis_Plugin::get_instance(); |
| 125 |
$purge_pressidum->purgeAllCaches(); |
| 126 |
return true; |
| 127 |
} elseif ($purge_type == self::PURGE_ID) { |
| 128 |
} |
| 129 |
} |
| 130 |
|
| 131 |
private static function tpv_pagely_cache($purge_type, $url_id) |
| 132 |
{ |
| 133 |
if (!class_exists('\PagelyCachePurge')) { |
| 134 |
return false; |
| 135 |
} |
| 136 |
if ($purge_type == self::PURGE_ALL) { |
| 137 |
$purge_pagely = new PagelyCachePurge(); |
| 138 |
$purge_pagely->purgeAll(); |
| 139 |
} elseif ($purge_type == self::PURGE_URL) { |
| 140 |
$purge_pagely = new PagelyCachePurge(); |
| 141 |
$purge_pagely->purgePath(parse_url($url_id, PHP_URL_PATH) . "(.*)"); |
| 142 |
} |
| 143 |
return true; |
| 144 |
} |
| 145 |
|
| 146 |
//wpengine - https://wpengine.com/support/cache/ |
| 147 |
private static function tpv_wpengine($purge_type, $url_id) |
| 148 |
{ |
| 149 |
if (!class_exists("\WpeCommon")) { |
| 150 |
return false; |
| 151 |
} |
| 152 |
if ($purge_type == self::PURGE_ALL) { |
| 153 |
\WpeCommon::purge_memcached(); |
| 154 |
\WpeCommon::purge_varnish_cache(); |
| 155 |
} elseif ($purge_type == self::PURGE_ID) { |
| 156 |
\WpeCommon::purge_varnish_cache($url_id); |
| 157 |
} |
| 158 |
return true; |
| 159 |
} |
| 160 |
|
| 161 |
//godaddy |
| 162 |
private static function tpv_wpass($purge_type, $url_id) |
| 163 |
{ |
| 164 |
if (!class_exists('WPaaS\Plugin')) { |
| 165 |
return false; |
| 166 |
} |
| 167 |
if ($purge_type == self::PURGE_ALL) { |
| 168 |
if (method_exists('WPass\Plugin', 'vip')) { |
| 169 |
//godaddy |
| 170 |
$method = 'BAN'; |
| 171 |
$url = home_url(); |
| 172 |
$host = wpraiser_get_domain(); |
| 173 |
$url = set_url_scheme(str_replace($host, WPaas\Plugin::vip(), $url), 'http'); |
| 174 |
update_option('gd_system_last_cache_flush', time(), 'no'); # purge apc |
| 175 |
wp_remote_request(esc_url_raw($url), array('method' => $method, 'blocking' => false, 'headers' => array('Host' => $host))); |
| 176 |
} |
| 177 |
return true; |
| 178 |
} elseif ($purge_type == self::PURGE_ID) { |
| 179 |
} |
| 180 |
} |
| 181 |
|
| 182 |
//https://github.com/cloudflare/Cloudflare-WordPress/blob/74acc2df4938df1fcfa4347e971d73e2fc6e4edc/src/WordPress/Hooks.php |
| 183 |
private static function tpv_cloudflare($purge_type, $url_id) |
| 184 |
{ |
| 185 |
if (!class_exists('\CF\WordPress\Hooks')) { |
| 186 |
return false; |
| 187 |
} |
| 188 |
if ($purge_type == self::PURGE_ALL) { |
| 189 |
$cfHooks = new \CF\WordPress\Hooks(); |
| 190 |
$cfHooks->purgeCacheEverything(); |
| 191 |
} elseif ($purge_type == self::PURGE_ID) { |
| 192 |
$cfHooks = new \CF\WordPress\Hooks(); |
| 193 |
$cfHooks->purgeCacheByRelevantURLs($url_id); |
| 194 |
} |
| 195 |
return true; |
| 196 |
} |
| 197 |
|
| 198 |
private static function tpv_kinsta($purge_type, $url_id) |
| 199 |
{ |
| 200 |
if (!defined("KINSTAMU_VERSION")) { |
| 201 |
return false; |
| 202 |
} |
| 203 |
if ($purge_type == self::PURGE_ALL) { |
| 204 |
$response = wp_remote_get('https://localhost/kinsta-clear-cache-all', [ |
| 205 |
'sslverify' => false, |
| 206 |
'timeout' => 5 |
| 207 |
]); |
| 208 |
} elseif ($purge_type == self::PURGE_URL) { |
| 209 |
$path = parse_url($url_id, PHP_URL_PATH); |
| 210 |
if (empty($path)) { |
| 211 |
$path = '/'; |
| 212 |
} |
| 213 |
$response = wp_remote_get(home_url() . "/kinsta-clear-cache/$path", [ |
| 214 |
'sslverify' => false, |
| 215 |
'timeout' => 5 |
| 216 |
]); |
| 217 |
} |
| 218 |
return true; |
| 219 |
} |
| 220 |
|
| 221 |
//SiteGround - https://wordpress.org/plugins/sg-cachepress/ |
| 222 |
private static function tpv_siteground_cachepress($purge_type, $url_id) |
| 223 |
{ |
| 224 |
if (!function_exists('sg_cachepress_purge_cache')) { |
| 225 |
return false; |
| 226 |
} |
| 227 |
if ($purge_type == self::PURGE_ALL) { |
| 228 |
sg_cachepress_purge_cache(); |
| 229 |
} elseif ($purge_type == self::PURGE_URL) { |
| 230 |
sg_cachepress_purge_cache($url_id); |
| 231 |
} |
| 232 |
return true; |
| 233 |
} |
| 234 |
|
| 235 |
private static function tpv_siteground($purge_type, $url_id) |
| 236 |
{ |
| 237 |
$wp_config_path = RL21UtilWP::get_wp_config(); |
| 238 |
if (empty($wp_config_path) || strpos(file_get_contents($wp_config_path), 'Added by SiteGround') === false) { |
| 239 |
return false; |
| 240 |
} |
| 241 |
if ($purge_type == self::PURGE_ALL) { |
| 242 |
$url_id = home_url() . '/(.*)'; |
| 243 |
} elseif ($purge_type == self::PURGE_URL) { |
| 244 |
//$url_id is already set |
| 245 |
} else if ($purge_type == self::PURGE_ID) { |
| 246 |
return false; |
| 247 |
} |
| 248 |
$url_id = preg_replace("/^www\./", "", $url_id); |
| 249 |
$url_id = str_ireplace("https://", "http://", $url_id); |
| 250 |
|
| 251 |
return self::purge_varnish($url_id, "PURGE"); |
| 252 |
} |
| 253 |
|
| 254 |
/** |
| 255 |
* Purge Varnish cache: |
| 256 |
* CloudWays: HTTP_X_VARNISH would be set if Varnish is on |
| 257 |
*/ |
| 258 |
private static function tpv_varnish($purge_type, $url_id) |
| 259 |
{ |
| 260 |
$is_cloudways = isset($_SERVER['cw_allowed_ip']); |
| 261 |
$uses_varnish = $is_cloudways && isset($_SERVER['HTTP_X_VARNISH']); |
| 262 |
if (!$uses_varnish && !empty(RabbitLoader_21_TP::$rl_wp_options['rl_varnish'])) { |
| 263 |
$uses_varnish = intval(RabbitLoader_21_TP::$rl_wp_options['rl_varnish']) === 1; |
| 264 |
} |
| 265 |
if (!$uses_varnish) { |
| 266 |
return false; |
| 267 |
} |
| 268 |
$method = 'PURGE'; |
| 269 |
if ($purge_type == self::PURGE_ALL) { |
| 270 |
$url_id = home_url() . '/.*'; |
| 271 |
} elseif ($purge_type == self::PURGE_URL) { |
| 272 |
//$url_id is already set for single URL |
| 273 |
if ($is_cloudways) { |
| 274 |
$method = 'URLPURGE'; |
| 275 |
} |
| 276 |
} else if ($purge_type == self::PURGE_ID) { |
| 277 |
return false; |
| 278 |
} |
| 279 |
return self::purge_varnish($url_id, $method); |
| 280 |
} |
| 281 |
|
| 282 |
private static function tpv_wp_rocket_residue($purge_type, $url_id) |
| 283 |
{ |
| 284 |
if (!defined('WP_ROCKET_CACHE_PATH')) { |
| 285 |
return; |
| 286 |
} |
| 287 |
#https://docs.wp-rocket.me/article/133-manually-clear-wp-rocket-cache |
| 288 |
$wp_rocket_cache_path = WP_ROCKET_CACHE_PATH; #/wp-content/cache/wp-rocket/ |
| 289 |
$host = parse_url(get_site_url(), PHP_URL_HOST); |
| 290 |
$wp_rocket_cache_dir = $wp_rocket_cache_path . $host; |
| 291 |
if (!file_exists($wp_rocket_cache_dir)) { |
| 292 |
$wp_rocket_cache_dir = $wp_rocket_cache_path . "www." . $host; |
| 293 |
} |
| 294 |
if (file_exists($wp_rocket_cache_dir)) { |
| 295 |
$special_files = ['.htaccess', '.', '..', '']; |
| 296 |
$files = glob($wp_rocket_cache_dir . '/*'); |
| 297 |
foreach ($files as $file) { |
| 298 |
if (empty($file) || in_array(basename($file), $special_files)) { |
| 299 |
continue; |
| 300 |
} |
| 301 |
if (is_file($file)) { |
| 302 |
@unlink($file); // delete file |
| 303 |
} elseif (is_dir($file)) { //category, tags etc |
| 304 |
$cat_files = glob($file . DIRECTORY_SEPARATOR . '/*'); |
| 305 |
foreach ($cat_files as $file) { |
| 306 |
@unlink($file); // delete file |
| 307 |
} |
| 308 |
} |
| 309 |
} |
| 310 |
} else { |
| 311 |
$files = glob($wp_rocket_cache_path . '*'); |
| 312 |
if (!empty($files) && count($files) == 1 && stripos($files[0], 'cache/wp-rocket/index.html') !== false) { |
| 313 |
//no dir exists |
| 314 |
} else { |
| 315 |
RabbitLoader_21_Core::on_exception('WP_ROCKET_CACHE_PATH is true but dir not found ' . $wp_rocket_cache_dir . ' files ' . print_r($files, true), 1); |
| 316 |
} |
| 317 |
} |
| 318 |
} |
| 319 |
|
| 320 |
/** |
| 321 |
* Doc - Hostgator and https://github.com/bluehost/endurance-page-cache/blob/master/endurance-page-cache.php |
| 322 |
*/ |
| 323 |
private static function tpv_endurance($purge_type, $url_id) |
| 324 |
{ |
| 325 |
if (!class_exists('Endurance_Page_Cache')) { |
| 326 |
return; |
| 327 |
} |
| 328 |
if ($purge_type == self::PURGE_ALL) { |
| 329 |
do_action('epc_purge'); |
| 330 |
} elseif ($purge_type == self::PURGE_URL) { |
| 331 |
do_action('epc_purge_request', $url_id); |
| 332 |
} |
| 333 |
} |
| 334 |
|
| 335 |
private static function tpv_sample($purge_type, $url_id) |
| 336 |
{ |
| 337 |
if ($purge_type == self::PURGE_ALL) { |
| 338 |
} elseif ($purge_type == self::PURGE_ID) { |
| 339 |
} elseif ($purge_type == self::PURGE_URL) { |
| 340 |
} |
| 341 |
} |
| 342 |
|
| 343 |
/** |
| 344 |
* @param string url_pattern - Single URL or a URL with wildcard |
| 345 |
* @param string method - PURGE, URLPURGE |
| 346 |
*/ |
| 347 |
private static function purge_varnish($url_pattern, $method) |
| 348 |
{ |
| 349 |
$url_parts = parse_url($url_pattern); |
| 350 |
$port = (empty($url_parts['scheme']) || $url_parts['scheme'] == 'https') ? '443' : '80'; |
| 351 |
$ch = curl_init($url_pattern); |
| 352 |
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $method); |
| 353 |
curl_setopt($ch, CURLOPT_RESOLVE, array($url_parts['host'] . ":$port:127.0.0.1")); |
| 354 |
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); |
| 355 |
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 2); |
| 356 |
curl_setopt($ch, CURLOPT_TIMEOUT, 2); |
| 357 |
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0); |
| 358 |
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0); |
| 359 |
|
| 360 |
curl_exec($ch); |
| 361 |
$curl_error = curl_error($ch); |
| 362 |
$httpcode = intval(curl_getinfo($ch, CURLINFO_HTTP_CODE)); |
| 363 |
curl_close($ch); |
| 364 |
/* |
| 365 |
if($curl_error){ |
| 366 |
RabbitLoader_21_Core::on_exception("Varnish curl error $curl_error for URL $url_id"); |
| 367 |
}else if(!in_array($httpcode,[200, 301, 302, 403, 404])){ |
| 368 |
RabbitLoader_21_Core::on_exception("Varnish unexpected response $httpcode URL $url_id"); |
| 369 |
}*/ |
| 370 |
return $httpcode; |
| 371 |
} |
| 372 |
} |
| 373 |
|