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