PluginProbe
Counter Box – Add Countdowns, Timers & Dynamic Counters to WordPress / 2.0
Counter Box – Add Countdowns, Timers & Dynamic Counters to WordPress v2.0
2.0.14 trunk 1.0 1.2 1.2.1 1.2.2 1.2.3 1.2.4 2.0 2.0.1 2.0.10 2.0.11 2.0.12 2.0.13 2.0.2 2.0.3 2.0.4 2.0.5 2.0.6 2.0.7 2.0.8 2.0.9
counter-box / classes / Update / UpdateDB.php

UpdateDB.php in Counter Box – Add Countdowns, Timers & Dynamic Counters to WordPress 2.0, at classes/Update/UpdateDB.php

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