Ajax
2 years ago
BackgroundProcessing
2 years ago
Dto
2 years ago
Entity
2 years ago
Exceptions
2 years ago
Interfaces
2 years ago
Job
2 years ago
Request
2 years ago
Service
2 years ago
Storage
2 years ago
Task
2 years ago
AfterRestore.php
3 years ago
BackupDeleter.php
3 years ago
BackupDownload.php
2 years ago
BackupFileIndex.php
2 years ago
BackupHeader.php
2 years ago
BackupProcessLock.php
2 years ago
BackupRepairer.php
3 years ago
BackupRetentionHandler.php
2 years ago
BackupScheduler.php
2 years ago
BackupServiceProvider.php
2 years ago
BackupValidator.php
2 years ago
FileHeader.php
2 years ago
FileHeaderAttribute.php
2 years ago
WithBackupIdentifier.php
2 years ago
wpstgBackupHeader.txt
3 years ago
BackupRetentionHandler.php
92 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 | return false; |
| 66 | } |
| 67 | |
| 68 | $currentBackup = $this->backupsRetention[$backupId]; |
| 69 | $storageToRemoveKey = array_search($storageToRemove, $currentBackup['storages'], true); |
| 70 | if ($storageToRemoveKey === false) { |
| 71 | return false; |
| 72 | } |
| 73 | |
| 74 | unset($currentBackup['storages'][$storageToRemoveKey]); |
| 75 | $this->backupsRetention[$backupId] = $currentBackup; |
| 76 | |
| 77 | // Don't hold backup without storage. |
| 78 | if (empty($currentBackup['storages'])) { |
| 79 | unset($this->backupsRetention[$backupId]); |
| 80 | } |
| 81 | |
| 82 | // Don't hold backup with only localStorage. |
| 83 | if (count($currentBackup['storages']) === 1 && reset($currentBackup['storages']) === 'localStorage') { |
| 84 | unset($this->backupsRetention[$backupId]); |
| 85 | } |
| 86 | |
| 87 | $this->updateBackupsRetentionOptions($this->backupsRetention); |
| 88 | |
| 89 | return true; |
| 90 | } |
| 91 | } |
| 92 |