-
Notifications
You must be signed in to change notification settings - Fork 2
/
spirit-knobs.php
165 lines (133 loc) · 4.1 KB
/
spirit-knobs.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
<?php
global $wpdb;
define("SPIRIT_KNOBS_TABLE_NAME", $wpdb->prefix . IFLPM_TABLE_PREFIX . "spirit_knobs");
define("SPIRIT_KNOBS_DB_VERSION", "1.0");
Class SpiritKnobs {
public static function does_spirit_knobs_table_exist_in_database() {
return IFLPMDBManager::does_table_exist_in_database(SPIRIT_KNOBS_TABLE_NAME);
}
public static function does_table_exist_in_database($table_name) {
global $wpdb;
$mytables = $wpdb->get_results("SHOW TABLES");
foreach ($mytables as $mytable) {
foreach ($mytable as $t) {
//echo "table name: " . $t . "<br>";
if ($t == $table_name) {
return true;
}
}
}
return false;
}
public static function is_spirit_knobs_table_empty() {
return self::is_table_empty(SPIRIT_KNOBS_TABLE_NAME);
}
public static function is_table_empty($table_name) {
global $wpdb;
$rows = $wpdb->get_results("SELECT COUNT(*) as num_rows FROM " . $table_name);
return $rows[0]->num_rows == 0;
}
public static function create_spirit_knobs_table() {
global $wpdb;
$charset_collate = $wpdb->get_charset_collate();
$sql = "CREATE TABLE " . SPIRIT_KNOBS_TABLE_NAME . " (
record_id mediumint(9) NOT NULL AUTO_INCREMENT,
user_id bigint(20) unsigned NOT NULL,
spirits tinytext NOT NULL,
PRIMARY KEY (record_id),
FOREIGN KEY (user_id) REFERENCES wp_users(ID)
) " . $charset_collate . ";";
require_once(ABSPATH . 'wp-admin/includes/upgrade.php');
dbDelta($sql);
add_option('spirit_knobs_db_version', SPIRIT_KNOBS_DB_VERSION);
}
public static function delete_all_spirits_from_spirit_knobs_table() {
self::delete_all_rows_from_table(SPIRIT_KNOBS_TABLE_NAME);
}
public static function delete_all_rows_from_table($table_name) {
global $wpdb;
$result = $wpdb->query("TRUNCATE TABLE " . $table_name);
}
public static function drop_spirit_knobs_table() {
self::drop_table(SPIRIT_KNOBS_TABLE_NAME);
}
public static function drop_table($table_name) {
global $wpdb;
$result = $wpdb->query("DROP TABLE IF EXISTS " . $table_name);
}
public static function update_spirits_by_user($user, $spirits) {
// echo $user;
// echo $spirits;
if (self::is_user_in_spirit_table($user->ID)) {
self::update_spirits($user, $spirits);
} else {
self::insert_spirits($user, $spirits);
}
return true;
}
///TODO
// Get spirits by user_ID
//
public static function update_spirits($user, $spirits) {
// echo $user->ID . " ";
// echo $event->event_id . "<br>";
/// Needs checks
$data = array('spirits' => $spirits);
$where = array('user_id' => $user->ID);
global $wpdb;
$result = $wpdb->update(
SPIRIT_KNOBS_TABLE_NAME,
$data,
$where
);
}
public static function insert_spirits($user, $spirits) {
// echo $user->ID . " ";
// echo $event->event_id . "<br>";
global $wpdb;
$result = $wpdb->insert(
SPIRIT_KNOBS_TABLE_NAME,
array(
'user_id' => $user->ID,
'spirits' => $spirits
)
);
pr($wpdb);
}
public static function get_user_spirits_by_id($id) {
global $wpdb;
if (!IFLPMDBManager::does_table_exist_in_database(SPIRIT_KNOBS_TABLE_NAME) || self::is_spirit_knobs_table_empty()) {
return null;
} else {
$result = $wpdb->get_results("SELECT * FROM " . SPIRIT_KNOBS_TABLE_NAME . " WHERE id = " . $id);
if ($wpdb->num_rows == 0) {
return null;
}
$spirits = $result[0]->spirits;
return $spirits;
}
}
public static function is_user_in_spirit_table($id) {
global $wpdb;
if (!IFLPMDBManager::does_table_exist_in_database(SPIRIT_KNOBS_TABLE_NAME) || self::is_spirit_knobs_table_empty()) {
return false;
} else {
$result = $wpdb->get_results("SELECT * FROM " . SPIRIT_KNOBS_TABLE_NAME . " WHERE user_id = " . $id);
if ($wpdb->num_rows == 0) {
return false;
}
return true;
}
}
public static function get_id_by_pairing($pairing_string) {
global $wpdb;
$result = $wpdb->get_results("SELECT * FROM " . USER_PAIRINGS_TABLE_NAME . " WHERE pairing = '" . $pairing_string . "'");
if ($wpdb->num_rows == 0) {
$id = self::add_pairing_to_user_pairings_table($pairing_string);
} else {
$id = $result[0]->id;
}
return $id;
}
}
?>