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 / PreserveDataSecondStep.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
PreserveDataSecondStep.php
225 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 * Copy wpstg_tmp_data back to wpstg_staging_sites after cloning with class::PreserveDataSecondStep
12 * @package WPStaging\Backend\Modules\Jobs
13 */
14 class PreserveDataSecondStep extends JobExecutable
15 {
16 /** @var \wpdb */
17 private $stagingDb;
18
19 /** @var \wpdb */
20 private $productionDb;
21
22 /** @var string */
23 private $stagingPrefix;
24
25 /** @var object */
26 private $preservedData;
27
28 protected function calculateTotalSteps()
29 {
30 $this->options->totalSteps = 1;
31 }
32
33 /** @return object */
34 public function start()
35 {
36 $this->run();
37 $this->saveOptions();
38
39 return (object)$this->response;
40 }
41
42 /** @return false */
43 protected function execute()
44 {
45 $db = new SourceDatabase($this->options);
46
47 $this->stagingDb = $db->getDatabase();
48 $this->productionDb = WPStaging::getInstance()->get("wpdb");
49 $this->stagingPrefix = $this->options->prefix;
50
51 if ($db->isExternalDatabase()) {
52 $this->stagingPrefix = $this->options->databasePrefix;
53 }
54
55 $this->copyToStaging();
56 $this->prepareResponse(true, true);
57
58 return false;
59 }
60
61 /** @return true */
62 public function copyToStaging()
63 {
64 // Early bail if table doesn't exist
65 if (!$this->tableExists($this->stagingPrefix . "options")) {
66 return true;
67 }
68
69 // Get wpstg_tmp_data from production database
70 $result = $this->productionDb->get_var(
71 $this->productionDb->prepare(
72 "SELECT `option_value` FROM " . $this->productionDb->prefix . "options WHERE `option_name` = %s",
73 "wpstg_tmp_data"
74 )
75 );
76
77 // Nothing to do
78 if (!$result) {
79 return true;
80 }
81
82 // Make sure this is compatible with Free Version
83 // @see \WPStaging\Backup\BackupScheduler::OPTION_BACKUP_SCHEDULES
84 $backupSchedulesOption = 'wpstg_backup_schedules';
85
86 // Delete wpstg_tmp_data from the production site
87 $deleteTmpData = $this->productionDb->query(
88 $this->productionDb->prepare("DELETE FROM " . $this->productionDb->prefix . "options WHERE `option_name` = %s", "wpstg_tmp_data")
89 );
90
91 if ($deleteTmpData === false) {
92 $this->log("Preserve Data Second Step: Failed to delete wpstg_tmp_data from the production site");
93 }
94
95 $this->preservedData = maybe_unserialize($result);
96
97 // Preserve wpstg_staging_sites in staging database
98 $this->preserveStagingOption(Sites::STAGING_SITES_OPTION, $this->preservedData->stagingSites, 'existing clones');
99
100 // Preserve wpstg_settings in staging database
101 $this->preserveStagingOption("wpstg_settings", $this->preservedData->settings, 'settings');
102
103 // Preserve wpstg_clone_options in staging database
104 $this->preserveStagingOption(CloneOptions::WPSTG_CLONE_SETTINGS_KEY, $this->preservedData->cloneOptions, 'clone options');
105
106 // Preserve backup schedules
107 $this->preserveStagingOption($backupSchedulesOption, $this->preservedData->backupSchedules, 'backup schedules');
108
109 if ($this->propertyExists('googleDrive')) {
110 $this->preserveStagingOption('wpstg_googledrive', $this->preservedData->googleDrive, 'Google Drive settings');
111 } else {
112 $this->deleteStagingSiteOption('wpstg_googledrive');
113 }
114
115 if ($this->propertyExists('amazonS3')) {
116 $this->preserveStagingOption('wpstg_amazons3', $this->preservedData->amazonS3, 'Amazon S3 settings');
117 } else {
118 $this->deleteStagingSiteOption('wpstg_amazons3');
119 }
120
121 if ($this->propertyExists('sftp')) {
122 $this->preserveStagingOption('wpstg_sftp', $this->preservedData->sftp, 'sFTP/FTP settings');
123 } else {
124 $this->deleteStagingSiteOption('wpstg_sftp');
125 }
126
127 if ($this->propertyExists('digitalOceanSpaces')) {
128 $this->preserveStagingOption('wpstg_digitalocean-spaces', $this->preservedData->digitalOceanSpaces, 'Digital Ocean Spaces settings');
129 } else {
130 $this->deleteStagingSiteOption('wpstg_digitalocean-spaces');
131 }
132
133 if ($this->propertyExists('wasabiS3')) {
134 $this->preserveStagingOption('wpstg_wasabi', $this->preservedData->wasabiS3, 'Wasabi S3 settings');
135 } else {
136 $this->deleteStagingSiteOption('wpstg_wasabi');
137 }
138
139 if ($this->propertyExists('genericS3')) {
140 $this->preserveStagingOption('wpstg_generic-s3', $this->preservedData->genericS3, 'S3 Compat settings');
141 } else {
142 $this->deleteStagingSiteOption('wpstg_generic-s3');
143 }
144
145 return true;
146 }
147
148 /**
149 * @param string $optionName
150 * @param string $optionValue
151 * @param bool $autoload
152 */
153 protected function preserveStagingOption($optionName, $optionValue, $logEntity, $autoload = false)
154 {
155 $isDeleted = $this->deleteStagingSiteOption($optionName);
156
157 if ($isDeleted === false) {
158 $this->log("Preserve Data Second Step: Failed to delete " . $optionName . " from the staging site");
159 }
160
161 $isInserted = $this->insertOptionIntoStagingSite($optionName, $optionValue, $autoload);
162
163 if ($isInserted === false) {
164 $this->log("Preserve Data Second Step: Failed to insert preserved " . $logEntity . " into " . $optionName . " of the staging site");
165 }
166 }
167
168 /**
169 * @param string $optionName
170 *
171 * @return bool|int Number of rows affected. Boolean false on error
172 */
173 protected function deleteStagingSiteOption($optionName)
174 {
175 return $this->stagingDb->query(
176 $this->stagingDb->prepare("DELETE FROM " . $this->stagingPrefix . "options WHERE `option_name` = %s", $optionName)
177 );
178 }
179
180 /**
181 * @param string $optionName
182 * @param string $optionValue
183 * @param bool $autoload
184 *
185 * @return bool|int Number of rows affected. Boolean false on error
186 */
187 protected function insertOptionIntoStagingSite($optionName, $optionValue, $autoload = false)
188 {
189 $autoload = $autoload ? 'yes' : 'no';
190
191 return $this->stagingDb->query(
192 $this->stagingDb->prepare(
193 "INSERT INTO `" . $this->stagingPrefix . "options` ( `option_id`, `option_name`, `option_value`, `autoload` ) VALUES ( NULL , %s, %s, %s )",
194 $optionName,
195 $optionValue,
196 $autoload
197 )
198 );
199 }
200
201 /**
202 * @param string $property
203 *
204 * @return bool
205 */
206 protected function propertyExists($property)
207 {
208 if (!property_exists($this->preservedData, $property)) {
209 return false;
210 }
211
212 return !empty($this->preservedData->{$property});
213 }
214
215 /**
216 * Check if table exists
217 * @param string $table
218 * @return bool
219 */
220 private function tableExists($table)
221 {
222 return !($table != $this->stagingDb->get_var("SHOW TABLES LIKE '{$table}'"));
223 }
224 }
225