PluginProbe
RabbitLoader / 3.1.0
RabbitLoader v3.1.0
4.0.2 4.0.1 4.0 3.2.0 3.1.1 3.1.0 3.0.5 3.0.3 3.0.4 2.17.1 2.17.2 2.17.3 2.17.4 2.17.5 2.17.6 2.17.7 2.18.0 2.18.1 2.18.2 2.18.3 2.18.4 2.18.5 2.18.6 2.18.7 2.18.8 All 129 releases
rabbit-loader / inc / core / core.php

core.php in RabbitLoader 3.1.0, at inc/core/core.php

770 lines 28.0 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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 &callPOSTAPIV2($endpoint, $body, &$apiError, &$apiMessage)
196 {
197 $http = [];
198 $args = [];
199 $apiError = true;
200 if (!RabbitLoader_21_Core::addKeys($args, $rabbitloader_field_domain)) {
201 $apiError = 'Keys could not be added';
202 return $http;
203 }
204 $url = RabbitLoader_21_Core::getRLDomainV2();
205 $args['method'] = 'POST';
206 $args['headers']['Content-Type'] = 'application/json';
207
208 try {
209 if (stripos($endpoint, '{domain_id}')) {
210 $did = RabbitLoader_21_Core::getWpOptVal('did');
211 if (empty($did)) {
212 $apiError = 'Please disconnect the plugin and connect again.';
213 return $http;
214 }
215 $endpoint = str_ireplace('{domain_id}', $did, $endpoint);
216 }
217
218 $args['body'] = wp_json_encode(is_array($body) ? $body : []);
219 $http = wp_remote_post($url . $endpoint, $args);
220 $code = wp_remote_retrieve_response_code($http);
221 if (is_wp_error($http)) {
222 $apiError = true;
223 $apiMessage = $http->get_error_message();
224 if (empty($apiMessage)) {
225 $apiMessage = '';
226 }
227 if (self::isTemporaryError($apiMessage)) {
228 //chill, it happens
229 } else {
230 RabbitLoader_21_Core::on_exception($http);
231 }
232 }
233
234 if (in_array($code, [401, 403])) {
235 $apiError = true;
236 $apiMessage = "Unauthorized access. Please disconnect and Login again.";
237 RabbitLoader_21_Core::update_api_tokens('', '', '', "$code when $endpoint was called");
238 } else {
239 $apiError = $code < 200 || $code >= 300;
240 }
241 } catch (Throwable $e) {
242 RabbitLoader_21_Core::on_exception($e);
243 $apiError = true;
244 $apiMessage = $e->getMessage();
245 }
246 $http['body'] = json_decode(wp_remote_retrieve_body($http), true);
247 if (!$apiError && empty($apiMessage) && is_array($http['body']) && !empty($http['body']['message'])) {
248 $apiMessage = $http['body']['message'];
249 }
250 return $http;
251 }
252
253 public static function &callPostApi($endpoint, $body, &$apiError, &$apiMessage)
254 {
255 $http = [];
256 $args = [];
257 $apiError = true;
258
259 if (!RabbitLoader_21_Core::addKeys($args, $rabbitloader_field_domain)) {
260 $apiError = 'Keys could not be added';
261 return $http;
262 }
263 $url = RabbitLoader_21_Core::getRLDomainV1() . 'api/v1/';
264
265 $body['domain'] = $rabbitloader_field_domain;
266 $body['plugin_cms'] = 'wp';
267 $body['plugin_v'] = RABBITLOADER_PLUG_VERSION;
268 $body['cms_v'] = get_bloginfo('version');
269
270 $args['method'] = 'POST';
271 $args['body'] = $body;
272
273 try {
274 $http = wp_remote_post($url . $endpoint, $args);
275
276 if (is_wp_error($http)) {
277 $apiError = true;
278 $apiMessage = $http->get_error_message();
279 if (empty($apiMessage)) {
280 $apiMessage = '';
281 }
282 if (self::isTemporaryError($apiMessage)) {
283 //chill, it happens
284 } else {
285 RabbitLoader_21_Core::on_exception($http->get_error_message() . $url . $endpoint);
286 }
287 $http = [];
288 }
289
290 if (!empty($http['response']['code']) && in_array($http['response']['code'], [200, 401])) {
291 $http['body'] = json_decode($http['body'], true);
292 if (!empty($http['body']['message'])) {
293 $message = $http['body']['message'];
294 if (!strcmp($message, 'AUTH_REQUIRED') || !strcmp($message, 'INVALID_DOMAIN')) {
295 RabbitLoader_21_Core::update_api_tokens('', '', '', "$message when $endpoint was called");
296 }
297 }
298 $apiError = empty($http['body']['result']);
299 $apiMessage = empty($http['body']['message']) ? '' : $http['body']['message'];
300 }
301 } catch (Throwable $e) {
302 RabbitLoader_21_Core::on_exception($e);
303 $apiError = true;
304 $apiMessage = $e->getMessage();
305 }
306 return $http;
307 }
308
309 private static function log_debug($message, $context = [])
310 {
311 if (!defined('WP_DEBUG') || !WP_DEBUG) {
312 return;
313 }
314
315 try {
316 RabbitLoader_21_Core::get_log_file('rl_debug', $debug_log);
317 $line = date('c') . ' ' . $message;
318 if (!empty($context)) {
319 $line .= ' ' . json_encode($context, JSON_INVALID_UTF8_IGNORE);
320 }
321 $line .= PHP_EOL;
322 $cache_dir = RL21UtilWP::get_cache_dir('');
323 if (!file_exists($cache_dir)) {
324 @mkdir($cache_dir, 0755, true);
325 }
326 file_put_contents($debug_log, $line, FILE_APPEND | LOCK_EX);
327 } catch (Throwable $e) {
328 RabbitLoader_21_Core::on_exception($e);
329 }
330 }
331
332 public static function notify_backend_disconnect($source, &$apiError, &$apiMessage)
333 {
334 $http = [];
335 $args = [];
336 $apiError = true;
337 $apiMessage = '';
338
339 if (!RabbitLoader_21_Core::addKeys($args, $rabbitloader_field_domain)) {
340 $apiMessage = 'RabbitLoader connection token is missing. Please reconnect the plugin and try again.';
341 self::log_debug('backend disconnect skipped', [
342 'source' => $source,
343 'reason' => 'missing token',
344 ]);
345 return false;
346 }
347
348 $endpoint = 'domain/heartbeat';
349 $url = RabbitLoader_21_Core::getRLDomainV1() . 'api/v1/' . $endpoint;
350 $body = [
351 'uninstall' => 1,
352 'domain' => $rabbitloader_field_domain,
353 'plugin_cms' => 'wp',
354 'plugin_v' => RABBITLOADER_PLUG_VERSION,
355 'cms_v' => get_bloginfo('version'),
356 ];
357
358 $args['method'] = 'POST';
359 $args['body'] = $body;
360
361 self::log_debug('backend disconnect request', [
362 'source' => $source,
363 'endpoint' => $endpoint,
364 'body' => $body,
365 ]);
366
367 try {
368 $http = wp_remote_post($url, $args);
369
370 if (is_wp_error($http)) {
371 $apiMessage = $http->get_error_message();
372 if (empty($apiMessage)) {
373 $apiMessage = 'Network error while disconnecting RabbitLoader.';
374 }
375 self::log_debug('backend disconnect wp_error', [
376 'source' => $source,
377 'message' => $apiMessage,
378 ]);
379
380 if (!self::isTemporaryError($apiMessage)) {
381 RabbitLoader_21_Core::on_exception($apiMessage . $url);
382 }
383 return false;
384 }
385
386 if (!is_array($http)) {
387 $apiMessage = 'Unexpected response while disconnecting RabbitLoader.';
388 self::log_debug('backend disconnect unexpected response', [
389 'source' => $source,
390 'response_type' => gettype($http),
391 ]);
392 return false;
393 }
394
395 $code = intval(wp_remote_retrieve_response_code($http));
396 $raw_body = wp_remote_retrieve_body($http);
397 $parsed_body = json_decode($raw_body, true);
398
399 self::log_debug('backend disconnect response', [
400 'source' => $source,
401 'code' => $code,
402 'body' => $parsed_body === null ? $raw_body : $parsed_body,
403 ]);
404
405 if (is_array($parsed_body) && isset($parsed_body['message']) && $parsed_body['message'] !== '') {
406 $apiMessage = $parsed_body['message'];
407 }
408
409 if ($code >= 200 && $code < 300 && is_array($parsed_body) && !empty($parsed_body['result'])) {
410 $apiError = false;
411 return true;
412 }
413
414 if (empty($apiMessage)) {
415 if (in_array($code, [401, 403])) {
416 $apiMessage = 'RabbitLoader could not authorize this disconnect request. Please try again or reconnect the plugin.';
417 } else if ($code >= 500 || $code === 0) {
418 $apiMessage = 'RabbitLoader backend is temporarily unavailable. Please try disconnecting again.';
419 } else {
420 $apiMessage = 'RabbitLoader backend did not confirm the disconnect. Please try again.';
421 }
422 }
423 } catch (Throwable $e) {
424 RabbitLoader_21_Core::on_exception($e);
425 $apiMessage = $e->getMessage();
426 }
427
428 return false;
429 }
430
431 public static function getWpUserOption(&$user_options)
432 {
433 if (!empty(self::$user_options)) {
434 $user_options = self::$user_options;
435 return;
436 }
437 if (function_exists('get_option')) {
438 $user_options = get_option('rabbit_loader_user_options');
439 } else {
440 RabbitLoader_21_Core::get_log_file('rl_user_options', $rl_user_options);
441 if (file_exists($rl_user_options)) {
442 $user_options = json_decode(file_get_contents($rl_user_options), true);
443 }
444 }
445 if (empty($user_options) || !is_array($user_options)) {
446 $user_options = [];
447 }
448 $default_values = [
449 'purge_on_change' => false,
450 'exclude_patterns' => '',
451 'ignore_params' => '',
452 'private_mode_val' => false,
453 ];
454 foreach ($default_values as $k => $v) {
455 if (!isset($user_options[$k])) {
456 $user_options[$k] = $v;
457 }
458 }
459 self::$user_options = $user_options;
460 }
461 public static function updateUserOption(&$user_options)
462 {
463 self::$user_options = $user_options;
464 update_option('rabbit_loader_user_options', $user_options, true);
465 try {
466 RabbitLoader_21_Core::get_log_file('rl_user_options', $rl_user_options);
467 $rl_json = json_encode($user_options, JSON_INVALID_UTF8_IGNORE);
468 RabbitLoader_21_Util_Core::fpc($rl_user_options, $rl_json, WP_DEBUG);
469 } catch (\Throwable $e) {
470 RabbitLoader_21_Core::on_exception($e);
471 }
472 }
473
474
475 public static function getWpOption(&$rl_wp_options)
476 {
477 if (!empty(self::$rl_wp_options)) {
478 $rl_wp_options = self::$rl_wp_options;
479 return;
480 }
481 if (function_exists('get_option')) {
482 $rl_wp_options = get_option('rabbit_loader_wp_options');
483 } else {
484 RabbitLoader_21_Core::get_log_file(self::LOCAL_CONFIG_FILE, $rl_config);
485 if (file_exists($rl_config)) {
486 $rl_wp_options = json_decode(file_get_contents($rl_config), true);
487 }
488 }
489 if (empty($rl_wp_options)) {
490 $rl_wp_options = [];
491 }
492 self::$rl_wp_options = $rl_wp_options;
493 }
494 /**
495 * Get value of single config option
496 */
497 public static function getWpOptVal($key)
498 {
499 RabbitLoader_21_Core::getWpOption($rl_wp_options);
500 return isset($rl_wp_options[$key]) ? $rl_wp_options[$key] : '';
501 }
502
503 public static function updateWpOption(&$rl_wp_options)
504 {
505 self::$rl_wp_options = $rl_wp_options;
506 try {
507 update_option('rabbit_loader_wp_options', $rl_wp_options, true);
508 } catch (\Throwable $e) {
509 RabbitLoader_21_Core::on_exception($e);
510 }
511 try {
512 RabbitLoader_21_Core::get_log_file(self::LOCAL_CONFIG_FILE, $rl_config);
513 $rl_json = json_encode($rl_wp_options, JSON_INVALID_UTF8_IGNORE);
514 RabbitLoader_21_Util_Core::fpc($rl_config, $rl_json, WP_DEBUG);
515 } catch (\Throwable $e) {
516 RabbitLoader_21_Core::on_exception($e);
517 }
518 }
519
520 public static function purge_all(&$purge_count, $purge_source, &$tp_purge_count)
521 {
522 try {
523 //RL purges
524 $purge_count = 0;
525 $purge_count += RabbitLoader_21_Core::cleanAllCachedFiles();
526
527 //other common platforms purges
528 RabbitLoader_21_TP::purge_all($tp_purge_count);
529 } catch (Throwable $e) {
530 RabbitLoader_21_Core::on_exception($e);
531 }
532 }
533
534 /**
535 * @param string $fn file name
536 * @param string $fp file path
537 */
538 public static function get_log_file($fn, &$fp)
539 {
540 $fp = RL21UtilWP::get_cache_dir() . DIRECTORY_SEPARATOR . $fn . ".log";
541 }
542
543 public static function delete_log_file($fn)
544 {
545 $fp = '';
546 self::get_log_file($fn, $fp);
547 if ($fp && file_exists($fp)) {
548 @unlink($fp);
549 }
550 }
551
552 public static function on_exception($exception, $limit = 8)
553 {
554 try {
555 $rlSDK = self::getSDK();
556 $rlSDK->excCatch($exception, ['src' => 'wp'], $limit);
557 } catch (Throwable $e) {
558 if (WP_DEBUG) {
559 echo $e->getMessage();
560 }
561 }
562 }
563
564 public static function sendJsonResponse(&$response)
565 {
566 header("Content-Type: application/json");
567 header("Cache-Control: no-store, no-cache, must-revalidate, max-age=0, s-max-age=0");
568 header("Cache-Control: post-check=0, pre-check=0", false);
569 header("Pragma: no-cache");
570 $encoded_str = json_encode($response, JSON_INVALID_UTF8_IGNORE);
571 if ($encoded_str === false) {
572 echo '{"time":"1", "failed":"1"}';
573 } else {
574 echo $encoded_str;
575 }
576 exit;
577 }
578
579 public static function get_common_cache_urls(&$urls_to_purge)
580 {
581 if (empty($urls_to_purge)) {
582 $urls_to_purge = [];
583 }
584
585 $urls_to_purge[] = get_home_url(); //always purge home page if any other page is modified
586 $urls_to_purge[] = get_home_url() . "/"; //always purge home page if any other page is modified
587 $urls_to_purge[] = home_url('/'); //always purge home page if any other page is modified
588 $urls_to_purge[] = site_url('/'); //always purge home page if any other page is modified
589
590 //clean pagination urls
591 try {
592 if (!empty(get_option('page_for_posts'))) {
593 $page_for_posts = get_permalink(get_option('page_for_posts'));
594 if (is_string($page_for_posts) && !empty($page_for_posts) && get_option('show_on_front') == 'page') {
595 $urls_to_purge[] = $page_for_posts;
596 }
597 }
598
599 $posts_per_page = get_option('posts_per_page');
600 $published_posts = RabbitLoader_21_Core::get_published_count();
601 $page_number_max = min(3, ceil($published_posts / $posts_per_page));
602 for ($pn = 1; $pn < $page_number_max; $pn++) {
603 $urls_to_purge[] = home_url(sprintf('/page/%s/', $pn));
604 }
605 } catch (Throwable $e) {
606 RabbitLoader_21_Core::on_exception($e);
607 }
608 }
609
610 public static function get_published_count()
611 {
612 //$published_count = wp_count_posts()->publish + wp_count_posts('page')->publish;
613 $published_count = 0;
614 $post_types = get_post_types(['public' => true], 'names', 'and');
615 $ex = ['attachment', 'elementor_library'];
616 foreach ($post_types as $post_type) {
617 if (in_array($post_type, $ex)) {
618 continue;
619 }
620 $published_count += wp_count_posts($post_type)->publish;
621 }
622 return $published_count + 1; //1 for home page
623 }
624
625 public static function get_recent_posts(&$offset, &$posts_per_page, &$published_count, &$permalinks)
626 {
627 $permalinks = [];
628 $queued_count = 0;
629
630 //$latest_modified_ts = 0;
631 //published posts
632 $published_count = RabbitLoader_21_Core::get_published_count();
633
634 self::sanitize_recent_posts_pagination($offset, $posts_per_page, $published_count);
635
636 $permalink_structure = get_option('permalink_structure');
637 $append_slash = substr($permalink_structure, -1) == "/" ? true : false;
638 $args = array(
639 'post_status' => 'publish',
640 'post_type' => 'any',
641 'orderby' => 'post_date',
642 'order' => 'DESC',
643 'fields' => 'ids', // Only get post IDs
644 'posts_per_page' => $posts_per_page,
645 'offset' => intval($offset),
646 'no_found_rows' => false
647 );
648
649 try {
650 $posts = get_posts($args);
651 if (!empty($posts)) {
652 foreach ($posts as $post_id) {
653 $the_post = get_post($post_id);
654 $permalink = get_permalink($the_post);
655 if (empty($permalink)) {
656 continue;
657 }
658 if ($append_slash) {
659 $permalink = trailingslashit($permalink);
660 } else {
661 $permalink = $permalink . $append_slash;
662 }
663
664 // $modified_ts = strtotime(get_the_modified_date('c', $the_post));
665 // if ($modified_ts > $latest_modified_ts) {
666 // $latest_modified_ts = $modified_ts;
667 // }
668 $permalinks[] = $permalink;
669 ++$queued_count;
670 }
671 } else {
672 $offset = 0;
673 }
674 } catch (Throwable $e) {
675 $responses['exception'] = true;
676 RabbitLoader_21_Core::on_exception($e);
677 }
678 }
679
680 protected static function sanitize_recent_posts_pagination(&$offset, &$posts_per_page, $published_count)
681 {
682 $posts_per_page = intval($posts_per_page);
683 if ($posts_per_page < 1) {
684 $posts_per_page = 10;
685 } elseif ($posts_per_page > 250) {
686 $posts_per_page = 250;
687 }
688
689 $offset = intval($offset);
690 if ($offset < 0 || $offset > intval($published_count)) {
691 $offset = 0;
692 }
693 }
694
695 private static function &checkHostingName()
696 {
697 $hosting_name = 'NA';
698 if (!empty($_SERVER['cw_allowed_ip'])) {
699 $hosting_name = $_SERVER['cw_allowed_ip'];
700 } else if (class_exists('WpeCommon') && method_exists('WpeCommon', 'purge_memcached')) {
701 $hosting_name = 'wpengine';
702 } else if (defined("KINSTAMU_VERSION")) {
703 $hosting_name = 'Kinsta';
704 } else if (RL21UtilWP::is_flywheel()) {
705 $hosting_name = 'flywheel';
706 } else if (preg_match("/^dp-.+/", gethostname())) {
707 $hosting_name = 'dreamhost';
708 } else if (defined("CLOSTE_APP_ID")) {
709 $hosting_name = 'closte';
710 } else if (function_exists('sg_cachepress_purge_cache')) {
711 $hosting_name = 'siteground';
712 } else if (class_exists('LiteSpeed_Cache_API') && method_exists('LiteSpeed_Cache_API', 'purge_all')) {
713 $hosting_name = 'litespeed';
714 } else if (class_exists('PagelyCachePurge') && method_exists('PagelyCachePurge', 'purgeAll')) {
715 $hosting_name = 'pagely';
716 } else if (class_exists('comet_cache') && method_exists('comet_cache', 'clear')) {
717 $hosting_name = 'comet';
718 } else if (defined('IS_PRESSABLE')) {
719 $hosting_name = 'pressable';
720 }
721 return $hosting_name;
722 }
723
724 public static function &getSDK()
725 {
726 global $wp_version;
727 if (empty($GLOBALS['rlSDK'])) {
728 $rlSDK = new RabbitLoader\SDK\RabbitLoader(RabbitLoader_21_Core::getWpOptVal('api_token'), RL21UtilWP::get_cache_dir());
729 $GLOBALS['rlSDK'] = &$rlSDK;
730 $rlSDK->setDebug(WP_DEBUG);
731
732 $rlSDK->registerPurgeCallback(function ($url) {
733 $tp_purge_count = 0;
734 RabbitLoader_21_TP::purge_url($url, $tp_purge_count);
735 });
736
737 $platform = [
738 'plugin_cms' => 'wp',
739 'plugin_v' => RL21UtilWP::getRLPlugVersion(),
740 'cms_v' => function_exists('get_bloginfo') ? get_bloginfo('version') : ''
741 ];
742 if (empty($platform['cms_v']) && $wp_version) {
743 $platform['cms_v'] = $wp_version;
744 }
745 $rlSDK->setPlatform($platform);
746 }
747 return $GLOBALS['rlSDK'];
748 }
749
750 public static function cleanAllCachedFiles()
751 {
752 $rlSDK = self::getSDK();
753 $deleted_count = $rlSDK->deleteAll();
754
755 if (function_exists("opcache_reset")) {
756 opcache_reset();
757 }
758 if (function_exists('delete_transient')) {
759 delete_transient('rabbitloader_trans_overview_data');
760 }
761 return $deleted_count;
762 }
763
764 public static function getCacheCount()
765 {
766 $rlSDK = self::getSDK();
767 return $rlSDK->getCacheCount();
768 }
769 }
770