import React, { useState, useEffect } from 'react'; import { __ } from '@wordpress/i18n'; import apiFetch from '@wordpress/api-fetch'; import { Card, Button, Alert, Badge, Toggle } from '../../../components/ui'; interface VersionSettings { auto_update: boolean; } interface BackupInfo { path: string; name: string; downloadUrl?: string; createdAt?: string; size?: number; } const CHANGELOG_URL = 'https://wordpress.org/plugins/prorank-seo/#developers'; interface VersionManagementTabProps { addNotice?: (type: string, message: string) => void; } const VersionManagementTab: React.FC = ({ addNotice }) => { const [currentVersion, setCurrentVersion] = useState(''); const [serviceMessage, setServiceMessage] = useState(''); const [settings, setSettings] = useState({ auto_update: false, }); const [isLoading, setIsLoading] = useState(true); const [isCreatingBackup, setIsCreatingBackup] = useState(false); const [isSavingSettings, setIsSavingSettings] = useState(false); const [error, setError] = useState(''); const [backupCreated, setBackupCreated] = useState(false); const [backupInfo, setBackupInfo] = useState(null); // Fetch versions and settings on mount useEffect(() => { fetchVersions(); fetchSettings(); }, []); const fetchVersions = async () => { try { const response = await apiFetch<{ success: boolean; current_version: string; message?: string; latest_backup?: { backup_name?: string; backup_path?: string; download_url?: string; created_at?: string; size?: number; } | null; }>({ path: '/prorank-seo/v1/versions', }); if (response.success) { setCurrentVersion(response.current_version || ''); setServiceMessage(response.message || ''); if (response.latest_backup?.backup_name || response.latest_backup?.backup_path) { setBackupInfo({ path: response.latest_backup.backup_path || '', name: response.latest_backup.backup_name || '', downloadUrl: response.latest_backup.download_url || '', createdAt: response.latest_backup.created_at || '', size: response.latest_backup.size || 0, }); } } else { setError(response.message || __('Failed to fetch versions', 'prorank-seo')); } } catch (err: any) { setError(err.message || __('Failed to fetch versions', 'prorank-seo')); } finally { setIsLoading(false); } }; const fetchSettings = async () => { try { const response = await apiFetch<{ success: boolean; settings: VersionSettings; }>({ path: '/prorank-seo/v1/versions/settings', }); if (response.success) { setSettings({ auto_update: Boolean(response.settings?.auto_update), }); } } catch (err) { console.error('Failed to fetch settings:', err); } }; const handleSaveSettings = async () => { setIsSavingSettings(true); try { const response = await apiFetch<{ success: boolean; message?: string; }>({ path: '/prorank-seo/v1/versions/settings', method: 'POST', data: settings, }); if (response.success) { addNotice?.('success', __('Settings saved successfully', 'prorank-seo')); } else { addNotice?.('error', response.message || __('Failed to save settings', 'prorank-seo')); } } catch (err: any) { addNotice?.('error', err.message || __('Failed to save settings', 'prorank-seo')); } finally { setIsSavingSettings(false); } }; const handleCreateBackup = async () => { setIsCreatingBackup(true); try { const response = await apiFetch<{ success: boolean; message?: string; backup_path?: string; backup_name?: string; download_url?: string; }>({ path: '/prorank-seo/v1/versions/backup', method: 'POST', }); if (response.success) { setBackupCreated(true); setBackupInfo({ path: response.backup_path || '', name: response.backup_name || (response.backup_path ? response.backup_path.split('/').pop() || '' : ''), downloadUrl: response.download_url || '', }); addNotice?.('success', __('Backup created successfully', 'prorank-seo')); } else { addNotice?.('error', response.message || __('Failed to create backup', 'prorank-seo')); } } catch (err: any) { addNotice?.('error', err.message || __('Failed to create backup', 'prorank-seo')); } finally { setIsCreatingBackup(false); } }; const handleDownloadBackup = () => { if (!backupInfo?.downloadUrl) { return; } window.location.assign(backupInfo.downloadUrl); }; if (isLoading) { return (

{__('Loading version information...', 'prorank-seo')}

); } return (
{/* Current Version */}

{__('Current Version', 'prorank-seo')}

{currentVersion}
{__('Installed', 'prorank-seo')}

{__('You are using the latest installed version of this free build', 'prorank-seo')}

{serviceMessage && ( {serviceMessage} )}
{/* Version History & Backup */}

{__('Backup & Restore Guidance', 'prorank-seo')}

{__( 'This free build uses the standard WordPress updater. Create a backup before manual updates or before installing another approved package.', 'prorank-seo' )} {error && ( {error} )}
{__('Recommended workflow', 'prorank-seo')}
  1. {__('Create a secure backup of the plugin files from this screen.', 'prorank-seo')}
  2. {__('Use the WordPress Plugins screen for normal updates.', 'prorank-seo')}
  3. {__('If you need to restore another approved build, replace the plugin package manually after backing up.', 'prorank-seo')}
{backupInfo?.downloadUrl && ( )}
{backupInfo && (
{__('Backup stored securely on the server.', 'prorank-seo')}
{backupInfo.name && (
{__('File:', 'prorank-seo')}{' '} {backupInfo.name}
)} {backupInfo.path && (
{__('Path:', 'prorank-seo')}{' '} {backupInfo.path}
)}
{backupInfo.downloadUrl ? __('Download it directly from this screen while logged into wp-admin, or use the server path if you need to retrieve it manually.', 'prorank-seo') : __('Retrieve it with your host file manager, SFTP, or SSH if you need the archive itself.', 'prorank-seo')}
)}
{/* Auto Update Settings */}

{__('Automatic Updates', 'prorank-seo')}

{__('This setting controls the standard WordPress auto-update behavior for the ProRank SEO plugin.', 'prorank-seo')}
{__('Auto Update Plugin', 'prorank-seo')}

{__('Automatically install stable updates when available', 'prorank-seo')}

setSettings({ ...settings, auto_update: checked })} />
); }; export default VersionManagementTab;