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 / Staging / Tasks / StagingSite / CleanupStagingFilesTask.php
wp-staging / Staging / Tasks / StagingSite Last commit date
Database 2 weeks ago DatabaseAdjustment 2 weeks ago FileAdjustment 1 week ago Filesystem 2 weeks ago CleanupStagingFilesTask.php 1 year ago CleanupStagingTablesTask.php 2 weeks ago
CleanupStagingFilesTask.php
156 lines
1 <?php
2
3 namespace WPStaging\Staging\Tasks\StagingSite;
4
5 use Exception;
6 use Throwable;
7 use WPStaging\Framework\Queue\SeekableQueueInterface;
8 use WPStaging\Framework\Job\Dto\StepsDto;
9 use WPStaging\Framework\Job\Dto\TaskResponseDto;
10 use WPStaging\Framework\Utils\Cache\Cache;
11 use WPStaging\Framework\Filesystem\Filesystem;
12 use WPStaging\Staging\Interfaces\StagingSiteDtoInterface;
13 use WPStaging\Staging\Tasks\StagingTask;
14 use WPStaging\Vendor\Psr\Log\LoggerInterface;
15
16 class CleanupStagingFilesTask extends StagingTask
17 {
18 /** @var Filesystem */
19 private $filesystem;
20
21 /**
22 * @param LoggerInterface $logger
23 * @param Cache $cache
24 * @param StepsDto $stepsDto
25 * @param Filesystem $filesystem
26 * @param SeekableQueueInterface $taskQueue
27 */
28 public function __construct(LoggerInterface $logger, Cache $cache, StepsDto $stepsDto, Filesystem $filesystem, SeekableQueueInterface $taskQueue)
29 {
30 parent::__construct($logger, $cache, $stepsDto, $taskQueue);
31 $this->filesystem = $filesystem;
32 }
33
34 /**
35 * @return string
36 */
37 public static function getTaskName()
38 {
39 return 'staging_cleanup_files';
40 }
41
42 /**
43 * @return string
44 */
45 public static function getTaskTitle()
46 {
47 return 'Cleaning Up Staging Site Files';
48 }
49
50 /**
51 * @return TaskResponseDto
52 */
53 public function execute()
54 {
55 $stagingSiteDir = '';
56 try {
57 $stagingSiteDir = $this->prepareCleanup();
58 } catch (Throwable $e) {
59 $this->logger->error($e->getMessage());
60 return $this->generateResponse(false);
61 }
62
63 $stagingSiteDir = untrailingslashit($stagingSiteDir);
64
65 if ($this->filesystem->normalizePath(ABSPATH, true) === $this->filesystem->normalizePath($stagingSiteDir, true)) {
66 $this->logger->warning(sprintf(
67 '%s: Path "%s" is the same as the WordPress root directory. This is not allowed.',
68 static::getTaskTitle(),
69 $stagingSiteDir
70 ));
71
72 return $this->generateResponse();
73 }
74
75 $relativePathForLogging = str_replace($this->filesystem->normalizePath(ABSPATH, true), '', $this->filesystem->normalizePath($stagingSiteDir, true));
76
77 // Early bail: Path to Clean does not exist
78 if (!file_exists($stagingSiteDir)) {
79 return $this->generateResponse();
80 }
81
82 try {
83 $deleted = $this->filesystem
84 ->setRecursive(true)
85 ->setShouldStop(function () {
86 return $this->isThreshold();
87 })
88 ->delete($stagingSiteDir);
89 } catch (Throwable $e) {
90 $this->logger->warning(sprintf(
91 '%s: Could not cleanup path "%s". May be a permission issue?',
92 static::getTaskTitle(),
93 $relativePathForLogging
94 ));
95
96 return $this->generateResponse();
97 }
98
99 if ($deleted) {
100 // Successfully deleted
101 $this->logger->info(sprintf(
102 '%s: Path "%s" successfully cleaned up.',
103 static::getTaskTitle(),
104 $relativePathForLogging
105 ));
106
107 return $this->generateResponse();
108 } else {
109 /*
110 * Not successfully deleted.
111 * This can happen if the folder to delete is too large
112 * to be deleted in a single request. We continue
113 * deleting it in the next request...
114 */
115 $response = $this->generateResponse(false);
116 $response->setIsRunning(true);
117
118 $this->logger->info(sprintf(
119 '%s: Re-enqueuing path %s for deletion, as it couldn\'t be deleted in a single request without
120 hitting execution limits. If you see this message in a loop, PHP might not be able to delete
121 this directory, so you might want to try to delete it manually.',
122 static::getTaskTitle(),
123 $relativePathForLogging
124 ));
125
126 // Early bail: Response modified for repeating
127 return $response;
128 }
129 }
130
131 /**
132 * @return string
133 */
134 public function prepareCleanup(): string
135 {
136 if (!$this->jobDataDto instanceof StagingSiteDtoInterface) {
137 throw new Exception('Clone ID not found in job data.');
138 }
139
140 /** @var StagingSiteDtoInterface */
141 $jobDataDto = $this->jobDataDto;
142
143 // Early bail: Already prepared
144 if ($this->stepsDto->getTotal() === 1) {
145 return $jobDataDto->getStagingSite()->getPath();
146 }
147
148 $stagingSiteDto = $this->getStagingSiteDto($jobDataDto->getCloneId());
149 $jobDataDto->setStagingSite($stagingSiteDto);
150
151 $this->stepsDto->setTotal(1);
152
153 return $stagingSiteDto->getPath();
154 }
155 }
156