PluginProbe ʕ •ᴥ•ʔ
WP STAGING – WordPress Backups, Restore, Migration & Clone / 4.11.0
WP STAGING – WordPress Backups, Restore, Migration & Clone v4.11.0
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 / Ajax / Delete / PrepareDelete.php
wp-staging / Staging / Ajax / Delete Last commit date
DeleteConfirm.php 1 day ago PrepareDelete.php 1 day ago
PrepareDelete.php
181 lines
1 <?php
2
3 namespace WPStaging\Staging\Ajax\Delete;
4
5 use WPStaging\Core\WPStaging;
6 use WPStaging\Framework\Facades\Sanitize;
7 use WPStaging\Framework\Job\Ajax\PrepareJob;
8 use WPStaging\Framework\Job\Exception\ProcessLockedException;
9 use WPStaging\Framework\Job\JobTransientCache;
10 use WPStaging\Staging\Dto\Job\StagingSiteDeleteDataDto;
11 use WPStaging\Staging\Jobs\StagingSiteDelete;
12
13 class PrepareDelete extends PrepareJob
14 {
15
16 private $jobDataDto;
17
18
19 private $jobDelete;
20
21
22
23
24
25 public function ajaxPrepare($data)
26 {
27 if (!$this->auth->isAuthenticatedRequest()) {
28 wp_send_json_error(null, 401);
29 }
30
31 try {
32 $this->processLock->lockProcess();
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
48
49
50 public function prepare($data = null)
51 {
52 if (empty($data) && array_key_exists('wpstgDeleteData', $_POST)) {
53 $data = Sanitize::sanitizeArray($_POST['wpstgDeleteData'], [
54 'isDeletingTables' => 'bool',
55 'isDeletingFiles' => 'bool',
56 'cloneId' => 'string',
57 ]);
58 $data['excludedTables'] = isset($_POST['wpstgDeleteData']['excludedTables']) ? Sanitize::sanitizeString($_POST['wpstgDeleteData']['excludedTables']) : [];
59 }
60
61 try {
62 $sanitizedData = $this->setupInitialJob($data);
63 } catch (\Exception $e) {
64 return new \WP_Error(400, $e->getMessage());
65 }
66
67 $this->deleteSseCacheFiles();
68
69 return $sanitizedData;
70 }
71
72
73
74
75
76 protected function setupInitialData($sanitizedData): array
77 {
78 $sanitizedData = $this->validateAndSanitizeData($sanitizedData);
79 $this->clearCacheFolder();
80
81
82 $services = WPStaging::getInstance()->getContainer();
83
84 $this->jobDataDto = $services->get(StagingSiteDeleteDataDto::class);
85
86 $this->jobDelete = $services->get(StagingSiteDelete::class);
87
88 $this->jobDataDto->hydrate($sanitizedData);
89 $this->jobDataDto->setInit(true);
90 $this->jobDataDto->setFinished(false);
91 $this->jobDataDto->setStartTime(time());
92
93 $this->jobDataDto->setId(substr(md5(mt_rand() . time()), 0, 12));
94
95 $this->jobDelete->getTransientCache()->startJob($this->jobDataDto->getId(), esc_html__('Staging Site Delete in Progress', 'wp-staging'), JobTransientCache::JOB_TYPE_STAGING_DELETE, $this->queueId);
96
97 $this->jobDelete->setJobDataDto($this->jobDataDto);
98
99 return $sanitizedData;
100 }
101
102
103
104
105
106 public function validateAndSanitizeData($data): array
107 {
108
109 foreach ($data as $key => $value) {
110 if (empty($value)) {
111 unset($data[$key]);
112 }
113 }
114
115 $defaults = [
116 'cloneId' => '',
117 'isDeletingFiles' => false,
118 'isDeletingTables' => false,
119 'excludedTables' => [],
120 ];
121
122 $data = wp_parse_args($data, $defaults);
123
124
125 $data = array_intersect_key($data, $defaults);
126
127
128 foreach ($defaults as $expectedKey => $value) {
129 if (!array_key_exists($expectedKey, $data)) {
130 throw new \UnexpectedValueException("Invalid request. Missing '$expectedKey'.");
131 }
132 }
133
134
135 $data['cloneId'] = sanitize_text_field($data['cloneId']);
136
137
138 $data['isDeletingFiles'] = $this->jsBoolean($data['isDeletingFiles']);
139 $data['isDeletingTables'] = $this->jsBoolean($data['isDeletingTables']);
140
141 if (!$data['isDeletingFiles'] && !$data['isDeletingTables']) {
142 throw new \UnexpectedValueException('Invalid request. Select at least one item to delete.');
143 }
144
145
146 $data['excludedTables'] = array_map('sanitize_text_field', $data['excludedTables']);
147
148 if (empty($data['cloneId'])) {
149 throw new \UnexpectedValueException("Invalid request. Missing 'cloneId'.");
150 }
151
152 return $data;
153 }
154
155
156
157
158
159
160 public function getJob()
161 {
162 return $this->jobDelete;
163 }
164
165
166
167
168
169
170 public function persist(): bool
171 {
172 if (!$this->jobDelete instanceof StagingSiteDelete) {
173 return false;
174 }
175
176 $this->jobDelete->persist();
177
178 return true;
179 }
180 }
181