PluginProbe ʕ •ᴥ•ʔ
NitroPack – Performance, Page Speed & Cache Plugin for Core Web Vitals, CDN & Image Optimization / 1.19.3
NitroPack – Performance, Page Speed & Cache Plugin for Core Web Vitals, CDN & Image Optimization v1.19.3
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 3 months ago np_notices.js 3 months ago np_safemode.js 1 year ago np_settings.js 3 months ago popper.min.js 1 year ago post_clear_cache.js 4 months ago preview_site.js 3 months ago system_report.js 4 months ago widgets_ajax.js 1 year ago
np_settings.js
941 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 if (resp.status_code) {
689 let text = '[Error] HTTP Status Code: ' + resp.status_code;
690 msg_box.text(text);
691 } else {
692 msg_box.text(np_settings.compression_not_determined);
693 }
694 }
695 setTimeout(function () {
696 msg_container.addClass("hidden");
697 compression_btn.removeClass("hidden");
698 }, 5000);
699 },
700 );
701 }
702 setHTMLCompression() {
703 const nitroSelf = this;
704 let enabled = $("#compression-status").is(":checked") ? 1 : 0;
705 nitroSelf.initial_settings.htmlCompression.enabled = enabled;
706 //on load check status
707 $(window).on("load", function () {
708 nitroSelf.autoDetectCompression();
709 });
710 $(document).on("click", "#compression-test-btn", (e) => {
711 e.preventDefault();
712 nitroSelf.autoDetectCompression();
713 });
714
715 //toggle on/off setting
716 $("#compression-status").on("click", function (e) {
717 $.post(
718 ajaxurl,
719 {
720 action: "nitropack_set_compression_ajax",
721 nonce: nitroNonce,
722 data: {
723 compressionStatus: $(this).is(":checked") ? 1 : 0,
724 },
725 },
726 function (response) {
727 var resp = JSON.parse(response);
728 NitropackUI.triggerToast(resp.type, resp.message);
729 },
730 );
731 });
732 }
733 beaverBuilder() {
734 let enabled = $("#bb-purge-status").is(":checked") ? 1 : 0;
735 this.initial_settings.bbCachePurgeSync.enabled = enabled;
736 $("#bb-purge-status").on("click", function (e) {
737 $.post(
738 ajaxurl,
739 {
740 action: "nitropack_set_bb_cache_purge_sync_ajax",
741 nonce: nitroNonce,
742 nonce: nitroNonce,
743 bbCachePurgeSyncStatus: $(this).is(":checked") ? 1 : 0,
744 },
745 function (response) {
746 var resp = JSON.parse(response);
747 NitropackUI.triggerToast(resp.type, resp.message);
748 },
749 );
750 });
751 }
752 editorPurgeCache() {
753 let enabled = $("#can-editor-clear-cache").is(":checked") ? 1 : 0;
754 this.initial_settings.canEditorClearCache.enabled = enabled;
755 $("#can-editor-clear-cache").on("click", function (e) {
756 $.post(
757 ajaxurl,
758 {
759 action: "nitropack_set_can_editor_clear_cache",
760 nonce: nitroNonce,
761 data: {
762 canEditorClearCache: $(this).is(":checked") ? 1 : 0,
763 },
764 },
765 function (response) {
766 var resp = JSON.parse(response);
767 NitropackUI.triggerToast(resp.type, resp.message);
768 },
769 );
770 });
771 }
772 cartCache() {
773 let enabled = $("#cart-cache-status").is(":checked") ? 1 : 0;
774 this.initial_settings.cartCache.enabled = enabled;
775 $("#cart-cache-status").on("click", function (e) {
776 $.post(
777 ajaxurl,
778 {
779 action: "nitropack_set_cart_cache_ajax",
780 nonce: nitroNonce,
781 cartCacheStatus: $(this).is(":checked") ? 1 : 0,
782 },
783 function (response) {
784 var resp = JSON.parse(response);
785 NitropackUI.triggerToast(resp.type, resp.message);
786 },
787 );
788 });
789 }
790 stockRefresh() {
791 let enabled = $("#woo-stock-reduce-status").is(":checked") ? 1 : 0;
792 this.initial_settings.stockReduce.enabled = enabled;
793
794 $("#woo-stock-reduce-status").on("click", function (e) {
795 $.post(
796 ajaxurl,
797 {
798 action: "nitropack_set_stock_reduce_status",
799 nonce: nitroNonce,
800 data: {
801 stockReduceStatus: $(this).is(":checked") ? 1 : 0,
802 },
803 },
804 function (response) {
805 var resp = JSON.parse(response);
806 NitropackUI.triggerToast(resp.type, resp.message);
807 },
808 );
809 });
810 }
811
812 restoreConnection() {
813 const loading_icon =
814 '<img src="' + np_settings.nitro_plugin_url + '/view/images/loading.svg" width="14" class="icon loading"/>',
815 success_icon =
816 '<img src="' + np_settings.nitro_plugin_url + '/view/images/check.svg" width="16" class="icon success"/>';
817
818 $("#nitro-restore-connection-btn").on("click", function () {
819 $.ajax({
820 url: ajaxurl,
821 type: "GET",
822 data: {
823 action: "nitropack_reconfigure_webhooks",
824 nonce: nitroNonce,
825 },
826 dataType: "json",
827 beforeSend: function () {
828 $("#nitro-restore-connection-btn").attr("disabled", true).html(loading_icon);
829 },
830 success: function (data) {
831 if (!data.status || data.status != "success") {
832 if (data.message) {
833 alert(data.message);
834 } else {
835 alert(
836 "We were unable to restore the connection. Please contact our support team to get this resolved.",
837 );
838 }
839 } else {
840 $("#nitro-restore-connection-btn").attr("disabled", true).html(success_icon);
841 NitropackUI.triggerToast("success", data.message);
842 }
843 },
844 complete: function () {
845 location.reload();
846 },
847 });
848 });
849 }
850 /* Was used in dashboard.php and oneclick.php */
851 loadDismissibleNotices() {
852 var $ = jQuery;
853
854 $(".nitro-notification.is-dismissible").each(function () {
855 var b = $(this),
856 c = $('<button type="button" class="notice-dismiss"><span class="screen-reader-text"></span></button>');
857 (c.on("click.wp-dismiss-notice", function ($) {
858 ($.preventDefault(),
859 b.fadeTo(100, 0, function () {
860 b.slideUp(100, function () {
861 b.remove();
862 });
863 }));
864 }),
865 b.append(c));
866 });
867 }
868
869 windowNotification() {
870 const nitroSelf = this;
871 window.Notification = ((_) => {
872 var timeout;
873 var display = (msg, type) => {
874 clearTimeout(timeout);
875 $(".nitro-notification").remove();
876 //tbd
877 $('[name="form"]').prepend(
878 '<div class="nitro-notification notification-' + type + '" is-dismissible"><p>' + msg + "</p></div>",
879 );
880
881 timeout = setTimeout((_) => {
882 $(".nitro-notification").remove();
883 }, 10000);
884
885 nitroSelf.loadDismissibleNotices();
886 };
887
888 return {
889 success: (msg) => {
890 display(msg, "success");
891 },
892 error: (msg) => {
893 display(msg, "error");
894 },
895 info: (msg) => {
896 display(msg, "info");
897 },
898 warning: (msg) => {
899 display(msg, "warning");
900 },
901 };
902 })();
903 }
904 clearResidualCache() {
905 let isClearing = false;
906 $(document).on("click", ".btn[nitropack-rc-data]", function (e) {
907 e.preventDefault();
908 if (isClearing) return;
909 let currentButton = $(this);
910 $.ajax({
911 url: ajaxurl,
912 type: "POST",
913 dataType: "text",
914 data: {
915 action: "nitropack_clear_residual_cache",
916 gde: currentButton.attr("nitropack-rc-data"),
917 nonce: nitroNonce,
918 },
919 beforeSend: function () {
920 isClearing = true;
921 },
922 success: function (resp) {
923 NitropackUI.triggerToast("success", np_settings.success_msg);
924 },
925 error: function (resp) {
926 NitropackUI.triggerToast("error", np_settings.success_msg);
927 },
928 complete: function () {
929 isClearing = false;
930 setTimeout(function () {
931 location.reload();
932 }, 3000);
933 },
934 });
935 });
936 }
937 }
938 const NitroPackSettings = new nitropackSettings();
939 window.NitroPackSettings = NitroPackSettings;
940 });
941