get_recent_logs($limit, $level); wp_send_json_success($logs); } // AJAX handler for testing S3 connection add_action('wp_ajax_tigerstyle_test_s3_connection', 'tigerstyle_ajax_test_s3_connection'); function tigerstyle_ajax_test_s3_connection() { check_ajax_referer('tigerstyle_backup_nonce', 'nonce'); if (!current_user_can('manage_options')) { wp_send_json_error('Insufficient permissions'); } // Temporarily update settings for testing $test_settings = array( 's3_bucket' => sanitize_text_field($_POST['s3_bucket']), 's3_access_key' => sanitize_text_field($_POST['s3_access_key']), 's3_secret_key' => sanitize_text_field($_POST['s3_secret_key']), 's3_region' => sanitize_text_field($_POST['s3_region']), 's3_endpoint' => sanitize_url($_POST['s3_endpoint']) ); // Temporarily override settings $original_settings = get_option('tigerstyle_backup_settings', array()); update_option('tigerstyle_backup_settings', array_merge($original_settings, $test_settings)); try { $storage_manager = new TigerStyleSEO_Storage_Manager(); $result = $storage_manager->test_s3_connection(); // Restore original settings update_option('tigerstyle_backup_settings', $original_settings); if ($result['success']) { wp_send_json_success($result['message']); } else { wp_send_json_error($result['message']); } } catch (Exception $e) { // Restore original settings update_option('tigerstyle_backup_settings', $original_settings); wp_send_json_error($e->getMessage()); } } // AJAX handler for generating reset code add_action('wp_ajax_tigerstyle_generate_reset_code', 'tigerstyle_ajax_generate_reset_code'); function tigerstyle_ajax_generate_reset_code() { check_ajax_referer('tigerstyle_backup_nonce', 'nonce'); if (!current_user_can('manage_options')) { wp_send_json_error('Insufficient permissions'); } $backup_module = tigerstyle_heat()->get_module('backup_restore'); $code = $backup_module->generate_reset_code(); wp_send_json_success(array('code' => $code)); } // AJAX handler for exporting logs add_action('wp_ajax_tigerstyle_export_backup_logs', 'tigerstyle_ajax_export_backup_logs'); function tigerstyle_ajax_export_backup_logs() { check_ajax_referer('tigerstyle_backup_nonce', 'nonce'); if (!current_user_can('manage_options')) { wp_die('Insufficient permissions'); } $start_date = sanitize_text_field($_GET['start_date'] ?? ''); $end_date = sanitize_text_field($_GET['end_date'] ?? ''); $level = sanitize_text_field($_GET['level'] ?? ''); $logger = new TigerStyleSEO_Backup_Logger(); $logs = $logger->export_logs($start_date, $end_date, $level); // Set headers for file download header('Content-Type: application/json'); header('Content-Disposition: attachment; filename="tigerstyle-backup-logs-' . date('Y-m-d') . '.json"'); header('Cache-Control: no-cache, must-revalidate'); header('Expires: 0'); echo json_encode($logs, JSON_PRETTY_PRINT); exit; } // AJAX handler for clearing old logs add_action('wp_ajax_tigerstyle_clear_backup_logs', 'tigerstyle_ajax_clear_backup_logs'); function tigerstyle_ajax_clear_backup_logs() { check_ajax_referer('tigerstyle_backup_nonce', 'nonce'); if (!current_user_can('manage_options')) { wp_send_json_error('Insufficient permissions'); } $days = absint($_POST['days'] ?? 30); $logger = new TigerStyleSEO_Backup_Logger(); $deleted_count = $logger->clear_old_logs($days); wp_send_json_success(array( 'message' => sprintf(__('Deleted %d old log entries', 'tigerstyle-heat'), $deleted_count), 'deleted_count' => $deleted_count )); } // AJAX handler for downloading backup add_action('wp_ajax_tigerstyle_download_backup', 'tigerstyle_ajax_download_backup'); function tigerstyle_ajax_download_backup() { check_ajax_referer('tigerstyle_backup_nonce', 'nonce'); if (!current_user_can('manage_options')) { wp_die('Insufficient permissions'); } $backup_id = sanitize_text_field($_GET['backup_id']); try { $storage_manager = new TigerStyleSEO_Storage_Manager(); $backup_file = $storage_manager->download_backup($backup_id); $backup_info = $storage_manager->get_backup_info($backup_id); // Determine filename $filename = $backup_id; if ($backup_info['storage_type'] === 'local') { $filename = basename($backup_info['file_path']); } else { $filename = basename($backup_info['s3_key']); } // Set headers for file download header('Content-Type: application/octet-stream'); header('Content-Disposition: attachment; filename="' . $filename . '"'); header('Content-Length: ' . filesize($backup_file)); header('Cache-Control: no-cache, must-revalidate'); header('Expires: 0'); // Output file readfile($backup_file); // Cleanup temporary file if needed if ($backup_info['storage_type'] === 's3' && strpos($backup_file, 'tigerstyle-temp') !== false) { unlink($backup_file); } exit; } catch (Exception $e) { wp_die('Download failed: ' . $e->getMessage()); } } // AJAX handler for upload progress add_action('wp_ajax_tigerstyle_upload_backup', 'tigerstyle_ajax_upload_backup'); function tigerstyle_ajax_upload_backup() { check_ajax_referer('tigerstyle_backup_nonce', 'nonce'); if (!current_user_can('manage_options')) { wp_send_json_error('Insufficient permissions'); } if (!isset($_FILES['backup_file'])) { wp_send_json_error('No file uploaded'); } $file = $_FILES['backup_file']; if ($file['error'] !== UPLOAD_ERR_OK) { wp_send_json_error('Upload error: ' . $file['error']); } // Validate file extension $allowed_extensions = array('zip', 'tar', 'gz', 'bz2'); $file_extension = strtolower(pathinfo($file['name'], PATHINFO_EXTENSION)); if (!in_array($file_extension, $allowed_extensions)) { wp_send_json_error('Invalid file type. Allowed: ' . implode(', ', $allowed_extensions)); } try { // Move uploaded file to backup directory $upload_dir = wp_upload_dir(); $backup_dir = $upload_dir['basedir'] . '/tigerstyle-backups'; if (!wp_mkdir_p($backup_dir)) { throw new Exception('Failed to create backup directory'); } $backup_id = 'uploaded_' . time() . '_' . wp_generate_password(8, false); $destination = $backup_dir . '/' . $backup_id . '.' . $file_extension; if (!move_uploaded_file($file['tmp_name'], $destination)) { throw new Exception('Failed to move uploaded file'); } // Store backup metadata $storage_manager = new TigerStyleSEO_Storage_Manager(); $metadata = array( 'backup_id' => $backup_id, 'storage_type' => 'local', 'file_path' => $destination, 'file_size' => filesize($destination), 'created_at' => current_time('mysql'), 'description' => 'Uploaded backup file' ); // This would need the storage manager to have a direct metadata storage method // For now, we'll just return success wp_send_json_success(array( 'backup_id' => $backup_id, 'message' => __('Backup file uploaded successfully', 'tigerstyle-heat') )); } catch (Exception $e) { wp_send_json_error($e->getMessage()); } } // AJAX handler for getting backup progress (for operations that don't set their own progress) add_action('wp_ajax_tigerstyle_backup_progress', 'tigerstyle_ajax_backup_progress'); function tigerstyle_ajax_backup_progress() { check_ajax_referer('tigerstyle_backup_nonce', 'nonce'); if (!current_user_can('manage_options')) { wp_send_json_error('Insufficient permissions'); } $operation_id = sanitize_text_field($_POST['operation_id']); // Check both backup and restore progress $progress = get_transient('tigerstyle_backup_progress_' . $operation_id); if (!$progress) { $progress = get_transient('tigerstyle_restore_progress_' . $operation_id); } if ($progress === false) { wp_send_json_error('Operation not found or completed'); } wp_send_json_success($progress); } // AJAX handler for validating backup add_action('wp_ajax_tigerstyle_validate_backup', 'tigerstyle_ajax_validate_backup'); function tigerstyle_ajax_validate_backup() { check_ajax_referer('tigerstyle_backup_nonce', 'nonce'); if (!current_user_can('manage_options')) { wp_send_json_error('Insufficient permissions'); } $backup_id = sanitize_text_field($_POST['backup_id']); try { $validator = new TigerStyleSEO_Backup_Validator(); $result = $validator->quick_validate_backup($backup_id); if ($result['valid']) { wp_send_json_success($result['message']); } else { wp_send_json_error($result['error']); } } catch (Exception $e) { wp_send_json_error($e->getMessage()); } } // AJAX handler for getting backup statistics add_action('wp_ajax_tigerstyle_backup_stats', 'tigerstyle_ajax_backup_stats'); function tigerstyle_ajax_backup_stats() { check_ajax_referer('tigerstyle_backup_nonce', 'nonce'); if (!current_user_can('manage_options')) { wp_send_json_error('Insufficient permissions'); } $days = absint($_POST['days'] ?? 30); $scheduler = new TigerStyleSEO_Backup_Scheduler(); $stats = $scheduler->get_backup_statistics($days); $storage_manager = new TigerStyleSEO_Storage_Manager(); $storage_stats = $storage_manager->get_storage_stats(); wp_send_json_success(array( 'backup_stats' => $stats, 'storage_stats' => $storage_stats )); } // AJAX handler for running manual backup add_action('wp_ajax_tigerstyle_manual_backup', 'tigerstyle_ajax_manual_backup'); function tigerstyle_ajax_manual_backup() { check_ajax_referer('tigerstyle_backup_nonce', 'nonce'); if (!current_user_can('manage_options')) { wp_send_json_error('Insufficient permissions'); } try { $scheduler = new TigerStyleSEO_Backup_Scheduler(); $backup_id = $scheduler->run_backup_now(); wp_send_json_success(array( 'backup_id' => $backup_id, 'message' => __('Manual backup started successfully', 'tigerstyle-heat') )); } catch (Exception $e) { wp_send_json_error($e->getMessage()); } } // Load this file when the backup module is loaded if (class_exists('TigerStyleSEO_Backup_Restore')) { // Already loaded above }