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_settings.php

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

380 lines 19.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 if (class_exists('RabbitLoader_21_Tab_Settings')) {
4 #it seems we have a conflict
5 return;
6 }
7
8 class RabbitLoader_21_Tab_Settings extends RabbitLoader_21_Tab_Init
9 {
10
11 public static function init() {}
12
13 public static function echoMainContent()
14 {
15
16 $isConnected = self::isPluginActivated();
17 $rlaction = sanitize_text_field(RabbitLoader_21_Util_Core::get_param('rlaction'));
18 $page = sanitize_text_field(RabbitLoader_21_Util_Core::get_param('page'));
19 $tab = sanitize_text_field(RabbitLoader_21_Util_Core::get_param('tab'));
20
21 $urlparts = parse_url(home_url());
22 $domain = $urlparts['scheme'] . '://' . $urlparts['host'];
23 if (!empty($urlparts['port'])) {
24 $domain .= ':' . $urlparts['port'];
25 }
26
27 $disconnect_error = '';
28
29 if (strcmp($rlaction, 'disconnect') === 0) {
30 if (!current_user_can('manage_options')) {
31 $disconnect_error = 'You are not allowed to disconnect RabbitLoader.';
32 } else if (empty($_GET['_wpnonce']) || !wp_verify_nonce(sanitize_text_field(wp_unslash($_GET['_wpnonce'])), 'rabbitloader_disconnect')) {
33 $disconnect_error = 'Invalid disconnect request. Please try again.';
34 } else {
35 $disconnect_success = RabbitLoader_21_Core::notify_backend_disconnect('user action disconnect', $apiError, $apiMessage);
36
37 if ($disconnect_success) {
38 RabbitLoader_21_Core::update_api_tokens('', '', '', 'user action disconnect');
39 $isConnected = false;
40 $url_connect = esc_url(add_query_arg(array('tab' => $tab, 'page' => $page, 'rlaction' => false)));
41 echo '<script>window.location="' . $url_connect . '";</script>';
42 return;
43 }
44
45 $disconnect_error = empty($apiMessage) ? 'RabbitLoader could not confirm the disconnect. Please try again.' : $apiMessage;
46 }
47 } else if (strcmp($rlaction, 'savekeys') === 0) {
48 $connected = false;
49
50 $tokens = RabbitLoader_21_Util_Core::get_param('rl-token', true);
51 $tokens = base64_decode($tokens);
52 if ($tokens) {
53 $tokens = json_decode($tokens, true);
54 if (!empty($tokens['api_token'])) {
55 RabbitLoader_21_Core::update_api_tokens($tokens['api_token'], $urlparts['host'], $tokens['did'], '');
56 $connected = true;
57 }
58 if (isset($tokens['cdn_prefix'])) {
59 update_option('rabbitloader_cdn_prefix', $tokens['cdn_prefix'], true);
60 }
61 }
62
63 if ($connected) {
64 $url_connect = esc_url(add_query_arg(array('tab' => 'home', 'page' => $page, 'rlaction' => false, 'token' => false)));
65 do_action('rl_site_connected');
66 echo '<script>window.location="' . $url_connect . '";</script>';
67 }
68 }
69 if ($isConnected) {
70 $url_disconnect = esc_url(wp_nonce_url(add_query_arg(array('tab' => $tab, 'page' => $page, 'rlaction' => 'disconnect')), 'rabbitloader_disconnect'));
71 $url_connected = esc_url(add_query_arg(array('tab' => $tab, 'page' => $page, 'rlaction' => false)));
72 ?>
73 <div class="" style="max-width: 1160px; margin:40px auto;">
74 <?php
75 if (!empty($disconnect_error)) {
76 echo '<div class="notice notice-error"><p><b>';
77 RL21UtilWP::_e('Disconnect failed');
78 echo ': </b>';
79 echo esc_html($disconnect_error);
80 echo '</p></div>';
81 }
82 self::show_cf_box();
83 self::general();
84 self::optimizationRule();
85 self::advanceSettings();
86 self::echoConnectedBox($url_disconnect, $url_connected);
87 ?>
88 </div>
89 <?php
90 } else {
91
92 $url_redirect = $domain . add_query_arg(array('tab' => $tab, 'page' => $page, 'rlaction' => 'savekeys'));
93
94 $url_oauth = esc_url(RabbitLoader_21_Core::getRLBaseDomain() . "account/?source=wp-plugin&action=connect&site_url=" . urlencode(site_url()) . "&redirect_url=" . urlencode($url_redirect) . '&cms_v=' . get_bloginfo('version') . '&plugin_v=' . RABBITLOADER_PLUG_VERSION . '&utm_source=wordpress&utm_medium=plugin&utm_content=activate-rabbitLoader');
95 ?>
96 <style>
97 @import url("https://fonts.googleapis.com/css2?family=Inter:wght@100;200;300;400;500;600;700;800;900&display=swap");
98 </style>
99 <div class="div-connect-parent">
100 <div class="rl-content-area border-bottom">
101 <img src="<?php echo RABBITLOADER_PLUG_URL; ?>/assets/logo-dark.svg" width="140" />
102 </div>
103 <div class=" rl-content-area border-bottom">
104 <h4>Get 100/100 PageSpeed</h4>
105 <p>on Google PageSpeed Insight</p>
106
107 <?php
108 $conflictState = RabbitLoader_21_Conflicts::getConflictState(true);
109 $conflictPluginMessages = empty($conflictState['messages']) ? [] : $conflictState['messages'];
110 foreach ($conflictPluginMessages as $plugMessage) {
111 echo '<div class="alert alert-danger" role="alert">';
112 _e($plugMessage, RABBITLOADER_TEXT_DOMAIN);
113 echo '</div>';
114 }
115
116 if (empty($conflictState['activation_blocked'])) {
117 echo '<a href="' . $url_oauth . '" class="rl-btn rl-btn-primary rl-btn-lg my-4">Activate RabbitLoader</a>';
118 } else {
119 echo '<div class="my-4">';
120 _e('Resolve the blocking warning(s) above before activating RabbitLoader.');
121 echo '</div>';
122 }
123
124 if (!empty($conflictPluginMessages) && empty($conflictState['activation_blocked'])) {
125 echo '<div class="my-4">';
126 _e('The above warning(s) should be reviewed before activating RabbitLoader.');
127 echo '</div>';
128 }
129 ?>
130
131 </div>
132 <div class="rl-content-area footer-area">
133 <div class="footer-nav">
134 <ul class="d-flex my-2">
135 <li><a href="https://rabbitloader.com/wordpress-crash-course/" target="_blank">Crash Courses</a></li>
136 <li><a href="https://rabbitloader.com/terms/" target="_blank">Terms &amp; Conditions</a></li>
137 <li><a href="mailto:support@rabbitloader.com" target="_blank">Get Support</a></li>
138 </ul>
139 </div>
140 </div>
141 </div>
142 <?php } ?>
143 <?php
144 }
145
146 private static function echoConnectedBox($disconnect_url, $redirect_url)
147 {
148 ?>
149 <div class="row mb-4">
150 <div class="col">
151 <div class="rl-mfe-card">
152 <div class="row">
153
154 <div class="col-8 text-secondary">
155 <h5 class="mt-0">Connected</h5>
156 <span>Your website is connected with RabbitLoader.</span>
157
158 <div class="mt-4">
159 <a
160 type="button"
161 class="btn btn-outline-danger"
162 id="rabbitloader-disconnect-btn"
163 href="<?php echo $disconnect_url; ?>"
164 data-ajax-url="<?php echo esc_url(admin_url('admin-ajax.php')); ?>"
165 data-redirect-url="<?php echo $redirect_url; ?>"
166 data-rl-nonce="<?php echo esc_attr(wp_create_nonce('rl-ajax-nonce')); ?>">Disconnect from RabbitLoader</a>
167 </div>
168 </div>
169
170 <div class="col-4 text-center">
171 <img src="<?php echo RABBITLOADER_PLUG_URL; ?>/assets/checked-2.png" class="img-fluid" style="max-height:150px;" />
172 </div>
173
174 </div>
175 </div>
176 </div>
177 </div>
178 <script>
179 document.addEventListener('DOMContentLoaded', function() {
180 var disconnectBtn = document.getElementById('rabbitloader-disconnect-btn');
181 if (!disconnectBtn) {
182 return;
183 }
184
185 disconnectBtn.addEventListener('click', async function(event) {
186 event.preventDefault();
187
188 if (disconnectBtn.dataset.loading === '1') {
189 return;
190 }
191
192 var originalLabel = disconnectBtn.textContent;
193 disconnectBtn.dataset.loading = '1';
194 disconnectBtn.classList.add('disabled');
195 disconnectBtn.setAttribute('aria-disabled', 'true');
196 disconnectBtn.textContent = 'Disconnecting...';
197
198 try {
199 var payload = new URLSearchParams();
200 payload.set('action', 'rabbitloader_disconnect');
201 payload.set('rl_nonce', disconnectBtn.getAttribute('data-rl-nonce') || '');
202
203 var response = await fetch(disconnectBtn.getAttribute('data-ajax-url') || '', {
204 method: 'POST',
205 headers: {
206 'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8'
207 },
208 body: payload.toString()
209 });
210
211 if (!response.ok) {
212 throw new Error('Request failed');
213 }
214
215 var result = await response.json();
216 if (!result || result.result !== true) {
217 throw new Error('Disconnect failed');
218 }
219
220 window.location = disconnectBtn.getAttribute('data-redirect-url') || disconnectBtn.href;
221 } catch (error) {
222 disconnectBtn.dataset.loading = '0';
223 disconnectBtn.classList.remove('disabled');
224 disconnectBtn.removeAttribute('aria-disabled');
225 disconnectBtn.textContent = originalLabel;
226 window.alert('Failed to disconnect from RabbitLoader.');
227 }
228 });
229 });
230 </script>
231 <?php
232 }
233
234 private static function general()
235 {
236 //introduced@2.14.0
237 RabbitLoader_21_Core::getWpUserOption($user_options);
238 if (isset($_POST['form_name']) && strcmp($_POST['form_name'], 'general_settings') == 0) {
239 $user_options['purge_on_change'] = !empty($_POST['chk_purge_on_change']);
240 $user_options['private_mode_val'] = !empty($_POST['chk_private_mode']);
241 RabbitLoader_21_Core::updateUserOption($user_options);
242 }
243 $purge_on_change = !empty($user_options['purge_on_change']);
244 $private_mode_val = !empty($user_options['private_mode_val']);
245 ?>
246 <div class="row mb-4">
247 <div class="col">
248 <div class="rl-mfe-card">
249 <div class="row">
250 <div class="col-sm-12 col-md-12 text-secondary">
251 <h5 class="mt-0">General Settings</h5>
252 <span></span>
253 <div class="mt-4">
254 <form method="post">
255 <input type="hidden" name="form_name" value="general_settings" />
256
257 <div class="form-check">
258 <input class="form-check-input" type="checkbox" value="1" id="chk_private_mode" name="chk_private_mode" style="margin-top: 0.25em; vertical-align: top;" <?php echo $private_mode_val ? ' checked="checked" ' : ''; ?>>
259 <label class="form-check-label" for="chk_private_mode">
260 <?php RL21UtilWP::_e('Turn on \'Me\' mode to do testing or resolving conflicts (default: off)'); ?>
261 </label>
262 <span class="dashicons dashicons-info-outline text-secondary tpopup" title="Click to know more about Me mode" title-html="<div style='text-align:left'><h4>'Me' Mode</h4><br><b>If on:</b> Me mode is for testing and debugging. Only you can see the RabbitLoader optimized pages by appending '?rltest=1' to URLs. Regular visitors will continue to see the original webpages without any affect of RabbitLoader. Keep this on if you are facing any compatibility issues likely due to RabbitLoader.<br><br><b>If off</b>: All public pages will be served with RabbitLoader optimization on. This is recommended for most users.</div>" style="font-size: 16px; line-height: 28px;"></span>
263 </div>
264
265 <div class="form-check">
266 <input class="form-check-input" type="checkbox" value="1" id="chk_purge_on_change" name="chk_purge_on_change" style="margin-top: 0.25em; vertical-align: top;" <?php echo $purge_on_change ? ' checked="checked" ' : ''; ?>>
267 <label class="form-check-label" for="chk_purge_on_change">
268 <?php RL21UtilWP::_e('Instant content change is crucial for my visitors (default: off)'); ?>
269 </label>
270 <span class="dashicons dashicons-info-outline text-secondary tpopup" title="Click to know more" title-html="<div style='text-align:left'><h4>Cache replace behavior when content is modified</h4><br><b>If on:</b> When you update a page or post content, the current cache will be wiped out and the pages would be optimized again. This is useful for sites where content freshness is crucial over the speed performance. This can cause an intermittent low PageSpeed score.<br><b>If off</b>: When you update a page or post content, the system will keep serving the visitors from currently cached content but start updating the cache gradually with the fresh content. This is recommended for most users.<br><a href='https://rabbitloader.com/kb/purging-cache-wordpress-plugin/' target='_blank'>read more</a></div>" style="font-size: 16px; line-height: 28px;"></span>
271
272 <?php
273 if ($purge_on_change) {
274 echo '<span class="text-danger d-block mb-2" style="font-size: 0.75rem;">';
275 RL21UtilWP::_e('You may see fluctuations in SpeedScore because the above checkbox is on.');
276 echo ' <a href="https://rabbitloader.com/kb/fluctuations-in-pagespeed-performance-score/" target="_blank" title="Click to know more about this.">Know Why.</a></span>';
277 echo '<div class="notice notice-error"><p><b>';
278 RL21UtilWP::_e('Warning');
279 echo ': </b> ';
280 RL21UtilWP::_e('You may see fluctuations in SpeedScore because the "Instant content change" checkbox is on.');
281 echo '</p></div>';
282 }
283 ?>
284 </div>
285
286 <button type="submit" class="rl-mfe-btn mt-2">Save</button>
287 <?php
288 self::saveNotice();
289 ?>
290 </form>
291 </div>
292 </div>
293 </div>
294 </div>
295 </div>
296 </div>
297 <?php
298 }
299
300 private static function saveNotice()
301 {
302 if (RL21UtilWP::is_flywheel()) {
303 echo '<span class="d-block mt-2">', sprintf(RL21UtilWP::__('Flywheel Note: You need to Flush Cache manually from Flywheel dashboard after saving the settings <a href="%s">check details</a>'), "https://rabbitloader.com/kb/settings-for-flywheel/"), '</span>';
304 }
305 }
306 private static function optimizationRule()
307 {
308 ?>
309 <div class="row">
310 <div class="col-sm-12" id="mfe_page_rule">
311 </div>
312 </div>
313 <?php
314 }
315
316 private static function advanceSettings()
317 {
318 $links = [
319 'rules' => ['label' => 'Optimization Settings', 'videoID' => 'lC0vWlugHJ4', 'duration' => 73],
320 'cloudflare' => ['label' => 'Cloudflare Integration', 'videoID' => 'uBPMn2mvnrs', 'duration' => 128],
321 'share' => ['label' => 'Delegate Access', 'videoID' => 'QxwaDxtRw-I', 'duration' => 79]
322 ];
323 ?>
324 <div class="row">
325 <div class="col">
326 <p>Advanced control and reports are available in your RabbitLoader account console.</p>
327 </div>
328 </div>
329 <div class="row mb-5">
330 <?php
331 foreach ($links as $hash => $item) {
332 $icon = ' <span class="dashicons dashicons-external mt-1"></span>';
333 $link = ' href="https://rabbitloader.com/account/#' . $hash . '" target="_blank" ';
334
335 echo '
336 <div class="col text-center">
337 <div class="rl-mfe-card">
338 <a class="btn btn-link" ' . $link . '>' . $item['label'] . $icon . ' </a>
339 <primer data-video-id="' . $item['videoID'] . '" data-duration="' . $item['duration'] . '"></primer>
340 </div>
341 </div>';
342 }
343 ?>
344 </div>
345 <?php
346 }
347
348 private static function show_cf_box()
349 {
350 if (empty($_SERVER['HTTP_CDN_LOOP']) || $_SERVER['HTTP_CDN_LOOP'] != 'cloudflare') {
351 return;
352 }
353 ?>
354 <div class="row mb-4">
355 <div class="col">
356 <div class="bg-white rounded p-4">
357 <div class="row">
358 <div class="col-8 text-secondary">
359 <h5 class="mt-0"><?php RL21UtilWP::_e('Using Cloudflare?'); ?></h5>
360 <span><?php RL21UtilWP::_e('If you are using Cloudflare, there are a few settings required in order to avoid conflicts and get the best performance.'); ?></span>
361 <primer data-video-id="uBPMn2mvnrs" data-duration="128"></primer>
362
363 <div class="mt-5">
364 <a target="_blank" class="rl-btn rl-btn-primary" href="https://rabbitloader.com/kb/cloudflare-settings-for-best-performance/"><?php RL21UtilWP::_e('Show recommended settings'); ?></a>
365 </div>
366
367 </div>
368 <div class="col-4 text-center">
369 <img src="<?php echo RABBITLOADER_PLUG_URL; ?>/assets/help.jpg" class="img-fluid" style="max-height:170px;" />
370 </div>
371 </div>
372 </div>
373 </div>
374 </div>
375 <?php
376 }
377 }
378
379 ?>
380