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