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