| 1 |
<?php |
| 2 |
|
| 3 |
class RabbitLoader_21_Admin{ |
| 4 |
|
| 5 |
private static $purge_queue = null; |
| 6 |
public static $rabbitloader_cache_warnings = false; |
| 7 |
public static $admin_notice_shown = false; |
| 8 |
|
| 9 |
const PURGE_POST_CHANGE = "PURGE_POST"; |
| 10 |
const PURGE_THEME_CHANGE = "PURGE_THEME"; |
| 11 |
const PURGE_PLUG_CHANGE = "PURGE_PLUGIN"; |
| 12 |
const PURGE_MANUAL_USER = "PURGE_USER"; |
| 13 |
|
| 14 |
public static function addActions(){ |
| 15 |
add_action('admin_notices', 'RabbitLoader_21_Admin::admin_notices'); |
| 16 |
add_action('network_admin_notices', 'RabbitLoader_21_Admin::admin_notices' ); |
| 17 |
//add_action('admin_init', 'RabbitLoader_21_Admin::init'); |
| 18 |
add_action('shutdown', 'RabbitLoader_21_Admin::shutdown'); |
| 19 |
add_action('admin_menu', 'RabbitLoader_21_Admin::leftMenuOption' ); |
| 20 |
add_action('enqueue_block_editor_assets', 'RabbitLoader_21_Admin::postSubmitButton'); |
| 21 |
|
| 22 |
add_action('admin_enqueue_scripts', function(){ |
| 23 |
wp_enqueue_script('rabbitloader-index', RABBITLOADER_PLUG_URL . 'admin/js/index.js', ['jquery'], RABBITLOADER_PLUG_VERSION); |
| 24 |
wp_localize_script( 'rabbitloader-index', 'rabbitloader_local_vars', [ |
| 25 |
'admin_ajax' => admin_url( 'admin-ajax.php' ), |
| 26 |
'hostname' => get_option('rabbitloader_field_domain') |
| 27 |
]); |
| 28 |
}); |
| 29 |
|
| 30 |
//we need to listen for a few things to invalidate cache |
| 31 |
#reading settings changed (permalink, post per page) |
| 32 |
#post+page+comment+tag+category added, deleted, updated |
| 33 |
#theme+plugin activated deactivated |
| 34 |
add_action( 'save_post', function($post_ID, $post, $update){ |
| 35 |
RabbitLoader_21_Admin::onPostChange($post_ID, self::PURGE_POST_CHANGE); |
| 36 |
}, 10, 3 ); |
| 37 |
add_action( 'wp_insert_post', function($post_ID, $post, $update){ |
| 38 |
RabbitLoader_21_Admin::onPostChange($post_ID, self::PURGE_POST_CHANGE); |
| 39 |
}, 10, 3 ); |
| 40 |
add_action( 'draft_to_publish', function($post){ |
| 41 |
if(!empty($post)){ |
| 42 |
RabbitLoader_21_Admin::onPostChange($post->ID, self::PURGE_POST_CHANGE); |
| 43 |
} |
| 44 |
}, 10, 1 ); |
| 45 |
add_action( 'pending_to_publish', function($post){ |
| 46 |
if(!empty($post)){ |
| 47 |
RabbitLoader_21_Admin::onPostChange($post->ID, self::PURGE_POST_CHANGE); |
| 48 |
} |
| 49 |
}, 10, 1); |
| 50 |
add_action( 'transition_post_status', function($new_status, $old_status, $post){ |
| 51 |
//No need to purge if the post was not public before and its not even now |
| 52 |
if('publish' !== $old_status && 'publish' !== $new_status){ |
| 53 |
return; |
| 54 |
} |
| 55 |
RabbitLoader_21_Admin::onPostChange($post->ID, self::PURGE_POST_CHANGE); |
| 56 |
}, 10, 3); |
| 57 |
add_action( 'transition_comment_status', function($new_status, $old_status, $comment){ |
| 58 |
RabbitLoader_21_Admin::onPostChange($comment->comment_post_ID, self::PURGE_POST_CHANGE); |
| 59 |
}, 10, 3); |
| 60 |
add_action( 'comment_post', function($comment_ID, $comment_approved, $commentdata){ |
| 61 |
if($comment_approved==1){ |
| 62 |
$comment = get_comment( $comment_ID ); |
| 63 |
RabbitLoader_21_Admin::onPostChange($comment->comment_post_ID, self::PURGE_POST_CHANGE); |
| 64 |
} |
| 65 |
}, 10, 3); |
| 66 |
|
| 67 |
add_action( 'switch_theme', function(){ |
| 68 |
RabbitLoader_21_Admin::onPostChange('all', self::PURGE_THEME_CHANGE); |
| 69 |
}, 10, 0); |
| 70 |
// add_action( 'upgrader_process_complete', function(){ |
| 71 |
// RabbitLoader_21_Admin::onPostChange('all', self::PURGE_PLUG_CHANGE); |
| 72 |
// }, 10, 0); |
| 73 |
// add_action( 'activated_plugin', function(){ |
| 74 |
// RabbitLoader_21_Admin::onPostChange('all', self::PURGE_PLUG_CHANGE); |
| 75 |
// }, 10, 0); |
| 76 |
// add_action( 'deactivated_plugin', function(){ |
| 77 |
// RabbitLoader_21_Admin::onPostChange('all', self::PURGE_PLUG_CHANGE); |
| 78 |
// }, 10, 0); |
| 79 |
|
| 80 |
add_action( 'woocommerce_updated_product_stock', function($product_id){ |
| 81 |
RabbitLoader_21_Admin::onPostChange($product_id, self::PURGE_POST_CHANGE); |
| 82 |
}, 10, 1); |
| 83 |
add_action( 'woocommerce_updated_product_price', function($product_id){ |
| 84 |
RabbitLoader_21_Admin::onPostChange($product_id, self::PURGE_POST_CHANGE); |
| 85 |
}, 10, 1); |
| 86 |
add_action( 'woocommerce_rest_insert_product', function($post, $request, $creating) { |
| 87 |
RabbitLoader_21_Admin::onPostChange($post->ID, self::PURGE_POST_CHANGE); |
| 88 |
}, 10, 3); |
| 89 |
add_action( 'woocommerce_rest_insert_product_object', function($product, $request, $creating) { |
| 90 |
RabbitLoader_21_Admin::onPostChange($product->id, self::PURGE_POST_CHANGE); |
| 91 |
}, 10, 3); |
| 92 |
add_action( 'woocommerce_product_object_updated_props', function($product, $updated) { |
| 93 |
RabbitLoader_21_Admin::onPostChange($product->get_id(), self::PURGE_POST_CHANGE); |
| 94 |
}, 0, 2); |
| 95 |
|
| 96 |
add_action( 'wp_ajax_rabbitloader_ajax_purge', function(){ |
| 97 |
$response = [ |
| 98 |
'result'=>false |
| 99 |
]; |
| 100 |
if(!empty($_POST['post_id'])){ |
| 101 |
$post_id = RabbitLoader_21_Util_Core::get_param('post_id', true); |
| 102 |
RabbitLoader_21_Admin::onPostChange($post_id, self::PURGE_MANUAL_USER); |
| 103 |
$response = [ |
| 104 |
'result'=>true |
| 105 |
]; |
| 106 |
}else{ |
| 107 |
RabbitLoader_21_Admin::onPostChange('all', self::PURGE_MANUAL_USER); |
| 108 |
$response = [ |
| 109 |
'result'=>true |
| 110 |
]; |
| 111 |
} |
| 112 |
RabbitLoader_21_Core::sendJsonResponse($response); |
| 113 |
}); |
| 114 |
|
| 115 |
add_action( 'wp_ajax_rabbitloader_mode_change', function(){ |
| 116 |
$response = [ |
| 117 |
'result'=>true |
| 118 |
]; |
| 119 |
$private_mode = !empty($_POST['private_mode']); |
| 120 |
RabbitLoader_21_Core::getWpUserOption($user_options); |
| 121 |
$user_options['private_mode_val'] = $private_mode; |
| 122 |
$user_options['private_mode_ts'] = date('c'); |
| 123 |
RabbitLoader_21_Core::updateUserOption($user_options); |
| 124 |
RabbitLoader_21_Core::sendJsonResponse($response); |
| 125 |
|
| 126 |
}); |
| 127 |
add_action( 'wp_ajax_rabbitloader_ajax_cron', function(){self::deferred_exe();}); |
| 128 |
add_action( 'rl_site_connected', function(){self::rl_site_connected();}); |
| 129 |
//listeners for taxonomy changes |
| 130 |
} |
| 131 |
|
| 132 |
public static function init(){ |
| 133 |
} |
| 134 |
|
| 135 |
public static function leftMenuOption(){ |
| 136 |
self::get_warnings($notification_count, false); |
| 137 |
add_menu_page( |
| 138 |
'RabbitLoader', |
| 139 |
$notification_count ? sprintf('RabbitLoader <span class="awaiting-mod">%d</span>', $notification_count) : 'RabbitLoader', |
| 140 |
'manage_options', |
| 141 |
'rabbitloader', |
| 142 |
'RabbitLoader_21_Tab_Init::echoPluginPage', |
| 143 |
dirname(plugin_dir_url(__FILE__)) . '/images/icon_16.png', |
| 144 |
//'', |
| 145 |
20 |
| 146 |
); |
| 147 |
|
| 148 |
$page = RabbitLoader_21_Util_Core::get_param('page'); |
| 149 |
|
| 150 |
if($page=='rabbitloader'){ |
| 151 |
add_action( 'admin_head', 'admin_styles', 10, 1); |
| 152 |
function admin_styles($a) { |
| 153 |
echo '<link rel="stylesheet" href="'.RABBITLOADER_PLUG_URL . 'admin/css/bootstrap.v5.1.3.min.css'.'" type="text/css" media="all" /> |
| 154 |
<link rel="stylesheet" href="'.RABBITLOADER_PLUG_URL . 'admin/css/style.css'.'" type="text/css" media="all" />'; |
| 155 |
} |
| 156 |
} |
| 157 |
} |
| 158 |
|
| 159 |
|
| 160 |
protected static function isPluginActivated(){ |
| 161 |
|
| 162 |
return !empty(RabbitLoader_21_Core::getWpOptVal('api_token')) || (!empty(get_option('rabbitloader_field_apikey')) && !empty(get_option('rabbitloader_field_apisecret')) && !empty(get_option('rabbitloader_field_domain'))); |
| 163 |
} |
| 164 |
|
| 165 |
private static function load_purged_urls(){ |
| 166 |
RabbitLoader_21_Core::get_log_file('purge_urls_fl', $purge_urls_fl); |
| 167 |
if(file_exists($purge_urls_fl)){ |
| 168 |
$purge_queue = json_decode(file_get_contents($purge_urls_fl), true); |
| 169 |
} |
| 170 |
if(empty($purge_queue)){ |
| 171 |
$purge_queue = []; |
| 172 |
} |
| 173 |
return $purge_queue; |
| 174 |
} |
| 175 |
|
| 176 |
public static function onPostChange($post_id, $purge_source){ |
| 177 |
if(self::$purge_queue===null){ |
| 178 |
self::$purge_queue = self::load_purged_urls(); |
| 179 |
} |
| 180 |
if($post_id=='all'){ |
| 181 |
self::$purge_queue['all'] = true; |
| 182 |
}else{ |
| 183 |
//invalidate cache for individual post |
| 184 |
try{ |
| 185 |
if(wp_is_post_autosave($post_id) || wp_is_post_revision($post_id)){ |
| 186 |
//no need to purge cache for these posts as they are never displayed on website |
| 187 |
return; |
| 188 |
} |
| 189 |
}catch(\Throwable $e){ |
| 190 |
RabbitLoader_21_Core::on_exception($e); |
| 191 |
} |
| 192 |
|
| 193 |
if(empty(self::$purge_queue['post_ids'])){ |
| 194 |
self::$purge_queue['post_ids'] = []; |
| 195 |
} |
| 196 |
self::$purge_queue['post_ids'][$post_id] = true; |
| 197 |
|
| 198 |
$post_ancestors = get_post_ancestors($post_id); |
| 199 |
foreach ($post_ancestors as $parent_id) { |
| 200 |
self::$purge_queue['post_ids'][$parent_id] = true; |
| 201 |
} |
| 202 |
} |
| 203 |
self::$purge_queue['purge_source'] = $purge_source; |
| 204 |
} |
| 205 |
|
| 206 |
private static function execute_purge() |
| 207 |
{ |
| 208 |
$purge_queue = self::load_purged_urls(); |
| 209 |
$purge_urls_json = '{}'; |
| 210 |
RabbitLoader_21_Core::get_log_file('purge_urls_fl', $purge_urls_fl); |
| 211 |
RabbitLoader_21_Util_Core::fpc($purge_urls_fl, $purge_urls_json, WP_DEBUG); |
| 212 |
|
| 213 |
RabbitLoader_21_Core::getWpUserOption($user_options); |
| 214 |
|
| 215 |
$purge_source = empty($purge_queue['purge_source'])?'':$purge_queue['purge_source']; |
| 216 |
$clean_cache = strcmp($purge_source, self::PURGE_MANUAL_USER)===0 || !empty($user_options['purge_on_change']); |
| 217 |
|
| 218 |
if(!empty($purge_queue['all'])){ |
| 219 |
if($clean_cache){ |
| 220 |
RabbitLoader_21_Core::purge_all($purge_count, $purge_source, $tp_purge_count); |
| 221 |
} |
| 222 |
try{ |
| 223 |
RabbitLoader_21_Core::push_recent_posts($queued_offset, $queued_count, $published_count); |
| 224 |
}catch(\Throwable $e){ |
| 225 |
RabbitLoader_21_Core::on_exception($e); |
| 226 |
} |
| 227 |
}else if(!empty($purge_queue['post_ids'])){ |
| 228 |
$urls_to_purge = []; |
| 229 |
RabbitLoader_21_Core::get_common_cache_urls($urls_to_purge); |
| 230 |
foreach($purge_queue['post_ids'] as $post_ID=>$val){ |
| 231 |
$post_obj = get_post(intval($post_ID)); |
| 232 |
if(!$post_obj){ |
| 233 |
continue; |
| 234 |
} |
| 235 |
$post_canonical_url = wp_get_canonical_url($post_ID); |
| 236 |
$urls_to_purge[] = $post_canonical_url; |
| 237 |
RabbitLoader_21_Admin::get_all_taxonomies($post_ID, $post_obj->post_type, $tax_ids, $urls_to_purge); |
| 238 |
if($clean_cache){ |
| 239 |
RabbitLoader_21_TP::purge_post_id($post_ID, $tp_purge_count); |
| 240 |
} |
| 241 |
} |
| 242 |
$cache_missed_log = ''; |
| 243 |
$urls_to_purge = array_filter($urls_to_purge); |
| 244 |
foreach($urls_to_purge as $url){ |
| 245 |
if($clean_cache){ |
| 246 |
RabbitLoader_21_Core::purge_url_cache_local($url, $local_purge_count, $tp_purge_count); |
| 247 |
RabbitLoader_21_Core::purge_url_cache_local(rawurldecode($url), $local_purge_count, $tp_purge_count); |
| 248 |
} |
| 249 |
$cache_missed_log .= "\n".$url; |
| 250 |
} |
| 251 |
if(!empty($cache_missed_log)){ |
| 252 |
RabbitLoader_21_Util_Core::fac('cache_missed', $cache_missed_log, WP_DEBUG); |
| 253 |
} |
| 254 |
|
| 255 |
RabbitLoader_21_Core::callPostApi('purge/request', ['purge_source'=>$purge_source, 'urls'=>array_slice($urls_to_purge,0, 100)], $apiError, $apiMessage); |
| 256 |
do_action('rl_purge_request_complete', $urls_to_purge, $apiError); |
| 257 |
} |
| 258 |
} |
| 259 |
|
| 260 |
public static function shutdown() |
| 261 |
{ |
| 262 |
if(!empty(self::$purge_queue)){ |
| 263 |
$purge_urls_json = json_encode(self::$purge_queue); |
| 264 |
RabbitLoader_21_Core::get_log_file('purge_urls_fl', $purge_urls_fl); |
| 265 |
RabbitLoader_21_Util_Core::fpc($purge_urls_fl, $purge_urls_json, WP_DEBUG); |
| 266 |
} |
| 267 |
} |
| 268 |
|
| 269 |
private static function get_all_taxonomies($post_ID, $post_type, &$tax_ids, &$tax_urls) { |
| 270 |
$tax_ids = array(); |
| 271 |
$taxonomies = get_object_taxonomies($post_type); |
| 272 |
foreach ($taxonomies as $taxonomy) { |
| 273 |
$taxonomy_data = get_taxonomy($taxonomy); |
| 274 |
if ($taxonomy_data instanceof WP_Taxonomy && $taxonomy_data->public===false) { |
| 275 |
continue; |
| 276 |
} |
| 277 |
$terms = get_the_terms( $post_ID, $taxonomy ); |
| 278 |
|
| 279 |
if (empty($terms) || is_wp_error($terms)) { |
| 280 |
continue; |
| 281 |
}else{ |
| 282 |
foreach ($terms as $term) { |
| 283 |
if(!empty($term)){ |
| 284 |
$tax_ids[] = $term->term_taxonomy_id; |
| 285 |
$tax_url = get_term_link($term); |
| 286 |
if(!is_wp_error($tax_url) && !empty($tax_url)){ |
| 287 |
$tax_urls[] = $tax_url; |
| 288 |
} |
| 289 |
} |
| 290 |
} |
| 291 |
} |
| 292 |
} |
| 293 |
|
| 294 |
$other_urls_to_merge = []; |
| 295 |
try{ |
| 296 |
$other_urls_to_merge[] = get_author_posts_url(get_post_field('post_author', $post_ID)); |
| 297 |
$other_urls_to_merge[] = get_author_feed_link(get_post_field('post_author', $post_ID)); |
| 298 |
$other_urls_to_merge[] = get_post_type_archive_link($post_type); |
| 299 |
$other_urls_to_merge[] = get_post_type_archive_feed_link($post_type); |
| 300 |
$other_urls_to_merge[] = get_permalink($post_ID); |
| 301 |
$other_urls_to_merge[] = get_post_comments_feed_link($post_ID); |
| 302 |
}catch(Throwable $e){ |
| 303 |
RabbitLoader_21_Core::on_exception($e); |
| 304 |
} |
| 305 |
if(!empty($other_urls_to_merge)){ |
| 306 |
foreach($other_urls_to_merge as $other_url){ |
| 307 |
if(!empty($other_url) && !is_wp_error($other_url)){ |
| 308 |
$tax_urls[] = $other_url; |
| 309 |
} |
| 310 |
} |
| 311 |
} |
| 312 |
} |
| 313 |
|
| 314 |
public static function admin_notices(){ |
| 315 |
|
| 316 |
try{ |
| 317 |
$page = RabbitLoader_21_Util_Core::get_param('page'); |
| 318 |
|
| 319 |
if(self::$admin_notice_shown || ($page=='rabbitloader')){ |
| 320 |
return; |
| 321 |
} |
| 322 |
|
| 323 |
self::$admin_notice_shown = true; |
| 324 |
|
| 325 |
$plug_url = admin_url("admin.php?page=rabbitloader"); |
| 326 |
|
| 327 |
if(!self::isPluginActivated()){ |
| 328 |
echo ' |
| 329 |
<div class="notice notice-error is-dismissible"><p>'; |
| 330 |
printf(RabbitLoader_21_Util_WP::__('RabbitLoader is disconnected. Your pages are not optimized. <a href="%s">Click here to connect</a>'), $plug_url); |
| 331 |
echo '</p></div>'; |
| 332 |
}else{ |
| 333 |
self::get_warnings($notification_count, false); |
| 334 |
|
| 335 |
if($notification_count>0){ |
| 336 |
echo '<div class="notice notice-error is-dismissible"><p>'; |
| 337 |
printf(RabbitLoader_21_Util_WP::_n( 'RabbitLoader has %d warning which is affecting your website\'s optimizations. <a href="%s">check details</a>', 'RabbitLoader has %d warnings which may affect your website\'s optimizations. <a href="%s">check details</a>', $notification_count), $notification_count, $plug_url); |
| 338 |
echo '</p></div>'; |
| 339 |
} |
| 340 |
} |
| 341 |
}catch(Throwable $e){ |
| 342 |
RabbitLoader_21_Core::on_exception($e); |
| 343 |
} |
| 344 |
} |
| 345 |
|
| 346 |
protected static function get_warnings(&$count, $print){ |
| 347 |
|
| 348 |
if(self::$rabbitloader_cache_warnings === false){ |
| 349 |
|
| 350 |
self::$rabbitloader_cache_warnings = []; |
| 351 |
|
| 352 |
$otherConflictPluginMessages = RabbitLoader_21_Conflicts::getMessages(); |
| 353 |
foreach ($otherConflictPluginMessages as $plugMessage) { |
| 354 |
self::$rabbitloader_cache_warnings[] = $plugMessage; |
| 355 |
} |
| 356 |
|
| 357 |
$adv_cache_msg = RabbitLoader_21_Util_WP::__("The file /wp-content/advanced-cache.php is not writable. Please make sure that the PHP script has write access to the /wp-content/ directory and refresh this page to make RabbitLoader work efficiently."); |
| 358 |
|
| 359 |
if(!defined('RABBITLOADER_AC_ACTIVE') || (RABBITLOADER_PLUG_VERSION!=RABBITLOADER_AC_PLUG_VERSION)){ |
| 360 |
$aac_code = self::activate_advanced_cache(); |
| 361 |
if($aac_code==4){ |
| 362 |
self::$rabbitloader_cache_warnings[] = $adv_cache_msg; |
| 363 |
} |
| 364 |
} |
| 365 |
|
| 366 |
$cache_warning = RabbitLoader_21_Util_WP::__("The file /wp-config.php is not writable. Please make sure the file is writable or set WP_CACHE value to true to make RabbitLoader work efficiently."); |
| 367 |
if ((!defined("WP_CACHE") || !WP_CACHE)) { |
| 368 |
if (!self::update_wp_config_const('WP_CACHE', 'true')) { |
| 369 |
self::$rabbitloader_cache_warnings[] = $cache_warning; |
| 370 |
$cache_warning = ''; |
| 371 |
} |
| 372 |
} |
| 373 |
|
| 374 |
if (!self::create_cache_folder()) { |
| 375 |
self::$rabbitloader_cache_warnings[] = RabbitLoader_21_Util_WP::__("The folder /wp-content/ is not writable. Please make sure that the PHP script has write access to the /wp-content/ directory and refresh this page. This is required to store the cached content."); |
| 376 |
} |
| 377 |
|
| 378 |
RabbitLoader_21_Core::getWpOption($rl_wp_options); |
| 379 |
if(!empty($rl_wp_options['rl_hb_messages'])){ |
| 380 |
foreach($rl_wp_options['rl_hb_messages'] as $message){ |
| 381 |
if(!empty($message['fd'])){ |
| 382 |
self::$rabbitloader_cache_warnings[] = RabbitLoader_21_Util_WP::__($message['fd']); |
| 383 |
} |
| 384 |
} |
| 385 |
} |
| 386 |
if(!empty($rl_wp_options['rl_latest_plugin_v'])){ |
| 387 |
if(version_compare(RABBITLOADER_PLUG_VERSION, $rl_wp_options['rl_latest_plugin_v'])==-1){ |
| 388 |
self::$rabbitloader_cache_warnings[] = RabbitLoader_21_Util_WP::__("You are using an outdated version of RabbitLoader plugin. Please update it for a better experience."); |
| 389 |
} |
| 390 |
} |
| 391 |
} |
| 392 |
|
| 393 |
$count = count(self::$rabbitloader_cache_warnings); |
| 394 |
|
| 395 |
if($print){ |
| 396 |
foreach(self::$rabbitloader_cache_warnings as $message){ |
| 397 |
// echo '<div class="alert alert-danger" role="alert">'; |
| 398 |
// _e($message, RABBITLOADER_TEXT_DOMAIN); |
| 399 |
// echo '</div>'; |
| 400 |
echo '<div class="notice notice-error"><p><b>'; |
| 401 |
RabbitLoader_21_Util_WP::_e('Warning'); echo ': </b>'; |
| 402 |
_e($message, RABBITLOADER_TEXT_DOMAIN); |
| 403 |
echo '</div>'; |
| 404 |
} |
| 405 |
} |
| 406 |
} |
| 407 |
|
| 408 |
private static function create_cache_folder(){ |
| 409 |
|
| 410 |
$dirs = [ |
| 411 |
RABBITLOADER_CACHE_DIR, |
| 412 |
RabbitLoader_21_Util_WP::get_cache_dir('short'), |
| 413 |
RabbitLoader_21_Util_WP::get_cache_dir('long') |
| 414 |
]; |
| 415 |
|
| 416 |
$dirs_exists = 0; |
| 417 |
|
| 418 |
try{ |
| 419 |
foreach($dirs as $dir){ |
| 420 |
if (!file_exists($dir)){ |
| 421 |
if(@mkdir($dir, 0755, true)){ |
| 422 |
++$dirs_exists; |
| 423 |
} |
| 424 |
}else{ |
| 425 |
++$dirs_exists; |
| 426 |
} |
| 427 |
} |
| 428 |
if(count($dirs)!=$dirs_exists){ |
| 429 |
return false; |
| 430 |
} |
| 431 |
|
| 432 |
//add htaccess file for security |
| 433 |
$htaccess_loc = RABBITLOADER_CACHE_DIR.DIRECTORY_SEPARATOR.".htaccess"; |
| 434 |
if (!file_exists($htaccess_loc)) { |
| 435 |
$htaccess_content = "deny from all"; |
| 436 |
RabbitLoader_21_Util_Core::fpc($htaccess_loc, $htaccess_content, WP_DEBUG); |
| 437 |
} |
| 438 |
|
| 439 |
return file_exists($htaccess_loc); |
| 440 |
|
| 441 |
}catch(Throwable $e){ |
| 442 |
RabbitLoader_21_Core::on_exception($e); |
| 443 |
} |
| 444 |
} |
| 445 |
|
| 446 |
public static function activate_advanced_cache(){ |
| 447 |
|
| 448 |
try{ |
| 449 |
if (!empty(RabbitLoader_21_Conflicts::getMessages())){ |
| 450 |
return 1; |
| 451 |
} |
| 452 |
|
| 453 |
if(!self::create_cache_folder()){ |
| 454 |
return 2; |
| 455 |
} |
| 456 |
|
| 457 |
$adv_cache_sample = RABBITLOADER_PLUG_DIR . "advanced-cache.php"; |
| 458 |
$file_updated = false; |
| 459 |
if (file_exists($adv_cache_sample)) { |
| 460 |
$adv_cache_contents = file_get_contents($adv_cache_sample); |
| 461 |
$adv_cache_contents = str_replace("%%RABBITLOADER_AC_ABSPATH%%", ABSPATH, $adv_cache_contents); |
| 462 |
$adv_cache_contents = str_replace("%%RABBITLOADER_AC_PLUG_DIR%%", RABBITLOADER_PLUG_DIR, $adv_cache_contents); |
| 463 |
$adv_cache_contents = str_replace("%%RABBITLOADER_AC_LOGGED_IN_COOKIE%%", LOGGED_IN_COOKIE, $adv_cache_contents); |
| 464 |
$adv_cache_contents = str_replace("%%RABBITLOADER_AC_CACHE_DIR%%", RABBITLOADER_CACHE_DIR, $adv_cache_contents); |
| 465 |
$adv_cache_contents = str_replace("%%RABBITLOADER_AC_PLUG_VERSION%%", RABBITLOADER_PLUG_VERSION, $adv_cache_contents); |
| 466 |
$adv_cache_contents = str_replace("%%RABBITLOADER_AC_PLUG_ENV%%", RABBITLOADER_PLUG_ENV, $adv_cache_contents); |
| 467 |
|
| 468 |
$advanced_cache_file = WP_CONTENT_DIR . DIRECTORY_SEPARATOR.'advanced-cache.php'; |
| 469 |
$file_updated = RabbitLoader_21_Util_Core::fpc($advanced_cache_file, $adv_cache_contents, WP_DEBUG); |
| 470 |
} |
| 471 |
|
| 472 |
if($file_updated){ |
| 473 |
self::update_wp_config_const('WP_CACHE', 'true'); |
| 474 |
return 3; |
| 475 |
}else{ |
| 476 |
return 4; |
| 477 |
} |
| 478 |
}catch(Throwable $e){ |
| 479 |
RabbitLoader_21_Core::on_exception($e); |
| 480 |
} |
| 481 |
} |
| 482 |
|
| 483 |
private static function update_wp_config_const($const_name, $const_val){ |
| 484 |
$wp_config_path = RabbitLoader_21_Util_WP::get_wp_config(); |
| 485 |
//check if config file is writable |
| 486 |
if ( empty($wp_config_path) || !is_writable($wp_config_path)) { |
| 487 |
//echo 'rl_not_writable__'; |
| 488 |
return; |
| 489 |
} |
| 490 |
|
| 491 |
$lines = file( $wp_config_path ); |
| 492 |
$last_line = count($lines) - 1; |
| 493 |
|
| 494 |
$new_file = array(); |
| 495 |
$const_added = false; |
| 496 |
foreach ( $lines as $current_line=>$line_content ) { |
| 497 |
//check if constant is already defined |
| 498 |
if ( preg_match( "/define\(\s*'{$const_name}'/i", $line_content ) ) { |
| 499 |
$const_added = true; |
| 500 |
$new_file[] = "define( '{$const_name}', {$const_val} ); //RabbitLoader\n"; |
| 501 |
continue; //dont't break here, its a complete file rewrite |
| 502 |
} |
| 503 |
|
| 504 |
$thatsAllLine = (preg_match( "/\/\* That's all, stop editing!.*/i", $line_content )); //constants should be before this line. |
| 505 |
$isLast = ($thatsAllLine && !defined($const_name)) || ($last_line==$current_line); |
| 506 |
|
| 507 |
// If we reach the end and no define - add it. |
| 508 |
if (empty($const_added) && $isLast) { |
| 509 |
$new_file[] = "define( '{$const_name}', {$const_val} ); //RabbitLoader\n"; |
| 510 |
} |
| 511 |
|
| 512 |
$new_file[] = $line_content; |
| 513 |
} |
| 514 |
$file_contents = implode("", $new_file); |
| 515 |
return RabbitLoader_21_Util_Core::fpc($wp_config_path, $file_contents, WP_DEBUG); |
| 516 |
} |
| 517 |
|
| 518 |
public static function plugin_deactivate(){ |
| 519 |
try{ |
| 520 |
self::update_wp_config_const('WP_CACHE', 'false'); |
| 521 |
$advanced_cache_file = WP_CONTENT_DIR . DIRECTORY_SEPARATOR.'advanced-cache.php'; |
| 522 |
if (file_exists($advanced_cache_file)) { //during uninstall RABBITLOADER_AC_ACTIVE will not be there |
| 523 |
$adv_cache_contents = ""; |
| 524 |
$file_updated = RabbitLoader_21_Util_Core::fpc($advanced_cache_file, $adv_cache_contents, WP_DEBUG); |
| 525 |
} |
| 526 |
}catch(Throwable $e){ |
| 527 |
RabbitLoader_21_Core::on_exception($e); |
| 528 |
} |
| 529 |
} |
| 530 |
|
| 531 |
public static function plugin_uninstall(){ |
| 532 |
self::plugin_deactivate(); |
| 533 |
|
| 534 |
$post_data['uninstall'] = 1; |
| 535 |
$http = RabbitLoader_21_Core::callPOSTAPI('domain/heartbeat', $post_data, $apiError, $apiMessage); |
| 536 |
|
| 537 |
RabbitLoader_21_Core::cleanAllCachedFiles('long'); |
| 538 |
|
| 539 |
$ourOptions = array('rabbitloader_field_apikey', 'rabbitloader_field_apisecret', 'rabbitloader_field_domain','rl_optimizer_engine_version', 'rabbit_loader_wp_options', 'rabbit_loader_user_options'); |
| 540 |
|
| 541 |
foreach ($ourOptions as $optionName) { |
| 542 |
delete_option($optionName); |
| 543 |
} |
| 544 |
} |
| 545 |
|
| 546 |
public static function postSubmitButton(){ |
| 547 |
wp_enqueue_script('rabbitloader-editor', RABBITLOADER_PLUG_URL . 'admin/js/editor.js', ['wp-edit-post', 'wp-plugins', 'wp-i18n', 'wp-element'], RABBITLOADER_PLUG_VERSION); |
| 548 |
} |
| 549 |
|
| 550 |
private static function rl_site_connected(){ |
| 551 |
try{ |
| 552 |
RabbitLoader_21_Core::push_recent_posts($queued_offset, $queued_count, $published_count); |
| 553 |
RabbitLoader_21_TP::purge_all($tp_purge_count); |
| 554 |
}catch(\Throwable $e){ |
| 555 |
RabbitLoader_21_Core::on_exception($e); |
| 556 |
} |
| 557 |
} |
| 558 |
|
| 559 |
public static function settings_link($links){ |
| 560 |
$url_settings = esc_url( add_query_arg( |
| 561 |
'page', |
| 562 |
'rabbitloader', |
| 563 |
get_admin_url() . 'admin.php' |
| 564 |
)); |
| 565 |
$link = "<a href='$url_settings'>" . __( 'Settings' ) . '</a>'; |
| 566 |
array_push($links, $link); |
| 567 |
$url_kb = esc_url('https://rabbitloader.com/kb/'); |
| 568 |
$link = "<a target='_blank' href='$url_kb'>" . __( 'Documentation' ) . '</a>'; |
| 569 |
array_push($links, $link); |
| 570 |
return $links; |
| 571 |
} |
| 572 |
|
| 573 |
private static function deferred_exe(){ |
| 574 |
try{ |
| 575 |
self::execute_purge(); |
| 576 |
RabbitLoader_21_Core::clean_orphaned_cached_files(RabbitLoader_21_Util_Core::isDev()?5:50); |
| 577 |
}catch(\Throwable $e){ |
| 578 |
RabbitLoader_21_Core::on_exception($e); |
| 579 |
} |
| 580 |
} |
| 581 |
} |
| 582 |
|
| 583 |
?> |