PluginProbe
Formidable Forms – WordPress Form Builder for Contact Forms, Calculators, Quizzes & More / 6.23
Formidable Forms – WordPress Form Builder for Contact Forms, Calculators, Quizzes & More v6.23
6.35 6.34 6.33.1 6.33 6.32.1 6.32 6.31 6.25 6.25.1 6.26 6.26.1 6.27 6.28 6.29 6.3 6.3.1 6.3.2 6.30 6.4 6.4.1 6.4.2 6.5 6.5.1 6.5.2 6.5.3 All 141 releases
← All changes | classes/models/FrmDb.php +94 -79 6.46.23 View file →
@@ -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,14 +247,14 @@
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 - * @return mixed
256 + * @return array
255 257 */
256 258 public static function get_col( $table, $where = array(), $field = 'id', $args = array(), $limit = '' ) {
257 259 return self::get_var( $table, $where, $field, $args, $limit, 'col' );
258 260 }
@@ -260,17 +262,16 @@
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() ) {
271 273 $args['limit'] = 1;
272 -
273 274 return self::get_var( $table, $where, $fields, $args, '', 'row' );
274 275 }
275 276
276 277 /**
@@ -278,13 +279,13 @@
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 - * @return mixed
287 + * @return array
287 288 */
288 289 public static function get_results( $table, $where = array(), $fields = '*', $args = array() ) {
289 290 return self::get_var( $table, $where, $fields, $args, '', 'results' );
290 291 }
@@ -316,10 +317,11 @@
316 317 return ' ' . $switch_to[ $where_is ];
317 318 }
318 319
319 320 // > 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
321 + if ( $where_is === '>' || $where_is === '<' ) {
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() ) {
@@ -438,15 +440,8 @@
438 440 if ( is_array( $where ) || empty( $where ) ) {
439 441 self::get_where_clause_and_values( $where );
440 442 global $wpdb;
441 443 $query = $wpdb->prepare( $query . $where['where'] . ' ' . implode( ' ', $args ), $where['values'] ); // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared
442 - } else {
443 - /**
444 - * Allow the $where to be prepared before we recieve it here.
445 - * This is a fallback for reverse compatibility, but is not recommended
446 - */
447 - _deprecated_argument( 'where', '2.0', esc_html__( 'Use the query in an array format so it can be properly prepared.', 'formidable' ) );
448 - $query .= $where . ' ' . implode( ' ', $args );
449 444 }
450 445
451 446 return $query;
452 447 }
@@ -455,11 +450,11 @@
455 450 * @since 2.05.07
456 451 */
457 452 private static function esc_query_args( &$args ) {
458 453 foreach ( $args as $param => $value ) {
459 - if ( $param == 'order_by' ) {
454 + if ( $param === 'order_by' ) {
460 455 $args[ $param ] = self::esc_order( $value );
461 - } elseif ( $param == 'limit' ) {
456 + } elseif ( $param === 'limit' ) {
462 457 $args[ $param ] = self::esc_limit( $value );
463 458 }
464 459
465 460 if ( $args[ $param ] == '' ) {
@@ -468,13 +463,13 @@
468 463 }
469 464 }
470 465
471 466 /**
472 - * Added for < WP 4.0 compatability
467 + * Added for < WP 4.0 compatibility
473 468 *
474 469 * @since 2.05.06
475 470 *
476 - * @param string $term The value to escape
471 + * @param string $term The value to escape.
477 472 *
478 473 * @return string The escaped value
479 474 */
480 475 public static function esc_like( $term ) {
@@ -492,9 +487,9 @@
492 487 if ( empty( $order_query ) ) {
493 488 return '';
494 489 }
495 490
496 - // remove ORDER BY before santizing
491 + // Remove ORDER BY before sanitizing.
497 492 $order_query = strtolower( $order_query );
498 493 if ( strpos( $order_query, 'order by' ) !== false ) {
499 494 $order_query = str_replace( 'order by', '', $order_query );
500 495 }
@@ -522,17 +517,16 @@
522 517 * @since 2.05.06
523 518 */
524 519 public static function esc_order_by( &$order_by ) {
525 520 $sort_options = array( 'asc', 'desc' );
526 - if ( ! in_array( strtolower( $order_by ), $sort_options ) ) {
521 + if ( ! in_array( strtolower( $order_by ), $sort_options, true ) ) {
527 522 $order_by = 'asc';
528 523 }
529 524 }
530 525
531 526 /**
527 + * @since 2.05.06
532 528 * @param string $limit
533 - *
534 - * @since 2.05.06
535 529 */
536 530 public static function esc_limit( $limit ) {
537 531 if ( empty( $limit ) ) {
538 532 return '';
@@ -567,20 +561,22 @@
567 561 }
568 562
569 563 /**
570 564 * @since 2.05.06
565 + *
566 + * @param string $starts_with
567 + * @param array|string $where
568 + * @return string
571 569 */
572 570 public static function prepend_and_or_where( $starts_with = ' WHERE ', $where = '' ) {
573 571 if ( empty( $where ) ) {
574 572 $where = '';
575 - } else {
576 - if ( is_array( $where ) ) {
573 + } elseif ( is_array( $where ) ) {
577 574 global $wpdb;
578 575 self::get_where_clause_and_values( $where, $starts_with );
579 576 $where = $wpdb->prepare( $where['where'], $where['values'] ); // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared
580 - } else {
581 - $where = $starts_with . $where;
582 - }
577 + } else {
578 + $where = $starts_with . $where;
583 579 }
584 580
585 581 /**
586 582 * Allows modifying where clause when using FrmDb::prepend_and_or_where() method.
@@ -595,12 +591,12 @@
595 591
596 592 /**
597 593 * Prepare and save settings in styles and actions
598 594 *
599 - * @param array $settings
595 + * @since 2.05.06
596 + * @param array $settings
600 597 * @param string $group
601 - *
602 - * @since 2.05.06
598 + * @return int|WP_Error
603 599 */
604 600 public static function save_settings( $settings, $group ) {
605 601 $settings = (array) $settings;
606 602 $settings['post_content'] = FrmAppHelper::prepare_and_encode( $settings['post_content'] );
@@ -650,24 +646,26 @@
650 646 * Check cache before fetching values and saving to cache
651 647 *
652 648 * @since 2.05.06
653 649 *
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
650 + * @param string $cache_key The unique name for this cache.
651 + * @param string $group The name of the cache group.
652 + * @param string $query If blank, don't run a db call.
653 + * @param string $type The wpdb function to use with this query.
658 654 *
659 655 * @return mixed $results The cache or query results
660 656 */
661 657 public static function check_cache( $cache_key, $group = '', $query = '', $type = 'get_var', $time = 300 ) {
662 - $results = wp_cache_get( $cache_key, $group );
663 - if ( ! FrmAppHelper::is_empty_value( $results, false ) || empty( $query ) ) {
658 + $found = null;
659 + $results = wp_cache_get( $cache_key, $group, false, $found );
660 +
661 + if ( ( $found === true && $results !== false ) || empty( $query ) ) {
664 662 return $results;
665 663 }
666 664
667 665 if ( 'get_posts' == $type ) {
668 666 $results = get_posts( $query );
669 - } elseif ( 'get_associative_results' == $type ) {
667 + } elseif ( 'get_associative_results' === $type ) {
670 668 global $wpdb;
671 669 $results = $wpdb->get_results( $query, OBJECT_K ); // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared
672 670 } else {
673 671 global $wpdb;
@@ -727,9 +725,9 @@
727 725 * Delete all caching in a single group
728 726 *
729 727 * @since 2.05.06
730 728 *
731 - * @param string $group The name of the cache group
729 + * @param string $group The name of the cache group.
732 730 */
733 731 public static function cache_delete_group( $group ) {
734 732 $cached_keys = self::get_group_cached_keys( $group );
735 733
@@ -739,6 +737,23 @@
739 737 }
740 738
741 739 wp_cache_delete( 'cached_keys', $group );
742 740 }
741 + }
742 +
743 + /**
744 + * Checks if a DB column exists.
745 + *
746 + * @since 6.7
747 + *
748 + * @param string $table Table name without `$wpdb->prefix`.
749 + * @param string $column Column name.
750 + * @return bool
751 + */
752 + public static function db_column_exists( $table, $column ) {
753 + global $wpdb;
754 +
755 + // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared
756 + $result = $wpdb->get_results( $wpdb->prepare( 'SHOW COLUMNS FROM ' . $wpdb->prefix . $table . ' LIKE %s', $column ) );
757 + return ! empty( $result );
743 758 }
744 759 }