Upgrade.php
374 lines
| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * Upgrade Class |
| 5 | * This must be loaded on every page init to ensure all settings are |
| 6 | * adjusted correctly and to run any upgrade process if necessary. |
| 7 | */ |
| 8 | |
| 9 | namespace WPStaging\Backend\Upgrade; |
| 10 | |
| 11 | use WPStaging\Core\Utils\IISWebConfig; |
| 12 | use WPStaging\Core\Utils\Htaccess; |
| 13 | use WPStaging\Core\WPStaging; |
| 14 | use WPStaging\Framework\BackgroundProcessing\Queue; |
| 15 | use WPStaging\Framework\Staging\Sites; |
| 16 | use WPStaging\Backup\BackupScheduler; |
| 17 | |
| 18 | // No Direct Access |
| 19 | if (!defined("WPINC")) { |
| 20 | die; |
| 21 | } |
| 22 | |
| 23 | class Upgrade |
| 24 | { |
| 25 | /** |
| 26 | * @var string |
| 27 | */ |
| 28 | const OPTION_UPGRADE_DATE = 'wpstg_free_upgrade_date'; |
| 29 | |
| 30 | /** |
| 31 | * @var string |
| 32 | */ |
| 33 | const OPTION_INSTALL_DATE = 'wpstg_free_install_date'; |
| 34 | |
| 35 | /** |
| 36 | * This is not an actual existing email to use and should be removed from our dev env and replaced with dev@wp-staging.local. |
| 37 | * Once changed, remove this constant and related code. |
| 38 | * @var string |
| 39 | */ |
| 40 | const WPSTG_DEV_EMAIL = 'dev@wp-staging.com'; |
| 41 | |
| 42 | /** |
| 43 | * Previous Version number |
| 44 | * @var string |
| 45 | */ |
| 46 | private $previousVersion; |
| 47 | |
| 48 | /** |
| 49 | * Global settings |
| 50 | * @var object |
| 51 | */ |
| 52 | private $settings; |
| 53 | |
| 54 | /** |
| 55 | * db object |
| 56 | * @var object |
| 57 | */ |
| 58 | private $db; |
| 59 | |
| 60 | /** |
| 61 | * @var Sites |
| 62 | */ |
| 63 | private $stagingSitesHelper; |
| 64 | |
| 65 | public function __construct() |
| 66 | { |
| 67 | // Previous version |
| 68 | $this->previousVersion = preg_replace('/[^0-9.].*/', '', get_option('wpstg_version')); |
| 69 | |
| 70 | $this->settings = (object) get_option("wpstg_settings", []); |
| 71 | |
| 72 | // db |
| 73 | $this->db = WPStaging::getInstance()->get("wpdb"); |
| 74 | |
| 75 | /** @var Sites */ |
| 76 | $this->stagingSitesHelper = WPStaging::make(Sites::class); |
| 77 | } |
| 78 | |
| 79 | /** |
| 80 | * @return void |
| 81 | */ |
| 82 | public function doUpgrade() |
| 83 | { |
| 84 | $this->upgrade2_0_3(); |
| 85 | $this->upgrade2_1_2(); |
| 86 | $this->upgrade2_2_0(); |
| 87 | $this->upgrade2_4_4(); |
| 88 | $this->upgrade2_5_9(); |
| 89 | $this->upgrade2_8_7(); |
| 90 | $this->upgrade3_0_7(); |
| 91 | $this->upgrade3_8_1(); |
| 92 | |
| 93 | $this->setVersion(); |
| 94 | } |
| 95 | |
| 96 | /** |
| 97 | * Enable backup notification by default |
| 98 | * @return void |
| 99 | */ |
| 100 | private function upgrade3_8_1() // phpcs:ignore |
| 101 | { |
| 102 | // Early bail: Previous version is greater than 3.8.1 |
| 103 | if (version_compare($this->previousVersion, '3.8.1', '>')) { |
| 104 | return; |
| 105 | } |
| 106 | |
| 107 | if (!class_exists('WPStaging\Backup\BackupScheduler')) { |
| 108 | return; |
| 109 | } |
| 110 | |
| 111 | $optionBackupScheduleReportEmail = get_option(BackupScheduler::OPTION_BACKUP_SCHEDULE_REPORT_EMAIL); |
| 112 | if (empty($optionBackupScheduleReportEmail) || !filter_var($optionBackupScheduleReportEmail, FILTER_VALIDATE_EMAIL)) { |
| 113 | $wpstgSettings = (array)get_option('wpstg_settings'); |
| 114 | |
| 115 | if (!empty($wpstgSettings['schedulesReportEmail'])) { |
| 116 | $optionBackupScheduleReportEmail = $wpstgSettings['schedulesReportEmail']; |
| 117 | } else { |
| 118 | $userObject = wp_get_current_user(); |
| 119 | if (is_object($userObject) && !empty($userObject->user_email)) { |
| 120 | $optionBackupScheduleReportEmail = $userObject->user_email; |
| 121 | } |
| 122 | } |
| 123 | |
| 124 | // If from wpstg_settings or current user email |
| 125 | if ($optionBackupScheduleReportEmail === self::WPSTG_DEV_EMAIL) { |
| 126 | $optionBackupScheduleReportEmail = ''; |
| 127 | update_option(BackupScheduler::OPTION_BACKUP_SCHEDULE_ERROR_REPORT, ''); |
| 128 | } |
| 129 | |
| 130 | update_option(BackupScheduler::OPTION_BACKUP_SCHEDULE_REPORT_EMAIL, $optionBackupScheduleReportEmail); |
| 131 | } |
| 132 | |
| 133 | // boolean false: Default value if the option does not exist/created yet |
| 134 | if (get_option(BackupScheduler::OPTION_BACKUP_SCHEDULE_ERROR_REPORT) === false && !empty($optionBackupScheduleReportEmail)) { |
| 135 | update_option(BackupScheduler::OPTION_BACKUP_SCHEDULE_ERROR_REPORT, 'true'); |
| 136 | } |
| 137 | } |
| 138 | |
| 139 | /** |
| 140 | * Add response field to queue table if not exist |
| 141 | * @return void |
| 142 | */ |
| 143 | private function upgrade3_0_7() // phpcs:ignore |
| 144 | { |
| 145 | // Early bail: Previous version is greater than 3.0.6 |
| 146 | if (version_compare($this->previousVersion, '3.0.6', '>')) { |
| 147 | return; |
| 148 | } |
| 149 | |
| 150 | $queueUtil = WPStaging::make(Queue::class); |
| 151 | $queueUtil->maybeAddResponseColumnToTable(); |
| 152 | } |
| 153 | |
| 154 | /** |
| 155 | * Move existing staging sites to new option defined in Sites::STAGING_SITES_OPTION |
| 156 | * @return void |
| 157 | */ |
| 158 | private function upgrade2_8_7() // phpcs:ignore |
| 159 | { |
| 160 | $this->stagingSitesHelper->addMissingCloneNameUpgradeStructure(); |
| 161 | $this->stagingSitesHelper->upgradeStagingSitesOption(); |
| 162 | } |
| 163 | |
| 164 | /** |
| 165 | * Fix array keys of staging sites |
| 166 | * @return void |
| 167 | */ |
| 168 | private function upgrade2_5_9() // phpcs:ignore |
| 169 | { |
| 170 | // Previous version lower than 2.5.9 |
| 171 | if (version_compare($this->previousVersion, '2.5.9', '<')) { |
| 172 | // Current options |
| 173 | $sites = $this->stagingSitesHelper->tryGettingStagingSites(); |
| 174 | |
| 175 | $new = []; |
| 176 | |
| 177 | // Fix keys. Replace white spaces with dash character |
| 178 | foreach ($sites as $oldKey => $site) { |
| 179 | $key = preg_replace("#\W+#", '-', strtolower($oldKey)); |
| 180 | $new[$key] = $sites[$oldKey]; |
| 181 | } |
| 182 | |
| 183 | if (!empty($new)) { |
| 184 | $this->stagingSitesHelper->updateStagingSites($new); |
| 185 | } |
| 186 | } |
| 187 | } |
| 188 | |
| 189 | /** |
| 190 | * @return void |
| 191 | */ |
| 192 | private function upgrade2_4_4() // phpcs:ignore |
| 193 | { |
| 194 | // Previous version lower than 2.4.4 |
| 195 | if (version_compare($this->previousVersion, '2.4.4', '<')) { |
| 196 | // Add htaccess to wp staging uploads folder |
| 197 | $htaccess = new Htaccess(); |
| 198 | $htaccess->create(trailingslashit(WPStaging::getContentDir()) . '.htaccess'); |
| 199 | $htaccess->create(trailingslashit(WPStaging::getContentDir()) . 'logs/.htaccess'); |
| 200 | |
| 201 | // Add litespeed htaccess to wp root folder |
| 202 | if (extension_loaded('litespeed')) { |
| 203 | $htaccess->createLitespeed(ABSPATH . '.htaccess'); |
| 204 | } |
| 205 | |
| 206 | // create web.config file for IIS in wp staging uploads folder |
| 207 | $webconfig = new IISWebConfig(); |
| 208 | $webconfig->create(trailingslashit(WPStaging::getContentDir()) . 'web.config'); |
| 209 | $webconfig->create(trailingslashit(WPStaging::getContentDir()) . 'logs/web.config'); |
| 210 | } |
| 211 | } |
| 212 | |
| 213 | /** |
| 214 | * Upgrade method 2.2.0 |
| 215 | * @return void |
| 216 | */ |
| 217 | public function upgrade2_2_0() // phpcs:ignore |
| 218 | { |
| 219 | // Previous version lower than 2.2.0 |
| 220 | if (version_compare($this->previousVersion, '2.2.0', '<')) { |
| 221 | $this->upgradeElements(); |
| 222 | } |
| 223 | } |
| 224 | |
| 225 | /** |
| 226 | * Add missing elements |
| 227 | * @return void |
| 228 | */ |
| 229 | private function upgradeElements() |
| 230 | { |
| 231 | // Current options |
| 232 | $sites = $this->stagingSitesHelper->tryGettingStagingSites(); |
| 233 | |
| 234 | if ($sites === false || count($sites) === 0) { |
| 235 | return; |
| 236 | } |
| 237 | |
| 238 | // Check if key prefix is missing and add it |
| 239 | foreach ($sites as $key => $value) { |
| 240 | if (empty($sites[$key]['directoryName'])) { |
| 241 | continue; |
| 242 | } |
| 243 | |
| 244 | !empty($sites[$key]['prefix']) ? |
| 245 | $sites[$key]['prefix'] = $value['prefix'] : |
| 246 | $sites[$key]['prefix'] = $this->getStagingPrefix($sites[$key]['directoryName']); |
| 247 | } |
| 248 | |
| 249 | if (count($sites) > 0) { |
| 250 | $this->stagingSitesHelper->updateStagingSites($sites); |
| 251 | } |
| 252 | } |
| 253 | |
| 254 | /** |
| 255 | * Check and return prefix of the staging site |
| 256 | * @param string $directory |
| 257 | * @return string |
| 258 | */ |
| 259 | private function getStagingPrefix($directory) |
| 260 | { |
| 261 | // Try to get staging prefix from wp-config.php of staging site |
| 262 | $path = ABSPATH . $directory . "/wp-config.php"; |
| 263 | |
| 264 | if (($content = @file_get_contents($path)) === false) { |
| 265 | $prefix = ""; |
| 266 | } else { |
| 267 | // Get prefix from wp-config.php |
| 268 | preg_match("/table_prefix\s*=\s*'(\w*)';/", $content, $matches); |
| 269 | |
| 270 | if (!empty($matches[1])) { |
| 271 | $prefix = $matches[1]; |
| 272 | } else { |
| 273 | $prefix = ""; |
| 274 | } |
| 275 | } |
| 276 | |
| 277 | // return result: Check if staging prefix is the same as the live prefix |
| 278 | if ($this->db->prefix != $prefix) { |
| 279 | return $prefix; |
| 280 | } else { |
| 281 | return ""; |
| 282 | } |
| 283 | } |
| 284 | |
| 285 | /** |
| 286 | * Upgrade method 2.0.3 |
| 287 | * @return void |
| 288 | */ |
| 289 | public function upgrade2_0_3() // phpcs:ignore |
| 290 | { |
| 291 | // Previous version lower than 2.0.2 |
| 292 | if (version_compare($this->previousVersion, '2.0.2', '<')) { |
| 293 | $this->initialInstall(); |
| 294 | $this->upgradeNotices(); |
| 295 | } |
| 296 | } |
| 297 | |
| 298 | /** |
| 299 | * Upgrade method 2.1.2 |
| 300 | * Sanitize the clone key value. |
| 301 | * @return void |
| 302 | */ |
| 303 | private function upgrade2_1_2() // phpcs:ignore |
| 304 | { |
| 305 | if ($this->previousVersion === false || version_compare($this->previousVersion, '2.1.2', '<')) { |
| 306 | // Current options |
| 307 | $clones = $this->stagingSitesHelper->tryGettingStagingSites(); |
| 308 | |
| 309 | foreach ($clones as $key => $value) { |
| 310 | unset($clones[$key]); |
| 311 | $clones[preg_replace("#\W+#", '-', strtolower($key))] = $value; |
| 312 | } |
| 313 | |
| 314 | if (!empty($clones)) { |
| 315 | $this->stagingSitesHelper->updateStagingSites($clones); |
| 316 | } |
| 317 | } |
| 318 | } |
| 319 | |
| 320 | /** |
| 321 | * Upgrade routine for new install |
| 322 | * @return void |
| 323 | */ |
| 324 | private function initialInstall() |
| 325 | { |
| 326 | // Write some default vars |
| 327 | add_option('wpstg_installDate', date('Y-m-d h:i:s')); // Common install date for free or pro version - deprecated. Remove 2023 |
| 328 | add_option(self::OPTION_INSTALL_DATE, date('Y-m-d h:i:s')); |
| 329 | $this->settings->optimizer = "1"; |
| 330 | update_option('wpstg_settings', $this->settings); |
| 331 | } |
| 332 | |
| 333 | /** |
| 334 | * Write new version number into db |
| 335 | * @return bool |
| 336 | */ |
| 337 | private function setVersion() |
| 338 | { |
| 339 | // Check if version number in DB is lower than version number in current plugin |
| 340 | if (version_compare($this->previousVersion, WPStaging::getVersion(), '<')) { |
| 341 | // Update Version number |
| 342 | update_option('wpstg_version', preg_replace('/[^0-9.].*/', '', WPStaging::getVersion())); |
| 343 | // Update "upgraded from" version number |
| 344 | update_option('wpstg_version_upgraded_from', preg_replace('/[^0-9.].*/', '', $this->previousVersion)); |
| 345 | // Update the time version upgraded at |
| 346 | update_option(self::OPTION_UPGRADE_DATE, date('Y-m-d H:i')); |
| 347 | |
| 348 | return true; |
| 349 | } |
| 350 | |
| 351 | return false; |
| 352 | } |
| 353 | |
| 354 | /** |
| 355 | * Upgrade Notices db options from wpstg 1.3 -> 2.0.1 |
| 356 | * Fix some logical db options |
| 357 | * @return void |
| 358 | */ |
| 359 | private function upgradeNotices() |
| 360 | { |
| 361 | $poll = get_option("wpstg_start_poll", false); |
| 362 | $beta = get_option("wpstg_hide_beta", false); |
| 363 | $rating = get_option("wpstg_RatingDiv", false); |
| 364 | |
| 365 | if ($beta && $beta === "yes") { |
| 366 | update_option('wpstg_beta', 'no'); |
| 367 | } |
| 368 | |
| 369 | if ($rating && $rating === 'yes') { |
| 370 | update_option('wpstg_rating', 'no'); |
| 371 | } |
| 372 | } |
| 373 | } |
| 374 |