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