jQuery(function ($) { var bulkConversion = { inprogress: false, serverDown: false, i18n: {}, settings: {}, startConvertButton: $('#wrio-start-conversion'), startOptButton: $('#wrio-start-optimization'), init: function () { this.i18n = wrio_l18n_bulk_page; this.settings = wrio_settings_bulk_page; this.registerEvents(); }, registerEvents: function () { var self = this; this.startConvertButton.on('click', function () { if ($(this).hasClass('wio-running')) { self.startOptButton.prop('disabled', false); self.stop(); return; } self.showModal(); return false; }); }, showModal: function () { var self = this; var infosModal = $('#wrio-tmpl-webp-conversion'); if ('server_5' !== $('#wrio-change-optimization-server').val()) { $.wrio_modal.showErrorModal(this.i18n.webp_premium_server_disabled); console.log('[Error]: ' + this.i18n.webp_premium_server_disabled); return; } if (!infosModal.length) { console.log('[Error]: Html template for modal not found.'); return; } // Swal Information before loading the optimize process. swal({ title: this.i18n.modal_conversion_title, html: infosModal.html(), type: '', customClass: 'wrio-modal wrio-modal-optimization-way', showCancelButton: true, showCloseButton: true, padding: 0, width: 740, confirmButtonText: this.i18n.modal_conversion_manual_button, cancelButtonText: this.i18n.modal_conversion_cron_button, reverseButtons: true, }).then(function (result) { self.startOptButton.prop('disabled', true); self.process(); window.onbeforeunload = function () { return self.i18n.leave_page_warning; } }, function (dismiss) { if (dismiss === 'cancel') { // you might also handle 'close' or 'timer' if you used those self.startOptButton.prop('disabled', true); self.process('cron'); } else { throw dismiss; } }); }, /** * Start conversion * @param {string} type */ process: function (type) { var self = this; this.inprogress = true; var sendData = { 'action': 'wrio-bulk-conversion-process', 'scope': this.settings.scope, 'multisite': 0, '_wpnonce': this.settings.conversion_nonce, }; this.setButtonStyleRun(type); if ('cron' === type) { this.startConvertButton.addClass('wrio-cron-mode'); sendData['action'] = 'wrio-webp-cron-start'; $.post(ajaxurl, sendData, function (response) { if (!response || !response.success) { console.log('[Error]: Failed ajax request (Start cron).'); console.log(sendData); console.log(response); if (response.data && response.data.error_message) { self.throwError(response.data.error_message); } } else { if (response.data && response.data.stop) { self.stop(); } } }).fail(function (xhr, status, error) { console.log(xhr); console.log(status); console.log(error); self.throwError(error); }); return; } this.showMessage(this.i18n.conversion_inprogress.replace("%s", parseInt($('#wio-unoptimized-num').text()))); // show message: Optimization remined /*if( "1" === this.settings.is_network_admin ) { sendData['multisite'] = 1; }*/ sendData['reset_current_errors'] = 1; this.sendRequest(sendData); }, stop: function () { var self = this; this.inprogress = false; window.onbeforeunload = null; self.setButtonStyleStop(); self.destroyMessages(); if (this.startConvertButton.hasClass('wrio-cron-mode')) { this.startConvertButton.removeClass('wrio-cron-mode'); $.post(ajaxurl, { 'action': 'wrio-webp-cron-stop', '_wpnonce': self.settings.conversion_nonce, 'scope': self.settings.scope }, function (response) { if (!response || !response.success) { console.log('[Error]: Failed ajax request (Stop cron).'); console.log(response); if (response.data && response.data.error_message) { self.throwError(response.data.error_message); } } else { self.startOptButton.prop('disabled', false); } }).fail(function (xhr, status, error) { console.log(xhr); console.log(status); console.log(error); self.throwError(error); }); } }, complete: function () { this.inprogress = false; window.onbeforeunload = null; this.setButtonStyleComplete(); }, setButtonStyleRun: function (mode) { this.startConvertButton.addClass('wio-running'); if ("cron" === mode) { this.startConvertButton.text(this.i18n.modal_conversion_cron_button_stop); return; } this.startConvertButton.text(this.i18n.button_stop); }, setButtonStyleComplete: function () { this.showMessage(this.i18n.conversion_complete); this.startConvertButton.text(this.i18n.button_completed); this.startConvertButton.removeClass('wio-running'); this.startConvertButton.prop('disabled', true); this.startOptButton.prop('disabled', false); }, setButtonStyleStop: function () { this.startConvertButton.removeClass('wio-running'); this.startConvertButton.text(this.i18n.webp_button_start); }, showMessage: function (text) { var contanier = $('.wio-page-statistic'), message; if (contanier.find('.wrio-statistic-message').length) { message = contanier.find('.wrio-statistic-message'); } else { message = $('