/** * TigerStyle SEO Backup System - Admin JavaScript * Handles AJAX interactions for backup and restore operations */ jQuery(document).ready(function($) { 'use strict'; // Get AJAX URL and nonce from localized script var tigerstyleBackup = { ajaxurl: ajaxurl || '/wp-admin/admin-ajax.php', nonce: $('#tigerstyle-backup-nonce').val() || '' }; // Progress tracking for operations var progressInterval = null; /** * Show progress indicator */ function showProgress(operation, message) { var progressHtml = '
' + '

' + operation + '

' + '
' + '

' + message + '

' + '
'; $('.backup-status-cards').after(progressHtml); } /** * Update progress indicator */ function updateProgress(percent, message) { $('#tigerstyle-progress .progress-fill').css('width', percent + '%'); $('#tigerstyle-progress .progress-message').text(message); } /** * Hide progress indicator */ function hideProgress() { $('#tigerstyle-progress').fadeOut(function() { $(this).remove(); }); if (progressInterval) { clearInterval(progressInterval); progressInterval = null; } } /** * Show notification */ function showNotification(type, message) { var noticeClass = type === 'success' ? 'notice-success' : 'notice-error'; var noticeHtml = '
' + '

' + message + '

' + '
'; $('.backup-status-cards').before(noticeHtml); // Auto-dismiss after 5 seconds setTimeout(function() { $('.notice').fadeOut(); }, 5000); } /** * Create Full Backup */ $('#create-full-backup').on('click', function(e) { e.preventDefault(); if (confirm('Are you sure you want to create a full backup? This may take several minutes.')) { showProgress('Creating Backup', 'Initializing backup process...'); $.ajax({ url: tigerstyleBackup.ajaxurl, type: 'POST', data: { action: 'tigerstyle_manual_backup', nonce: tigerstyleBackup.nonce, backup_type: 'full' }, success: function(response) { if (response.success) { updateProgress(10, 'Backup started successfully...'); // Start polling for progress startProgressPolling(response.data.backup_id, 'backup'); showNotification('success', 'Backup process started. ID: ' + response.data.backup_id); } else { hideProgress(); showNotification('error', 'Backup failed: ' + response.data); } }, error: function() { hideProgress(); showNotification('error', 'Failed to start backup process.'); } }); } }); /** * View Backup History */ $('#view-backup-history').on('click', function(e) { e.preventDefault(); showProgress('Loading History', 'Fetching backup history...'); $.ajax({ url: tigerstyleBackup.ajaxurl, type: 'POST', data: { action: 'tigerstyle_backup_stats', nonce: tigerstyleBackup.nonce, days: 30 }, success: function(response) { hideProgress(); if (response.success) { displayBackupHistory(response.data); } else { showNotification('error', 'Failed to load backup history: ' + response.data); } }, error: function() { hideProgress(); showNotification('error', 'Failed to connect to server.'); } }); }); /** * Display backup history in a modal-like interface */ function displayBackupHistory(data) { var historyHtml = '
' + '

Backup History (Last 30 Days)

' + '' + '' + ''; if (data.backup_stats && data.backup_stats.length > 0) { data.backup_stats.forEach(function(backup) { historyHtml += '' + '' + '' + '' + '' + '' + ''; }); } else { historyHtml += ''; } historyHtml += '
DateTypeSizeStatusActions
' + backup.created_at + '' + (backup.backup_type || 'Full') + '' + (backup.file_size || 'Unknown') + '' + (backup.status || 'Completed') + '
No backups found
' + '

Storage Stats:

' + '' + '' + '
' + '
'; $('body').append(historyHtml); // Close modal handlers $('#close-history-modal, #backup-history-overlay').on('click', function() { $('#backup-history-modal, #backup-history-overlay').remove(); }); } /** * Start polling for backup/restore progress */ function startProgressPolling(operationId, type) { var pollCount = 0; var maxPolls = 60; // Maximum 5 minutes of polling (5 second intervals) progressInterval = setInterval(function() { pollCount++; $.ajax({ url: tigerstyleBackup.ajaxurl, type: 'POST', data: { action: 'tigerstyle_backup_progress', nonce: tigerstyleBackup.nonce, operation_id: operationId }, success: function(response) { if (response.success) { var progress = response.data; updateProgress(progress.percent || 50, progress.message || 'Processing...'); // Check if operation is complete if (progress.status === 'completed') { updateProgress(100, 'Operation completed successfully!'); setTimeout(hideProgress, 2000); showNotification('success', type === 'backup' ? 'Backup completed successfully!' : 'Restore completed successfully!'); } else if (progress.status === 'failed') { hideProgress(); showNotification('error', 'Operation failed: ' + (progress.error || 'Unknown error')); } } else { // Operation might be completed or failed if (pollCount > 5) { // Give it a few tries before assuming completion updateProgress(100, 'Operation completed (status unknown)'); setTimeout(hideProgress, 2000); showNotification('success', 'Operation appears to have completed.'); } } }, error: function() { if (pollCount > maxPolls) { hideProgress(); showNotification('error', 'Progress tracking timed out. Operation may still be running.'); } } }); if (pollCount > maxPolls) { hideProgress(); showNotification('error', 'Operation timed out after 5 minutes.'); } }, 5000); // Poll every 5 seconds } // Add some basic CSS for progress indicators if ($('#tigerstyle-backup-styles').length === 0) { $('').appendTo('head'); } });