PluginProbe ʕ •ᴥ•ʔ
NitroPack – Performance, Page Speed & Cache Plugin for Core Web Vitals, CDN & Image Optimization / 1.19.0
NitroPack – Performance, Page Speed & Cache Plugin for Core Web Vitals, CDN & Image Optimization v1.19.0
1.19.8 1.19.7 1.19.6 1.19.5 trunk 1.10.0 1.10.1 1.10.2 1.10.3 1.10.4 1.11.0 1.12.0 1.13.0 1.14.0 1.15.0 1.15.1 1.15.2 1.15.3 1.16.0 1.16.1 1.16.2 1.16.3 1.16.4 1.16.5 1.16.6 1.16.7 1.16.8 1.17.0 1.17.6 1.17.7 1.17.8 1.17.9 1.18.0 1.18.1 1.18.2 1.18.3 1.18.4 1.18.5 1.18.6 1.18.7 1.18.8 1.18.9 1.19.0 1.19.1 1.19.2 1.19.3 1.19.4 1.3.19 1.3.20 1.4.0 1.4.1 1.5.0 1.5.1 1.5.10 1.5.11 1.5.12 1.5.13 1.5.14 1.5.15 1.5.16 1.5.17 1.5.18 1.5.19 1.5.2 1.5.3 1.5.4 1.5.5 1.5.6 1.5.7 1.5.8 1.5.9 1.6.0 1.6.1 1.7.0 1.7.1 1.8.0 1.8.1 1.8.3 1.9.0 1.9.1 1.9.2
nitropack / view / javascript / np_settings.js
nitropack / view / javascript Last commit date
admin_bar_menu.js 4 months ago elementor_cache_integration.js 5 months ago flowbite.min.js 1 year ago gravity_forms.js 1 year ago math_captcha.js 1 year ago nitropackUI.js 1 year ago np_notices.js 7 months ago np_safemode.js 1 year ago np_settings.js 4 months ago popper.min.js 1 year ago post_clear_cache.js 4 months ago preview_site.js 7 months ago system_report.js 4 months ago widgets_ajax.js 1 year ago
np_settings.js
936 lines
1 jQuery(document).ready(function ($) {
2 class nitropackSettings {
3 constructor() {
4 this.initial_settings = {
5 ajaxShortcodes: {
6 enabled: 0,
7 shortcodes: [],
8 },
9 cacheWarmUp: {
10 enabled: 0,
11 },
12 htmlCompression: {
13 enabled: 0,
14 },
15 bbCachePurgeSync: {
16 enabled: 0,
17 },
18 canEditorClearCache: {
19 enabled: 0,
20 },
21 cartCache: {
22 enabled: 0,
23 },
24 stockReduce: {
25 enabled: 0,
26 },
27 optimizationLevel: {
28 int: 0,
29 name: "",
30 },
31 };
32 //Settings
33 this.nitropackAddEventListeners();
34 this.purgeCacheClick();
35 this.optimizations();
36 this.optimizationModeClick();
37 this.autoPurgeCache();
38 this.cacheWarmUp();
39 this.enableCacheWarmup();
40 this.skipCacheWarmup();
41 this.setHTMLCompression();
42 this.beaverBuilder();
43 this.editorPurgeCache();
44 this.cartCache();
45 this.stockRefresh();
46 //shortcodes
47 this.ajaxShortcodes = this.ajaxShortcodes();
48 this.restoreConnection();
49 this.windowNotification();
50 this.clearResidualCache();
51 //unsaved changes
52 this.onPageLeave();
53 //must be last so we get updated copy of inital settings after all other settings init
54 this.unsavedChangesModal = false;
55 this.modified_settings = JSON.parse(JSON.stringify(this.initial_settings));
56 }
57 setupCacheEventListeners(nitroSelf) {
58 window.addEventListener("cache.invalidate.request", nitroSelf.clearCacheHandler("invalidate"));
59 window.addEventListener("cache.purge.request", nitroSelf.clearCacheHandler("purge"));
60 if ($("#np-onstate-cache-purge").length) {
61 window.addEventListener("cache.purge.success", function () {
62 $.post(
63 ajaxurl,
64 {
65 action: "nitropack_cookie_path_ajax",
66 nonce: np_settings.nitroNonce,
67 },
68 function (response) {
69 var resp = JSON.parse(response);
70 setTimeout(function () {
71 document.cookie =
72 "nitropack_apwarning=1; expires=Thu, 01 Jan 1970 00:00:01 GMT; path=" + resp.cookie_path + ";";
73 window.location.reload();
74 }, 1500);
75 },
76 );
77 });
78 } else {
79 window.addEventListener("cache.purge.success", () => setTimeout(() => nitroSelf.fetchOptimizations(), 1500));
80 }
81 window.addEventListener("cache.invalidate.success", () => setTimeout(() => nitroSelf.fetchOptimizations(), 1500));
82 }
83 nitropackAddEventListeners() {
84 const nitroSelf = this;
85
86 // Check if document is already complete
87 if (document.readyState === "complete") {
88 // Page already loaded - run immediately
89 this.setupCacheEventListeners(nitroSelf);
90 } else {
91 // Page still loading - wait for load event
92 window.addEventListener(
93 "load",
94 () => {
95 this.setupCacheEventListeners(nitroSelf);
96 },
97 { once: true },
98 );
99 }
100 }
101 /* AJAX purge/invalidate cache, used in nitropack/classes/WordPress/PurgeCache.php */
102 clearCacheHandler = (clearCacheAction) => {
103 return function (success, error) {
104 $.ajax({
105 url: ajaxurl,
106 type: "GET",
107 data: {
108 action: "nitropack_" + clearCacheAction + "_cache",
109 nonce: nitroNonce,
110 },
111 dataType: "json",
112 beforeSend: function () {
113 $("#optimizations-purge-cache").attr("disabled", true);
114 },
115 success: function (data) {
116 if (data.type === "success") {
117 NitropackUI.triggerToast("success", data.message);
118 window.dispatchEvent(new Event("cache." + clearCacheAction + ".success"));
119 } else {
120 NitropackUI.triggerToast("error", data.message);
121 window.dispatchEvent(new Event("cache." + clearCacheAction + ".error"));
122 }
123 },
124 error: function (data) {
125 NitropackUI.triggerToast("error", data.message);
126 window.dispatchEvent(new Event("cache." + clearCacheAction + ".error"));
127 },
128 complete: function () {
129 setTimeout(function () {
130 $("#optimizations-purge-cache").attr("disabled", false);
131 }, 3000);
132 },
133 });
134 };
135 };
136 purgeCacheClick() {
137 const nitroSelf = this;
138 $("#modal-purge-cache .modal-action").click(function (e) {
139 let purgeEvent = new Event("cache.purge.request");
140 window.dispatchEvent(purgeEvent);
141 });
142 }
143 /* Fetch optimizations data every 2 minutes in Dashboard => Optimized pages */
144 fetchOptimizations() {
145 $.post(
146 ajaxurl,
147 {
148 action: "nitropack_fetch_optimizations",
149 nonce: np_settings.nitroNonce,
150 },
151 function (resp) {
152 $("[data-last-cache-purge]").text(resp.data.last_cache_purge.timeAgo);
153 if (resp.data.last_cache_purge.reason) {
154 $("[data-purge-reason]").text(resp.data.last_cache_purge.reason);
155 $("[data-purge-reason]").attr("title", resp.data.last_cache_purge.reason);
156 $("#last-cache-purge-reason").show();
157 } else {
158 $("#last-cache-purge-reason").hide();
159 }
160 if (resp.data.pending_count) {
161 $("#pending-optimizations-count").text(resp.data.pending_count);
162 $("#pending-optimizations-section").show();
163 } else {
164 $("#pending-optimizations-section").hide();
165 }
166 $("[data-optimized-pages-total]").text(resp.data.optimized_pages.total);
167 },
168 );
169 }
170 optimizations() {
171 // Run every 120 seconds, starting after 120 seconds
172 setInterval(this.fetchOptimizations, 120000);
173 }
174 saveOptimizationMode = (mode_int, mode_name) => {
175 const nitroSelf = this;
176 $.post(
177 ajaxurl,
178 {
179 action: "nitropack_set_optimization_mode",
180 nonce: np_settings.nitroNonce,
181 mode_int,
182 mode_name,
183 },
184 function (response) {
185 var resp = JSON.parse(response);
186 if (resp.type == "success") {
187 nitroSelf.applyOptimizationCosmetics(mode_name);
188 NitropackUI.triggerToast(
189 "info",
190 'Optimization mode changed to <strong class="capitalized">' + mode_name + "</strong>.",
191 );
192 } else {
193 NitropackUI.triggerToast("error", resp.message);
194 }
195 },
196 );
197 };
198 applyOptimizationCosmetics(mode) {
199 const modes_btn = "#optimization-modes a";
200
201 $(modes_btn).removeClass("btn-primary active").addClass("btn-link");
202 $(modes_btn + '[data-mode="' + mode + '"]')
203 .addClass("btn-primary active")
204 .removeClass("btn-link");
205 $(".active-mode").text(mode);
206
207 $(".card-optimization-mode .tab-content").addClass("hidden");
208 $('.card-optimization-mode .tab-content[data-tab="' + mode + '-tab"].hidden').removeClass("hidden");
209 }
210 optimizationModeClick() {
211 const nitroSelf = this,
212 modal_wrapper = $("#modal-optimization-mode"),
213 modal_footer = modal_wrapper.find(".popup-footer"),
214 action_btn = modal_footer.find(".modal-action"),
215 modes_btn = "#optimization-modes a";
216
217 $(modes_btn).click(function () {
218 var mode_name = $(this).data("mode");
219 action_btn.data("mode", mode_name);
220 });
221 action_btn.click(function () {
222 var mode_name = $(this).data("mode"),
223 mode_int = $(modes_btn + '[data-mode="' + mode_name + '"]').index() + 1;
224 nitroSelf.saveOptimizationMode(mode_int, mode_name);
225 });
226 this.loadInitOptimizationMode();
227 }
228 loadInitOptimizationMode() {
229 const mode = $("#optimization-modes a.active").data("mode"),
230 mode_int = $("#optimization-modes a.active").index() + 1;
231 this.initial_settings.optimizationLevel.int = mode_int;
232 this.initial_settings.optimizationLevel.name = mode;
233 }
234 autoPurgeCache() {
235 $("#auto-purge-status").on("click", function (e) {
236 $.post(
237 ajaxurl,
238 {
239 action: "nitropack_set_auto_cache_purge_ajax",
240 nonce: nitroNonce,
241 autoCachePurgeStatus: $(this).is(":checked") ? 1 : 0,
242 },
243 function (response) {
244 var resp = JSON.parse(response);
245 NitropackUI.triggerToast(resp.type, resp.message);
246 },
247 );
248 });
249 }
250
251 cacheWarmUp() {
252 const setting_id = "#warmup-status",
253 msg_wrapper = $("#loading-warmup-status"),
254 msg_icon = msg_wrapper.find(".icon"),
255 msg_text = msg_wrapper.find(".msg"),
256 nitroSelf = this;
257
258 $(setting_id).change(function () {
259 if ($(this).is(":checked")) {
260 estimateWarmup();
261 } else {
262 disableWarmup();
263 }
264 });
265 var disableWarmup = () => {
266 $.post(
267 ajaxurl,
268 {
269 action: "nitropack_disable_warmup",
270 nonce: np_settings.nitroNonce,
271 },
272 function (response) {
273 var resp = JSON.parse(response);
274 if (resp.type == "success") {
275 nitroSelf.modified_settings.cacheWarmUp.enabled = 0;
276 NitropackUI.triggerToast("success", np_settings.success_msg);
277 } else {
278 NitropackUI.triggerToast("error", np_settings.error_msg);
279 }
280 },
281 );
282 };
283
284 var estimateWarmup = (id, retry) => {
285 id = id || null;
286 retry = retry || 0;
287 msg_wrapper.removeClass("hidden");
288 if (!id) {
289 msg_text.text(np_settings.est_cachewarmup_msg);
290 $.post(
291 ajaxurl,
292 {
293 action: "nitropack_estimate_warmup",
294 nonce: np_settings.nitroNonce,
295 },
296 function (response) {
297 var resp = JSON.parse(response);
298 if (resp.type == "success") {
299 setTimeout(
300 (function (id) {
301 estimateWarmup(id);
302 })(resp.res),
303 1000,
304 );
305 } else {
306 $(setting_id).prop("checked", true);
307 msg_text.text(resp.message);
308
309 msg_icon.attr("src", np_settings.nitro_plugin_url + "/view/images/info.svg");
310 setTimeout(function () {
311 msg_wrapper.addClass("hidden");
312 }, 3000);
313 }
314 },
315 );
316 } else {
317 $.post(
318 ajaxurl,
319 {
320 action: "nitropack_estimate_warmup",
321 estId: id,
322 nonce: np_settings.nitroNonce,
323 },
324 function (response) {
325 var resp = JSON.parse(response);
326 if (resp.type == "success") {
327 if (isNaN(resp.res) || resp.res == -1) {
328 // Still calculating
329 if (retry >= 10) {
330 $(setting_id).prop("checked", false);
331 msg_icon.attr("src", np_settings.nitro_plugin_url + "/view/images/info.svg");
332 msg_text.text(resp.message);
333
334 setTimeout(function () {
335 msg_wrapper.addClass("hidden");
336 }, 3000);
337 } else {
338 setTimeout(
339 (function (id, retry) {
340 estimateWarmup(id, retry);
341 })(id, retry + 1),
342 1000,
343 );
344 }
345 } else {
346 if (resp.res == 0) {
347 $(setting_id).prop("checked", false);
348 msg_icon.attr("src", np_settings.nitro_plugin_url + "/view/images/info.svg");
349 msg_text.text(resp.message);
350 setTimeout(function () {
351 msg_wrapper.addClass("hidden");
352 }, 3000);
353 } else {
354 enableWarmup();
355 }
356 }
357 } else {
358 msg_text.text(resp.message);
359 setTimeout(function () {
360 msg_wrapper.addClass("hidden");
361 }, 3000);
362 }
363 },
364 );
365 }
366 };
367 var enableWarmup = () => {
368 $.post(
369 ajaxurl,
370 {
371 action: "nitropack_enable_warmup",
372 nonce: np_settings.nitroNonce,
373 },
374 function (response) {
375 var resp = JSON.parse(response);
376 if (resp.type == "success") {
377 nitroSelf.initial_settings.cacheWarmUp.enabled = 1;
378 $(setting_id).prop("checked", true);
379 msg_wrapper.addClass("hidden");
380 NitropackUI.triggerToast("success", np_settings.success_msg);
381 } else {
382 setTimeout(enableWarmup, 1000);
383 }
384 },
385 );
386 };
387
388 var loadWarmupStatus = function () {
389 if ($("#warmup-status").is(":checked") == 1) {
390 nitroSelf.initial_settings.cacheWarmUp.enabled = 1;
391 } else {
392 nitroSelf.initial_settings.cacheWarmUp.enabled = 0;
393 }
394 };
395
396 loadWarmupStatus();
397 }
398 skipCacheWarmupAjax() {
399 $.post(
400 ajaxurl,
401 {
402 action: "nitropack_skip_cache_warmup",
403 nonce: np_settings.nitroNonce,
404 },
405 function (response) {
406 var resp = JSON.parse(response);
407 if (resp.type == "success") {
408 $(".cache-warmup.card").remove();
409 } else {
410 NitropackUI.triggerToast("error", np_settings.error_msg);
411 }
412 },
413 );
414 }
415 enableCacheWarmup() {
416 const nitroSelf = this;
417 $("#enable-cache-warmup").on("click", function () {
418 //enable CW
419 $("#warmup-status").prop("checked", true).trigger("change");
420 //dismiss notice forever
421 nitroSelf.skipCacheWarmupAjax();
422 });
423 }
424
425 skipCacheWarmup() {
426 const nitroSelf = this;
427 $("#skip-cache-warmup").on("click", function () {
428 nitroSelf.skipCacheWarmupAjax();
429 });
430 }
431 ajaxShortcodes() {
432 //main setting
433 const setting_id = "#ajax-shortcodes",
434 nitroSelf = this;
435 if ($(setting_id).is(":checked")) {
436 nitroSelf.initial_settings.ajaxShortcodes.enabled = 1;
437 }
438
439 $(setting_id).change(function () {
440 if ($(this).is(":checked")) {
441 ajaxShortcodeRequest(null, 1);
442 } else {
443 ajaxShortcodeRequest(null, 0);
444 }
445 });
446 //template for selected shortcodes tags
447 let select2 = $("#ajax-shortcodes-dropdown").select2({
448 selectOnClose: false,
449 tags: true,
450 multiple: true,
451 width: "100%",
452 placeholder: "Enter a shortcode",
453 templateSelection: shortcodeTagTemplate,
454 }),
455 shortcodes_val = select2.val();
456
457 /* Show the container when select2 is initialized to avoid flickering! */
458 if (nitroSelf.initial_settings.ajaxShortcodes.enabled) $(".ajax-shortcodes").removeClass("hidden");
459
460 if (shortcodes_val && shortcodes_val.length > 0) {
461 nitroSelf.initial_settings.ajaxShortcodes.shortcodes = select2.val();
462 } else {
463 nitroSelf.initial_settings.ajaxShortcodes.shortcodes = [];
464 }
465
466 select2.on("change", (event) => {
467 const selectedValues = $(event.target).val(); // Get selected values
468 this.modified_settings.ajaxShortcodes.shortcodes = selectedValues;
469 if (selectedValues.length === 0) {
470 $(".select2-search.select2-search--inline .select2-search__field").addClass("w-full");
471 } else {
472 $(".select2-search.select2-search--inline .select2-search__field").removeClass("w-full");
473 }
474 });
475 $(".select2-search.select2-search--inline .select2-search__field").addClass("w-full");
476 //select2
477 function shortcodeTagTemplate(item) {
478 if (!item.id) {
479 return item.text;
480 }
481 var $item = $(
482 '<span class="select2-selection__choice-inner">' +
483 item.text +
484 '<span class="np-select2-remove"></span>' +
485 "</span>",
486 );
487 return $item;
488 }
489 //remove single shortcode
490 $(".ajax-shortcodes").on("click", ".np-select2-remove", function () {
491 let valueToRemove = $(this).closest("li.select2-selection__choice").attr("title"),
492 newVals = select2.val().filter(function (item) {
493 return item !== valueToRemove;
494 });
495 select2.val(newVals).trigger("change");
496 });
497 //btn save click
498 $(".ajax-shortcodes #save-shortcodes").click(function () {
499 let shortcodes = $("#ajax-shortcodes-dropdown").val();
500 ajaxShortcodeRequest(shortcodes, null);
501 });
502
503 /* shortcodes - array of shortcodes or null
504 enabled - 1 or 0
505 */
506 const ajaxShortcodeRequest = function (shortcodes, enabled) {
507 let data_obj = {
508 action: "nitropack_set_ajax_shortcodes_ajax",
509 nonce: np_settings.nitroNonce,
510 shortcodes: Array.isArray(shortcodes) && shortcodes.length ? shortcodes : [JSON.stringify([])], // Ensure it's always an array
511 };
512
513 if (enabled !== null) data_obj.enabled = enabled;
514
515 const response = $.ajax({
516 url: ajaxurl,
517 type: "POST",
518 data: data_obj,
519 dataType: "json",
520 success: function (resp) {
521 if (resp.type == "success") {
522 if (enabled == 1) {
523 $(".ajax-shortcodes").removeClass("hidden");
524 nitroSelf.modified_settings.ajaxShortcodes.enabled = 1;
525 }
526 if (enabled == 0) {
527 $(".ajax-shortcodes").addClass("hidden");
528 nitroSelf.modified_settings.ajaxShortcodes.enabled = 0;
529 }
530 // Ensure we're setting an array in settings
531 if (Array.isArray(shortcodes) && shortcodes.length) {
532 nitroSelf.initial_settings.ajaxShortcodes.shortcodes = shortcodes;
533 } else {
534 nitroSelf.initial_settings.ajaxShortcodes.shortcodes = [];
535 }
536 NitropackUI.triggerToast("success", np_settings.success_msg);
537 } else {
538 NitropackUI.triggerToast("error", np_settings.error_msg);
539 }
540 },
541 });
542 return response;
543 };
544 return {
545 ajaxShortcodeRequest: ajaxShortcodeRequest,
546 };
547 }
548 // Function to omit 'enabled' property
549 omitEnabledProperty(obj) {
550 return Object.keys(obj).reduce((acc, key) => {
551 if (typeof obj[key] === "object" && obj[key] !== null) {
552 acc[key] = this.omitEnabledProperty(obj[key]);
553 } else if (key !== "enabled") {
554 acc[key] = obj[key];
555 }
556 return acc;
557 }, {});
558 }
559
560 // Function to check for unsaved changes, ignoring 'enabled' property
561 hasUnsavedChanges() {
562 const initialWithoutEnabled = this.omitEnabledProperty(this.initial_settings);
563 const modifiedWithoutEnabled = this.omitEnabledProperty(this.modified_settings);
564 return JSON.stringify(initialWithoutEnabled) !== JSON.stringify(modifiedWithoutEnabled);
565 }
566
567 // Function to handle page leave
568 onPageLeave() {
569 const nitroSelf = this;
570 window.onbeforeunload = function (event) {
571 if (
572 nitroSelf.hasUnsavedChanges() &&
573 !nitroSelf.unsavedChangesModal &&
574 nitroSelf.modified_settings.ajaxShortcodes.enabled === 1
575 ) {
576 event.preventDefault(); // show prompt
577 }
578 };
579 //a links - display modal
580 $(document).on("click", 'a[href]:not([target="_blank"])', function (event) {
581 if (nitroSelf.hasUnsavedChanges() && nitroSelf.modified_settings.ajaxShortcodes.enabled === 1) {
582 event.preventDefault();
583 const leaveUrl = this.href;
584 nitroSelf.showUnsavedChangesModal(() => {
585 ``;
586 window.location.href = leaveUrl;
587 });
588 }
589 });
590 }
591 // Show unsaved changes modal
592 showUnsavedChangesModal(onConfirm) {
593 const nitroSelf = this;
594 //vanilla js
595 const modalID = "modal-unsavedChanges",
596 $modal_target = document.getElementById(modalID),
597 modal_options = {
598 backdrop: "static",
599 backdropClasses: "nitro-backdrop",
600 closable: true,
601 onHide: () => {
602 this.unsavedChangesModal = false;
603 },
604 onShow: () => {
605 this.unsavedChangesModal = true;
606 },
607 },
608 instanceOptions = {
609 id: modalID,
610 },
611 modal = new Modal($modal_target, modal_options, instanceOptions);
612 //jquery
613 const modal_wrapper = $("#" + modalID),
614 x_button = modal_wrapper.find(".close-modal"),
615 modal_footer = modal_wrapper.find(".popup-footer"),
616 secondary_btn = modal_footer.find(".popup-close"),
617 action_btn = modal_footer.find(".btn-primary");
618 modal.show();
619
620 //no action
621 $(x_button).one("click", function () {
622 modal.hide();
623 });
624 //redirect without saving
625 $(secondary_btn).one("click", function () {
626 onConfirm();
627 modal.hide();
628 });
629 //save and redirect
630 $(action_btn).one("click", function () {
631 const ajaxRequest = nitroSelf.ajaxShortcodes.ajaxShortcodeRequest(
632 nitroSelf.modified_settings.ajaxShortcodes.shortcodes,
633 null,
634 );
635 ajaxRequest.done(function (response) {
636 if (response.type === "success") onConfirm();
637 });
638 ajaxRequest.fail(function () {
639 console.error("AJAX request failed.");
640 NitropackUI.triggerToast("error", "Error saving shortcodes.");
641 onConfirm();
642 });
643 modal.hide();
644 });
645 }
646 removeElement(array, value) {
647 const index = array.indexOf(value);
648 if (index !== -1) {
649 array.splice(index, 1);
650 }
651 }
652 autoDetectCompression() {
653 let msg_container = $("#compression-widget .msg-container"),
654 msg_box = msg_container.find(".msg"),
655 compression_setting = $("#compression-status"),
656 compression_btn = $("#compression-test-btn");
657 //add spinner here
658 msg_box.html(
659 '<img src="' +
660 np_settings.nitro_plugin_url +
661 '/view/images/loading.svg" alt="loading" class="icon"> ' +
662 np_settings.testing_compression,
663 );
664 compression_btn.addClass("hidden");
665 msg_container.removeClass("hidden");
666 $.post(
667 ajaxurl,
668 {
669 action: "nitropack_test_compression_ajax",
670 nonce: nitroNonce,
671 },
672 function (response) {
673 var resp = JSON.parse(response);
674
675 if (resp.type == "success") {
676 if (resp.hasCompression) {
677 // compression already enabled
678 compression_setting.attr("checked", false);
679 compression_setting.attr("disabled", true);
680 msg_box.text(np_settings.compression_already_enabled);
681 } else {
682 compression_setting.attr("checked", true);
683 compression_setting.attr("disabled", false);
684 msg_box.text(np_settings.compression_not_detected);
685 NitropackUI.triggerToast(resp.type, resp.message);
686 }
687 } else {
688 msg_box.text(np_settings.compression_not_determined);
689 }
690 setTimeout(function () {
691 msg_container.addClass("hidden");
692 compression_btn.removeClass("hidden");
693 }, 5000);
694 },
695 );
696 }
697 setHTMLCompression() {
698 const nitroSelf = this;
699 let enabled = $("#compression-status").is(":checked") ? 1 : 0;
700 nitroSelf.initial_settings.htmlCompression.enabled = enabled;
701 //on load check status
702 $(window).on("load", function () {
703 nitroSelf.autoDetectCompression();
704 });
705 $(document).on("click", "#compression-test-btn", (e) => {
706 e.preventDefault();
707 nitroSelf.autoDetectCompression();
708 });
709
710 //toggle on/off setting
711 $("#compression-status").on("click", function (e) {
712 $.post(
713 ajaxurl,
714 {
715 action: "nitropack_set_compression_ajax",
716 nonce: nitroNonce,
717 data: {
718 compressionStatus: $(this).is(":checked") ? 1 : 0,
719 },
720 },
721 function (response) {
722 var resp = JSON.parse(response);
723 NitropackUI.triggerToast(resp.type, resp.message);
724 },
725 );
726 });
727 }
728 beaverBuilder() {
729 let enabled = $("#bb-purge-status").is(":checked") ? 1 : 0;
730 this.initial_settings.bbCachePurgeSync.enabled = enabled;
731 $("#bb-purge-status").on("click", function (e) {
732 $.post(
733 ajaxurl,
734 {
735 action: "nitropack_set_bb_cache_purge_sync_ajax",
736 nonce: nitroNonce,
737 nonce: nitroNonce,
738 bbCachePurgeSyncStatus: $(this).is(":checked") ? 1 : 0,
739 },
740 function (response) {
741 var resp = JSON.parse(response);
742 NitropackUI.triggerToast(resp.type, resp.message);
743 },
744 );
745 });
746 }
747 editorPurgeCache() {
748 let enabled = $("#can-editor-clear-cache").is(":checked") ? 1 : 0;
749 this.initial_settings.canEditorClearCache.enabled = enabled;
750 $("#can-editor-clear-cache").on("click", function (e) {
751 $.post(
752 ajaxurl,
753 {
754 action: "nitropack_set_can_editor_clear_cache",
755 nonce: nitroNonce,
756 data: {
757 canEditorClearCache: $(this).is(":checked") ? 1 : 0,
758 },
759 },
760 function (response) {
761 var resp = JSON.parse(response);
762 NitropackUI.triggerToast(resp.type, resp.message);
763 },
764 );
765 });
766 }
767 cartCache() {
768 let enabled = $("#cart-cache-status").is(":checked") ? 1 : 0;
769 this.initial_settings.cartCache.enabled = enabled;
770 $("#cart-cache-status").on("click", function (e) {
771 $.post(
772 ajaxurl,
773 {
774 action: "nitropack_set_cart_cache_ajax",
775 nonce: nitroNonce,
776 cartCacheStatus: $(this).is(":checked") ? 1 : 0,
777 },
778 function (response) {
779 var resp = JSON.parse(response);
780 NitropackUI.triggerToast(resp.type, resp.message);
781 },
782 );
783 });
784 }
785 stockRefresh() {
786 let enabled = $("#woo-stock-reduce-status").is(":checked") ? 1 : 0;
787 this.initial_settings.stockReduce.enabled = enabled;
788
789 $("#woo-stock-reduce-status").on("click", function (e) {
790 $.post(
791 ajaxurl,
792 {
793 action: "nitropack_set_stock_reduce_status",
794 nonce: nitroNonce,
795 data: {
796 stockReduceStatus: $(this).is(":checked") ? 1 : 0,
797 },
798 },
799 function (response) {
800 var resp = JSON.parse(response);
801 NitropackUI.triggerToast(resp.type, resp.message);
802 },
803 );
804 });
805 }
806
807 restoreConnection() {
808 const loading_icon =
809 '<img src="' + np_settings.nitro_plugin_url + '/view/images/loading.svg" width="14" class="icon loading"/>',
810 success_icon =
811 '<img src="' + np_settings.nitro_plugin_url + '/view/images/check.svg" width="16" class="icon success"/>';
812
813 $("#nitro-restore-connection-btn").on("click", function () {
814 $.ajax({
815 url: ajaxurl,
816 type: "GET",
817 data: {
818 action: "nitropack_reconfigure_webhooks",
819 nonce: nitroNonce,
820 },
821 dataType: "json",
822 beforeSend: function () {
823 $("#nitro-restore-connection-btn").attr("disabled", true).html(loading_icon);
824 },
825 success: function (data) {
826 if (!data.status || data.status != "success") {
827 if (data.message) {
828 alert(data.message);
829 } else {
830 alert(
831 "We were unable to restore the connection. Please contact our support team to get this resolved.",
832 );
833 }
834 } else {
835 $("#nitro-restore-connection-btn").attr("disabled", true).html(success_icon);
836 NitropackUI.triggerToast("success", data.message);
837 }
838 },
839 complete: function () {
840 location.reload();
841 },
842 });
843 });
844 }
845 /* Was used in dashboard.php and oneclick.php */
846 loadDismissibleNotices() {
847 var $ = jQuery;
848
849 $(".nitro-notification.is-dismissible").each(function () {
850 var b = $(this),
851 c = $('<button type="button" class="notice-dismiss"><span class="screen-reader-text"></span></button>');
852 (c.on("click.wp-dismiss-notice", function ($) {
853 ($.preventDefault(),
854 b.fadeTo(100, 0, function () {
855 b.slideUp(100, function () {
856 b.remove();
857 });
858 }));
859 }),
860 b.append(c));
861 });
862 }
863
864 windowNotification() {
865 const nitroSelf = this;
866 window.Notification = ((_) => {
867 var timeout;
868 var display = (msg, type) => {
869 clearTimeout(timeout);
870 $(".nitro-notification").remove();
871 //tbd
872 $('[name="form"]').prepend(
873 '<div class="nitro-notification notification-' + type + '" is-dismissible"><p>' + msg + "</p></div>",
874 );
875
876 timeout = setTimeout((_) => {
877 $(".nitro-notification").remove();
878 }, 10000);
879
880 nitroSelf.loadDismissibleNotices();
881 };
882
883 return {
884 success: (msg) => {
885 display(msg, "success");
886 },
887 error: (msg) => {
888 display(msg, "error");
889 },
890 info: (msg) => {
891 display(msg, "info");
892 },
893 warning: (msg) => {
894 display(msg, "warning");
895 },
896 };
897 })();
898 }
899 clearResidualCache() {
900 let isClearing = false;
901 $(document).on("click", ".btn[nitropack-rc-data]", function (e) {
902 e.preventDefault();
903 if (isClearing) return;
904 let currentButton = $(this);
905 $.ajax({
906 url: ajaxurl,
907 type: "POST",
908 dataType: "text",
909 data: {
910 action: "nitropack_clear_residual_cache",
911 gde: currentButton.attr("nitropack-rc-data"),
912 nonce: nitroNonce,
913 },
914 beforeSend: function () {
915 isClearing = true;
916 },
917 success: function (resp) {
918 NitropackUI.triggerToast("success", np_settings.success_msg);
919 },
920 error: function (resp) {
921 NitropackUI.triggerToast("error", np_settings.success_msg);
922 },
923 complete: function () {
924 isClearing = false;
925 setTimeout(function () {
926 location.reload();
927 }, 3000);
928 },
929 });
930 });
931 }
932 }
933 const NitroPackSettings = new nitropackSettings();
934 window.NitroPackSettings = NitroPackSettings;
935 });
936