| @@ -24,10 +24,11 @@ | ||
| 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 | + * @return void | |
| 30 | 31 | */ |
| 31 | 32 | public static function get_where_clause_and_values( &$args, $starts_with = ' WHERE ' ) { |
| 32 | 33 | if ( empty( $args ) ) { |
| 33 | 34 | // add an arg to prevent prepare from failing |
| @@ -50,12 +51,12 @@ | ||
| 50 | 51 | $args = compact( 'where', 'values' ); |
| 51 | 52 | } |
| 52 | 53 | |
| 53 | 54 | /** |
| 54 | - * @param array $args | |
| 55 | + * @param array $args | |
| 55 | 56 | * @param string $base_where |
| 56 | 57 | * @param string $where |
| 57 | - * @param array $values | |
| 58 | + * @param array $values | |
| 58 | 59 | */ |
| 59 | 60 | public static function parse_where_from_array( $args, $base_where, &$where, &$values ) { |
| 60 | 61 | $condition = ' AND'; |
| 61 | 62 | if ( isset( $args['or'] ) ) { |
| @@ -63,12 +64,12 @@ | ||
| 63 | 64 | unset( $args['or'] ); |
| 64 | 65 | } |
| 65 | 66 | |
| 66 | 67 | foreach ( $args as $key => $value ) { |
| 67 | - $where .= empty( $where ) ? $base_where : $condition; | |
| 68 | + $where .= empty( $where ) ? $base_where : $condition; | |
| 68 | 69 | $array_inc_null = ( ! is_numeric( $key ) && is_array( $value ) && in_array( null, $value ) ); |
| 69 | 70 | if ( is_numeric( $key ) || $array_inc_null ) { |
| 70 | - $where .= ' ( '; | |
| 71 | + $where .= ' ( '; | |
| 71 | 72 | $nested_where = ''; |
| 72 | 73 | if ( $array_inc_null ) { |
| 73 | 74 | foreach ( $value as $val ) { |
| 74 | 75 | $parse_where = array( |
| @@ -84,24 +85,24 @@ | ||
| 84 | 85 | $where .= ' ) '; |
| 85 | 86 | } else { |
| 86 | 87 | self::interpret_array_to_sql( $key, $value, $where, $values ); |
| 87 | 88 | } |
| 88 | - } | |
| 89 | + }//end foreach | |
| 89 | 90 | } |
| 90 | 91 | |
| 91 | 92 | /** |
| 92 | - * @param string $key | |
| 93 | - * @param string|array $value | |
| 94 | - * @param string $where | |
| 95 | - * @param array $values | |
| 93 | + * @param string $key | |
| 94 | + * @param array|string $value | |
| 95 | + * @param string $where | |
| 96 | + * @param array $values | |
| 97 | + * @return void | |
| 96 | 98 | */ |
| 97 | 99 | private static function interpret_array_to_sql( $key, $value, &$where, &$values ) { |
| 98 | 100 | $key = trim( $key ); |
| 99 | 101 | |
| 100 | 102 | 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'; | |
| 103 | + $k = explode( ' ', $key ); | |
| 104 | + $where .= ' CAST(' . reset( $k ) . ' as CHAR) ' . str_replace( reset( $k ), '', $key ); | |
| 104 | 105 | } else { |
| 105 | 106 | $where .= ' ' . $key; |
| 106 | 107 | } |
| 107 | 108 | |
| @@ -110,22 +111,22 @@ | ||
| 110 | 111 | |
| 111 | 112 | if ( is_array( $value ) ) { |
| 112 | 113 | // translate array of values to "in" |
| 113 | 114 | if ( strpos( $lowercase_key, 'like' ) !== false ) { |
| 114 | - $where = preg_replace( '/' . $key . '$/', '', $where ); | |
| 115 | + $where = preg_replace( '/' . $key . '$/', '', $where ); | |
| 115 | 116 | $where .= '('; |
| 116 | - $start = true; | |
| 117 | + $start = true; | |
| 117 | 118 | foreach ( $value as $v ) { |
| 118 | 119 | if ( ! $start ) { |
| 119 | 120 | $where .= ' OR '; |
| 120 | 121 | } |
| 121 | 122 | $start = false; |
| 122 | - $where .= $key . ' %s'; | |
| 123 | + $where .= $key . ' %s'; | |
| 123 | 124 | $values[] = '%' . self::esc_like( $v ) . '%'; |
| 124 | 125 | } |
| 125 | 126 | $where .= ')'; |
| 126 | 127 | } elseif ( ! empty( $value ) ) { |
| 127 | - $where .= ' in (' . self::prepare_array_values( $value, '%s' ) . ')'; | |
| 128 | + $where .= ' in (' . self::prepare_array_values( $value, '%s' ) . ')'; | |
| 128 | 129 | $values = array_merge( $values, $value ); |
| 129 | 130 | } |
| 130 | 131 | } elseif ( strpos( $lowercase_key, 'like' ) !== false ) { |
| 131 | 132 | /** |
| @@ -134,18 +135,18 @@ | ||
| 134 | 135 | * If the key is %like then skip the last % for ends with |
| 135 | 136 | */ |
| 136 | 137 | $start = '%'; |
| 137 | 138 | $end = '%'; |
| 138 | - if ( $lowercase_key == 'like%' ) { | |
| 139 | + if ( $lowercase_key === 'like%' ) { | |
| 139 | 140 | $start = ''; |
| 140 | 141 | $where = rtrim( $where, '%' ); |
| 141 | 142 | } elseif ( $lowercase_key == '%like' ) { |
| 142 | - $end = ''; | |
| 143 | - $where = rtrim( rtrim( $where, '%like' ), '%LIKE' ); | |
| 143 | + $end = ''; | |
| 144 | + $where = rtrim( rtrim( $where, '%like' ), '%LIKE' ); | |
| 144 | 145 | $where .= 'like'; |
| 145 | 146 | } |
| 146 | 147 | |
| 147 | - $where .= ' %s'; | |
| 148 | + $where .= ' %s'; | |
| 148 | 149 | $values[] = $start . self::esc_like( $value ) . $end; |
| 149 | 150 | |
| 150 | 151 | } elseif ( $value === null ) { |
| 151 | 152 | $where .= ' IS NULL'; |
| @@ -150,9 +151,9 @@ | ||
| 150 | 151 | } elseif ( $value === null ) { |
| 151 | 152 | $where .= ' IS NULL'; |
| 152 | 153 | } else { |
| 153 | 154 | // allow a - to prevent = from being added |
| 154 | - if ( substr( $key, - 1 ) == '-' ) { | |
| 155 | + if ( substr( $key, - 1 ) === '-' ) { | |
| 155 | 156 | $where = rtrim( $where, '-' ); |
| 156 | 157 | } else { |
| 157 | 158 | $where .= '='; |
| 158 | 159 | } |
| @@ -159,9 +160,9 @@ | ||
| 159 | 160 | |
| 160 | 161 | self::add_query_placeholder( $key, $value, $where ); |
| 161 | 162 | |
| 162 | 163 | $values[] = $value; |
| 163 | - } | |
| 164 | + }//end if | |
| 164 | 165 | } |
| 165 | 166 | |
| 166 | 167 | /** |
| 167 | 168 | * Add %d, or %s to query |
| @@ -167,15 +168,16 @@ | ||
| 167 | 168 | * Add %d, or %s to query |
| 168 | 169 | * |
| 169 | 170 | * @since 2.02.05 |
| 170 | 171 | * |
| 171 | - * @param string $key | |
| 172 | + * @param string $key | |
| 172 | 173 | * @param int|string $value |
| 173 | - * @param string $where | |
| 174 | + * @param string $where | |
| 174 | 175 | */ |
| 175 | 176 | private static function add_query_placeholder( $key, $value, &$where ) { |
| 176 | 177 | if ( is_numeric( $value ) && ( strpos( $key, 'meta_value' ) === false || strpos( $key, '+0' ) !== false ) ) { |
| 177 | - $value = $value + 0; // switch string to number | |
| 178 | + // Switch string to number. | |
| 179 | + $value = $value + 0; | |
| 178 | 180 | $where .= is_float( $value ) ? '%f' : '%d'; |
| 179 | 181 | } else { |
| 180 | 182 | $where .= '%s'; |
| 181 | 183 | } |
| @@ -182,10 +184,10 @@ | ||
| 182 | 184 | } |
| 183 | 185 | |
| 184 | 186 | /** |
| 185 | 187 | * @param string $table |
| 186 | - * @param array $where | |
| 187 | - * @param array $args | |
| 188 | + * @param array $where | |
| 189 | + * @param array $args | |
| 188 | 190 | * |
| 189 | 191 | * @return int |
| 190 | 192 | */ |
| 191 | 193 | public static function get_count( $table, $where = array(), $args = array() ) { |
| @@ -195,15 +197,15 @@ | ||
| 195 | 197 | } |
| 196 | 198 | |
| 197 | 199 | /** |
| 198 | 200 | * @param string $table |
| 199 | - * @param array $where | |
| 201 | + * @param array $where | |
| 200 | 202 | * @param string $field |
| 201 | - * @param array $args | |
| 203 | + * @param array $args | |
| 202 | 204 | * @param string $limit |
| 203 | 205 | * @param string $type |
| 204 | 206 | * |
| 205 | - * @return array|null|string|object | |
| 207 | + * @return array|object|string|null | |
| 206 | 208 | */ |
| 207 | 209 | public static function get_var( $table, $where = array(), $field = 'id', $args = array(), $limit = '', $type = 'var' ) { |
| 208 | 210 | $group = ''; |
| 209 | 211 | self::get_group_and_table_name( $table, $group ); |
| @@ -224,10 +226,10 @@ | ||
| 224 | 226 | * Generate a cache key from the where query, field, type, and other arguments |
| 225 | 227 | * |
| 226 | 228 | * @since 2.03.07 |
| 227 | 229 | * |
| 228 | - * @param array $where | |
| 229 | - * @param array $args | |
| 230 | + * @param array $where | |
| 231 | + * @param array $args | |
| 230 | 232 | * @param string $field |
| 231 | 233 | * @param string $type |
| 232 | 234 | * |
| 233 | 235 | * @return string |
| @@ -238,9 +240,9 @@ | ||
| 238 | 240 | foreach ( $where as $key => $value ) { |
| 239 | 241 | $cache_key .= $key . '_' . $value; |
| 240 | 242 | } |
| 241 | 243 | $cache_key .= implode( '_', $args ) . $field . '_' . $type; |
| 242 | - $cache_key = str_replace( array( ' ', ',' ), '_', $cache_key ); | |
| 244 | + $cache_key = str_replace( array( ' ', ',' ), '_', $cache_key ); | |
| 243 | 245 | |
| 244 | 246 | return $cache_key; |
| 245 | 247 | } |
| 246 | 248 | |
| @@ -245,11 +247,11 @@ | ||
| 245 | 247 | } |
| 246 | 248 | |
| 247 | 249 | /** |
| 248 | 250 | * @param string $table |
| 249 | - * @param array $where | |
| 251 | + * @param array $where | |
| 250 | 252 | * @param string $field |
| 251 | - * @param array $args | |
| 253 | + * @param array $args | |
| 252 | 254 | * @param string $limit |
| 253 | 255 | * |
| 254 | 256 | * @return mixed |
| 255 | 257 | */ |
| @@ -260,11 +262,11 @@ | ||
| 260 | 262 | /** |
| 261 | 263 | * @since 2.0 |
| 262 | 264 | * |
| 263 | 265 | * @param string $table |
| 264 | - * @param array $where | |
| 266 | + * @param array $where | |
| 265 | 267 | * @param string $fields |
| 266 | - * @param array $args | |
| 268 | + * @param array $args | |
| 267 | 269 | * |
| 268 | 270 | * @return mixed |
| 269 | 271 | */ |
| 270 | 272 | public static function get_row( $table, $where = array(), $fields = '*', $args = array() ) { |
| @@ -278,11 +280,11 @@ | ||
| 278 | 280 | * |
| 279 | 281 | * @since 2.0 |
| 280 | 282 | * |
| 281 | 283 | * @param string $table |
| 282 | - * @param array $where | |
| 284 | + * @param array $where | |
| 283 | 285 | * @param string $fields |
| 284 | - * @param array $args | |
| 286 | + * @param array $args | |
| 285 | 287 | * |
| 286 | 288 | * @return mixed |
| 287 | 289 | */ |
| 288 | 290 | public static function get_results( $table, $where = array(), $fields = '*', $args = array() ) { |
| @@ -316,10 +318,11 @@ | ||
| 316 | 318 | return ' ' . $switch_to[ $where_is ]; |
| 317 | 319 | } |
| 318 | 320 | |
| 319 | 321 | // > and < need a little more work since we don't want them switched to >= and <= |
| 320 | - if ( $where_is == '>' || $where_is == '<' ) { | |
| 321 | - return ' ' . $where_is . '-'; // the - indicates that the = should not be added later | |
| 322 | + if ( $where_is === '>' || $where_is === '<' ) { | |
| 323 | + // The - indicates that the = should not be added later. | |
| 324 | + return ' ' . $where_is . '-'; | |
| 322 | 325 | } |
| 323 | 326 | |
| 324 | 327 | // fallback to = if the query is none of these |
| 325 | 328 | return ''; |
| @@ -401,9 +404,9 @@ | ||
| 401 | 404 | * @since 2.02.05 |
| 402 | 405 | * |
| 403 | 406 | * @param string $columns |
| 404 | 407 | * @param string $table |
| 405 | - * @param array $where | |
| 408 | + * @param array $where | |
| 406 | 409 | * |
| 407 | 410 | * @return mixed |
| 408 | 411 | */ |
| 409 | 412 | public static function get_associative_array_results( $columns, $table, $where ) { |
| @@ -424,10 +427,10 @@ | ||
| 424 | 427 | * @since 2.02.05 |
| 425 | 428 | * |
| 426 | 429 | * @param string $columns |
| 427 | 430 | * @param string $table |
| 428 | - * @param mixed $where | |
| 429 | - * @param array $args | |
| 431 | + * @param mixed $where | |
| 432 | + * @param array $args | |
| 430 | 433 | * |
| 431 | 434 | * @return string |
| 432 | 435 | */ |
| 433 | 436 | private static function generate_query_string_from_pieces( $columns, $table, $where, $args = array() ) { |
| @@ -455,11 +458,11 @@ | ||
| 455 | 458 | * @since 2.05.07 |
| 456 | 459 | */ |
| 457 | 460 | private static function esc_query_args( &$args ) { |
| 458 | 461 | foreach ( $args as $param => $value ) { |
| 459 | - if ( $param == 'order_by' ) { | |
| 462 | + if ( $param === 'order_by' ) { | |
| 460 | 463 | $args[ $param ] = self::esc_order( $value ); |
| 461 | - } elseif ( $param == 'limit' ) { | |
| 464 | + } elseif ( $param === 'limit' ) { | |
| 462 | 465 | $args[ $param ] = self::esc_limit( $value ); |
| 463 | 466 | } |
| 464 | 467 | |
| 465 | 468 | if ( $args[ $param ] == '' ) { |
| @@ -472,9 +475,9 @@ | ||
| 472 | 475 | * Added for < WP 4.0 compatability |
| 473 | 476 | * |
| 474 | 477 | * @since 2.05.06 |
| 475 | 478 | * |
| 476 | - * @param string $term The value to escape | |
| 479 | + * @param string $term The value to escape. | |
| 477 | 480 | * |
| 478 | 481 | * @return string The escaped value |
| 479 | 482 | */ |
| 480 | 483 | public static function esc_like( $term ) { |
| @@ -522,17 +525,16 @@ | ||
| 522 | 525 | * @since 2.05.06 |
| 523 | 526 | */ |
| 524 | 527 | public static function esc_order_by( &$order_by ) { |
| 525 | 528 | $sort_options = array( 'asc', 'desc' ); |
| 526 | - if ( ! in_array( strtolower( $order_by ), $sort_options ) ) { | |
| 529 | + if ( ! in_array( strtolower( $order_by ), $sort_options, true ) ) { | |
| 527 | 530 | $order_by = 'asc'; |
| 528 | 531 | } |
| 529 | 532 | } |
| 530 | 533 | |
| 531 | 534 | /** |
| 535 | + * @since 2.05.06 | |
| 532 | 536 | * @param string $limit |
| 533 | - * | |
| 534 | - * @since 2.05.06 | |
| 535 | 537 | */ |
| 536 | 538 | public static function esc_limit( $limit ) { |
| 537 | 539 | if ( empty( $limit ) ) { |
| 538 | 540 | return ''; |
| @@ -567,20 +569,22 @@ | ||
| 567 | 569 | } |
| 568 | 570 | |
| 569 | 571 | /** |
| 570 | 572 | * @since 2.05.06 |
| 573 | + * | |
| 574 | + * @param string $starts_with | |
| 575 | + * @param array|string $where | |
| 576 | + * @return string | |
| 571 | 577 | */ |
| 572 | 578 | public static function prepend_and_or_where( $starts_with = ' WHERE ', $where = '' ) { |
| 573 | 579 | if ( empty( $where ) ) { |
| 574 | 580 | $where = ''; |
| 575 | - } else { | |
| 576 | - if ( is_array( $where ) ) { | |
| 581 | + } elseif ( is_array( $where ) ) { | |
| 577 | 582 | global $wpdb; |
| 578 | 583 | self::get_where_clause_and_values( $where, $starts_with ); |
| 579 | 584 | $where = $wpdb->prepare( $where['where'], $where['values'] ); // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared |
| 580 | - } else { | |
| 581 | - $where = $starts_with . $where; | |
| 582 | - } | |
| 585 | + } else { | |
| 586 | + $where = $starts_with . $where; | |
| 583 | 587 | } |
| 584 | 588 | |
| 585 | 589 | /** |
| 586 | 590 | * Allows modifying where clause when using FrmDb::prepend_and_or_where() method. |
| @@ -595,12 +599,12 @@ | ||
| 595 | 599 | |
| 596 | 600 | /** |
| 597 | 601 | * Prepare and save settings in styles and actions |
| 598 | 602 | * |
| 599 | - * @param array $settings | |
| 603 | + * @since 2.05.06 | |
| 604 | + * @param array $settings | |
| 600 | 605 | * @param string $group |
| 601 | - * | |
| 602 | - * @since 2.05.06 | |
| 606 | + * @return int|WP_Error | |
| 603 | 607 | */ |
| 604 | 608 | public static function save_settings( $settings, $group ) { |
| 605 | 609 | $settings = (array) $settings; |
| 606 | 610 | $settings['post_content'] = FrmAppHelper::prepare_and_encode( $settings['post_content'] ); |
| @@ -650,12 +654,12 @@ | ||
| 650 | 654 | * Check cache before fetching values and saving to cache |
| 651 | 655 | * |
| 652 | 656 | * @since 2.05.06 |
| 653 | 657 | * |
| 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 | |
| 658 | + * @param string $cache_key The unique name for this cache. | |
| 659 | + * @param string $group The name of the cache group. | |
| 660 | + * @param string $query If blank, don't run a db call. | |
| 661 | + * @param string $type The wpdb function to use with this query. | |
| 658 | 662 | * |
| 659 | 663 | * @return mixed $results The cache or query results |
| 660 | 664 | */ |
| 661 | 665 | public static function check_cache( $cache_key, $group = '', $query = '', $type = 'get_var', $time = 300 ) { |
| @@ -727,9 +731,9 @@ | ||
| 727 | 731 | * Delete all caching in a single group |
| 728 | 732 | * |
| 729 | 733 | * @since 2.05.06 |
| 730 | 734 | * |
| 731 | - * @param string $group The name of the cache group | |
| 735 | + * @param string $group The name of the cache group. | |
| 732 | 736 | */ |
| 733 | 737 | public static function cache_delete_group( $group ) { |
| 734 | 738 | $cached_keys = self::get_group_cached_keys( $group ); |
| 735 | 739 | |
| @@ -739,6 +743,23 @@ | ||
| 739 | 743 | } |
| 740 | 744 | |
| 741 | 745 | wp_cache_delete( 'cached_keys', $group ); |
| 742 | 746 | } |
| 747 | + } | |
| 748 | + | |
| 749 | + /** | |
| 750 | + * Checks if a DB column exists. | |
| 751 | + * | |
| 752 | + * @since 6.7 | |
| 753 | + * | |
| 754 | + * @param string $table Table name without `$wpdb->prefix`. | |
| 755 | + * @param string $column Column name. | |
| 756 | + * @return bool | |
| 757 | + */ | |
| 758 | + public static function db_column_exists( $table, $column ) { | |
| 759 | + global $wpdb; | |
| 760 | + | |
| 761 | + // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared | |
| 762 | + $result = $wpdb->get_results( $wpdb->prepare( 'SHOW COLUMNS FROM ' . $wpdb->prefix . $table . ' LIKE %s', $column ) ); | |
| 763 | + return ! empty( $result ); | |
| 743 | 764 | } |
| 744 | 765 | } |