| @@ -24,9 +24,9 @@ | ||
| 24 | 24 | |
| 25 | 25 | /** |
| 26 | 26 | * Change array into format $wpdb->prepare can use |
| 27 | 27 | * |
| 28 | - * @param array $args | |
| 28 | + * @param array $args | |
| 29 | 29 | * @param string $starts_with |
| 30 | 30 | */ |
| 31 | 31 | public static function get_where_clause_and_values( &$args, $starts_with = ' WHERE ' ) { |
| 32 | 32 | if ( empty( $args ) ) { |
| @@ -50,12 +50,12 @@ | ||
| 50 | 50 | $args = compact( 'where', 'values' ); |
| 51 | 51 | } |
| 52 | 52 | |
| 53 | 53 | /** |
| 54 | - * @param array $args | |
| 54 | + * @param array $args | |
| 55 | 55 | * @param string $base_where |
| 56 | 56 | * @param string $where |
| 57 | - * @param array $values | |
| 57 | + * @param array $values | |
| 58 | 58 | */ |
| 59 | 59 | public static function parse_where_from_array( $args, $base_where, &$where, &$values ) { |
| 60 | 60 | $condition = ' AND'; |
| 61 | 61 | if ( isset( $args['or'] ) ) { |
| @@ -84,24 +84,24 @@ | ||
| 84 | 84 | $where .= ' ) '; |
| 85 | 85 | } else { |
| 86 | 86 | self::interpret_array_to_sql( $key, $value, $where, $values ); |
| 87 | 87 | } |
| 88 | - } | |
| 88 | + }//end foreach | |
| 89 | 89 | } |
| 90 | 90 | |
| 91 | 91 | /** |
| 92 | - * @param string $key | |
| 92 | + * @param string $key | |
| 93 | 93 | * @param string|array $value |
| 94 | - * @param string $where | |
| 95 | - * @param array $values | |
| 94 | + * @param string $where | |
| 95 | + * @param array $values | |
| 96 | + * @return void | |
| 96 | 97 | */ |
| 97 | 98 | private static function interpret_array_to_sql( $key, $value, &$where, &$values ) { |
| 98 | 99 | $key = trim( $key ); |
| 99 | 100 | |
| 100 | 101 | if ( strpos( $key, 'created_at' ) !== false || strpos( $key, 'updated_at' ) !== false ) { |
| 101 | - $k = explode( ' ', $key ); | |
| 102 | - $where .= ' DATE_FORMAT(' . reset( $k ) . ', %s) ' . str_replace( reset( $k ), '', $key ); | |
| 103 | - $values[] = '%Y-%m-%d %H:%i:%s'; | |
| 102 | + $k = explode( ' ', $key ); | |
| 103 | + $where .= ' CAST(' . reset( $k ) . ' as CHAR) ' . str_replace( reset( $k ), '', $key ); | |
| 104 | 104 | } else { |
| 105 | 105 | $where .= ' ' . $key; |
| 106 | 106 | } |
| 107 | 107 | |
| @@ -159,9 +159,9 @@ | ||
| 159 | 159 | |
| 160 | 160 | self::add_query_placeholder( $key, $value, $where ); |
| 161 | 161 | |
| 162 | 162 | $values[] = $value; |
| 163 | - } | |
| 163 | + }//end if | |
| 164 | 164 | } |
| 165 | 165 | |
| 166 | 166 | /** |
| 167 | 167 | * Add %d, or %s to query |
| @@ -167,15 +167,16 @@ | ||
| 167 | 167 | * Add %d, or %s to query |
| 168 | 168 | * |
| 169 | 169 | * @since 2.02.05 |
| 170 | 170 | * |
| 171 | - * @param string $key | |
| 171 | + * @param string $key | |
| 172 | 172 | * @param int|string $value |
| 173 | - * @param string $where | |
| 173 | + * @param string $where | |
| 174 | 174 | */ |
| 175 | 175 | private static function add_query_placeholder( $key, $value, &$where ) { |
| 176 | 176 | if ( is_numeric( $value ) && ( strpos( $key, 'meta_value' ) === false || strpos( $key, '+0' ) !== false ) ) { |
| 177 | - $value = $value + 0; // switch string to number | |
| 177 | + // Switch string to number. | |
| 178 | + $value = $value + 0; | |
| 178 | 179 | $where .= is_float( $value ) ? '%f' : '%d'; |
| 179 | 180 | } else { |
| 180 | 181 | $where .= '%s'; |
| 181 | 182 | } |
| @@ -182,10 +183,10 @@ | ||
| 182 | 183 | } |
| 183 | 184 | |
| 184 | 185 | /** |
| 185 | 186 | * @param string $table |
| 186 | - * @param array $where | |
| 187 | - * @param array $args | |
| 187 | + * @param array $where | |
| 188 | + * @param array $args | |
| 188 | 189 | * |
| 189 | 190 | * @return int |
| 190 | 191 | */ |
| 191 | 192 | public static function get_count( $table, $where = array(), $args = array() ) { |
| @@ -195,11 +196,11 @@ | ||
| 195 | 196 | } |
| 196 | 197 | |
| 197 | 198 | /** |
| 198 | 199 | * @param string $table |
| 199 | - * @param array $where | |
| 200 | + * @param array $where | |
| 200 | 201 | * @param string $field |
| 201 | - * @param array $args | |
| 202 | + * @param array $args | |
| 202 | 203 | * @param string $limit |
| 203 | 204 | * @param string $type |
| 204 | 205 | * |
| 205 | 206 | * @return array|null|string|object |
| @@ -224,10 +225,10 @@ | ||
| 224 | 225 | * Generate a cache key from the where query, field, type, and other arguments |
| 225 | 226 | * |
| 226 | 227 | * @since 2.03.07 |
| 227 | 228 | * |
| 228 | - * @param array $where | |
| 229 | - * @param array $args | |
| 229 | + * @param array $where | |
| 230 | + * @param array $args | |
| 230 | 231 | * @param string $field |
| 231 | 232 | * @param string $type |
| 232 | 233 | * |
| 233 | 234 | * @return string |
| @@ -245,11 +246,11 @@ | ||
| 245 | 246 | } |
| 246 | 247 | |
| 247 | 248 | /** |
| 248 | 249 | * @param string $table |
| 249 | - * @param array $where | |
| 250 | + * @param array $where | |
| 250 | 251 | * @param string $field |
| 251 | - * @param array $args | |
| 252 | + * @param array $args | |
| 252 | 253 | * @param string $limit |
| 253 | 254 | * |
| 254 | 255 | * @return mixed |
| 255 | 256 | */ |
| @@ -260,11 +261,11 @@ | ||
| 260 | 261 | /** |
| 261 | 262 | * @since 2.0 |
| 262 | 263 | * |
| 263 | 264 | * @param string $table |
| 264 | - * @param array $where | |
| 265 | + * @param array $where | |
| 265 | 266 | * @param string $fields |
| 266 | - * @param array $args | |
| 267 | + * @param array $args | |
| 267 | 268 | * |
| 268 | 269 | * @return mixed |
| 269 | 270 | */ |
| 270 | 271 | public static function get_row( $table, $where = array(), $fields = '*', $args = array() ) { |
| @@ -278,11 +279,11 @@ | ||
| 278 | 279 | * |
| 279 | 280 | * @since 2.0 |
| 280 | 281 | * |
| 281 | 282 | * @param string $table |
| 282 | - * @param array $where | |
| 283 | + * @param array $where | |
| 283 | 284 | * @param string $fields |
| 284 | - * @param array $args | |
| 285 | + * @param array $args | |
| 285 | 286 | * |
| 286 | 287 | * @return mixed |
| 287 | 288 | */ |
| 288 | 289 | public static function get_results( $table, $where = array(), $fields = '*', $args = array() ) { |
| @@ -317,9 +318,10 @@ | ||
| 317 | 318 | } |
| 318 | 319 | |
| 319 | 320 | // > and < need a little more work since we don't want them switched to >= and <= |
| 320 | 321 | if ( $where_is == '>' || $where_is == '<' ) { |
| 321 | - return ' ' . $where_is . '-'; // the - indicates that the = should not be added later | |
| 322 | + // The - indicates that the = should not be added later. | |
| 323 | + return ' ' . $where_is . '-'; | |
| 322 | 324 | } |
| 323 | 325 | |
| 324 | 326 | // fallback to = if the query is none of these |
| 325 | 327 | return ''; |
| @@ -401,9 +403,9 @@ | ||
| 401 | 403 | * @since 2.02.05 |
| 402 | 404 | * |
| 403 | 405 | * @param string $columns |
| 404 | 406 | * @param string $table |
| 405 | - * @param array $where | |
| 407 | + * @param array $where | |
| 406 | 408 | * |
| 407 | 409 | * @return mixed |
| 408 | 410 | */ |
| 409 | 411 | public static function get_associative_array_results( $columns, $table, $where ) { |
| @@ -424,10 +426,10 @@ | ||
| 424 | 426 | * @since 2.02.05 |
| 425 | 427 | * |
| 426 | 428 | * @param string $columns |
| 427 | 429 | * @param string $table |
| 428 | - * @param mixed $where | |
| 429 | - * @param array $args | |
| 430 | + * @param mixed $where | |
| 431 | + * @param array $args | |
| 430 | 432 | * |
| 431 | 433 | * @return string |
| 432 | 434 | */ |
| 433 | 435 | private static function generate_query_string_from_pieces( $columns, $table, $where, $args = array() ) { |
| @@ -472,9 +474,9 @@ | ||
| 472 | 474 | * Added for < WP 4.0 compatability |
| 473 | 475 | * |
| 474 | 476 | * @since 2.05.06 |
| 475 | 477 | * |
| 476 | - * @param string $term The value to escape | |
| 478 | + * @param string $term The value to escape. | |
| 477 | 479 | * |
| 478 | 480 | * @return string The escaped value |
| 479 | 481 | */ |
| 480 | 482 | public static function esc_like( $term ) { |
| @@ -595,9 +597,9 @@ | ||
| 595 | 597 | |
| 596 | 598 | /** |
| 597 | 599 | * Prepare and save settings in styles and actions |
| 598 | 600 | * |
| 599 | - * @param array $settings | |
| 601 | + * @param array $settings | |
| 600 | 602 | * @param string $group |
| 601 | 603 | * |
| 602 | 604 | * @since 2.05.06 |
| 603 | 605 | */ |
| @@ -650,12 +652,12 @@ | ||
| 650 | 652 | * Check cache before fetching values and saving to cache |
| 651 | 653 | * |
| 652 | 654 | * @since 2.05.06 |
| 653 | 655 | * |
| 654 | - * @param string $cache_key The unique name for this cache | |
| 655 | - * @param string $group The name of the cache group | |
| 656 | - * @param string $query If blank, don't run a db call | |
| 657 | - * @param string $type The wpdb function to use with this query | |
| 656 | + * @param string $cache_key The unique name for this cache. | |
| 657 | + * @param string $group The name of the cache group. | |
| 658 | + * @param string $query If blank, don't run a db call. | |
| 659 | + * @param string $type The wpdb function to use with this query. | |
| 658 | 660 | * |
| 659 | 661 | * @return mixed $results The cache or query results |
| 660 | 662 | */ |
| 661 | 663 | public static function check_cache( $cache_key, $group = '', $query = '', $type = 'get_var', $time = 300 ) { |
| @@ -727,9 +729,9 @@ | ||
| 727 | 729 | * Delete all caching in a single group |
| 728 | 730 | * |
| 729 | 731 | * @since 2.05.06 |
| 730 | 732 | * |
| 731 | - * @param string $group The name of the cache group | |
| 733 | + * @param string $group The name of the cache group. | |
| 732 | 734 | */ |
| 733 | 735 | public static function cache_delete_group( $group ) { |
| 734 | 736 | $cached_keys = self::get_group_cached_keys( $group ); |
| 735 | 737 | |
| @@ -739,6 +741,23 @@ | ||
| 739 | 741 | } |
| 740 | 742 | |
| 741 | 743 | wp_cache_delete( 'cached_keys', $group ); |
| 742 | 744 | } |
| 745 | + } | |
| 746 | + | |
| 747 | + /** | |
| 748 | + * Checks if a DB column exists. | |
| 749 | + * | |
| 750 | + * @since 6.7 | |
| 751 | + * | |
| 752 | + * @param string $table Table name without `$wpdb->prefix`. | |
| 753 | + * @param string $column Column name. | |
| 754 | + * @return bool | |
| 755 | + */ | |
| 756 | + public static function db_column_exists( $table, $column ) { | |
| 757 | + global $wpdb; | |
| 758 | + | |
| 759 | + // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared | |
| 760 | + $result = $wpdb->get_results( $wpdb->prepare( 'SHOW COLUMNS FROM ' . $wpdb->prefix . $table . ' LIKE %s', $column ) ); | |
| 761 | + return ! empty( $result ); | |
| 743 | 762 | } |
| 744 | 763 | } |