PluginProbe
RabbitLoader / 3.2.0
RabbitLoader v3.2.0
4.0.2 4.0.1 4.0 3.2.0 3.1.1 3.1.0 3.0.5 3.0.3 3.0.4 2.17.1 2.17.2 2.17.3 2.17.4 2.17.5 2.17.6 2.17.7 2.18.0 2.18.1 2.18.2 2.18.3 2.18.4 2.18.5 2.18.6 2.18.7 2.18.8 All 129 releases
rabbit-loader / inc / tab_init.php

tab_init.php in RabbitLoader 3.2.0, at inc/tab_init.php

370 lines 16.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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">
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 'canonical_url_count' => 0,
146 'optimized_url_count' => 0,
147 'optimized_url_per' => 0,
148 'pv_used' => 0,
149 'pv_quota' => 0,
150 'pv_remaining' => 0,
151 'pp_used' => 0,
152 'plan_title' => 0,
153 'plan_end_date' => 0,
154 'image_comp_avg_cp' => 0,
155 'css_size_pp' => 0,
156 'home_page_url_id' => ''
157 ];
158
159 $http = RabbitLoader_21_Core::callGETAPI('report/overview', $apiError, $apiMessage);
160 if ($apiError) {
161 return $overview;
162 }
163
164 if (!empty($http['body']['data']['domain_details']['host'])) {
165 //CODENAME#401041
166 $dbHost = parse_url(get_site_url())['host'];
167 $remoteHost = $http['body']['data']['domain_details']['host'];
168 if ((strcmp($dbHost, $remoteHost) !== 0) && (strcmp($dbHost, "www." . $remoteHost) !== 0)) {
169 RabbitLoader_21_Core::update_api_tokens('', '', '', "dbHost $dbHost when remoteHost $remoteHost");
170 $apiError = "Invalid registration";
171 $apiMessage = "INVALID_DOMAIN";
172 return $overview;
173 }
174 }
175
176 $expected_url_count = RabbitLoader_21_Core::get_published_count();
177
178 if (!empty($http['body']['data']['speed_score']['max_score'])) {
179 $overview['score_circle_best'] = intval($http['body']['data']['speed_score']['max_score'] * 100);
180 }
181
182 if (!empty($http['body']['data']['speed_score']['avg_score'])) {
183 $overview['score_circle_avg'] = intval($http['body']['data']['speed_score']['avg_score'] * 100);
184 }
185
186 if (!empty($http['body']['data']['speed_score']['canonical_url_count'])) {
187 $canonical_url_count = intval($http['body']['data']['speed_score']['canonical_url_count']);
188 $overview['canonical_url_count'] = max($canonical_url_count, $expected_url_count);
189 }
190
191 if (!empty($http['body']['data']['speed_score']['optimized_url_count'])) {
192 $overview['optimized_url_count'] = 0;
193 }
194
195 if (!empty($http['body']['data']['bill']['end_date'])) {
196 $overview['plan_end_date'] = $http['body']['data']['bill']['end_date'];
197 }
198 if (!empty($http['body']['data']['bill']['usage']['pageviews_ctr'])) {
199 $overview['pv_used'] = $http['body']['data']['bill']['usage']['pageviews_ctr'];
200 }
201 if (!empty($http['body']['data']['plan_limits']['pageviews_ctr'])) {
202 $overview['pv_quota'] = round($http['body']['data']['plan_limits']['pageviews_ctr'], 0);
203 }
204 if (!empty($http['body']['data']['plan_details']['title'])) {
205 $overview['plan_title'] = $http['body']['data']['plan_details']['title'];
206 }
207 if (!empty($http['body']['data']['css_size_pp'])) {
208 $overview['css_size_pp'] = round($http['body']['data']['css_size_pp'], 1);
209 }
210 if (!empty($http['body']['data']['image_comp_avg_cp'])) {
211 $overview['image_comp_avg_cp'] = round($http['body']['data']['image_comp_avg_cp'], 1);
212 }
213 $overview['pv_remaining'] = $overview['pv_quota'] - $overview['pv_used'];
214 $overview['pp_used'] = $overview['pv_quota'] > 0 ? round(($overview['pv_used'] / $overview['pv_quota']) * 100, 0) : 0;
215
216 $overview['optimized_url_count'] = RabbitLoader_21_Core::getCacheCount();
217 if ($overview['optimized_url_count'] > $overview['canonical_url_count']) {
218 //local cache might have removed URLs
219 $overview['optimized_url_count'] = $overview['canonical_url_count'];
220 }
221 $optimized_url_per = empty($overview['canonical_url_count']) ? 0 : ($overview['optimized_url_count'] / $overview['canonical_url_count']) * 100;
222 $overview['optimized_url_per'] = round($optimized_url_per, 1);
223
224 if (!empty($http['body']['data']['home_page_url_id'])) {
225 $overview['home_page_url_id'] = $http['body']['data']['home_page_url_id'];
226 }
227
228 RabbitLoader_21_Core::getWpOption($rl_wp_options);
229 if (!empty($http['body']['data']['rl_latest_plugin_v'])) {
230 $rl_wp_options['rl_latest_plugin_v'] = $http['body']['data']['rl_latest_plugin_v'];
231 }
232 if (empty($rl_wp_options['rl_varnish'])) {
233 $rl_wp_options['rl_varnish'] = self::check_varnish(2) ? 1 : -1;
234 }
235 RabbitLoader_21_Core::updateWpOption($rl_wp_options);
236
237 set_transient('rabbitloader_trans_overview_data', $overview, 60);
238 return $overview;
239 }
240
241 protected static function quota_used_box(&$overview, $show_arrow)
242 {
243 ?>
244 <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'])); ?>">
245 <h4 class="<?php echo $overview['pp_used'] >= 99 ? "text-danger" : ""; ?>">
246 <?php echo intval($overview['pv_used']); ?>/<small style="font-size:14px;"><?php echo $overview['pv_quota']; ?> </small>
247 </h4>
248 <a class="rl-dash-link" href="<?php echo self::getTabUrl('usage'); ?>">
249 <span class="text-secondary mt-2"><?php RL21UtilWP::_e(sprintf('PageViews Used (%s Plan)', $overview['plan_title'])); ?>
250 <span class="dashicons dashicons-arrow-right-alt mt-05 <?php echo !$show_arrow ? 'd-none' : ''; ?>"></span>
251 </span>
252 </a>
253 </div>
254 <?php
255 }
256
257 protected static function quota_remaining_box(&$overview)
258 {
259 $gb_remaining_nz = $overview['pv_remaining'] < 0 ? 0 : intval($overview['pv_remaining']);
260 ?>
261 <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'])); ?>">
262 <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>
263 <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>
264 </div>
265 <?php
266 }
267
268 protected static function urls_detected_box(&$overview, $show_arrow)
269 {
270 $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']));
271 ?>
272 <div class="rl-mfe-card tpopup" title="<?php echo $title; ?>">
273 <h4 class=""><?php echo $overview['optimized_url_count']; ?>/<small style="font-size:14px;"><?php echo $overview['canonical_url_count']; ?></small></h4>
274 <a class="rl-dash-link" href="<?php echo self::getTabUrl('urls'); ?>">
275 <span class="text-secondary mt-2"><?php RL21UtilWP::_e('Hot URLs Cache'); ?>
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 optimization_image_home(&$overview, $show_arrow)
284 {
285 ?>
286 <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))); ?>">
287 <h4 class="">
288 <?php echo round($overview['image_comp_avg_cp'], 2); ?><small style="font-size:14px;">%</small>
289 </h4>
290 <a class="rl-dash-link" href="<?php echo self::getTabUrl('images'); ?>">
291 <span class="text-secondary mt-2"><?php RL21UtilWP::_e('Image Compression'); ?>
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_css_home(&$overview, $show_arrow)
300 {
301 ?>
302 <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))); ?>">
303 <h4 class="">
304 <?php echo round($overview['css_size_pp'], 2); ?><small style="font-size:14px;">%</small>
305 </h4>
306 <a class="rl-dash-link" href="<?php echo self::getTabUrl('css'); ?>">
307 <span class="text-secondary mt-2"><?php RL21UtilWP::_e('CSS Reduction'); ?>
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 addDtDependencies()
316 {
317 wp_enqueue_script('rabbitloader-datatable-js', '//cdn.datatables.net/1.11.3/js/jquery.dataTables.min.js', ['jquery']);
318 wp_enqueue_style('rabbitloader-datatable-css', '//cdn.datatables.net/1.11.3/css/jquery.dataTables.min.css');
319 }
320
321 protected static function getUpgradeLink($utm_term, $plan_title)
322 {
323 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()) . "/");
324 }
325
326 private static function check_varnish($attempts)
327 {
328 $httpcode = 0;
329 try {
330 $url_id = home_url() . '/';
331 $url_parts = parse_url($url_id);
332 $port = (empty($url_parts['scheme']) || $url_parts['scheme'] == 'https') ? '443' : '80';
333 $ch = curl_init($url_id);
334 curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PURGE");
335 curl_setopt($ch, CURLOPT_RESOLVE, array($url_parts['host'] . ":$port:127.0.0.1"));
336 curl_setopt($ch, CURLOPT_TIMEOUT, 30);
337 curl_exec($ch);
338 //$curl_error = curl_error($ch);
339 $httpcode = intval(curl_getinfo($ch, CURLINFO_HTTP_CODE));
340 curl_close($ch);
341 } catch (Throwable $e) {
342 }
343 if ($httpcode == 200) {
344 return true;
345 } else if ($attempts > 0) {
346 $attempts--;
347 if ($attempts == 0) {
348 return false;
349 }
350 return self::check_varnish($attempts);
351 }
352 }
353
354 protected static function rlTabJS()
355 {
356 $overview = self::getOverviewData();
357 $tabVars = [
358 'domain_name' => RabbitLoader_21_Core::getWpOptVal('domain'),
359 'domain_id' => RabbitLoader_21_Core::getWpOptVal('did'),
360 'rl_nonce' => wp_create_nonce('rl-ajax-nonce'),
361 'api_token' => RabbitLoader_21_Core::getWpOptVal('api_token'),
362 'plan_title' => isset($overview['plan_title']) ? $overview['plan_title'] : '',
363 'home_page_url_id' => isset($overview['home_page_url_id']) ? $overview['home_page_url_id'] : ''
364 ];
365 //wp_add_inline_script('rabbitloader-index', 'RLAdmin.Tab(window, ' . json_encode($tabVars) . ');');
366 echo '<script>RLAdmin.Tab(window, ' . json_encode($tabVars) . ');</script>';
367 }
368 }
369 ?>
370