| 1 |
<?php |
| 2 |
/** |
| 3 |
* Database.php |
| 4 |
* |
| 5 |
* The Database class file. |
| 6 |
* |
| 7 |
* PHP versions 5 |
| 8 |
* |
| 9 |
* @author Alexander Schneider <alexanderschneider85@gmail.com> |
| 10 |
* @copyright 2008-2017 Alexander Schneider |
| 11 |
* @license http://www.gnu.org/licenses/gpl-2.0.html GNU General Public License, version 2 |
| 12 |
* @version SVN: $id$ |
| 13 |
* @link http://wordpress.org/extend/plugins/user-access-manager/ |
| 14 |
*/ |
| 15 |
|
| 16 |
declare(strict_types=1); |
| 17 |
|
| 18 |
namespace UserAccessManager\Database; |
| 19 |
|
| 20 |
use UserAccessManager\Wrapper\Wordpress; |
| 21 |
use wpdb; |
| 22 |
|
| 23 |
/** |
| 24 |
* Class Database |
| 25 |
* |
| 26 |
* @package UserAccessManager\Database |
| 27 |
*/ |
| 28 |
class Database |
| 29 |
{ |
| 30 |
const USER_GROUP_TABLE_NAME = 'uam_accessgroups'; |
| 31 |
const USER_GROUP_TO_OBJECT_TABLE_NAME = 'uam_accessgroup_to_object'; |
| 32 |
|
| 33 |
/** |
| 34 |
* @var Wordpress |
| 35 |
*/ |
| 36 |
private $wordpress; |
| 37 |
|
| 38 |
/** |
| 39 |
* @var wpdb |
| 40 |
*/ |
| 41 |
private $wpDatabase; |
| 42 |
|
| 43 |
/** |
| 44 |
* Database constructor. |
| 45 |
* @param Wordpress $wordpress |
| 46 |
*/ |
| 47 |
public function __construct(Wordpress $wordpress) |
| 48 |
{ |
| 49 |
$this->wordpress = $wordpress; |
| 50 |
$this->wpDatabase = $wordpress->getDatabase(); |
| 51 |
} |
| 52 |
|
| 53 |
/** |
| 54 |
* Returns the wordpress database. |
| 55 |
* @return wpdb |
| 56 |
*/ |
| 57 |
public function getWordpressDatabase(): wpdb |
| 58 |
{ |
| 59 |
return $this->wpDatabase; |
| 60 |
} |
| 61 |
|
| 62 |
/** |
| 63 |
* Returns the user group table name. |
| 64 |
* @return string |
| 65 |
*/ |
| 66 |
public function getUserGroupTable(): string |
| 67 |
{ |
| 68 |
return $this->wpDatabase->prefix . self::USER_GROUP_TABLE_NAME; |
| 69 |
} |
| 70 |
|
| 71 |
/** |
| 72 |
* Returns the user group table name. |
| 73 |
* @return string |
| 74 |
*/ |
| 75 |
public function getUserGroupToObjectTable(): string |
| 76 |
{ |
| 77 |
return $this->wpDatabase->prefix . self::USER_GROUP_TO_OBJECT_TABLE_NAME; |
| 78 |
} |
| 79 |
|
| 80 |
/** |
| 81 |
* @param string $queries |
| 82 |
* @param bool $execute |
| 83 |
* @return array |
| 84 |
* @see dbDelta() |
| 85 |
*/ |
| 86 |
public function dbDelta($queries = '', $execute = true): array |
| 87 |
{ |
| 88 |
return $this->wordpress->dbDelta($queries, $execute); |
| 89 |
} |
| 90 |
|
| 91 |
/** |
| 92 |
* @return string |
| 93 |
* @see \wpdb::$prefix |
| 94 |
*/ |
| 95 |
public function getPrefix(): string |
| 96 |
{ |
| 97 |
return $this->wpDatabase->prefix; |
| 98 |
} |
| 99 |
|
| 100 |
/** |
| 101 |
* Returns the last insert id. |
| 102 |
* @return mixed |
| 103 |
*/ |
| 104 |
public function getLastInsertId() |
| 105 |
{ |
| 106 |
return $this->wpDatabase->insert_id; |
| 107 |
} |
| 108 |
|
| 109 |
/** |
| 110 |
* Returns the current blog id. |
| 111 |
* @return int|string |
| 112 |
*/ |
| 113 |
public function getCurrentBlogId() |
| 114 |
{ |
| 115 |
return $this->wpDatabase->blogid; |
| 116 |
} |
| 117 |
|
| 118 |
/** |
| 119 |
* Returns the blogs table name. |
| 120 |
* @return string |
| 121 |
*/ |
| 122 |
public function getBlogsTable(): string |
| 123 |
{ |
| 124 |
return $this->wpDatabase->blogs; |
| 125 |
} |
| 126 |
|
| 127 |
/** |
| 128 |
* Returns the posts table name. |
| 129 |
* @return string |
| 130 |
*/ |
| 131 |
public function getPostsTable(): string |
| 132 |
{ |
| 133 |
return $this->wpDatabase->posts; |
| 134 |
} |
| 135 |
|
| 136 |
/** |
| 137 |
* Returns the term_relationships table name. |
| 138 |
* @return string |
| 139 |
*/ |
| 140 |
public function getTermRelationshipsTable(): string |
| 141 |
{ |
| 142 |
return $this->wpDatabase->term_relationships; |
| 143 |
} |
| 144 |
|
| 145 |
/** |
| 146 |
* Returns the term_taxonomy table name. |
| 147 |
* @return string |
| 148 |
*/ |
| 149 |
public function getTermTaxonomyTable(): string |
| 150 |
{ |
| 151 |
return $this->wpDatabase->term_taxonomy; |
| 152 |
} |
| 153 |
|
| 154 |
/** |
| 155 |
* Returns the users table name. |
| 156 |
* @return string |
| 157 |
*/ |
| 158 |
public function getUsersTable(): string |
| 159 |
{ |
| 160 |
return $this->wpDatabase->users; |
| 161 |
} |
| 162 |
|
| 163 |
/** |
| 164 |
* Returns the capabilities table name. |
| 165 |
* @return string |
| 166 |
*/ |
| 167 |
public function getCapabilitiesTable(): string |
| 168 |
{ |
| 169 |
return $this->wpDatabase->prefix . 'capabilities'; |
| 170 |
} |
| 171 |
|
| 172 |
/** |
| 173 |
* @param string $query |
| 174 |
* @param int $column |
| 175 |
* @return array |
| 176 |
* @see \wpdb::get_col() |
| 177 |
*/ |
| 178 |
public function getColumn($query = null, $column = 0): array |
| 179 |
{ |
| 180 |
return $this->wpDatabase->get_col($query, $column); |
| 181 |
} |
| 182 |
|
| 183 |
/** |
| 184 |
* @param string $query |
| 185 |
* @param string $output |
| 186 |
* @param int $row |
| 187 |
* @return array|null|object |
| 188 |
* @see \wpdb::get_row() |
| 189 |
*/ |
| 190 |
public function getRow($query = null, $output = OBJECT, $row = 0) |
| 191 |
{ |
| 192 |
return $this->wpDatabase->get_row($query, $output, $row); |
| 193 |
} |
| 194 |
|
| 195 |
/** |
| 196 |
* @param null|string $query |
| 197 |
* @param int $column |
| 198 |
* @param int $row |
| 199 |
* @return null|int|string |
| 200 |
* @see \wpdb::get_var() |
| 201 |
*/ |
| 202 |
public function getVariable($query = null, $column = 0, $row = 0) |
| 203 |
{ |
| 204 |
return $this->wpDatabase->get_var($query, $column, $row); |
| 205 |
} |
| 206 |
|
| 207 |
/** |
| 208 |
* @param int $blogId |
| 209 |
* @return string |
| 210 |
* @see \wpdb::get_blog_prefix() |
| 211 |
*/ |
| 212 |
public function getBlogPrefix($blogId = null): string |
| 213 |
{ |
| 214 |
return $this->wpDatabase->get_blog_prefix($blogId); |
| 215 |
} |
| 216 |
|
| 217 |
/** |
| 218 |
* @param string $query |
| 219 |
* @param mixed $arguments |
| 220 |
* @return string |
| 221 |
* @see \wpdb::prepare() |
| 222 |
*/ |
| 223 |
public function prepare(string $query, $arguments): string |
| 224 |
{ |
| 225 |
return $this->wpDatabase->prepare($query, $arguments); |
| 226 |
} |
| 227 |
|
| 228 |
/** |
| 229 |
* @param string $query |
| 230 |
* @return false|int |
| 231 |
* @see \wpdb::query() |
| 232 |
*/ |
| 233 |
public function query(string $query) |
| 234 |
{ |
| 235 |
return $this->wpDatabase->query($query); |
| 236 |
} |
| 237 |
|
| 238 |
/** |
| 239 |
* @param string $query |
| 240 |
* @param string $output |
| 241 |
* @return array|null|object |
| 242 |
* @see \wpdb::get_results() |
| 243 |
*/ |
| 244 |
public function getResults($query = null, $output = OBJECT) |
| 245 |
{ |
| 246 |
return $this->wpDatabase->get_results($query, $output); |
| 247 |
} |
| 248 |
|
| 249 |
/** |
| 250 |
* @param string $table |
| 251 |
* @param array $data |
| 252 |
* @param null $format |
| 253 |
* @return false|int |
| 254 |
* @see \wpdb::insert() |
| 255 |
*/ |
| 256 |
public function insert(string $table, array $data, $format = null) |
| 257 |
{ |
| 258 |
return $this->wpDatabase->insert($table, $data, $format); |
| 259 |
} |
| 260 |
|
| 261 |
/** |
| 262 |
* @param string $table |
| 263 |
* @param array $data |
| 264 |
* @param array $where |
| 265 |
* @param null $format |
| 266 |
* @param null $whereFormat |
| 267 |
* @return false|int |
| 268 |
* @see \wpdb::update() |
| 269 |
*/ |
| 270 |
public function update(string $table, array $data, array $where, $format = null, $whereFormat = null) |
| 271 |
{ |
| 272 |
return $this->wpDatabase->update($table, $data, $where, $format, $whereFormat); |
| 273 |
} |
| 274 |
|
| 275 |
/** |
| 276 |
* @param string $table |
| 277 |
* @param array $data |
| 278 |
* @param null $format |
| 279 |
* @return false|int |
| 280 |
* @see \wpdb::insert() |
| 281 |
*/ |
| 282 |
public function replace(string $table, array $data, $format = null) |
| 283 |
{ |
| 284 |
return $this->wpDatabase->replace($table, $data, $format); |
| 285 |
} |
| 286 |
|
| 287 |
/** |
| 288 |
* @param string $table |
| 289 |
* @param array $where |
| 290 |
* @param null $whereFormat |
| 291 |
* @return false|int |
| 292 |
* @see \wpdb::delete() |
| 293 |
*/ |
| 294 |
public function delete(string $table, array $where, $whereFormat = null) |
| 295 |
{ |
| 296 |
return $this->wpDatabase->delete($table, $where, $whereFormat); |
| 297 |
} |
| 298 |
|
| 299 |
/** |
| 300 |
* Returns the database charset. |
| 301 |
* @return string |
| 302 |
*/ |
| 303 |
public function getCharset(): string |
| 304 |
{ |
| 305 |
$charsetCollate = ''; |
| 306 |
|
| 307 |
$mySlqVersion = (string) $this->getVariable('SELECT VERSION() as mysql_version'); |
| 308 |
|
| 309 |
if (version_compare($mySlqVersion, '4.1.0', '>=')) { |
| 310 |
if (!empty($this->wpDatabase->charset)) { |
| 311 |
$charsetCollate = "DEFAULT CHARACTER SET {$this->wpDatabase->charset}"; |
| 312 |
} |
| 313 |
|
| 314 |
if (!empty($this->wpDatabase->collate)) { |
| 315 |
$charsetCollate .= " COLLATE {$this->wpDatabase->collate}"; |
| 316 |
} |
| 317 |
} |
| 318 |
|
| 319 |
return $charsetCollate; |
| 320 |
} |
| 321 |
} |
| 322 |
|