PluginProbe ʕ •ᴥ•ʔ
WP STAGING – WordPress Backup, Restore, Migration & Clone / 4.5.0
WP STAGING – WordPress Backup, Restore, Migration & Clone v4.5.0
4.9.1 4.9.0 4.8.1 trunk 3.0.0 3.0.1 3.0.2 3.0.3 3.0.4 3.0.5 3.0.6 3.1.0 3.1.1 3.1.2 3.1.3 3.1.4 3.10.0 3.2.0 3.3.1 3.3.2 3.3.3 3.4.1 3.4.3 3.5.0 3.6.0 3.7.1 3.8.0 3.8.1 3.8.2 3.8.3 3.8.4 3.8.5 3.8.6 3.8.7 3.9.0 3.9.1 3.9.2 3.9.3 3.9.4 4.0.0 4.1.0 4.1.1 4.1.2 4.1.3 4.1.4 4.2.0 4.2.1 4.3.0 4.3.1 4.3.2 4.4.0 4.5.0 4.6.0 4.7.0 4.7.1 4.7.2 4.7.3 4.8.0
wp-staging / Backup / BackupRetentionHandler.php
wp-staging / Backup Last commit date
Ajax 5 months ago BackgroundProcessing 1 year ago Dto 6 months ago Entity 5 months ago Exceptions 1 year ago Interfaces 6 months ago Job 5 months ago Request 1 year ago Service 5 months ago Storage 8 months ago Task 5 months ago Traits 10 months ago AfterRestore.php 5 months ago BackupDeleter.php 6 months ago BackupDownload.php 8 months ago BackupFileIndex.php 1 year ago BackupGlitchReason.php 1 year ago BackupHeader.php 8 months ago BackupRepairer.php 6 months ago BackupRetentionHandler.php 1 year ago BackupScheduler.php 5 months ago BackupServiceProvider.php 10 months ago BackupValidator.php 6 months ago FileHeader.php 8 months 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 if it is empty string('') all backups retention will be returned!
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