PluginProbe ʕ •ᴥ•ʔ
WP STAGING – WordPress Backups, Restore, Migration & Clone / trunk
WP STAGING – WordPress Backups, Restore, Migration & Clone vtrunk
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 2 days ago BackgroundProcessing 2 days ago Dto 2 days ago Entity 2 days ago Exceptions 2 days ago FileHeader 2 days ago Interfaces 2 days ago Job 2 days ago Request 2 days ago Service 2 days ago Storage 2 days ago Task 2 days ago Traits 2 days ago Utils 2 days ago AfterRestore.php 2 days ago BackupDeleter.php 2 days ago BackupDownload.php 2 days ago BackupFileIndex.php 2 days ago BackupGlitchReason.php 2 days ago BackupHeader.php 2 days ago BackupNextOffer.php 2 days ago BackupRepairer.php 2 days ago BackupRetentionHandler.php 2 days ago BackupScheduler.php 2 days ago BackupServiceProvider.php 2 days ago BackupValidator.php 2 days ago BeforeUpdateRowStatus.php 2 days ago FileHeader.php 2 days ago FileHeaderAttribute.php 2 days ago UpdateProtectionPausedNotice.php 2 days ago WithBackupIdentifier.php 2 days 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