Create
5 days ago
Delete
2 weeks ago
Reset
2 weeks ago
Update
2 weeks ago
AbstractAjaxPrepare.php
5 days 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
4 months ago
Update.php
2 weeks ago
AbstractAjaxPrepare.php
131 lines
| 1 | <?php |
| 2 | |
| 3 | namespace WPStaging\Staging\Ajax; |
| 4 | |
| 5 | use WPStaging\Core\WPStaging; |
| 6 | use WPStaging\Framework\Facades\Sanitize; |
| 7 | use WPStaging\Framework\Filesystem\Scanning\ScanConst; |
| 8 | use WPStaging\Framework\Job\Ajax\PrepareJob; |
| 9 | use WPStaging\Framework\Job\Exception\ProcessLockedException; |
| 10 | use WPStaging\Staging\Service\StagingEngine; |
| 11 | |
| 12 | abstract class AbstractAjaxPrepare extends PrepareJob |
| 13 | { |
| 14 | /** @var string */ |
| 15 | protected $postDataKey = ''; |
| 16 | |
| 17 | /** |
| 18 | * @param array|null $data |
| 19 | * @return void |
| 20 | */ |
| 21 | public function ajaxPrepare($data) |
| 22 | { |
| 23 | if (!$this->auth->isAuthenticatedRequest()) { |
| 24 | wp_send_json_error(null, 401); |
| 25 | } |
| 26 | |
| 27 | if (!WPStaging::make(StagingEngine::class)->isNextGenEnabled()) { |
| 28 | wp_send_json_error(esc_html__('The Next-Gen engine is temporarily unavailable. Please use the Classic engine.', 'wp-staging'), 403); |
| 29 | } |
| 30 | |
| 31 | try { |
| 32 | $this->processLock->checkProcessLocked(); |
| 33 | } catch (ProcessLockedException $e) { |
| 34 | wp_send_json_error($e->getMessage(), $e->getCode()); |
| 35 | } |
| 36 | |
| 37 | $response = $this->prepare($data); |
| 38 | |
| 39 | if ($response instanceof \WP_Error) { |
| 40 | wp_send_json_error($response->get_error_message(), $response->get_error_code()); |
| 41 | } |
| 42 | |
| 43 | wp_send_json_success(); |
| 44 | } |
| 45 | |
| 46 | /** |
| 47 | * @param array|null $data |
| 48 | * @return array|\WP_Error |
| 49 | */ |
| 50 | public function prepare($data = null) |
| 51 | { |
| 52 | if (empty($this->postDataKey)) { |
| 53 | return new \WP_Error(400, "Invalid request. Missing 'postDataKey' in Ajax Prepare class."); |
| 54 | } |
| 55 | |
| 56 | if (empty($data) && array_key_exists($this->postDataKey, $_POST)) { |
| 57 | $data = $this->postDataSanitization(); |
| 58 | } |
| 59 | |
| 60 | try { |
| 61 | $sanitizedData = $this->setupInitialJob($data); |
| 62 | } catch (\Exception $e) { |
| 63 | return new \WP_Error(400, $e->getMessage()); |
| 64 | } |
| 65 | |
| 66 | $this->deleteSseCacheFiles(); |
| 67 | |
| 68 | return $sanitizedData; |
| 69 | } |
| 70 | |
| 71 | /** |
| 72 | * @param array|null $data |
| 73 | * @return array |
| 74 | */ |
| 75 | public function validateAndSanitizeData($data): array |
| 76 | { |
| 77 | if (empty($data)) { |
| 78 | $data = []; |
| 79 | } |
| 80 | |
| 81 | // Unset any empty value so that we replace them with the defaults. |
| 82 | foreach ($data as $key => $value) { |
| 83 | if ($value === '' || $value === null) { |
| 84 | unset($data[$key]); |
| 85 | } |
| 86 | } |
| 87 | |
| 88 | $defaults = $this->getDefaults(); |
| 89 | |
| 90 | $data = wp_parse_args($data, $defaults); |
| 91 | |
| 92 | // Make sure data has no keys other than the expected ones. |
| 93 | $data = array_intersect_key($data, $defaults); |
| 94 | |
| 95 | // Make sure data has all expected keys. |
| 96 | foreach ($defaults as $expectedKey => $value) { |
| 97 | if (!array_key_exists($expectedKey, $data)) { |
| 98 | throw new \UnexpectedValueException("Invalid request. Missing '$expectedKey'."); |
| 99 | } |
| 100 | } |
| 101 | |
| 102 | $data = $this->additionalSanitization($data); |
| 103 | |
| 104 | return $data; |
| 105 | } |
| 106 | |
| 107 | abstract protected function getDefaults(): array; |
| 108 | |
| 109 | abstract protected function postDataSanitization(): array; |
| 110 | |
| 111 | abstract protected function additionalSanitization(array $data): array; |
| 112 | |
| 113 | protected function parseAndSanitizeTables(string $tables): array |
| 114 | { |
| 115 | $tables = $tables === '' ? [] : explode(ScanConst::DIRECTORIES_SEPARATOR, $tables); |
| 116 | |
| 117 | return array_map('sanitize_text_field', $tables); |
| 118 | } |
| 119 | |
| 120 | protected function parseAndSanitizeDirectories(string $directories): array |
| 121 | { |
| 122 | // JS posts values encodeURIComponent-wrapped, so the separator comma arrives as %2C. Decode |
| 123 | // first, otherwise explode() never splits the list and path matching silently fails. |
| 124 | $directories = $directories === '' ? [] : explode(ScanConst::DIRECTORIES_SEPARATOR, wpstg_urldecode(Sanitize::sanitizeString($directories))); |
| 125 | |
| 126 | return array_map('sanitize_text_field', $directories); |
| 127 | } |
| 128 | |
| 129 | abstract protected function prepareStagingSiteDto(); |
| 130 | } |
| 131 |