PluginProbe
MainWP Dashboard: Self-hosted WordPress Management for Agencies / 5.2.1
MainWP Dashboard: Self-hosted WordPress Management for Agencies v5.2.1
6.2 6.1.8 6.1.7 6.1.6 6.1.5 6.1.4 6.1.3 6.1.2 6.1.1 6.1 6.0.12 6.0.11 4.6.0.1 5.0 5.0.1 5.0.2 5.0.3 5.0.3.1 5.0.3.2 5.1 5.1.1 5.2 5.2.1 5.2.2 5.3 All 153 releases
mainwp / class / class-mainwp-install.php

class-mainwp-install.php in MainWP Dashboard: Self-hosted WordPress Management for Agencies 5.2.1, at class/class-mainwp-install.php

627 lines 21.8 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * MainWP Install
4 *
5 * This file handles install MainWP DB.
6 *
7 * @package MainWP/Dashboard
8 */
9
10 namespace MainWP\Dashboard;
11
12 /**
13 * Class MainWP_Install
14 *
15 * @package MainWP\Dashboard
16 *
17 * @uses \MainWP\Dashboard\MainWP_DB_Base
18 */
19 class MainWP_Install extends MainWP_DB_Base { // phpcs:ignore Generic.Classes.OpeningBraceSameLine.ContentAfterBrace -- NOSONAR.
20
21 // phpcs:disable WordPress.DB.RestrictedFunctions, WordPress.DB.PreparedSQL.NotPrepared -- unprepared SQL ok, accessing the database directly to custom database functions.
22
23 /**
24 * Private variable to hold the database version info.
25 *
26 * @var string DB version info.
27 */
28 protected $mainwp_db_version = '9.0.0.12'; // NOSONAR - no IP.
29
30 /**
31 * Protected variable to hold the database option name.
32 *
33 * @var string DB version info.
34 */
35 protected $option_db_key = 'mainwp_db_version';
36
37 /**
38 * Private static variable to hold the single instance of the class.
39 *
40 * @static
41 *
42 * @var mixed Default null
43 */
44 private static $instance = null;
45
46 /**
47 * Method instance()
48 *
49 * Return public static instance.
50 *
51 * @static
52 * @return MainWP_DB
53 */
54 public static function instance() {
55 if ( null === static::$instance ) {
56 static::$instance = new self();
57 }
58
59 static::$instance->test_connection();
60
61 return static::$instance;
62 }
63
64 /**
65 * Method install()
66 *
67 * Installs the new DB.
68 *
69 * @return void
70 *
71 * @uses \MainWP\Dashboard\MainWP_Utility::update_option()
72 */
73 public function install() { // phpcs:ignore -- NOSONAR - complex function. Current complexity is the only way to achieve desired results, pull request solutions appreciated.
74 // get_site_option is multisite aware!
75 $currentVersion = get_site_option( $this->option_db_key );
76
77 if ( empty( $currentVersion ) ) {
78 update_option( 'mainwp_run_quick_setup', 'yes' );
79 MainWP_Utility::update_option( 'mainwp_enableLegacyBackupFeature', 0 );
80 } elseif ( false === get_option( 'mainwp_enableLegacyBackupFeature' ) ) {
81 MainWP_Utility::update_option( 'mainwp_enableLegacyBackupFeature', 1 );
82 }
83
84 if ( empty( $currentVersion ) || version_compare( $currentVersion, '8.8', '<' ) ) {
85 MainWP_Utility::update_option( 'mainwp_selected_theme', 'default' );
86 }
87
88 $rslt = static::instance()->query( "SHOW TABLES LIKE '" . $this->table_name( 'wp' ) . "'" );
89 if ( empty( static::num_rows( $rslt ) ) ) {
90 $currentVersion = false;
91 }
92
93 if ( $currentVersion === $this->mainwp_db_version ) {
94 return;
95 }
96
97 $this->pre_update_tables();
98
99 $charset_collate = $this->wpdb->get_charset_collate();
100
101 $sql = array();
102 $tbl = 'CREATE TABLE ' . $this->table_name( 'wp' ) . " (
103 id int(11) NOT NULL auto_increment,
104 userid int(11) NOT NULL,
105 adminname text NOT NULL,
106 name text NOT NULL,
107 url text NOT NULL,
108 pubkey text NOT NULL,
109 privkey text NOT NULL,
110 siteurl text NOT NULL,
111 ga_id text NOT NULL,
112 gas_id int(11) NOT NULL,
113 offline_checks_last int(11) NOT NULL,
114 offline_check_result int(11) NOT NULL,
115 http_response_code int(11) NOT NULL DEFAULT 0,
116 http_code_noticed tinyint(1) NOT NULL DEFAULT 1,
117 disable_status_check tinyint(1) NOT NULL DEFAULT 0,
118 disable_health_check tinyint(1) NOT NULL DEFAULT 0,
119 status_check_interval tinyint(1) NOT NULL DEFAULT 0,
120 health_threshold int(11) NOT NULL DEFAULT 0,
121 note text NOT NULL,
122 statsUpdate int(11) NOT NULL,
123 directories longtext NOT NULL,
124 plugin_upgrades longtext NOT NULL,
125 theme_upgrades longtext NOT NULL,
126 translation_upgrades longtext NOT NULL,
127 premium_upgrades longtext NOT NULL,
128 securityIssues longtext NOT NULL,
129 themes longtext NOT NULL,
130 ignored_themes longtext NOT NULL,
131 plugins longtext NOT NULL,
132 ignored_plugins longtext NOT NULL,
133 users longtext NOT NULL,
134 categories longtext NOT NULL,
135 pluginDir text NOT NULL,
136 automatic_update tinyint(1) NOT NULL,
137 backup_before_upgrade tinyint(1) NOT NULL DEFAULT 2,
138 mainwpdir tinyint(1) NOT NULL,
139 loadFilesBeforeZip tinyint(1) NOT NULL DEFAULT 1,
140 is_ignoreCoreUpdates tinyint(1) NOT NULL DEFAULT 0,
141 is_ignorePluginUpdates tinyint(1) NOT NULL DEFAULT 0,
142 is_ignoreThemeUpdates tinyint(1) NOT NULL DEFAULT 0,
143 verify_certificate tinyint(1) NOT NULL DEFAULT 1,
144 force_use_ipv4 tinyint(1) NOT NULL DEFAULT 0,
145 ssl_version tinyint(1) NOT NULL DEFAULT 0,
146 ip text NOT NULL DEFAULT '',
147 uniqueId text NOT NULL,
148 maximumFileDescriptorsOverride tinyint(1) NOT NULL DEFAULT 0,
149 maximumFileDescriptorsAuto tinyint(1) NOT NULL DEFAULT 1,
150 maximumFileDescriptors int(11) NOT NULL DEFAULT 150,
151 http_user text NOT NULL DEFAULT '',
152 http_pass text NOT NULL DEFAULT '',
153 wpe tinyint(1) NOT NULL,
154 is_staging tinyint(1) NOT NULL DEFAULT 0,
155 client_id int(11) NOT NULL DEFAULT 0,
156 `suspended` tinyint(1) NOT NULL DEFAULT 0,
157 KEY idx_userid (userid)";
158 if ( empty( $currentVersion ) ) {
159 $tbl .= ',
160 PRIMARY KEY (id) ';
161 }
162 $tbl .= ') ' . $charset_collate;
163 $sql[] = $tbl;
164
165 $tbl = 'CREATE TABLE ' . $this->table_name( 'wp_sync' ) . " (
166 sync_id int(11) NOT NULL auto_increment,
167 wpid int(11) NOT NULL,
168 version text NOT NULL DEFAULT '',
169 sync_errors longtext NOT NULL DEFAULT '',
170 uptodate longtext NOT NULL DEFAULT '',
171 dtsAutomaticSync int(11) NOT NULL DEFAULT 0,
172 dtsAutomaticSyncStart int(11) NOT NULL DEFAULT 0,
173 dtsSync int(11) NOT NULL DEFAULT 0,
174 dtsSyncStart int(11) NOT NULL DEFAULT 0,
175 totalsize int(11) NOT NULL DEFAULT 0,
176 dbsize int(11) NOT NULL DEFAULT 0,
177 extauth text NOT NULL DEFAULT '',
178 last_post_gmt int(11) NOT NULL DEFAULT 0,
179 health_value int(11) NOT NULL DEFAULT 0,
180 health_status tinyint(1) NOT NULL DEFAULT 0,
181 health_site_noticed tinyint(1) NOT NULL DEFAULT 1,
182 KEY idx_wpid (wpid)";
183
184 if ( empty( $currentVersion ) ) {
185 $tbl .= ',
186 PRIMARY KEY (sync_id)';
187 }
188
189 $tbl .= ') ' . $charset_collate;
190
191 $sql[] = $tbl;
192
193 $tbl = 'CREATE TABLE ' . $this->table_name( 'wp_options' ) . " (
194 opt_id int(11) NOT NULL auto_increment,
195 wpid int(11) NOT NULL,
196 name text NOT NULL DEFAULT '',
197 value longtext NOT NULL DEFAULT '',
198 KEY idx_wpid (wpid)";
199
200 if ( empty( $currentVersion ) ) {
201 $tbl .= ',
202 PRIMARY KEY (opt_id)';
203 }
204 $tbl .= ') ' . $charset_collate;
205 $sql[] = $tbl;
206
207 $tbl = 'CREATE TABLE ' . $this->table_name( 'wp_settings_backup' ) . ' (
208 set_id int(11) NOT NULL auto_increment,
209 wpid int(11) NOT NULL,
210 archiveFormat text NOT NULL,
211 KEY idx_wpid (wpid)';
212
213 if ( empty( $currentVersion ) ) {
214 $tbl .= ',
215 PRIMARY KEY (set_id)';
216 }
217 $tbl .= ') ' . $charset_collate;
218 $sql[] = $tbl;
219
220 $tbl = 'CREATE TABLE ' . $this->table_name( 'users' ) . " (
221 userid int(11) NOT NULL,
222 user_email text NOT NULL DEFAULT '',
223 ignored_plugins longtext NOT NULL DEFAULT '',
224 trusted_plugins longtext NOT NULL DEFAULT '',
225 trusted_plugins_notes longtext NOT NULL DEFAULT '',
226 ignored_themes longtext NOT NULL DEFAULT '',
227 ignored_wp_upgrades longtext NOT NULL DEFAULT '',
228 trusted_themes longtext NOT NULL DEFAULT '',
229 trusted_themes_notes longtext NOT NULL DEFAULT '',
230 site_view tinyint(1) NOT NULL DEFAULT '0',
231 pluginDir text NOT NULL DEFAULT '',
232 dismissed_plugins longtext NOT NULL DEFAULT '',
233 dismissed_themes longtext NOT NULL DEFAULT ''";
234 if ( empty( $currentVersion ) ) {
235 $tbl .= ',
236 PRIMARY KEY (userid) ';
237 }
238 $tbl .= ') ' . $charset_collate;
239 $sql[] = $tbl;
240
241 $tbl = 'CREATE TABLE ' . $this->table_name( 'wp_status' ) . ' (
242 statusid bigint(20) unsigned NOT NULL auto_increment,
243 wpid int(11) NOT NULL,
244 http_code smallint NOT NULL DEFAULT 0,
245 status tinyint(1) NOT NULL DEFAULT 0,
246 event_timestamp int(11) NOT NULL,
247 duration int(11) NOT NULL DEFAULT 0';
248 if ( empty( $currentVersion ) || version_compare( $currentVersion, '8.31', '<=' ) ) {
249 $tbl .= ',
250 PRIMARY KEY (statusid) ';
251 }
252 $tbl .= ') ' . $charset_collate;
253 $sql[] = $tbl;
254
255 $tbl = 'CREATE TABLE ' . $this->table_name( 'group' ) . ' (
256 id int(11) NOT NULL auto_increment,
257 userid int(11) NOT NULL,
258 name text NOT NULL,
259 color varchar(32) NOT NULL DEFAULT ""';
260 if ( empty( $currentVersion ) ) {
261 $tbl .= ',
262 PRIMARY KEY (id) ';
263 }
264 $tbl .= ') ' . $charset_collate;
265 $sql[] = $tbl;
266
267 $tbl = 'CREATE TABLE ' . $this->table_name( 'wp_group' ) . ' (
268 wp_group_id int(11) NOT NULL auto_increment,
269 wpid int(11) NOT NULL,
270 groupid int(11) NOT NULL,
271 KEY idx_wpid (wpid),
272 KEY idx_groupid (groupid)';
273 if ( empty( $currentVersion ) || version_compare( $currentVersion, '8.57', '<=' ) ) {
274 $tbl .= ',
275 PRIMARY KEY (wp_group_id) ';
276 }
277 $tbl .= ') ' . $charset_collate;
278 $sql[] = $tbl;
279
280 $tbl = 'CREATE TABLE ' . $this->table_name( 'lookup_item_objects' ) . ' (
281 lookup_id bigint(20) unsigned NOT NULL auto_increment,
282 item_id bigint(20) unsigned NOT NULL,
283 item_name varchar(32) NOT NULL,
284 object_id bigint(20) unsigned NOT NULL,
285 object_name varchar(32) NOT NULL,
286 KEY item_id (item_id),
287 KEY object_id (object_id)';
288
289 if ( empty( $currentVersion ) || version_compare( $currentVersion, '9.0.0.5', '<' ) ) { // NOSONAR - none IP.
290 $tbl .= ',
291 PRIMARY KEY (lookup_id) ';
292 }
293 $tbl .= ') ' . $charset_collate . ';';
294 $sql[] = $tbl;
295
296 $tbl = 'CREATE TABLE ' . $this->table_name( 'wp_backup_progress' ) . " (
297 task_id int(11) NOT NULL,
298 wp_id int(11) NOT NULL,
299 dtsFetched int(11) NOT NULL DEFAULT 0,
300 fetchResult text NOT NULL DEFAULT '',
301 downloadedDB text NOT NULL DEFAULT '',
302 downloadedFULL text NOT NULL DEFAULT '',
303 downloadedDBComplete tinyint(1) NOT NULL DEFAULT 0,
304 downloadedFULLComplete tinyint(1) NOT NULL DEFAULT 0,
305 removedFiles tinyint(1) NOT NULL DEFAULT 0,
306 attempts int(11) NOT NULL DEFAULT 0,
307 last_error text NOT NULL DEFAULT '',
308 pid int(11) NOT NULL DEFAULT 0,
309 KEY idx_task_id (task_id)";
310 if ( empty( $currentVersion ) || version_compare( $currentVersion, '8.53', '<=' ) ) {
311 $tbl .= ',
312 UNIQUE (task_id)';
313 }
314 $tbl .= ') ' . $charset_collate;
315 $sql[] = $tbl;
316
317 $tbl = 'CREATE TABLE ' . $this->table_name( 'wp_backup' ) . ' (
318 id int(11) NOT NULL auto_increment,
319 userid int(11) NOT NULL,
320 name text NOT NULL,
321 schedule text NOT NULL,
322 type text NOT NULL,
323 exclude text NOT NULL,
324 sites text NOT NULL,
325 `groups` text NOT NULL,
326 last int(11) NOT NULL,
327 last_run int(11) NOT NULL,
328 lastStartNotificationSent int(11) NOT NULL DEFAULT 0,
329 last_run_manually int(11) NOT NULL,
330 completed_sites text NOT NULL,
331 completed int(11) NOT NULL,
332 backup_errors text NOT NULL,
333 subfolder text NOT NULL,
334 filename text NOT NULL,
335 paused tinyint(1) NOT NULL,
336 template tinyint(1) DEFAULT 0,
337 excludebackup tinyint(1) DEFAULT 0,
338 excludecache tinyint(1) DEFAULT 0,
339 excludenonwp tinyint(1) DEFAULT 0,
340 excludezip tinyint(1) DEFAULT 0,
341 archiveFormat text NOT NULL,
342 loadFilesBeforeZip tinyint(1) NOT NULL DEFAULT 1,
343 maximumFileDescriptorsOverride tinyint(1) NOT NULL DEFAULT 0,
344 maximumFileDescriptorsAuto tinyint(1) NOT NULL DEFAULT 1,
345 maximumFileDescriptors int(11) NOT NULL DEFAULT 150';
346 if ( empty( $currentVersion ) ) {
347 $tbl .= ',
348 PRIMARY KEY (id) ';
349 }
350 $tbl .= ') ' . $charset_collate;
351 $sql[] = $tbl;
352
353 $tbl = 'CREATE TABLE ' . $this->table_name( 'api_keys' ) . ' (
354 key_id bigint(20) unsigned NOT NULL auto_increment,
355 user_id bigint(20) unsigned NOT NULL,
356 description varchar(200) NULL,
357 permissions varchar(10) NOT NULL,
358 consumer_key char(64) NOT NULL,
359 consumer_secret char(43) NOT NULL,
360 nonces longtext NULL,
361 truncated_key char(7) NOT NULL,
362 `enabled` tinyint(1) DEFAULT 0,
363 last_access datetime NULL default null,
364 KEY consumer_key (consumer_key),
365 KEY consumer_secret (consumer_secret)';
366 if ( empty( $currentVersion ) || version_compare( $currentVersion, '9.0.0.9', '<=' ) ) { // NOSONAR - none IP.
367 $tbl .= ',
368 PRIMARY KEY (key_id) ';
369 }
370 $tbl .= ') ' . $charset_collate . ';';
371 $sql[] = $tbl;
372
373 $tbl = 'CREATE TABLE ' . $this->table_name( 'action_log' ) . " (
374 id int(11) NOT NULL auto_increment,
375 log_content mediumtext NOT NULL DEFAULT '',
376 log_type tinyint(1) DEFAULT 0,
377 log_color tinyint(1) DEFAULT 0,
378 log_user varchar(128) NOT NULL DEFAULT '',
379 log_timestamp int(11) NOT NULL DEFAULT 0";
380 if ( empty( $currentVersion ) || version_compare( $currentVersion, '8.50', '<=' ) ) {
381 $tbl .= ',
382 PRIMARY KEY (id) ';
383 }
384 $tbl .= ') ' . $charset_collate . ';';
385 $sql[] = $tbl;
386
387 $tbl = 'CREATE TABLE ' . $this->table_name( 'request_log' ) . " (
388 id int(11) NOT NULL auto_increment,
389 wpid int(11) NOT NULL,
390 ip text NOT NULL DEFAULT '',
391 subnet text NOT NULL DEFAULT '',
392 micro_timestamp_stop DECIMAL( 12, 2 ) NOT NULL DEFAULT 0,
393 micro_timestamp_start DECIMAL( 12, 2 ) NOT NULL DEFAULT 0";
394 if ( empty( $currentVersion ) || version_compare( $currentVersion, '5.7', '<=' ) ) {
395 $tbl .= ',
396 PRIMARY KEY (id) ';
397 }
398 $tbl .= ') ' . $charset_collate . ';';
399 $sql[] = $tbl;
400
401 $sql = apply_filters( 'mainwp_db_install_tables', $sql, $currentVersion, $charset_collate );
402
403 require_once ABSPATH . 'wp-admin/includes/upgrade.php'; // NOSONAR - WP compatible.
404
405 global $wpdb;
406
407 if ( MainWP_Utility::instance()->is_disabled_functions( 'error_log' ) || ! function_exists( '\error_log' ) ) {
408 error_reporting(0); // phpcs:ignore -- try to disabled the error_log somewhere in WP.
409 }
410
411 $suppress = $wpdb->suppress_errors();
412 foreach ( $sql as $query ) {
413 dbDelta( $query );
414 }
415 $wpdb->suppress_errors( $suppress );
416
417 $this->post_update();
418
419 if ( ! is_multisite() ) {
420 MainWP_Utility::update_option( $this->option_db_key, $this->mainwp_db_version );
421 } else {
422 update_site_option( $this->option_db_key, $this->mainwp_db_version );
423 }
424 }
425
426 /**
427 * Returns the database version.
428 *
429 * @return string
430 */
431 public function get_db_version() {
432 return get_site_option( $this->option_db_key );
433 }
434
435 /**
436 * Method post_update()
437 *
438 * Update MainWP DB.
439 *
440 * @return void
441 */
442 public function post_update() { // phpcs:ignore -- NOSONAR - complex.
443
444 // get_site_option is multisite aware!
445 $currentVersion = get_site_option( $this->option_db_key );
446
447 if ( false === $currentVersion ) {
448 return;
449 }
450
451 $suppress = $this->wpdb->suppress_errors();
452
453 $this->post_update_81( $currentVersion );
454
455 if ( version_compare( $currentVersion, '8.984', '<' ) ) {
456 $sslColumns = array(
457 'nossl',
458 'nosslkey',
459 );
460 foreach ( $sslColumns as $col ) {
461 $this->wpdb->query( 'ALTER TABLE ' . $this->table_name( 'wp' ) . ' DROP COLUMN ' . $col );
462 }
463 }
464
465 // delete old columns.
466 if ( version_compare( $currentVersion, '8.17', '<' ) ) {
467 $rankColumns = array(
468 'pagerank',
469 'indexed',
470 'alexia',
471 'pagerank_old',
472 'indexed_old',
473 'alexia_old',
474 'last_db_backup_size',
475 );
476
477 foreach ( $rankColumns as $rankColumn ) {
478 $this->wpdb->query( 'ALTER TABLE ' . $this->table_name( 'wp' ) . ' DROP COLUMN ' . $rankColumn );
479 }
480
481 $syncColumns = array( 'uptodate' );
482 foreach ( $syncColumns as $column ) {
483 $this->wpdb->query( 'ALTER TABLE ' . $this->table_name( 'wp_sync' ) . ' DROP COLUMN ' . $column );
484 }
485 }
486
487 // delete old columns.
488 if ( version_compare( $currentVersion, '8.35', '<' ) ) {
489 $delColumns = array( 'offline_checks' );
490 foreach ( $delColumns as $column ) {
491 $this->wpdb->query( 'ALTER TABLE ' . $this->table_name( 'wp' ) . ' DROP COLUMN ' . $column );
492 }
493 $delColumns = array( 'heatMap' );
494 foreach ( $delColumns as $column ) {
495 $this->wpdb->query( 'ALTER TABLE ' . $this->table_name( 'users' ) . ' DROP COLUMN ' . $column );
496 }
497 }
498
499 // change columns.
500 if ( version_compare( $currentVersion, '8.40', '<' ) && version_compare( $currentVersion, '8.30', '>' ) ) {
501 $this->wpdb->query( 'ALTER TABLE ' . $this->table_name( 'wp_status' ) . ' CHANGE COLUMN `timestamp_status` `event_timestamp` int(11) NOT NULL' );
502 }
503
504 // delete columns.
505 if ( version_compare( $currentVersion, '8.42', '<' ) ) {
506 $delColumns = array( 'offlineChecksOnlineNotification' );
507 foreach ( $delColumns as $column ) {
508 $this->wpdb->query( 'ALTER TABLE ' . $this->table_name( 'users' ) . ' DROP COLUMN ' . $column );
509 }
510 }
511
512 // fix missing PRIMARY keys.
513 if ( version_compare( $currentVersion, '8.53', '<=' ) ) {
514 $this->wpdb->query( 'ALTER TABLE ' . $this->table_name( 'wp_options' ) . ' ADD opt_id int NOT NULL AUTO_INCREMENT PRIMARY KEY' );
515 $this->wpdb->query( 'ALTER TABLE ' . $this->table_name( 'wp_settings_backup' ) . ' ADD set_id int NOT NULL AUTO_INCREMENT PRIMARY KEY' );
516 $this->wpdb->query( 'ALTER TABLE ' . $this->table_name( 'wp_sync' ) . ' ADD sync_id int NOT NULL AUTO_INCREMENT PRIMARY KEY' );
517 }
518
519 $this->wpdb->suppress_errors( $suppress );
520 MainWP_DB_Client::instance()->check_to_updates_reports_data_861( $currentVersion );
521 }
522
523 /**
524 * Method pre_update_tables()
525 *
526 * Handle pre update tables.
527 *
528 * @return void
529 */
530 public function pre_update_tables() {
531 // get_site_option is multisite aware!
532 $currentVersion = get_site_option( $this->option_db_key );
533
534 if ( false === $currentVersion ) {
535 return;
536 }
537
538 $suppress = $this->wpdb->suppress_errors();
539
540 if ( version_compare( $currentVersion, '8.98', '<=' ) ) {
541 $this->wpdb->query( 'ALTER TABLE ' . $this->table_name( 'wp' ) . ' DROP COLUMN backups' );
542 $this->wpdb->query( 'ALTER TABLE ' . $this->table_name( 'wp' ) . ' DROP COLUMN note_lastupdate' );
543 $this->wpdb->query( 'ALTER TABLE ' . $this->table_name( 'wp' ) . ' DROP COLUMN pages' );
544 }
545
546 $this->wpdb->suppress_errors( $suppress );
547 }
548
549 /**
550 * Method post_update_81()
551 *
552 * Update MainWP DB for version 8.1.
553 *
554 * @param string $current_version Current version DB.
555 *
556 * @return void
557 */
558 public function post_update_81( $current_version ) { //phpcs:ignore -- NOSONAR - complex.
559
560 if ( version_compare( $current_version, '8.1', '<' ) ) {
561
562 // We can't split up here!
563 $wpSyncColumns = array(
564 'version',
565 'totalsize',
566 'dbsize',
567 'extauth',
568 'last_post_gmt',
569 'sync_errors',
570 'dtsSync',
571 'dtsSyncStart',
572 'dtsAutomaticSync',
573 'dtsAutomaticSyncStart',
574 );
575 foreach ( $wpSyncColumns as $wpSyncColumn ) {
576 $rslts = $this->wpdb->get_results( 'SELECT id,' . $wpSyncColumn . ' FROM ' . $this->table_name( 'wp' ), ARRAY_A );
577 if ( empty( $rslts ) ) {
578 continue;
579 }
580
581 foreach ( $rslts as $rslt ) {
582 $exists = $this->wpdb->get_results( $this->wpdb->prepare( 'SELECT wpid FROM ' . $this->table_name( 'wp_sync' ) . ' WHERE wpid = %d', $rslt['id'] ), ARRAY_A );
583 if ( empty( $exists ) ) {
584 $this->wpdb->insert(
585 $this->table_name( 'wp_sync' ),
586 array(
587 'wpid' => $rslt['id'],
588 $wpSyncColumn => $rslt[ $wpSyncColumn ],
589 )
590 );
591 } else {
592 $this->wpdb->update( $this->table_name( 'wp_sync' ), array( $wpSyncColumn => $rslt[ $wpSyncColumn ] ), array( 'wpid' => $rslt['id'] ) );
593 }
594 }
595
596 $suppress = $this->wpdb->suppress_errors();
597 $this->wpdb->query( 'ALTER TABLE ' . $this->table_name( 'wp' ) . ' DROP COLUMN ' . $wpSyncColumn );
598 $this->wpdb->suppress_errors( $suppress );
599 }
600
601 $optionColumns = array(
602 'last_wp_upgrades',
603 'last_plugin_upgrades',
604 'last_theme_upgrades',
605 'wp_upgrades',
606 'recent_comments',
607 'recent_posts',
608 'recent_pages',
609 );
610 foreach ( $optionColumns as $optionColumn ) {
611 $rslts = $this->wpdb->get_results( 'SELECT id,' . $optionColumn . ' FROM ' . $this->table_name( 'wp' ), ARRAY_A );
612 if ( empty( $rslts ) ) {
613 continue;
614 }
615
616 foreach ( $rslts as $rslt ) {
617 static::update_website_option( (object) $rslt, $optionColumn, $rslt[ $optionColumn ] );
618 }
619
620 $suppress = $this->wpdb->suppress_errors();
621 $this->wpdb->query( 'ALTER TABLE ' . $this->table_name( 'wp' ) . ' DROP COLUMN ' . $optionColumn );
622 $this->wpdb->suppress_errors( $suppress );
623 }
624 }
625 }
626 }
627