PluginProbe ʕ •ᴥ•ʔ
WP STAGING – WordPress Backups, Restore, Migration & Clone / 4.7.0
WP STAGING – WordPress Backups, Restore, Migration & Clone v4.7.0
4.11.2 4.11.1 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 / StagingSiteCreate / CreateRequirementsCheckTask.php
wp-staging / Staging / Tasks / StagingSiteCreate Last commit date
CreateRequirementsCheckTask.php 7 months ago FinishStagingSiteCreateTask.php 7 months ago
CreateRequirementsCheckTask.php
184 lines
1 <?php
2
3 namespace WPStaging\Staging\Tasks\StagingSiteCreate;
4
5 use RuntimeException;
6 use WPStaging\Backend\Modules\SystemInfo;
7 use WPStaging\Core\WPStaging;
8 use WPStaging\Framework\Adapter\Database;
9 use WPStaging\Framework\Adapter\Directory;
10 use WPStaging\Framework\Analytics\Actions\AnalyticsStagingCreate;
11 use WPStaging\Framework\Filesystem\DiskWriteCheck;
12 use WPStaging\Framework\Filesystem\Filesystem;
13 use WPStaging\Framework\Queue\SeekableQueueInterface;
14 use WPStaging\Framework\Utils\Cache\Cache;
15 use WPStaging\Framework\Job\Dto\StepsDto;
16 use WPStaging\Framework\Job\Exception\DiskNotWritableException;
17 use WPStaging\Staging\Dto\Job\StagingSiteJobsDataDto;
18 use WPStaging\Staging\Dto\StagingSiteDto;
19 use WPStaging\Staging\Sites;
20 use WPStaging\Staging\Tasks\StagingTask;
21 use WPStaging\Vendor\Psr\Log\LoggerInterface;
22
23 class CreateRequirementsCheckTask extends StagingTask
24 {
25 /** @var Directory */
26 protected $directory;
27
28 /** @var Database */
29 protected $database;
30
31 /** @var Filesystem */
32 protected $filesystem;
33
34 /** @var DiskWriteCheck */
35 protected $diskWriteCheck;
36
37 /** @var AnalyticsStagingCreate */
38 protected $analyticsStagingCreate;
39
40 /** @var SystemInfo */
41 protected $systemInfo;
42
43 /** @var StagingSiteJobsDataDto $jobDataDto */
44 protected $jobDataDto;
45
46 /** @var Sites */
47 protected $sites;
48
49 public function __construct(
50 Directory $directory,
51 Database $database,
52 Filesystem $filesystem,
53 LoggerInterface $logger,
54 Cache $cache,
55 StepsDto $stepsDto,
56 SeekableQueueInterface $taskQueue,
57 DiskWriteCheck $diskWriteCheck,
58 AnalyticsStagingCreate $analyticsStagingCreate,
59 SystemInfo $systemInfo,
60 Sites $sites
61 ) {
62 parent::__construct($logger, $cache, $stepsDto, $taskQueue);
63 $this->directory = $directory;
64 $this->filesystem = $filesystem;
65 $this->diskWriteCheck = $diskWriteCheck;
66 $this->analyticsStagingCreate = $analyticsStagingCreate;
67 $this->systemInfo = $systemInfo;
68 $this->database = $database;
69 $this->sites = $sites;
70 }
71
72 public static function getTaskName()
73 {
74 return 'staging_site_create_requirements_check';
75 }
76
77 public static function getTaskTitle()
78 {
79 return 'Requirements Check';
80 }
81
82 public function execute()
83 {
84 if (!$this->stepsDto->getTotal()) {
85 $this->stepsDto->setTotal(1);
86 }
87
88 try {
89 $this->logger->info('#################### Start Staging Site Create Job ####################');
90 $this->logger->writeLogHeader();
91 $this->logger->writeInstalledPluginsAndThemes();
92 $this->writeStagingSettingsLogs();
93 $this->cannotCreateStagingSiteOnMultisite();
94 $this->cannotCreateIfCantWriteToDisk();
95 $this->cannotCreateStagingDirectory();
96 $this->cannotCreateIfPrefixContainsInvalidCharacter();
97 $this->cannotCreateIfUsingExternalDatabase();
98 $this->cannotCreateIfStagingPrefixSameAsProductionSite();
99 } catch (RuntimeException $e) {
100 $this->analyticsStagingCreate->enqueueFinishEvent($this->jobDataDto->getId(), $this->jobDataDto);
101 $this->logger->critical($e->getMessage());
102
103 return $this->generateResponse(false);
104 }
105
106 $this->saveStagingSite();
107 $this->logger->info('Staging Site creation requirements passed...');
108
109 return $this->generateResponse();
110 }
111
112 /**
113 * @return void
114 */
115 protected function saveStagingSite()
116 {
117 $stagingSites = $this->sites->tryGettingStagingSites();
118 $stagingSites[$this->jobDataDto->getCloneId()] = $this->jobDataDto->getStagingSite()->toArray();
119 $this->sites->updateStagingSites($stagingSites);
120 }
121
122 protected function cannotCreateStagingSiteOnMultisite()
123 {
124 if (is_multisite()) {
125 throw new RuntimeException(__('Basic version doesn\'t support staging feature on multisite.', 'wp-staging'));
126 }
127 }
128
129 protected function cannotCreateIfCantWriteToDisk()
130 {
131 try {
132 $this->diskWriteCheck->testDiskIsWriteable();
133 } catch (DiskNotWritableException $e) {
134 throw new RuntimeException($e->getMessage());
135 }
136 }
137
138 protected function cannotCreateStagingDirectory()
139 {
140 $stagingSitePath = $this->jobDataDto->getStagingSitePath();
141 if (!is_dir($stagingSitePath) && !$this->filesystem->mkdir($stagingSitePath)) {
142 throw new RuntimeException(esc_html__('Cannot create staging site. Staging site directory does not exist and cannot be created!', 'wp-staging'));
143 }
144
145 if (!$this->filesystem->isEmptyDir($stagingSitePath)) {
146 throw new RuntimeException(esc_html__('Cannot create staging site. Staging site directory is not empty!', 'wp-staging'));
147 }
148 }
149
150 protected function cannotCreateIfPrefixContainsInvalidCharacter()
151 {
152 $stagingSitePrefix = $this->jobDataDto->getDatabasePrefix();
153 if (preg_match('|[^a-z0-9_]|i', $stagingSitePrefix)) {
154 throw new RuntimeException(esc_html__("Staging site prefix contains invalid character(s). Use different prefix with valid characters.", 'wp-staging'));
155 }
156 }
157
158 protected function cannotCreateIfUsingExternalDatabase()
159 {
160 if ($this->jobDataDto->getIsExternalDatabase()) {
161 throw new RuntimeException(esc_html__('Staging site creation with external database is not supported in the basic version.', 'wp-staging'));
162 }
163 }
164
165 protected function cannotCreateIfStagingPrefixSameAsProductionSite()
166 {
167 $isSamePrefix = $this->database->getBasePrefix() === $this->jobDataDto->getDatabasePrefix();
168 if ($isSamePrefix) {
169 throw new RuntimeException(esc_html__('Staging site prefix is same as production site prefix. Use different prefix for staging site.', 'wp-staging'));
170 }
171 }
172
173 protected function writeStagingSettingsLogs()
174 {
175 $this->logger->info('Staging Settings:');
176 $this->logger->info('Staging Site Path: ' . $this->jobDataDto->getStagingSitePath());
177 $this->logger->info('Staging Site URL: ' . $this->jobDataDto->getStagingSiteUrl());
178 $this->logger->info('Database Prefix: ' . $this->jobDataDto->getDatabasePrefix());
179 $this->logger->info('Clone ID: ' . $this->jobDataDto->getCloneId());
180 $this->logger->info('Clone Name: ' . $this->jobDataDto->getName());
181 $this->logger->info('Is External Database: ' . ($this->jobDataDto->getIsExternalDatabase() ? 'Yes' : 'No'));
182 }
183 }
184