| 1 |
<?php |
| 2 |
|
| 3 |
declare(strict_types=1); |
| 4 |
|
| 5 |
namespace UserAccessManager\Database; |
| 6 |
|
| 7 |
use UserAccessManager\Wrapper\Wordpress; |
| 8 |
use wpdb; |
| 9 |
|
| 10 |
class Database |
| 11 |
{ |
| 12 |
public const USER_GROUP_TABLE_NAME = 'uam_accessgroups'; |
| 13 |
public const USER_GROUP_TO_OBJECT_TABLE_NAME = 'uam_accessgroup_to_object'; |
| 14 |
|
| 15 |
private wpdb $wpDatabase; |
| 16 |
|
| 17 |
public function __construct( |
| 18 |
private Wordpress $wordpress |
| 19 |
) { |
| 20 |
$this->wpDatabase = $wordpress->getDatabase(); |
| 21 |
} |
| 22 |
|
| 23 |
public function getWordpressDatabase(): wpdb |
| 24 |
{ |
| 25 |
return $this->wpDatabase; |
| 26 |
} |
| 27 |
|
| 28 |
public function getUserGroupTable(): string |
| 29 |
{ |
| 30 |
return $this->wpDatabase->prefix . self::USER_GROUP_TABLE_NAME; |
| 31 |
} |
| 32 |
|
| 33 |
public function getUserGroupToObjectTable(): string |
| 34 |
{ |
| 35 |
return $this->wpDatabase->prefix . self::USER_GROUP_TO_OBJECT_TABLE_NAME; |
| 36 |
} |
| 37 |
|
| 38 |
/** |
| 39 |
* @see dbDelta() |
| 40 |
*/ |
| 41 |
public function dbDelta(string $queries = '', bool $execute = true): array |
| 42 |
{ |
| 43 |
return $this->wordpress->dbDelta($queries, $execute); |
| 44 |
} |
| 45 |
|
| 46 |
/** |
| 47 |
* @see wpdb |
| 48 |
*/ |
| 49 |
public function getPrefix(): string |
| 50 |
{ |
| 51 |
return $this->wpDatabase->prefix; |
| 52 |
} |
| 53 |
|
| 54 |
public function getLastInsertId(): int|string |
| 55 |
{ |
| 56 |
return $this->wpDatabase->insert_id; |
| 57 |
} |
| 58 |
|
| 59 |
public function getCurrentBlogId(): int|string |
| 60 |
{ |
| 61 |
return $this->wpDatabase->blogid; |
| 62 |
} |
| 63 |
|
| 64 |
public function getBlogsTable(): string |
| 65 |
{ |
| 66 |
return $this->wpDatabase->blogs; |
| 67 |
} |
| 68 |
|
| 69 |
public function getPostsTable(): string |
| 70 |
{ |
| 71 |
return $this->wpDatabase->posts; |
| 72 |
} |
| 73 |
|
| 74 |
public function getTermRelationshipsTable(): string |
| 75 |
{ |
| 76 |
return $this->wpDatabase->term_relationships; |
| 77 |
} |
| 78 |
|
| 79 |
public function getTermTaxonomyTable(): string |
| 80 |
{ |
| 81 |
return $this->wpDatabase->term_taxonomy; |
| 82 |
} |
| 83 |
|
| 84 |
public function getUsersTable(): string |
| 85 |
{ |
| 86 |
return $this->wpDatabase->users; |
| 87 |
} |
| 88 |
|
| 89 |
public function getCapabilitiesTable(): string |
| 90 |
{ |
| 91 |
return $this->wpDatabase->prefix . 'capabilities'; |
| 92 |
} |
| 93 |
|
| 94 |
/** |
| 95 |
* @see wpdb::get_col |
| 96 |
*/ |
| 97 |
public function getColumn(?string $query = null, int $column = 0): array |
| 98 |
{ |
| 99 |
return $this->wpDatabase->get_col($query, $column); |
| 100 |
} |
| 101 |
|
| 102 |
/** |
| 103 |
* @see wpdb::get_row |
| 104 |
*/ |
| 105 |
public function getRow(?string $query = null, string $output = OBJECT, int $row = 0): object|array|null |
| 106 |
{ |
| 107 |
return $this->wpDatabase->get_row($query, $output, $row); |
| 108 |
} |
| 109 |
|
| 110 |
/** |
| 111 |
* @see wpdb::get_var |
| 112 |
*/ |
| 113 |
public function getVariable(?string $query = null, int $column = 0, int $row = 0): int|string|null |
| 114 |
{ |
| 115 |
return $this->wpDatabase->get_var($query, $column, $row); |
| 116 |
} |
| 117 |
|
| 118 |
/** |
| 119 |
* @see wpdb::get_blog_prefix |
| 120 |
*/ |
| 121 |
public function getBlogPrefix(int|string|null $blogId = null): string |
| 122 |
{ |
| 123 |
return $this->wpDatabase->get_blog_prefix($blogId); |
| 124 |
} |
| 125 |
|
| 126 |
/** |
| 127 |
* @see wpdb::prepare |
| 128 |
*/ |
| 129 |
public function prepare(string $query, mixed $arguments): string |
| 130 |
{ |
| 131 |
return $this->wpDatabase->prepare($query, $arguments); |
| 132 |
} |
| 133 |
|
| 134 |
/** |
| 135 |
* @see wpdb::query |
| 136 |
*/ |
| 137 |
public function query(string $query): bool|int |
| 138 |
{ |
| 139 |
return $this->wpDatabase->query($query); |
| 140 |
} |
| 141 |
|
| 142 |
/** |
| 143 |
* @see wpdb::get_results |
| 144 |
*/ |
| 145 |
public function getResults(?string $query = null, string $output = OBJECT): object|array|null |
| 146 |
{ |
| 147 |
return $this->wpDatabase->get_results($query, $output); |
| 148 |
} |
| 149 |
|
| 150 |
/** |
| 151 |
* @see wpdb::insert |
| 152 |
*/ |
| 153 |
public function insert(string $table, array $data, $format = null): bool|int |
| 154 |
{ |
| 155 |
return $this->wpDatabase->insert($table, $data, $format); |
| 156 |
} |
| 157 |
|
| 158 |
/** |
| 159 |
* @see wpdb::update |
| 160 |
*/ |
| 161 |
public function update(string $table, array $data, array $where, $format = null, $whereFormat = null): bool|int |
| 162 |
{ |
| 163 |
return $this->wpDatabase->update($table, $data, $where, $format, $whereFormat); |
| 164 |
} |
| 165 |
|
| 166 |
/** |
| 167 |
* @see wpdb::insert |
| 168 |
*/ |
| 169 |
public function replace(string $table, array $data, $format = null): bool|int |
| 170 |
{ |
| 171 |
return $this->wpDatabase->replace($table, $data, $format); |
| 172 |
} |
| 173 |
|
| 174 |
/** |
| 175 |
* @see wpdb::delete |
| 176 |
*/ |
| 177 |
public function delete(string $table, array $where, $whereFormat = null): bool|int |
| 178 |
{ |
| 179 |
return $this->wpDatabase->delete($table, $where, $whereFormat); |
| 180 |
} |
| 181 |
|
| 182 |
public function getCharset(): string |
| 183 |
{ |
| 184 |
$charsetCollate = ''; |
| 185 |
|
| 186 |
$mySlqVersion = (string) $this->getVariable('SELECT VERSION() as mysql_version'); |
| 187 |
|
| 188 |
if (version_compare($mySlqVersion, '4.1.0', '>=')) { |
| 189 |
if (!empty($this->wpDatabase->charset)) { |
| 190 |
$charsetCollate = "DEFAULT CHARACTER SET {$this->wpDatabase->charset}"; |
| 191 |
} |
| 192 |
|
| 193 |
if (!empty($this->wpDatabase->collate)) { |
| 194 |
$charsetCollate .= " COLLATE {$this->wpDatabase->collate}"; |
| 195 |
} |
| 196 |
} |
| 197 |
|
| 198 |
return $charsetCollate; |
| 199 |
} |
| 200 |
} |
| 201 |
|