File: /home/mahdetej/public_html/wp-content/plugins/Melli-shild/assets/js/scanner.js
/**
* MelliShield Scanner
*
* @package Melli_Shield
* @since 1.0.0
*/
(function($) {
'use strict';
var Scanner = {
batchId: '',
totalBatches: 0,
currentBatch: 0,
isPaused: false,
isStopped: false,
isRunning: false,
init: function() {
if ($('#melli-shield-start-scan').length === 0) return;
this.bind();
},
bind: function() {
var self = this;
$('#melli-shield-start-scan').on('click', function() { self.start('files'); });
$('#melli-shield-scan-db').on('click', function() { self.start('database'); });
$('#melli-shield-clear-scan').on('click', function() { self.clear(); });
$('#melli-shield-pause-scan').on('click', function() { self.togglePause(); });
$('#melli-shield-stop-scan').on('click', function() { self.stop(); });
},
start: function(type) {
if (this.isRunning) return;
this.isRunning = true;
this.isPaused = false;
this.isStopped = false;
this.currentBatch = 0;
var self = this;
var s = melli_shield_ajax.strings;
this.setStatus(type === 'files' ? s.scanning_files : s.scanning_db, 'active');
this.showControls(true);
this.showProgress(true);
this.updateProgress(0);
$.ajax({
url: melli_shield_ajax.ajax_url,
type: 'POST',
dataType: 'json',
data: {
action: 'melli_shield_start_scan',
scan_type: type,
_wpnonce: melli_shield_ajax.nonce
},
success: function(r) {
if (r.success) {
self.batchId = r.data.batch_id;
self.totalBatches = r.data.total_batches;
self.currentBatch = 0;
self.processNext();
} else {
self.finish(false, r.data.message || s.error);
}
},
error: function() {
self.finish(false, s.error);
}
});
},
processNext: function() {
if (this.isStopped) return;
if (this.isPaused) return;
if (this.currentBatch >= this.totalBatches) {
this.finish(true);
return;
}
var self = this;
var pct = Math.round((this.currentBatch / this.totalBatches) * 100);
this.updateProgress(pct);
this.setStatus(melli_shield_ajax.strings.scanning + ' (' + this.currentBatch + '/' + this.totalBatches + ')', 'active');
$.ajax({
url: melli_shield_ajax.ajax_url,
type: 'POST',
dataType: 'json',
data: {
action: 'melli_shield_process_batch',
batch_id: this.batchId,
batch_index: this.currentBatch,
_wpnonce: melli_shield_ajax.nonce
},
success: function(r) {
if (r.success) {
self.currentBatch++;
setTimeout(function() { self.processNext(); }, 50);
} else {
self.finish(false, r.data.message || melli_shield_ajax.strings.error);
}
},
error: function() {
self.finish(false, melli_shield_ajax.strings.error);
}
});
},
togglePause: function() {
this.isPaused = !this.isPaused;
var $btn = $('#melli-shield-pause-scan');
var s = melli_shield_ajax.strings;
if (this.isPaused) {
$btn.text(s.resume);
this.setStatus(s.paused_status, 'warn');
} else {
$btn.text(s.pause);
this.processNext();
}
},
stop: function() {
if (!confirm(melli_shield_ajax.strings.confirm_stop)) return;
this.isStopped = true;
var self = this;
$.ajax({
url: melli_shield_ajax.ajax_url,
type: 'POST',
dataType: 'json',
data: {
action: 'melli_shield_stop_scan',
batch_id: this.batchId,
_wpnonce: melli_shield_ajax.nonce
},
success: function() {
var msg = melli_shield_ajax.strings.scan_stopped_detail
.replace('%completed%', self.currentBatch)
.replace('%total%', self.totalBatches);
self.finish(true, msg);
}
});
},
clear: function() {
if (!confirm(melli_shield_ajax.strings.confirm_clear)) return;
$.ajax({
url: melli_shield_ajax.ajax_url,
type: 'POST',
dataType: 'json',
data: {
action: 'melli_shield_clear_scan',
_wpnonce: melli_shield_ajax.nonce
},
success: function(r) {
if (r.success) {
$('#melli-shield-scan-results').html('<p>' + melli_shield_ajax.strings.no_results + '</p>');
MelliShield.showNotice(r.data.message, 'success');
}
}
});
},
finish: function(success, message) {
this.isRunning = false;
this.showControls(false);
this.updateProgress(100);
var s = melli_shield_ajax.strings;
var msg = message || s.scan_complete;
var type = success ? 'success' : 'error';
this.setStatus(msg, success ? 'success' : 'error');
MelliShield.showNotice(msg, type);
// Reload results
this.loadResults();
},
loadResults: function() {
$.ajax({
url: melli_shield_ajax.ajax_url,
type: 'POST',
dataType: 'json',
data: {
action: 'melli_shield_get_scan_results',
_wpnonce: melli_shield_ajax.nonce
},
success: function(r) {
if (r.success && r.data.html) {
$('#melli-shield-scan-results').html(r.data.html);
}
}
});
},
setStatus: function(text, state) {
var $el = $('#melli-shield-scan-status');
$el.text(text).removeClass('is-active is-success is-warn is-error');
if (state) {
$el.addClass('is-' + state);
}
},
showControls: function(show) {
$('#melli-shield-scan-controls').toggle(show);
$('#melli-shield-start-scan, #melli-shield-scan-db, #melli-shield-clear-scan').prop('disabled', show);
if (show) {
$('#melli-shield-pause-scan').text(melli_shield_ajax.strings.pause);
}
},
showProgress: function(show) {
$('#melli-shield-scan-progress').toggle(show);
},
updateProgress: function(pct) {
pct = Math.min(100, Math.max(0, pct));
$('#melli-shield-progress-bar').css('width', pct + '%').text(pct + '%');
$('#melli-shield-progress-text').text(this.currentBatch + ' / ' + this.totalBatches);
}
};
$(document).ready(function() {
Scanner.init();
});
})(jQuery);