admin_bar_menu.js
4 months ago
elementor_cache_integration.js
5 months ago
gravity_forms.js
2 months ago
math_captcha.js
1 year ago
nitropackUI.js
2 months ago
np_notices.js
3 months ago
np_safemode.js
1 year ago
np_select2.js
2 months ago
np_select2.min.js
2 months ago
np_settings.js
2 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
2 months ago
np_settings.js
910 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 | const $shortcodesDropdown = $("#ajax-shortcodes-dropdown"); |
| 440 | $shortcodesDropdown.npSelect(); |
| 441 | let shortcodes_val = $shortcodesDropdown.val(); |
| 442 | |
| 443 | /* Show the container when npSelect is initialized to avoid flickering. */ |
| 444 | if (nitroSelf.initial_settings.ajaxShortcodes.enabled) $(".ajax-shortcodes").removeClass("hidden"); |
| 445 | |
| 446 | if (shortcodes_val && shortcodes_val.length > 0) { |
| 447 | nitroSelf.initial_settings.ajaxShortcodes.shortcodes = $shortcodesDropdown.val(); |
| 448 | } else { |
| 449 | nitroSelf.initial_settings.ajaxShortcodes.shortcodes = []; |
| 450 | } |
| 451 | |
| 452 | $shortcodesDropdown.on("change", (event) => { |
| 453 | const selectedValues = $(event.target).val() || []; |
| 454 | this.modified_settings.ajaxShortcodes.shortcodes = selectedValues; |
| 455 | }); |
| 456 | |
| 457 | $(setting_id).change(function () { |
| 458 | let shortcodes = $shortcodesDropdown.val(); |
| 459 | if ($(this).is(":checked")) { |
| 460 | ajaxShortcodeRequest(shortcodes, 1); |
| 461 | } else { |
| 462 | ajaxShortcodeRequest(shortcodes, 0); |
| 463 | } |
| 464 | }); |
| 465 | |
| 466 | //btn save click |
| 467 | $(".ajax-shortcodes #save-shortcodes").click(function () { |
| 468 | let shortcodes = $("#ajax-shortcodes-dropdown").val(); |
| 469 | ajaxShortcodeRequest(shortcodes, null); |
| 470 | }); |
| 471 | |
| 472 | /* shortcodes - array of shortcodes or null |
| 473 | enabled - 1 or 0 |
| 474 | */ |
| 475 | const ajaxShortcodeRequest = function (shortcodes, enabled) { |
| 476 | let data_obj = { |
| 477 | action: "nitropack_set_ajax_shortcodes_ajax", |
| 478 | nonce: np_settings.nitroNonce, |
| 479 | shortcodes: Array.isArray(shortcodes) && shortcodes.length ? shortcodes : [JSON.stringify([])], // Ensure it's always an array |
| 480 | }; |
| 481 | |
| 482 | if (enabled !== null) data_obj.enabled = enabled; |
| 483 | |
| 484 | const response = $.ajax({ |
| 485 | url: ajaxurl, |
| 486 | type: "POST", |
| 487 | data: data_obj, |
| 488 | dataType: "json", |
| 489 | success: function (resp) { |
| 490 | if (resp.type == "success") { |
| 491 | if (enabled == 1) { |
| 492 | $(".ajax-shortcodes").removeClass("hidden"); |
| 493 | nitroSelf.modified_settings.ajaxShortcodes.enabled = 1; |
| 494 | } |
| 495 | if (enabled == 0) { |
| 496 | $(".ajax-shortcodes").addClass("hidden"); |
| 497 | nitroSelf.modified_settings.ajaxShortcodes.enabled = 0; |
| 498 | } |
| 499 | // Ensure we're setting an array in settings |
| 500 | if (Array.isArray(shortcodes) && shortcodes.length) { |
| 501 | nitroSelf.initial_settings.ajaxShortcodes.shortcodes = shortcodes; |
| 502 | } else { |
| 503 | nitroSelf.initial_settings.ajaxShortcodes.shortcodes = []; |
| 504 | } |
| 505 | NitropackUI.triggerToast("success", np_settings.success_msg); |
| 506 | } else { |
| 507 | NitropackUI.triggerToast("error", np_settings.error_msg); |
| 508 | } |
| 509 | }, |
| 510 | }); |
| 511 | return response; |
| 512 | }; |
| 513 | return { |
| 514 | ajaxShortcodeRequest: ajaxShortcodeRequest, |
| 515 | }; |
| 516 | } |
| 517 | // Function to omit 'enabled' property |
| 518 | omitEnabledProperty(obj) { |
| 519 | return Object.keys(obj).reduce((acc, key) => { |
| 520 | if (typeof obj[key] === "object" && obj[key] !== null) { |
| 521 | acc[key] = this.omitEnabledProperty(obj[key]); |
| 522 | } else if (key !== "enabled") { |
| 523 | acc[key] = obj[key]; |
| 524 | } |
| 525 | return acc; |
| 526 | }, {}); |
| 527 | } |
| 528 | |
| 529 | // Function to check for unsaved changes, ignoring 'enabled' property |
| 530 | hasUnsavedChanges() { |
| 531 | const initialWithoutEnabled = this.omitEnabledProperty(this.initial_settings); |
| 532 | const modifiedWithoutEnabled = this.omitEnabledProperty(this.modified_settings); |
| 533 | return JSON.stringify(initialWithoutEnabled) !== JSON.stringify(modifiedWithoutEnabled); |
| 534 | } |
| 535 | |
| 536 | // Function to handle page leave |
| 537 | onPageLeave() { |
| 538 | const nitroSelf = this; |
| 539 | window.onbeforeunload = function (event) { |
| 540 | if ( |
| 541 | nitroSelf.hasUnsavedChanges() && |
| 542 | !nitroSelf.unsavedChangesModal && |
| 543 | nitroSelf.modified_settings.ajaxShortcodes.enabled === 1 |
| 544 | ) { |
| 545 | event.preventDefault(); // show prompt |
| 546 | } |
| 547 | }; |
| 548 | //a links - display modal |
| 549 | $(document).on("click", 'a[href]:not([target="_blank"])', function (event) { |
| 550 | if (nitroSelf.hasUnsavedChanges() && nitroSelf.modified_settings.ajaxShortcodes.enabled === 1) { |
| 551 | event.preventDefault(); |
| 552 | const leaveUrl = this.href; |
| 553 | nitroSelf.showUnsavedChangesModal(() => { |
| 554 | ``; |
| 555 | window.location.href = leaveUrl; |
| 556 | }); |
| 557 | } |
| 558 | }); |
| 559 | } |
| 560 | // Show unsaved changes modal |
| 561 | showUnsavedChangesModal(onConfirm) { |
| 562 | const nitroSelf = this; |
| 563 | //vanilla js |
| 564 | const modalID = "modal-unsavedChanges", |
| 565 | $modal_target = document.getElementById(modalID), |
| 566 | modal_options = { |
| 567 | backdrop: "static", |
| 568 | backdropClasses: "nitro-backdrop", |
| 569 | closable: true, |
| 570 | onHide: () => { |
| 571 | this.unsavedChangesModal = false; |
| 572 | }, |
| 573 | onShow: () => { |
| 574 | this.unsavedChangesModal = true; |
| 575 | }, |
| 576 | }, |
| 577 | instanceOptions = { |
| 578 | id: modalID, |
| 579 | }, |
| 580 | modal = new Modal($modal_target, modal_options, instanceOptions); |
| 581 | //jquery |
| 582 | const modal_wrapper = $("#" + modalID), |
| 583 | x_button = modal_wrapper.find(".close-modal"), |
| 584 | modal_footer = modal_wrapper.find(".popup-footer"), |
| 585 | secondary_btn = modal_footer.find(".popup-close"), |
| 586 | action_btn = modal_footer.find(".btn-primary"); |
| 587 | modal.show(); |
| 588 | |
| 589 | //no action |
| 590 | $(x_button).one("click", function () { |
| 591 | modal.hide(); |
| 592 | }); |
| 593 | //redirect without saving |
| 594 | $(secondary_btn).one("click", function () { |
| 595 | onConfirm(); |
| 596 | modal.hide(); |
| 597 | }); |
| 598 | //save and redirect |
| 599 | $(action_btn).one("click", function () { |
| 600 | const ajaxRequest = nitroSelf.ajaxShortcodes.ajaxShortcodeRequest( |
| 601 | nitroSelf.modified_settings.ajaxShortcodes.shortcodes, |
| 602 | null, |
| 603 | ); |
| 604 | ajaxRequest.done(function (response) { |
| 605 | if (response.type === "success") onConfirm(); |
| 606 | }); |
| 607 | ajaxRequest.fail(function () { |
| 608 | console.error("AJAX request failed."); |
| 609 | NitropackUI.triggerToast("error", "Error saving shortcodes."); |
| 610 | onConfirm(); |
| 611 | }); |
| 612 | modal.hide(); |
| 613 | }); |
| 614 | } |
| 615 | removeElement(array, value) { |
| 616 | const index = array.indexOf(value); |
| 617 | if (index !== -1) { |
| 618 | array.splice(index, 1); |
| 619 | } |
| 620 | } |
| 621 | autoDetectCompression() { |
| 622 | let msg_container = $("#compression-widget .msg-container"), |
| 623 | msg_box = msg_container.find(".msg"), |
| 624 | compression_setting = $("#compression-status"), |
| 625 | compression_btn = $("#compression-test-btn"); |
| 626 | //add spinner here |
| 627 | msg_box.html( |
| 628 | '<img src="' + |
| 629 | np_settings.nitro_plugin_url + |
| 630 | '/view/images/loading.svg" alt="loading" class="icon"> ' + |
| 631 | np_settings.testing_compression, |
| 632 | ); |
| 633 | compression_btn.addClass("hidden"); |
| 634 | msg_container.removeClass("hidden"); |
| 635 | $.post( |
| 636 | ajaxurl, |
| 637 | { |
| 638 | action: "nitropack_test_compression_ajax", |
| 639 | nonce: nitroNonce, |
| 640 | }, |
| 641 | function (response) { |
| 642 | var resp = JSON.parse(response); |
| 643 | |
| 644 | if (resp.type == "success") { |
| 645 | if (resp.hasCompression) { |
| 646 | // compression already enabled |
| 647 | compression_setting.attr("checked", false); |
| 648 | compression_setting.attr("disabled", true); |
| 649 | msg_box.text(np_settings.compression_already_enabled); |
| 650 | } else { |
| 651 | compression_setting.attr("checked", true); |
| 652 | compression_setting.attr("disabled", false); |
| 653 | msg_box.text(np_settings.compression_not_detected); |
| 654 | NitropackUI.triggerToast(resp.type, resp.message); |
| 655 | } |
| 656 | } else { |
| 657 | if (resp.status_code) { |
| 658 | let text = "[Error] HTTP Status Code: " + resp.status_code; |
| 659 | msg_box.text(text); |
| 660 | } else { |
| 661 | msg_box.text(np_settings.compression_not_determined); |
| 662 | } |
| 663 | } |
| 664 | setTimeout(function () { |
| 665 | msg_container.addClass("hidden"); |
| 666 | compression_btn.removeClass("hidden"); |
| 667 | }, 5000); |
| 668 | }, |
| 669 | ); |
| 670 | } |
| 671 | setHTMLCompression() { |
| 672 | const nitroSelf = this; |
| 673 | let enabled = $("#compression-status").is(":checked") ? 1 : 0; |
| 674 | nitroSelf.initial_settings.htmlCompression.enabled = enabled; |
| 675 | //on load check status |
| 676 | $(window).on("load", function () { |
| 677 | nitroSelf.autoDetectCompression(); |
| 678 | }); |
| 679 | $(document).on("click", "#compression-test-btn", (e) => { |
| 680 | e.preventDefault(); |
| 681 | nitroSelf.autoDetectCompression(); |
| 682 | }); |
| 683 | |
| 684 | //toggle on/off setting |
| 685 | $("#compression-status").on("click", function (e) { |
| 686 | $.post( |
| 687 | ajaxurl, |
| 688 | { |
| 689 | action: "nitropack_set_compression_ajax", |
| 690 | nonce: nitroNonce, |
| 691 | data: { |
| 692 | compressionStatus: $(this).is(":checked") ? 1 : 0, |
| 693 | }, |
| 694 | }, |
| 695 | function (response) { |
| 696 | var resp = JSON.parse(response); |
| 697 | NitropackUI.triggerToast(resp.type, resp.message); |
| 698 | }, |
| 699 | ); |
| 700 | }); |
| 701 | } |
| 702 | beaverBuilder() { |
| 703 | let enabled = $("#bb-purge-status").is(":checked") ? 1 : 0; |
| 704 | this.initial_settings.bbCachePurgeSync.enabled = enabled; |
| 705 | $("#bb-purge-status").on("click", function (e) { |
| 706 | $.post( |
| 707 | ajaxurl, |
| 708 | { |
| 709 | action: "nitropack_set_bb_cache_purge_sync_ajax", |
| 710 | nonce: nitroNonce, |
| 711 | nonce: nitroNonce, |
| 712 | bbCachePurgeSyncStatus: $(this).is(":checked") ? 1 : 0, |
| 713 | }, |
| 714 | function (response) { |
| 715 | var resp = JSON.parse(response); |
| 716 | NitropackUI.triggerToast(resp.type, resp.message); |
| 717 | }, |
| 718 | ); |
| 719 | }); |
| 720 | } |
| 721 | editorPurgeCache() { |
| 722 | let enabled = $("#can-editor-clear-cache").is(":checked") ? 1 : 0; |
| 723 | this.initial_settings.canEditorClearCache.enabled = enabled; |
| 724 | $("#can-editor-clear-cache").on("click", function (e) { |
| 725 | $.post( |
| 726 | ajaxurl, |
| 727 | { |
| 728 | action: "nitropack_set_can_editor_clear_cache", |
| 729 | nonce: nitroNonce, |
| 730 | data: { |
| 731 | canEditorClearCache: $(this).is(":checked") ? 1 : 0, |
| 732 | }, |
| 733 | }, |
| 734 | function (response) { |
| 735 | var resp = JSON.parse(response); |
| 736 | NitropackUI.triggerToast(resp.type, resp.message); |
| 737 | }, |
| 738 | ); |
| 739 | }); |
| 740 | } |
| 741 | cartCache() { |
| 742 | let enabled = $("#cart-cache-status").is(":checked") ? 1 : 0; |
| 743 | this.initial_settings.cartCache.enabled = enabled; |
| 744 | $("#cart-cache-status").on("click", function (e) { |
| 745 | $.post( |
| 746 | ajaxurl, |
| 747 | { |
| 748 | action: "nitropack_set_cart_cache_ajax", |
| 749 | nonce: nitroNonce, |
| 750 | cartCacheStatus: $(this).is(":checked") ? 1 : 0, |
| 751 | }, |
| 752 | function (response) { |
| 753 | var resp = JSON.parse(response); |
| 754 | NitropackUI.triggerToast(resp.type, resp.message); |
| 755 | }, |
| 756 | ); |
| 757 | }); |
| 758 | } |
| 759 | stockRefresh() { |
| 760 | let enabled = $("#woo-stock-reduce-status").is(":checked") ? 1 : 0; |
| 761 | this.initial_settings.stockReduce.enabled = enabled; |
| 762 | |
| 763 | $("#woo-stock-reduce-status").on("click", function (e) { |
| 764 | $.post( |
| 765 | ajaxurl, |
| 766 | { |
| 767 | action: "nitropack_set_stock_reduce_status", |
| 768 | nonce: nitroNonce, |
| 769 | data: { |
| 770 | stockReduceStatus: $(this).is(":checked") ? 1 : 0, |
| 771 | }, |
| 772 | }, |
| 773 | function (response) { |
| 774 | var resp = JSON.parse(response); |
| 775 | NitropackUI.triggerToast(resp.type, resp.message); |
| 776 | }, |
| 777 | ); |
| 778 | }); |
| 779 | } |
| 780 | |
| 781 | restoreConnection() { |
| 782 | const loading_icon = |
| 783 | '<img src="' + np_settings.nitro_plugin_url + '/view/images/loading.svg" width="14" class="icon loading"/>', |
| 784 | success_icon = |
| 785 | '<img src="' + np_settings.nitro_plugin_url + '/view/images/check.svg" width="16" class="icon success"/>'; |
| 786 | |
| 787 | $("#nitro-restore-connection-btn").on("click", function () { |
| 788 | $.ajax({ |
| 789 | url: ajaxurl, |
| 790 | type: "GET", |
| 791 | data: { |
| 792 | action: "nitropack_reconfigure_webhooks", |
| 793 | nonce: nitroNonce, |
| 794 | }, |
| 795 | dataType: "json", |
| 796 | beforeSend: function () { |
| 797 | $("#nitro-restore-connection-btn").attr("disabled", true).html(loading_icon); |
| 798 | }, |
| 799 | success: function (data) { |
| 800 | if (!data.status || data.status != "success") { |
| 801 | if (data.message) { |
| 802 | alert(data.message); |
| 803 | } else { |
| 804 | alert( |
| 805 | "We were unable to restore the connection. Please contact our support team to get this resolved.", |
| 806 | ); |
| 807 | } |
| 808 | } else { |
| 809 | $("#nitro-restore-connection-btn").attr("disabled", true).html(success_icon); |
| 810 | NitropackUI.triggerToast("success", data.message); |
| 811 | } |
| 812 | }, |
| 813 | complete: function () { |
| 814 | location.reload(); |
| 815 | }, |
| 816 | }); |
| 817 | }); |
| 818 | } |
| 819 | /* Was used in dashboard.php and oneclick.php */ |
| 820 | loadDismissibleNotices() { |
| 821 | var $ = jQuery; |
| 822 | |
| 823 | $(".nitro-notification.is-dismissible").each(function () { |
| 824 | var b = $(this), |
| 825 | c = $('<button type="button" class="notice-dismiss"><span class="screen-reader-text"></span></button>'); |
| 826 | (c.on("click.wp-dismiss-notice", function ($) { |
| 827 | ($.preventDefault(), |
| 828 | b.fadeTo(100, 0, function () { |
| 829 | b.slideUp(100, function () { |
| 830 | b.remove(); |
| 831 | }); |
| 832 | })); |
| 833 | }), |
| 834 | b.append(c)); |
| 835 | }); |
| 836 | } |
| 837 | |
| 838 | windowNotification() { |
| 839 | const nitroSelf = this; |
| 840 | window.Notification = ((_) => { |
| 841 | var timeout; |
| 842 | var display = (msg, type) => { |
| 843 | clearTimeout(timeout); |
| 844 | $(".nitro-notification").remove(); |
| 845 | //tbd |
| 846 | $('[name="form"]').prepend( |
| 847 | '<div class="nitro-notification notification-' + type + '" is-dismissible"><p>' + msg + "</p></div>", |
| 848 | ); |
| 849 | |
| 850 | timeout = setTimeout((_) => { |
| 851 | $(".nitro-notification").remove(); |
| 852 | }, 10000); |
| 853 | |
| 854 | nitroSelf.loadDismissibleNotices(); |
| 855 | }; |
| 856 | |
| 857 | return { |
| 858 | success: (msg) => { |
| 859 | display(msg, "success"); |
| 860 | }, |
| 861 | error: (msg) => { |
| 862 | display(msg, "error"); |
| 863 | }, |
| 864 | info: (msg) => { |
| 865 | display(msg, "info"); |
| 866 | }, |
| 867 | warning: (msg) => { |
| 868 | display(msg, "warning"); |
| 869 | }, |
| 870 | }; |
| 871 | })(); |
| 872 | } |
| 873 | clearResidualCache() { |
| 874 | let isClearing = false; |
| 875 | $(document).on("click", ".btn[nitropack-rc-data]", function (e) { |
| 876 | e.preventDefault(); |
| 877 | if (isClearing) return; |
| 878 | let currentButton = $(this); |
| 879 | $.ajax({ |
| 880 | url: ajaxurl, |
| 881 | type: "POST", |
| 882 | dataType: "text", |
| 883 | data: { |
| 884 | action: "nitropack_clear_residual_cache", |
| 885 | gde: currentButton.attr("nitropack-rc-data"), |
| 886 | nonce: nitroNonce, |
| 887 | }, |
| 888 | beforeSend: function () { |
| 889 | isClearing = true; |
| 890 | }, |
| 891 | success: function (resp) { |
| 892 | NitropackUI.triggerToast("success", np_settings.success_msg); |
| 893 | }, |
| 894 | error: function (resp) { |
| 895 | NitropackUI.triggerToast("error", np_settings.success_msg); |
| 896 | }, |
| 897 | complete: function () { |
| 898 | isClearing = false; |
| 899 | setTimeout(function () { |
| 900 | location.reload(); |
| 901 | }, 3000); |
| 902 | }, |
| 903 | }); |
| 904 | }); |
| 905 | } |
| 906 | } |
| 907 | const NitroPackSettings = new nitropackSettings(); |
| 908 | window.NitroPackSettings = NitroPackSettings; |
| 909 | }); |
| 910 |