Create
1 day ago
Delete
2 weeks ago
Reset
2 weeks ago
Update
2 weeks ago
AbstractAjaxPrepare.php
1 day ago
Create.php
2 weeks ago
Delete.php
1 year ago
Listing.php
9 months ago
Repair.php
7 months ago
Reset.php
2 weeks ago
Setup.php
2 weeks ago
StagingSiteDataChecker.php
3 months ago
Update.php
2 weeks ago
Setup.php
188 lines
| 1 | <?php |
| 2 | |
| 3 | namespace WPStaging\Staging\Ajax; |
| 4 | |
| 5 | use WPStaging\Backend\Modules\Jobs\Job; |
| 6 | use WPStaging\Core\WPStaging; |
| 7 | use WPStaging\Framework\Component\AbstractTemplateComponent; |
| 8 | use WPStaging\Framework\Facades\Sanitize; |
| 9 | use WPStaging\Framework\Job\Exception\ProcessLockedException; |
| 10 | use WPStaging\Framework\Job\ProcessLock; |
| 11 | use WPStaging\Framework\TemplateEngine\TemplateEngine; |
| 12 | use WPStaging\Staging\Dto\StagingSiteDto; |
| 13 | use WPStaging\Staging\Service\LegacyOptionsCache; |
| 14 | use WPStaging\Staging\Service\AbstractStagingSetup; |
| 15 | use WPStaging\Staging\Service\DirectoryScanner; |
| 16 | use WPStaging\Staging\Service\TableScanner; |
| 17 | use WPStaging\Staging\Sites; |
| 18 | |
| 19 | /** |
| 20 | * Renders the unified staging setup UI for create, update, and reset jobs. |
| 21 | */ |
| 22 | class Setup extends AbstractTemplateComponent |
| 23 | { |
| 24 | /** |
| 25 | * @var AbstractStagingSetup |
| 26 | */ |
| 27 | private $stagingSetup; |
| 28 | |
| 29 | /** |
| 30 | * @var DirectoryScanner |
| 31 | */ |
| 32 | private $directoryScanner; |
| 33 | |
| 34 | /** |
| 35 | * @var TableScanner |
| 36 | */ |
| 37 | private $tableScanner; |
| 38 | |
| 39 | /** |
| 40 | * @var ProcessLock |
| 41 | */ |
| 42 | private $processLock; |
| 43 | |
| 44 | public function __construct(TemplateEngine $templateEngine, AbstractStagingSetup $stagingSetup, DirectoryScanner $directoryScanner, TableScanner $tableScanner, ProcessLock $processLock) |
| 45 | { |
| 46 | parent::__construct($templateEngine); |
| 47 | $this->stagingSetup = $stagingSetup; |
| 48 | $this->processLock = $processLock; |
| 49 | $this->directoryScanner = $directoryScanner; |
| 50 | $this->tableScanner = $tableScanner; |
| 51 | } |
| 52 | |
| 53 | /** |
| 54 | * @return void |
| 55 | */ |
| 56 | public function ajaxSetup() |
| 57 | { |
| 58 | if (!$this->canRenderAjax()) { |
| 59 | return; |
| 60 | } |
| 61 | |
| 62 | try { |
| 63 | $this->processLock->checkProcessLocked(); |
| 64 | } catch (ProcessLockedException $e) { |
| 65 | wp_send_json_error($e->getMessage(), $e->getCode()); |
| 66 | } |
| 67 | |
| 68 | $cloneId = $this->getValidatedCloneId(); |
| 69 | $isReset = $this->getIsReset(); |
| 70 | $isCreateModal = $this->getIsCreateModal(); |
| 71 | $isUpdateModal = $this->getIsUpdateModal(); |
| 72 | if (empty($cloneId)) { |
| 73 | $this->stagingSetup->initNewStagingSite(); |
| 74 | $this->prepareNewStagingSiteDto(); |
| 75 | } elseif ($isReset) { |
| 76 | $this->stagingSetup->initResetJob($this->getStagingSiteDtoByCloneId($cloneId)); |
| 77 | } else { |
| 78 | $this->stagingSetup->initUpdateJob($this->getStagingSiteDtoByCloneId($cloneId)); |
| 79 | } |
| 80 | |
| 81 | $this->directoryScanner->setStagingSetup($this->stagingSetup); |
| 82 | $this->tableScanner->setStagingSetup($this->stagingSetup); |
| 83 | $this->maybePrepareLegacyOptionsCache($cloneId, $isReset); |
| 84 | |
| 85 | $templateData = [ |
| 86 | 'stagingSetup' => $this->stagingSetup, |
| 87 | 'stagingSiteDto' => $this->stagingSetup->getStagingSiteDto(), |
| 88 | 'directoryScanner' => $this->directoryScanner, |
| 89 | 'tableScanner' => $this->tableScanner, |
| 90 | ]; |
| 91 | |
| 92 | $modalSetupMode = ''; |
| 93 | if ($isReset) { |
| 94 | $modalSetupMode = 'reset'; |
| 95 | } elseif ($isUpdateModal && !empty($cloneId)) { |
| 96 | $modalSetupMode = 'update'; |
| 97 | $this->directoryScanner->setShowFileDestination(false); |
| 98 | } elseif ($isCreateModal && empty($cloneId)) { |
| 99 | $modalSetupMode = 'create'; |
| 100 | } |
| 101 | |
| 102 | if ($modalSetupMode !== '') { |
| 103 | $templateData['setupMode'] = $modalSetupMode; |
| 104 | } |
| 105 | |
| 106 | $result = $this->templateEngine->render('staging/setup.php', $templateData); |
| 107 | |
| 108 | wp_send_json_success($result); |
| 109 | } |
| 110 | |
| 111 | private function maybePrepareLegacyOptionsCache(string $cloneId, bool $isReset) |
| 112 | { |
| 113 | WPStaging::make(LegacyOptionsCache::class)->prepare($this->getLegacyMainJob($cloneId, $isReset), $cloneId); |
| 114 | } |
| 115 | |
| 116 | private function prepareNewStagingSiteDto() |
| 117 | { |
| 118 | $cloneId = (string)time(); |
| 119 | $stagingSiteDto = $this->stagingSetup->getStagingSiteDto(); |
| 120 | $siteName = WPStaging::make(Sites::class)->generateStagingSiteName($cloneId); |
| 121 | |
| 122 | $stagingSiteDto->setCloneId($cloneId); |
| 123 | $stagingSiteDto->setCloneName($siteName); |
| 124 | $stagingSiteDto->setDirectoryName($siteName); |
| 125 | } |
| 126 | |
| 127 | private function getLegacyMainJob(string $cloneId, bool $isReset): string |
| 128 | { |
| 129 | if (empty($cloneId)) { |
| 130 | return Job::STAGING; |
| 131 | } |
| 132 | |
| 133 | return $isReset ? Job::RESET : Job::UPDATE; |
| 134 | } |
| 135 | |
| 136 | private function getValidatedCloneId(): string |
| 137 | { |
| 138 | if (empty($_POST['cloneId'])) { |
| 139 | return ''; |
| 140 | } |
| 141 | |
| 142 | return Sanitize::sanitizeString($_POST['cloneId']); |
| 143 | } |
| 144 | |
| 145 | private function getIsReset(): bool |
| 146 | { |
| 147 | if (empty($_POST['reset'])) { |
| 148 | return false; |
| 149 | } |
| 150 | |
| 151 | return Sanitize::sanitizeString($_POST['reset']) === 'true'; |
| 152 | } |
| 153 | |
| 154 | private function getIsCreateModal(): bool |
| 155 | { |
| 156 | if (empty($_POST['createModal'])) { |
| 157 | return false; |
| 158 | } |
| 159 | |
| 160 | return Sanitize::sanitizeString($_POST['createModal']) === 'true'; |
| 161 | } |
| 162 | |
| 163 | private function getIsUpdateModal(): bool |
| 164 | { |
| 165 | if (empty($_POST['updateModal'])) { |
| 166 | return false; |
| 167 | } |
| 168 | |
| 169 | return Sanitize::sanitizeString($_POST['updateModal']) === 'true'; |
| 170 | } |
| 171 | |
| 172 | /** |
| 173 | * @param string $cloneId |
| 174 | * @return StagingSiteDto |
| 175 | * @throws \Exception |
| 176 | */ |
| 177 | private function getStagingSiteDtoByCloneId(string $cloneId): StagingSiteDto |
| 178 | { |
| 179 | /** |
| 180 | * Lazy loading and it is not needed everywhere. |
| 181 | * @var Sites $stagingSitesService |
| 182 | */ |
| 183 | $stagingSitesService = WPStaging::make(Sites::class); |
| 184 | |
| 185 | return $stagingSitesService->getStagingSiteDtoByCloneId($cloneId); |
| 186 | } |
| 187 | } |
| 188 |