Ajax
1 month ago
BackgroundProcessing
1 year ago
Dto
1 week ago
Entity
1 week ago
Exceptions
1 year ago
FileHeader
1 month ago
Interfaces
6 months ago
Job
1 month ago
Request
1 year ago
Service
1 week ago
Storage
1 month ago
Task
1 week ago
Traits
10 months ago
Utils
3 months ago
AfterRestore.php
5 months ago
BackupDeleter.php
2 months ago
BackupDownload.php
8 months ago
BackupFileIndex.php
1 year ago
BackupGlitchReason.php
1 year ago
BackupHeader.php
1 month ago
BackupRepairer.php
6 months ago
BackupRetentionHandler.php
1 month ago
BackupScheduler.php
1 week ago
BackupServiceProvider.php
1 month ago
BackupValidator.php
6 months ago
FileHeader.php
1 week ago
FileHeaderAttribute.php
2 years ago
WithBackupIdentifier.php
1 year ago
BackupRetentionHandler.php
150 lines
| 1 | <?php |
| 2 | |
| 3 | namespace WPStaging\Backup; |
| 4 | |
| 5 | use WPStaging\Backup\Storage\Traits\StorageIdNormalizerTrait; |
| 6 | |
| 7 | class BackupRetentionHandler |
| 8 | { |
| 9 | use StorageIdNormalizerTrait; |
| 10 | |
| 11 | /** |
| 12 | * @var string |
| 13 | */ |
| 14 | const OPTION_BACKUPS_RETENTION = 'wpstg_backups_retention'; |
| 15 | |
| 16 | /** |
| 17 | * @var array |
| 18 | */ |
| 19 | protected $backupsRetention; |
| 20 | |
| 21 | /** |
| 22 | * @param array $backups |
| 23 | * @return bool |
| 24 | */ |
| 25 | public function updateBackupsRetentionOptions(array $backups): bool |
| 26 | { |
| 27 | $backups = $this->normalizeStorageIds($backups); |
| 28 | return update_option(self::OPTION_BACKUPS_RETENTION, $backups); |
| 29 | } |
| 30 | |
| 31 | /** |
| 32 | * @param string|bool $storage if it is empty string('') all backups retention will be returned! |
| 33 | * |
| 34 | * @return array |
| 35 | * |
| 36 | * An array of arrays containing backup information: |
| 37 | * - 'backupId': An array with backup details. |
| 38 | * - 'createdDate': A string representing the date and time of backup creation. |
| 39 | * - 'storages': An array of storage types used for the backup. |
| 40 | * - 'backupSize': An integer representing the size of the backup. |
| 41 | * - 'isMultipart': A boolean indicating whether the backup is multipart. |
| 42 | */ |
| 43 | public function getBackupsRetention($storage = ''): array |
| 44 | { |
| 45 | if ($storage === false) { |
| 46 | return []; |
| 47 | } |
| 48 | |
| 49 | $backups = (array) get_option(self::OPTION_BACKUPS_RETENTION, []); |
| 50 | $originalBackups = $backups; |
| 51 | $backups = $this->normalizeStorageIds($backups); |
| 52 | |
| 53 | // Persist normalized storage IDs for future lookups |
| 54 | if ($backups !== $originalBackups) { |
| 55 | update_option(self::OPTION_BACKUPS_RETENTION, $backups); |
| 56 | } |
| 57 | |
| 58 | // Normalize the storage parameter as well |
| 59 | $storage = $this->normalizeStorageId($storage); |
| 60 | |
| 61 | if ($storage) { |
| 62 | $backups = array_filter($backups, function ($backup) use ($storage) { |
| 63 | return in_array($storage, $backup['storages'], true); |
| 64 | }); |
| 65 | } |
| 66 | |
| 67 | return $backups; |
| 68 | } |
| 69 | |
| 70 | /** |
| 71 | * @param string $backupId |
| 72 | * @param string $storageToRemove |
| 73 | * @return bool |
| 74 | */ |
| 75 | public function unsetStorageFromBackupsRetention(string $backupId, string $storageToRemove): bool |
| 76 | { |
| 77 | $this->backupsRetention = $this->getBackupsRetention(); |
| 78 | |
| 79 | // Normalize the storage to remove |
| 80 | $storageToRemove = $this->normalizeStorageId($storageToRemove); |
| 81 | |
| 82 | if (!isset($this->backupsRetention[$backupId])) { |
| 83 | $backupId = $this->getBackupId($backupId); |
| 84 | } |
| 85 | |
| 86 | if (!isset($this->backupsRetention[$backupId])) { |
| 87 | return false; |
| 88 | } |
| 89 | |
| 90 | $currentBackup = $this->backupsRetention[$backupId]; |
| 91 | $storageToRemoveKey = array_search($storageToRemove, $currentBackup['storages'], true); |
| 92 | if ($storageToRemoveKey === false) { |
| 93 | return false; |
| 94 | } |
| 95 | |
| 96 | unset($currentBackup['storages'][$storageToRemoveKey]); |
| 97 | $this->backupsRetention[$backupId] = $currentBackup; |
| 98 | |
| 99 | // Don't hold backup without storage. |
| 100 | if (empty($currentBackup['storages'])) { |
| 101 | unset($this->backupsRetention[$backupId]); |
| 102 | } |
| 103 | |
| 104 | // Don't hold backup with only localStorage. |
| 105 | if (count($currentBackup['storages']) === 1 && reset($currentBackup['storages']) === 'localStorage') { |
| 106 | unset($this->backupsRetention[$backupId]); |
| 107 | } |
| 108 | |
| 109 | $this->updateBackupsRetentionOptions($this->backupsRetention); |
| 110 | |
| 111 | return true; |
| 112 | } |
| 113 | |
| 114 | /** |
| 115 | * Normalize storage IDs in backups array for backward compatibility |
| 116 | * Converts legacy storage IDs to new hyphenated identifiers (e.g. googleDrive -> google-drive) |
| 117 | * |
| 118 | * @param array $backups |
| 119 | * @return array |
| 120 | */ |
| 121 | private function normalizeStorageIds(array $backups): array |
| 122 | { |
| 123 | foreach ($backups as $backupId => &$backup) { |
| 124 | if (!isset($backup['storages']) || !is_array($backup['storages'])) { |
| 125 | continue; |
| 126 | } |
| 127 | |
| 128 | $backup['storages'] = array_unique( |
| 129 | array_map([$this, 'normalizeStorageId'], $backup['storages']) |
| 130 | ); |
| 131 | } |
| 132 | |
| 133 | unset($backup); |
| 134 | |
| 135 | return $backups; |
| 136 | } |
| 137 | |
| 138 | private function getBackupId(string $backupName): string |
| 139 | { |
| 140 | $backupsRetention = $this->getBackupsRetention(); |
| 141 | foreach ($backupsRetention as $retainedBackupId => $retainedBackup) { |
| 142 | if (strpos($backupName, $retainedBackupId) !== false) { |
| 143 | return $retainedBackupId; |
| 144 | } |
| 145 | } |
| 146 | |
| 147 | return ''; |
| 148 | } |
| 149 | } |
| 150 |