PluginProbe
Formidable Forms – WordPress Form Builder for Contact Forms, Calculators, Quizzes & More / 6.8
Formidable Forms – WordPress Form Builder for Contact Forms, Calculators, Quizzes & More v6.8
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 +48 -155 6.266.8 View file →
@@ -3,35 +3,11 @@
3 3 die( 'You are not allowed to call this page directly.' );
4 4 }
5 5
6 6 class FrmDb {
7 -
8 - /**
9 - * The table name for Formidable Fields.
10 - *
11 - * @var string
12 - */
13 7 public $fields;
14 -
15 - /**
16 - * The table name for Formidable Forms.
17 - *
18 - * @var string
19 - */
20 8 public $forms;
21 -
22 - /**
23 - * The table name for Formidable Entries.
24 - *
25 - * @var string
26 - */
27 9 public $entries;
28 -
29 - /**
30 - * The table name for Formidable Entry Metas.
31 - *
32 - * @var string
33 - */
34 10 public $entry_metas;
35 11
36 12 public function __construct() {
37 13 if ( ! defined( 'ABSPATH' ) ) {
@@ -50,10 +26,8 @@
50 26 * Change array into format $wpdb->prepare can use
51 27 *
52 28 * @param array $args
53 29 * @param string $starts_with
54 - *
55 - * @return void
56 30 */
57 31 public static function get_where_clause_and_values( &$args, $starts_with = ' WHERE ' ) {
58 32 if ( empty( $args ) ) {
59 33 // add an arg to prevent prepare from failing
@@ -80,14 +54,11 @@
80 54 * @param array $args
81 55 * @param string $base_where
82 56 * @param string $where
83 57 * @param array $values
84 - *
85 - * @return void
86 58 */
87 59 public static function parse_where_from_array( $args, $base_where, &$where, &$values ) {
88 60 $condition = ' AND';
89 -
90 61 if ( isset( $args['or'] ) ) {
91 62 $condition = ' OR';
92 63 unset( $args['or'] );
93 64 }
@@ -92,15 +63,13 @@
92 63 unset( $args['or'] );
93 64 }
94 65
95 66 foreach ( $args as $key => $value ) {
96 - $where .= empty( $where ) ? $base_where : $condition;
67 + $where .= empty( $where ) ? $base_where : $condition;
97 68 $array_inc_null = ( ! is_numeric( $key ) && is_array( $value ) && in_array( null, $value ) );
98 -
99 69 if ( is_numeric( $key ) || $array_inc_null ) {
100 - $where .= ' ( ';
70 + $where .= ' ( ';
101 71 $nested_where = '';
102 -
103 72 if ( $array_inc_null ) {
104 73 foreach ( $value as $val ) {
105 74 $parse_where = array(
106 75 $key => $val,
@@ -110,23 +79,21 @@
110 79 }
111 80 } else {
112 81 self::parse_where_from_array( $value, '', $nested_where, $values );
113 82 }
114 -
115 83 $where .= $nested_where;
116 84 $where .= ' ) ';
117 85 } else {
118 86 self::interpret_array_to_sql( $key, $value, $where, $values );
119 - }//end if
87 + }
120 88 }//end foreach
121 89 }
122 90
123 91 /**
124 92 * @param string $key
125 - * @param array|string $value
93 + * @param string|array $value
126 94 * @param string $where
127 95 * @param array $values
128 - *
129 96 * @return void
130 97 */
131 98 private static function interpret_array_to_sql( $key, $value, &$where, &$values ) {
132 99 $key = trim( $key );
@@ -143,25 +110,22 @@
143 110
144 111 if ( is_array( $value ) ) {
145 112 // translate array of values to "in"
146 113 if ( strpos( $lowercase_key, 'like' ) !== false ) {
147 - $where = preg_replace( '/' . $key . '$/', '', $where );
114 + $where = preg_replace( '/' . $key . '$/', '', $where );
148 115 $where .= '(';
149 - $start = true;
150 -
116 + $start = true;
151 117 foreach ( $value as $v ) {
152 118 if ( ! $start ) {
153 119 $where .= ' OR ';
154 120 }
155 -
156 121 $start = false;
157 - $where .= $key . ' %s';
122 + $where .= $key . ' %s';
158 123 $values[] = '%' . self::esc_like( $v ) . '%';
159 124 }
160 -
161 125 $where .= ')';
162 126 } elseif ( ! empty( $value ) ) {
163 - $where .= ' in (' . self::prepare_array_values( $value, '%s' ) . ')';
127 + $where .= ' in (' . self::prepare_array_values( $value, '%s' ) . ')';
164 128 $values = array_merge( $values, $value );
165 129 }
166 130 } elseif ( strpos( $lowercase_key, 'like' ) !== false ) {
167 131 /**
@@ -170,19 +134,18 @@
170 134 * If the key is %like then skip the last % for ends with
171 135 */
172 136 $start = '%';
173 137 $end = '%';
174 -
175 - if ( $lowercase_key === 'like%' ) {
138 + if ( $lowercase_key == 'like%' ) {
176 139 $start = '';
177 140 $where = rtrim( $where, '%' );
178 - } elseif ( $lowercase_key === '%like' ) {
179 - $end = '';
180 - $where = rtrim( rtrim( $where, '%like' ), '%LIKE' );
141 + } elseif ( $lowercase_key == '%like' ) {
142 + $end = '';
143 + $where = rtrim( rtrim( $where, '%like' ), '%LIKE' );
181 144 $where .= 'like';
182 145 }
183 146
184 - $where .= ' %s';
147 + $where .= ' %s';
185 148 $values[] = $start . self::esc_like( $value ) . $end;
186 149
187 150 } elseif ( $value === null ) {
188 151 $where .= ' IS NULL';
@@ -187,9 +150,9 @@
187 150 } elseif ( $value === null ) {
188 151 $where .= ' IS NULL';
189 152 } else {
190 153 // allow a - to prevent = from being added
191 - if ( substr( $key, - 1 ) === '-' ) {
154 + if ( substr( $key, - 1 ) == '-' ) {
192 155 $where = rtrim( $where, '-' );
193 156 } else {
194 157 $where .= '=';
195 158 }
@@ -207,15 +170,13 @@
207 170 *
208 171 * @param string $key
209 172 * @param int|string $value
210 173 * @param string $where
211 - *
212 - * @return void
213 174 */
214 175 private static function add_query_placeholder( $key, $value, &$where ) {
215 176 if ( is_numeric( $value ) && ( strpos( $key, 'meta_value' ) === false || strpos( $key, '+0' ) !== false ) ) {
216 177 // Switch string to number.
217 - $value = $value + 0;
178 + $value = $value + 0;
218 179 $where .= is_float( $value ) ? '%f' : '%d';
219 180 } else {
220 181 $where .= '%s';
221 182 }
@@ -241,15 +202,14 @@
241 202 * @param array $args
242 203 * @param string $limit
243 204 * @param string $type
244 205 *
245 - * @return array|object|string|null
206 + * @return array|null|string|object
246 207 */
247 208 public static function get_var( $table, $where = array(), $field = 'id', $args = array(), $limit = '', $type = 'var' ) {
248 209 $group = '';
249 210 self::get_group_and_table_name( $table, $group );
250 211 self::convert_options_to_array( $args, '', $limit );
251 -
252 212 if ( $type === 'var' && ! isset( $args['limit'] ) ) {
253 213 $args['limit'] = 1;
254 214 }
255 215
@@ -275,15 +235,13 @@
275 235 */
276 236 public static function generate_cache_key( $where, $args, $field, $type ) {
277 237 $cache_key = '';
278 238 $where = FrmAppHelper::array_flatten( $where );
279 -
280 239 foreach ( $where as $key => $value ) {
281 240 $cache_key .= $key . '_' . $value;
282 241 }
283 -
284 242 $cache_key .= implode( '_', $args ) . $field . '_' . $type;
285 - $cache_key = str_replace( array( ' ', ',' ), '_', $cache_key );
243 + $cache_key = str_replace( array( ' ', ',' ), '_', $cache_key );
286 244
287 245 return $cache_key;
288 246 }
289 247
@@ -293,9 +251,9 @@
293 251 * @param string $field
294 252 * @param array $args
295 253 * @param string $limit
296 254 *
297 - * @return array
255 + * @return mixed
298 256 */
299 257 public static function get_col( $table, $where = array(), $field = 'id', $args = array(), $limit = '' ) {
300 258 return self::get_var( $table, $where, $field, $args, $limit, 'col' );
301 259 }
@@ -311,8 +269,9 @@
311 269 * @return mixed
312 270 */
313 271 public static function get_row( $table, $where = array(), $fields = '*', $args = array() ) {
314 272 $args['limit'] = 1;
273 +
315 274 return self::get_var( $table, $where, $fields, $args, '', 'row' );
316 275 }
317 276
318 277 /**
@@ -324,9 +283,9 @@
324 283 * @param array $where
325 284 * @param string $fields
326 285 * @param array $args
327 286 *
328 - * @return array
287 + * @return mixed
329 288 */
330 289 public static function get_results( $table, $where = array(), $fields = '*', $args = array() ) {
331 290 return self::get_var( $table, $where, $fields, $args, '', 'results' );
332 291 }
@@ -353,15 +312,14 @@
353 312 '%like' => '%like',
354 313 );
355 314
356 315 $where_is = strtolower( $where_is );
357 -
358 316 if ( isset( $switch_to[ $where_is ] ) ) {
359 317 return ' ' . $switch_to[ $where_is ];
360 318 }
361 319
362 320 // > and < need a little more work since we don't want them switched to >= and <=
363 - if ( $where_is === '>' || $where_is === '<' ) {
321 + if ( $where_is == '>' || $where_is == '<' ) {
364 322 // The - indicates that the = should not be added later.
365 323 return ' ' . $where_is . '-';
366 324 }
367 325
@@ -374,19 +332,17 @@
374 332 * Also add the wpdb->prefix to the table if it's missing
375 333 *
376 334 * @param string $table
377 335 * @param string $group
378 - *
379 - * @return void
380 336 */
381 337 private static function get_group_and_table_name( &$table, &$group ) {
382 - global $wpdb;
338 + global $wpdb, $wpmuBaseTablePrefix;
383 339
384 340 $table_parts = explode( ' ', $table );
385 341 $group = reset( $table_parts );
386 342 self::maybe_remove_prefix( $wpdb->prefix, $group );
387 343
388 - $prefix = $wpdb->base_prefix;
344 + $prefix = $wpmuBaseTablePrefix ? $wpmuBaseTablePrefix : $wpdb->base_prefix;
389 345 self::maybe_remove_prefix( $prefix, $group );
390 346
391 347 if ( $group == $table ) {
392 348 $table = $wpdb->prefix . $table;
@@ -399,13 +355,8 @@
399 355 /**
400 356 * Only remove the db prefix when at the beginning.
401 357 *
402 358 * @since 4.04.02
403 - *
404 - * @param string $prefix Prefix to remove.
405 - * @param string $name Name to strip prefix from, passed by reference.
406 - *
407 - * @return void
408 359 */
409 360 private static function maybe_remove_prefix( $prefix, &$name ) {
410 361 if ( substr( $name, 0, strlen( $prefix ) ) === $prefix ) {
411 362 $name = substr( $name, strlen( $prefix ) );
@@ -411,15 +362,8 @@
411 362 $name = substr( $name, strlen( $prefix ) );
412 363 }
413 364 }
414 365
415 - /**
416 - * @param array|string $args
417 - * @param string $order_by
418 - * @param int|string $limit
419 - *
420 - * @return void
421 - */
422 366 private static function convert_options_to_array( &$args, $order_by = '', $limit = '' ) {
423 367 if ( ! is_array( $args ) ) {
424 368 $args = array( 'order_by' => $args );
425 369 }
@@ -432,9 +376,8 @@
432 376 $args['limit'] = $limit;
433 377 }
434 378
435 379 $temp_args = $args;
436 -
437 380 foreach ( $temp_args as $k => $v ) {
438 381 if ( $v == '' ) {
439 382 unset( $args[ $k ] );
440 383 continue;
@@ -440,9 +383,8 @@
440 383 continue;
441 384 }
442 385
443 386 $db_name = strtoupper( str_replace( '_', ' ', $k ) );
444 -
445 387 if ( strpos( $v, $db_name ) === false ) {
446 388 $args[ $k ] = $db_name . ' ' . $v;
447 389 }
448 390 }
@@ -498,8 +440,15 @@
498 440 if ( is_array( $where ) || empty( $where ) ) {
499 441 self::get_where_clause_and_values( $where );
500 442 global $wpdb;
501 443 $query = $wpdb->prepare( $query . $where['where'] . ' ' . implode( ' ', $args ), $where['values'] ); // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared
444 + } else {
445 + /**
446 + * Allow the $where to be prepared before we recieve it here.
447 + * This is a fallback for reverse compatibility, but is not recommended
448 + */
449 + _deprecated_argument( 'where', '2.0', esc_html__( 'Use the query in an array format so it can be properly prepared.', 'formidable' ) );
450 + $query .= $where . ' ' . implode( ' ', $args );
502 451 }
503 452
504 453 return $query;
505 454 }
@@ -505,18 +454,14 @@
505 454 }
506 455
507 456 /**
508 457 * @since 2.05.07
509 - *
510 - * @param array $args Query arguments, passed by reference.
511 - *
512 - * @return void
513 458 */
514 459 private static function esc_query_args( &$args ) {
515 460 foreach ( $args as $param => $value ) {
516 - if ( $param === 'order_by' ) {
461 + if ( $param == 'order_by' ) {
517 462 $args[ $param ] = self::esc_order( $value );
518 - } elseif ( $param === 'limit' ) {
463 + } elseif ( $param == 'limit' ) {
519 464 $args[ $param ] = self::esc_limit( $value );
520 465 }
521 466
522 467 if ( $args[ $param ] == '' ) {
@@ -525,9 +470,9 @@
525 470 }
526 471 }
527 472
528 473 /**
529 - * Added for < WP 4.0 compatibility
474 + * Added for < WP 4.0 compatability
530 475 *
531 476 * @since 2.05.06
532 477 *
533 478 * @param string $term The value to escape.
@@ -543,10 +488,8 @@
543 488 /**
544 489 * @since 2.05.06
545 490 *
546 491 * @param string $order_query
547 - *
548 - * @return string
549 492 */
550 493 public static function esc_order( $order_query ) {
551 494 if ( empty( $order_query ) ) {
552 495 return '';
@@ -551,11 +494,10 @@
551 494 if ( empty( $order_query ) ) {
552 495 return '';
553 496 }
554 497
555 - // Remove ORDER BY before sanitizing.
498 + // remove ORDER BY before santizing
556 499 $order_query = strtolower( $order_query );
557 -
558 500 if ( strpos( $order_query, 'order by' ) !== false ) {
559 501 $order_query = str_replace( 'order by', '', $order_query );
560 502 }
561 503
@@ -562,15 +504,13 @@
562 504 $order_query = explode( ' ', trim( $order_query ) );
563 505
564 506 $order = trim( reset( $order_query ) );
565 507 $safe_order = array( 'count(*)' );
566 -
567 508 if ( ! in_array( strtolower( $order ), $safe_order ) ) {
568 509 $order = preg_replace( '/[^a-zA-Z0-9\-\_\.\+]/', '', $order );
569 510 }
570 511
571 512 $order_by = '';
572 -
573 513 if ( count( $order_query ) > 1 ) {
574 514 $order_by = end( $order_query );
575 515 self::esc_order_by( $order_by );
576 516 }
@@ -581,27 +521,20 @@
581 521 /**
582 522 * Make sure this is ordering by either ASC or DESC
583 523 *
584 524 * @since 2.05.06
585 - *
586 - * @param string $order_by Sort direction, passed by reference.
587 - *
588 - * @return void
589 525 */
590 526 public static function esc_order_by( &$order_by ) {
591 527 $sort_options = array( 'asc', 'desc' );
592 -
593 - if ( ! in_array( strtolower( $order_by ), $sort_options, true ) ) {
528 + if ( ! in_array( strtolower( $order_by ), $sort_options ) ) {
594 529 $order_by = 'asc';
595 530 }
596 531 }
597 532
598 533 /**
599 - * @since 2.05.06
600 - *
601 534 * @param string $limit
602 535 *
603 - * @return string
536 + * @since 2.05.06
604 537 */
605 538 public static function esc_limit( $limit ) {
606 539 if ( empty( $limit ) ) {
607 540 return '';
@@ -607,15 +540,13 @@
607 540 return '';
608 541 }
609 542
610 543 $limit = trim( str_replace( 'limit ', '', strtolower( $limit ) ) );
611 -
612 544 if ( is_numeric( $limit ) ) {
613 545 return ' LIMIT ' . $limit;
614 546 }
615 547
616 548 $limit = explode( ',', trim( $limit ) );
617 -
618 549 foreach ( $limit as $k => $l ) {
619 550 if ( is_numeric( $l ) ) {
620 551 $limit[ $k ] = $l;
621 552 }
@@ -629,13 +560,8 @@
629 560 /**
630 561 * Get an array of values ready to go through $wpdb->prepare
631 562 *
632 563 * @since 2.05.06
633 - *
634 - * @param array $array Array of values.
635 - * @param string $type Placeholder type.
636 - *
637 - * @return string
638 564 */
639 565 public static function prepare_array_values( $array, $type = '%s' ) {
640 566 $placeholders = array_fill( 0, count( $array ), $type );
641 567
@@ -643,23 +569,20 @@
643 569 }
644 570
645 571 /**
646 572 * @since 2.05.06
647 - *
648 - * @param string $starts_with
649 - * @param array|string $where
650 - *
651 - * @return string
652 573 */
653 574 public static function prepend_and_or_where( $starts_with = ' WHERE ', $where = '' ) {
654 575 if ( empty( $where ) ) {
655 576 $where = '';
656 - } elseif ( is_array( $where ) ) {
577 + } else {
578 + if ( is_array( $where ) ) {
657 579 global $wpdb;
658 580 self::get_where_clause_and_values( $where, $starts_with );
659 581 $where = $wpdb->prepare( $where['where'], $where['values'] ); // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared
660 - } else {
661 - $where = $starts_with . $where;
582 + } else {
583 + $where = $starts_with . $where;
584 + }
662 585 }
663 586
664 587 /**
665 588 * Allows modifying where clause when using FrmDb::prepend_and_or_where() method.
@@ -674,14 +597,12 @@
674 597
675 598 /**
676 599 * Prepare and save settings in styles and actions
677 600 *
601 + * @param array $settings
602 + * @param string $group
603 + *
678 604 * @since 2.05.06
679 - *
680 - * @param array|object $settings
681 - * @param string $group
682 - *
683 - * @return int|WP_Error
684 605 */
685 606 public static function save_settings( $settings, $group ) {
686 607 $settings = (array) $settings;
687 608 $settings['post_content'] = FrmAppHelper::prepare_and_encode( $settings['post_content'] );
@@ -705,14 +626,12 @@
705 626 *
706 627 * @since 2.05.06
707 628 *
708 629 * @param array $settings
709 - *
710 630 * @return int|WP_Error
711 631 */
712 632 public static function save_json_post( $settings ) {
713 633 global $wp_filter;
714 -
715 634 if ( isset( $wp_filter['content_save_pre'] ) ) {
716 635 $filters = $wp_filter['content_save_pre'];
717 636 }
718 637
@@ -737,23 +656,20 @@
737 656 * @param string $cache_key The unique name for this cache.
738 657 * @param string $group The name of the cache group.
739 658 * @param string $query If blank, don't run a db call.
740 659 * @param string $type The wpdb function to use with this query.
741 - * @param int $time Cache expiration time in seconds.
742 660 *
743 661 * @return mixed $results The cache or query results
744 662 */
745 663 public static function check_cache( $cache_key, $group = '', $query = '', $type = 'get_var', $time = 300 ) {
746 - $found = null;
747 - $results = wp_cache_get( $cache_key, $group, false, $found );
748 -
749 - if ( ( $found === true && $results !== false ) || empty( $query ) ) {
664 + $results = wp_cache_get( $cache_key, $group );
665 + if ( ! FrmAppHelper::is_empty_value( $results, false ) || empty( $query ) ) {
750 666 return $results;
751 667 }
752 668
753 669 if ( 'get_posts' == $type ) {
754 670 $results = get_posts( $query );
755 - } elseif ( 'get_associative_results' === $type ) {
671 + } elseif ( 'get_associative_results' == $type ) {
756 672 global $wpdb;
757 673 $results = $wpdb->get_results( $query, OBJECT_K ); // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared
758 674 } else {
759 675 global $wpdb;
@@ -766,15 +682,8 @@
766 682 }
767 683
768 684 /**
769 685 * @since 2.05.06
770 - *
771 - * @param string $cache_key The unique name for this cache.
772 - * @param mixed $results Cached results.
773 - * @param string $group The name of the cache group.
774 - * @param int $time Cache expiration time in seconds.
775 - *
776 - * @return void
777 686 */
778 687 public static function set_cache( $cache_key, $results, $group = '', $time = 300 ) {
779 688 if ( ! FrmAppHelper::prevent_caching() ) {
780 689 self::add_key_to_group_cache( $cache_key, $group );
@@ -786,13 +695,8 @@
786 695 * Keep track of the keys cached in each group so they can be deleted
787 696 * in Redis and Memcache
788 697 *
789 698 * @since 2.05.06
790 - *
791 - * @param string $key Cache key.
792 - * @param string $group Cache group name.
793 - *
794 - * @return void
795 699 */
796 700 public static function add_key_to_group_cache( $key, $group ) {
797 701 $cached = self::get_group_cached_keys( $group );
798 702 $cached[ $key ] = $key;
@@ -800,16 +704,11 @@
800 704 }
801 705
802 706 /**
803 707 * @since 2.05.06
804 - *
805 - * @param string $group Cache group name.
806 - *
807 - * @return array
808 708 */
809 709 public static function get_group_cached_keys( $group ) {
810 710 $cached = wp_cache_get( 'cached_keys', $group );
811 -
812 711 if ( ! $cached || ! is_array( $cached ) ) {
813 712 $cached = array();
814 713 }
815 714
@@ -818,12 +717,9 @@
818 717
819 718 /**
820 719 * @since 2.05.06
821 720 *
822 - * @param string $cache_key Cache key to delete.
823 - * @param string $group Cache group name.
824 - *
825 - * @return void
721 + * @param string $cache_key
826 722 */
827 723 public static function delete_cache_and_transient( $cache_key, $group = 'default' ) {
828 724 delete_transient( $cache_key );
829 725 wp_cache_delete( $cache_key, $group );
@@ -834,10 +730,8 @@
834 730 *
835 731 * @since 2.05.06
836 732 *
837 733 * @param string $group The name of the cache group.
838 - *
839 - * @return void
840 734 */
841 735 public static function cache_delete_group( $group ) {
842 736 $cached_keys = self::get_group_cached_keys( $group );
843 737
@@ -856,9 +750,8 @@
856 750 * @since 6.7
857 751 *
858 752 * @param string $table Table name without `$wpdb->prefix`.
859 753 * @param string $column Column name.
860 - *
861 754 * @return bool
862 755 */
863 756 public static function db_column_exists( $table, $column ) {
864 757 global $wpdb;