tigerstyle-heat/includes/backup/backup-admin.js
Ryan Malloy 0028738e33 Initial commit: TigerStyle Heat v2.0.0
Make your WordPress site irresistible. Natural SEO attraction with:
- robots.txt management
- sitemap.xml generation
- LLMs.txt support
- Google integration (Analytics, Search Console, Tag Manager)
- Schema.org structured data
- Open Graph / Twitter Card meta tags
- AMP support
- Visual elements gallery
- Built-in backup/restore module

Includes build.sh and .distignore for WordPress-installable release ZIPs.
2026-05-27 13:41:35 -06:00

254 lines
10 KiB
JavaScript

/**
* 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 = '<div id="tigerstyle-progress" class="notice notice-info">' +
'<p><strong>' + operation + '</strong></p>' +
'<div class="progress-bar"><div class="progress-fill" style="width: 0%"></div></div>' +
'<p class="progress-message">' + message + '</p>' +
'</div>';
$('.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 = '<div class="notice ' + noticeClass + ' is-dismissible">' +
'<p>' + message + '</p>' +
'<button type="button" class="notice-dismiss">' +
'<span class="screen-reader-text">Dismiss this notice.</span>' +
'</button></div>';
$('.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 = '<div id="backup-history-modal" style="position: fixed; top: 50px; left: 50%; transform: translateX(-50%); width: 90%; max-width: 800px; background: white; border: 1px solid #ccc; box-shadow: 0 4px 8px rgba(0,0,0,0.1); z-index: 10000; padding: 20px;">' +
'<h3>Backup History (Last 30 Days)</h3>' +
'<table class="wp-list-table widefat fixed striped">' +
'<thead><tr><th>Date</th><th>Type</th><th>Size</th><th>Status</th><th>Actions</th></tr></thead>' +
'<tbody>';
if (data.backup_stats && data.backup_stats.length > 0) {
data.backup_stats.forEach(function(backup) {
historyHtml += '<tr>' +
'<td>' + backup.created_at + '</td>' +
'<td>' + (backup.backup_type || 'Full') + '</td>' +
'<td>' + (backup.file_size || 'Unknown') + '</td>' +
'<td>' + (backup.status || 'Completed') + '</td>' +
'<td><button class="button button-small">Download</button></td>' +
'</tr>';
});
} else {
historyHtml += '<tr><td colspan="5">No backups found</td></tr>';
}
historyHtml += '</tbody></table>' +
'<p><strong>Storage Stats:</strong></p>' +
'<ul>' +
'<li>Total Backups: ' + (data.storage_stats ? data.storage_stats.total_count : 0) + '</li>' +
'<li>Total Size: ' + (data.storage_stats ? data.storage_stats.total_size : 'Unknown') + '</li>' +
'</ul>' +
'<button id="close-history-modal" class="button button-primary">Close</button>' +
'</div>' +
'<div id="backup-history-overlay" style="position: fixed; top: 0; left: 0; width: 100%; height: 100%; background: rgba(0,0,0,0.5); z-index: 9999;"></div>';
$('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) {
$('<style id="tigerstyle-backup-styles">' +
'.progress-bar { background: #f1f1f1; border-radius: 3px; overflow: hidden; height: 20px; margin: 10px 0; }' +
'.progress-fill { background: #0073aa; height: 100%; transition: width 0.3s ease; }' +
'.status-indicator { display: inline-block; width: 10px; height: 10px; border-radius: 50%; margin-right: 5px; }' +
'.status-indicator.success { background: #46b450; }' +
'.status-indicator.warning { background: #ffba00; }' +
'.status-indicator.error { background: #dc3232; }' +
'.backup-status-cards { display: flex; gap: 20px; margin: 20px 0; }' +
'.status-card { flex: 1; background: #f9f9f9; padding: 15px; border: 1px solid #ddd; border-radius: 3px; }' +
'.backup-features ul { list-style: none; padding: 0; }' +
'.backup-features li { padding: 5px 0; }' +
'</style>').appendTo('head');
}
});