| @@ -20,9 +20,8 @@ | ||
| 20 | 20 | * |
| 21 | 21 | * @since 0.0.10 |
| 22 | 22 | */ |
| 23 | 23 | abstract class Base { |
| 24 | - | |
| 25 | 24 | /** |
| 26 | 25 | * WordPress Database class instance. |
| 27 | 26 | * |
| 28 | 27 | * @var \wpdb |
| @@ -70,9 +69,9 @@ | ||
| 70 | 69 | /** |
| 71 | 70 | * Whether or not the current database table is upgradable. |
| 72 | 71 | * Determines on the basis of the table version. |
| 73 | 72 | * |
| 74 | - * @var boolean | |
| 73 | + * @var bool | |
| 75 | 74 | * @since 0.0.13 |
| 76 | 75 | */ |
| 77 | 76 | private $db_upgradable; |
| 78 | 77 | |
| @@ -143,14 +142,14 @@ | ||
| 143 | 142 | /** |
| 144 | 143 | * Array of columns that needs to be renamed to new column name. It will be used by maybe_rename_columns() method. |
| 145 | 144 | * Format: |
| 146 | 145 | * [ |
| 147 | - [ | |
| 148 | - 'from' => 'old_column_name', | |
| 149 | - 'to' => 'new_column_name', | |
| 150 | - 'type' => 'column type definition eg: LONGTEXT', // Optional. | |
| 151 | - ], | |
| 152 | - ] | |
| 146 | + * [ | |
| 147 | + * 'from' => 'old_column_name', | |
| 148 | + * 'to' => 'new_column_name', | |
| 149 | + * 'type' => 'column type definition eg: LONGTEXT', // Optional. | |
| 150 | + * ], | |
| 151 | + * ] | |
| 153 | 152 | * |
| 154 | 153 | * @since 0.0.13 |
| 155 | 154 | * @return array<array<string,string>> |
| 156 | 155 | */ |
| @@ -183,9 +182,9 @@ | ||
| 183 | 182 | /** |
| 184 | 183 | * Stop the database upgrade process. |
| 185 | 184 | * |
| 186 | 185 | * @since 0.0.13 |
| 187 | - * @return boolean Returns true on success. | |
| 186 | + * @return bool Returns true on success. | |
| 188 | 187 | */ |
| 189 | 188 | public function stop_db_upgrade() { |
| 190 | 189 | if ( ! $this->db_upgradable ) { |
| 191 | 190 | // Only upgrade when it is needed. |
| @@ -204,9 +203,9 @@ | ||
| 204 | 203 | /** |
| 205 | 204 | * Check if current table's DB is upgradable or not. |
| 206 | 205 | * |
| 207 | 206 | * @since 0.0.13 |
| 208 | - * @return boolean True or false depending if DB is upgradable or not. | |
| 207 | + * @return bool True or false depending if DB is upgradable or not. | |
| 209 | 208 | */ |
| 210 | 209 | public function is_db_upgradable() { |
| 211 | 210 | return $this->db_upgradable; |
| 212 | 211 | } |
| @@ -221,47 +220,8 @@ | ||
| 221 | 220 | return $this->table_name; |
| 222 | 221 | } |
| 223 | 222 | |
| 224 | 223 | /** |
| 225 | - * Retrieve a cached value by its key. | |
| 226 | - * | |
| 227 | - * @param string $key The cache key. | |
| 228 | - * @since 0.0.10 | |
| 229 | - * @return mixed|null The cached value if it exists, or null if the key does not exist in the cache. | |
| 230 | - */ | |
| 231 | - protected function cache_get( $key ) { | |
| 232 | - $key = md5( $key ); | |
| 233 | - if ( ! isset( $this->caches[ $key ] ) ) { | |
| 234 | - return null; | |
| 235 | - } | |
| 236 | - return $this->caches[ $key ]; | |
| 237 | - } | |
| 238 | - | |
| 239 | - /** | |
| 240 | - * Store a value in the cache with the specified key. | |
| 241 | - * | |
| 242 | - * @param string $key The cache key. | |
| 243 | - * @param mixed $value The value to store in the cache. | |
| 244 | - * @since 0.0.10 | |
| 245 | - * @return mixed The stored value. | |
| 246 | - */ | |
| 247 | - protected function cache_set( $key, $value ) { | |
| 248 | - $key = md5( $key ); | |
| 249 | - $this->caches[ $key ] = $value; | |
| 250 | - return $value; | |
| 251 | - } | |
| 252 | - | |
| 253 | - /** | |
| 254 | - * Reset the cache by clearing all stored values. | |
| 255 | - * | |
| 256 | - * @since 0.0.10 | |
| 257 | - * @return void | |
| 258 | - */ | |
| 259 | - protected function cache_reset() { | |
| 260 | - $this->caches = []; | |
| 261 | - } | |
| 262 | - | |
| 263 | - /** | |
| 264 | 224 | * Conditionally returns current database charset or collate. |
| 265 | 225 | * |
| 266 | 226 | * @since 0.0.10 |
| 267 | 227 | * @return string |
| @@ -438,9 +398,9 @@ | ||
| 438 | 398 | } |
| 439 | 399 | |
| 440 | 400 | if ( $alter_queries ) { |
| 441 | 401 | $query = $wpdb->prepare( |
| 442 | - "ALTER TABLE %1s %2s", // phpcs:ignore -- We don't want to quote the value strings for the query. | |
| 402 | + 'ALTER TABLE %1s %2s', // phpcs:ignore -- We don't want to quote the value strings for the query. | |
| 443 | 403 | $this->get_tablename(), |
| 444 | 404 | implode( ', ', $alter_queries ) |
| 445 | 405 | ); |
| 446 | 406 | |
| @@ -499,9 +459,9 @@ | ||
| 499 | 459 | */ |
| 500 | 460 | public function get_indexes() { |
| 501 | 461 | $wpdb = $this->wpdb; |
| 502 | 462 | |
| 503 | - $indexes = $wpdb->get_results( $wpdb->prepare( "SHOW INDEX FROM %1s", $this->get_tablename() ), ARRAY_A ); // phpcs:ignore -- We don't need quote here so this is fine. | |
| 463 | + $indexes = $wpdb->get_results( $wpdb->prepare( 'SHOW INDEX FROM %1s', $this->get_tablename() ), ARRAY_A ); // phpcs:ignore -- We don't need quote here so this is fine. | |
| 504 | 464 | |
| 505 | 465 | if ( empty( $indexes ) ) { |
| 506 | 466 | return []; |
| 507 | 467 | } |
| @@ -536,9 +496,9 @@ | ||
| 536 | 496 | if ( is_null( $format ) ) { |
| 537 | 497 | /** |
| 538 | 498 | * Use formats from schema if not provided explicitly. |
| 539 | 499 | * |
| 540 | - * @var array<string>|string|null | |
| 500 | + * @var array<string>|string|null $format Format specifier for the data. | |
| 541 | 501 | */ |
| 542 | 502 | $format = $prepared_data['format']; |
| 543 | 503 | } |
| 544 | 504 | |
| @@ -566,9 +526,9 @@ | ||
| 566 | 526 | |
| 567 | 527 | /** |
| 568 | 528 | * Data format specifier. |
| 569 | 529 | * |
| 570 | - * @var array<string>|string|null | |
| 530 | + * @var array<string>|string|null $format Format specifier for the data. | |
| 571 | 531 | */ |
| 572 | 532 | $format = $prepared_data['format']; |
| 573 | 533 | |
| 574 | 534 | return $this->wpdb->update( |
| @@ -581,18 +541,18 @@ | ||
| 581 | 541 | |
| 582 | 542 | /** |
| 583 | 543 | * Delete a row data of current table. Basically, a wrapper method for wpdb::delete. |
| 584 | 544 | * |
| 585 | - * @param array<string,mixed> $where A named array of WHERE clauses (in column => value pairs). | |
| 586 | - * Multiple clauses will be joined with ANDs. | |
| 587 | - * Both $where columns and $where values should be "raw". | |
| 588 | - * Sending a null value will create an IS NULL comparison - the corresponding | |
| 589 | - * format will be ignored in this case. | |
| 590 | - * @param string[]|string $where_format Optional. An array of formats to be mapped to each of the values in $where. | |
| 591 | - * If string, that format will be used for all of the items in $where. | |
| 592 | - * A format is one of '%d', '%f', '%s' (integer, float, string). | |
| 593 | - * If omitted, all values in $data will be treated as strings unless otherwise | |
| 594 | - * specified in wpdb::$field_types. Default null. | |
| 545 | + * @param array<string,mixed> $where A named array of WHERE clauses (in column => value pairs). | |
| 546 | + * Multiple clauses will be joined with ANDs. | |
| 547 | + * Both $where columns and $where values should be "raw". | |
| 548 | + * Sending a null value will create an IS NULL comparison - the corresponding | |
| 549 | + * format will be ignored in this case. | |
| 550 | + * @param array<string>|string $where_format Optional. An array of formats to be mapped to each of the values in $where. | |
| 551 | + * If string, that format will be used for all of the items in $where. | |
| 552 | + * A format is one of '%d', '%f', '%s' (integer, float, string). | |
| 553 | + * If omitted, all values in $data will be treated as strings unless otherwise | |
| 554 | + * specified in wpdb::$field_types. Default null. | |
| 595 | 555 | * @since 0.0.13 |
| 596 | 556 | * @return int|false The number of rows deleted, or false on error. |
| 597 | 557 | */ |
| 598 | 558 | public function use_delete( $where, $where_format = null ) { |
| @@ -611,9 +571,9 @@ | ||
| 611 | 571 | * Example: ['column1' => 'value1', 'column2' => ['value2', 'value3']]. |
| 612 | 572 | * Default is an empty array. |
| 613 | 573 | * @param string $columns Optional. A string specifying which columns to select. Defaults to '*' (all columns). |
| 614 | 574 | * @param array<string> $extra_queries Optional. Array of extra queries to append at the end of main query. |
| 615 | - * @param boolean $decode Optional. Whether to decode the results by datatype. Default is true. | |
| 575 | + * @param bool $decode Optional. Whether to decode the results by datatype. Default is true. | |
| 616 | 576 | * @since 0.0.10 |
| 617 | 577 | * @return array<mixed> An associative array of results where each element represents a row, or an empty array if no results are found. |
| 618 | 578 | */ |
| 619 | 579 | public function get_results( $where_clauses = [], $columns = '*', $extra_queries = [], $decode = true ) { |
| @@ -687,8 +647,47 @@ | ||
| 687 | 647 | return Helper::get_integer_value( $this->cache_set( $query, $results ) ); |
| 688 | 648 | } |
| 689 | 649 | |
| 690 | 650 | /** |
| 651 | + * Retrieve a cached value by its key. | |
| 652 | + * | |
| 653 | + * @param string $key The cache key. | |
| 654 | + * @since 0.0.10 | |
| 655 | + * @return mixed|null The cached value if it exists, or null if the key does not exist in the cache. | |
| 656 | + */ | |
| 657 | + protected function cache_get( $key ) { | |
| 658 | + $key = md5( $key ); | |
| 659 | + if ( ! isset( $this->caches[ $key ] ) ) { | |
| 660 | + return null; | |
| 661 | + } | |
| 662 | + return $this->caches[ $key ]; | |
| 663 | + } | |
| 664 | + | |
| 665 | + /** | |
| 666 | + * Store a value in the cache with the specified key. | |
| 667 | + * | |
| 668 | + * @param string $key The cache key. | |
| 669 | + * @param mixed $value The value to store in the cache. | |
| 670 | + * @since 0.0.10 | |
| 671 | + * @return mixed The stored value. | |
| 672 | + */ | |
| 673 | + protected function cache_set( $key, $value ) { | |
| 674 | + $key = md5( $key ); | |
| 675 | + $this->caches[ $key ] = $value; | |
| 676 | + return $value; | |
| 677 | + } | |
| 678 | + | |
| 679 | + /** | |
| 680 | + * Reset the cache by clearing all stored values. | |
| 681 | + * | |
| 682 | + * @since 0.0.10 | |
| 683 | + * @return void | |
| 684 | + */ | |
| 685 | + protected function cache_reset() { | |
| 686 | + $this->caches = []; | |
| 687 | + } | |
| 688 | + | |
| 689 | + /** | |
| 691 | 690 | * Prepares WHERE clauses for a SQL query based on the provided conditions. |
| 692 | 691 | * |
| 693 | 692 | * This method constructs a WHERE statement by iterating through the |
| 694 | 693 | * specified conditions, appending them with the appropriate SQL syntax. |
| @@ -769,9 +768,9 @@ | ||
| 769 | 768 | * Prepare and format data based on the schema. |
| 770 | 769 | * |
| 771 | 770 | * @param array<mixed> $data An associative array of data where the key is the column name and the value is the data to process. |
| 772 | 771 | * Missing values will be replaced with default values specified in the schema. |
| 773 | - * @param boolean $skip_defaults Whether or not to skip the defaults values. Pass true if updating the data. | |
| 772 | + * @param bool $skip_defaults Whether or not to skip the defaults values. Pass true if updating the data. | |
| 774 | 773 | * @since 0.0.10 |
| 775 | 774 | * @return array<array<mixed>> An associative array containing: |
| 776 | 775 | * - 'data': Prepared data with values encoded according to their data types. |
| 777 | 776 | * - 'format': An array of format specifiers corresponding to the data values. |