PluginProbe ʕ •ᴥ•ʔ
WP STAGING – WordPress Backup, Restore, Migration & Clone / 3.3.1
WP STAGING – WordPress Backup, Restore, Migration & Clone v3.3.1
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 2 years ago BackgroundProcessing 2 years ago Dto 2 years ago Entity 2 years ago Exceptions 3 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 3 years ago BackupProcessLock.php 3 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 WithBackupIdentifier.php 2 years ago wpstgBackupHeader.txt 3 years ago
BackupDeleter.php
109 lines
1 <?php
2
3 namespace WPStaging\Backup;
4
5 use WPStaging\Framework\Filesystem\FileObject;
6 use WPStaging\Backup\Entity\BackupMetadata;
7 use WPStaging\Backup\Service\BackupsFinder;
8
9 class BackupDeleter
10 {
11 protected $backupsFinder;
12 protected $backupMetadata;
13
14 protected $errors = [];
15
16 protected $deletingAutomatedDatabaseOnlyBackup = false;
17
18 public function __construct(BackupsFinder $backupsFinder, BackupMetadata $backupMetadata)
19 {
20 $this->backupsFinder = $backupsFinder;
21 $this->backupMetadata = $backupMetadata;
22 }
23
24 /** @return array */
25 public function getErrors()
26 {
27 return $this->errors;
28 }
29
30 public function clearErrors()
31 {
32 $this->errors = [];
33 $this->deletingAutomatedDatabaseOnlyBackup = false;
34 }
35
36 /**
37 * Used by unit test tests/wpunit/Backup/BackupSchedulerTest.php
38 * @return void
39 */
40 public function deleteAllBackups()
41 {
42 $this->clearErrors();
43
44 foreach ($this->backupsFinder->findBackups() as $backup) {
45 $this->deleteBackup($backup);
46 }
47 }
48
49 public function deleteAllAutomatedDbOnlyBackups()
50 {
51 $this->clearErrors();
52 $this->deletingAutomatedDatabaseOnlyBackup = true;
53
54 foreach ($this->backupsFinder->findBackups() as $backup) {
55 $metadata = $this->backupMetadata->hydrateByFilePath($backup->getRealPath());
56 if (
57 $metadata->getIsAutomatedBackup() &&
58 $metadata->getIsExportingDatabase() &&
59 !$metadata->getIsExportingMuPlugins() &&
60 !$metadata->getIsExportingPlugins() &&
61 !$metadata->getIsExportingThemes() &&
62 !$metadata->getIsExportingUploads() &&
63 !$metadata->getIsExportingOtherWpContentFiles()
64 ) {
65 $this->deleteBackup($backup, $metadata);
66 }
67 }
68 }
69
70 /**
71 * @param FileObject $backup
72 * @param BackupMetadata $metadata
73 */
74 public function deleteBackup($backup, $metadata = null)
75 {
76 $additionalLog = '';
77 if ($this->deletingAutomatedDatabaseOnlyBackup) {
78 $additionalLog = 'database-only automated';
79 }
80
81 if ($metadata === null) {
82 $metadata = $this->backupMetadata->hydrateByFilePath($backup->getRealPath());
83 }
84
85 if (!$metadata->getIsMultipartBackup()) {
86 $deleted = unlink($backup->getRealPath());
87 if (!$deleted) {
88 $this->errors[] = sprintf(__('Unable to delete %s backup: %s', 'wp-staging'), $additionalLog, $backup->getFilename());
89 }
90
91 return;
92 }
93
94 $backupDirectory = $this->backupsFinder->getBackupsDirectory();
95 foreach ($metadata->getMultipartMetadata()->getBackupParts() as $part) {
96 $partPath = $backupDirectory . $part;
97 if (!file_exists($partPath)) {
98 continue;
99 }
100
101 $partName = str_replace(dirname($partPath), '', $partPath);
102 $deleted = unlink($partPath);
103 if (!$deleted) {
104 $this->errors[] = sprintf(__('Unable to delete %s split backup part: %s', 'wp-staging'), $additionalLog, $partName);
105 }
106 }
107 }
108 }
109