| 1 |
<?php |
| 2 |
|
| 3 |
/** |
| 4 |
* Class UpdateDB |
| 5 |
* |
| 6 |
* Contains methods for updating the database structure and data |
| 7 |
* |
| 8 |
* @package BubbleMenu |
| 9 |
* @subpackage Update |
| 10 |
* @author Dmytro Lobov <hey@wow-company.com>, Wow-Company |
| 11 |
* @copyright 2024 Dmytro Lobov |
| 12 |
* @license GPL-2.0+ |
| 13 |
* |
| 14 |
*/ |
| 15 |
|
| 16 |
namespace BubbleMenu\Update; |
| 17 |
|
| 18 |
use BubbleMenu\Admin\DBManager; |
| 19 |
use BubbleMenu\Settings_Helper; |
| 20 |
use BubbleMenu\WOWP_Plugin; |
| 21 |
|
| 22 |
class UpdateDB { |
| 23 |
|
| 24 |
const NEW_DB_VERSION = '5.0'; |
| 25 |
const TABLE_COLUMNS = " |
| 26 |
id mediumint(9) NOT NULL AUTO_INCREMENT, |
| 27 |
title VARCHAR(200) DEFAULT '' NOT NULL, |
| 28 |
param longtext DEFAULT '' NOT NULL, |
| 29 |
status boolean DEFAULT 0 NOT NULL, |
| 30 |
mode boolean DEFAULT 0 NOT NULL, |
| 31 |
tag text DEFAULT '' NOT NULL, |
| 32 |
PRIMARY KEY (id) |
| 33 |
"; |
| 34 |
|
| 35 |
public static function init(): void { |
| 36 |
$current_db_version = get_option( WOWP_Plugin::PREFIX . '_db_version' ); |
| 37 |
|
| 38 |
if ( $current_db_version && version_compare( $current_db_version, self::NEW_DB_VERSION, '>=' ) ) { |
| 39 |
return; |
| 40 |
} |
| 41 |
|
| 42 |
self::start_update(); |
| 43 |
update_option( WOWP_Plugin::PREFIX . '_db_version', self::NEW_DB_VERSION ); |
| 44 |
} |
| 45 |
|
| 46 |
private static function start_update(): void { |
| 47 |
self::update_database(); |
| 48 |
self::update_fields(); |
| 49 |
} |
| 50 |
|
| 51 |
private static function update_database(): void { |
| 52 |
global $wpdb; |
| 53 |
|
| 54 |
$table = $wpdb->prefix . WOWP_Plugin::PREFIX; |
| 55 |
$charset_collate = $wpdb->get_charset_collate(); |
| 56 |
|
| 57 |
require_once( ABSPATH . 'wp-admin/includes/upgrade.php' ); |
| 58 |
|
| 59 |
$sql = "CREATE TABLE $table (" . self::TABLE_COLUMNS . ") $charset_collate;"; |
| 60 |
dbDelta( $sql ); |
| 61 |
} |
| 62 |
|
| 63 |
|
| 64 |
private static function update_fields(): void { |
| 65 |
$results = DBManager::get_all_data(); |
| 66 |
|
| 67 |
if ( empty( $results ) || ! is_array( $results ) ) { |
| 68 |
return; |
| 69 |
} |
| 70 |
|
| 71 |
foreach ( $results as $result ) { |
| 72 |
|
| 73 |
$param_updater = new ParamUpdater( maybe_unserialize( $result->param ) ); |
| 74 |
$updated_param = $param_updater->update(); |
| 75 |
|
| 76 |
$data = [ |
| 77 |
'param' => maybe_serialize( $updated_param ), |
| 78 |
'status' => absint( ! empty( $updated_param['status'] ) ), |
| 79 |
'mode' => absint( ! empty( $updated_param['test_mode'] ) ), |
| 80 |
'tag' => '', |
| 81 |
]; |
| 82 |
|
| 83 |
DBManager::update( $data, [ 'id' => $result->id ], [ '%s', '%d', '%d', '%s' ] ); |
| 84 |
} |
| 85 |
} |
| 86 |
|
| 87 |
} |