Ajax
1 year ago
BackgroundProcessing
1 year ago
Dto
1 year ago
Entity
1 year ago
Exceptions
1 year ago
Interfaces
1 year ago
Job
1 year ago
Request
2 years ago
Service
1 year ago
Storage
1 year ago
Task
1 year ago
AfterRestore.php
1 year ago
BackupDeleter.php
1 year ago
BackupDownload.php
1 year ago
BackupFileIndex.php
1 year ago
BackupHeader.php
1 year ago
BackupRepairer.php
1 year ago
BackupRetentionHandler.php
1 year ago
BackupScheduler.php
1 year ago
BackupServiceProvider.php
1 year ago
BackupValidator.php
1 year ago
FileHeader.php
1 year ago
FileHeaderAttribute.php
2 years ago
WithBackupIdentifier.php
1 year ago
BackupRetentionHandler.php
108 lines
| 1 | <?php |
| 2 | |
| 3 | namespace WPStaging\Backup; |
| 4 | |
| 5 | class BackupRetentionHandler |
| 6 | { |
| 7 | /** |
| 8 | * @var string |
| 9 | */ |
| 10 | const OPTION_BACKUPS_RETENTION = 'wpstg_backups_retention'; |
| 11 | |
| 12 | /** |
| 13 | * @var array |
| 14 | */ |
| 15 | protected $backupsRetention; |
| 16 | |
| 17 | /** |
| 18 | * @param array $backups |
| 19 | * @return bool |
| 20 | */ |
| 21 | public function updateBackupsRetentionOptions(array $backups): bool |
| 22 | { |
| 23 | return update_option(self::OPTION_BACKUPS_RETENTION, $backups); |
| 24 | } |
| 25 | |
| 26 | /** |
| 27 | * @param string|bool $storage |
| 28 | * |
| 29 | * @return array |
| 30 | * |
| 31 | * An array of arrays containing backup information: |
| 32 | * - 'backupId': An array with backup details. |
| 33 | * - 'createdDate': A string representing the date and time of backup creation. |
| 34 | * - 'storages': An array of storage types used for the backup. |
| 35 | * - 'backupSize': An integer representing the size of the backup. |
| 36 | * - 'isMultipart': A boolean indicating whether the backup is multipart. |
| 37 | */ |
| 38 | public function getBackupsRetention($storage = ''): array |
| 39 | { |
| 40 | if ($storage === false) { |
| 41 | return []; |
| 42 | } |
| 43 | |
| 44 | $backups = (array) get_option(self::OPTION_BACKUPS_RETENTION, []); |
| 45 | |
| 46 | if ($storage) { |
| 47 | $backups = array_filter($backups, function ($backup) use ($storage) { |
| 48 | return in_array($storage, $backup['storages'], true); |
| 49 | }); |
| 50 | } |
| 51 | |
| 52 | return $backups; |
| 53 | } |
| 54 | |
| 55 | /** |
| 56 | * @param string $backupId |
| 57 | * @param string $storageToRemove |
| 58 | * @return bool |
| 59 | */ |
| 60 | public function unsetStorageFromBackupsRetention(string $backupId, string $storageToRemove): bool |
| 61 | { |
| 62 | $this->backupsRetention = $this->getBackupsRetention(); |
| 63 | |
| 64 | if (!isset($this->backupsRetention[$backupId])) { |
| 65 | $backupId = $this->getBackupId($backupId); |
| 66 | } |
| 67 | |
| 68 | if (!isset($this->backupsRetention[$backupId])) { |
| 69 | return false; |
| 70 | } |
| 71 | |
| 72 | $currentBackup = $this->backupsRetention[$backupId]; |
| 73 | $storageToRemoveKey = array_search($storageToRemove, $currentBackup['storages'], true); |
| 74 | if ($storageToRemoveKey === false) { |
| 75 | return false; |
| 76 | } |
| 77 | |
| 78 | unset($currentBackup['storages'][$storageToRemoveKey]); |
| 79 | $this->backupsRetention[$backupId] = $currentBackup; |
| 80 | |
| 81 | // Don't hold backup without storage. |
| 82 | if (empty($currentBackup['storages'])) { |
| 83 | unset($this->backupsRetention[$backupId]); |
| 84 | } |
| 85 | |
| 86 | // Don't hold backup with only localStorage. |
| 87 | if (count($currentBackup['storages']) === 1 && reset($currentBackup['storages']) === 'localStorage') { |
| 88 | unset($this->backupsRetention[$backupId]); |
| 89 | } |
| 90 | |
| 91 | $this->updateBackupsRetentionOptions($this->backupsRetention); |
| 92 | |
| 93 | return true; |
| 94 | } |
| 95 | |
| 96 | private function getBackupId(string $backupName): string |
| 97 | { |
| 98 | $backupsRetention = $this->getBackupsRetention(); |
| 99 | foreach ($backupsRetention as $retainedBackupId => $retainedBackup) { |
| 100 | if (strpos($backupName, $retainedBackupId) !== false) { |
| 101 | return $retainedBackupId; |
| 102 | } |
| 103 | } |
| 104 | |
| 105 | return ''; |
| 106 | } |
| 107 | } |
| 108 |