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 / Backend / Modules / Jobs / PreserveDataSecondStep.php
wp-staging / Backend / Modules / Jobs Last commit date
Cleaners 3 days ago Exceptions 3 days ago Cancel.php 3 days ago CancelUpdate.php 3 days ago Cloning.php 3 days ago CloningProcess.php 3 days ago Data.php 3 days ago Database.php 3 days ago Delete.php 3 days ago Directories.php 3 days ago Files.php 3 days ago Finish.php 3 days ago Job.php 3 days ago JobExecutable.php 3 days ago Logs.php 3 days ago PreserveDataFirstStep.php 3 days ago PreserveDataSecondStep.php 3 days ago ProcessLock.php 3 days ago Scan.php 3 days ago SearchReplace.php 3 days ago TotalStepsAreNumberOfTables.php 3 days ago Updating.php 3 days ago
PreserveDataSecondStep.php
268 lines
1 <?php
2
3 namespace WPStaging\Backend\Modules\Jobs;
4
5 use WPStaging\Backup\Storage\Providers;
6 use WPStaging\Core\WPStaging;
7 use WPStaging\Framework\Adapter\SourceDatabase;
8 use WPStaging\Staging\CloneOptions;
9 use WPStaging\Staging\Sites;
10 use WPStaging\Staging\FirstRun;
11 use WPStaging\Backend\Modules\Jobs\Job as MainJob;
12
13 use function WPStaging\functions\debug_log;
14
15
16
17
18
19 class PreserveDataSecondStep extends JobExecutable
20 {
21
22 private $stagingDb;
23
24
25 private $productionDb;
26
27
28 private $stagingPrefix;
29
30
31 private $preservedData;
32
33 protected function calculateTotalSteps()
34 {
35 $this->options->totalSteps = 1;
36 }
37
38
39 public function start()
40 {
41 $this->run();
42 $this->saveOptions();
43
44 return (object)$this->response;
45 }
46
47
48 protected function execute()
49 {
50 $db = new SourceDatabase($this->options);
51
52 $this->stagingDb = $db->getDatabase();
53 $this->productionDb = WPStaging::getInstance()->get("wpdb");
54 $this->stagingPrefix = $this->options->prefix;
55
56 if ($db->isExternalDatabase()) {
57 $this->stagingPrefix = $this->options->databasePrefix;
58 }
59
60 $this->copyToStaging();
61 $this->prepareResponse(true, true);
62
63 return false;
64 }
65
66
67 public function copyToStaging()
68 {
69
70 if (!$this->tableExists($this->stagingPrefix . "options")) {
71 return true;
72 }
73
74
75 $result = $this->productionDb->get_var(
76 $this->productionDb->prepare(
77 "SELECT `option_value` FROM " . $this->productionDb->prefix . "options WHERE `option_name` = %s",
78 "wpstg_tmp_data"
79 )
80 );
81
82
83 if (!$result) {
84 return true;
85 }
86
87
88
89 $backupSchedulesOption = 'wpstg_backup_schedules';
90
91
92 $deleteTmpData = $this->productionDb->query(
93 $this->productionDb->prepare("DELETE FROM " . $this->productionDb->prefix . "options WHERE `option_name` = %s", "wpstg_tmp_data")
94 );
95
96 if ($deleteTmpData === false) {
97 $this->log("Preserve Data Second Step: Failed to delete wpstg_tmp_data from the production site");
98 }
99
100 $this->preservedData = maybe_unserialize($result);
101
102
103 $this->preserveStagingOption(Sites::STAGING_SITES_OPTION, $this->preservedData->stagingSites, 'existing clones');
104
105 if ($this->options->mainJob === MainJob::UPDATE && isset($this->preservedData->settings)) {
106 $this->preserveStagingOption("wpstg_settings", $this->preservedData->settings, 'settings');
107 }
108
109
110 $this->preserveStagingOption("wpstg_login_link_settings", $this->preservedData->loginLinkSettings, 'login settings');
111
112
113 $this->updateCloneOptions();
114 $this->preserveStagingOption(CloneOptions::WPSTG_CLONE_SETTINGS_KEY, $this->preservedData->cloneOptions, 'clone options');
115
116
117 $this->preserveStagingOption($backupSchedulesOption, $this->preservedData->backupSchedules, 'backup schedules');
118
119 foreach (Providers::STORAGE_LABELS as $identifier => $label) {
120 $optionName = 'wpstg_' . $identifier;
121 $value = $this->getPreservedStorageValue($identifier);
122 if ($value !== null) {
123 $this->preserveStagingOption($optionName, $value, $label . ' settings');
124 } else {
125 $this->deleteStagingSiteOption($optionName);
126 }
127 }
128
129 return true;
130 }
131
132
133
134
135
136
137 protected function preserveStagingOption($optionName, $optionValue, $logEntity, $autoload = false)
138 {
139 $isDeleted = $this->deleteStagingSiteOption($optionName);
140
141 if ($isDeleted === false) {
142 $this->log("Preserve Data Second Step: Failed to delete " . $optionName . " from the staging site");
143 }
144
145 $isInserted = $this->insertOptionIntoStagingSite($optionName, $optionValue, $autoload);
146
147 if ($isInserted === false) {
148 $this->log("Preserve Data Second Step: Failed to insert preserved " . $logEntity . " into " . $optionName . " of the staging site");
149 }
150 }
151
152
153
154
155
156
157 protected function deleteStagingSiteOption($optionName)
158 {
159 return $this->stagingDb->query(
160 $this->stagingDb->prepare("DELETE FROM " . $this->stagingPrefix . "options WHERE `option_name` = %s", $optionName)
161 );
162 }
163
164
165
166
167
168
169
170
171 protected function insertOptionIntoStagingSite($optionName, $optionValue, $autoload = false)
172 {
173 $autoload = $autoload ? 'yes' : 'no';
174
175 return $this->stagingDb->query(
176 $this->stagingDb->prepare(
177 "INSERT INTO `" . $this->stagingPrefix . "options` ( `option_id`, `option_name`, `option_value`, `autoload` ) VALUES ( NULL , %s, %s, %s )",
178 $optionName,
179 $optionValue,
180 $autoload
181 )
182 );
183 }
184
185
186
187
188
189
190
191
192 protected function getPreservedStorageValue(string $identifier)
193 {
194 if ($this->propertyExists($identifier)) {
195 return $this->preservedData->{$identifier};
196 }
197
198 $legacyProperty = isset(Providers::LEGACY_PROPERTY_MAP[$identifier]) ? Providers::LEGACY_PROPERTY_MAP[$identifier] : $identifier;
199 if ($legacyProperty !== $identifier && $this->propertyExists($legacyProperty)) {
200 return $this->preservedData->{$legacyProperty};
201 }
202
203 return null;
204 }
205
206
207
208
209
210
211 protected function propertyExists($property)
212 {
213 if (!is_object($this->preservedData)) {
214 return false;
215 }
216
217 if (!property_exists($this->preservedData, $property)) {
218 return false;
219 }
220
221 return !empty($this->preservedData->{$property});
222 }
223
224
225
226
227
228
229 private function tableExists($table)
230 {
231 return !($table != $this->stagingDb->get_var("SHOW TABLES LIKE '{$table}'"));
232 }
233
234
235
236
237
238
239 private function updateCloneOptions()
240 {
241 if ($this->getOptions()->mainJob !== MainJob::UPDATE) {
242 return;
243 }
244
245 $cloneOptions = $this->preservedData->cloneOptions;
246
247 $data = maybe_unserialize($cloneOptions);
248 if (empty($cloneOptions)) {
249 $data = new \stdClass();
250 }
251
252
253 if (!is_object($data)) {
254 debug_log('Fail to update clone options before restore.');
255 return;
256 }
257
258
259 if (property_exists($data, 'wpstg_woo_scheduler_disabled')) {
260 unset($data->wpstg_woo_scheduler_disabled);
261 }
262
263 $schedulerKey = FirstRun::WOO_SCHEDULER_ENABLED_KEY;
264 $data->{$schedulerKey} = empty($this->getOptions()->isWooSchedulerEnabled) ? false : true;
265 $this->preservedData->cloneOptions = maybe_serialize($data);
266 }
267 }
268