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