| @@ -11,9 +11,10 @@ | ||
| 11 | 11 | */ |
| 12 | 12 | |
| 13 | 13 | use WP_Error; |
| 14 | 14 | |
| 15 | -class Model { | |
| 15 | +class Model | |
| 16 | +{ | |
| 16 | 17 | protected static $table; |
| 17 | 18 | protected static $primary_key; |
| 18 | 19 | protected $app_db; |
| 19 | 20 | protected $table_name; |
| @@ -21,9 +22,10 @@ | ||
| 21 | 22 | |
| 22 | 23 | /** |
| 23 | 24 | * Undocumented function |
| 24 | 25 | */ |
| 25 | - public function __construct() { | |
| 26 | + public function __construct() | |
| 27 | + { | |
| 26 | 28 | global $wpdb; |
| 27 | 29 | $this->app_db = $wpdb; |
| 28 | 30 | $this->table_name = $wpdb->prefix . static::$table; |
| 29 | 31 | } |
| @@ -32,9 +34,10 @@ | ||
| 32 | 34 | * Undocumented function |
| 33 | 35 | * |
| 34 | 36 | * @return void |
| 35 | 37 | */ |
| 36 | - public function insert($data = []) { | |
| 38 | + public function insert($data = []) | |
| 39 | + { | |
| 37 | 40 | if (is_null($data)) { |
| 38 | 41 | return new WP_Error('empty_data', __('Form data is empty', 'bit-form')); |
| 39 | 42 | } |
| 40 | 43 | $result = $this->app_db->insert( |
| @@ -51,9 +54,19 @@ | ||
| 51 | 54 | * @param array $condition |
| 52 | 55 | * |
| 53 | 56 | * @return mixed |
| 54 | 57 | */ |
| 55 | - public function get($item = '*', $condition = [], $limit = null, $offset = null, $order_by = null, $order_follow = null) { | |
| 58 | + public function get($item = '*', $condition = [], $limit = null, $offset = null, $order_by = null, $order_follow = null) | |
| 59 | + { | |
| 60 | + $checkIfTableExists = $this->app_db->get_var( | |
| 61 | + $this->app_db->prepare( | |
| 62 | + 'SHOW TABLES LIKE %s', | |
| 63 | + $this->table_name | |
| 64 | + ) | |
| 65 | + ); | |
| 66 | + if (is_null($checkIfTableExists)) { | |
| 67 | + return []; | |
| 68 | + } | |
| 56 | 69 | if (\is_array($item)) { |
| 57 | 70 | $column_to_select = implode(',', $item); |
| 58 | 71 | } else { |
| 59 | 72 | $column_to_select = $item; |
| @@ -101,9 +114,10 @@ | ||
| 101 | 114 | * @param array $condition |
| 102 | 115 | * |
| 103 | 116 | * @return void |
| 104 | 117 | */ |
| 105 | - public function count($condition = null) { | |
| 118 | + public function count($condition = null) | |
| 119 | + { | |
| 106 | 120 | $checkCondition = $this->checkCondition($condition); |
| 107 | 121 | if (is_wp_error($checkCondition)) { |
| 108 | 122 | return $checkCondition; |
| 109 | 123 | } |
| @@ -145,9 +159,10 @@ | ||
| 145 | 159 | * @param array $condition |
| 146 | 160 | * |
| 147 | 161 | * @return void |
| 148 | 162 | */ |
| 149 | - public function update(array $data, array $condition) { | |
| 163 | + public function update(array $data, array $condition) | |
| 164 | + { | |
| 150 | 165 | if ( |
| 151 | 166 | !\is_null($data) |
| 152 | 167 | && \is_array($data) |
| 153 | 168 | && array_keys($data) !== range(0, count($data) - 1) |
| @@ -176,9 +191,10 @@ | ||
| 176 | 191 | * @param array $condition |
| 177 | 192 | * |
| 178 | 193 | * @return void |
| 179 | 194 | */ |
| 180 | - public function bulkUpdate(array $data = null, array $condition = null) { | |
| 195 | + public function bulkUpdate(array $data = null, array $condition = null) | |
| 196 | + { | |
| 181 | 197 | if ( |
| 182 | 198 | !\is_null($data) |
| 183 | 199 | && \is_array($data) |
| 184 | 200 | && array_keys($data) !== range(0, count($data) - 1) |
| @@ -228,9 +244,10 @@ | ||
| 228 | 244 | * @param array $condition |
| 229 | 245 | * |
| 230 | 246 | * @return void |
| 231 | 247 | */ |
| 232 | - public function duplicate(array $columns, array $duplicate, array $condition) { | |
| 248 | + public function duplicate(array $columns, array $duplicate, array $condition) | |
| 249 | + { | |
| 233 | 250 | if (!(!\is_null($columns) |
| 234 | 251 | && \is_array($columns) |
| 235 | 252 | && array_keys($columns) === range(0, count($columns) - 1) |
| 236 | 253 | && !\is_null($duplicate) |
| @@ -270,9 +287,10 @@ | ||
| 270 | 287 | $this->execute($query, $all_values); |
| 271 | 288 | return $this->getResult(); |
| 272 | 289 | } |
| 273 | 290 | |
| 274 | - public function trash(array $condition = null) { | |
| 291 | + public function trash(array $condition = null) | |
| 292 | + { | |
| 275 | 293 | if ( |
| 276 | 294 | !\is_null($condition) |
| 277 | 295 | && \is_array($condition) |
| 278 | 296 | && array_keys($condition) !== range(0, count($condition) - 1) |
| @@ -293,9 +311,10 @@ | ||
| 293 | 311 | ); |
| 294 | 312 | return $this->getResult($result); |
| 295 | 313 | } |
| 296 | 314 | |
| 297 | - public function delete(array $condition = null) { | |
| 315 | + public function delete(array $condition = null) | |
| 316 | + { | |
| 298 | 317 | if ( |
| 299 | 318 | !\is_null($condition) |
| 300 | 319 | && \is_array($condition) |
| 301 | 320 | && array_keys($condition) !== range(0, count($condition) - 1) |
| @@ -313,9 +332,10 @@ | ||
| 313 | 332 | ); |
| 314 | 333 | return $this->getResult($result); |
| 315 | 334 | } |
| 316 | 335 | |
| 317 | - public function bulkDelete(array $condition = null) { | |
| 336 | + public function bulkDelete(array $condition = null) | |
| 337 | + { | |
| 318 | 338 | if ( |
| 319 | 339 | !\is_null($condition) |
| 320 | 340 | && \is_array($condition) |
| 321 | 341 | && array_keys($condition) !== range(0, count($condition) - 1) |
| @@ -347,14 +367,16 @@ | ||
| 347 | 367 | ); |
| 348 | 368 | return $this->getResult($result); |
| 349 | 369 | } |
| 350 | 370 | |
| 351 | - protected function getFieldFormat($value) { | |
| 371 | + protected function getFieldFormat($value) | |
| 372 | + { | |
| 352 | 373 | return ('integer' === gettype($value)) ? |
| 353 | 374 | '%d' : (('double' === gettype($value)) ? '%f' : '%s'); |
| 354 | 375 | } |
| 355 | 376 | |
| 356 | - protected function getFormatedCondition($condition, $check_operator = null, $join_operator = ' AND ') { | |
| 377 | + protected function getFormatedCondition($condition, $check_operator = null, $join_operator = ' AND ') | |
| 378 | + { | |
| 357 | 379 | if (\is_null($condition)) { |
| 358 | 380 | return false; |
| 359 | 381 | } |
| 360 | 382 | $no_condition = count($condition); |
| @@ -399,9 +421,10 @@ | ||
| 399 | 421 | 'values' => $all_values |
| 400 | 422 | ]; |
| 401 | 423 | } |
| 402 | 424 | |
| 403 | - protected function checkCondition(array $condition) { | |
| 425 | + protected function checkCondition(array $condition) | |
| 426 | + { | |
| 404 | 427 | if (!is_null($condition) && array_keys($condition) === range(0, count($condition) - 1)) { |
| 405 | 428 | return new WP_Error( |
| 406 | 429 | 'get_condition', |
| 407 | 430 | 'Require ASSOC_ARRAY but found N_ARRAY' |
| @@ -409,9 +432,10 @@ | ||
| 409 | 432 | } |
| 410 | 433 | return true; |
| 411 | 434 | } |
| 412 | 435 | |
| 413 | - protected function execute($sql, $values = null) { | |
| 436 | + protected function execute($sql, $values = null) | |
| 437 | + { | |
| 414 | 438 | if (is_null($values)) { |
| 415 | 439 | $preparedQuery = $sql; |
| 416 | 440 | } else { |
| 417 | 441 | $preparedQuery = $this->app_db->prepare($sql, $values); |
| @@ -426,9 +450,10 @@ | ||
| 426 | 450 | // print_r($this->app_db->last_query); |
| 427 | 451 | return $this; |
| 428 | 452 | } |
| 429 | 453 | |
| 430 | - protected function getResult($db_response = null) { | |
| 454 | + protected function getResult($db_response = null) | |
| 455 | + { | |
| 431 | 456 | $db_response = !empty($this->db_response) ? $this->db_response : $db_response; |
| 432 | 457 | if (!empty($this->app_db->last_error)) { |
| 433 | 458 | return new WP_Error('db_error', $this->app_db->last_error); |
| 434 | 459 | } |
| @@ -448,6 +473,15 @@ | ||
| 448 | 473 | $response = $db_response; |
| 449 | 474 | } |
| 450 | 475 | $this->app_db->flush(); |
| 451 | 476 | return $response; |
| 477 | + } | |
| 478 | + | |
| 479 | + // get last entry id | |
| 480 | + public function lastId() | |
| 481 | + { | |
| 482 | + $sql = "SELECT id FROM {$this->table_name} | |
| 483 | + ORDER BY id DESC LIMIT 1"; | |
| 484 | + $result = $this->execute($sql)->getResult()[0]; | |
| 485 | + return $result->id; | |
| 452 | 486 | } |
| 453 | 487 | } |