admin_bar_menu.js
1 year ago
flowbite.min.js
2 years ago
gravity_forms.js
3 years ago
math_captcha.js
3 years ago
metabox.js
2 years ago
nitropackUI.js
2 years ago
np_notices.js
1 year ago
np_safemode.js
2 years ago
np_settings.js
1 year ago
popper.min.js
4 years ago
widgets_ajax.js
3 years ago
np_settings.js
371 lines
| 1 | jQuery(document).ready(function ($) { |
| 2 | class nitropackSettings { |
| 3 | |
| 4 | constructor() { |
| 5 | this.initial_settings = { |
| 6 | ajaxShortcodes: { |
| 7 | enabled: 0, |
| 8 | shortcodes: [] |
| 9 | }, |
| 10 | cacheWarmUp: { |
| 11 | enabled: 0 |
| 12 | } |
| 13 | }; |
| 14 | //run settings |
| 15 | this.cacheWarmUp(); |
| 16 | this.ajaxShortcodes = this.ajaxShortcodes(); |
| 17 | //unsaved changes |
| 18 | this.onPageLeave(); |
| 19 | //must be last so we get updated copy |
| 20 | this.unsavedChangesModal = false; |
| 21 | this.modified_settings = JSON.parse(JSON.stringify(this.initial_settings)); |
| 22 | } |
| 23 | cacheWarmUp() { |
| 24 | const setting_id = '#warmup-status', |
| 25 | msg_wrapper = $('#loading-warmup-status'), |
| 26 | msg_icon = msg_wrapper.find('.icon'), |
| 27 | msg_text = msg_wrapper.find('.msg'), |
| 28 | nitroSelf = this; |
| 29 | |
| 30 | $(setting_id).change(function () { |
| 31 | if ($(this).is(':checked')) { |
| 32 | estimateWarmup(); |
| 33 | } else { |
| 34 | disableWarmup(); |
| 35 | } |
| 36 | }); |
| 37 | var disableWarmup = () => { |
| 38 | $.post(ajaxurl, { |
| 39 | action: 'nitropack_disable_warmup', |
| 40 | nonce: np_settings.nitroNonce |
| 41 | }, function (response) { |
| 42 | var resp = JSON.parse(response); |
| 43 | if (resp.type == "success") { |
| 44 | nitroSelf.modified_settings.cacheWarmUp.enabled = 0; |
| 45 | NitropackUI.triggerToast('success', np_settings.success_msg); |
| 46 | } else { |
| 47 | NitropackUI.triggerToast('error', np_settings.error_msg); |
| 48 | } |
| 49 | }); |
| 50 | } |
| 51 | |
| 52 | var estimateWarmup = (id, retry) => { |
| 53 | id = id || null; |
| 54 | retry = retry || 0; |
| 55 | msg_wrapper.removeClass('hidden'); |
| 56 | if (!id) { |
| 57 | msg_text.text(np_settings.est_cachewarmup_msg); |
| 58 | $.post(ajaxurl, { |
| 59 | action: 'nitropack_estimate_warmup', |
| 60 | nonce: np_settings.nitroNonce |
| 61 | }, function (response) { |
| 62 | var resp = JSON.parse(response); |
| 63 | if (resp.type == "success") { |
| 64 | setTimeout((function (id) { |
| 65 | estimateWarmup(id); |
| 66 | })(resp.res), 1000); |
| 67 | } else { |
| 68 | $(setting_id).prop("checked", true); |
| 69 | msg_text.text(resp.message); |
| 70 | |
| 71 | msg_icon.attr('src', np_settings.nitro_plugin_url + '/view/images/info.svg'); |
| 72 | setTimeout(function () { |
| 73 | msg_wrapper.addClass('hidden'); |
| 74 | }, 3000); |
| 75 | |
| 76 | } |
| 77 | }); |
| 78 | } else { |
| 79 | $.post(ajaxurl, { |
| 80 | action: 'nitropack_estimate_warmup', |
| 81 | estId: id, |
| 82 | nonce: np_settings.nitroNonce |
| 83 | }, function (response) { |
| 84 | var resp = JSON.parse(response); |
| 85 | if (resp.type == "success") { |
| 86 | if (isNaN(resp.res) || resp.res == -1) { // Still calculating |
| 87 | if (retry >= 10) { |
| 88 | $(setting_id).prop("checked", false); |
| 89 | msg_icon.attr('src', np_settings.nitro_plugin_url + '/view/images/info.svg'); |
| 90 | msg_text.text(resp.message); |
| 91 | |
| 92 | setTimeout(function () { |
| 93 | msg_wrapper.addClass('hidden'); |
| 94 | }, 3000); |
| 95 | } else { |
| 96 | setTimeout((function (id, retry) { |
| 97 | estimateWarmup(id, retry); |
| 98 | })(id, retry + 1), 1000); |
| 99 | } |
| 100 | } else { |
| 101 | if (resp.res == 0) { |
| 102 | $(setting_id).prop("checked", false); |
| 103 | msg_icon.attr('src', np_settings.nitro_plugin_url + '/view/images/info.svg'); |
| 104 | msg_text.text(resp.message); |
| 105 | setTimeout(function () { |
| 106 | msg_wrapper.addClass('hidden'); |
| 107 | }, 3000); |
| 108 | } else { |
| 109 | enableWarmup(); |
| 110 | } |
| 111 | } |
| 112 | } else { |
| 113 | msg_text.text(resp.message); |
| 114 | setTimeout(function () { |
| 115 | msg_wrapper.addClass('hidden'); |
| 116 | }, 3000); |
| 117 | } |
| 118 | }); |
| 119 | } |
| 120 | } |
| 121 | var enableWarmup = () => { |
| 122 | $.post(ajaxurl, { |
| 123 | action: 'nitropack_enable_warmup', |
| 124 | nonce: np_settings.nitroNonce |
| 125 | }, function (response) { |
| 126 | var resp = JSON.parse(response); |
| 127 | if (resp.type == "success") { |
| 128 | nitroSelf.modified_settings.cacheWarmUp.enabled = 1; |
| 129 | $(setting_id).prop("checked", true); |
| 130 | msg_wrapper.addClass('hidden'); |
| 131 | NitropackUI.triggerToast('success', np_settings.success_msg); |
| 132 | } else { |
| 133 | setTimeout(enableWarmup, 1000); |
| 134 | } |
| 135 | }); |
| 136 | } |
| 137 | |
| 138 | var loadWarmupStatus = function () { |
| 139 | $.ajax({ |
| 140 | url: ajaxurl, |
| 141 | type: "POST", |
| 142 | data: { |
| 143 | action: "nitropack_warmup_stats", |
| 144 | nonce: np_settings.nitroNonce |
| 145 | }, |
| 146 | dataType: "json", |
| 147 | success: function (resp) { |
| 148 | if (resp.type == "success") { |
| 149 | nitroSelf.initial_settings.cacheWarmUp.enabled = 1; |
| 150 | nitroSelf.modified_settings.cacheWarmUp.enabled = 1; |
| 151 | $(setting_id).prop("checked", !!resp.stats.status); |
| 152 | msg_wrapper.addClass('hidden'); |
| 153 | } else { |
| 154 | setTimeout(loadWarmupStatus, 500); |
| 155 | } |
| 156 | } |
| 157 | }); |
| 158 | } |
| 159 | |
| 160 | loadWarmupStatus(); |
| 161 | } |
| 162 | ajaxShortcodes() { |
| 163 | //main setting |
| 164 | const setting_id = '#ajax-shortcodes', |
| 165 | nitroSelf = this; |
| 166 | if ($(setting_id).is(':checked')) { |
| 167 | nitroSelf.initial_settings.ajaxShortcodes.enabled = 1; |
| 168 | } |
| 169 | |
| 170 | $(setting_id).change(function () { |
| 171 | if ($(this).is(':checked')) { |
| 172 | ajaxShortcodeRequest(null, 1); |
| 173 | } else { |
| 174 | ajaxShortcodeRequest(null, 0); |
| 175 | } |
| 176 | }); |
| 177 | //template for selected shortcodes tags |
| 178 | let select2 = $('#ajax-shortcodes-dropdown').select2({ |
| 179 | selectOnClose: false, |
| 180 | tags: true, |
| 181 | multiple: true, |
| 182 | width: '100%', |
| 183 | placeholder: 'Enter a shortcode', |
| 184 | templateSelection: shortcodeTagTemplate, |
| 185 | }); |
| 186 | nitroSelf.initial_settings.ajaxShortcodes.shortcodes.push(select2.val()); |
| 187 | |
| 188 | select2.on('change', (event) => { |
| 189 | const selectedValues = $(event.target).val(); // Get selected values |
| 190 | this.modified_settings.ajaxShortcodes.shortcodes = selectedValues; |
| 191 | if (selectedValues.length === 0) { |
| 192 | $('.select2-search.select2-search--inline .select2-search__field').addClass('w-full'); |
| 193 | } else { |
| 194 | $('.select2-search.select2-search--inline .select2-search__field').removeClass('w-full'); |
| 195 | } |
| 196 | }); |
| 197 | $('.select2-search.select2-search--inline .select2-search__field').addClass('w-full'); |
| 198 | //select2 |
| 199 | function shortcodeTagTemplate(item) { |
| 200 | if (!item.id) { |
| 201 | return item.text; |
| 202 | } |
| 203 | var $item = $( |
| 204 | '<span class="select2-selection__choice-inner">' + |
| 205 | item.text + |
| 206 | '<span class="np-select2-remove"></span>' + |
| 207 | '</span>' |
| 208 | ); |
| 209 | return $item; |
| 210 | }; |
| 211 | //remove single shortcode |
| 212 | $('.ajax-shortcodes').on('click', '.np-select2-remove', function () { |
| 213 | let valueToRemove = $(this).closest('li.select2-selection__choice').attr('title'), |
| 214 | newVals = select2.val().filter(function (item) { |
| 215 | return item !== valueToRemove; |
| 216 | }); |
| 217 | select2.val(newVals).trigger('change'); |
| 218 | }); |
| 219 | //btn save click |
| 220 | $('.ajax-shortcodes #save-shortcodes').click(function () { |
| 221 | let shortcodes = $('#ajax-shortcodes-dropdown').val(); |
| 222 | ajaxShortcodeRequest(shortcodes, null); |
| 223 | }); |
| 224 | |
| 225 | /* shortcodes - array of shortcodes or null |
| 226 | enabled - 1 or 0 |
| 227 | */ |
| 228 | const ajaxShortcodeRequest = function (shortcodes, enabled) { |
| 229 | let data_obj = { |
| 230 | action: "nitropack_set_ajax_shortcodes_ajax", |
| 231 | nonce: np_settings.nitroNonce, |
| 232 | } |
| 233 | |
| 234 | if (Array.isArray(shortcodes)) { |
| 235 | if (shortcodes.length == 0) { |
| 236 | data_obj.shortcodes = ['']; |
| 237 | } else { |
| 238 | data_obj.shortcodes = shortcodes; |
| 239 | } |
| 240 | } |
| 241 | if (enabled !== null) data_obj.enabled = enabled; |
| 242 | |
| 243 | const response = $.ajax({ |
| 244 | url: ajaxurl, |
| 245 | type: "POST", |
| 246 | data: data_obj, |
| 247 | dataType: "json", |
| 248 | success: function (resp) { |
| 249 | if (resp.type == "success") { |
| 250 | if (enabled == 1) { |
| 251 | $('.ajax-shortcodes').removeClass('hidden'); |
| 252 | nitroSelf.modified_settings.ajaxShortcodes.enabled = 1; |
| 253 | } |
| 254 | if (enabled == 0) { |
| 255 | $('.ajax-shortcodes').addClass('hidden'); |
| 256 | nitroSelf.modified_settings.ajaxShortcodes.enabled = 0; |
| 257 | } |
| 258 | if (shortcodes) { |
| 259 | nitroSelf.initial_settings.ajaxShortcodes.shortcodes = shortcodes; |
| 260 | } |
| 261 | NitropackUI.triggerToast('success', np_settings.success_msg); |
| 262 | } else { |
| 263 | NitropackUI.triggerToast('error', np_settings.error_msg); |
| 264 | } |
| 265 | } |
| 266 | }); |
| 267 | return response; |
| 268 | } |
| 269 | return { |
| 270 | ajaxShortcodeRequest: ajaxShortcodeRequest |
| 271 | }; |
| 272 | } |
| 273 | // Function to omit 'enabled' property |
| 274 | omitEnabledProperty(obj) { |
| 275 | return Object.keys(obj).reduce((acc, key) => { |
| 276 | if (typeof obj[key] === 'object' && obj[key] !== null) { |
| 277 | acc[key] = this.omitEnabledProperty(obj[key]); |
| 278 | } else if (key !== 'enabled') { |
| 279 | acc[key] = obj[key]; |
| 280 | } |
| 281 | return acc; |
| 282 | }, {}); |
| 283 | } |
| 284 | |
| 285 | // Function to check for unsaved changes, ignoring 'enabled' property |
| 286 | hasUnsavedChanges() { |
| 287 | const initialWithoutEnabled = this.omitEnabledProperty(this.initial_settings); |
| 288 | const modifiedWithoutEnabled = this.omitEnabledProperty(this.modified_settings); |
| 289 | return JSON.stringify(initialWithoutEnabled) !== JSON.stringify(modifiedWithoutEnabled); |
| 290 | } |
| 291 | |
| 292 | // Function to handle page leave |
| 293 | onPageLeave() { |
| 294 | const nitroSelf = this; |
| 295 | window.onbeforeunload = function (event) { |
| 296 | if (nitroSelf.hasUnsavedChanges() && !nitroSelf.unsavedChangesModal && nitroSelf.modified_settings.ajaxShortcodes.enabled === 1) { |
| 297 | event.preventDefault(); // show prompt |
| 298 | } |
| 299 | }; |
| 300 | //a links - display modal |
| 301 | $(document).on('click', 'a[href]:not([target="_blank"])', function (event) { |
| 302 | if (nitroSelf.hasUnsavedChanges() && nitroSelf.modified_settings.ajaxShortcodes.enabled === 1) { |
| 303 | event.preventDefault(); |
| 304 | const leaveUrl = this.href |
| 305 | nitroSelf.showUnsavedChangesModal(() => { |
| 306 | window.location.href = leaveUrl |
| 307 | }); |
| 308 | } |
| 309 | }); |
| 310 | } |
| 311 | // Show unsaved changes modal |
| 312 | showUnsavedChangesModal(onConfirm) { |
| 313 | const nitroSelf = this; |
| 314 | //vanilla js |
| 315 | const modalID = 'modal-unsavedChanges', |
| 316 | $modal_target = document.getElementById(modalID), |
| 317 | modal_options = { |
| 318 | backdrop: 'static', |
| 319 | backdropClasses: 'nitro-backdrop', |
| 320 | closable: true, |
| 321 | onHide: () => { |
| 322 | this.unsavedChangesModal = false |
| 323 | }, |
| 324 | onShow: () => { |
| 325 | this.unsavedChangesModal = true |
| 326 | }, |
| 327 | }, |
| 328 | instanceOptions = { |
| 329 | id: modalID, |
| 330 | }, |
| 331 | modal = new Modal($modal_target, modal_options, instanceOptions); |
| 332 | //jquery |
| 333 | const modal_wrapper = $('#' + modalID), |
| 334 | x_button = modal_wrapper.find('.close-modal'), |
| 335 | modal_footer = modal_wrapper.find('.popup-footer'), |
| 336 | secondary_btn = modal_footer.find('.popup-close'), |
| 337 | action_btn = modal_footer.find('.btn-primary'); |
| 338 | modal.show(); |
| 339 | |
| 340 | //no action |
| 341 | $(x_button).one('click', function () { |
| 342 | modal.hide(); |
| 343 | }); |
| 344 | //redirect without saving |
| 345 | $(secondary_btn).one('click', function () { |
| 346 | onConfirm(); |
| 347 | modal.hide(); |
| 348 | }); |
| 349 | //save and redirect |
| 350 | $(action_btn).one('click', function () { |
| 351 | const ajaxRequest = nitroSelf.ajaxShortcodes.ajaxShortcodeRequest(nitroSelf.modified_settings.ajaxShortcodes.shortcodes, null); |
| 352 | ajaxRequest.done(function (response) { |
| 353 | if (response.type === 'success') onConfirm(); |
| 354 | }); |
| 355 | ajaxRequest.fail(function () { |
| 356 | console.error("AJAX request failed."); |
| 357 | NitropackUI.triggerToast('error', 'Error saving shortcodes.'); |
| 358 | onConfirm(); |
| 359 | }); |
| 360 | modal.hide(); |
| 361 | }); |
| 362 | } |
| 363 | removeElement(array, value) { |
| 364 | const index = array.indexOf(value); |
| 365 | if (index !== -1) { |
| 366 | array.splice(index, 1); |
| 367 | } |
| 368 | } |
| 369 | } |
| 370 | const NitroPackSettings = new nitropackSettings(); |
| 371 | }); |