Ajax
2 years ago
BackgroundProcessing
2 years ago
Dto
1 year ago
Entity
2 years ago
Exceptions
2 years ago
Interfaces
2 years ago
Job
2 years ago
Request
2 years ago
Service
1 year ago
Storage
2 years ago
Task
1 year ago
AfterRestore.php
3 years ago
BackupDeleter.php
2 years ago
BackupDownload.php
2 years ago
BackupFileIndex.php
2 years ago
BackupHeader.php
2 years ago
BackupProcessLock.php
2 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
FileHeader.php
2 years ago
FileHeaderAttribute.php
2 years ago
WithBackupIdentifier.php
2 years ago
wpstgBackupHeader.txt
3 years ago
BackupDeleter.php
110 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 | !$metadata->getIsExportingOtherWpRootFiles() |
| 65 | ) { |
| 66 | $this->deleteBackup($backup, $metadata); |
| 67 | } |
| 68 | } |
| 69 | } |
| 70 | |
| 71 | /** |
| 72 | * @param FileObject $backup |
| 73 | * @param BackupMetadata $metadata |
| 74 | */ |
| 75 | public function deleteBackup($backup, $metadata = null) |
| 76 | { |
| 77 | $additionalLog = ''; |
| 78 | if ($this->deletingAutomatedDatabaseOnlyBackup) { |
| 79 | $additionalLog = 'database-only automated'; |
| 80 | } |
| 81 | |
| 82 | if ($metadata === null) { |
| 83 | $metadata = $this->backupMetadata->hydrateByFilePath($backup->getRealPath()); |
| 84 | } |
| 85 | |
| 86 | if (!$metadata->getIsMultipartBackup()) { |
| 87 | $deleted = unlink($backup->getRealPath()); |
| 88 | if (!$deleted) { |
| 89 | $this->errors[] = sprintf(__('Unable to delete %s backup: %s', 'wp-staging'), $additionalLog, $backup->getFilename()); |
| 90 | } |
| 91 | |
| 92 | return; |
| 93 | } |
| 94 | |
| 95 | $backupDirectory = $this->backupsFinder->getBackupsDirectory(); |
| 96 | foreach ($metadata->getMultipartMetadata()->getBackupParts() as $part) { |
| 97 | $partPath = $backupDirectory . $part; |
| 98 | if (!file_exists($partPath)) { |
| 99 | continue; |
| 100 | } |
| 101 | |
| 102 | $partName = str_replace(dirname($partPath), '', $partPath); |
| 103 | $deleted = unlink($partPath); |
| 104 | if (!$deleted) { |
| 105 | $this->errors[] = sprintf(__('Unable to delete %s split backup part: %s', 'wp-staging'), $additionalLog, $partName); |
| 106 | } |
| 107 | } |
| 108 | } |
| 109 | } |
| 110 |