PluginProbe ʕ •ᴥ•ʔ
WP STAGING – WordPress Backup, Restore, Migration & Clone / 4.9.2
WP STAGING – WordPress Backup, Restore, Migration & Clone v4.9.2
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 / Ajax / Delete.php
wp-staging / Backup / Ajax Last commit date
Backup 2 months ago FileList 4 months ago Restore 2 months ago Backup.php 2 years ago BackupDownloader.php 1 day ago BackupSizeCalculator.php 1 day ago BackupSpeedIndex.php 9 months ago BaseFileList.php 1 year ago BaseListing.php 9 months ago Delete.php 4 months ago Edit.php 4 months ago Explore.php 3 months ago ExploreCache.php 3 months ago FileInfo.php 9 months ago FileList.php 1 year ago Listing.php 7 months ago Parts.php 1 day ago Restore.php 2 years ago ScheduleList.php 1 year ago Upload.php 1 month ago
Delete.php
146 lines
1 <?php
2
3 namespace WPStaging\Backup\Ajax;
4
5 use Exception;
6 use SplFileInfo;
7 use WPStaging\Backup\Entity\BackupMetadata;
8 use WPStaging\Backup\Exceptions\BackupRuntimeException;
9 use WPStaging\Backup\Service\BackupsFinder;
10 use WPStaging\Core\WPStaging;
11 use WPStaging\Framework\Component\AbstractTemplateComponent;
12 use WPStaging\Framework\Filesystem\FileObject;
13 use WPStaging\Framework\TemplateEngine\TemplateEngine;
14 use WPStaging\Framework\Utils\Cache\TransientCache;
15
16 use function WPStaging\functions\debug_log;
17
18 class Delete extends AbstractTemplateComponent
19 {
20 /** @var BackupsFinder */
21 private $backupsFinder;
22
23 public function __construct(BackupsFinder $backupsFinder, TemplateEngine $templateEngine)
24 {
25 parent::__construct($templateEngine);
26 $this->backupsFinder = $backupsFinder;
27 }
28
29 public function render()
30 {
31 if (!$this->canRenderAjax()) {
32 return;
33 }
34
35 $md5 = isset($_POST['md5']) ? sanitize_text_field($_POST['md5']) : '';
36
37 if (strlen($md5) !== 32) {
38 wp_send_json([
39 'error' => true,
40 'message' => __('Invalid request.', 'wp-staging'),
41 ]);
42 }
43
44 $backups = $this->backupsFinder->findBackups();
45
46 // Early bail: No backups found, nothing to delete
47 if (empty($backups)) {
48 wp_send_json([
49 'error' => true,
50 'message' => __('No backups found, nothing to delete.', 'wp-staging'),
51 ]);
52 }
53
54 foreach ($backups as $backup) {
55 if ($md5 === md5($backup->getBasename())) {
56 $this->deleteBackup($backup);
57 }
58 }
59 }
60
61 /**
62 * @param SplFileInfo $backup
63 * @throws BackupRuntimeException
64 */
65 protected function deleteBackup($backup)
66 {
67 if (!$this->deleteSplitBackupParts($backup)) {
68 return;
69 }
70
71 $deleted = unlink($backup->getRealPath());
72
73 if ($deleted) {
74 delete_transient(TransientCache::KEY_INVALID_BACKUP_FILE_INDEX);
75 wp_send_json([
76 'error' => false,
77 'message' => __('Successfully deleted the backup.', 'wp-staging'),
78 ]);
79 } else {
80 debug_log('WP STAGING: User tried to delete backup but "unlink" returned false. Backup that couldn\'t be deleted: ' . $backup->getRealPath());
81
82 wp_send_json([
83 'error' => true,
84 'message' => __('Could not delete the backup. Maybe a permission issue?', 'wp-staging'),
85 ]);
86 }
87 }
88
89 /**
90 * @param SplFileInfo $backup
91 * @return bool
92 * @throws BackupRuntimeException
93 */
94 protected function deleteSplitBackupParts($backup)
95 {
96 clearstatcache();
97
98 try {
99 $file = new FileObject($backup->getRealPath(), FileObject::MODE_APPEND_AND_READ);
100 $backupMetadata = new BackupMetadata();
101 $backupMetadata = $backupMetadata->hydrateByFile($file);
102 } catch (Exception $e) {
103 // Couldn't read backup metadata, continue deleting the main file but log error
104 debug_log('WP STAGING: User tried to delete backup but "unlink" returned false on deleting backup parts. Backup that couldn\'t be deleted: ' . $backup->getRealPath());
105
106 return true;
107 }
108
109 // Early bail: Not a split backup
110 if (!$backupMetadata->getIsMultipartBackup()) {
111 return true;
112 }
113
114 $errors = [];
115
116 $backupsDirectory = WPStaging::make(BackupsFinder::class)->getBackupsDirectory();
117
118 foreach ($backupMetadata->getMultipartMetadata()->getBackupParts() as $part) {
119 $backupPart = $backupsDirectory . $part;
120 if (!file_exists($backupPart)) {
121 continue;
122 }
123
124 $deleted = unlink($backupPart);
125 if (!$deleted) {
126 $error = "Couldn't delete backup part. Maybe Permission Issue? Part: " . $backupPart;
127 debug_log('WP STAGING: ' . $error);
128
129 $errors[] = $error;
130 }
131 }
132
133 if (count($errors) === 0) {
134 return false;
135 }
136
137 wp_send_json([
138 'error' => true,
139 'message' => '',
140 'messages' => $errors,
141 ]);
142
143 return false;
144 }
145 }
146