function SGPBDetect() {
}
SGPBDetect.prototype.init = function () {
this.pluginData = [];
this.finishBtn = '';
this.blockScreen();
this.modalDetectionStyles();
this.checkIfModalClosed();
};
SGPBDetect.prototype.blockScreen = function () {
var body = this.generateHtml(SGPB_JS_DETECTIONS);
this.modalDetection(SGPB_JS_DETECTIONS.modalData.header, SGPB_JS_DETECTIONS.modalData.logo, body);
this.updateActions();
};
SGPBDetect.prototype.generateHtml = function (dataToGenerate) {
let html = '';
if (dataToGenerate.autoUpdate.length) {
html = '
';
for (let i = 0; i < dataToGenerate.autoUpdate.length; i++) {
html += `
';
}
if (dataToGenerate.manualUpdate.length) {
html += dataToGenerate.modalData.manualMessage;
html += '
';
for (let i = 0; i < dataToGenerate.manualUpdate.length; i++) {
html += `${dataToGenerate.manualUpdate[i].plugin.name}`;
html += dataToGenerate.manualUpdate[i + 1] !== undefined ? ', ' : '';
}
html += '
';
html += dataToGenerate.modalData.footerMessage;
}
return html;
};
SGPBDetect.prototype.update = function (args) {
var that = this;
args = _.extend({
success: that.updatePluginSuccess,
error: that.updatePluginError,
}, args);
return wp.updates.ajax('update-plugin', args);
};
SGPBDetect.prototype.updatePluginSuccess = function (response) {
jQuery("li[data-key='" + response.plugin + "']").removeClass('updating sgpb-update-plugin-progress');
jQuery("li[data-key='" + response.plugin + "']").addClass('sgpb-update-plugin-success');
jQuery("#sgpbCanUpdateNextPlugin").val(1);
};
SGPBDetect.prototype.updatePluginError = function (response) {
jQuery("li[data-key='" + response.plugin + "']").removeClass('updating sgpb-update-plugin-progress');
jQuery("li[data-key='" + response.plugin + "']").addClass('sgpb-update-plugin-failed');
jQuery("#sgpbCanUpdateNextPlugin").val(0);
};
SGPBDetect.prototype.updateActions = function () {
var that = this;
jQuery('.updateMyPlugin').each(function () {
that.pluginData.push({
plugin: jQuery(this).data('key'),
slug: jQuery(this).data('slug'),
name: jQuery(this).data('name')
});
});
this.updateRecursion();
setInterval(function () {
if (jQuery("#sgpbCanUpdateNextPlugin").val() === '1') {
that.updateRecursion();
}
}, 1000);
};
SGPBDetect.prototype.updateRecursion = function () {
jQuery("#sgpbCanUpdateNextPlugin").val(0);
if (!this.pluginData[+jQuery("#sgpbNextPluginIndex").val()]) {
jQuery('.sgpb-plugins-list-auto-update').empty();
if (!SGPB_JS_DETECTIONS.manualUpdate.length) {
jQuery('.sgpb-modal-detection .sgpb-modal-body').append(this.finishBtn);
jQuery('#sgpbFinishUpdatingProcess').on('click', function () {
window.location.reload();
});
}
return;
}
this.singleItem = this.pluginData[+jQuery("#sgpbNextPluginIndex").val()];
jQuery("li[data-slug='" + this.singleItem.slug + "']").addClass('updating');
this.update({plugin: this.singleItem.plugin, slug: this.singleItem.slug});
jQuery("#sgpbNextPluginIndex").val(+jQuery("#sgpbNextPluginIndex").val() + 1)
};
SGPBDetect.prototype.modalDetectionStyles = function () {
var css = '';
jQuery(css).appendTo(document.body);
};
SGPBDetect.prototype.modalDetection = function (header, logo, body, url) {
jQuery(document.body).addClass('sgpb-overflow-hidden');
var modal = jQuery('', {
"class": 'sgpb sgpb-modal-detection',
html: jQuery('', {
"class": 'sgpb-modal-detection-main',
html:
jQuery('', {
"class": 'sgpb-modal-body',
html: [
jQuery(header),
jQuery('', {
"class": 'sgpb-modal-logo',
src: logo,
alt: 'logo'
}),
body
]
})
})
});
jQuery(modal).appendTo(document.body);
};
SGPBDetect.prototype.checkIfModalClosed = function () {
var that = this;
setInterval(function () {
if (!jQuery('.sgpb.sgpb-modal-detection').length || !jQuery('#sgpb-modal-detection-styles').length) {
that.init();
}
}, 800)
};
jQuery(document).ready(function () {
sgpbDetect = new SGPBDetect();
sgpbDetect.init();
});