| 1 |
<?php |
| 2 |
|
| 3 |
class RabbitLoader_21_Public |
| 4 |
{ |
| 5 |
|
| 6 |
static $skip_reason = ''; |
| 7 |
const skip_reason_pm = "me-mode"; |
| 8 |
const skip_reason_ep = "exclude-pattern"; |
| 9 |
const skip_reason_us = "user-session"; |
| 10 |
static $add_action_called = false; |
| 11 |
static $is_origin = false; |
| 12 |
static $cache_served = false; |
| 13 |
static $cacheFile = null; |
| 14 |
|
| 15 |
public static function addActions() |
| 16 |
{ |
| 17 |
if (self::$add_action_called) { |
| 18 |
return; |
| 19 |
} |
| 20 |
self::$add_action_called = true; |
| 21 |
//is_user_logged_in is a pluggable function and you could get a fatal error if you call it too early. |
| 22 |
add_action('init', 'RabbitLoader_21_Public::init', 10); |
| 23 |
add_action('shutdown', 'RabbitLoader_21_Public::shutdown', 10000); |
| 24 |
add_filter('wp_redirect', 'RabbitLoader_21_Public::wp_redirect', 10, 2); |
| 25 |
add_filter('redirect_canonical', 'RabbitLoader_21_Public::redirect_canonical', 10, 2); |
| 26 |
add_filter('pre_handle_404', 'RabbitLoader_21_Public::pre_handle_404', 10, 2); |
| 27 |
add_filter('paginate_links', 'RabbitLoader_21_Public::paginate_links', 10, 1); |
| 28 |
add_filter('nonce_life', 'RabbitLoader_21_Public::nonce_life'); |
| 29 |
|
| 30 |
RabbitLoader_21_CanonicalUrl::init(); |
| 31 |
RL21UtilWP::init(); |
| 32 |
|
| 33 |
//Third party theme hooks |
| 34 |
add_action('kirki_output_inline_styles', function ($value) { |
| 35 |
#theme developed using kirki framework may generate lots of inline css referring to font files making the page load slow |
| 36 |
#https://github.com/kirki-framework/kirki/blob/3a5d5093b1a99d1c7b813608479fe8fc9348b6a3/packages/kirki-framework/module-css/src/CSS.php |
| 37 |
$value = false; |
| 38 |
return $value; |
| 39 |
}, -1); |
| 40 |
} |
| 41 |
|
| 42 |
public static function init() |
| 43 |
{ |
| 44 |
if (current_user_can('manage_options') || current_user_can('edit_posts')) { |
| 45 |
add_action('admin_bar_menu', 'RabbitLoader_21_Public::adminBarMenu', 50); |
| 46 |
add_action('wp_enqueue_scripts', 'RabbitLoader_21_Public::adminBarScript'); |
| 47 |
} |
| 48 |
|
| 49 |
//few checks inside can_cache_request() can only be done when WordPress init hook is called. |
| 50 |
$rlSDK = RabbitLoader_21_Core::getSDK(); |
| 51 |
if (!self::can_cache_request()) { |
| 52 |
$rlSDK->ignoreRequest(self::$skip_reason); |
| 53 |
} |
| 54 |
} |
| 55 |
|
| 56 |
public static function shutdown() {} |
| 57 |
|
| 58 |
private static function can_cache_request() |
| 59 |
{ |
| 60 |
$allow_logged_in_test_request = RL21UtilWP::is_rl_test_request() && !RL21UtilWP::is_admin_request(); |
| 61 |
|
| 62 |
if (!empty(self::$skip_reason)) { |
| 63 |
return false; |
| 64 |
} |
| 65 |
if (RabbitLoader_21_Util_Core::get_request_type() != 'get') { |
| 66 |
//Not a GET request, return |
| 67 |
self::$skip_reason = 'method-' . RabbitLoader_21_Util_Core::get_request_type(); |
| 68 |
return false; |
| 69 |
} |
| 70 |
|
| 71 |
if ((RL21UtilWP::is_user_logged_in() && !$allow_logged_in_test_request) || RL21UtilWP::is_login_page()) { |
| 72 |
//we are not serving cached content to logged in users |
| 73 |
self::$skip_reason = self::skip_reason_us; |
| 74 |
return false; |
| 75 |
} |
| 76 |
|
| 77 |
if (RL21UtilWP::is_ajax()) { |
| 78 |
self::$skip_reason = 'ajax'; |
| 79 |
return false; |
| 80 |
} |
| 81 |
|
| 82 |
if (!empty($_COOKIE['woocommerce_items_in_cart'])) { |
| 83 |
//TBD- user has some items in cart, in future version we will check how to serve a cached page with cart |
| 84 |
self::$skip_reason = 'cart-items'; |
| 85 |
return false; |
| 86 |
} |
| 87 |
|
| 88 |
if (empty($_SERVER["HTTP_HOST"]) || RL21UtilWP::is_cli() || !empty($_GET["mwprid"])) { |
| 89 |
self::$skip_reason = 'cli'; |
| 90 |
return false; |
| 91 |
} |
| 92 |
|
| 93 |
global $wp_query; |
| 94 |
if (isset($wp_query) && $wp_query->is_404()) { |
| 95 |
self::$skip_reason = '404-not-found'; |
| 96 |
return false; |
| 97 |
} |
| 98 |
|
| 99 |
if (RL21UtilWP::is_search()) { |
| 100 |
self::$skip_reason = 'search'; |
| 101 |
return false; |
| 102 |
} |
| 103 |
|
| 104 |
if (RL21UtilWP::is_cart()) { |
| 105 |
self::$skip_reason = 'cart'; |
| 106 |
return false; |
| 107 |
} |
| 108 |
|
| 109 |
if (RL21UtilWP::is_checkout()) { |
| 110 |
self::$skip_reason = 'checkout'; |
| 111 |
return false; |
| 112 |
} |
| 113 |
|
| 114 |
RabbitLoader_21_Core::getWpUserOption($user_options); |
| 115 |
|
| 116 |
if (!empty($user_options['cart_uri']) && strcasecmp(RabbitLoader_21_Util_Core::serverURINoGet(), $user_options['cart_uri']) == 0) { |
| 117 |
self::$skip_reason = 'cart'; |
| 118 |
return false; |
| 119 |
} |
| 120 |
|
| 121 |
if (!empty($user_options['checkout_uri']) && strcasecmp(RabbitLoader_21_Util_Core::serverURINoGet(), $user_options['checkout_uri']) == 0) { |
| 122 |
self::$skip_reason = 'checkout'; |
| 123 |
return false; |
| 124 |
} |
| 125 |
|
| 126 |
if (!empty($user_options['exclude_patterns'])) { |
| 127 |
$exclude_patterns = explode("\n", $user_options['exclude_patterns']); |
| 128 |
$rlSDK = RabbitLoader_21_Core::getSDK(); |
| 129 |
$rlSDK->skipForPaths($exclude_patterns); |
| 130 |
} |
| 131 |
|
| 132 |
if (!empty(self::$skip_reason)) { |
| 133 |
self::$skip_reason = self::$skip_reason; |
| 134 |
return false; |
| 135 |
} |
| 136 |
|
| 137 |
return true; |
| 138 |
} |
| 139 |
|
| 140 |
public static function getSkipReason() |
| 141 |
{ |
| 142 |
return self::$skip_reason; |
| 143 |
} |
| 144 |
|
| 145 |
public static function process_incoming_request($mode = 'ac') |
| 146 |
{ |
| 147 |
try { |
| 148 |
RabbitLoader\SDK\Util::sendHeader("x-rl-mode: $mode", false); |
| 149 |
$rlSDK = RabbitLoader_21_Core::getSDK(); |
| 150 |
|
| 151 |
RabbitLoader_21_Core::getWpUserOption($user_options); |
| 152 |
if (!empty($user_options['ignore_params'])) { |
| 153 |
$ignore_params = explode("\n", $user_options['ignore_params']); |
| 154 |
if (!empty($ignore_params)) { |
| 155 |
$rlSDK->ignoreParams($ignore_params); |
| 156 |
} |
| 157 |
} |
| 158 |
if (!empty($user_options['private_mode_val'])) { |
| 159 |
$rlSDK->setMeMode(); |
| 160 |
} |
| 161 |
|
| 162 |
if (!self::can_cache_request()) { |
| 163 |
$rlSDK->ignoreRequest(self::$skip_reason); |
| 164 |
} |
| 165 |
$rlSDK->registerPurgeCallback(function ($url) { |
| 166 |
$tp_purge_count = 0; |
| 167 |
RabbitLoader_21_TP::purge_url($url, $tp_purge_count); |
| 168 |
}); |
| 169 |
$rlSDK->process(); |
| 170 |
} catch (Throwable $e) { |
| 171 |
RabbitLoader_21_Core::on_exception($e); |
| 172 |
} |
| 173 |
} |
| 174 |
|
| 175 |
|
| 176 |
public static function adminBarMenu($admin_bar) |
| 177 |
{ |
| 178 |
$top_menu = array( |
| 179 |
'id' => 'rabbitloader_top_menu', |
| 180 |
'title' => 'RabbitLoader', |
| 181 |
'href' => admin_url('options-general.php?page=rabbitloader') |
| 182 |
); |
| 183 |
|
| 184 |
$list_menu = array( |
| 185 |
'parent' => 'rabbitloader_top_menu', |
| 186 |
'id' => 'rabbitloader_purge_page', |
| 187 |
'title' => 'Purge cache for this page', |
| 188 |
'href' => "#", |
| 189 |
'meta' => array( |
| 190 |
'class' => 'rabbitloader_purge_page', |
| 191 |
) |
| 192 |
); |
| 193 |
|
| 194 |
$admin_bar->add_node($top_menu); |
| 195 |
$admin_bar->add_node($list_menu); |
| 196 |
} |
| 197 |
|
| 198 |
public static function adminBarScript() |
| 199 |
{ |
| 200 |
global $post; |
| 201 |
|
| 202 |
wp_enqueue_script('rabbitloader-index', RABBITLOADER_PLUG_URL . 'admin/js/index.js', ['jquery'], RABBITLOADER_PLUG_VERSION); |
| 203 |
$localVars = [ |
| 204 |
'admin_ajax' => admin_url('admin-ajax.php'), |
| 205 |
'post_id' => empty($post) ? 0 : $post->ID, |
| 206 |
'rl_nonce' => wp_create_nonce('rl-ajax-nonce'), |
| 207 |
'rl_acct' => !empty(RabbitLoader_21_Core::getWpOptVal('api_token')) |
| 208 |
]; |
| 209 |
//wp_localize_script('rabbitloader-index', 'rabbitloader_local_vars', $localVars); |
| 210 |
// wp_add_inline_script('rabbitloader-index', 'RLAdmin.Init(window, ' . json_encode($localVars) . ');'); |
| 211 |
$localVarsJson = wp_json_encode($localVars, JSON_HEX_TAG | JSON_HEX_AMP | JSON_HEX_APOS | JSON_HEX_QUOT); |
| 212 |
if ($localVarsJson === false) { |
| 213 |
$localVarsJson = '{}'; |
| 214 |
} |
| 215 |
|
| 216 |
wp_add_inline_script('rabbitloader-index', 'window.rabbitloader_local_vars = ' . $localVarsJson . ';'); |
| 217 |
wp_add_inline_script('rabbitloader-index', <<<'JS' |
| 218 |
(function () { |
| 219 |
function bindRabbitLoaderPurgePage() { |
| 220 |
var menuItem = document.getElementById('wp-admin-bar-rabbitloader_purge_page'); |
| 221 |
if (!menuItem || menuItem.dataset.rlPurgeBound === '1') { |
| 222 |
return; |
| 223 |
} |
| 224 |
|
| 225 |
menuItem.dataset.rlPurgeBound = '1'; |
| 226 |
menuItem.addEventListener('click', async function (event) { |
| 227 |
if (!event.target.closest('a')) { |
| 228 |
return; |
| 229 |
} |
| 230 |
|
| 231 |
event.preventDefault(); |
| 232 |
|
| 233 |
var config = window.rabbitloader_local_vars || {}; |
| 234 |
var postId = parseInt(config.post_id, 10); |
| 235 |
if (!postId || !config.admin_ajax || !config.rl_nonce) { |
| 236 |
return; |
| 237 |
} |
| 238 |
|
| 239 |
var body = new URLSearchParams(); |
| 240 |
body.append('action', 'rabbitloader_ajax_purge'); |
| 241 |
body.append('post_id', postId); |
| 242 |
body.append('rl_nonce', config.rl_nonce); |
| 243 |
|
| 244 |
await fetch(config.admin_ajax, { |
| 245 |
method: 'POST', |
| 246 |
credentials: 'same-origin', |
| 247 |
headers: { |
| 248 |
'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8' |
| 249 |
}, |
| 250 |
body: body |
| 251 |
}); |
| 252 |
location.reload() |
| 253 |
}); |
| 254 |
} |
| 255 |
|
| 256 |
if (document.readyState === 'loading') { |
| 257 |
document.addEventListener('DOMContentLoaded', bindRabbitLoaderPurgePage, { once: true }); |
| 258 |
return; |
| 259 |
} |
| 260 |
|
| 261 |
bindRabbitLoaderPurgePage(); |
| 262 |
}()); |
| 263 |
JS); |
| 264 |
} |
| 265 |
|
| 266 |
public static function wp_redirect($location, $status) |
| 267 |
{ |
| 268 |
if (!empty($location)) { |
| 269 |
self::$skip_reason = 'redirect-' . $status; |
| 270 |
} |
| 271 |
return $location; |
| 272 |
} |
| 273 |
|
| 274 |
public static function redirect_canonical($redirect_url, $requested_url) |
| 275 |
{ |
| 276 |
if (!empty($redirect_url)) { |
| 277 |
self::$skip_reason = 'redirect-canonical'; |
| 278 |
} |
| 279 |
return $redirect_url; |
| 280 |
} |
| 281 |
|
| 282 |
public static function pre_handle_404($preempt, $wp_query) |
| 283 |
{ |
| 284 |
if ($preempt) { |
| 285 |
self::$skip_reason = '404'; |
| 286 |
} |
| 287 |
return $preempt; |
| 288 |
} |
| 289 |
|
| 290 |
public static function paginate_links($link) |
| 291 |
{ |
| 292 |
if (!empty($link)) { |
| 293 |
$unsets = ['rl-no-optimization', 'norl', 'rl-warmup', 'rltest', 'rl-rand', 'rl-only-after']; |
| 294 |
list($urlpart, $qspart) = array_pad(explode('?', $link), 2, ''); |
| 295 |
parse_str($qspart, $qsvars); |
| 296 |
|
| 297 |
foreach ($unsets as $key) { |
| 298 |
if (isset($qsvars[$key])) { |
| 299 |
unset($qsvars[$key]); |
| 300 |
} |
| 301 |
} |
| 302 |
|
| 303 |
$newqs = http_build_query($qsvars); |
| 304 |
$link = $urlpart . (empty($newqs) ? '' : '?' . $newqs); |
| 305 |
return $link; |
| 306 |
} |
| 307 |
} |
| 308 |
|
| 309 |
public static function nonce_life($life) |
| 310 |
{ |
| 311 |
$referer = empty($_SERVER["HTTP_REFERER"]) ? '' : $_SERVER["HTTP_REFERER"]; |
| 312 |
$rlSDK = RabbitLoader_21_Core::getSDK(); |
| 313 |
|
| 314 |
if (RL21UtilWP::is_user_logged_in()) { |
| 315 |
return $life; |
| 316 |
} elseif (intval($life) > RabbitLoader_21_Core::ORPHANED_LONG_AGE_SEC) { |
| 317 |
return $life; |
| 318 |
} else if ($rlSDK->isWarmUp()) { |
| 319 |
return RabbitLoader_21_Core::ORPHANED_LONG_AGE_SEC; |
| 320 |
} else if ((RL21UtilWP::is_ajax() || RL21UtilWP::is_rest()) && (!empty($_COOKIE["rlCached"]) || RL21UtilWP::is_no_rl($referer))) { |
| 321 |
//for ajax, if cookie is set, the page was cached who called the ajax |
| 322 |
//or the referer page had no_optimization that matched the previous else condition |
| 323 |
return RabbitLoader_21_Core::ORPHANED_LONG_AGE_SEC; |
| 324 |
} else { |
| 325 |
//unhandled scenarios |
| 326 |
} |
| 327 |
return $life; |
| 328 |
} |
| 329 |
} |
| 330 |
|