PluginProbe ʕ •ᴥ•ʔ
WP STAGING – WordPress Backups, Restore, Migration & Clone / 4.11.1
WP STAGING – WordPress Backups, Restore, Migration & Clone v4.11.1
4.11.1 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 / BackupDeleter.php
wp-staging / Backup Last commit date
Ajax 6 days ago BackgroundProcessing 1 week ago Dto 6 days ago Entity 1 week ago Exceptions 1 week ago FileHeader 1 week ago Interfaces 1 week ago Job 6 days ago Request 1 week ago Service 6 days ago Storage 1 week ago Task 6 days ago Traits 1 week ago Utils 1 week ago AfterRestore.php 1 week ago BackupDeleter.php 1 week ago BackupDownload.php 1 week ago BackupFileIndex.php 1 week ago BackupGlitchReason.php 1 week ago BackupHeader.php 6 days ago BackupNextOffer.php 1 week ago BackupRepairer.php 1 week ago BackupRetentionHandler.php 1 week ago BackupScheduler.php 6 days ago BackupServiceProvider.php 1 week ago BackupValidator.php 6 days ago BeforeUpdateRowStatus.php 1 week ago FileHeader.php 1 week ago FileHeaderAttribute.php 1 week ago UpdateProtectionPausedNotice.php 1 week ago WithBackupIdentifier.php 1 week ago
BackupDeleter.php
173 lines
1 <?php
2
3 namespace WPStaging\Backup;
4
5 use SplFileInfo;
6 use WPStaging\Backup\Entity\BackupMetadata;
7 use WPStaging\Backup\Service\BackupsFinder;
8 use WPStaging\Backup\Utils\BackupPathResolver;
9
10 class BackupDeleter
11 {
12
13 protected $backupsFinder;
14
15
16 protected $backupMetadata;
17
18
19 protected $backupPathResolver;
20
21
22 protected $errors = [];
23
24
25 protected $deletingAutomatedDatabaseOnlyBackup = false;
26
27 public function __construct(BackupsFinder $backupsFinder, BackupMetadata $backupMetadata, BackupPathResolver $backupPathResolver)
28 {
29 $this->backupsFinder = $backupsFinder;
30 $this->backupMetadata = $backupMetadata;
31 $this->backupPathResolver = $backupPathResolver;
32 }
33
34
35 public function getErrors()
36 {
37 return $this->errors;
38 }
39
40 public function clearErrors()
41 {
42 $this->errors = [];
43 $this->deletingAutomatedDatabaseOnlyBackup = false;
44 }
45
46
47
48
49
50 public function deleteAllBackups()
51 {
52 $this->clearErrors();
53
54 foreach ($this->backupsFinder->findBackups() as $backup) {
55 $this->deleteBackup($backup);
56 }
57 }
58
59 public function deleteAllAutomatedDbOnlyBackups()
60 {
61 $this->clearErrors();
62 $this->deletingAutomatedDatabaseOnlyBackup = true;
63
64 foreach ($this->backupsFinder->findBackups() as $backup) {
65 $metadata = $this->backupMetadata->hydrateByFilePath($backup->getRealPath());
66 if (
67 $metadata->getIsAutomatedBackup() &&
68 $metadata->getIsExportingDatabase() &&
69 !$metadata->getIsExportingMuPlugins() &&
70 !$metadata->getIsExportingPlugins() &&
71 !$metadata->getIsExportingThemes() &&
72 !$metadata->getIsExportingUploads() &&
73 !$metadata->getIsExportingOtherWpContentFiles() &&
74 !$metadata->getIsExportingOtherWpRootFiles()
75 ) {
76 $this->deleteBackup($backup, $metadata);
77 }
78 }
79 }
80
81
82
83
84
85 public function deleteAllAutomatedUploadsOnlyBackups()
86 {
87 $this->clearErrors();
88
89 foreach ($this->backupsFinder->findBackups() as $backup) {
90 $metadata = $this->backupMetadata->hydrateByFilePath($backup->getRealPath());
91 if (
92 $metadata->getIsAutomatedBackup() &&
93 $metadata->getIsExportingUploads() &&
94 !$metadata->getIsExportingDatabase() &&
95 !$metadata->getIsExportingMuPlugins() &&
96 !$metadata->getIsExportingPlugins() &&
97 !$metadata->getIsExportingThemes() &&
98 !$metadata->getIsExportingOtherWpContentFiles() &&
99 !$metadata->getIsExportingOtherWpRootFiles()
100 ) {
101 $this->deleteBackup($backup, $metadata);
102 }
103 }
104 }
105
106
107
108
109
110 public function deleteAllAutomatedPushBackups()
111 {
112 $this->clearErrors();
113
114 foreach ($this->backupsFinder->findBackups() as $backup) {
115 $metadata = $this->backupMetadata->hydrateByFilePath($backup->getRealPath());
116 if (
117 $metadata->getIsAutomatedBackup() &&
118 $metadata->getIsExportingDatabase() &&
119 $metadata->getIsExportingUploads() &&
120 !$metadata->getIsExportingMuPlugins() &&
121 !$metadata->getIsExportingPlugins() &&
122 !$metadata->getIsExportingThemes() &&
123 !$metadata->getIsExportingOtherWpContentFiles() &&
124 !$metadata->getIsExportingOtherWpRootFiles()
125 ) {
126 $this->deleteBackup($backup, $metadata);
127 }
128 }
129 }
130
131
132
133
134
135 public function deleteBackup($backup, $metadata = null)
136 {
137 $additionalLog = '';
138 if ($this->deletingAutomatedDatabaseOnlyBackup) {
139 $additionalLog = 'database-only automated';
140 }
141
142 if ($metadata === null) {
143 $metadata = $this->backupMetadata->hydrateByFilePath($backup->getRealPath());
144 }
145
146 if (!$metadata->getIsMultipartBackup()) {
147 $deleted = unlink($backup->getRealPath());
148 if (!$deleted) {
149 $this->errors[] = sprintf(__('Unable to delete %s backup: %s', 'wp-staging'), $additionalLog, $backup->getFilename());
150 }
151
152 return;
153 }
154
155 foreach ($metadata->getMultipartMetadata()->getBackupParts() as $part) {
156 $partPath = $this->backupPathResolver->resolveBackupPartPath($part, $backup->getFilename());
157 if ($partPath === '') {
158 $this->errors[] = sprintf(__('Skipped a backup part that does not belong to this backup: %s', 'wp-staging'), esc_html($part));
159 continue;
160 }
161
162 if (!file_exists($partPath)) {
163 continue;
164 }
165
166 $deleted = unlink($partPath);
167 if (!$deleted) {
168 $this->errors[] = sprintf(__('Unable to delete %s split backup part: %s', 'wp-staging'), $additionalLog, wp_basename($partPath));
169 }
170 }
171 }
172 }
173