admin_bar_menu.js
1 year 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
metabox.js
1 year ago
nitropackUI.js
1 year ago
np_notices.js
7 months ago
np_safemode.js
1 year ago
np_settings.js
5 months ago
popper.min.js
1 year ago
preview_site.js
7 months ago
widgets_ajax.js
1 year ago
np_settings.js
1044 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 | //logger |
| 52 | this.loggerToggle(); |
| 53 | this.setLoggerLevel(); |
| 54 | this.archiveLogs(); |
| 55 | //unsaved changes |
| 56 | this.onPageLeave(); |
| 57 | //must be last so we get updated copy of inital settings after all other settings init |
| 58 | this.unsavedChangesModal = false; |
| 59 | this.modified_settings = JSON.parse(JSON.stringify(this.initial_settings)); |
| 60 | } |
| 61 | setupCacheEventListeners(nitroSelf) { |
| 62 | window.addEventListener("cache.invalidate.request", nitroSelf.clearCacheHandler("invalidate")); |
| 63 | window.addEventListener("cache.purge.request", nitroSelf.clearCacheHandler("purge")); |
| 64 | if ($("#np-onstate-cache-purge").length) { |
| 65 | window.addEventListener("cache.purge.success", function () { |
| 66 | $.post( |
| 67 | ajaxurl, |
| 68 | { |
| 69 | action: "nitropack_cookie_path_ajax", |
| 70 | nonce: np_settings.nitroNonce, |
| 71 | }, |
| 72 | function (response) { |
| 73 | var resp = JSON.parse(response); |
| 74 | setTimeout(function () { |
| 75 | document.cookie = |
| 76 | "nitropack_apwarning=1; expires=Thu, 01 Jan 1970 00:00:01 GMT; path=" + resp.cookie_path + ";"; |
| 77 | window.location.reload(); |
| 78 | }, 1500); |
| 79 | } |
| 80 | ); |
| 81 | }); |
| 82 | } else { |
| 83 | window.addEventListener("cache.purge.success", () => setTimeout(() => nitroSelf.fetchOptimizations(), 1500)); |
| 84 | } |
| 85 | window.addEventListener("cache.invalidate.success", () => setTimeout(() => nitroSelf.fetchOptimizations(), 1500)); |
| 86 | } |
| 87 | nitropackAddEventListeners() { |
| 88 | const nitroSelf = this; |
| 89 | |
| 90 | // Check if document is already complete |
| 91 | if (document.readyState === "complete") { |
| 92 | // Page already loaded - run immediately |
| 93 | this.setupCacheEventListeners(nitroSelf); |
| 94 | } else { |
| 95 | // Page still loading - wait for load event |
| 96 | window.addEventListener( |
| 97 | "load", |
| 98 | () => { |
| 99 | this.setupCacheEventListeners(nitroSelf); |
| 100 | }, |
| 101 | { once: true } |
| 102 | ); |
| 103 | } |
| 104 | } |
| 105 | /* AJAX purge/invalidate cache */ |
| 106 | clearCacheHandler = (clearCacheAction) => { |
| 107 | return function (success, error) { |
| 108 | $.ajax({ |
| 109 | url: ajaxurl, |
| 110 | type: "GET", |
| 111 | data: { |
| 112 | action: "nitropack_" + clearCacheAction + "_cache", |
| 113 | nonce: nitroNonce, |
| 114 | }, |
| 115 | dataType: "json", |
| 116 | beforeSend: function () { |
| 117 | $("#optimizations-purge-cache").attr("disabled", true); |
| 118 | }, |
| 119 | success: function (data) { |
| 120 | if (data.type === "success") { |
| 121 | NitropackUI.triggerToast("success", data.message); |
| 122 | window.dispatchEvent(new Event("cache." + clearCacheAction + ".success")); |
| 123 | } else { |
| 124 | NitropackUI.triggerToast("error", data.message); |
| 125 | window.dispatchEvent(new Event("cache." + clearCacheAction + ".error")); |
| 126 | } |
| 127 | }, |
| 128 | error: function (data) { |
| 129 | NitropackUI.triggerToast("error", data.message); |
| 130 | window.dispatchEvent(new Event("cache." + clearCacheAction + ".error")); |
| 131 | }, |
| 132 | complete: function () { |
| 133 | setTimeout(function () { |
| 134 | $("#optimizations-purge-cache").attr("disabled", false); |
| 135 | }, 3000); |
| 136 | }, |
| 137 | }); |
| 138 | }; |
| 139 | }; |
| 140 | purgeCacheClick() { |
| 141 | const nitroSelf = this; |
| 142 | $("#modal-purge-cache .modal-action").click(function (e) { |
| 143 | let purgeEvent = new Event("cache.purge.request"); |
| 144 | window.dispatchEvent(purgeEvent); |
| 145 | }); |
| 146 | } |
| 147 | /* Fetch optimizations data every 2 minutes in Dashboard => Optimized pages */ |
| 148 | fetchOptimizations() { |
| 149 | $.post( |
| 150 | ajaxurl, |
| 151 | { |
| 152 | action: "nitropack_fetch_optimizations", |
| 153 | nonce: np_settings.nitroNonce, |
| 154 | }, |
| 155 | function (resp) { |
| 156 | $("[data-last-cache-purge]").text(resp.data.last_cache_purge.timeAgo); |
| 157 | if (resp.data.last_cache_purge.reason) { |
| 158 | $("[data-purge-reason]").text(resp.data.last_cache_purge.reason); |
| 159 | $("[data-purge-reason]").attr("title", resp.data.last_cache_purge.reason); |
| 160 | $("#last-cache-purge-reason").show(); |
| 161 | } else { |
| 162 | $("#last-cache-purge-reason").hide(); |
| 163 | } |
| 164 | if (resp.data.pending_count) { |
| 165 | $("#pending-optimizations-count").text(resp.data.pending_count); |
| 166 | $("#pending-optimizations-section").show(); |
| 167 | } else { |
| 168 | $("#pending-optimizations-section").hide(); |
| 169 | } |
| 170 | $("[data-optimized-pages-total]").text(resp.data.optimized_pages.total); |
| 171 | } |
| 172 | ); |
| 173 | } |
| 174 | optimizations() { |
| 175 | // Run every 120 seconds, starting after 120 seconds |
| 176 | setInterval(this.fetchOptimizations, 120000); |
| 177 | } |
| 178 | saveOptimizationMode = (mode_int, mode_name) => { |
| 179 | const nitroSelf = this; |
| 180 | $.post( |
| 181 | ajaxurl, |
| 182 | { |
| 183 | action: "nitropack_set_optimization_mode", |
| 184 | nonce: np_settings.nitroNonce, |
| 185 | mode_int, |
| 186 | mode_name, |
| 187 | }, |
| 188 | function (response) { |
| 189 | var resp = JSON.parse(response); |
| 190 | if (resp.type == "success") { |
| 191 | nitroSelf.applyOptimizationCosmetics(mode_name); |
| 192 | NitropackUI.triggerToast( |
| 193 | "info", |
| 194 | 'Optimization mode changed to <strong class="capitalized">' + mode_name + "</strong>." |
| 195 | ); |
| 196 | } else { |
| 197 | NitropackUI.triggerToast("error", resp.message); |
| 198 | } |
| 199 | } |
| 200 | ); |
| 201 | }; |
| 202 | applyOptimizationCosmetics(mode) { |
| 203 | const modes_btn = "#optimization-modes a"; |
| 204 | |
| 205 | $(modes_btn).removeClass("btn-primary active").addClass("btn-link"); |
| 206 | $(modes_btn + '[data-mode="' + mode + '"]') |
| 207 | .addClass("btn-primary active") |
| 208 | .removeClass("btn-link"); |
| 209 | $(".active-mode").text(mode); |
| 210 | |
| 211 | $(".card-optimization-mode .tab-content").addClass("hidden"); |
| 212 | $('.card-optimization-mode .tab-content[data-tab="' + mode + '-tab"].hidden').removeClass("hidden"); |
| 213 | } |
| 214 | optimizationModeClick() { |
| 215 | const nitroSelf = this, |
| 216 | modal_wrapper = $("#modal-optimization-mode"), |
| 217 | modal_footer = modal_wrapper.find(".popup-footer"), |
| 218 | action_btn = modal_footer.find(".modal-action"), |
| 219 | modes_btn = "#optimization-modes a"; |
| 220 | |
| 221 | $(modes_btn).click(function () { |
| 222 | var mode_name = $(this).data("mode"); |
| 223 | action_btn.data("mode", mode_name); |
| 224 | }); |
| 225 | action_btn.click(function () { |
| 226 | var mode_name = $(this).data("mode"), |
| 227 | mode_int = $(modes_btn + '[data-mode="' + mode_name + '"]').index() + 1; |
| 228 | nitroSelf.saveOptimizationMode(mode_int, mode_name); |
| 229 | }); |
| 230 | this.loadInitOptimizationMode(); |
| 231 | } |
| 232 | loadInitOptimizationMode() { |
| 233 | const mode = $("#optimization-modes a.active").data("mode"), |
| 234 | mode_int = $("#optimization-modes a.active").index() + 1; |
| 235 | this.initial_settings.optimizationLevel.int = mode_int; |
| 236 | this.initial_settings.optimizationLevel.name = mode; |
| 237 | } |
| 238 | autoPurgeCache() { |
| 239 | $("#auto-purge-status").on("click", function (e) { |
| 240 | $.post( |
| 241 | ajaxurl, |
| 242 | { |
| 243 | action: "nitropack_set_auto_cache_purge_ajax", |
| 244 | nonce: nitroNonce, |
| 245 | autoCachePurgeStatus: $(this).is(":checked") ? 1 : 0, |
| 246 | }, |
| 247 | function (response) { |
| 248 | var resp = JSON.parse(response); |
| 249 | NitropackUI.triggerToast(resp.type, resp.message); |
| 250 | } |
| 251 | ); |
| 252 | }); |
| 253 | } |
| 254 | |
| 255 | cacheWarmUp() { |
| 256 | const setting_id = "#warmup-status", |
| 257 | msg_wrapper = $("#loading-warmup-status"), |
| 258 | msg_icon = msg_wrapper.find(".icon"), |
| 259 | msg_text = msg_wrapper.find(".msg"), |
| 260 | nitroSelf = this; |
| 261 | |
| 262 | $(setting_id).change(function () { |
| 263 | if ($(this).is(":checked")) { |
| 264 | estimateWarmup(); |
| 265 | } else { |
| 266 | disableWarmup(); |
| 267 | } |
| 268 | }); |
| 269 | var disableWarmup = () => { |
| 270 | $.post( |
| 271 | ajaxurl, |
| 272 | { |
| 273 | action: "nitropack_disable_warmup", |
| 274 | nonce: np_settings.nitroNonce, |
| 275 | }, |
| 276 | function (response) { |
| 277 | var resp = JSON.parse(response); |
| 278 | if (resp.type == "success") { |
| 279 | nitroSelf.modified_settings.cacheWarmUp.enabled = 0; |
| 280 | NitropackUI.triggerToast("success", np_settings.success_msg); |
| 281 | } else { |
| 282 | NitropackUI.triggerToast("error", np_settings.error_msg); |
| 283 | } |
| 284 | } |
| 285 | ); |
| 286 | }; |
| 287 | |
| 288 | var estimateWarmup = (id, retry) => { |
| 289 | id = id || null; |
| 290 | retry = retry || 0; |
| 291 | msg_wrapper.removeClass("hidden"); |
| 292 | if (!id) { |
| 293 | msg_text.text(np_settings.est_cachewarmup_msg); |
| 294 | $.post( |
| 295 | ajaxurl, |
| 296 | { |
| 297 | action: "nitropack_estimate_warmup", |
| 298 | nonce: np_settings.nitroNonce, |
| 299 | }, |
| 300 | function (response) { |
| 301 | var resp = JSON.parse(response); |
| 302 | if (resp.type == "success") { |
| 303 | setTimeout( |
| 304 | (function (id) { |
| 305 | estimateWarmup(id); |
| 306 | })(resp.res), |
| 307 | 1000 |
| 308 | ); |
| 309 | } else { |
| 310 | $(setting_id).prop("checked", true); |
| 311 | msg_text.text(resp.message); |
| 312 | |
| 313 | msg_icon.attr("src", np_settings.nitro_plugin_url + "/view/images/info.svg"); |
| 314 | setTimeout(function () { |
| 315 | msg_wrapper.addClass("hidden"); |
| 316 | }, 3000); |
| 317 | } |
| 318 | } |
| 319 | ); |
| 320 | } else { |
| 321 | $.post( |
| 322 | ajaxurl, |
| 323 | { |
| 324 | action: "nitropack_estimate_warmup", |
| 325 | estId: id, |
| 326 | nonce: np_settings.nitroNonce, |
| 327 | }, |
| 328 | function (response) { |
| 329 | var resp = JSON.parse(response); |
| 330 | if (resp.type == "success") { |
| 331 | if (isNaN(resp.res) || resp.res == -1) { |
| 332 | // Still calculating |
| 333 | if (retry >= 10) { |
| 334 | $(setting_id).prop("checked", false); |
| 335 | msg_icon.attr("src", np_settings.nitro_plugin_url + "/view/images/info.svg"); |
| 336 | msg_text.text(resp.message); |
| 337 | |
| 338 | setTimeout(function () { |
| 339 | msg_wrapper.addClass("hidden"); |
| 340 | }, 3000); |
| 341 | } else { |
| 342 | setTimeout( |
| 343 | (function (id, retry) { |
| 344 | estimateWarmup(id, retry); |
| 345 | })(id, retry + 1), |
| 346 | 1000 |
| 347 | ); |
| 348 | } |
| 349 | } else { |
| 350 | if (resp.res == 0) { |
| 351 | $(setting_id).prop("checked", false); |
| 352 | msg_icon.attr("src", np_settings.nitro_plugin_url + "/view/images/info.svg"); |
| 353 | msg_text.text(resp.message); |
| 354 | setTimeout(function () { |
| 355 | msg_wrapper.addClass("hidden"); |
| 356 | }, 3000); |
| 357 | } else { |
| 358 | enableWarmup(); |
| 359 | } |
| 360 | } |
| 361 | } else { |
| 362 | msg_text.text(resp.message); |
| 363 | setTimeout(function () { |
| 364 | msg_wrapper.addClass("hidden"); |
| 365 | }, 3000); |
| 366 | } |
| 367 | } |
| 368 | ); |
| 369 | } |
| 370 | }; |
| 371 | var enableWarmup = () => { |
| 372 | $.post( |
| 373 | ajaxurl, |
| 374 | { |
| 375 | action: "nitropack_enable_warmup", |
| 376 | nonce: np_settings.nitroNonce, |
| 377 | }, |
| 378 | function (response) { |
| 379 | var resp = JSON.parse(response); |
| 380 | if (resp.type == "success") { |
| 381 | nitroSelf.initial_settings.cacheWarmUp.enabled = 1; |
| 382 | $(setting_id).prop("checked", true); |
| 383 | msg_wrapper.addClass("hidden"); |
| 384 | NitropackUI.triggerToast("success", np_settings.success_msg); |
| 385 | } else { |
| 386 | setTimeout(enableWarmup, 1000); |
| 387 | } |
| 388 | } |
| 389 | ); |
| 390 | }; |
| 391 | |
| 392 | var loadWarmupStatus = function () { |
| 393 | if ($("#warmup-status").is(":checked") == 1) { |
| 394 | nitroSelf.initial_settings.cacheWarmUp.enabled = 1; |
| 395 | } else { |
| 396 | nitroSelf.initial_settings.cacheWarmUp.enabled = 0; |
| 397 | } |
| 398 | }; |
| 399 | |
| 400 | loadWarmupStatus(); |
| 401 | } |
| 402 | skipCacheWarmupAjax() { |
| 403 | $.post( |
| 404 | ajaxurl, |
| 405 | { |
| 406 | action: "nitropack_skip_cache_warmup", |
| 407 | nonce: np_settings.nitroNonce, |
| 408 | }, |
| 409 | function (response) { |
| 410 | var resp = JSON.parse(response); |
| 411 | if (resp.type == "success") { |
| 412 | $(".cache-warmup.card").remove(); |
| 413 | } else { |
| 414 | NitropackUI.triggerToast("error", np_settings.error_msg); |
| 415 | } |
| 416 | } |
| 417 | ); |
| 418 | } |
| 419 | enableCacheWarmup() { |
| 420 | const nitroSelf = this; |
| 421 | $("#enable-cache-warmup").on("click", function () { |
| 422 | //enable CW |
| 423 | $("#warmup-status").prop("checked", true).trigger("change"); |
| 424 | //dismiss notice forever |
| 425 | nitroSelf.skipCacheWarmupAjax(); |
| 426 | }); |
| 427 | } |
| 428 | |
| 429 | skipCacheWarmup() { |
| 430 | const nitroSelf = this; |
| 431 | $("#skip-cache-warmup").on("click", function () { |
| 432 | nitroSelf.skipCacheWarmupAjax(); |
| 433 | }); |
| 434 | } |
| 435 | ajaxShortcodes() { |
| 436 | //main setting |
| 437 | const setting_id = "#ajax-shortcodes", |
| 438 | nitroSelf = this; |
| 439 | if ($(setting_id).is(":checked")) { |
| 440 | nitroSelf.initial_settings.ajaxShortcodes.enabled = 1; |
| 441 | } |
| 442 | |
| 443 | $(setting_id).change(function () { |
| 444 | if ($(this).is(":checked")) { |
| 445 | ajaxShortcodeRequest(null, 1); |
| 446 | } else { |
| 447 | ajaxShortcodeRequest(null, 0); |
| 448 | } |
| 449 | }); |
| 450 | //template for selected shortcodes tags |
| 451 | let select2 = $("#ajax-shortcodes-dropdown").select2({ |
| 452 | selectOnClose: false, |
| 453 | tags: true, |
| 454 | multiple: true, |
| 455 | width: "100%", |
| 456 | placeholder: "Enter a shortcode", |
| 457 | templateSelection: shortcodeTagTemplate, |
| 458 | }), |
| 459 | shortcodes_val = select2.val(); |
| 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.status == "success") { |
| 676 | if (resp.hasCompression) { |
| 677 | // compression already enabled |
| 678 | compression_setting.attr("checked", false); |
| 679 | |
| 680 | msg_box.text(np_settings.compression_already_enabled); |
| 681 | } else { |
| 682 | compression_setting.attr("checked", true); |
| 683 | msg_box.text(np_settings.compression_not_detected); |
| 684 | } |
| 685 | NitropackUI.triggerToast(resp.type, resp.message); |
| 686 | } else { |
| 687 | msg_box.text(np_settings.compression_not_determined); |
| 688 | } |
| 689 | setTimeout(function () { |
| 690 | msg_container.addClass("hidden"); |
| 691 | compression_btn.removeClass("hidden"); |
| 692 | }, 5000); |
| 693 | } |
| 694 | ); |
| 695 | } |
| 696 | setHTMLCompression() { |
| 697 | const nitroSelf = this; |
| 698 | let enabled = $("#compression-status").is(":checked") ? 1 : 0; |
| 699 | nitroSelf.initial_settings.htmlCompression.enabled = enabled; |
| 700 | //on load check status |
| 701 | $(window).on("load", function () { |
| 702 | if (enabled != 1) { |
| 703 | nitroSelf.autoDetectCompression(); |
| 704 | } |
| 705 | }); |
| 706 | $(document).on("click", "#compression-test-btn", (e) => { |
| 707 | e.preventDefault(); |
| 708 | nitroSelf.autoDetectCompression(); |
| 709 | }); |
| 710 | |
| 711 | //toggle on/off setting |
| 712 | $("#compression-status").on("click", function (e) { |
| 713 | $.post( |
| 714 | ajaxurl, |
| 715 | { |
| 716 | action: "nitropack_set_compression_ajax", |
| 717 | nonce: nitroNonce, |
| 718 | data: { |
| 719 | compressionStatus: $(this).is(":checked") ? 1 : 0, |
| 720 | }, |
| 721 | }, |
| 722 | function (response) { |
| 723 | var resp = JSON.parse(response); |
| 724 | NitropackUI.triggerToast(resp.type, resp.message); |
| 725 | } |
| 726 | ); |
| 727 | }); |
| 728 | } |
| 729 | beaverBuilder() { |
| 730 | let enabled = $("#bb-purge-status").is(":checked") ? 1 : 0; |
| 731 | this.initial_settings.bbCachePurgeSync.enabled = enabled; |
| 732 | $("#bb-purge-status").on("click", function (e) { |
| 733 | $.post( |
| 734 | ajaxurl, |
| 735 | { |
| 736 | action: "nitropack_set_bb_cache_purge_sync_ajax", |
| 737 | nonce: nitroNonce, |
| 738 | nonce: nitroNonce, |
| 739 | bbCachePurgeSyncStatus: $(this).is(":checked") ? 1 : 0, |
| 740 | }, |
| 741 | function (response) { |
| 742 | var resp = JSON.parse(response); |
| 743 | NitropackUI.triggerToast(resp.type, resp.message); |
| 744 | } |
| 745 | ); |
| 746 | }); |
| 747 | } |
| 748 | editorPurgeCache() { |
| 749 | let enabled = $("#can-editor-clear-cache").is(":checked") ? 1 : 0; |
| 750 | this.initial_settings.canEditorClearCache.enabled = enabled; |
| 751 | $("#can-editor-clear-cache").on("click", function (e) { |
| 752 | $.post( |
| 753 | ajaxurl, |
| 754 | { |
| 755 | action: "nitropack_set_can_editor_clear_cache", |
| 756 | nonce: nitroNonce, |
| 757 | data: { |
| 758 | canEditorClearCache: $(this).is(":checked") ? 1 : 0, |
| 759 | }, |
| 760 | }, |
| 761 | function (response) { |
| 762 | var resp = JSON.parse(response); |
| 763 | NitropackUI.triggerToast(resp.type, resp.message); |
| 764 | } |
| 765 | ); |
| 766 | }); |
| 767 | } |
| 768 | cartCache() { |
| 769 | let enabled = $("#cart-cache-status").is(":checked") ? 1 : 0; |
| 770 | this.initial_settings.cartCache.enabled = enabled; |
| 771 | $("#cart-cache-status").on("click", function (e) { |
| 772 | $.post( |
| 773 | ajaxurl, |
| 774 | { |
| 775 | action: "nitropack_set_cart_cache_ajax", |
| 776 | nonce: nitroNonce, |
| 777 | cartCacheStatus: $(this).is(":checked") ? 1 : 0, |
| 778 | }, |
| 779 | function (response) { |
| 780 | var resp = JSON.parse(response); |
| 781 | NitropackUI.triggerToast(resp.type, resp.message); |
| 782 | } |
| 783 | ); |
| 784 | }); |
| 785 | } |
| 786 | stockRefresh() { |
| 787 | let enabled = $("#woo-stock-reduce-status").is(":checked") ? 1 : 0; |
| 788 | this.initial_settings.stockReduce.enabled = enabled; |
| 789 | |
| 790 | $("#woo-stock-reduce-status").on("click", function (e) { |
| 791 | $.post( |
| 792 | ajaxurl, |
| 793 | { |
| 794 | action: "nitropack_set_stock_reduce_status", |
| 795 | nonce: nitroNonce, |
| 796 | data: { |
| 797 | stockReduceStatus: $(this).is(":checked") ? 1 : 0, |
| 798 | }, |
| 799 | }, |
| 800 | function (response) { |
| 801 | var resp = JSON.parse(response); |
| 802 | NitropackUI.triggerToast(resp.type, resp.message); |
| 803 | } |
| 804 | ); |
| 805 | }); |
| 806 | } |
| 807 | loggerToggle() { |
| 808 | const radio = $("#minimum-log-level-status"), |
| 809 | widget = $("#minimum-log-level-widget"), |
| 810 | fancy_radios = widget.find(".fancy-radio"), |
| 811 | fancy_radios_container = widget.find(".fancy-radio-container"); |
| 812 | let minimum_log_level; |
| 813 | |
| 814 | radio.on("change", function () { |
| 815 | const self = $(this); |
| 816 | if (self.is(":checked")) { |
| 817 | minimum_log_level = 3; |
| 818 | } else { |
| 819 | minimum_log_level = null; |
| 820 | } |
| 821 | $.post( |
| 822 | ajaxurl, |
| 823 | { |
| 824 | action: "nitropack_set_log_level_ajax", |
| 825 | minimum_log_level: minimum_log_level, |
| 826 | nonce: np_settings.nitroNonce, |
| 827 | }, |
| 828 | function (response) { |
| 829 | var resp = JSON.parse(response); |
| 830 | if (resp.type == "success") { |
| 831 | if (self.is(":checked")) { |
| 832 | $(".logging").removeClass("hidden"); |
| 833 | fancy_radios_container.removeClass("selected"); |
| 834 | fancy_radios.removeClass("selected"); |
| 835 | widget.find('.fancy-radio-container[data-value="' + minimum_log_level + '"').addClass("selected"); |
| 836 | widget |
| 837 | .find('.fancy-radio-container[data-value="' + minimum_log_level + '"') |
| 838 | .find(".fancy-radio") |
| 839 | .addClass("selected"); |
| 840 | } else { |
| 841 | fancy_radios.removeClass("selected"); |
| 842 | $(".logging").addClass("hidden"); |
| 843 | } |
| 844 | |
| 845 | NitropackUI.triggerToast("success", np_settings.success_msg); |
| 846 | } else { |
| 847 | NitropackUI.triggerToast("error", np_settings.success_msg); |
| 848 | $(this).prop("checked", false); |
| 849 | } |
| 850 | } |
| 851 | ); |
| 852 | }); |
| 853 | } |
| 854 | /* Set logger level */ |
| 855 | setLoggerLevel() { |
| 856 | const widget = $("#minimum-log-level-widget"), |
| 857 | fancy_radios_container = widget.find(".fancy-radio-container"), |
| 858 | fancy_radios = widget.find(".fancy-radio"); |
| 859 | let initial_minimum_log_level = widget.find(".fancy-radio-container.selected").data("value"); |
| 860 | |
| 861 | fancy_radios_container.click(function () { |
| 862 | let fancy_radio_container = $(this), |
| 863 | fancy_radio = $(this).find(".fancy-radio"), |
| 864 | minimum_log_level = fancy_radio_container.data("value"); |
| 865 | if (minimum_log_level === initial_minimum_log_level) return; |
| 866 | |
| 867 | $.post( |
| 868 | ajaxurl, |
| 869 | { |
| 870 | action: "nitropack_set_log_level_ajax", |
| 871 | minimum_log_level: minimum_log_level, |
| 872 | nonce: np_settings.nitroNonce, |
| 873 | }, |
| 874 | function (response) { |
| 875 | var resp = JSON.parse(response); |
| 876 | if (resp.type == "success") { |
| 877 | //container |
| 878 | fancy_radios_container.removeClass("selected"); |
| 879 | fancy_radio_container.addClass("selected"); |
| 880 | //custom radios |
| 881 | fancy_radios.removeClass("selected"); |
| 882 | fancy_radio.addClass("selected"); |
| 883 | initial_minimum_log_level = minimum_log_level; |
| 884 | NitropackUI.triggerToast("success", np_settings.success_msg); |
| 885 | } else { |
| 886 | NitropackUI.triggerToast("error", np_settings.success_msg); |
| 887 | $(this).prop("checked", false); |
| 888 | } |
| 889 | } |
| 890 | ); |
| 891 | }); |
| 892 | } |
| 893 | /* Zips all logs and downloads them */ |
| 894 | archiveLogs() { |
| 895 | $(".archive-logs").click(function (e) { |
| 896 | e.preventDefault(); |
| 897 | $.post( |
| 898 | ajaxurl, |
| 899 | { |
| 900 | action: "nitropack_archive_logs_ajax", |
| 901 | nonce: np_settings.nitroNonce, |
| 902 | }, |
| 903 | function (response) { |
| 904 | var resp = JSON.parse(response); |
| 905 | if (resp.type == "success") { |
| 906 | window.location.href = resp.url; |
| 907 | NitropackUI.triggerToast("success", np_settings.success_msg); |
| 908 | } else { |
| 909 | NitropackUI.triggerToast("error", np_settings.success_msg); |
| 910 | } |
| 911 | } |
| 912 | ); |
| 913 | }); |
| 914 | } |
| 915 | restoreConnection() { |
| 916 | const loading_icon = |
| 917 | '<img src="' + np_settings.nitro_plugin_url + '/view/images/loading.svg" width="14" class="icon loading"/>', |
| 918 | success_icon = |
| 919 | '<img src="' + np_settings.nitro_plugin_url + '/view/images/check.svg" width="16" class="icon success"/>'; |
| 920 | |
| 921 | $("#nitro-restore-connection-btn").on("click", function () { |
| 922 | $.ajax({ |
| 923 | url: ajaxurl, |
| 924 | type: "GET", |
| 925 | data: { |
| 926 | action: "nitropack_reconfigure_webhooks", |
| 927 | nonce: nitroNonce, |
| 928 | }, |
| 929 | dataType: "json", |
| 930 | beforeSend: function () { |
| 931 | $("#nitro-restore-connection-btn").attr("disabled", true).html(loading_icon); |
| 932 | }, |
| 933 | success: function (data) { |
| 934 | if (!data.status || data.status != "success") { |
| 935 | if (data.message) { |
| 936 | alert(data.message); |
| 937 | } else { |
| 938 | alert( |
| 939 | "We were unable to restore the connection. Please contact our support team to get this resolved." |
| 940 | ); |
| 941 | } |
| 942 | } else { |
| 943 | $("#nitro-restore-connection-btn").attr("disabled", true).html(success_icon); |
| 944 | NitropackUI.triggerToast("success", data.message); |
| 945 | } |
| 946 | }, |
| 947 | complete: function () { |
| 948 | location.reload(); |
| 949 | }, |
| 950 | }); |
| 951 | }); |
| 952 | } |
| 953 | /* Was used in dashboard.php and oneclick.php */ |
| 954 | loadDismissibleNotices() { |
| 955 | var $ = jQuery; |
| 956 | |
| 957 | $(".nitro-notification.is-dismissible").each(function () { |
| 958 | var b = $(this), |
| 959 | c = $('<button type="button" class="notice-dismiss"><span class="screen-reader-text"></span></button>'); |
| 960 | c.on("click.wp-dismiss-notice", function ($) { |
| 961 | $.preventDefault(), |
| 962 | b.fadeTo(100, 0, function () { |
| 963 | b.slideUp(100, function () { |
| 964 | b.remove(); |
| 965 | }); |
| 966 | }); |
| 967 | }), |
| 968 | b.append(c); |
| 969 | }); |
| 970 | } |
| 971 | |
| 972 | windowNotification() { |
| 973 | const nitroSelf = this; |
| 974 | window.Notification = ((_) => { |
| 975 | var timeout; |
| 976 | var display = (msg, type) => { |
| 977 | clearTimeout(timeout); |
| 978 | $(".nitro-notification").remove(); |
| 979 | //tbd |
| 980 | $('[name="form"]').prepend( |
| 981 | '<div class="nitro-notification notification-' + type + '" is-dismissible"><p>' + msg + "</p></div>" |
| 982 | ); |
| 983 | |
| 984 | timeout = setTimeout((_) => { |
| 985 | $(".nitro-notification").remove(); |
| 986 | }, 10000); |
| 987 | |
| 988 | nitroSelf.loadDismissibleNotices(); |
| 989 | }; |
| 990 | |
| 991 | return { |
| 992 | success: (msg) => { |
| 993 | display(msg, "success"); |
| 994 | }, |
| 995 | error: (msg) => { |
| 996 | display(msg, "error"); |
| 997 | }, |
| 998 | info: (msg) => { |
| 999 | display(msg, "info"); |
| 1000 | }, |
| 1001 | warning: (msg) => { |
| 1002 | display(msg, "warning"); |
| 1003 | }, |
| 1004 | }; |
| 1005 | })(); |
| 1006 | } |
| 1007 | clearResidualCache() { |
| 1008 | let isClearing = false; |
| 1009 | $(document).on("click", ".btn[nitropack-rc-data]", function (e) { |
| 1010 | e.preventDefault(); |
| 1011 | if (isClearing) return; |
| 1012 | let currentButton = $(this); |
| 1013 | $.ajax({ |
| 1014 | url: ajaxurl, |
| 1015 | type: "POST", |
| 1016 | dataType: "text", |
| 1017 | data: { |
| 1018 | action: "nitropack_clear_residual_cache", |
| 1019 | gde: currentButton.attr("nitropack-rc-data"), |
| 1020 | nonce: nitroNonce, |
| 1021 | }, |
| 1022 | beforeSend: function () { |
| 1023 | isClearing = true; |
| 1024 | }, |
| 1025 | success: function (resp) { |
| 1026 | NitropackUI.triggerToast("success", np_settings.success_msg); |
| 1027 | }, |
| 1028 | error: function (resp) { |
| 1029 | NitropackUI.triggerToast("error", np_settings.success_msg); |
| 1030 | }, |
| 1031 | complete: function () { |
| 1032 | isClearing = false; |
| 1033 | setTimeout(function () { |
| 1034 | location.reload(); |
| 1035 | }, 3000); |
| 1036 | }, |
| 1037 | }); |
| 1038 | }); |
| 1039 | } |
| 1040 | } |
| 1041 | const NitroPackSettings = new nitropackSettings(); |
| 1042 | window.NitroPackSettings = NitroPackSettings; |
| 1043 | }); |
| 1044 |