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