-
Notifications
You must be signed in to change notification settings - Fork 105
Expand file tree
/
Copy pathDatabase.php
More file actions
149 lines (123 loc) · 3.63 KB
/
Database.php
File metadata and controls
149 lines (123 loc) · 3.63 KB
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
<?php
declare(strict_types=1);
namespace Auth0\WordPress;
use Throwable;
final class Database
{
/**
* @var string
*/
public const CONST_TABLE_ACCOUNTS = 'accounts';
/**
* @var string
*/
public const CONST_TABLE_LOG = 'log';
/**
* @var string
*/
public const CONST_TABLE_OPTIONS = 'options';
/**
* @var string
*/
public const CONST_TABLE_SYNC = 'sync';
public function createTable(string $table)
{
if (self::CONST_TABLE_ACCOUNTS === $table) {
return $this->createTableAccounts();
}
if (self::CONST_TABLE_SYNC === $table) {
return $this->createTableSync();
}
}
public function deleteRow(
string $table,
array $where,
array $format,
): int | bool {
return $this->getWpdb()->delete($table, $where, $format);
}
public function getTableName(
string $table,
): string {
return $this->getWpdb()->prefix . 'auth0_' . $table;
}
public function insertRow(
string $table,
array $data,
array $formats,
): int | bool {
try {
return $this->getWpdb()->insert($table, $data, $formats);
} catch (Throwable) {
return false;
}
}
public function selectDistinctResults(
string $select,
string $from,
string $query,
array $args = [],
): array | object | null {
$query = $this->getWpdb()->prepare($query, ...$args);
return $this->getWpdb()->get_results(sprintf('SELECT DISTINCT %s FROM %s ', $select, $from) . $query);
}
public function selectResults(
string $select,
string $from,
string $query,
array $args = [],
): array | object | null {
$query = $this->getWpdb()->prepare($query, ...$args);
return $this->getWpdb()->get_results(sprintf('SELECT %s FROM %s ', $select, $from) . $query);
}
public function selectRow(
string $select,
string $from,
string $query,
array $args = [],
): array | object | null {
$query = $this->getWpdb()->prepare($query, ...$args);
return $this->getWpdb()->get_row(sprintf('SELECT %s FROM %s ', $select, $from) . $query);
}
private function createTableAccounts()
{
$charset = $this->getWpdb()->get_charset_collate();
$table = $this->getTableName(self::CONST_TABLE_ACCOUNTS);
require_once ABSPATH . 'wp-admin/includes/upgrade.php';
return maybe_create_table(
$table,
sprintf('CREATE TABLE %s (
id BIGINT NOT NULL AUTO_INCREMENT,
site TINYINT NOT NULL,
blog BIGINT NOT NULL,
user BIGINT NOT NULL,
auth0 TEXT NOT NULL,
PRIMARY KEY (id)
)' . $charset . ';', $table),
);
}
private function createTableSync()
{
$charset = $this->getWpdb()->get_charset_collate();
$table = $this->getTableName(self::CONST_TABLE_SYNC);
require_once ABSPATH . 'wp-admin/includes/upgrade.php';
return maybe_create_table(
$table,
sprintf('CREATE TABLE %s (
id BIGINT NOT NULL AUTO_INCREMENT,
site TINYINT NOT NULL,
blog BIGINT NOT NULL,
created INT(11) NOT NULL,
payload TEXT NOT NULL,
hashsum VARCHAR(64) NOT NULL UNIQUE,
locked INT(1) NOT NULL,
PRIMARY KEY (id)
)' . $charset . ';', $table),
);
}
private function getWpdb(): object
{
global $wpdb;
return $wpdb;
}
}