| 1 |
<?php |
| 2 |
|
| 3 |
class RabbitLoader_21_Tab_Init extends RabbitLoader_21_Admin |
| 4 |
{ |
| 5 |
|
| 6 |
public static function echoPluginPage() |
| 7 |
{ |
| 8 |
if (!current_user_can('manage_options')) { |
| 9 |
#the use is not authorized to manage options |
| 10 |
return; |
| 11 |
} |
| 12 |
|
| 13 |
wp_enqueue_style('rabbitloader-inter', 'https://fonts.googleapis.com/css2?family=Manrope:wght@200;300;400;500;600;700;800&display=swap'); |
| 14 |
|
| 15 |
// self::rlTabJS(); |
| 16 |
|
| 17 |
?> |
| 18 |
|
| 19 |
<div class="wrap rl-admin-wrap" id="rabbitloader-admin-wrap"> |
| 20 |
<?php |
| 21 |
try { |
| 22 |
$tab = self::decideTabToShow($isConnected); |
| 23 |
echo '<div id="rabbitloader-root"></div>'; |
| 24 |
if ($isConnected) { |
| 25 |
wp_enqueue_script( |
| 26 |
'rabbitloader-chat-widget', |
| 27 |
RABBITLOADER_PLUG_URL . 'admin/js/chat-widget.js', |
| 28 |
[], |
| 29 |
RABBITLOADER_PLUG_VERSION, |
| 30 |
true |
| 31 |
); |
| 32 |
|
| 33 |
// echo '<h1>' . esc_html(get_admin_page_title()) . '</h1>'; |
| 34 |
|
| 35 |
// self::echoTabBar($tab); |
| 36 |
|
| 37 |
echo '<div id="mfe_rl-chat"></div>'; |
| 38 |
|
| 39 |
// if ($tab == 'exclusions') { |
| 40 |
// RabbitLoader_21_Tab_Exclusions::init(); |
| 41 |
// RabbitLoader_21_Tab_Exclusions::echoMainContent(); |
| 42 |
// } else if ($tab == 'settings') { |
| 43 |
// RabbitLoader_21_Tab_Settings::init(); |
| 44 |
// RabbitLoader_21_Tab_Settings::echoMainContent(); |
| 45 |
// } else if ($tab == 'help') { |
| 46 |
// RabbitLoader_21_Tab_Help::init(); |
| 47 |
// RabbitLoader_21_Tab_Help::echoMainContent(); |
| 48 |
// } else if ($tab == 'log') { |
| 49 |
// RabbitLoader_21_Tab_Log::init(); |
| 50 |
// RabbitLoader_21_Tab_Log::echoMainContent(); |
| 51 |
// } else if ($tab == 'urls') { |
| 52 |
// RabbitLoader_21_Tab_Urls::init(); |
| 53 |
// RabbitLoader_21_Tab_Urls::echoMainContent(); |
| 54 |
// } else if ($tab == 'usage') { |
| 55 |
// RabbitLoader_21_Tab_Usage::init(); |
| 56 |
// RabbitLoader_21_Tab_Usage::echoMainContent(); |
| 57 |
// // } else if ($tab == 'images') { |
| 58 |
// // RabbitLoader_21_Tab_Images::init(); |
| 59 |
// // RabbitLoader_21_Tab_Images::echoMainContent(); |
| 60 |
// } else if ($tab == 'css') { |
| 61 |
// RabbitLoader_21_Tab_Css::init(); |
| 62 |
// RabbitLoader_21_Tab_Css::echoMainContent(); |
| 63 |
// } else { |
| 64 |
// //anything not defined will show the home tab |
| 65 |
// RabbitLoader_21_Tab_Home::init(); |
| 66 |
// RabbitLoader_21_Tab_Home::echoMainContent(); |
| 67 |
// } |
| 68 |
} else { |
| 69 |
// RabbitLoader_21_Tab_Settings::init(); |
| 70 |
// RabbitLoader_21_Tab_Settings::echoMainContent(); |
| 71 |
} |
| 72 |
} catch (Throwable $e) { |
| 73 |
RabbitLoader_21_Core::on_exception($e); |
| 74 |
} |
| 75 |
?> |
| 76 |
</div> |
| 77 |
<?php |
| 78 |
} |
| 79 |
/** |
| 80 |
* Echo the tabs in our plugin's admin page |
| 81 |
*/ |
| 82 |
private static function echoTabBar($activeTab = 'home') |
| 83 |
{ |
| 84 |
|
| 85 |
self::get_warnings($messages_count, true); |
| 86 |
|
| 87 |
$page = RabbitLoader_21_Util_Core::get_param('page'); |
| 88 |
$tabs = [ |
| 89 |
'home' => 'Home', |
| 90 |
'urls' => 'URLs', |
| 91 |
'css' => 'Critical CSS', |
| 92 |
'usage' => 'Usage', |
| 93 |
'settings' => 'Settings', |
| 94 |
'exclusions' => 'Exclusions', |
| 95 |
'log' => 'Notifications', |
| 96 |
'help' => 'Help' |
| 97 |
]; |
| 98 |
echo '<div id="icon-themes" class="icon32"><br></div>'; |
| 99 |
echo '<h2 class="nav-tab-wrapper">'; |
| 100 |
foreach ($tabs as $tab => $name) { |
| 101 |
$url = esc_url(add_query_arg(array('tab' => $tab, 'page' => $page))); |
| 102 |
$class = ($tab == $activeTab) ? ' nav-tab-active' : ''; |
| 103 |
echo "<a class='nav-tab $class' href='$url'>"; |
| 104 |
RL21UtilWP::_e($name); |
| 105 |
echo "</a>"; |
| 106 |
} |
| 107 |
echo '</h2>'; |
| 108 |
//self::pass_keys_to_js(); |
| 109 |
} |
| 110 |
|
| 111 |
private static function decideTabToShow(&$isConnected) |
| 112 |
{ |
| 113 |
$tab = RabbitLoader_21_Util_Core::get_param('tab'); |
| 114 |
|
| 115 |
if (!self::isPluginActivated()) { |
| 116 |
$isConnected = false; |
| 117 |
if ($tab != 'help') { |
| 118 |
$tab = 'settings'; |
| 119 |
} |
| 120 |
} else { |
| 121 |
$isConnected = true; |
| 122 |
} |
| 123 |
if (empty($tab)) { |
| 124 |
$tab = 'home'; |
| 125 |
} |
| 126 |
return $tab; |
| 127 |
} |
| 128 |
|
| 129 |
protected static function getTabUrl($tab_key) |
| 130 |
{ |
| 131 |
$page = RabbitLoader_21_Util_Core::get_param('page'); |
| 132 |
return esc_url(add_query_arg(array('tab' => $tab_key, 'page' => $page))); |
| 133 |
} |
| 134 |
|
| 135 |
protected static function &getOverviewData(&$apiError = '', &$apiMessage = '') |
| 136 |
{ |
| 137 |
$overview = get_transient('rabbitloader_trans_overview_data'); |
| 138 |
if (!empty($overview)) { |
| 139 |
return $overview; |
| 140 |
} |
| 141 |
|
| 142 |
$overview = [ |
| 143 |
'score_circle_best' => 0, |
| 144 |
'score_circle_avg' => 0, |
| 145 |
'published_url_count' => 0, |
| 146 |
'canonical_url_count' => 0, |
| 147 |
'optimized_url_count' => 0, |
| 148 |
'optimized_url_per' => 0, |
| 149 |
'pv_used' => 0, |
| 150 |
'pv_quota' => 0, |
| 151 |
'pv_remaining' => 0, |
| 152 |
'pp_used' => 0, |
| 153 |
'plan_title' => 0, |
| 154 |
'plan_end_date' => 0, |
| 155 |
'image_comp_avg_cp' => 0, |
| 156 |
'css_size_pp' => 0, |
| 157 |
'home_page_url_id' => '' |
| 158 |
]; |
| 159 |
|
| 160 |
$http = RabbitLoader_21_Core::callGETAPI('report/overview', $apiError, $apiMessage); |
| 161 |
if ($apiError) { |
| 162 |
return $overview; |
| 163 |
} |
| 164 |
|
| 165 |
if (!empty($http['body']['data']['domain_details']['host'])) { |
| 166 |
//CODENAME#401041 |
| 167 |
$dbHost = parse_url(get_site_url())['host']; |
| 168 |
$remoteHost = $http['body']['data']['domain_details']['host']; |
| 169 |
if ((strcmp($dbHost, $remoteHost) !== 0) && (strcmp($dbHost, "www." . $remoteHost) !== 0)) { |
| 170 |
RabbitLoader_21_Core::update_api_tokens('', '', '', "dbHost $dbHost when remoteHost $remoteHost"); |
| 171 |
$apiError = "Invalid registration"; |
| 172 |
$apiMessage = "INVALID_DOMAIN"; |
| 173 |
return $overview; |
| 174 |
} |
| 175 |
} |
| 176 |
|
| 177 |
$published_url_count = RabbitLoader_21_Core::get_published_count(); |
| 178 |
$overview['published_url_count'] = intval($published_url_count); |
| 179 |
|
| 180 |
if (!empty($http['body']['data']['speed_score']['max_score'])) { |
| 181 |
$overview['score_circle_best'] = intval($http['body']['data']['speed_score']['max_score'] * 100); |
| 182 |
} |
| 183 |
|
| 184 |
if (!empty($http['body']['data']['speed_score']['avg_score'])) { |
| 185 |
$overview['score_circle_avg'] = intval($http['body']['data']['speed_score']['avg_score'] * 100); |
| 186 |
} |
| 187 |
|
| 188 |
$speed_score = isset($http['body']['data']['speed_score']) && is_array($http['body']['data']['speed_score']) ? $http['body']['data']['speed_score'] : []; |
| 189 |
self::applyOverviewCounts($overview, $speed_score, $published_url_count, RabbitLoader_21_Core::getCacheCount()); |
| 190 |
|
| 191 |
if (!empty($http['body']['data']['bill']['end_date'])) { |
| 192 |
$overview['plan_end_date'] = $http['body']['data']['bill']['end_date']; |
| 193 |
} |
| 194 |
if (!empty($http['body']['data']['bill']['usage']['pageviews_ctr'])) { |
| 195 |
$overview['pv_used'] = $http['body']['data']['bill']['usage']['pageviews_ctr']; |
| 196 |
} |
| 197 |
if (!empty($http['body']['data']['plan_limits']['pageviews_ctr'])) { |
| 198 |
$overview['pv_quota'] = round($http['body']['data']['plan_limits']['pageviews_ctr'], 0); |
| 199 |
} |
| 200 |
if (!empty($http['body']['data']['plan_details']['title'])) { |
| 201 |
$overview['plan_title'] = $http['body']['data']['plan_details']['title']; |
| 202 |
} |
| 203 |
if (!empty($http['body']['data']['css_size_pp'])) { |
| 204 |
$overview['css_size_pp'] = round($http['body']['data']['css_size_pp'], 1); |
| 205 |
} |
| 206 |
if (!empty($http['body']['data']['image_comp_avg_cp'])) { |
| 207 |
$overview['image_comp_avg_cp'] = round($http['body']['data']['image_comp_avg_cp'], 1); |
| 208 |
} |
| 209 |
$overview['pv_remaining'] = $overview['pv_quota'] - $overview['pv_used']; |
| 210 |
$overview['pp_used'] = $overview['pv_quota'] > 0 ? round(($overview['pv_used'] / $overview['pv_quota']) * 100, 0) : 0; |
| 211 |
|
| 212 |
$optimized_url_per = empty($overview['canonical_url_count']) ? 0 : ($overview['optimized_url_count'] / $overview['canonical_url_count']) * 100; |
| 213 |
$overview['optimized_url_per'] = round($optimized_url_per, 1); |
| 214 |
|
| 215 |
if (!empty($http['body']['data']['home_page_url_id'])) { |
| 216 |
$overview['home_page_url_id'] = $http['body']['data']['home_page_url_id']; |
| 217 |
} |
| 218 |
|
| 219 |
RabbitLoader_21_Core::getWpOption($rl_wp_options); |
| 220 |
if (!empty($http['body']['data']['rl_latest_plugin_v'])) { |
| 221 |
$rl_wp_options['rl_latest_plugin_v'] = $http['body']['data']['rl_latest_plugin_v']; |
| 222 |
} |
| 223 |
if (empty($rl_wp_options['rl_varnish'])) { |
| 224 |
$rl_wp_options['rl_varnish'] = self::check_varnish(2) ? 1 : -1; |
| 225 |
} |
| 226 |
RabbitLoader_21_Core::updateWpOption($rl_wp_options); |
| 227 |
|
| 228 |
set_transient('rabbitloader_trans_overview_data', $overview, 60); |
| 229 |
return $overview; |
| 230 |
} |
| 231 |
|
| 232 |
protected static function applyOverviewCounts(&$overview, $speed_score, $published_url_count, $fallback_optimized_url_count) |
| 233 |
{ |
| 234 |
$overview['published_url_count'] = max(0, intval($published_url_count)); |
| 235 |
$overview['canonical_url_count'] = self::readOverviewCount($speed_score, 'canonical_url_count'); |
| 236 |
|
| 237 |
if (array_key_exists('optimized_url_count', $speed_score) && $speed_score['optimized_url_count'] !== null && $speed_score['optimized_url_count'] !== '') { |
| 238 |
$overview['optimized_url_count'] = max(0, intval($speed_score['optimized_url_count'])); |
| 239 |
return; |
| 240 |
} |
| 241 |
|
| 242 |
$overview['optimized_url_count'] = max(0, intval($fallback_optimized_url_count)); |
| 243 |
if ($overview['optimized_url_count'] > $overview['canonical_url_count']) { |
| 244 |
//local cache might have removed URLs |
| 245 |
$overview['optimized_url_count'] = $overview['canonical_url_count']; |
| 246 |
} |
| 247 |
} |
| 248 |
|
| 249 |
protected static function readOverviewCount($source, $key) |
| 250 |
{ |
| 251 |
if (!is_array($source) || !array_key_exists($key, $source) || $source[$key] === null || $source[$key] === '') { |
| 252 |
return 0; |
| 253 |
} |
| 254 |
return max(0, intval($source[$key])); |
| 255 |
} |
| 256 |
|
| 257 |
protected static function quota_used_box(&$overview, $show_arrow) |
| 258 |
{ |
| 259 |
?> |
| 260 |
<div class="rl-mfe-card h-100 d-flex flex-column justify-content-between" title="<?php RL21UtilWP::_e(sprintf('You have consumed %s page-views out of %s page-views monthly quota available in your current plan.', intval($overview['pv_used']), $overview['pv_quota'])); ?>"> |
| 261 |
<h4 class="<?php echo $overview['pp_used'] >= 99 ? "text-danger" : ""; ?>"> |
| 262 |
<?php echo intval($overview['pv_used']); ?>/<small style="font-size:14px;"><?php echo $overview['pv_quota']; ?> </small> |
| 263 |
</h4> |
| 264 |
<a class="rl-dash-link" href="<?php echo self::getTabUrl('usage'); ?>"> |
| 265 |
<span class="text-secondary mt-2"><?php RL21UtilWP::_e(sprintf('PageViews Used (%s Plan)', $overview['plan_title'])); ?> |
| 266 |
<span class="dashicons dashicons-arrow-right-alt mt-05 <?php echo !$show_arrow ? 'd-none' : ''; ?>"></span> |
| 267 |
</span> |
| 268 |
</a> |
| 269 |
</div> |
| 270 |
<?php |
| 271 |
} |
| 272 |
|
| 273 |
protected static function quota_remaining_box(&$overview) |
| 274 |
{ |
| 275 |
$gb_remaining_nz = $overview['pv_remaining'] < 0 ? 0 : intval($overview['pv_remaining']); |
| 276 |
?> |
| 277 |
<div class="rl-mfe-card" title="<?php RL21UtilWP::_e(sprintf('You have %s page-views available out of %s page-views monthly quota in the current billing cycle.', $gb_remaining_nz, $overview['pv_quota'])); ?>"> |
| 278 |
<h4 class="<?php echo $overview['pp_used'] >= 99 ? "text-danger" : ""; ?>"><?php echo $gb_remaining_nz; ?>/<small style="font-size:14px;"><?php echo $overview['pv_quota']; ?> </small></h4> |
| 279 |
<span class="text-secondary mt-2"><?php RL21UtilWP::_e('PageViews Remaining'); ?></span> <a target="_blank" href="<?php echo self::getUpgradeLink('quota_remaining', $overview['plan_title']); ?>" class="badge rl-bg-primary text-white" style="text-decoration: none;"><?php RL21UtilWP::_e('GET MORE'); ?></a> |
| 280 |
</div> |
| 281 |
<?php |
| 282 |
} |
| 283 |
|
| 284 |
protected static function urls_detected_box(&$overview, $show_arrow) |
| 285 |
{ |
| 286 |
$title = RL21UtilWP::__(sprintf('Optimized cache exists for %d hot URL(s) out of total %d URL(s) detected', $overview['optimized_url_count'], $overview['canonical_url_count'])); |
| 287 |
?> |
| 288 |
<div class="rl-mfe-card tpopup" title="<?php echo $title; ?>"> |
| 289 |
<h4 class=""><?php echo $overview['optimized_url_count']; ?>/<small style="font-size:14px;"><?php echo $overview['canonical_url_count']; ?></small></h4> |
| 290 |
<a class="rl-dash-link" href="<?php echo self::getTabUrl('urls'); ?>"> |
| 291 |
<span class="text-secondary mt-2"><?php RL21UtilWP::_e('Hot URLs Cache'); ?> |
| 292 |
<span class="dashicons dashicons-arrow-right-alt mt-05 <?php echo !$show_arrow ? 'd-none' : ''; ?>"></span> |
| 293 |
</span> |
| 294 |
</a> |
| 295 |
</div> |
| 296 |
<?php |
| 297 |
} |
| 298 |
|
| 299 |
protected static function optimization_image_home(&$overview, $show_arrow) |
| 300 |
{ |
| 301 |
?> |
| 302 |
<div class="bg-white rounded p-4 tpopup" title="<?php RL21UtilWP::_e(sprintf('Images on the website are converted to WebP and resulted in to %s%% lesser size.', round($overview['image_comp_avg_cp'], 2))); ?>"> |
| 303 |
<h4 class=""> |
| 304 |
<?php echo round($overview['image_comp_avg_cp'], 2); ?><small style="font-size:14px;">%</small> |
| 305 |
</h4> |
| 306 |
<a class="rl-dash-link" href="<?php echo self::getTabUrl('images'); ?>"> |
| 307 |
<span class="text-secondary mt-2"><?php RL21UtilWP::_e('Image Compression'); ?> |
| 308 |
<span class="dashicons dashicons-arrow-right-alt mt-05 <?php echo !$show_arrow ? 'd-none' : ''; ?>"></span> |
| 309 |
</span> |
| 310 |
</a> |
| 311 |
</div> |
| 312 |
<?php |
| 313 |
} |
| 314 |
|
| 315 |
protected static function optimization_css_home(&$overview, $show_arrow) |
| 316 |
{ |
| 317 |
?> |
| 318 |
<div class="rl-mfe-card tpopup" title="<?php RL21UtilWP::_e(sprintf('%s%% reduction in CSS is achieved in form of Critical CSS that is required for initial page rendering.', round($overview['css_size_pp'], 2))); ?>"> |
| 319 |
<h4 class=""> |
| 320 |
<?php echo round($overview['css_size_pp'], 2); ?><small style="font-size:14px;">%</small> |
| 321 |
</h4> |
| 322 |
<a class="rl-dash-link" href="<?php echo self::getTabUrl('css'); ?>"> |
| 323 |
<span class="text-secondary mt-2"><?php RL21UtilWP::_e('CSS Reduction'); ?> |
| 324 |
<span class="dashicons dashicons-arrow-right-alt mt-05 <?php echo !$show_arrow ? 'd-none' : ''; ?>"></span> |
| 325 |
</span> |
| 326 |
</a> |
| 327 |
</div> |
| 328 |
<?php |
| 329 |
} |
| 330 |
|
| 331 |
protected static function addDtDependencies() |
| 332 |
{ |
| 333 |
wp_enqueue_script('rabbitloader-datatable-js', '//cdn.datatables.net/1.11.3/js/jquery.dataTables.min.js', ['jquery']); |
| 334 |
wp_enqueue_style('rabbitloader-datatable-css', '//cdn.datatables.net/1.11.3/css/jquery.dataTables.min.css'); |
| 335 |
} |
| 336 |
|
| 337 |
protected static function getUpgradeLink($utm_term, $plan_title) |
| 338 |
{ |
| 339 |
return esc_url(RabbitLoader_21_Core::getRLBaseDomain() . "account/?utm_source=wordpress&utm_medium=plugin&utm_term=$utm_term&utm_content=$utm_term&action=upgrade&domain_id=" . RabbitLoader_21_Core::getWpOptVal('did') . '&domain_name=' . urlencode(get_home_url()) . "/"); |
| 340 |
} |
| 341 |
|
| 342 |
private static function check_varnish($attempts) |
| 343 |
{ |
| 344 |
$httpcode = 0; |
| 345 |
try { |
| 346 |
$url_id = home_url() . '/'; |
| 347 |
$url_parts = parse_url($url_id); |
| 348 |
$port = (empty($url_parts['scheme']) || $url_parts['scheme'] == 'https') ? '443' : '80'; |
| 349 |
$ch = curl_init($url_id); |
| 350 |
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PURGE"); |
| 351 |
curl_setopt($ch, CURLOPT_RESOLVE, array($url_parts['host'] . ":$port:127.0.0.1")); |
| 352 |
curl_setopt($ch, CURLOPT_TIMEOUT, 30); |
| 353 |
curl_exec($ch); |
| 354 |
//$curl_error = curl_error($ch); |
| 355 |
$httpcode = intval(curl_getinfo($ch, CURLINFO_HTTP_CODE)); |
| 356 |
curl_close($ch); |
| 357 |
} catch (Throwable $e) { |
| 358 |
} |
| 359 |
if ($httpcode == 200) { |
| 360 |
return true; |
| 361 |
} else if ($attempts > 0) { |
| 362 |
$attempts--; |
| 363 |
if ($attempts == 0) { |
| 364 |
return false; |
| 365 |
} |
| 366 |
return self::check_varnish($attempts); |
| 367 |
} |
| 368 |
} |
| 369 |
|
| 370 |
protected static function rlTabJS() |
| 371 |
{ |
| 372 |
$overview = self::getOverviewData(); |
| 373 |
$tabVars = [ |
| 374 |
'domain_name' => RabbitLoader_21_Core::getWpOptVal('domain'), |
| 375 |
'domain_id' => RabbitLoader_21_Core::getWpOptVal('did'), |
| 376 |
'rl_nonce' => wp_create_nonce('rl-ajax-nonce'), |
| 377 |
'api_token' => RabbitLoader_21_Core::getWpOptVal('api_token'), |
| 378 |
'plan_title' => isset($overview['plan_title']) ? $overview['plan_title'] : '', |
| 379 |
'home_page_url_id' => isset($overview['home_page_url_id']) ? $overview['home_page_url_id'] : '', |
| 380 |
'published_url_count' => isset($overview['published_url_count']) ? intval($overview['published_url_count']) : 0, |
| 381 |
'canonical_url_count' => isset($overview['canonical_url_count']) ? intval($overview['canonical_url_count']) : 0, |
| 382 |
'optimized_url_count' => isset($overview['optimized_url_count']) ? intval($overview['optimized_url_count']) : 0 |
| 383 |
]; |
| 384 |
//wp_add_inline_script('rabbitloader-index', 'RLAdmin.Tab(window, ' . json_encode($tabVars) . ');'); |
| 385 |
echo '<script>RLAdmin.Tab(window, ' . json_encode($tabVars) . ');</script>'; |
| 386 |
} |
| 387 |
} |
| 388 |
?> |
| 389 |
|