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