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 / PreserveDataFirstStep.php
wp-staging / Backend / Modules / Jobs Last commit date
Cleaners 1 day ago Exceptions 1 day ago Cancel.php 1 day ago CancelUpdate.php 1 day ago Cloning.php 1 day ago CloningProcess.php 1 day ago Data.php 1 day ago Database.php 1 day ago Delete.php 1 day ago Directories.php 1 day ago Files.php 1 day ago Finish.php 1 day ago Job.php 1 day ago JobExecutable.php 1 day ago Logs.php 1 day ago PreserveDataFirstStep.php 1 day ago PreserveDataSecondStep.php 1 day ago ProcessLock.php 1 day ago Scan.php 1 day ago SearchReplace.php 1 day ago TotalStepsAreNumberOfTables.php 1 day ago Updating.php 1 day ago
PreserveDataFirstStep.php
234 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\Backend\Modules\Jobs\Job as MainJob;
11
12
13
14
15
16
17
18 class PreserveDataFirstStep extends JobExecutable
19 {
20
21 private $stagingDb;
22
23
24 private $productionDb;
25
26
27 private $stagingPrefix;
28
29
30 private $backupSchedulesOption;
31
32 protected function calculateTotalSteps()
33 {
34 $this->options->totalSteps = 1;
35
36 if (class_exists('\WPStaging\Backup\BackupScheduler')) {
37 $this->backupSchedulesOption = \WPStaging\Backup\BackupScheduler::OPTION_BACKUP_SCHEDULES;
38 } else {
39
40
41 $this->backupSchedulesOption = 'wpstg_backup_schedules';
42 }
43 }
44
45
46 public function start()
47 {
48 $db = new SourceDatabase($this->options);
49
50 $this->stagingDb = $db->getDatabase();
51 $this->productionDb = WPStaging::getInstance()->get("wpdb");
52 $this->stagingPrefix = $this->options->prefix;
53
54 if ($db->isExternalDatabase()) {
55 $this->stagingPrefix = $this->options->databasePrefix;
56 }
57
58 $this->run();
59 $this->saveOptions();
60
61 return (object)$this->response;
62 }
63
64
65 protected function execute()
66 {
67 $this->copyToTmp();
68 $this->prepareResponse(true, true);
69
70 return false;
71 }
72
73
74 public function copyToTmp()
75 {
76
77 $delete = $this->productionDb->query(
78 $this->productionDb->prepare("DELETE FROM " . $this->productionDb->prefix . "options WHERE `option_name` = %s", "wpstg_tmp_data")
79 );
80
81 if (!$this->tableExists($this->stagingPrefix . "options")) {
82 return true;
83 }
84
85
86 $stagingSites = $this->getStagingSiteOption(Sites::STAGING_SITES_OPTION);
87
88
89 if ($this->options->mainJob === MainJob::UPDATE) {
90 $settings = $this->getStagingSiteOption("wpstg_settings");
91 }
92
93 $loginLinkSettings = $this->getStagingSiteOption("wpstg_login_link_settings");
94
95
96 $cloneOptions = $this->getStagingSiteOption(CloneOptions::WPSTG_CLONE_SETTINGS_KEY);
97
98
99 $backupSchedules = $this->getStagingSiteOption($this->backupSchedulesOption);
100
101
102 $remoteStorages = $this->preserveRemoteStorages();
103
104
105 if (!$stagingSites && !$settings && !$cloneOptions && !$backupSchedules && !$loginLinkSettings && empty($remoteStorages)) {
106 return true;
107 }
108
109 $options = [
110 'stagingSites' => $stagingSites,
111 'cloneOptions' => $cloneOptions,
112 'backupSchedules' => $backupSchedules,
113 'loginLinkSettings' => $loginLinkSettings,
114 ];
115
116
117 if ($this->options->mainJob === MainJob::UPDATE) {
118 $options['settings'] = $settings;
119 }
120
121 $options = array_merge($options, $remoteStorages);
122 $tmpData = serialize((object) $options);
123
124
125 $insert = $this->productionDb->query(
126 $this->productionDb->prepare(
127 "INSERT INTO `" . $this->productionDb->prefix . "options` ( `option_id`, `option_name`, `option_value`, `autoload` ) VALUES ( NULL , %s, %s, %s )",
128 "wpstg_tmp_data",
129 $tmpData,
130 "no"
131 )
132 );
133
134 if ($delete === false) {
135 $this->log("Preserve Data: Failed to delete wpstg_tmp_data");
136 }
137
138 if ($stagingSites === false) {
139 $this->log("Preserve Data: Failed to get wpstg_staging_sites");
140 }
141
142 if ($settings === false) {
143 $this->log("Preserve Data: Failed to get wpstg_settings");
144 }
145
146 if ($cloneOptions === false) {
147 $this->log("Preserve Data: Failed to get wpstg_clone_options");
148 }
149
150 if ($loginLinkSettings === false) {
151 $this->log("Preserve Data: Failed to get wpstg_login_link_settings");
152 }
153
154 if ($backupSchedules === false) {
155 $this->log("Preserve Data: Failed to get " . $this->backupSchedulesOption);
156 }
157
158 if ($insert === false) {
159 $this->log("Preserve Data: Failed to insert wpstg_staging_sites to wpstg_tmp_data");
160 }
161
162 return true;
163 }
164
165
166 protected function preserveRemoteStorages()
167 {
168 $storages = [];
169
170 foreach (Providers::STORAGE_LABELS as $identifier => $label) {
171 $value = $this->getStagingSiteStorageOption($identifier);
172 if ($value === false) {
173 $this->log("Preserve Data: Failed to get {$label} Settings");
174 } else {
175 $storages[$identifier] = $value;
176 }
177 }
178
179 return $storages;
180 }
181
182
183
184
185
186
187
188
189 protected function getStagingSiteStorageOption($identifier)
190 {
191 $value = $this->getStagingSiteOption('wpstg_' . $identifier);
192
193
194
195 if ($value !== null) {
196 return $value;
197 }
198
199 if (isset(Providers::LEGACY_OPTION_MAP[$identifier])) {
200 $legacyValue = $this->getStagingSiteOption(Providers::LEGACY_OPTION_MAP[$identifier]);
201 if ($legacyValue !== null) {
202 return $legacyValue;
203 }
204 }
205
206 return $value;
207 }
208
209
210
211
212
213
214 protected function getStagingSiteOption($optionName)
215 {
216 return $this->stagingDb->get_var(
217 $this->stagingDb->prepare(
218 "SELECT `option_value` FROM " . $this->stagingPrefix . "options WHERE `option_name` = %s",
219 $optionName
220 )
221 );
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