| 1 |
<?php |
| 2 |
|
| 3 |
spl_autoload_register(function ($class) { |
| 4 |
$file = str_replace("\\", DIRECTORY_SEPARATOR, $class) . ".php"; |
| 5 |
$path = dirname(__DIR__) . DIRECTORY_SEPARATOR . $file; |
| 6 |
if (file_exists($path)) { |
| 7 |
require_once $path; |
| 8 |
} |
| 9 |
}); |
| 10 |
class RabbitLoader_21_Core |
| 11 |
{ |
| 12 |
|
| 13 |
private static $rl_wp_options = []; |
| 14 |
private static $user_options = []; |
| 15 |
|
| 16 |
/** |
| 17 |
* max time a cache can live |
| 18 |
*/ |
| 19 |
const ORPHANED_LONG_AGE_SEC = 30 * 24 * 3600; |
| 20 |
const LOCAL_CONFIG_FILE = "rl_config"; |
| 21 |
|
| 22 |
private static function addKeys(&$args, &$rabbitloader_field_domain) |
| 23 |
{ |
| 24 |
if (empty($args)) { |
| 25 |
$args = []; |
| 26 |
} |
| 27 |
|
| 28 |
if (empty($args['headers'])) { |
| 29 |
$args['headers'] = []; |
| 30 |
} |
| 31 |
|
| 32 |
if (RabbitLoader_21_Util_Core::isDev()) { |
| 33 |
$args['sslverify'] = false; |
| 34 |
} |
| 35 |
$args['timeout'] = 30; |
| 36 |
|
| 37 |
$api_token = RabbitLoader_21_Core::getWpOptVal('api_token'); |
| 38 |
if (!empty($api_token)) { |
| 39 |
$args['headers'] += [ |
| 40 |
'AUTHORIZATION' => 'Bearer ' . $api_token |
| 41 |
]; |
| 42 |
$rabbitloader_field_domain = RabbitLoader_21_Core::getWpOptVal('domain'); |
| 43 |
} else { |
| 44 |
return false; |
| 45 |
} |
| 46 |
|
| 47 |
return true; |
| 48 |
} |
| 49 |
|
| 50 |
public static function update_api_tokens($api_token, $domain, $did, $comments) |
| 51 |
{ |
| 52 |
RabbitLoader_21_Core::getWpOption($rl_wp_options); |
| 53 |
$rl_wp_options['api_token'] = $api_token; |
| 54 |
$rl_wp_options['domain'] = $domain; |
| 55 |
$rl_wp_options['did'] = $did; |
| 56 |
$rl_wp_options['comments'] = $comments; |
| 57 |
$rl_wp_options['token_update_ts'] = time(); |
| 58 |
RabbitLoader_21_Core::updateWpOption($rl_wp_options); |
| 59 |
} |
| 60 |
|
| 61 |
public static function getRLBaseDomain() |
| 62 |
{ |
| 63 |
return RabbitLoader_21_Util_Core::isDev() ? 'https://rabbitloader.local/' : 'https://rabbitloader.com/'; |
| 64 |
} |
| 65 |
|
| 66 |
public static function getRLDomainV1() |
| 67 |
{ |
| 68 |
return RabbitLoader_21_Util_Core::isDev() ? 'https://api-v1.rabbitloader.local/' : 'https://api-v1.rabbitloader.com/'; |
| 69 |
} |
| 70 |
|
| 71 |
public static function getRLDomainV2() |
| 72 |
{ |
| 73 |
return RabbitLoader_21_Util_Core::isDev() ? 'https://api-v2.rabbitloader.local/' : 'https://api-v2.rabbitloader.com/'; |
| 74 |
} |
| 75 |
|
| 76 |
private static function isTemporaryError($apiMessage) |
| 77 |
{ |
| 78 |
$temp_errors = ['timed out', 'Could not resolve host', 'error setting certificate', 'Connection reset', 'OpenSSL', 'getaddrinfo() thread', 'SSL connection timeout', 'Unknown SSL', 'SSL_ERROR_SYSCALL', 'Failed connect to', 'cURL error 77']; |
| 79 |
$found = false; |
| 80 |
foreach ($temp_errors as $msg) { |
| 81 |
if (stripos($apiMessage, $msg) !== false) { |
| 82 |
$found = true; |
| 83 |
break; |
| 84 |
} |
| 85 |
} |
| 86 |
return $found; |
| 87 |
} |
| 88 |
|
| 89 |
public static function &callGETAPI($endpoint, &$apiError, &$apiMessage) |
| 90 |
{ |
| 91 |
$http = []; |
| 92 |
$args = []; |
| 93 |
$apiError = true; |
| 94 |
if (!RabbitLoader_21_Core::addKeys($args, $rabbitloader_field_domain)) { |
| 95 |
$apiError = 'Keys could not be added'; |
| 96 |
return $http; |
| 97 |
} |
| 98 |
$url = RabbitLoader_21_Core::getRLDomainV1() . 'api/v1/'; |
| 99 |
if (strpos($endpoint, '?')) { |
| 100 |
$endpoint .= '&'; |
| 101 |
} else { |
| 102 |
$endpoint .= '?'; |
| 103 |
} |
| 104 |
|
| 105 |
$endpoint .= 'domain=' . $rabbitloader_field_domain . '&plugin_cms=wp&plugin_v=' . RABBITLOADER_PLUG_VERSION . '&cms_v=' . get_bloginfo('version'); |
| 106 |
|
| 107 |
$args['method'] = 'GET'; |
| 108 |
|
| 109 |
try { |
| 110 |
$http = wp_remote_get($url . $endpoint, $args); |
| 111 |
|
| 112 |
if (is_wp_error($http)) { |
| 113 |
$apiError = true; |
| 114 |
$apiMessage = $http->get_error_message(); |
| 115 |
if (empty($apiMessage)) { |
| 116 |
$apiMessage = ''; |
| 117 |
} |
| 118 |
if (self::isTemporaryError($apiMessage)) { |
| 119 |
//chill, it happens |
| 120 |
} else { |
| 121 |
RabbitLoader_21_Core::on_exception($http); |
| 122 |
} |
| 123 |
$http = []; |
| 124 |
} |
| 125 |
|
| 126 |
if (!empty($http['response']['code']) && in_array($http['response']['code'], [200, 401])) { |
| 127 |
$http['body'] = json_decode($http['body'], true); |
| 128 |
if (!empty($http['body']['message'])) { |
| 129 |
$message = $http['body']['message']; |
| 130 |
if (!strcmp($message, 'AUTH_REQUIRED') || !strcmp($message, 'INVALID_DOMAIN')) { |
| 131 |
RabbitLoader_21_Core::update_api_tokens('', '', '', "$message when $endpoint was called"); |
| 132 |
} |
| 133 |
} |
| 134 |
$apiError = empty($http['body']['result']); |
| 135 |
$apiMessage = empty($http['body']['message']) ? '' : $http['body']['message']; |
| 136 |
} |
| 137 |
} catch (Throwable $e) { |
| 138 |
RabbitLoader_21_Core::on_exception($e); |
| 139 |
$apiError = true; |
| 140 |
$apiMessage = $e->getMessage(); |
| 141 |
} |
| 142 |
return $http; |
| 143 |
} |
| 144 |
|
| 145 |
public static function &callGETAPIV2($endpoint, &$apiError, &$apiMessage) |
| 146 |
{ |
| 147 |
$http = []; |
| 148 |
$args = []; |
| 149 |
$apiError = true; |
| 150 |
if (!RabbitLoader_21_Core::addKeys($args, $rabbitloader_field_domain)) { |
| 151 |
$apiError = 'Keys could not be added'; |
| 152 |
return $http; |
| 153 |
} |
| 154 |
$url = RabbitLoader_21_Core::getRLDomainV2(); |
| 155 |
$args['method'] = 'GET'; |
| 156 |
|
| 157 |
try { |
| 158 |
if (stripos($endpoint, '{domain_id}')) { |
| 159 |
$did = RabbitLoader_21_Core::getWpOptVal('did'); |
| 160 |
if (empty($did)) { |
| 161 |
$apiError = 'Please disconnect the plugin and connect again.'; |
| 162 |
return $http; |
| 163 |
} |
| 164 |
$endpoint = str_ireplace('{domain_id}', $did, $endpoint); |
| 165 |
} |
| 166 |
$http = wp_remote_get($url . $endpoint, $args); |
| 167 |
$code = wp_remote_retrieve_response_code($http); |
| 168 |
if (is_wp_error($http)) { |
| 169 |
$apiError = true; |
| 170 |
$apiMessage = $http->get_error_message(); |
| 171 |
if (empty($apiMessage)) { |
| 172 |
$apiMessage = ''; |
| 173 |
} |
| 174 |
if (self::isTemporaryError($apiMessage)) { |
| 175 |
//chill, it happens |
| 176 |
} else { |
| 177 |
RabbitLoader_21_Core::on_exception($http); |
| 178 |
} |
| 179 |
} |
| 180 |
|
| 181 |
if (in_array($code, [401, 403])) { |
| 182 |
$apiError = true; |
| 183 |
$apiMessage = "Unauthorized access. Please disconnect and Login again."; |
| 184 |
RabbitLoader_21_Core::update_api_tokens('', '', '', "$code when $endpoint was called"); |
| 185 |
} |
| 186 |
} catch (Throwable $e) { |
| 187 |
RabbitLoader_21_Core::on_exception($e); |
| 188 |
$apiError = true; |
| 189 |
$apiMessage = $e->getMessage(); |
| 190 |
} |
| 191 |
$http['body'] = json_decode(wp_remote_retrieve_body($http), true); |
| 192 |
return $http; |
| 193 |
} |
| 194 |
|
| 195 |
public static function &callPostApi($endpoint, $body, &$apiError, &$apiMessage) |
| 196 |
{ |
| 197 |
$http = []; |
| 198 |
$args = []; |
| 199 |
$apiError = true; |
| 200 |
|
| 201 |
if (!RabbitLoader_21_Core::addKeys($args, $rabbitloader_field_domain)) { |
| 202 |
$apiError = 'Keys could not be added'; |
| 203 |
return $http; |
| 204 |
} |
| 205 |
$url = RabbitLoader_21_Core::getRLDomainV1() . 'api/v1/'; |
| 206 |
|
| 207 |
$body['domain'] = $rabbitloader_field_domain; |
| 208 |
$body['plugin_cms'] = 'wp'; |
| 209 |
$body['plugin_v'] = RABBITLOADER_PLUG_VERSION; |
| 210 |
$body['cms_v'] = get_bloginfo('version'); |
| 211 |
|
| 212 |
$args['method'] = 'POST'; |
| 213 |
$args['body'] = $body; |
| 214 |
|
| 215 |
try { |
| 216 |
$http = wp_remote_post($url . $endpoint, $args); |
| 217 |
|
| 218 |
if (is_wp_error($http)) { |
| 219 |
$apiError = true; |
| 220 |
$apiMessage = $http->get_error_message(); |
| 221 |
if (empty($apiMessage)) { |
| 222 |
$apiMessage = ''; |
| 223 |
} |
| 224 |
if (self::isTemporaryError($apiMessage)) { |
| 225 |
//chill, it happens |
| 226 |
} else { |
| 227 |
RabbitLoader_21_Core::on_exception($http->get_error_message() . $url . $endpoint); |
| 228 |
} |
| 229 |
$http = []; |
| 230 |
} |
| 231 |
|
| 232 |
if (!empty($http['response']['code']) && in_array($http['response']['code'], [200, 401])) { |
| 233 |
$http['body'] = json_decode($http['body'], true); |
| 234 |
if (!empty($http['body']['message'])) { |
| 235 |
$message = $http['body']['message']; |
| 236 |
if (!strcmp($message, 'AUTH_REQUIRED') || !strcmp($message, 'INVALID_DOMAIN')) { |
| 237 |
RabbitLoader_21_Core::update_api_tokens('', '', '', "$message when $endpoint was called"); |
| 238 |
} |
| 239 |
} |
| 240 |
$apiError = empty($http['body']['result']); |
| 241 |
$apiMessage = empty($http['body']['message']) ? '' : $http['body']['message']; |
| 242 |
} |
| 243 |
} catch (Throwable $e) { |
| 244 |
RabbitLoader_21_Core::on_exception($e); |
| 245 |
$apiError = true; |
| 246 |
$apiMessage = $e->getMessage(); |
| 247 |
} |
| 248 |
return $http; |
| 249 |
} |
| 250 |
|
| 251 |
private static function log_debug($message, $context = []) |
| 252 |
{ |
| 253 |
if (!defined('WP_DEBUG') || !WP_DEBUG) { |
| 254 |
return; |
| 255 |
} |
| 256 |
|
| 257 |
try { |
| 258 |
RabbitLoader_21_Core::get_log_file('rl_debug', $debug_log); |
| 259 |
$line = date('c') . ' ' . $message; |
| 260 |
if (!empty($context)) { |
| 261 |
$line .= ' ' . json_encode($context, JSON_INVALID_UTF8_IGNORE); |
| 262 |
} |
| 263 |
$line .= PHP_EOL; |
| 264 |
$cache_dir = RL21UtilWP::get_cache_dir(''); |
| 265 |
if (!file_exists($cache_dir)) { |
| 266 |
@mkdir($cache_dir, 0755, true); |
| 267 |
} |
| 268 |
file_put_contents($debug_log, $line, FILE_APPEND | LOCK_EX); |
| 269 |
} catch (Throwable $e) { |
| 270 |
RabbitLoader_21_Core::on_exception($e); |
| 271 |
} |
| 272 |
} |
| 273 |
|
| 274 |
public static function notify_backend_disconnect($source, &$apiError, &$apiMessage) |
| 275 |
{ |
| 276 |
$http = []; |
| 277 |
$args = []; |
| 278 |
$apiError = true; |
| 279 |
$apiMessage = ''; |
| 280 |
|
| 281 |
if (!RabbitLoader_21_Core::addKeys($args, $rabbitloader_field_domain)) { |
| 282 |
$apiMessage = 'RabbitLoader connection token is missing. Please reconnect the plugin and try again.'; |
| 283 |
self::log_debug('backend disconnect skipped', [ |
| 284 |
'source' => $source, |
| 285 |
'reason' => 'missing token', |
| 286 |
]); |
| 287 |
return false; |
| 288 |
} |
| 289 |
|
| 290 |
$endpoint = 'domain/heartbeat'; |
| 291 |
$url = RabbitLoader_21_Core::getRLDomainV1() . 'api/v1/' . $endpoint; |
| 292 |
$body = [ |
| 293 |
'uninstall' => 1, |
| 294 |
'domain' => $rabbitloader_field_domain, |
| 295 |
'plugin_cms' => 'wp', |
| 296 |
'plugin_v' => RABBITLOADER_PLUG_VERSION, |
| 297 |
'cms_v' => get_bloginfo('version'), |
| 298 |
]; |
| 299 |
|
| 300 |
$args['method'] = 'POST'; |
| 301 |
$args['body'] = $body; |
| 302 |
|
| 303 |
self::log_debug('backend disconnect request', [ |
| 304 |
'source' => $source, |
| 305 |
'endpoint' => $endpoint, |
| 306 |
'body' => $body, |
| 307 |
]); |
| 308 |
|
| 309 |
try { |
| 310 |
$http = wp_remote_post($url, $args); |
| 311 |
|
| 312 |
if (is_wp_error($http)) { |
| 313 |
$apiMessage = $http->get_error_message(); |
| 314 |
if (empty($apiMessage)) { |
| 315 |
$apiMessage = 'Network error while disconnecting RabbitLoader.'; |
| 316 |
} |
| 317 |
self::log_debug('backend disconnect wp_error', [ |
| 318 |
'source' => $source, |
| 319 |
'message' => $apiMessage, |
| 320 |
]); |
| 321 |
|
| 322 |
if (!self::isTemporaryError($apiMessage)) { |
| 323 |
RabbitLoader_21_Core::on_exception($apiMessage . $url); |
| 324 |
} |
| 325 |
return false; |
| 326 |
} |
| 327 |
|
| 328 |
if (!is_array($http)) { |
| 329 |
$apiMessage = 'Unexpected response while disconnecting RabbitLoader.'; |
| 330 |
self::log_debug('backend disconnect unexpected response', [ |
| 331 |
'source' => $source, |
| 332 |
'response_type' => gettype($http), |
| 333 |
]); |
| 334 |
return false; |
| 335 |
} |
| 336 |
|
| 337 |
$code = intval(wp_remote_retrieve_response_code($http)); |
| 338 |
$raw_body = wp_remote_retrieve_body($http); |
| 339 |
$parsed_body = json_decode($raw_body, true); |
| 340 |
|
| 341 |
self::log_debug('backend disconnect response', [ |
| 342 |
'source' => $source, |
| 343 |
'code' => $code, |
| 344 |
'body' => $parsed_body === null ? $raw_body : $parsed_body, |
| 345 |
]); |
| 346 |
|
| 347 |
if (is_array($parsed_body) && isset($parsed_body['message']) && $parsed_body['message'] !== '') { |
| 348 |
$apiMessage = $parsed_body['message']; |
| 349 |
} |
| 350 |
|
| 351 |
if ($code >= 200 && $code < 300 && is_array($parsed_body) && !empty($parsed_body['result'])) { |
| 352 |
$apiError = false; |
| 353 |
return true; |
| 354 |
} |
| 355 |
|
| 356 |
if (empty($apiMessage)) { |
| 357 |
if (in_array($code, [401, 403])) { |
| 358 |
$apiMessage = 'RabbitLoader could not authorize this disconnect request. Please try again or reconnect the plugin.'; |
| 359 |
} else if ($code >= 500 || $code === 0) { |
| 360 |
$apiMessage = 'RabbitLoader backend is temporarily unavailable. Please try disconnecting again.'; |
| 361 |
} else { |
| 362 |
$apiMessage = 'RabbitLoader backend did not confirm the disconnect. Please try again.'; |
| 363 |
} |
| 364 |
} |
| 365 |
} catch (Throwable $e) { |
| 366 |
RabbitLoader_21_Core::on_exception($e); |
| 367 |
$apiMessage = $e->getMessage(); |
| 368 |
} |
| 369 |
|
| 370 |
return false; |
| 371 |
} |
| 372 |
|
| 373 |
public static function getWpUserOption(&$user_options) |
| 374 |
{ |
| 375 |
if (!empty(self::$user_options)) { |
| 376 |
$user_options = self::$user_options; |
| 377 |
return; |
| 378 |
} |
| 379 |
if (function_exists('get_option')) { |
| 380 |
$user_options = get_option('rabbit_loader_user_options'); |
| 381 |
} else { |
| 382 |
RabbitLoader_21_Core::get_log_file('rl_user_options', $rl_user_options); |
| 383 |
if (file_exists($rl_user_options)) { |
| 384 |
$user_options = json_decode(file_get_contents($rl_user_options), true); |
| 385 |
} |
| 386 |
} |
| 387 |
if (empty($user_options) || !is_array($user_options)) { |
| 388 |
$user_options = []; |
| 389 |
} |
| 390 |
$default_values = [ |
| 391 |
'purge_on_change' => false, |
| 392 |
'exclude_patterns' => '', |
| 393 |
'ignore_params' => '', |
| 394 |
'private_mode_val' => false, |
| 395 |
]; |
| 396 |
foreach ($default_values as $k => $v) { |
| 397 |
if (!isset($user_options[$k])) { |
| 398 |
$user_options[$k] = $v; |
| 399 |
} |
| 400 |
} |
| 401 |
self::$user_options = $user_options; |
| 402 |
} |
| 403 |
public static function updateUserOption(&$user_options) |
| 404 |
{ |
| 405 |
self::$user_options = $user_options; |
| 406 |
update_option('rabbit_loader_user_options', $user_options, true); |
| 407 |
try { |
| 408 |
RabbitLoader_21_Core::get_log_file('rl_user_options', $rl_user_options); |
| 409 |
$rl_json = json_encode($user_options, JSON_INVALID_UTF8_IGNORE); |
| 410 |
RabbitLoader_21_Util_Core::fpc($rl_user_options, $rl_json, WP_DEBUG); |
| 411 |
} catch (\Throwable $e) { |
| 412 |
RabbitLoader_21_Core::on_exception($e); |
| 413 |
} |
| 414 |
} |
| 415 |
|
| 416 |
|
| 417 |
public static function getWpOption(&$rl_wp_options) |
| 418 |
{ |
| 419 |
if (!empty(self::$rl_wp_options)) { |
| 420 |
$rl_wp_options = self::$rl_wp_options; |
| 421 |
return; |
| 422 |
} |
| 423 |
if (function_exists('get_option')) { |
| 424 |
$rl_wp_options = get_option('rabbit_loader_wp_options'); |
| 425 |
} else { |
| 426 |
RabbitLoader_21_Core::get_log_file(self::LOCAL_CONFIG_FILE, $rl_config); |
| 427 |
if (file_exists($rl_config)) { |
| 428 |
$rl_wp_options = json_decode(file_get_contents($rl_config), true); |
| 429 |
} |
| 430 |
} |
| 431 |
if (empty($rl_wp_options)) { |
| 432 |
$rl_wp_options = []; |
| 433 |
} |
| 434 |
self::$rl_wp_options = $rl_wp_options; |
| 435 |
} |
| 436 |
/** |
| 437 |
* Get value of single config option |
| 438 |
*/ |
| 439 |
public static function getWpOptVal($key) |
| 440 |
{ |
| 441 |
RabbitLoader_21_Core::getWpOption($rl_wp_options); |
| 442 |
return isset($rl_wp_options[$key]) ? $rl_wp_options[$key] : ''; |
| 443 |
} |
| 444 |
|
| 445 |
public static function updateWpOption(&$rl_wp_options) |
| 446 |
{ |
| 447 |
self::$rl_wp_options = $rl_wp_options; |
| 448 |
try { |
| 449 |
update_option('rabbit_loader_wp_options', $rl_wp_options, true); |
| 450 |
} catch (\Throwable $e) { |
| 451 |
RabbitLoader_21_Core::on_exception($e); |
| 452 |
} |
| 453 |
try { |
| 454 |
RabbitLoader_21_Core::get_log_file(self::LOCAL_CONFIG_FILE, $rl_config); |
| 455 |
$rl_json = json_encode($rl_wp_options, JSON_INVALID_UTF8_IGNORE); |
| 456 |
RabbitLoader_21_Util_Core::fpc($rl_config, $rl_json, WP_DEBUG); |
| 457 |
} catch (\Throwable $e) { |
| 458 |
RabbitLoader_21_Core::on_exception($e); |
| 459 |
} |
| 460 |
} |
| 461 |
|
| 462 |
public static function purge_all(&$purge_count, $purge_source, &$tp_purge_count) |
| 463 |
{ |
| 464 |
try { |
| 465 |
//RL purges |
| 466 |
$purge_count = 0; |
| 467 |
$purge_count += RabbitLoader_21_Core::cleanAllCachedFiles(); |
| 468 |
|
| 469 |
//other common platforms purges |
| 470 |
RabbitLoader_21_TP::purge_all($tp_purge_count); |
| 471 |
} catch (Throwable $e) { |
| 472 |
RabbitLoader_21_Core::on_exception($e); |
| 473 |
} |
| 474 |
} |
| 475 |
|
| 476 |
/** |
| 477 |
* @param string $fn file name |
| 478 |
* @param string $fp file path |
| 479 |
*/ |
| 480 |
public static function get_log_file($fn, &$fp) |
| 481 |
{ |
| 482 |
$fp = RL21UtilWP::get_cache_dir() . DIRECTORY_SEPARATOR . $fn . ".log"; |
| 483 |
} |
| 484 |
|
| 485 |
public static function delete_log_file($fn) |
| 486 |
{ |
| 487 |
$fp = ''; |
| 488 |
self::get_log_file($fn, $fp); |
| 489 |
if ($fp && file_exists($fp)) { |
| 490 |
@unlink($fp); |
| 491 |
} |
| 492 |
} |
| 493 |
|
| 494 |
public static function on_exception($exception, $limit = 8) |
| 495 |
{ |
| 496 |
try { |
| 497 |
$rlSDK = self::getSDK(); |
| 498 |
$rlSDK->excCatch($exception, ['src' => 'wp'], $limit); |
| 499 |
} catch (Throwable $e) { |
| 500 |
if (WP_DEBUG) { |
| 501 |
echo $e->getMessage(); |
| 502 |
} |
| 503 |
} |
| 504 |
} |
| 505 |
|
| 506 |
public static function sendJsonResponse(&$response) |
| 507 |
{ |
| 508 |
header("Content-Type: application/json"); |
| 509 |
header("Cache-Control: no-store, no-cache, must-revalidate, max-age=0, s-max-age=0"); |
| 510 |
header("Cache-Control: post-check=0, pre-check=0", false); |
| 511 |
header("Pragma: no-cache"); |
| 512 |
$encoded_str = json_encode($response, JSON_INVALID_UTF8_IGNORE); |
| 513 |
if ($encoded_str === false) { |
| 514 |
echo '{"time":"1", "failed":"1"}'; |
| 515 |
} else { |
| 516 |
echo $encoded_str; |
| 517 |
} |
| 518 |
exit; |
| 519 |
} |
| 520 |
|
| 521 |
public static function get_common_cache_urls(&$urls_to_purge) |
| 522 |
{ |
| 523 |
if (empty($urls_to_purge)) { |
| 524 |
$urls_to_purge = []; |
| 525 |
} |
| 526 |
|
| 527 |
$urls_to_purge[] = get_home_url(); //always purge home page if any other page is modified |
| 528 |
$urls_to_purge[] = get_home_url() . "/"; //always purge home page if any other page is modified |
| 529 |
$urls_to_purge[] = home_url('/'); //always purge home page if any other page is modified |
| 530 |
$urls_to_purge[] = site_url('/'); //always purge home page if any other page is modified |
| 531 |
|
| 532 |
//clean pagination urls |
| 533 |
try { |
| 534 |
if (!empty(get_option('page_for_posts'))) { |
| 535 |
$page_for_posts = get_permalink(get_option('page_for_posts')); |
| 536 |
if (is_string($page_for_posts) && !empty($page_for_posts) && get_option('show_on_front') == 'page') { |
| 537 |
$urls_to_purge[] = $page_for_posts; |
| 538 |
} |
| 539 |
} |
| 540 |
|
| 541 |
$posts_per_page = get_option('posts_per_page'); |
| 542 |
$published_posts = RabbitLoader_21_Core::get_published_count(); |
| 543 |
$page_number_max = min(3, ceil($published_posts / $posts_per_page)); |
| 544 |
for ($pn = 1; $pn < $page_number_max; $pn++) { |
| 545 |
$urls_to_purge[] = home_url(sprintf('/page/%s/', $pn)); |
| 546 |
} |
| 547 |
} catch (Throwable $e) { |
| 548 |
RabbitLoader_21_Core::on_exception($e); |
| 549 |
} |
| 550 |
} |
| 551 |
|
| 552 |
public static function get_published_count() |
| 553 |
{ |
| 554 |
//$published_count = wp_count_posts()->publish + wp_count_posts('page')->publish; |
| 555 |
$published_count = 0; |
| 556 |
$post_types = get_post_types(['public' => true], 'names', 'and'); |
| 557 |
$ex = ['attachment', 'elementor_library']; |
| 558 |
foreach ($post_types as $post_type) { |
| 559 |
if (in_array($post_type, $ex)) { |
| 560 |
continue; |
| 561 |
} |
| 562 |
$published_count += wp_count_posts($post_type)->publish; |
| 563 |
} |
| 564 |
return $published_count + 1; //1 for home page |
| 565 |
} |
| 566 |
|
| 567 |
public static function get_recent_posts(&$offset, &$posts_per_page, &$published_count, &$permalinks) |
| 568 |
{ |
| 569 |
$permalinks = []; |
| 570 |
$posts_per_page = 250; |
| 571 |
$queued_count = 0; |
| 572 |
|
| 573 |
if ($posts_per_page < 0 || $posts_per_page > 250) { |
| 574 |
$posts_per_page = 250; |
| 575 |
} |
| 576 |
|
| 577 |
//$latest_modified_ts = 0; |
| 578 |
//published posts |
| 579 |
$published_count = RabbitLoader_21_Core::get_published_count(); |
| 580 |
|
| 581 |
$offset = intval($offset); |
| 582 |
if ($offset > $published_count) { |
| 583 |
$offset = 0; |
| 584 |
} |
| 585 |
|
| 586 |
$permalink_structure = get_option('permalink_structure'); |
| 587 |
$append_slash = substr($permalink_structure, -1) == "/" ? true : false; |
| 588 |
$args = array( |
| 589 |
'post_status' => 'publish', |
| 590 |
'post_type' => 'any', |
| 591 |
'orderby' => 'post_date', |
| 592 |
'order' => 'DESC', |
| 593 |
'fields' => 'ids', // Only get post IDs |
| 594 |
'posts_per_page' => $posts_per_page, |
| 595 |
'offset' => intval($offset), |
| 596 |
'no_found_rows' => false |
| 597 |
); |
| 598 |
|
| 599 |
try { |
| 600 |
$posts = get_posts($args); |
| 601 |
if (!empty($posts)) { |
| 602 |
foreach ($posts as $post_id) { |
| 603 |
$the_post = get_post($post_id); |
| 604 |
$permalink = get_permalink($the_post); |
| 605 |
if (empty($permalink)) { |
| 606 |
continue; |
| 607 |
} |
| 608 |
if ($append_slash) { |
| 609 |
$permalink = trailingslashit($permalink); |
| 610 |
} else { |
| 611 |
$permalink = $permalink . $append_slash; |
| 612 |
} |
| 613 |
|
| 614 |
// $modified_ts = strtotime(get_the_modified_date('c', $the_post)); |
| 615 |
// if ($modified_ts > $latest_modified_ts) { |
| 616 |
// $latest_modified_ts = $modified_ts; |
| 617 |
// } |
| 618 |
$permalinks[] = $permalink; |
| 619 |
++$queued_count; |
| 620 |
} |
| 621 |
} else { |
| 622 |
$offset = 0; |
| 623 |
} |
| 624 |
} catch (Throwable $e) { |
| 625 |
$responses['exception'] = true; |
| 626 |
RabbitLoader_21_Core::on_exception($e); |
| 627 |
} |
| 628 |
} |
| 629 |
|
| 630 |
private static function &checkHostingName() |
| 631 |
{ |
| 632 |
$hosting_name = 'NA'; |
| 633 |
if (!empty($_SERVER['cw_allowed_ip'])) { |
| 634 |
$hosting_name = $_SERVER['cw_allowed_ip']; |
| 635 |
} else if (class_exists('WpeCommon') && method_exists('WpeCommon', 'purge_memcached')) { |
| 636 |
$hosting_name = 'wpengine'; |
| 637 |
} else if (defined("KINSTAMU_VERSION")) { |
| 638 |
$hosting_name = 'Kinsta'; |
| 639 |
} else if (RL21UtilWP::is_flywheel()) { |
| 640 |
$hosting_name = 'flywheel'; |
| 641 |
} else if (preg_match("/^dp-.+/", gethostname())) { |
| 642 |
$hosting_name = 'dreamhost'; |
| 643 |
} else if (defined("CLOSTE_APP_ID")) { |
| 644 |
$hosting_name = 'closte'; |
| 645 |
} else if (function_exists('sg_cachepress_purge_cache')) { |
| 646 |
$hosting_name = 'siteground'; |
| 647 |
} else if (class_exists('LiteSpeed_Cache_API') && method_exists('LiteSpeed_Cache_API', 'purge_all')) { |
| 648 |
$hosting_name = 'litespeed'; |
| 649 |
} else if (class_exists('PagelyCachePurge') && method_exists('PagelyCachePurge', 'purgeAll')) { |
| 650 |
$hosting_name = 'pagely'; |
| 651 |
} else if (class_exists('comet_cache') && method_exists('comet_cache', 'clear')) { |
| 652 |
$hosting_name = 'comet'; |
| 653 |
} else if (defined('IS_PRESSABLE')) { |
| 654 |
$hosting_name = 'pressable'; |
| 655 |
} |
| 656 |
return $hosting_name; |
| 657 |
} |
| 658 |
|
| 659 |
public static function &getSDK() |
| 660 |
{ |
| 661 |
global $wp_version; |
| 662 |
if (empty($GLOBALS['rlSDK'])) { |
| 663 |
$rlSDK = new RabbitLoader\SDK\RabbitLoader(RabbitLoader_21_Core::getWpOptVal('api_token'), RL21UtilWP::get_cache_dir()); |
| 664 |
$GLOBALS['rlSDK'] = &$rlSDK; |
| 665 |
$rlSDK->setDebug(WP_DEBUG); |
| 666 |
|
| 667 |
$rlSDK->registerPurgeCallback(function ($url) { |
| 668 |
$tp_purge_count = 0; |
| 669 |
RabbitLoader_21_TP::purge_url($url, $tp_purge_count); |
| 670 |
}); |
| 671 |
|
| 672 |
$platform = [ |
| 673 |
'plugin_cms' => 'wp', |
| 674 |
'plugin_v' => RL21UtilWP::getRLPlugVersion(), |
| 675 |
'cms_v' => function_exists('get_bloginfo') ? get_bloginfo('version') : '' |
| 676 |
]; |
| 677 |
if (empty($platform['cms_v']) && $wp_version) { |
| 678 |
$platform['cms_v'] = $wp_version; |
| 679 |
} |
| 680 |
$rlSDK->setPlatform($platform); |
| 681 |
} |
| 682 |
return $GLOBALS['rlSDK']; |
| 683 |
} |
| 684 |
|
| 685 |
public static function cleanAllCachedFiles() |
| 686 |
{ |
| 687 |
$rlSDK = self::getSDK(); |
| 688 |
$deleted_count = $rlSDK->deleteAll(); |
| 689 |
|
| 690 |
if (function_exists("opcache_reset")) { |
| 691 |
opcache_reset(); |
| 692 |
} |
| 693 |
if (function_exists('delete_transient')) { |
| 694 |
delete_transient('rabbitloader_trans_overview_data'); |
| 695 |
} |
| 696 |
return $deleted_count; |
| 697 |
} |
| 698 |
|
| 699 |
public static function getCacheCount() |
| 700 |
{ |
| 701 |
$rlSDK = self::getSDK(); |
| 702 |
return $rlSDK->getCacheCount(); |
| 703 |
} |
| 704 |
} |
| 705 |
|