PluginProbe ʕ •ᴥ•ʔ
WP STAGING – WordPress Backup, Restore, Migration & Clone / 3.8.2
WP STAGING – WordPress Backup, Restore, Migration & Clone v3.8.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 5 years ago Exceptions 5 years ago Cancel.php 2 years ago CancelUpdate.php 2 years ago Cloning.php 2 years ago CloningProcess.php 2 years ago Data.php 2 years ago Database.php 2 years ago Delete.php 2 years ago Directories.php 2 years ago Files.php 2 years ago Finish.php 1 year ago Job.php 2 years ago JobExecutable.php 2 years ago Logs.php 3 years ago PreserveDataFirstStep.php 3 years ago PreserveDataSecondStep.php 2 years ago ProcessLock.php 2 years ago Scan.php 2 years ago SearchReplace.php 2 years ago TotalStepsAreNumberOfTables.php 5 years ago Updating.php 2 years ago
PreserveDataFirstStep.php
255 lines
1 <?php
2
3 namespace WPStaging\Backend\Modules\Jobs;
4
5 use WPStaging\Core\WPStaging;
6 use WPStaging\Framework\Adapter\SourceDatabase;
7 use WPStaging\Framework\Staging\CloneOptions;
8 use WPStaging\Framework\Staging\Sites;
9
10 /**
11 * Preserve staging sites in wpstg_staging_sites in staging database while updating a site
12 * While cloning, copy an existing entry from staging site to wpstg_tmp_data and after cloning restore that data.
13 * Mainly used while an existing staging site is updated, not initially cloned
14 * @package WPStaging\Backend\Modules\Jobs
15 */
16 class PreserveDataFirstStep extends JobExecutable
17 {
18 /** @var \wpdb */
19 private $stagingDb;
20
21 /** @var \wpdb */
22 private $productionDb;
23
24 /** @var string */
25 private $stagingPrefix;
26
27 /** @var string */
28 private $backupSchedulesOption;
29
30 protected function calculateTotalSteps()
31 {
32 $this->options->totalSteps = 1;
33
34 if (class_exists('\WPStaging\Backup\BackupScheduler')) {
35 $this->backupSchedulesOption = \WPStaging\Backup\BackupScheduler::OPTION_BACKUP_SCHEDULES;
36 } else {
37 // Fallback if pro namespace is not available
38 // @see \WPStaging\Backup\BackupScheduler::OPTION_BACKUP_SCHEDULES
39 $this->backupSchedulesOption = 'wpstg_backup_schedules';
40 }
41 }
42
43 /** @return object */
44 public function start()
45 {
46 $db = new SourceDatabase($this->options);
47
48 $this->stagingDb = $db->getDatabase();
49 $this->productionDb = WPStaging::getInstance()->get("wpdb");
50 $this->stagingPrefix = $this->options->prefix;
51
52 if ($db->isExternalDatabase()) {
53 $this->stagingPrefix = $this->options->databasePrefix;
54 }
55
56 $this->run();
57 $this->saveOptions();
58
59 return (object)$this->response;
60 }
61
62 /** @return false */
63 protected function execute()
64 {
65 $this->copyToTmp();
66 $this->prepareResponse(true, true);
67
68 return false;
69 }
70
71 /** @return true */
72 public function copyToTmp()
73 {
74 // Delete wpstg_tmp_data and reset it
75 $delete = $this->productionDb->query(
76 $this->productionDb->prepare("DELETE FROM " . $this->productionDb->prefix . "options WHERE `option_name` = %s", "wpstg_tmp_data")
77 );
78
79 if (!$this->tableExists($this->stagingPrefix . "options")) {
80 return true;
81 }
82
83 // Get wpstg_staging_sites from staging database
84 $stagingSites = $this->getStagingSiteOption(Sites::STAGING_SITES_OPTION);
85
86 // Get wpstg_settings from staging database
87 $settings = $this->getStagingSiteOption("wpstg_settings");
88
89 // Get wpstg_clone_options from staging database
90 $cloneOptions = $this->getStagingSiteOption(CloneOptions::WPSTG_CLONE_SETTINGS_KEY);
91
92 // Automated backup schedules
93 $backupSchedules = $this->getStagingSiteOption($this->backupSchedulesOption);
94
95 // All remote storages for backup
96 $remoteStorages = $this->preserveRemoteStorages();
97
98 // Nothing to do
99 if (!$stagingSites && !$settings && !$cloneOptions && !$backupSchedules && empty($remoteStorages)) {
100 return true;
101 }
102
103 $options = [
104 'stagingSites' => $stagingSites,
105 'settings' => $settings,
106 'cloneOptions' => $cloneOptions,
107 'backupSchedules' => $backupSchedules,
108 ];
109
110 $options = array_merge($options, $remoteStorages);
111 $tmpData = serialize((object) $options);
112
113 // Insert staging site preserved data into wpstg_tmp_data in production database
114 $insert = $this->productionDb->query(
115 $this->productionDb->prepare(
116 "INSERT INTO `" . $this->productionDb->prefix . "options` ( `option_id`, `option_name`, `option_value`, `autoload` ) VALUES ( NULL , %s, %s, %s )",
117 "wpstg_tmp_data",
118 $tmpData,
119 "no"
120 )
121 );
122
123 if ($delete === false) {
124 $this->log("Preserve Data: Failed to delete wpstg_tmp_data");
125 }
126
127 if ($stagingSites === false) {
128 $this->log("Preserve Data: Failed to get wpstg_staging_sites");
129 }
130
131 if ($settings === false) {
132 $this->log("Preserve Data: Failed to get wpstg_settings");
133 }
134
135 if ($cloneOptions === false) {
136 $this->log("Preserve Data: Failed to get wpstg_clone_options");
137 }
138
139 if ($backupSchedules === false) {
140 $this->log("Preserve Data: Failed to get " . $this->backupSchedulesOption);
141 }
142
143 if ($insert === false) {
144 $this->log("Preserve Data: Failed to insert wpstg_staging_sites to wpstg_tmp_data");
145 }
146
147 return true;
148 }
149
150 /** @return array */
151 protected function preserveRemoteStorages()
152 {
153 $storages = [];
154
155 /**
156 * Google Drive Options
157 * @see WPStaging\Pro\Backup\Storage\Storages\GoogleDrive\Auth::getOptionName for option name
158 */
159 $googleDrive = $this->getStagingSiteOption('wpstg_googledrive');
160
161 /**
162 * Amazon S3 Options
163 * @see WPStaging\Pro\Backup\Storage\Storages\Amazon\S3::getOptionName for option name
164 */
165 $amazonS3 = $this->getStagingSiteOption('wpstg_amazons3');
166
167 /**
168 * sFTP/FTP Options
169 * @see WPStaging\Pro\Backup\Storage\Storages\SFTP\Auth::getOptionName for option name
170 */
171 $sftp = $this->getStagingSiteOption('wpstg_sftp');
172
173 /**
174 * Digital Ocean Spaces Options
175 * @see WPStaging\Pro\Backup\Storage\Storages\DigitalOceanSpaces\Auth::getOptionName for option name
176 */
177 $digitalOceanSpaces = $this->getStagingSiteOption('wpstg_digitalocean-spaces');
178
179 /**
180 * Wasabi S3 Options
181 * @see WPStaging\Pro\Backup\Storage\Storages\Wasabi\Auth::getOptionName for option name
182 */
183 $wasabiS3 = $this->getStagingSiteOption('wpstg_wasabi');
184
185 /**
186 * Generic S3 / Other S3 Compat Options.
187 * @see WPStaging\Pro\Backup\Storage\Storages\GenericS3\Auth::getOptionName for option name
188 */
189 $genericS3 = $this->getStagingSiteOption('wpstg_generic-s3');
190
191 if ($googleDrive === false) {
192 $this->log("Preserve Data: Failed to get Google Drive Settings");
193 } else {
194 $storages['googleDrive'] = $googleDrive;
195 }
196
197 if ($amazonS3 === false) {
198 $this->log("Preserve Data: Failed to get Amazon S3 Settings");
199 } else {
200 $storages['amazonS3'] = $amazonS3;
201 }
202
203 if ($sftp === false) {
204 $this->log("Preserve Data: Failed to get sFTP/FTP Settings");
205 } else {
206 $storages['sftp'] = $sftp;
207 }
208
209 if ($digitalOceanSpaces === false) {
210 $this->log("Preserve Data: Failed to get Digital Ocean Spaces Settings");
211 } else {
212 $storages['digitalOceanSpaces'] = $digitalOceanSpaces;
213 }
214
215 if ($wasabiS3 === false) {
216 $this->log("Preserve Data: Failed to get Wasabi S3 Settings");
217 } else {
218 $storages['wasabiS3'] = $wasabiS3;
219 }
220
221 if ($genericS3 === false) {
222 $this->log("Preserve Data: Failed to get Generic S3 Settings");
223 } else {
224 $storages['genericS3'] = $genericS3;
225 }
226
227 return $storages;
228 }
229
230 /**
231 * @param string $optionName
232 *
233 * @return string|null
234 */
235 protected function getStagingSiteOption($optionName)
236 {
237 return $this->stagingDb->get_var(
238 $this->stagingDb->prepare(
239 "SELECT `option_value` FROM " . $this->stagingPrefix . "options WHERE `option_name` = %s",
240 $optionName
241 )
242 );
243 }
244
245 /**
246 * Check if table exists
247 * @param string $table
248 * @return bool
249 */
250 private function tableExists($table)
251 {
252 return !($table != $this->stagingDb->get_var("SHOW TABLES LIKE '{$table}'"));
253 }
254 }
255