PluginProbe ʕ •ᴥ•ʔ
WP STAGING – WordPress Backups, Restore, Migration & Clone / 4.11.0
WP STAGING – WordPress Backups, Restore, Migration & Clone v4.11.0
4.11.0 4.10.0 4.9.5 4.9.4 4.9.3 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 1 day ago DatabaseAdjustment 1 day ago FileAdjustment 1 day ago Filesystem 1 day ago CleanupStagingFilesTask.php 1 day ago CleanupStagingTablesTask.php 1 day 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
19 private $filesystem;
20
21
22
23
24
25
26
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
36
37 public static function getTaskName()
38 {
39 return 'staging_cleanup_files';
40 }
41
42
43
44
45 public static function getTaskTitle()
46 {
47 return 'Cleaning Up Staging Site Files';
48 }
49
50
51
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
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
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
111
112
113
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
127 return $response;
128 }
129 }
130
131
132
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
141 $jobDataDto = $this->jobDataDto;
142
143
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