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