PluginProbe ʕ •ᴥ•ʔ
WP STAGING – WordPress Backups, Restore, Migration & Clone / 4.11.0
WP STAGING – WordPress Backups, Restore, Migration & Clone v4.11.0
4.11.0 4.10.0 4.9.5 4.9.4 4.9.3 4.9.2 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 1 day ago BackgroundProcessing 1 day ago Dto 1 day ago Entity 1 day ago Exceptions 1 day ago FileHeader 1 day ago Interfaces 1 day ago Job 1 day ago Request 1 day ago Service 1 day ago Storage 1 day ago Task 1 day ago Traits 1 day ago Utils 1 day ago AfterRestore.php 1 day ago BackupDeleter.php 1 day ago BackupDownload.php 1 day ago BackupFileIndex.php 1 day ago BackupGlitchReason.php 1 day ago BackupHeader.php 1 day ago BackupNextOffer.php 1 day ago BackupRepairer.php 1 day ago BackupRetentionHandler.php 1 day ago BackupScheduler.php 1 day ago BackupServiceProvider.php 1 day ago BackupValidator.php 1 day ago BeforeUpdateRowStatus.php 1 day ago FileHeader.php 1 day ago FileHeaderAttribute.php 1 day ago UpdateProtectionPausedNotice.php 1 day ago WithBackupIdentifier.php 1 day 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
13
14 const OPTION_BACKUPS_RETENTION = 'wpstg_backups_retention';
15
16
17
18
19 protected $backupsRetention;
20
21
22
23
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
33
34
35
36
37
38
39
40
41
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
54 if ($backups !== $originalBackups) {
55 update_option(self::OPTION_BACKUPS_RETENTION, $backups);
56 }
57
58
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
72
73
74
75 public function unsetStorageFromBackupsRetention(string $backupId, string $storageToRemove): bool
76 {
77 $this->backupsRetention = $this->getBackupsRetention();
78
79
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
100 if (empty($currentBackup['storages'])) {
101 unset($this->backupsRetention[$backupId]);
102 }
103
104
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
116
117
118
119
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