| 1 |
<?php |
| 2 |
|
| 3 |
class RL21UtilWP |
| 4 |
{ |
| 5 |
|
| 6 |
const POST_ID_ALL = "all"; |
| 7 |
|
| 8 |
private static $isSearch = false; |
| 9 |
private static $isPageAccount = false; |
| 10 |
private static $purge_queue = []; |
| 11 |
|
| 12 |
public static function init() |
| 13 |
{ |
| 14 |
add_action('template_redirect', function () { |
| 15 |
self::$isSearch = (is_search() || !empty($_GET["s"])); |
| 16 |
self::$isPageAccount = (function_exists("is_page") && is_page("account")); |
| 17 |
}); |
| 18 |
} |
| 19 |
|
| 20 |
public static function is_cli() |
| 21 |
{ |
| 22 |
return (defined("WP_CLI") && WP_CLI); |
| 23 |
} |
| 24 |
|
| 25 |
public static function is_user_logged_in() |
| 26 |
{ |
| 27 |
|
| 28 |
if (function_exists('is_user_logged_in')) { |
| 29 |
//if WP is not initialized, we may not get the function, so we have to do our own checks as well |
| 30 |
return is_user_logged_in(); |
| 31 |
} |
| 32 |
|
| 33 |
$cookies_keys = []; |
| 34 |
|
| 35 |
if (defined('RABBITLOADER_AC_LOGGED_IN_COOKIE')) { |
| 36 |
$cookies_keys[] = RABBITLOADER_AC_LOGGED_IN_COOKIE; |
| 37 |
} |
| 38 |
|
| 39 |
if (defined('LOGGED_IN_COOKIE')) { |
| 40 |
$cookies_keys[] = LOGGED_IN_COOKIE; |
| 41 |
} |
| 42 |
|
| 43 |
foreach ($cookies_keys as $key) { |
| 44 |
if (!empty($_COOKIE[$key])) { |
| 45 |
return true; |
| 46 |
} |
| 47 |
} |
| 48 |
|
| 49 |
return false; |
| 50 |
} |
| 51 |
|
| 52 |
public static function is_login_page() |
| 53 |
{ |
| 54 |
$is_login = function_exists('is_login') && function_exists('wp_login_url') && is_login(); |
| 55 |
$is_lostpass = !empty($_GET["action"]) && $_GET["action"] == 'lostpassword'; |
| 56 |
$incl_path = str_replace(['\\', '/'], DIRECTORY_SEPARATOR, ABSPATH); |
| 57 |
|
| 58 |
return $is_login || $is_lostpass || (in_array($incl_path . 'wp-login.php', get_included_files()) |
| 59 |
|| in_array($incl_path . 'wp-register.php', get_included_files())) |
| 60 |
|| (isset($_GLOBALS['pagenow']) && $GLOBALS['pagenow'] === 'wp-login.php') |
| 61 |
|| $_SERVER['PHP_SELF'] == '/wp-login.php' || self::$isPageAccount; |
| 62 |
} |
| 63 |
|
| 64 |
public static function is_ajax() |
| 65 |
{ |
| 66 |
|
| 67 |
$incl_path = str_replace(['\\', '/'], DIRECTORY_SEPARATOR, ABSPATH); |
| 68 |
|
| 69 |
return (function_exists("wp_doing_ajax") && wp_doing_ajax()) || |
| 70 |
(defined('DOING_AJAX') && DOING_AJAX) || |
| 71 |
(!empty($_SERVER["HTTP_X_REQUESTED_WITH"]) && $_SERVER["HTTP_X_REQUESTED_WITH"] == "XMLHttpRequest") || |
| 72 |
(function_exists('get_included_files') && in_array($incl_path . 'wp-cron.php', get_included_files())) || |
| 73 |
(function_exists('get_included_files') && in_array($incl_path . 'admin-ajax.php', get_included_files())); |
| 74 |
} |
| 75 |
|
| 76 |
public static function is_admin_request() |
| 77 |
{ |
| 78 |
if (function_exists('is_admin') && is_admin()) { |
| 79 |
return true; |
| 80 |
} |
| 81 |
|
| 82 |
$request_uri = empty($_SERVER['REQUEST_URI']) ? '' : parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH); |
| 83 |
if (!empty($request_uri) && strpos($request_uri, '/wp-admin/') === 0) { |
| 84 |
return true; |
| 85 |
} |
| 86 |
|
| 87 |
$php_self = empty($_SERVER['PHP_SELF']) ? '' : $_SERVER['PHP_SELF']; |
| 88 |
return !empty($php_self) && strpos($php_self, '/wp-admin/') === 0; |
| 89 |
} |
| 90 |
|
| 91 |
public static function is_rl_test_request() |
| 92 |
{ |
| 93 |
if (isset($_GET['rltest'])) { |
| 94 |
return true; |
| 95 |
} |
| 96 |
|
| 97 |
$query_string = empty($_SERVER['QUERY_STRING']) ? '' : $_SERVER['QUERY_STRING']; |
| 98 |
if (empty($query_string)) { |
| 99 |
return false; |
| 100 |
} |
| 101 |
|
| 102 |
parse_str($query_string, $query_vars); |
| 103 |
return isset($query_vars['rltest']); |
| 104 |
} |
| 105 |
|
| 106 |
public static function is_rest() |
| 107 |
{ |
| 108 |
return defined('REST_REQUEST') && REST_REQUEST; |
| 109 |
} |
| 110 |
|
| 111 |
public static function is_no_rl($url) |
| 112 |
{ |
| 113 |
return (stripos($url, "rl-no-optimization") !== false) || (stripos($url, "norl") !== false); |
| 114 |
} |
| 115 |
|
| 116 |
public static function is_cart() |
| 117 |
{ |
| 118 |
$isCart = false; |
| 119 |
try { |
| 120 |
$isCart = (function_exists("is_cart") && is_cart()); |
| 121 |
if ($isCart) { |
| 122 |
RabbitLoader_21_Core::getWpUserOption($user_options); |
| 123 |
$user_options['cart_uri'] = RabbitLoader_21_Util_Core::serverURINoGet(); |
| 124 |
RabbitLoader_21_Core::updateUserOption($user_options); |
| 125 |
} |
| 126 |
} catch (Throwable $e) { |
| 127 |
//is_cart may have dependency on wc_get_page_id() |
| 128 |
RabbitLoader_21_Core::on_exception($e); |
| 129 |
} |
| 130 |
return $isCart; |
| 131 |
} |
| 132 |
|
| 133 |
|
| 134 |
public static function is_checkout() |
| 135 |
{ |
| 136 |
$isCheckout = (function_exists("is_checkout") && is_checkout()); |
| 137 |
if ($isCheckout) { |
| 138 |
RabbitLoader_21_Core::getWpUserOption($user_options); |
| 139 |
$user_options['checkout_uri'] = RabbitLoader_21_Util_Core::serverURINoGet(); |
| 140 |
RabbitLoader_21_Core::updateUserOption($user_options); |
| 141 |
} |
| 142 |
return $isCheckout; |
| 143 |
} |
| 144 |
|
| 145 |
public static function is_search() |
| 146 |
{ |
| 147 |
return self::$isSearch || (function_exists("is_search") && is_search()) || isset($_GET["s"]) || (stripos(RabbitLoader_21_Util_Core::serverURINoGet(), '/search/') === 0); |
| 148 |
} |
| 149 |
|
| 150 |
public static function is_flywheel() |
| 151 |
{ |
| 152 |
return defined("FLYWHEEL_PLUGIN_DIR"); |
| 153 |
} |
| 154 |
|
| 155 |
public static function get_wp_config() |
| 156 |
{ |
| 157 |
$wp_config_path = ''; |
| 158 |
if (file_exists(ABSPATH . 'wp-config.php')) { |
| 159 |
$wp_config_path = ABSPATH . 'wp-config.php'; |
| 160 |
} elseif (@file_exists(dirname(ABSPATH) . '/wp-config.php') && !@file_exists(dirname(ABSPATH) . '/wp-settings.php')) { |
| 161 |
// config file is not part of another installation |
| 162 |
$wp_config_path = dirname(ABSPATH) . '/wp-config.php'; |
| 163 |
} else { |
| 164 |
$wp_config_path = false; |
| 165 |
} |
| 166 |
return $wp_config_path; |
| 167 |
} |
| 168 |
|
| 169 |
public static function getRLPlugVersion() |
| 170 |
{ |
| 171 |
return defined('RABBITLOADER_PLUG_VERSION') ? RABBITLOADER_PLUG_VERSION : (defined('RABBITLOADER_AC_PLUG_VERSION') ? RABBITLOADER_AC_PLUG_VERSION : ''); |
| 172 |
} |
| 173 |
|
| 174 |
public static function _e($txt) |
| 175 |
{ |
| 176 |
echo RL21UtilWP::__($txt); |
| 177 |
} |
| 178 |
public static function __($txt) |
| 179 |
{ |
| 180 |
return __($txt, RABBITLOADER_TEXT_DOMAIN); |
| 181 |
} |
| 182 |
public static function _n($txt_singular, $txt_plural, $count) |
| 183 |
{ |
| 184 |
return _n($txt_singular, $txt_plural, $count, RABBITLOADER_TEXT_DOMAIN); |
| 185 |
} |
| 186 |
|
| 187 |
/** |
| 188 |
* @return string directory path (without trailing slash) where cache files are stored |
| 189 |
*/ |
| 190 |
public static function &get_cache_dir($cache_ttl = '') |
| 191 |
{ |
| 192 |
$cache_dir = ''; |
| 193 |
|
| 194 |
if (defined('RABBITLOADER_AC_CACHE_DIR')) { |
| 195 |
$cache_dir = RABBITLOADER_AC_CACHE_DIR; |
| 196 |
} else if (defined('RABBITLOADER_CACHE_DIR')) { |
| 197 |
$cache_dir = RABBITLOADER_CACHE_DIR; |
| 198 |
} else { |
| 199 |
$cache_dir = WP_CONTENT_DIR . DIRECTORY_SEPARATOR . "rabbitloader"; |
| 200 |
} |
| 201 |
if (!empty($cache_ttl)) { |
| 202 |
$cache_dir = $cache_dir . DIRECTORY_SEPARATOR . $cache_ttl; |
| 203 |
} |
| 204 |
return $cache_dir; |
| 205 |
} |
| 206 |
|
| 207 |
public static function onPostChange($post_id) |
| 208 |
{ |
| 209 |
if (strcmp($post_id, RL21UtilWP::POST_ID_ALL) === 0) { |
| 210 |
self::$purge_queue[RL21UtilWP::POST_ID_ALL] = true; |
| 211 |
} else { |
| 212 |
try { |
| 213 |
if (wp_is_post_autosave($post_id) || wp_is_post_revision($post_id)) { |
| 214 |
//no need to purge cache for these posts as they are never displayed on website |
| 215 |
return; |
| 216 |
} |
| 217 |
} catch (\Throwable $e) { |
| 218 |
RabbitLoader_21_Core::on_exception($e); |
| 219 |
} |
| 220 |
|
| 221 |
if (empty(self::$purge_queue['post_ids'])) { |
| 222 |
self::$purge_queue['post_ids'] = []; |
| 223 |
} |
| 224 |
self::$purge_queue['post_ids'][$post_id] = true; |
| 225 |
|
| 226 |
$post_ancestors = get_post_ancestors($post_id); |
| 227 |
if (!empty($post_ancestors)) { |
| 228 |
foreach ($post_ancestors as $parent_id) { |
| 229 |
self::$purge_queue['post_ids'][$parent_id] = true; |
| 230 |
} |
| 231 |
} |
| 232 |
|
| 233 |
$wpml_master_post_id = apply_filters('wpml_master_post_from_duplicate', $post_id); |
| 234 |
$wpml_master_post_id = empty($wpml_master_post_id) ? $post_id : $wpml_master_post_id; |
| 235 |
$wpml_posts = apply_filters('wpml_post_duplicates', $wpml_master_post_id); |
| 236 |
if (!empty($wpml_posts) && is_array($wpml_posts)) { |
| 237 |
foreach ($wpml_posts as $lang => $lang_post_id) { |
| 238 |
self::$purge_queue['post_ids'][$lang_post_id] = true; |
| 239 |
} |
| 240 |
RabbitLoader\SDK\Util::sendHeader("x-rl-wpml_posts: " . json_encode($wpml_posts), false); |
| 241 |
} |
| 242 |
RabbitLoader\SDK\Util::sendHeader("x-rl-posts:" . json_encode(self::$purge_queue['post_ids']), false); |
| 243 |
} |
| 244 |
} |
| 245 |
|
| 246 |
public static function execute_purge(&$local_purge_count) |
| 247 |
{ |
| 248 |
RabbitLoader_21_Core::getWpUserOption($user_options); |
| 249 |
|
| 250 |
$purge_source = empty(self::$purge_queue['purge_source']) ? '' : self::$purge_queue['purge_source']; |
| 251 |
$clean_cache = !empty($user_options['purge_on_change']); |
| 252 |
|
| 253 |
if (!empty(self::$purge_queue[RL21UtilWP::POST_ID_ALL])) { |
| 254 |
RabbitLoader_21_Core::purge_all($local_purge_count, $purge_source, $tp_purge_count); |
| 255 |
} else if (!empty(self::$purge_queue['post_ids'])) { |
| 256 |
$urls_to_purge = []; |
| 257 |
RabbitLoader_21_Core::get_common_cache_urls($urls_to_purge); |
| 258 |
foreach (self::$purge_queue['post_ids'] as $post_ID => $val) { |
| 259 |
$post_obj = get_post(intval($post_ID)); |
| 260 |
if (!$post_obj) { |
| 261 |
continue; |
| 262 |
} |
| 263 |
$post_canonical_url = wp_get_canonical_url($post_ID); |
| 264 |
$urls_to_purge[] = $post_canonical_url; |
| 265 |
self::get_all_taxonomies($post_ID, $post_obj->post_type, $tax_ids, $urls_to_purge); |
| 266 |
if ($clean_cache) { |
| 267 |
RabbitLoader_21_TP::purge_post_id($post_ID, $tp_purge_count); |
| 268 |
} |
| 269 |
} |
| 270 |
|
| 271 |
$urls_to_purge = array_filter($urls_to_purge); |
| 272 |
$urls_to_purge = array_unique($urls_to_purge); |
| 273 |
$rlSDK = RabbitLoader_21_Core::getSDK(); |
| 274 |
foreach ($urls_to_purge as $url) { |
| 275 |
if ($clean_cache) { |
| 276 |
if ($rlSDK->delete($url)) { |
| 277 |
$local_purge_count++; |
| 278 |
} |
| 279 |
RabbitLoader_21_TP::purge_url($url, $tp_purge_count); |
| 280 |
} else { |
| 281 |
$rlSDK->onContentChange($url); |
| 282 |
$local_purge_count++; |
| 283 |
} |
| 284 |
RabbitLoader\SDK\Util::sendHeader("x-rl-url: $url", false); |
| 285 |
} |
| 286 |
do_action('rl_purge_request_complete', $urls_to_purge); |
| 287 |
} |
| 288 |
self::$purge_queue = []; |
| 289 |
} |
| 290 |
|
| 291 |
private static function get_all_taxonomies($post_ID, $post_type, &$tax_ids, &$tax_urls) |
| 292 |
{ |
| 293 |
$tax_ids = array(); |
| 294 |
$taxonomies = get_object_taxonomies($post_type); |
| 295 |
foreach ($taxonomies as $taxonomy) { |
| 296 |
$taxonomy_data = get_taxonomy($taxonomy); |
| 297 |
if ($taxonomy_data instanceof WP_Taxonomy && $taxonomy_data->public === false) { |
| 298 |
continue; |
| 299 |
} |
| 300 |
$terms = get_the_terms($post_ID, $taxonomy); |
| 301 |
|
| 302 |
if (empty($terms) || is_wp_error($terms)) { |
| 303 |
continue; |
| 304 |
} else { |
| 305 |
foreach ($terms as $term) { |
| 306 |
if (!empty($term)) { |
| 307 |
$tax_ids[] = $term->term_taxonomy_id; |
| 308 |
$tax_url = get_term_link($term); |
| 309 |
if (!is_wp_error($tax_url) && !empty($tax_url)) { |
| 310 |
$tax_urls[] = $tax_url; |
| 311 |
} |
| 312 |
} |
| 313 |
} |
| 314 |
} |
| 315 |
} |
| 316 |
|
| 317 |
$other_urls_to_merge = []; |
| 318 |
try { |
| 319 |
$other_urls_to_merge[] = get_author_posts_url(get_post_field('post_author', $post_ID)); |
| 320 |
$other_urls_to_merge[] = get_author_feed_link(get_post_field('post_author', $post_ID)); |
| 321 |
$other_urls_to_merge[] = get_post_type_archive_link($post_type); |
| 322 |
$other_urls_to_merge[] = get_post_type_archive_feed_link($post_type); |
| 323 |
$other_urls_to_merge[] = get_permalink($post_ID); |
| 324 |
$other_urls_to_merge[] = get_post_comments_feed_link($post_ID); |
| 325 |
} catch (Throwable $e) { |
| 326 |
RabbitLoader_21_Core::on_exception($e); |
| 327 |
} |
| 328 |
if (!empty($other_urls_to_merge)) { |
| 329 |
foreach ($other_urls_to_merge as $other_url) { |
| 330 |
if (!empty($other_url) && !is_wp_error($other_url)) { |
| 331 |
$tax_urls[] = $other_url; |
| 332 |
} |
| 333 |
} |
| 334 |
} |
| 335 |
} |
| 336 |
|
| 337 |
private static function verifyToken() |
| 338 |
{ |
| 339 |
error_log("RabbitLoader: Verifying token..."); |
| 340 |
$did = RabbitLoader_21_Core::getWpOptVal('did'); |
| 341 |
if (empty($did)) { |
| 342 |
error_log("RabbitLoader: Domain ID (did) not found in options."); |
| 343 |
wp_send_json_error(['message' => 'Domain ID not configured'], 403); |
| 344 |
return; |
| 345 |
} |
| 346 |
|
| 347 |
$base_url = "https://api-v2.rabbitloader.com"; |
| 348 |
$ssl_verify = true; |
| 349 |
if (strpos(home_url(), 'wpunittest.com') !== false) { |
| 350 |
$base_url = "https://api-v2.rabbitloader.local"; |
| 351 |
$ssl_verify = false; |
| 352 |
} |
| 353 |
|
| 354 |
$url = $base_url . "/domain/verify/" . $did; |
| 355 |
error_log("RabbitLoader: Verification URL: " . $url); |
| 356 |
|
| 357 |
$response = wp_remote_post($url, [ |
| 358 |
'sslverify' => $ssl_verify, |
| 359 |
'headers' => [ |
| 360 |
'Authorization' => 'Bearer ' . $_POST['token'] |
| 361 |
], |
| 362 |
'body' => [ |
| 363 |
'url' => home_url() |
| 364 |
] |
| 365 |
]); |
| 366 |
|
| 367 |
if (is_wp_error($response)) { |
| 368 |
error_log("RabbitLoader: Token verification failed with WP Error: " . $response->get_error_message()); |
| 369 |
print_r($response); |
| 370 |
wp_send_json_error(null, 403); |
| 371 |
} |
| 372 |
|
| 373 |
$code = wp_remote_retrieve_response_code($response); |
| 374 |
error_log("RabbitLoader: Token verification response code: " . $code); |
| 375 |
|
| 376 |
if ($code === 200) { |
| 377 |
error_log("RabbitLoader: Token verified successfully."); |
| 378 |
return; |
| 379 |
} |
| 380 |
|
| 381 |
$body = wp_remote_retrieve_body($response); |
| 382 |
error_log("RabbitLoader: Token verification unsuccessful. Body: " . $body); |
| 383 |
wp_send_json_error(null, 403); |
| 384 |
} |
| 385 |
|
| 386 |
public static function verifyAjaxNonce() |
| 387 |
{ |
| 388 |
error_log("RabbitLoader: verifyAjaxNonce called."); |
| 389 |
if (empty($_POST['rl_nonce']) || !wp_verify_nonce($_POST['rl_nonce'], 'rl-ajax-nonce')) { |
| 390 |
error_log("RabbitLoader: Nonce verification failed or missing."); |
| 391 |
if (empty($_POST['token'])) { |
| 392 |
error_log("RabbitLoader: No token provided. Access denied."); |
| 393 |
wp_send_json_error(null, 403); |
| 394 |
} else { |
| 395 |
error_log("RabbitLoader: Token provided. Proceeding to verify token."); |
| 396 |
self::verifyToken(); |
| 397 |
} |
| 398 |
} else { |
| 399 |
error_log("RabbitLoader: Nonce verified successfully."); |
| 400 |
} |
| 401 |
} |
| 402 |
} |
| 403 |
|