PrepareReset.php
196 lines
| 1 | <?php |
| 2 | |
| 3 | namespace WPStaging\Staging\Ajax\Reset; |
| 4 | |
| 5 | use WPStaging\Core\WPStaging; |
| 6 | use WPStaging\Framework\Facades\Sanitize; |
| 7 | use WPStaging\Framework\Job\JobTransientCache; |
| 8 | use WPStaging\Staging\Ajax\AbstractAjaxPrepare; |
| 9 | use WPStaging\Staging\Dto\Job\StagingSiteJobsDataDto; |
| 10 | use WPStaging\Staging\Dto\StagingSiteDto; |
| 11 | use WPStaging\Staging\Jobs\StagingSiteReset; |
| 12 | use WPStaging\Staging\Service\StagingEngine; |
| 13 | use WPStaging\Staging\Service\StagingSetup; |
| 14 | use WPStaging\Staging\Sites; |
| 15 | |
| 16 | class PrepareReset extends AbstractAjaxPrepare |
| 17 | { |
| 18 | |
| 19 | protected $postDataKey = 'wpstgResetData'; |
| 20 | |
| 21 | |
| 22 | protected $jobDataDto; |
| 23 | |
| 24 | |
| 25 | protected $jobReset; |
| 26 | |
| 27 | protected function postDataSanitization(): array |
| 28 | { |
| 29 | if (empty($_POST['wpstgResetData'])) { |
| 30 | throw new \UnexpectedValueException("Invalid request. Missing 'wpstgResetData'. Should never happen."); |
| 31 | } |
| 32 | |
| 33 | $data = Sanitize::sanitizeArray($_POST['wpstgResetData'], [ |
| 34 | 'cloneId' => 'string', |
| 35 | 'stagingEngine' => 'string', |
| 36 | 'allTablesExcluded' => 'bool', |
| 37 | 'excludeSizeGreaterThan' => 'string', |
| 38 | ]); |
| 39 | |
| 40 | $data['excludedTables'] = isset($_POST['wpstgResetData']['excludedTables']) ? $this->parseAndSanitizeTables($_POST['wpstgResetData']['excludedTables']) : []; // phpcs:ignore |
| 41 | $data['includedTables'] = isset($_POST['wpstgResetData']['includedTables']) ? $this->parseAndSanitizeTables($_POST['wpstgResetData']['includedTables']) : []; // phpcs:ignore |
| 42 | $data['nonSiteTables'] = isset($_POST['wpstgResetData']['nonSiteTables']) ? $this->parseAndSanitizeTables($_POST['wpstgResetData']['nonSiteTables']) : []; // phpcs:ignore |
| 43 | $data['excludedDirectories'] = isset($_POST['wpstgResetData']['excludedDirectories']) ? $this->parseAndSanitizeDirectories($_POST['wpstgResetData']['excludedDirectories']) : []; // phpcs:ignore |
| 44 | $data['extraDirectories'] = isset($_POST['wpstgResetData']['extraDirectories']) ? $this->parseAndSanitizeDirectories($_POST['wpstgResetData']['extraDirectories']) : []; // phpcs:ignore |
| 45 | |
| 46 | $data['excludeFileRules'] = isset($_POST['wpstgResetData']['excludeFileRules']) ? $this->parseAndSanitizeDirectories($_POST['wpstgResetData']['excludeFileRules']) : []; // phpcs:ignore |
| 47 | $data['excludeFolderRules'] = isset($_POST['wpstgResetData']['excludeFolderRules']) ? $this->parseAndSanitizeDirectories($_POST['wpstgResetData']['excludeFolderRules']) : []; // phpcs:ignore |
| 48 | $data['excludeExtensionRules'] = isset($_POST['wpstgResetData']['excludeExtensionRules']) ? $this->parseAndSanitizeDirectories($_POST['wpstgResetData']['excludeExtensionRules']) : []; // phpcs:ignore |
| 49 | |
| 50 | return $data; |
| 51 | } |
| 52 | |
| 53 | protected function additionalSanitization(array $data): array |
| 54 | { |
| 55 | |
| 56 | $data['cloneId'] = sanitize_text_field($data['cloneId']); |
| 57 | $data['stagingEngine'] = StagingEngine::ENGINE_NEXT_GEN; |
| 58 | |
| 59 | if (empty($data['cloneId'])) { |
| 60 | throw new \UnexpectedValueException("Invalid request. Missing 'cloneId'."); |
| 61 | } |
| 62 | |
| 63 | |
| 64 | $data['excludedTables'] = array_map('sanitize_text_field', $data['excludedTables']); |
| 65 | $data['includedTables'] = array_map('sanitize_text_field', $data['includedTables']); |
| 66 | $data['nonSiteTables'] = array_map('sanitize_text_field', $data['nonSiteTables']); |
| 67 | |
| 68 | |
| 69 | $data['extraDirectories'] = array_map('sanitize_text_field', $data['extraDirectories']); |
| 70 | $data['excludedDirectories'] = array_map('sanitize_text_field', $data['excludedDirectories']); |
| 71 | |
| 72 | |
| 73 | $data['excludeSizeGreaterThan'] = sanitize_text_field($data['excludeSizeGreaterThan']); |
| 74 | $data['excludeFileRules'] = array_map('sanitize_text_field', $data['excludeFileRules']); |
| 75 | $data['excludeFolderRules'] = array_map('sanitize_text_field', $data['excludeFolderRules']); |
| 76 | $data['excludeExtensionRules'] = array_map('sanitize_text_field', $data['excludeExtensionRules']); |
| 77 | |
| 78 | return $data; |
| 79 | } |
| 80 | |
| 81 | protected function getDefaults(): array |
| 82 | { |
| 83 | return [ |
| 84 | 'cloneId' => '', |
| 85 | 'stagingEngine' => StagingEngine::ENGINE_NEXT_GEN, |
| 86 | 'allTablesExcluded' => false, |
| 87 | 'excludedTables' => [], |
| 88 | 'includedTables' => [], |
| 89 | 'nonSiteTables' => [], |
| 90 | 'excludedDirectories' => [], |
| 91 | 'extraDirectories' => [], |
| 92 | |
| 93 | 'excludeSizeGreaterThan' => 8, |
| 94 | 'excludeFileRules' => [], |
| 95 | 'excludeFolderRules' => [], |
| 96 | 'excludeExtensionRules' => [], |
| 97 | ]; |
| 98 | } |
| 99 | |
| 100 | |
| 101 | |
| 102 | |
| 103 | |
| 104 | protected function setupInitialData($sanitizedData): array |
| 105 | { |
| 106 | $sanitizedData = $this->validateAndSanitizeData($sanitizedData); |
| 107 | $this->clearCacheFolder(); |
| 108 | |
| 109 | |
| 110 | $services = WPStaging::getInstance()->getContainer(); |
| 111 | |
| 112 | $this->jobDataDto = $services->get(StagingSiteJobsDataDto::class); |
| 113 | |
| 114 | $this->jobReset = $services->get($this->getJobClass()); |
| 115 | |
| 116 | $this->populateJobDataDtoByCloneId($sanitizedData['cloneId']); |
| 117 | |
| 118 | $this->jobDataDto->hydrate($sanitizedData); |
| 119 | $this->jobDataDto->setInit(true); |
| 120 | $this->jobDataDto->setFinished(false); |
| 121 | $this->jobDataDto->setStartTime(time()); |
| 122 | $this->jobDataDto->setStagingSiteUploads($this->directory->getRelativeUploadsDirectory()); |
| 123 | $this->jobDataDto->setJobType(StagingSetup::JOB_RESET); |
| 124 | |
| 125 | $this->prepareStagingSiteDto(); |
| 126 | |
| 127 | $this->jobDataDto->setId(substr(md5(mt_rand() . time()), 0, 12)); |
| 128 | |
| 129 | $this->jobReset->getTransientCache()->startJob($this->jobDataDto->getId(), esc_html__('Staging Site Reset in Progress', 'wp-staging'), JobTransientCache::JOB_TYPE_STAGING_RESET, $this->queueId); |
| 130 | |
| 131 | $this->jobReset->setJobDataDto($this->jobDataDto); |
| 132 | |
| 133 | return $sanitizedData; |
| 134 | } |
| 135 | |
| 136 | |
| 137 | |
| 138 | |
| 139 | |
| 140 | |
| 141 | public function getJob() |
| 142 | { |
| 143 | return $this->jobReset; |
| 144 | } |
| 145 | |
| 146 | |
| 147 | |
| 148 | |
| 149 | protected function getJobClass(): string |
| 150 | { |
| 151 | return StagingSiteReset::class; |
| 152 | } |
| 153 | |
| 154 | |
| 155 | |
| 156 | |
| 157 | |
| 158 | |
| 159 | public function persist(): bool |
| 160 | { |
| 161 | if (!$this->jobReset instanceof StagingSiteReset) { |
| 162 | return false; |
| 163 | } |
| 164 | |
| 165 | $this->jobReset->persist(); |
| 166 | |
| 167 | return true; |
| 168 | } |
| 169 | |
| 170 | protected function prepareStagingSiteDto() |
| 171 | { |
| 172 | $stagingSite = $this->jobDataDto->getStagingSite(); |
| 173 | $stagingSite->setStatus(StagingSiteDto::STATUS_UNFINISHED_BROKEN); |
| 174 | $stagingSite->setDatetime(time()); |
| 175 | $stagingSite->setVersion(WPStaging::getVersion()); |
| 176 | $stagingSite->setOwnerId(get_current_user_id()); |
| 177 | |
| 178 | $this->jobDataDto->setStagingSite($stagingSite); |
| 179 | } |
| 180 | |
| 181 | protected function populateJobDataDtoByCloneId(string $cloneId) |
| 182 | { |
| 183 | |
| 184 | |
| 185 | |
| 186 | $stagingSites = WPStaging::make(Sites::class); // @phpstan-ignore-line |
| 187 | $stagingSite = $stagingSites->getStagingSiteDtoByCloneId($cloneId); |
| 188 | $this->jobDataDto->setStagingSite($stagingSite); |
| 189 | $this->jobDataDto->setCloneId($cloneId); |
| 190 | $this->jobDataDto->setStagingSiteUrl($stagingSite->getUrl()); |
| 191 | $this->jobDataDto->setStagingSitePath($stagingSite->getPath()); |
| 192 | $this->jobDataDto->setDatabasePrefix($stagingSite->getUsedPrefix()); |
| 193 | $this->jobDataDto->setIsExternalDatabase($stagingSite->getIsExternalDatabase()); |
| 194 | } |
| 195 | } |
| 196 |