Ajax
1 week ago
BackgroundProcessing
1 year ago
Dto
1 month ago
Entity
2 weeks ago
Exceptions
1 year ago
FileHeader
2 months ago
Interfaces
8 months ago
Job
3 months ago
Request
1 year ago
Service
1 month ago
Storage
2 months ago
Task
1 week ago
Traits
11 months ago
Utils
1 week ago
AfterRestore.php
6 months ago
BackupDeleter.php
1 week ago
BackupDownload.php
10 months ago
BackupFileIndex.php
1 year ago
BackupGlitchReason.php
1 year ago
BackupHeader.php
2 months ago
BackupRepairer.php
8 months ago
BackupRetentionHandler.php
2 months ago
BackupScheduler.php
1 month ago
BackupServiceProvider.php
3 months ago
BackupValidator.php
1 week ago
FileHeader.php
1 month ago
FileHeaderAttribute.php
2 years 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 | /** @var BackupsFinder */ |
| 13 | protected $backupsFinder; |
| 14 | |
| 15 | /** @var BackupMetadata */ |
| 16 | protected $backupMetadata; |
| 17 | |
| 18 | /** @var BackupPathResolver */ |
| 19 | protected $backupPathResolver; |
| 20 | |
| 21 | /** @var array */ |
| 22 | protected $errors = []; |
| 23 | |
| 24 | /** @var bool */ |
| 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 | /** @return array */ |
| 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 | * Used by unit test tests/wpunit/Backup/BackupSchedulerTest.php |
| 48 | * @return void |
| 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 | * Delete all automated uploads-only backups |
| 83 | * @return void |
| 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 | * Delete all automated push backups (database + uploads only) |
| 108 | * @return void |
| 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 | * @param SplFileInfo $backup |
| 133 | * @param BackupMetadata $metadata |
| 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 |