PluginProbe ʕ •ᴥ•ʔ
NitroPack – Performance, Page Speed & Cache Plugin for Core Web Vitals, CDN & Image Optimization / 1.17.6
NitroPack – Performance, Page Speed & Cache Plugin for Core Web Vitals, CDN & Image Optimization v1.17.6
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 1 year ago flowbite.min.js 2 years ago gravity_forms.js 3 years ago math_captcha.js 3 years ago metabox.js 2 years ago nitropackUI.js 2 years ago np_notices.js 1 year ago np_safemode.js 2 years ago np_settings.js 1 year ago popper.min.js 4 years ago widgets_ajax.js 3 years ago
np_settings.js
560 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 };
13 //run settings
14 this.cacheWarmUp();
15 this.ajaxShortcodes = this.ajaxShortcodes();
16 //logger
17 this.loggerToggle();
18 this.setLoggerLevel();
19 this.archiveLogs();
20 //unsaved changes
21 this.onPageLeave();
22 //must be last so we get updated copy
23 this.unsavedChangesModal = false;
24 this.modified_settings = JSON.parse(
25 JSON.stringify(this.initial_settings)
26 );
27 }
28 cacheWarmUp() {
29 const setting_id = "#warmup-status",
30 msg_wrapper = $("#loading-warmup-status"),
31 msg_icon = msg_wrapper.find(".icon"),
32 msg_text = msg_wrapper.find(".msg"),
33 nitroSelf = this;
34
35 $(setting_id).change(function () {
36 if ($(this).is(":checked")) {
37 estimateWarmup();
38 } else {
39 disableWarmup();
40 }
41 });
42 var disableWarmup = () => {
43 $.post(
44 ajaxurl,
45 {
46 action: "nitropack_disable_warmup",
47 nonce: np_settings.nitroNonce,
48 },
49 function (response) {
50 var resp = JSON.parse(response);
51 if (resp.type == "success") {
52 nitroSelf.modified_settings.cacheWarmUp.enabled = 0;
53 NitropackUI.triggerToast("success", np_settings.success_msg);
54 } else {
55 NitropackUI.triggerToast("error", np_settings.error_msg);
56 }
57 }
58 );
59 };
60
61 var estimateWarmup = (id, retry) => {
62 id = id || null;
63 retry = retry || 0;
64 msg_wrapper.removeClass("hidden");
65 if (!id) {
66 msg_text.text(np_settings.est_cachewarmup_msg);
67 $.post(
68 ajaxurl,
69 {
70 action: "nitropack_estimate_warmup",
71 nonce: np_settings.nitroNonce,
72 },
73 function (response) {
74 var resp = JSON.parse(response);
75 if (resp.type == "success") {
76 setTimeout(
77 (function (id) {
78 estimateWarmup(id);
79 })(resp.res),
80 1000
81 );
82 } else {
83 $(setting_id).prop("checked", true);
84 msg_text.text(resp.message);
85
86 msg_icon.attr(
87 "src",
88 np_settings.nitro_plugin_url + "/view/images/info.svg"
89 );
90 setTimeout(function () {
91 msg_wrapper.addClass("hidden");
92 }, 3000);
93 }
94 }
95 );
96 } else {
97 $.post(
98 ajaxurl,
99 {
100 action: "nitropack_estimate_warmup",
101 estId: id,
102 nonce: np_settings.nitroNonce,
103 },
104 function (response) {
105 var resp = JSON.parse(response);
106 if (resp.type == "success") {
107 if (isNaN(resp.res) || resp.res == -1) {
108 // Still calculating
109 if (retry >= 10) {
110 $(setting_id).prop("checked", false);
111 msg_icon.attr(
112 "src",
113 np_settings.nitro_plugin_url + "/view/images/info.svg"
114 );
115 msg_text.text(resp.message);
116
117 setTimeout(function () {
118 msg_wrapper.addClass("hidden");
119 }, 3000);
120 } else {
121 setTimeout(
122 (function (id, retry) {
123 estimateWarmup(id, retry);
124 })(id, retry + 1),
125 1000
126 );
127 }
128 } else {
129 if (resp.res == 0) {
130 $(setting_id).prop("checked", false);
131 msg_icon.attr(
132 "src",
133 np_settings.nitro_plugin_url + "/view/images/info.svg"
134 );
135 msg_text.text(resp.message);
136 setTimeout(function () {
137 msg_wrapper.addClass("hidden");
138 }, 3000);
139 } else {
140 enableWarmup();
141 }
142 }
143 } else {
144 msg_text.text(resp.message);
145 setTimeout(function () {
146 msg_wrapper.addClass("hidden");
147 }, 3000);
148 }
149 }
150 );
151 }
152 };
153 var enableWarmup = () => {
154 $.post(
155 ajaxurl,
156 {
157 action: "nitropack_enable_warmup",
158 nonce: np_settings.nitroNonce,
159 },
160 function (response) {
161 var resp = JSON.parse(response);
162 if (resp.type == "success") {
163 nitroSelf.modified_settings.cacheWarmUp.enabled = 1;
164 $(setting_id).prop("checked", true);
165 msg_wrapper.addClass("hidden");
166 NitropackUI.triggerToast("success", np_settings.success_msg);
167 } else {
168 setTimeout(enableWarmup, 1000);
169 }
170 }
171 );
172 };
173
174 var loadWarmupStatus = function () {
175 $.ajax({
176 url: ajaxurl,
177 type: "POST",
178 data: {
179 action: "nitropack_warmup_stats",
180 nonce: np_settings.nitroNonce,
181 },
182 dataType: "json",
183 success: function (resp) {
184 if (resp.type == "success") {
185 nitroSelf.initial_settings.cacheWarmUp.enabled = 1;
186 nitroSelf.modified_settings.cacheWarmUp.enabled = 1;
187 $(setting_id).prop("checked", !!resp.stats.status);
188 msg_wrapper.addClass("hidden");
189 } else {
190 setTimeout(loadWarmupStatus, 500);
191 }
192 },
193 });
194 };
195
196 if ($(setting_id).length) loadWarmupStatus();
197 }
198 ajaxShortcodes() {
199 //main setting
200 const setting_id = "#ajax-shortcodes",
201 nitroSelf = this;
202 if ($(setting_id).is(":checked")) {
203 nitroSelf.initial_settings.ajaxShortcodes.enabled = 1;
204 }
205
206 $(setting_id).change(function () {
207 if ($(this).is(":checked")) {
208 ajaxShortcodeRequest(null, 1);
209 } else {
210 ajaxShortcodeRequest(null, 0);
211 }
212 });
213 //template for selected shortcodes tags
214 let select2 = $("#ajax-shortcodes-dropdown").select2({
215 selectOnClose: false,
216 tags: true,
217 multiple: true,
218 width: "100%",
219 placeholder: "Enter a shortcode",
220 templateSelection: shortcodeTagTemplate,
221 });
222 nitroSelf.initial_settings.ajaxShortcodes.shortcodes.push(select2.val());
223
224 select2.on("change", (event) => {
225 const selectedValues = $(event.target).val(); // Get selected values
226 this.modified_settings.ajaxShortcodes.shortcodes = selectedValues;
227 if (selectedValues.length === 0) {
228 $(
229 ".select2-search.select2-search--inline .select2-search__field"
230 ).addClass("w-full");
231 } else {
232 $(
233 ".select2-search.select2-search--inline .select2-search__field"
234 ).removeClass("w-full");
235 }
236 });
237 $(
238 ".select2-search.select2-search--inline .select2-search__field"
239 ).addClass("w-full");
240 //select2
241 function shortcodeTagTemplate(item) {
242 if (!item.id) {
243 return item.text;
244 }
245 var $item = $(
246 '<span class="select2-selection__choice-inner">' +
247 item.text +
248 '<span class="np-select2-remove"></span>' +
249 "</span>"
250 );
251 return $item;
252 }
253 //remove single shortcode
254 $(".ajax-shortcodes").on("click", ".np-select2-remove", function () {
255 let valueToRemove = $(this)
256 .closest("li.select2-selection__choice")
257 .attr("title"),
258 newVals = select2.val().filter(function (item) {
259 return item !== valueToRemove;
260 });
261 select2.val(newVals).trigger("change");
262 });
263 //btn save click
264 $(".ajax-shortcodes #save-shortcodes").click(function () {
265 let shortcodes = $("#ajax-shortcodes-dropdown").val();
266 ajaxShortcodeRequest(shortcodes, null);
267 });
268
269 /* shortcodes - array of shortcodes or null
270 enabled - 1 or 0
271 */
272 const ajaxShortcodeRequest = function (shortcodes, enabled) {
273 let data_obj = {
274 action: "nitropack_set_ajax_shortcodes_ajax",
275 nonce: np_settings.nitroNonce,
276 };
277
278 if (Array.isArray(shortcodes)) {
279 if (shortcodes.length == 0) {
280 data_obj.shortcodes = [""];
281 } else {
282 data_obj.shortcodes = shortcodes;
283 }
284 }
285 if (enabled !== null) data_obj.enabled = enabled;
286
287 const response = $.ajax({
288 url: ajaxurl,
289 type: "POST",
290 data: data_obj,
291 dataType: "json",
292 success: function (resp) {
293 if (resp.type == "success") {
294 if (enabled == 1) {
295 $(".ajax-shortcodes").removeClass("hidden");
296 nitroSelf.modified_settings.ajaxShortcodes.enabled = 1;
297 }
298 if (enabled == 0) {
299 $(".ajax-shortcodes").addClass("hidden");
300 nitroSelf.modified_settings.ajaxShortcodes.enabled = 0;
301 }
302 if (shortcodes) {
303 nitroSelf.initial_settings.ajaxShortcodes.shortcodes =
304 shortcodes;
305 } else if (data_obj.shortcods == [""]) {
306 nitroSelf.initial_settings.ajaxShortcodes.shortcodes = [];
307 }
308 NitropackUI.triggerToast("success", np_settings.success_msg);
309 } else {
310 NitropackUI.triggerToast("error", np_settings.error_msg);
311 }
312 },
313 });
314 return response;
315 };
316 return {
317 ajaxShortcodeRequest: ajaxShortcodeRequest,
318 };
319 }
320 // Function to omit 'enabled' property
321 omitEnabledProperty(obj) {
322 return Object.keys(obj).reduce((acc, key) => {
323 if (typeof obj[key] === "object" && obj[key] !== null) {
324 acc[key] = this.omitEnabledProperty(obj[key]);
325 } else if (key !== "enabled") {
326 acc[key] = obj[key];
327 }
328 return acc;
329 }, {});
330 }
331
332 // Function to check for unsaved changes, ignoring 'enabled' property
333 hasUnsavedChanges() {
334 const initialWithoutEnabled = this.omitEnabledProperty(
335 this.initial_settings
336 );
337 const modifiedWithoutEnabled = this.omitEnabledProperty(
338 this.modified_settings
339 );
340 return (
341 JSON.stringify(initialWithoutEnabled) !==
342 JSON.stringify(modifiedWithoutEnabled)
343 );
344 }
345
346 // Function to handle page leave
347 onPageLeave() {
348 const nitroSelf = this;
349 window.onbeforeunload = function (event) {
350 if (
351 nitroSelf.hasUnsavedChanges() &&
352 !nitroSelf.unsavedChangesModal &&
353 nitroSelf.modified_settings.ajaxShortcodes.enabled === 1
354 ) {
355 event.preventDefault(); // show prompt
356 }
357 };
358 //a links - display modal
359 $(document).on(
360 "click",
361 'a[href]:not([target="_blank"])',
362 function (event) {
363 if (
364 nitroSelf.hasUnsavedChanges() &&
365 nitroSelf.modified_settings.ajaxShortcodes.enabled === 1
366 ) {
367 event.preventDefault();
368 const leaveUrl = this.href;
369 nitroSelf.showUnsavedChangesModal(() => {
370 window.location.href = leaveUrl;
371 });
372 }
373 }
374 );
375 }
376 // Show unsaved changes modal
377 showUnsavedChangesModal(onConfirm) {
378 const nitroSelf = this;
379 //vanilla js
380 const modalID = "modal-unsavedChanges",
381 $modal_target = document.getElementById(modalID),
382 modal_options = {
383 backdrop: "static",
384 backdropClasses: "nitro-backdrop",
385 closable: true,
386 onHide: () => {
387 this.unsavedChangesModal = false;
388 },
389 onShow: () => {
390 this.unsavedChangesModal = true;
391 },
392 },
393 instanceOptions = {
394 id: modalID,
395 },
396 modal = new Modal($modal_target, modal_options, instanceOptions);
397 //jquery
398 const modal_wrapper = $("#" + modalID),
399 x_button = modal_wrapper.find(".close-modal"),
400 modal_footer = modal_wrapper.find(".popup-footer"),
401 secondary_btn = modal_footer.find(".popup-close"),
402 action_btn = modal_footer.find(".btn-primary");
403 modal.show();
404
405 //no action
406 $(x_button).one("click", function () {
407 modal.hide();
408 });
409 //redirect without saving
410 $(secondary_btn).one("click", function () {
411 onConfirm();
412 modal.hide();
413 });
414 //save and redirect
415 $(action_btn).one("click", function () {
416 const ajaxRequest = nitroSelf.ajaxShortcodes.ajaxShortcodeRequest(
417 nitroSelf.modified_settings.ajaxShortcodes.shortcodes,
418 null
419 );
420 ajaxRequest.done(function (response) {
421 if (response.type === "success") onConfirm();
422 });
423 ajaxRequest.fail(function () {
424 console.error("AJAX request failed.");
425 NitropackUI.triggerToast("error", "Error saving shortcodes.");
426 onConfirm();
427 });
428 modal.hide();
429 });
430 }
431 removeElement(array, value) {
432 const index = array.indexOf(value);
433 if (index !== -1) {
434 array.splice(index, 1);
435 }
436 }
437 loggerToggle() {
438 const radio = $("#minimum-log-level-status"),
439 widget = $("#minimum-log-level-widget"),
440 fancy_radios = widget.find(".fancy-radio"),
441 fancy_radios_container = widget.find(".fancy-radio-container");
442 let minimum_log_level;
443
444 radio.on("change", function () {
445 const self = $(this);
446 if (self.is(":checked")) {
447 minimum_log_level = 3;
448 } else {
449 minimum_log_level = null;
450 }
451 $.post(
452 ajaxurl,
453 {
454 action: "nitropack_set_log_level_ajax",
455 minimum_log_level: minimum_log_level,
456 nonce: np_settings.nitroNonce,
457 },
458 function (response) {
459 var resp = JSON.parse(response);
460 if (resp.type == "success") {
461 if (self.is(":checked")) {
462 $(".logging").removeClass("hidden");
463 fancy_radios_container.removeClass("selected");
464 fancy_radios.removeClass("selected");
465 widget
466 .find(
467 '.fancy-radio-container[data-value="' +
468 minimum_log_level +
469 '"'
470 )
471 .addClass("selected");
472 widget
473 .find(
474 '.fancy-radio-container[data-value="' +
475 minimum_log_level +
476 '"'
477 )
478 .find(".fancy-radio")
479 .addClass("selected");
480 } else {
481 fancy_radios.removeClass("selected");
482 $(".logging").addClass("hidden");
483 }
484
485 NitropackUI.triggerToast("success", np_settings.success_msg);
486 } else {
487 NitropackUI.triggerToast("error", np_settings.success_msg);
488 $(this).prop("checked", false);
489 }
490 }
491 );
492 });
493 }
494 /* Set logger level */
495 setLoggerLevel() {
496 const widget = $("#minimum-log-level-widget"),
497 fancy_radios_container = widget.find(".fancy-radio-container"),
498 fancy_radios = widget.find(".fancy-radio");
499 let initial_minimum_log_level = widget
500 .find(".fancy-radio-container.selected")
501 .data("value");
502
503 fancy_radios_container.click(function () {
504 let fancy_radio_container = $(this),
505 fancy_radio = $(this).find(".fancy-radio"),
506 minimum_log_level = fancy_radio_container.data("value");
507 if (minimum_log_level === initial_minimum_log_level) return;
508
509 $.post(
510 ajaxurl,
511 {
512 action: "nitropack_set_log_level_ajax",
513 minimum_log_level: minimum_log_level,
514 nonce: np_settings.nitroNonce,
515 },
516 function (response) {
517 var resp = JSON.parse(response);
518 if (resp.type == "success") {
519 //container
520 fancy_radios_container.removeClass("selected");
521 fancy_radio_container.addClass("selected");
522 //custom radios
523 fancy_radios.removeClass("selected");
524 fancy_radio.addClass("selected");
525 initial_minimum_log_level = minimum_log_level;
526 NitropackUI.triggerToast("success", np_settings.success_msg);
527 } else {
528 NitropackUI.triggerToast("error", np_settings.success_msg);
529 $(this).prop("checked", false);
530 }
531 }
532 );
533 });
534 }
535 /* Zips all logs and downloads them */
536 archiveLogs() {
537 $(".archive-logs").click(function (e) {
538 e.preventDefault();
539 $.post(
540 ajaxurl,
541 {
542 action: "nitropack_archive_logs_ajax",
543 nonce: np_settings.nitroNonce,
544 },
545 function (response) {
546 var resp = JSON.parse(response);
547 if (resp.type == "success") {
548 window.location.href = resp.url;
549 NitropackUI.triggerToast("success", np_settings.success_msg);
550 } else {
551 NitropackUI.triggerToast("error", np_settings.success_msg);
552 }
553 }
554 );
555 });
556 }
557 }
558 const NitroPackSettings = new nitropackSettings();
559 });
560