PluginProbe ʕ •ᴥ•ʔ
WP STAGING – WordPress Backup, Restore, Migration & Clone / 3.8.2
WP STAGING – WordPress Backup, Restore, Migration & Clone v3.8.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 / Task / Tasks / CleanupTmpFilesTask.php
wp-staging / Backup / Task / Tasks Last commit date
JobBackup 1 year ago JobRestore 1 year ago CleanupBakTablesTask.php 2 years ago CleanupTmpFilesTask.php 2 years ago CleanupTmpTablesTask.php 2 years ago
CleanupTmpFilesTask.php
190 lines
1 <?php
2
3 namespace WPStaging\Backup\Task\Tasks;
4
5 use Throwable;
6 use WPStaging\Backup\Dto\Job\JobRestoreDataDto;
7 use WPStaging\Framework\Adapter\Directory;
8 use WPStaging\Framework\Filesystem\FilesystemExceptions;
9 use WPStaging\Framework\Filesystem\PathIdentifier;
10 use WPStaging\Framework\Queue\SeekableQueueInterface;
11 use WPStaging\Backup\Dto\StepsDto;
12 use WPStaging\Backup\Dto\TaskResponseDto;
13 use WPStaging\Backup\Task\AbstractTask;
14 use WPStaging\Backup\Task\RestoreTask;
15 use WPStaging\Vendor\Psr\Log\LoggerInterface;
16 use WPStaging\Framework\Utils\Cache\Cache;
17 use WPStaging\Framework\Filesystem\Filesystem;
18
19 class CleanupTmpFilesTask extends AbstractTask
20 {
21 /** @var Filesystem */
22 private $filesystem;
23
24 /** @var Directory */
25 private $directory;
26
27 /** @var PathIdentifier */
28 private $pathIdentifier;
29
30 /**
31 * @param LoggerInterface $logger
32 * @param Cache $cache
33 * @param StepsDto $stepsDto
34 * @param Filesystem $filesystem
35 * @param Directory $directory
36 * @param SeekableQueueInterface $taskQueue
37 * @param PathIdentifier $pathIdentifier
38 */
39 public function __construct(LoggerInterface $logger, Cache $cache, StepsDto $stepsDto, Filesystem $filesystem, Directory $directory, SeekableQueueInterface $taskQueue, PathIdentifier $pathIdentifier)
40 {
41 parent::__construct($logger, $cache, $stepsDto, $taskQueue);
42 $this->filesystem = $filesystem;
43 $this->directory = $directory;
44 $this->pathIdentifier = $pathIdentifier;
45 }
46
47 /**
48 * @example 'backup_site_restore_themes'
49 * @return string
50 */
51 public static function getTaskName(): string
52 {
53 return 'backup_restore_cleanup_files';
54 }
55
56 /**
57 * @example 'Restoring Themes From Backup'
58 * @return string
59 */
60 public static function getTaskTitle(): string
61 {
62 return 'Cleaning Up Restore Files';
63 }
64
65 /**
66 * @return TaskResponseDto
67 */
68 public function execute(): TaskResponseDto
69 {
70 $this->prepareCleanupRestoreTask();
71
72 $tmpRestoreDir = $this->directory->getTmpDirectory();
73
74 $tmpRestoreDir = untrailingslashit($tmpRestoreDir);
75
76 $relativePathForLogging = str_replace($this->filesystem->normalizePath(ABSPATH, true), '', $this->filesystem->normalizePath($tmpRestoreDir, true));
77
78 // Early bail: Path to Clean does not exist
79 if (!file_exists($tmpRestoreDir)) {
80 return $this->generateResponse();
81 }
82
83 try {
84 $deleted = $this->filesystem
85 ->setRecursive(true)
86 ->setShouldStop(function () {
87 return $this->isThreshold();
88 })
89 ->delete($tmpRestoreDir);
90 } catch (Throwable $e) {
91 $this->logger->warning(sprintf(
92 '%s: Could not cleanup path "%s". May be a permission issue?',
93 static::getTaskTitle(),
94 $relativePathForLogging
95 ));
96
97 return $this->generateResponse();
98 }
99
100 if ($deleted) {
101 // Successfully deleted
102 $this->logger->info(sprintf(
103 '%s: Path "%s" successfully cleaned up.',
104 static::getTaskTitle(),
105 $relativePathForLogging
106 ));
107
108 try {
109 $this->cleanPluginWpContentDir();
110 } catch (Throwable $ex) {
111 }
112
113 return $this->generateResponse();
114 } else {
115 /*
116 * Not successfully deleted.
117 * This can happen if the folder to delete is too large
118 * to be deleted in a single request. We continue
119 * deleting it in the next request...
120 */
121 $response = $this->generateResponse(false);
122 $response->setIsRunning(true);
123
124 $this->logger->info(sprintf(
125 '%s: Re-enqueuing path %s for deletion, as it couldn\'t be deleted in a single request without
126 hitting execution limits. If you see this message in a loop, PHP might not be able to delete
127 this directory, so you might want to try to delete it manually.',
128 static::getTaskTitle(),
129 $relativePathForLogging
130 ));
131
132 // Early bail: Response modified for repeating
133 return $response;
134 }
135 }
136
137 /**
138 * @return void
139 * @throws FilesystemExceptions
140 */
141 protected function cleanPluginWpContentDir()
142 {
143 $pluginWpContentDir = $this->directory->getPluginWpContentDirectory();
144 if (!file_exists($pluginWpContentDir)) {
145 return;
146 }
147
148 $tmpDirectory = trailingslashit($pluginWpContentDir) . 'tmp';
149 if (file_exists($tmpDirectory) && $this->filesystem->isEmptyDir($tmpDirectory)) {
150 $this->filesystem->delete($tmpDirectory);
151 }
152
153 if ($this->filesystem->isEmptyDir($pluginWpContentDir)) {
154 $this->filesystem->delete($pluginWpContentDir);
155 }
156 }
157
158 /**
159 * @return void
160 */
161 public function prepareCleanupRestoreTask()
162 {
163 // We only cleanup database file for RestoreTask
164 if (!$this instanceof RestoreTask) {
165 return;
166 }
167
168 // Early bail: Already prepared
169 if ($this->stepsDto->getTotal() === 1) {
170 return;
171 }
172
173 /** @var JobRestoreDataDto */
174 $jobDataDto = $this->jobDataDto;
175
176 // Clear the .sql file used during the restore, if this backup includes a database.
177 $databaseFile = $jobDataDto->getBackupMetadata()->getDatabaseFile();
178
179 if ($databaseFile) {
180 $databaseFile = $this->pathIdentifier->transformIdentifiableToPath($jobDataDto->getBackupMetadata()->getDatabaseFile());
181
182 if (file_exists($databaseFile)) {
183 unlink($databaseFile);
184 }
185 }
186
187 $this->stepsDto->setTotal(1);
188 }
189 }
190