jQuery(function($){
const ajax = WSBR.ajax, nonce = WSBR.nonce;
function post(data){ data.nonce=nonce; return $.post(ajax,data); }
function esc(s){ return $('
').text(s||'').html(); }
function loadBackups(){
const box=$('#wsbr-backups'); if(!box.length)return;
box.html('
Loading backups…
');
post({action:'wsbr_list_backups'}).done(function(r){
if(!r.success){box.html('
'+esc(r.data.message)+'
');return;}
if(!r.data.backups.length){box.html('
No server backups found.
');return;}
let html='
BackupSizeDateActions
';
r.data.backups.forEach(function(b){
html+='
'+esc(b.name)+''+esc(b.size)+''+esc(b.date)+'Download ';
});
html+='
'; box.html(html);
});
}
function createBackup(){
const status=$('#wsbr-status'), bar=$('#wsbr-progress-bar'), btn=$('#wsbr-create');
btn.prop('disabled',true); bar.css('width','0%'); status.text('Preparing backup list…');
post({action:'wsbr_prepare_backup'}).done(function(r){
if(!r.success){status.text((r && r.data && r.data.message) ? r.data.message : 'Backup could not be started.');btn.prop('disabled',false);return;}
const id=r.data.id; status.text('Backing up 0 of '+r.data.total+' files…');
function batch(){
post({action:'wsbr_backup_batch',id:id}).done(function(x){
if(!x.success){status.text((x && x.data && x.data.message) ? x.data.message : 'Backup failed.');btn.prop('disabled',false);return;}
bar.css('width',(typeof x.data.progress !== 'undefined' ? x.data.progress : 0)+'%');
if(x.data.done){status.text('Backup completed: '+x.data.name+' ('+x.data.size+')');btn.prop('disabled',false);loadBackups();}
else {status.text('Backing up '+x.data.processed+' of '+x.data.total+' files…'); setTimeout(batch,50);}
}).fail(function(){status.text('A server error interrupted the backup.');btn.prop('disabled',false);});
} batch();
});
}
$(document).on('click','#wsbr-create',createBackup);
$(document).on('click','#wsbr-refresh',function(e){e.preventDefault();loadBackups();});
$(document).on('click','.wsbr-delete',function(){
if(!confirm(WSBR.i18n.confirmDelete))return;
const name=$(this).data('name');
post({action:'wsbr_delete_backup',name:name}).done(function(r){if(r.success)loadBackups();else alert(r.data.message);});
});
$(document).on('click','.wsbr-restore',function(){
if(!confirm(WSBR.i18n.confirmRestore))return;
const name=$(this).data('name');
const row=$(this).closest('.wsbr-tr');
if(!$('#wsbr-restore-progress').length){
$('.wsbr-card').first().before('
Restore Progress
0%
Preparing restore…
');
}
const bar=$('#wsbr-restore-bar'), pct=$('#wsbr-restore-percent'), status=$('#wsbr-restore-status');
row.find('button').prop('disabled',true); bar.css('width','0%'); pct.text('0%'); status.text('Preparing restore…');
post({action:'wsbr_prepare_restore',name:name}).done(function(r){
if(!r || !r.success){status.text((r && r.data && r.data.message) ? r.data.message : 'Unable to prepare the restore. Please try again.');row.find('button').prop('disabled',false);return;}
const id=r.data.id;
function batch(){
post({action:'wsbr_restore_batch',id:id}).done(function(x){
if(!x || !x.success){status.text((x && x.data && x.data.message) ? x.data.message : 'Restore failed. Please check the browser console/server error log.');row.find('button').prop('disabled',false);return;}
const progress=Number((typeof x.data.progress !== 'undefined' ? x.data.progress : 0)||0); bar.css('width',progress+'%'); pct.text(progress.toFixed(1)+'%'); status.text(x.data.message||'Restoring…');
if(x.data.done){bar.css('width','100%');pct.text('100%');status.text((x && x.data && x.data.message) ? x.data.message : 'Restore completed.');row.find('button').prop('disabled',false);}
else setTimeout(batch,80);
}).fail(function(xhr){var msg='A server error interrupted the restore.'; if(xhr.responseJSON && xhr.responseJSON.data && xhr.responseJSON.data.message) msg=xhr.responseJSON.data.message; status.text(msg);row.find('button').prop('disabled',false);});
} batch();
});
});
$('#wsbr-s3-test').on('click',function(){
post({action:'wsbr_s3_test',bucket:$('#wsbr-s3-bucket').val(),region:$('#wsbr-s3-region').val(),access_key:$('#wsbr-s3-access').val(),secret_key:$('#wsbr-s3-secret').val()}).done(function(r){$('#wsbr-s3-message').html('
'+esc(r.data.message)+'
');});
});
$('#wsbr-settings').on('submit',function(e){e.preventDefault();post({action:'wsbr_save_settings',batch_size:$('[name=batch_size]',this).val()}).done(function(r){$('#wsbr-settings-message').text(r.data.message);});});
loadBackups();
});