| 1 |
<?php |
| 2 |
|
| 3 |
class RabbitLoader_21_Tab_Init extends RabbitLoader_21_Admin{ |
| 4 |
|
| 5 |
public static function echoPluginPage(){ |
| 6 |
if ( ! current_user_can( 'manage_options' ) ) { |
| 7 |
#the use is not authorized to manage options |
| 8 |
return; |
| 9 |
} |
| 10 |
|
| 11 |
// check if the user have submitted the settings |
| 12 |
//if (RabbitLoader_21_Util_Core::get_param('settings-updated') ) { |
| 13 |
// add settings saved message with the class of "updated" |
| 14 |
//add_settings_error( 'rabbitloader_messages', 'rabbitloader_message', __( 'Settings Saved', 'rl' ), 'updated' ); |
| 15 |
//} |
| 16 |
|
| 17 |
// show error/update messages |
| 18 |
//settings_errors( 'wporg_messages' ); |
| 19 |
/* |
| 20 |
add_filter( 'style_loader_tag', function($html, $handle){ |
| 21 |
if ( 'rabbitloader-bootstrap' === $handle ) { |
| 22 |
return str_replace( "media='all'", "media='all' integrity='sha384-1BmE4kWBq78iYhFldvKuhfTAU6auU8tT94WrHftjDbrCEXSU1oBoqyl2QvZ6jIW3' crossorigin='anonymous'", $html ); |
| 23 |
} |
| 24 |
return $html; |
| 25 |
}, 10, 2 ); |
| 26 |
wp_enqueue_style( 'rabbitloader-bootstrap', 'https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/css/bootstrap.min.css' ); */ |
| 27 |
//wp_enqueue_style( 'rabbitloader-bootstrap', RABBITLOADER_PLUG_URL . 'admin/css/bootstrap.v5.1.3.min.css' ); |
| 28 |
|
| 29 |
wp_enqueue_style('rabbitloader-inter', 'https://fonts.googleapis.com/css2?family=Manrope:wght@200;300;400;500;600;700;800&display=swap'); |
| 30 |
//wp_enqueue_style( 'rabbitloader-css', RABBITLOADER_PLUG_URL . 'admin/css/style.css', [], RABBITLOADER_PLUG_VERSION); |
| 31 |
|
| 32 |
?> |
| 33 |
|
| 34 |
<div class="wrap"> |
| 35 |
<?php |
| 36 |
try{ |
| 37 |
$tab = self::decideTabToShow($isConnected); |
| 38 |
if($isConnected){ |
| 39 |
echo '<h1>'.esc_html( get_admin_page_title() ).'</h1>'; |
| 40 |
|
| 41 |
self::echoTabBar($tab); |
| 42 |
|
| 43 |
if($tab=='settings'){ |
| 44 |
RabbitLoader_21_Tab_Settings::init(); |
| 45 |
RabbitLoader_21_Tab_Settings::echoMainContent(); |
| 46 |
}else if($tab=='help'){ |
| 47 |
RabbitLoader_21_Tab_Help::init(); |
| 48 |
RabbitLoader_21_Tab_Help::echoMainContent(); |
| 49 |
}else if($tab=='log'){ |
| 50 |
RabbitLoader_21_Tab_Log::init(); |
| 51 |
RabbitLoader_21_Tab_Log::echoMainContent(); |
| 52 |
}else if($tab=='urls'){ |
| 53 |
RabbitLoader_21_Tab_Urls::init(); |
| 54 |
RabbitLoader_21_Tab_Urls::echoMainContent(); |
| 55 |
}else if($tab=='usage'){ |
| 56 |
RabbitLoader_21_Tab_Usage::init(); |
| 57 |
RabbitLoader_21_Tab_Usage::echoMainContent(); |
| 58 |
}else if($tab=='geo'){ |
| 59 |
RabbitLoader_21_Tab_Geo::init(); |
| 60 |
RabbitLoader_21_Tab_Geo::echoMainContent(); |
| 61 |
}else if($tab=='images'){ |
| 62 |
RabbitLoader_21_Tab_Images::init(); |
| 63 |
RabbitLoader_21_Tab_Images::echoMainContent(); |
| 64 |
}else if($tab=='css'){ |
| 65 |
RabbitLoader_21_Tab_Css::init(); |
| 66 |
RabbitLoader_21_Tab_Css::echoMainContent(); |
| 67 |
}else{ |
| 68 |
//anything not defined will show the home tab |
| 69 |
RabbitLoader_21_Tab_Home::init(); |
| 70 |
RabbitLoader_21_Tab_Home::echoMainContent(); |
| 71 |
} |
| 72 |
}else{ |
| 73 |
RabbitLoader_21_Tab_Settings::init(); |
| 74 |
RabbitLoader_21_Tab_Settings::echoMainContent(); |
| 75 |
} |
| 76 |
}catch(Throwable $e){ |
| 77 |
RabbitLoader_21_Core::on_exception($e); |
| 78 |
} |
| 79 |
?> |
| 80 |
</div> |
| 81 |
<?php |
| 82 |
} |
| 83 |
/** |
| 84 |
* Echo the tabs in our plugin's admin page |
| 85 |
*/ |
| 86 |
private static function echoTabBar($activeTab = 'home'){ |
| 87 |
|
| 88 |
self::get_warnings($messages_count, true); |
| 89 |
|
| 90 |
$page = RabbitLoader_21_Util_Core::get_param('page'); |
| 91 |
$tabs = array( 'home' => 'Home', 'urls'=>'URLs', 'images'=>'Images', 'css'=>'Critical CSS', 'usage'=>'CDN Usage', 'geo'=>'Demographics', 'settings' => 'Settings', 'log'=>'Log Messages', 'help' => 'Help'); |
| 92 |
echo '<div id="icon-themes" class="icon32"><br></div>'; |
| 93 |
echo '<h2 class="nav-tab-wrapper">'; |
| 94 |
foreach( $tabs as $tab => $name ){ |
| 95 |
$url = add_query_arg(array('tab'=>$tab,'page'=>$page), null); |
| 96 |
$class = ( $tab == $activeTab ) ? ' nav-tab-active' : ''; |
| 97 |
echo "<a class='nav-tab $class' href='$url'>"; |
| 98 |
RabbitLoader_21_Util_WP::_e($name); |
| 99 |
echo "</a>"; |
| 100 |
} |
| 101 |
echo '</h2>'; |
| 102 |
//self::pass_keys_to_js(); |
| 103 |
} |
| 104 |
|
| 105 |
private static function decideTabToShow(&$isConnected){ |
| 106 |
$tab = RabbitLoader_21_Util_Core::get_param('tab'); |
| 107 |
|
| 108 |
if(!self::isPluginActivated()){ |
| 109 |
$isConnected = false; |
| 110 |
if($tab!='help'){ |
| 111 |
$tab = 'settings'; |
| 112 |
} |
| 113 |
}else{ |
| 114 |
$isConnected = true; |
| 115 |
} |
| 116 |
if(empty($tab)){ |
| 117 |
$tab = 'home'; |
| 118 |
} |
| 119 |
return $tab; |
| 120 |
} |
| 121 |
|
| 122 |
protected static function getTabUrl($tab_key){ |
| 123 |
$page = RabbitLoader_21_Util_Core::get_param('page'); |
| 124 |
return add_query_arg(array('tab'=>$tab_key, 'page'=>$page), null); |
| 125 |
} |
| 126 |
|
| 127 |
protected static function &getOverviewData(&$apiError, &$apiMessage){ |
| 128 |
$overview = get_transient( 'rabbitloader_trans_overview_data' ); |
| 129 |
if(!empty($overview)){ |
| 130 |
return $overview; |
| 131 |
} |
| 132 |
|
| 133 |
$overview = [ |
| 134 |
'score_circle_best'=>0, |
| 135 |
'score_circle_avg'=>0, |
| 136 |
'canonical_url_count'=>0, |
| 137 |
'optimized_url_count'=>0, |
| 138 |
'optimized_url_per'=>0, |
| 139 |
'gb_used'=>0, |
| 140 |
'gb_quota'=>0, |
| 141 |
'gb_remaining'=>0, |
| 142 |
'pp_used'=>0, |
| 143 |
'plan_title'=>0, |
| 144 |
'plan_end_date'=>0, |
| 145 |
'image_comp_avg_cp'=>0, |
| 146 |
'css_size_pp'=>0 |
| 147 |
]; |
| 148 |
|
| 149 |
$http = RabbitLoader_21_Core::callGETAPI('report/overview', $apiError, $apiMessage); |
| 150 |
if($apiError){ |
| 151 |
return $overview; |
| 152 |
} |
| 153 |
|
| 154 |
$expected_url_count = RabbitLoader_21_Core::get_published_count(); |
| 155 |
|
| 156 |
if(!empty($http['body']['data']['speed_score']['max_score'])){ |
| 157 |
$overview['score_circle_best'] = intval($http['body']['data']['speed_score']['max_score']*100); |
| 158 |
} |
| 159 |
|
| 160 |
if(!empty($http['body']['data']['speed_score']['avg_score'])){ |
| 161 |
$overview['score_circle_avg'] = intval($http['body']['data']['speed_score']['avg_score']*100); |
| 162 |
} |
| 163 |
|
| 164 |
if(!empty($http['body']['data']['speed_score']['canonical_url_count'])){ |
| 165 |
$canonical_url_count = intval($http['body']['data']['speed_score']['canonical_url_count']); |
| 166 |
$overview['canonical_url_count'] = max($canonical_url_count, $expected_url_count); |
| 167 |
} |
| 168 |
|
| 169 |
if(!empty($http['body']['data']['speed_score']['optimized_url_count'])){ |
| 170 |
$overview['optimized_url_count'] = intval($http['body']['data']['speed_score']['optimized_url_count']); |
| 171 |
} |
| 172 |
|
| 173 |
if(!empty($http['body']['data']['bill']['end_date'])){ |
| 174 |
$overview['plan_end_date'] = $http['body']['data']['bill']['end_date']; |
| 175 |
} |
| 176 |
if(!empty($http['body']['data']['bill']['usage']['bandwidth_gb'])){ |
| 177 |
$overview['gb_used'] = $http['body']['data']['bill']['usage']['bandwidth_gb']; |
| 178 |
} |
| 179 |
if(!empty($http['body']['data']['plan_limits']['bandwidth_gb'])){ |
| 180 |
$overview['gb_quota'] = round($http['body']['data']['plan_limits']['bandwidth_gb'], 0); |
| 181 |
} |
| 182 |
if(!empty($http['body']['data']['plan_details']['title'])){ |
| 183 |
$overview['plan_title'] = $http['body']['data']['plan_details']['title']; |
| 184 |
} |
| 185 |
if(!empty($http['body']['data']['css_size_pp'])){ |
| 186 |
$overview['css_size_pp'] = round($http['body']['data']['css_size_pp'],1); |
| 187 |
} |
| 188 |
if(!empty($http['body']['data']['image_comp_avg_cp'])){ |
| 189 |
$overview['image_comp_avg_cp'] = round($http['body']['data']['image_comp_avg_cp'],1); |
| 190 |
} |
| 191 |
$overview['gb_remaining'] = $overview['gb_quota'] - $overview['gb_used']; |
| 192 |
$overview['pp_used'] = $overview['gb_quota']>0 ? round(($overview['gb_used']/$overview['gb_quota'])*100, 0) : 0; |
| 193 |
|
| 194 |
try{ |
| 195 |
$fi = new FilesystemIterator(RabbitLoader_21_Util_WP::get_cache_dir('long'), FilesystemIterator::SKIP_DOTS); |
| 196 |
$fcount = iterator_count($fi)/2; ///2 cause content and header |
| 197 |
if(!empty($fcount)){ |
| 198 |
$overview['optimized_url_count'] = round($fcount,0); |
| 199 |
} |
| 200 |
}catch(Throwable $e){ |
| 201 |
RabbitLoader_21_Core::on_exception($e); |
| 202 |
} |
| 203 |
if($overview['optimized_url_count']>$overview['canonical_url_count']){ |
| 204 |
//local cache might have removed URLs |
| 205 |
$overview['optimized_url_count'] = $overview['canonical_url_count']; |
| 206 |
} |
| 207 |
$optimized_url_per = empty($overview['canonical_url_count']) ? 0 : ($overview['optimized_url_count']/$overview['canonical_url_count'])*100; |
| 208 |
$overview['optimized_url_per'] = round($optimized_url_per, 1); |
| 209 |
|
| 210 |
set_transient('rabbitloader_trans_overview_data', $overview, 60); |
| 211 |
return $overview; |
| 212 |
} |
| 213 |
|
| 214 |
protected static function quota_used_box(&$overview, $show_arrow){ |
| 215 |
?> |
| 216 |
<div class="bg-white rounded p-4" title="<?php RabbitLoader_21_Util_WP::_e(sprintf('You have consumed %s GB out of %s GB monthly quota available in your current plan.', round($overview['gb_used'], 2), $overview['gb_quota']));?>"> |
| 217 |
<h4 class="<?php echo $overview['pp_used']>=99?"text-danger":"";?>"> |
| 218 |
<?php echo round($overview['gb_used'], 2);?>/<small style="font-size:14px;"><?php echo $overview['gb_quota'];?> GB</small> |
| 219 |
</h4> |
| 220 |
<a class="rl-dash-link" href="<?php echo self::getTabUrl('usage');?>"> |
| 221 |
<span class="text-secondary mt-2"><?php RabbitLoader_21_Util_WP::_e(sprintf('Quota Used (%s Plan)', $overview['plan_title']));?> |
| 222 |
<span class="dashicons dashicons-arrow-right-alt mt-05 <?php echo !$show_arrow?'d-none':'';?>"></span> |
| 223 |
</span> |
| 224 |
</a> |
| 225 |
</div> |
| 226 |
<?php |
| 227 |
} |
| 228 |
|
| 229 |
protected static function quota_remaining_box(&$overview){ |
| 230 |
$gb_remaining_nz = $overview['gb_remaining'] < 0 ? 0 : round($overview['gb_remaining'], 2); |
| 231 |
?> |
| 232 |
<div class="bg-white rounded p-4" title="<?php RabbitLoader_21_Util_WP::_e(sprintf('You have %s GB bandwidth available out of %s GB monthly quota in the current billing cycle.', $gb_remaining_nz, $overview['gb_quota']));?>"> |
| 233 |
<h4 class="<?php echo $overview['pp_used']>=99?"text-danger":"";?>"><?php echo $gb_remaining_nz;?>/<small style="font-size:14px;"><?php echo $overview['gb_quota'];?> GB</small></h4> |
| 234 |
<span class="text-secondary mt-2"><?php RabbitLoader_21_Util_WP::_e('Quota Remaining');?></span> <a target="_blank" href="<?php echo self::getUpgradeLink('quota_remaining', $overview['plan_title']);?>" class="badge bg-primary text-white" style="text-decoration: none;"><?php RabbitLoader_21_Util_WP::_e('GET MORE');?></a> |
| 235 |
</div> |
| 236 |
<?php |
| 237 |
} |
| 238 |
|
| 239 |
protected static function urls_detected_box(&$overview, $show_arrow){ |
| 240 |
$title = RabbitLoader_21_Util_WP::__(sprintf('Cache exists for %d hot URL(s) out of total %d URL(s) detected', $overview['optimized_url_count'], $overview['canonical_url_count'])); |
| 241 |
?> |
| 242 |
<div class="bg-white rounded p-4 tpopup" title="<?php echo $title; ?>"> |
| 243 |
<h4 class=""><?php echo $overview['optimized_url_count'];?>/<small style="font-size:14px;"><?php echo $overview['canonical_url_count'];?></small></h4> |
| 244 |
<a class="rl-dash-link" href="<?php echo self::getTabUrl('urls');?>"> |
| 245 |
<span class="text-secondary mt-2"><?php RabbitLoader_21_Util_WP::_e('Hot URLs Cache');?> |
| 246 |
<span class="dashicons dashicons-arrow-right-alt mt-05 <?php echo !$show_arrow?'d-none':'';?>"></span> |
| 247 |
</span> |
| 248 |
</a> |
| 249 |
</div> |
| 250 |
<?php |
| 251 |
} |
| 252 |
|
| 253 |
protected static function optimization_image_home(&$overview, $show_arrow){ |
| 254 |
?> |
| 255 |
<div class="bg-white rounded p-4 tpopup" title="<?php RabbitLoader_21_Util_WP::_e(sprintf('Images on the website are converted to WebP and resulted in to %s%% lesser size.', round($overview['image_comp_avg_cp'], 2)));?>"> |
| 256 |
<h4 class=""> |
| 257 |
<?php echo round($overview['image_comp_avg_cp'], 2);?><small style="font-size:14px;">%</small> |
| 258 |
</h4> |
| 259 |
<a class="rl-dash-link" href="<?php echo self::getTabUrl('images');?>"> |
| 260 |
<span class="text-secondary mt-2"><?php RabbitLoader_21_Util_WP::_e('Image Compression');?> |
| 261 |
<span class="dashicons dashicons-arrow-right-alt mt-05 <?php echo !$show_arrow?'d-none':'';?>"></span> |
| 262 |
</span> |
| 263 |
</a> |
| 264 |
</div> |
| 265 |
<?php |
| 266 |
} |
| 267 |
|
| 268 |
protected static function optimization_css_home(&$overview, $show_arrow){ |
| 269 |
?> |
| 270 |
<div class="bg-white rounded p-4 tpopup" title="<?php RabbitLoader_21_Util_WP::_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)));?>"> |
| 271 |
<h4 class=""> |
| 272 |
<?php echo round($overview['css_size_pp'], 2);?><small style="font-size:14px;">%</small> |
| 273 |
</h4> |
| 274 |
<a class="rl-dash-link" href="<?php echo self::getTabUrl('css');?>"> |
| 275 |
<span class="text-secondary mt-2"><?php RabbitLoader_21_Util_WP::_e('CSS Reduction');?> |
| 276 |
<span class="dashicons dashicons-arrow-right-alt mt-05 <?php echo !$show_arrow?'d-none':'';?>"></span> |
| 277 |
</span> |
| 278 |
</a> |
| 279 |
</div> |
| 280 |
<?php |
| 281 |
} |
| 282 |
|
| 283 |
protected static function addDtDependencies(){ |
| 284 |
wp_enqueue_script('rabbitloader-datatable-js', '//cdn.datatables.net/1.11.3/js/jquery.dataTables.min.js', ['jquery']); |
| 285 |
wp_enqueue_style( 'rabbitloader-datatable-css', '//cdn.datatables.net/1.11.3/css/jquery.dataTables.min.css' ); |
| 286 |
} |
| 287 |
|
| 288 |
protected static function getUpgradeLink($utm_term, $plan_title){ |
| 289 |
if(stripos($plan_title, 'AppSumo')!==false){ |
| 290 |
return 'https://appsumo.com/account/products/'; |
| 291 |
}else{ |
| 292 |
return RabbitLoader_21_Core::getRLDomain()."pricing/?utm_source=wordpress&utm_medium=plugin&utm_term=$utm_term#domain=".urlencode(get_home_url())."/"; |
| 293 |
} |
| 294 |
} |
| 295 |
} |
| 296 |
?> |