PluginProbe ʕ •ᴥ•ʔ
WP STAGING – WordPress Backups, Restore, Migration & Clone / 3.6.0
WP STAGING – WordPress Backups, Restore, Migration & Clone v3.6.0
4.11.2 4.11.1 4.11.0 4.10.0 4.9.5 4.9.4 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 2 years ago
Upgrade.php
318 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 /**
65 * @return void
66 */
67 public function doUpgrade()
68 {
69 $this->upgrade2_0_3();
70 $this->upgrade2_1_2();
71 $this->upgrade2_2_0();
72 $this->upgrade2_4_4();
73 $this->upgrade2_5_9();
74 $this->upgrade2_8_7();
75 $this->upgrade3_0_7();
76
77 $this->setVersion();
78 }
79
80 /**
81 * Add response field to queue table if not exist
82 * @return void
83 */
84 private function upgrade3_0_7()
85 {
86 // Early bail: Previous version is greater than 3.0.6
87 if (version_compare($this->previousVersion, '3.0.6', '>')) {
88 return;
89 }
90
91 $queueUtil = WPStaging::make(Queue::class);
92 $queueUtil->maybeAddResponseColumnToTable();
93 }
94
95 /**
96 * Move existing staging sites to new option defined in Sites::STAGING_SITES_OPTION
97 * @return void
98 */
99 private function upgrade2_8_7()
100 {
101 $this->stagingSitesHelper->addMissingCloneNameUpgradeStructure();
102 $this->stagingSitesHelper->upgradeStagingSitesOption();
103 }
104
105 /**
106 * Fix array keys of staging sites
107 * @return void
108 */
109 private function upgrade2_5_9()
110 {
111 // Previous version lower than 2.5.9
112 if (version_compare($this->previousVersion, '2.5.9', '<')) {
113
114 // Current options
115 $sites = $this->stagingSitesHelper->tryGettingStagingSites();
116
117 $new = [];
118
119 // Fix keys. Replace white spaces with dash character
120 foreach ($sites as $oldKey => $site) {
121 $key = preg_replace("#\W+#", '-', strtolower($oldKey));
122 $new[$key] = $sites[$oldKey];
123 }
124
125 if (!empty($new)) {
126 $this->stagingSitesHelper->updateStagingSites($new);
127 }
128 }
129 }
130
131 /**
132 * @return void
133 */
134 private function upgrade2_4_4()
135 {
136 // Previous version lower than 2.4.4
137 if (version_compare($this->previousVersion, '2.4.4', '<')) {
138 // Add htaccess to wp staging uploads folder
139 $htaccess = new Htaccess();
140 $htaccess->create(trailingslashit(WPStaging::getContentDir()) . '.htaccess');
141 $htaccess->create(trailingslashit(WPStaging::getContentDir()) . 'logs/.htaccess');
142
143 // Add litespeed htaccess to wp root folder
144 if (extension_loaded('litespeed')) {
145 $htaccess->createLitespeed(ABSPATH . '.htaccess');
146 }
147
148 // create web.config file for IIS in wp staging uploads folder
149 $webconfig = new IISWebConfig();
150 $webconfig->create(trailingslashit(WPStaging::getContentDir()) . 'web.config');
151 $webconfig->create(trailingslashit(WPStaging::getContentDir()) . 'logs/web.config');
152 }
153 }
154
155 /**
156 * Upgrade method 2.2.0
157 * @return void
158 */
159 public function upgrade2_2_0()
160 {
161 // Previous version lower than 2.2.0
162 if (version_compare($this->previousVersion, '2.2.0', '<')) {
163 $this->upgradeElements();
164 }
165 }
166
167 /**
168 * Add missing elements
169 * @return void
170 */
171 private function upgradeElements()
172 {
173 // Current options
174 $sites = $this->stagingSitesHelper->tryGettingStagingSites();
175
176 if ($sites === false || count($sites) === 0) {
177 return;
178 }
179
180 // Check if key prefix is missing and add it
181 foreach ($sites as $key => $value) {
182 if (empty($sites[$key]['directoryName'])) {
183 continue;
184 }
185
186 //!empty( $sites[$key]['prefix'] ) ? $sites[$key]['prefix'] = $value['prefix'] : $sites[$key]['prefix'] = $key . '_';
187 !empty($sites[$key]['prefix']) ?
188 $sites[$key]['prefix'] = $value['prefix'] :
189 $sites[$key]['prefix'] = $this->getStagingPrefix($sites[$key]['directoryName']);
190 }
191
192 if (count($sites) > 0) {
193 $this->stagingSitesHelper->updateStagingSites($sites);
194 }
195 }
196
197 /**
198 * Check and return prefix of the staging site
199 * @param string $directory
200 * @return string
201 */
202 private function getStagingPrefix($directory)
203 {
204 // Try to get staging prefix from wp-config.php of staging site
205 $path = ABSPATH . $directory . "/wp-config.php";
206
207 if (($content = @file_get_contents($path)) === false) {
208 $prefix = "";
209 } else {
210 // Get prefix from wp-config.php
211 preg_match("/table_prefix\s*=\s*'(\w*)';/", $content, $matches);
212
213 if (!empty($matches[1])) {
214 $prefix = $matches[1];
215 } else {
216 $prefix = "";
217 }
218 }
219
220 // return result: Check if staging prefix is the same as the live prefix
221 if ($this->db->prefix != $prefix) {
222 return $prefix;
223 } else {
224 return "";
225 }
226 }
227
228 /**
229 * Upgrade method 2.0.3
230 * @return void
231 */
232 public function upgrade2_0_3()
233 {
234 // Previous version lower than 2.0.2
235 if (version_compare($this->previousVersion, '2.0.2', '<')) {
236 $this->initialInstall();
237 $this->upgradeNotices();
238 }
239 }
240
241 /**
242 * Upgrade method 2.1.2
243 * Sanitize the clone key value.
244 * @return void
245 */
246 private function upgrade2_1_2()
247 {
248 if ($this->previousVersion === false || version_compare($this->previousVersion, '2.1.2', '<')) {
249 // Current options
250 $clones = $this->stagingSitesHelper->tryGettingStagingSites();
251
252 foreach ($clones as $key => $value) {
253 unset($clones[$key]);
254 $clones[preg_replace("#\W+#", '-', strtolower($key))] = $value;
255 }
256
257 if (!empty($clones)) {
258 $this->stagingSitesHelper->updateStagingSites($clones);
259 }
260 }
261 }
262
263 /**
264 * Upgrade routine for new install
265 * @return void
266 */
267 private function initialInstall()
268 {
269 // Write some default vars
270 add_option('wpstg_installDate', date('Y-m-d h:i:s')); // Common install date for free or pro version - deprecated. Remove 2023
271 add_option(self::OPTION_INSTALL_DATE, date('Y-m-d h:i:s'));
272 $this->settings->optimizer = "1";
273 update_option('wpstg_settings', $this->settings);
274 }
275
276 /**
277 * Write new version number into db
278 * @return bool
279 */
280 private function setVersion()
281 {
282 // Check if version number in DB is lower than version number in current plugin
283 if (version_compare($this->previousVersion, WPStaging::getVersion(), '<')) {
284 // Update Version number
285 update_option('wpstg_version', preg_replace('/[^0-9.].*/', '', WPStaging::getVersion()));
286 // Update "upgraded from" version number
287 update_option('wpstg_version_upgraded_from', preg_replace('/[^0-9.].*/', '', $this->previousVersion));
288 // Update the time version upgraded at
289 update_option(self::OPTION_UPGRADE_DATE, date('Y-m-d H:i'));
290
291 return true;
292 }
293
294 return false;
295 }
296
297 /**
298 * Upgrade Notices db options from wpstg 1.3 -> 2.0.1
299 * Fix some logical db options
300 * @return void
301 */
302 private function upgradeNotices()
303 {
304 $poll = get_option("wpstg_start_poll", false);
305 $beta = get_option("wpstg_hide_beta", false);
306 $rating = get_option("wpstg_RatingDiv", false);
307
308 if ($beta && $beta === "yes") {
309 update_option('wpstg_beta', 'no');
310 }
311
312 if ($rating && $rating === 'yes') {
313 update_option('wpstg_rating', 'no');
314 }
315 }
316
317 }
318