admin_bar_menu.js
1 year 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
1 year ago
np_safemode.js
1 year ago
np_settings.js
1 year ago
popper.min.js
1 year ago
widgets_ajax.js
1 year ago
np_settings.js
770 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 | this.restoreConnection(); |
| 17 | this.windowNotification(); |
| 18 | this.nitropackAddEventListeners(); |
| 19 | this.clearResidualCache(); |
| 20 | //logger |
| 21 | this.loggerToggle(); |
| 22 | this.setLoggerLevel(); |
| 23 | this.archiveLogs(); |
| 24 | //unsaved changes |
| 25 | this.onPageLeave(); |
| 26 | //must be last so we get updated copy |
| 27 | this.unsavedChangesModal = false; |
| 28 | this.modified_settings = JSON.parse( |
| 29 | JSON.stringify(this.initial_settings) |
| 30 | ); |
| 31 | } |
| 32 | cacheWarmUp() { |
| 33 | const setting_id = "#warmup-status", |
| 34 | msg_wrapper = $("#loading-warmup-status"), |
| 35 | msg_icon = msg_wrapper.find(".icon"), |
| 36 | msg_text = msg_wrapper.find(".msg"), |
| 37 | nitroSelf = this; |
| 38 | |
| 39 | $(setting_id).change(function () { |
| 40 | if ($(this).is(":checked")) { |
| 41 | estimateWarmup(); |
| 42 | } else { |
| 43 | disableWarmup(); |
| 44 | } |
| 45 | }); |
| 46 | var disableWarmup = () => { |
| 47 | $.post( |
| 48 | ajaxurl, |
| 49 | { |
| 50 | action: "nitropack_disable_warmup", |
| 51 | nonce: np_settings.nitroNonce, |
| 52 | }, |
| 53 | function (response) { |
| 54 | var resp = JSON.parse(response); |
| 55 | if (resp.type == "success") { |
| 56 | nitroSelf.modified_settings.cacheWarmUp.enabled = 0; |
| 57 | NitropackUI.triggerToast("success", np_settings.success_msg); |
| 58 | } else { |
| 59 | NitropackUI.triggerToast("error", np_settings.error_msg); |
| 60 | } |
| 61 | } |
| 62 | ); |
| 63 | }; |
| 64 | |
| 65 | var estimateWarmup = (id, retry) => { |
| 66 | id = id || null; |
| 67 | retry = retry || 0; |
| 68 | msg_wrapper.removeClass("hidden"); |
| 69 | if (!id) { |
| 70 | msg_text.text(np_settings.est_cachewarmup_msg); |
| 71 | $.post( |
| 72 | ajaxurl, |
| 73 | { |
| 74 | action: "nitropack_estimate_warmup", |
| 75 | nonce: np_settings.nitroNonce, |
| 76 | }, |
| 77 | function (response) { |
| 78 | var resp = JSON.parse(response); |
| 79 | if (resp.type == "success") { |
| 80 | setTimeout( |
| 81 | (function (id) { |
| 82 | estimateWarmup(id); |
| 83 | })(resp.res), |
| 84 | 1000 |
| 85 | ); |
| 86 | } else { |
| 87 | $(setting_id).prop("checked", true); |
| 88 | msg_text.text(resp.message); |
| 89 | |
| 90 | msg_icon.attr( |
| 91 | "src", |
| 92 | np_settings.nitro_plugin_url + "/view/images/info.svg" |
| 93 | ); |
| 94 | setTimeout(function () { |
| 95 | msg_wrapper.addClass("hidden"); |
| 96 | }, 3000); |
| 97 | } |
| 98 | } |
| 99 | ); |
| 100 | } else { |
| 101 | $.post( |
| 102 | ajaxurl, |
| 103 | { |
| 104 | action: "nitropack_estimate_warmup", |
| 105 | estId: id, |
| 106 | nonce: np_settings.nitroNonce, |
| 107 | }, |
| 108 | function (response) { |
| 109 | var resp = JSON.parse(response); |
| 110 | if (resp.type == "success") { |
| 111 | if (isNaN(resp.res) || resp.res == -1) { |
| 112 | // Still calculating |
| 113 | if (retry >= 10) { |
| 114 | $(setting_id).prop("checked", false); |
| 115 | msg_icon.attr( |
| 116 | "src", |
| 117 | np_settings.nitro_plugin_url + "/view/images/info.svg" |
| 118 | ); |
| 119 | msg_text.text(resp.message); |
| 120 | |
| 121 | setTimeout(function () { |
| 122 | msg_wrapper.addClass("hidden"); |
| 123 | }, 3000); |
| 124 | } else { |
| 125 | setTimeout( |
| 126 | (function (id, retry) { |
| 127 | estimateWarmup(id, retry); |
| 128 | })(id, retry + 1), |
| 129 | 1000 |
| 130 | ); |
| 131 | } |
| 132 | } else { |
| 133 | if (resp.res == 0) { |
| 134 | $(setting_id).prop("checked", false); |
| 135 | msg_icon.attr( |
| 136 | "src", |
| 137 | np_settings.nitro_plugin_url + "/view/images/info.svg" |
| 138 | ); |
| 139 | msg_text.text(resp.message); |
| 140 | setTimeout(function () { |
| 141 | msg_wrapper.addClass("hidden"); |
| 142 | }, 3000); |
| 143 | } else { |
| 144 | enableWarmup(); |
| 145 | } |
| 146 | } |
| 147 | } else { |
| 148 | msg_text.text(resp.message); |
| 149 | setTimeout(function () { |
| 150 | msg_wrapper.addClass("hidden"); |
| 151 | }, 3000); |
| 152 | } |
| 153 | } |
| 154 | ); |
| 155 | } |
| 156 | }; |
| 157 | var enableWarmup = () => { |
| 158 | $.post( |
| 159 | ajaxurl, |
| 160 | { |
| 161 | action: "nitropack_enable_warmup", |
| 162 | nonce: np_settings.nitroNonce, |
| 163 | }, |
| 164 | function (response) { |
| 165 | var resp = JSON.parse(response); |
| 166 | if (resp.type == "success") { |
| 167 | nitroSelf.modified_settings.cacheWarmUp.enabled = 1; |
| 168 | $(setting_id).prop("checked", true); |
| 169 | msg_wrapper.addClass("hidden"); |
| 170 | NitropackUI.triggerToast("success", np_settings.success_msg); |
| 171 | } else { |
| 172 | setTimeout(enableWarmup, 1000); |
| 173 | } |
| 174 | } |
| 175 | ); |
| 176 | }; |
| 177 | |
| 178 | var loadWarmupStatus = function () { |
| 179 | $.ajax({ |
| 180 | url: ajaxurl, |
| 181 | type: "POST", |
| 182 | data: { |
| 183 | action: "nitropack_warmup_stats", |
| 184 | nonce: np_settings.nitroNonce, |
| 185 | }, |
| 186 | dataType: "json", |
| 187 | success: function (resp) { |
| 188 | if (resp.type == "success") { |
| 189 | nitroSelf.initial_settings.cacheWarmUp.enabled = 1; |
| 190 | nitroSelf.modified_settings.cacheWarmUp.enabled = 1; |
| 191 | $(setting_id).prop("checked", !!resp.stats.status); |
| 192 | msg_wrapper.addClass("hidden"); |
| 193 | } else { |
| 194 | setTimeout(loadWarmupStatus, 500); |
| 195 | } |
| 196 | }, |
| 197 | }); |
| 198 | }; |
| 199 | |
| 200 | if ($(setting_id).length) loadWarmupStatus(); |
| 201 | } |
| 202 | ajaxShortcodes() { |
| 203 | //main setting |
| 204 | const setting_id = "#ajax-shortcodes", |
| 205 | nitroSelf = this; |
| 206 | if ($(setting_id).is(":checked")) { |
| 207 | nitroSelf.initial_settings.ajaxShortcodes.enabled = 1; |
| 208 | } |
| 209 | |
| 210 | $(setting_id).change(function () { |
| 211 | if ($(this).is(":checked")) { |
| 212 | ajaxShortcodeRequest(null, 1); |
| 213 | } else { |
| 214 | ajaxShortcodeRequest(null, 0); |
| 215 | } |
| 216 | }); |
| 217 | //template for selected shortcodes tags |
| 218 | let select2 = $("#ajax-shortcodes-dropdown").select2({ |
| 219 | selectOnClose: false, |
| 220 | tags: true, |
| 221 | multiple: true, |
| 222 | width: "100%", |
| 223 | placeholder: "Enter a shortcode", |
| 224 | templateSelection: shortcodeTagTemplate, |
| 225 | }), |
| 226 | shortcodes_val = select2.val(); |
| 227 | if (shortcodes_val && shortcodes_val.length > 0) { |
| 228 | nitroSelf.initial_settings.ajaxShortcodes.shortcodes = select2.val(); |
| 229 | } else { |
| 230 | nitroSelf.initial_settings.ajaxShortcodes.shortcodes = []; |
| 231 | } |
| 232 | |
| 233 | select2.on("change", (event) => { |
| 234 | const selectedValues = $(event.target).val(); // Get selected values |
| 235 | this.modified_settings.ajaxShortcodes.shortcodes = selectedValues; |
| 236 | if (selectedValues.length === 0) { |
| 237 | $( |
| 238 | ".select2-search.select2-search--inline .select2-search__field" |
| 239 | ).addClass("w-full"); |
| 240 | } else { |
| 241 | $( |
| 242 | ".select2-search.select2-search--inline .select2-search__field" |
| 243 | ).removeClass("w-full"); |
| 244 | } |
| 245 | }); |
| 246 | $( |
| 247 | ".select2-search.select2-search--inline .select2-search__field" |
| 248 | ).addClass("w-full"); |
| 249 | //select2 |
| 250 | function shortcodeTagTemplate(item) { |
| 251 | if (!item.id) { |
| 252 | return item.text; |
| 253 | } |
| 254 | var $item = $( |
| 255 | '<span class="select2-selection__choice-inner">' + |
| 256 | item.text + |
| 257 | '<span class="np-select2-remove"></span>' + |
| 258 | "</span>" |
| 259 | ); |
| 260 | return $item; |
| 261 | } |
| 262 | //remove single shortcode |
| 263 | $(".ajax-shortcodes").on("click", ".np-select2-remove", function () { |
| 264 | let valueToRemove = $(this) |
| 265 | .closest("li.select2-selection__choice") |
| 266 | .attr("title"), |
| 267 | newVals = select2.val().filter(function (item) { |
| 268 | return item !== valueToRemove; |
| 269 | }); |
| 270 | select2.val(newVals).trigger("change"); |
| 271 | }); |
| 272 | //btn save click |
| 273 | $(".ajax-shortcodes #save-shortcodes").click(function () { |
| 274 | let shortcodes = $("#ajax-shortcodes-dropdown").val(); |
| 275 | ajaxShortcodeRequest(shortcodes, null); |
| 276 | }); |
| 277 | |
| 278 | /* shortcodes - array of shortcodes or null |
| 279 | enabled - 1 or 0 |
| 280 | */ |
| 281 | const ajaxShortcodeRequest = function (shortcodes, enabled) { |
| 282 | let data_obj = { |
| 283 | action: "nitropack_set_ajax_shortcodes_ajax", |
| 284 | nonce: np_settings.nitroNonce, |
| 285 | shortcodes: |
| 286 | Array.isArray(shortcodes) && shortcodes.length |
| 287 | ? shortcodes |
| 288 | : [JSON.stringify([])], // Ensure it's always an array |
| 289 | }; |
| 290 | |
| 291 | if (enabled !== null) data_obj.enabled = enabled; |
| 292 | |
| 293 | const response = $.ajax({ |
| 294 | url: ajaxurl, |
| 295 | type: "POST", |
| 296 | data: data_obj, |
| 297 | dataType: "json", |
| 298 | success: function (resp) { |
| 299 | if (resp.type == "success") { |
| 300 | if (enabled == 1) { |
| 301 | $(".ajax-shortcodes").removeClass("hidden"); |
| 302 | nitroSelf.modified_settings.ajaxShortcodes.enabled = 1; |
| 303 | } |
| 304 | if (enabled == 0) { |
| 305 | $(".ajax-shortcodes").addClass("hidden"); |
| 306 | nitroSelf.modified_settings.ajaxShortcodes.enabled = 0; |
| 307 | } |
| 308 | // Ensure we're setting an array in settings |
| 309 | if (Array.isArray(shortcodes) && shortcodes.length) { |
| 310 | nitroSelf.initial_settings.ajaxShortcodes.shortcodes = |
| 311 | shortcodes; |
| 312 | } else { |
| 313 | nitroSelf.initial_settings.ajaxShortcodes.shortcodes = []; |
| 314 | } |
| 315 | NitropackUI.triggerToast("success", np_settings.success_msg); |
| 316 | } else { |
| 317 | NitropackUI.triggerToast("error", np_settings.error_msg); |
| 318 | } |
| 319 | }, |
| 320 | }); |
| 321 | return response; |
| 322 | }; |
| 323 | return { |
| 324 | ajaxShortcodeRequest: ajaxShortcodeRequest, |
| 325 | }; |
| 326 | } |
| 327 | // Function to omit 'enabled' property |
| 328 | omitEnabledProperty(obj) { |
| 329 | return Object.keys(obj).reduce((acc, key) => { |
| 330 | if (typeof obj[key] === "object" && obj[key] !== null) { |
| 331 | acc[key] = this.omitEnabledProperty(obj[key]); |
| 332 | } else if (key !== "enabled") { |
| 333 | acc[key] = obj[key]; |
| 334 | } |
| 335 | return acc; |
| 336 | }, {}); |
| 337 | } |
| 338 | |
| 339 | // Function to check for unsaved changes, ignoring 'enabled' property |
| 340 | hasUnsavedChanges() { |
| 341 | const initialWithoutEnabled = this.omitEnabledProperty( |
| 342 | this.initial_settings |
| 343 | ); |
| 344 | const modifiedWithoutEnabled = this.omitEnabledProperty( |
| 345 | this.modified_settings |
| 346 | ); |
| 347 | return ( |
| 348 | JSON.stringify(initialWithoutEnabled) !== |
| 349 | JSON.stringify(modifiedWithoutEnabled) |
| 350 | ); |
| 351 | } |
| 352 | |
| 353 | // Function to handle page leave |
| 354 | onPageLeave() { |
| 355 | const nitroSelf = this; |
| 356 | window.onbeforeunload = function (event) { |
| 357 | if ( |
| 358 | nitroSelf.hasUnsavedChanges() && |
| 359 | !nitroSelf.unsavedChangesModal && |
| 360 | nitroSelf.modified_settings.ajaxShortcodes.enabled === 1 |
| 361 | ) { |
| 362 | event.preventDefault(); // show prompt |
| 363 | } |
| 364 | }; |
| 365 | //a links - display modal |
| 366 | $(document).on( |
| 367 | "click", |
| 368 | 'a[href]:not([target="_blank"])', |
| 369 | function (event) { |
| 370 | if ( |
| 371 | nitroSelf.hasUnsavedChanges() && |
| 372 | nitroSelf.modified_settings.ajaxShortcodes.enabled === 1 |
| 373 | ) { |
| 374 | event.preventDefault(); |
| 375 | const leaveUrl = this.href; |
| 376 | nitroSelf.showUnsavedChangesModal(() => { |
| 377 | window.location.href = leaveUrl; |
| 378 | }); |
| 379 | } |
| 380 | } |
| 381 | ); |
| 382 | } |
| 383 | // Show unsaved changes modal |
| 384 | showUnsavedChangesModal(onConfirm) { |
| 385 | const nitroSelf = this; |
| 386 | //vanilla js |
| 387 | const modalID = "modal-unsavedChanges", |
| 388 | $modal_target = document.getElementById(modalID), |
| 389 | modal_options = { |
| 390 | backdrop: "static", |
| 391 | backdropClasses: "nitro-backdrop", |
| 392 | closable: true, |
| 393 | onHide: () => { |
| 394 | this.unsavedChangesModal = false; |
| 395 | }, |
| 396 | onShow: () => { |
| 397 | this.unsavedChangesModal = true; |
| 398 | }, |
| 399 | }, |
| 400 | instanceOptions = { |
| 401 | id: modalID, |
| 402 | }, |
| 403 | modal = new Modal($modal_target, modal_options, instanceOptions); |
| 404 | //jquery |
| 405 | const modal_wrapper = $("#" + modalID), |
| 406 | x_button = modal_wrapper.find(".close-modal"), |
| 407 | modal_footer = modal_wrapper.find(".popup-footer"), |
| 408 | secondary_btn = modal_footer.find(".popup-close"), |
| 409 | action_btn = modal_footer.find(".btn-primary"); |
| 410 | modal.show(); |
| 411 | |
| 412 | //no action |
| 413 | $(x_button).one("click", function () { |
| 414 | modal.hide(); |
| 415 | }); |
| 416 | //redirect without saving |
| 417 | $(secondary_btn).one("click", function () { |
| 418 | onConfirm(); |
| 419 | modal.hide(); |
| 420 | }); |
| 421 | //save and redirect |
| 422 | $(action_btn).one("click", function () { |
| 423 | const ajaxRequest = nitroSelf.ajaxShortcodes.ajaxShortcodeRequest( |
| 424 | nitroSelf.modified_settings.ajaxShortcodes.shortcodes, |
| 425 | null |
| 426 | ); |
| 427 | ajaxRequest.done(function (response) { |
| 428 | if (response.type === "success") onConfirm(); |
| 429 | }); |
| 430 | ajaxRequest.fail(function () { |
| 431 | console.error("AJAX request failed."); |
| 432 | NitropackUI.triggerToast("error", "Error saving shortcodes."); |
| 433 | onConfirm(); |
| 434 | }); |
| 435 | modal.hide(); |
| 436 | }); |
| 437 | } |
| 438 | removeElement(array, value) { |
| 439 | const index = array.indexOf(value); |
| 440 | if (index !== -1) { |
| 441 | array.splice(index, 1); |
| 442 | } |
| 443 | } |
| 444 | loggerToggle() { |
| 445 | const radio = $("#minimum-log-level-status"), |
| 446 | widget = $("#minimum-log-level-widget"), |
| 447 | fancy_radios = widget.find(".fancy-radio"), |
| 448 | fancy_radios_container = widget.find(".fancy-radio-container"); |
| 449 | let minimum_log_level; |
| 450 | |
| 451 | radio.on("change", function () { |
| 452 | const self = $(this); |
| 453 | if (self.is(":checked")) { |
| 454 | minimum_log_level = 3; |
| 455 | } else { |
| 456 | minimum_log_level = null; |
| 457 | } |
| 458 | $.post( |
| 459 | ajaxurl, |
| 460 | { |
| 461 | action: "nitropack_set_log_level_ajax", |
| 462 | minimum_log_level: minimum_log_level, |
| 463 | nonce: np_settings.nitroNonce, |
| 464 | }, |
| 465 | function (response) { |
| 466 | var resp = JSON.parse(response); |
| 467 | if (resp.type == "success") { |
| 468 | if (self.is(":checked")) { |
| 469 | $(".logging").removeClass("hidden"); |
| 470 | fancy_radios_container.removeClass("selected"); |
| 471 | fancy_radios.removeClass("selected"); |
| 472 | widget |
| 473 | .find( |
| 474 | '.fancy-radio-container[data-value="' + |
| 475 | minimum_log_level + |
| 476 | '"' |
| 477 | ) |
| 478 | .addClass("selected"); |
| 479 | widget |
| 480 | .find( |
| 481 | '.fancy-radio-container[data-value="' + |
| 482 | minimum_log_level + |
| 483 | '"' |
| 484 | ) |
| 485 | .find(".fancy-radio") |
| 486 | .addClass("selected"); |
| 487 | } else { |
| 488 | fancy_radios.removeClass("selected"); |
| 489 | $(".logging").addClass("hidden"); |
| 490 | } |
| 491 | |
| 492 | NitropackUI.triggerToast("success", np_settings.success_msg); |
| 493 | } else { |
| 494 | NitropackUI.triggerToast("error", np_settings.success_msg); |
| 495 | $(this).prop("checked", false); |
| 496 | } |
| 497 | } |
| 498 | ); |
| 499 | }); |
| 500 | } |
| 501 | /* Set logger level */ |
| 502 | setLoggerLevel() { |
| 503 | const widget = $("#minimum-log-level-widget"), |
| 504 | fancy_radios_container = widget.find(".fancy-radio-container"), |
| 505 | fancy_radios = widget.find(".fancy-radio"); |
| 506 | let initial_minimum_log_level = widget |
| 507 | .find(".fancy-radio-container.selected") |
| 508 | .data("value"); |
| 509 | |
| 510 | fancy_radios_container.click(function () { |
| 511 | let fancy_radio_container = $(this), |
| 512 | fancy_radio = $(this).find(".fancy-radio"), |
| 513 | minimum_log_level = fancy_radio_container.data("value"); |
| 514 | if (minimum_log_level === initial_minimum_log_level) return; |
| 515 | |
| 516 | $.post( |
| 517 | ajaxurl, |
| 518 | { |
| 519 | action: "nitropack_set_log_level_ajax", |
| 520 | minimum_log_level: minimum_log_level, |
| 521 | nonce: np_settings.nitroNonce, |
| 522 | }, |
| 523 | function (response) { |
| 524 | var resp = JSON.parse(response); |
| 525 | if (resp.type == "success") { |
| 526 | //container |
| 527 | fancy_radios_container.removeClass("selected"); |
| 528 | fancy_radio_container.addClass("selected"); |
| 529 | //custom radios |
| 530 | fancy_radios.removeClass("selected"); |
| 531 | fancy_radio.addClass("selected"); |
| 532 | initial_minimum_log_level = minimum_log_level; |
| 533 | NitropackUI.triggerToast("success", np_settings.success_msg); |
| 534 | } else { |
| 535 | NitropackUI.triggerToast("error", np_settings.success_msg); |
| 536 | $(this).prop("checked", false); |
| 537 | } |
| 538 | } |
| 539 | ); |
| 540 | }); |
| 541 | } |
| 542 | /* Zips all logs and downloads them */ |
| 543 | archiveLogs() { |
| 544 | $(".archive-logs").click(function (e) { |
| 545 | e.preventDefault(); |
| 546 | $.post( |
| 547 | ajaxurl, |
| 548 | { |
| 549 | action: "nitropack_archive_logs_ajax", |
| 550 | nonce: np_settings.nitroNonce, |
| 551 | }, |
| 552 | function (response) { |
| 553 | var resp = JSON.parse(response); |
| 554 | if (resp.type == "success") { |
| 555 | window.location.href = resp.url; |
| 556 | NitropackUI.triggerToast("success", np_settings.success_msg); |
| 557 | } else { |
| 558 | NitropackUI.triggerToast("error", np_settings.success_msg); |
| 559 | } |
| 560 | } |
| 561 | ); |
| 562 | }); |
| 563 | } |
| 564 | restoreConnection() { |
| 565 | const loading_icon = |
| 566 | '<img src="' + |
| 567 | np_settings.nitro_plugin_url + |
| 568 | '/view/images/loading.svg" width="14" class="icon loading"/>', |
| 569 | success_icon = |
| 570 | '<img src="' + |
| 571 | np_settings.nitro_plugin_url + |
| 572 | '/view/images/check.svg" width="16" class="icon success"/>'; |
| 573 | |
| 574 | $("#nitro-restore-connection-btn").on("click", function () { |
| 575 | $.ajax({ |
| 576 | url: ajaxurl, |
| 577 | type: "GET", |
| 578 | data: { |
| 579 | action: "nitropack_reconfigure_webhooks", |
| 580 | nonce: nitroNonce, |
| 581 | }, |
| 582 | dataType: "json", |
| 583 | beforeSend: function () { |
| 584 | $("#nitro-restore-connection-btn") |
| 585 | .attr("disabled", true) |
| 586 | .html(loading_icon); |
| 587 | }, |
| 588 | success: function (data) { |
| 589 | if (!data.status || data.status != "success") { |
| 590 | if (data.message) { |
| 591 | alert( |
| 592 | "<?php esc_html_e('Error:', 'nitropack'); ?> " + data.message |
| 593 | ); |
| 594 | } else { |
| 595 | alert( |
| 596 | "<?php esc_html_e('Error: We were unable to restore the connection. Please contact our support team to get this resolved.', 'nitropack'); ?>" |
| 597 | ); |
| 598 | } |
| 599 | } else { |
| 600 | $("#nitro-restore-connection-btn") |
| 601 | .attr("disabled", true) |
| 602 | .html(success_icon); |
| 603 | NitropackUI.triggerToast("success", data.message); |
| 604 | } |
| 605 | }, |
| 606 | complete: function () { |
| 607 | location.reload(); |
| 608 | }, |
| 609 | }); |
| 610 | }); |
| 611 | } |
| 612 | /* Was used in dashboard.php and oneclick.php */ |
| 613 | loadDismissibleNotices() { |
| 614 | var $ = jQuery; |
| 615 | |
| 616 | $(".nitro-notification.is-dismissible").each(function () { |
| 617 | var b = $(this), |
| 618 | c = $( |
| 619 | '<button type="button" class="notice-dismiss"><span class="screen-reader-text"></span></button>' |
| 620 | ); |
| 621 | c.on("click.wp-dismiss-notice", function ($) { |
| 622 | $.preventDefault(), |
| 623 | b.fadeTo(100, 0, function () { |
| 624 | b.slideUp(100, function () { |
| 625 | b.remove(); |
| 626 | }); |
| 627 | }); |
| 628 | }), |
| 629 | b.append(c); |
| 630 | }); |
| 631 | } |
| 632 | |
| 633 | clearCacheHandler = (clearCacheAction) => { |
| 634 | return function (success, error) { |
| 635 | $.ajax({ |
| 636 | url: ajaxurl, |
| 637 | type: "GET", |
| 638 | data: { |
| 639 | action: "nitropack_" + clearCacheAction + "_cache", |
| 640 | nonce: nitroNonce, |
| 641 | }, |
| 642 | dataType: "json", |
| 643 | beforeSend: function () { |
| 644 | $("#optimizations-purge-cache").attr("disabled", true); |
| 645 | }, |
| 646 | success: function (data) { |
| 647 | if (data.type === "success") { |
| 648 | NitropackUI.triggerToast("success", data.message); |
| 649 | window.dispatchEvent( |
| 650 | new Event("cache." + clearCacheAction + ".success") |
| 651 | ); |
| 652 | } else { |
| 653 | NitropackUI.triggerToast("error", data.message); |
| 654 | window.dispatchEvent( |
| 655 | new Event("cache." + clearCacheAction + ".error") |
| 656 | ); |
| 657 | } |
| 658 | }, |
| 659 | error: function (data) { |
| 660 | NitropackUI.triggerToast("error", data.message); |
| 661 | window.dispatchEvent( |
| 662 | new Event("cache." + clearCacheAction + ".error") |
| 663 | ); |
| 664 | }, |
| 665 | complete: function () { |
| 666 | setTimeout(function () { |
| 667 | $("#optimizations-purge-cache").attr("disabled", false); |
| 668 | }, 3000); |
| 669 | }, |
| 670 | }); |
| 671 | }; |
| 672 | }; |
| 673 | windowNotification() { |
| 674 | const nitroSelf = this; |
| 675 | window.Notification = ((_) => { |
| 676 | var timeout; |
| 677 | var display = (msg, type) => { |
| 678 | clearTimeout(timeout); |
| 679 | $(".nitro-notification").remove(); |
| 680 | //tbd |
| 681 | $('[name="form"]').prepend( |
| 682 | '<div class="nitro-notification notification-' + |
| 683 | type + |
| 684 | '" is-dismissible"><p>' + |
| 685 | msg + |
| 686 | "</p></div>" |
| 687 | ); |
| 688 | |
| 689 | timeout = setTimeout((_) => { |
| 690 | $(".nitro-notification").remove(); |
| 691 | }, 10000); |
| 692 | |
| 693 | nitroSelf.loadDismissibleNotices(); |
| 694 | }; |
| 695 | |
| 696 | return { |
| 697 | success: (msg) => { |
| 698 | display(msg, "success"); |
| 699 | }, |
| 700 | error: (msg) => { |
| 701 | display(msg, "error"); |
| 702 | }, |
| 703 | info: (msg) => { |
| 704 | display(msg, "info"); |
| 705 | }, |
| 706 | warning: (msg) => { |
| 707 | display(msg, "warning"); |
| 708 | }, |
| 709 | }; |
| 710 | })(); |
| 711 | } |
| 712 | nitropackAddEventListeners() { |
| 713 | const nitroSelf = this; |
| 714 | $(window).on("load", (_) => { |
| 715 | window.addEventListener( |
| 716 | "cache.invalidate.request", |
| 717 | nitroSelf.clearCacheHandler("invalidate") |
| 718 | ); |
| 719 | window.addEventListener( |
| 720 | "cache.purge.request", |
| 721 | nitroSelf.clearCacheHandler("purge") |
| 722 | ); |
| 723 | if ($("#np-onstate-cache-purge").length) { |
| 724 | window.addEventListener("cache.purge.success", function () { |
| 725 | setTimeout(function () { |
| 726 | document.cookie = |
| 727 | "nitropack_apwarning=1; expires=Thu, 01 Jan 1970 00:00:01 GMT; path=/"; |
| 728 | window.location.reload(); |
| 729 | }, 1500); |
| 730 | }); |
| 731 | } |
| 732 | }); |
| 733 | } |
| 734 | clearResidualCache() { |
| 735 | let isClearing = false; |
| 736 | $(document).on("click", ".btn[nitropack-rc-data]", function (e) { |
| 737 | e.preventDefault(); |
| 738 | if (isClearing) return; |
| 739 | let currentButton = $(this); |
| 740 | $.ajax({ |
| 741 | url: ajaxurl, |
| 742 | type: "POST", |
| 743 | dataType: "text", |
| 744 | data: { |
| 745 | action: "nitropack_clear_residual_cache", |
| 746 | gde: currentButton.attr("nitropack-rc-data"), |
| 747 | nonce: nitroNonce, |
| 748 | }, |
| 749 | beforeSend: function () { |
| 750 | isClearing = true; |
| 751 | }, |
| 752 | success: function (resp) { |
| 753 | NitropackUI.triggerToast("success", np_settings.success_msg); |
| 754 | }, |
| 755 | error: function (resp) { |
| 756 | NitropackUI.triggerToast("error", np_settings.success_msg); |
| 757 | }, |
| 758 | complete: function () { |
| 759 | isClearing = false; |
| 760 | setTimeout(function () { |
| 761 | location.reload(); |
| 762 | }, 3000); |
| 763 | }, |
| 764 | }); |
| 765 | }); |
| 766 | } |
| 767 | } |
| 768 | const NitroPackSettings = new nitropackSettings(); |
| 769 | }); |
| 770 |