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