PluginProbe ʕ •ᴥ•ʔ
WP STAGING – WordPress Backup, Restore, Migration & Clone / 3.3.1
WP STAGING – WordPress Backup, Restore, Migration & Clone v3.3.1
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 2 years ago Job.php 2 years ago JobExecutable.php 2 years ago Logs.php 3 years ago PreserveDataFirstStep.php 3 years ago PreserveDataSecondStep.php 3 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
210 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 // Get wpstg_tmp_data from production database
65 $result = $this->productionDb->get_var(
66 $this->productionDb->prepare(
67 "SELECT `option_value` FROM " . $this->productionDb->prefix . "options WHERE `option_name` = %s",
68 "wpstg_tmp_data"
69 )
70 );
71
72 // Nothing to do
73 if (!$result) {
74 return true;
75 }
76
77 // Make sure this is compatible with Free Version
78 // @see \WPStaging\Backup\BackupScheduler::OPTION_BACKUP_SCHEDULES
79 $backupSchedulesOption = 'wpstg_backup_schedules';
80
81 // Delete wpstg_tmp_data from the production site
82 $deleteTmpData = $this->productionDb->query(
83 $this->productionDb->prepare("DELETE FROM " . $this->productionDb->prefix . "options WHERE `option_name` = %s", "wpstg_tmp_data")
84 );
85
86 if ($deleteTmpData === false) {
87 $this->log("Preserve Data Second Step: Failed to delete wpstg_tmp_data from the production site");
88 }
89
90 $this->preservedData = maybe_unserialize($result);
91
92 // Preserve wpstg_staging_sites in staging database
93 $this->preserveStagingOption(Sites::STAGING_SITES_OPTION, $this->preservedData->stagingSites, 'existing clones');
94
95 // Preserve wpstg_settings in staging database
96 $this->preserveStagingOption("wpstg_settings", $this->preservedData->settings, 'settings');
97
98 // Preserve wpstg_clone_options in staging database
99 $this->preserveStagingOption(CloneOptions::WPSTG_CLONE_SETTINGS_KEY, $this->preservedData->cloneOptions, 'clone options');
100
101 // Preserve backup schedules
102 $this->preserveStagingOption($backupSchedulesOption, $this->preservedData->backupSchedules, 'backup schedules');
103
104 if ($this->propertyExists('googleDrive')) {
105 $this->preserveStagingOption('wpstg_googledrive', $this->preservedData->googleDrive, 'Google Drive settings');
106 } else {
107 $this->deleteStagingSiteOption('wpstg_googledrive');
108 }
109
110 if ($this->propertyExists('amazonS3')) {
111 $this->preserveStagingOption('wpstg_amazons3', $this->preservedData->amazonS3, 'Amazon S3 settings');
112 } else {
113 $this->deleteStagingSiteOption('wpstg_amazons3');
114 }
115
116 if ($this->propertyExists('sftp')) {
117 $this->preserveStagingOption('wpstg_sftp', $this->preservedData->sftp, 'sFTP/FTP settings');
118 } else {
119 $this->deleteStagingSiteOption('wpstg_sftp');
120 }
121
122 if ($this->propertyExists('digitalOceanSpaces')) {
123 $this->preserveStagingOption('wpstg_digitalocean-spaces', $this->preservedData->digitalOceanSpaces, 'Digital Ocean Spaces settings');
124 } else {
125 $this->deleteStagingSiteOption('wpstg_digitalocean-spaces');
126 }
127
128 if ($this->propertyExists('wasabiS3')) {
129 $this->preserveStagingOption('wpstg_wasabi', $this->preservedData->wasabiS3, 'Wasabi S3 settings');
130 } else {
131 $this->deleteStagingSiteOption('wpstg_wasabi');
132 }
133
134 if ($this->propertyExists('genericS3')) {
135 $this->preserveStagingOption('wpstg_generic-s3', $this->preservedData->genericS3, 'S3 Compat settings');
136 } else {
137 $this->deleteStagingSiteOption('wpstg_generic-s3');
138 }
139
140 return true;
141 }
142
143 /**
144 * @param string $optionName
145 * @param string $optionValue
146 * @param bool $autoload
147 */
148 protected function preserveStagingOption($optionName, $optionValue, $logEntity, $autoload = false)
149 {
150 $isDeleted = $this->deleteStagingSiteOption($optionName);
151
152 if ($isDeleted === false) {
153 $this->log("Preserve Data Second Step: Failed to delete " . $optionName . " from the staging site");
154 }
155
156 $isInserted = $this->insertOptionIntoStagingSite($optionName, $optionValue, $autoload);
157
158 if ($isInserted === false) {
159 $this->log("Preserve Data Second Step: Failed to insert preserved " . $logEntity . " into " . $optionName . " of the staging site");
160 }
161 }
162
163 /**
164 * @param string $optionName
165 *
166 * @return bool|int Number of rows affected. Boolean false on error
167 */
168 protected function deleteStagingSiteOption($optionName)
169 {
170 return $this->stagingDb->query(
171 $this->stagingDb->prepare("DELETE FROM " . $this->stagingPrefix . "options WHERE `option_name` = %s", $optionName)
172 );
173 }
174
175 /**
176 * @param string $optionName
177 * @param string $optionValue
178 * @param bool $autoload
179 *
180 * @return bool|int Number of rows affected. Boolean false on error
181 */
182 protected function insertOptionIntoStagingSite($optionName, $optionValue, $autoload = false)
183 {
184 $autoload = $autoload ? 'yes' : 'no';
185
186 return $this->stagingDb->query(
187 $this->stagingDb->prepare(
188 "INSERT INTO `" . $this->stagingPrefix . "options` ( `option_id`, `option_name`, `option_value`, `autoload` ) VALUES ( NULL , %s, %s, %s )",
189 $optionName,
190 $optionValue,
191 $autoload
192 )
193 );
194 }
195
196 /**
197 * @param string $property
198 *
199 * @return bool
200 */
201 protected function propertyExists($property)
202 {
203 if (!property_exists($this->preservedData, $property)) {
204 return false;
205 }
206
207 return !empty($this->preservedData->{$property});
208 }
209 }
210