PluginProbe
Formidable Forms – WordPress Form Builder for Contact Forms, Calculators, Quizzes & More / 6.17
Formidable Forms – WordPress Form Builder for Contact Forms, Calculators, Quizzes & More v6.17
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 +13 -125 6.266.17 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 }
@@ -505,12 +448,8 @@
505 448 }
506 449
507 450 /**
508 451 * @since 2.05.07
509 - *
510 - * @param array $args Query arguments, passed by reference.
511 - *
512 - * @return void
513 452 */
514 453 private static function esc_query_args( &$args ) {
515 454 foreach ( $args as $param => $value ) {
516 455 if ( $param === 'order_by' ) {
@@ -543,10 +482,8 @@
543 482 /**
544 483 * @since 2.05.06
545 484 *
546 485 * @param string $order_query
547 - *
548 - * @return string
549 486 */
550 487 public static function esc_order( $order_query ) {
551 488 if ( empty( $order_query ) ) {
552 489 return '';
@@ -553,9 +490,8 @@
553 490 }
554 491
555 492 // Remove ORDER BY before sanitizing.
556 493 $order_query = strtolower( $order_query );
557 -
558 494 if ( strpos( $order_query, 'order by' ) !== false ) {
559 495 $order_query = str_replace( 'order by', '', $order_query );
560 496 }
561 497
@@ -562,15 +498,13 @@
562 498 $order_query = explode( ' ', trim( $order_query ) );
563 499
564 500 $order = trim( reset( $order_query ) );
565 501 $safe_order = array( 'count(*)' );
566 -
567 502 if ( ! in_array( strtolower( $order ), $safe_order ) ) {
568 503 $order = preg_replace( '/[^a-zA-Z0-9\-\_\.\+]/', '', $order );
569 504 }
570 505
571 506 $order_by = '';
572 -
573 507 if ( count( $order_query ) > 1 ) {
574 508 $order_by = end( $order_query );
575 509 self::esc_order_by( $order_by );
576 510 }
@@ -581,16 +515,11 @@
581 515 /**
582 516 * Make sure this is ordering by either ASC or DESC
583 517 *
584 518 * @since 2.05.06
585 - *
586 - * @param string $order_by Sort direction, passed by reference.
587 - *
588 - * @return void
589 519 */
590 520 public static function esc_order_by( &$order_by ) {
591 521 $sort_options = array( 'asc', 'desc' );
592 -
593 522 if ( ! in_array( strtolower( $order_by ), $sort_options, true ) ) {
594 523 $order_by = 'asc';
595 524 }
596 525 }
@@ -596,12 +525,9 @@
596 525 }
597 526
598 527 /**
599 528 * @since 2.05.06
600 - *
601 529 * @param string $limit
602 - *
603 - * @return string
604 530 */
605 531 public static function esc_limit( $limit ) {
606 532 if ( empty( $limit ) ) {
607 533 return '';
@@ -607,15 +533,13 @@
607 533 return '';
608 534 }
609 535
610 536 $limit = trim( str_replace( 'limit ', '', strtolower( $limit ) ) );
611 -
612 537 if ( is_numeric( $limit ) ) {
613 538 return ' LIMIT ' . $limit;
614 539 }
615 540
616 541 $limit = explode( ',', trim( $limit ) );
617 -
618 542 foreach ( $limit as $k => $l ) {
619 543 if ( is_numeric( $l ) ) {
620 544 $limit[ $k ] = $l;
621 545 }
@@ -629,13 +553,8 @@
629 553 /**
630 554 * Get an array of values ready to go through $wpdb->prepare
631 555 *
632 556 * @since 2.05.06
633 - *
634 - * @param array $array Array of values.
635 - * @param string $type Placeholder type.
636 - *
637 - * @return string
638 557 */
639 558 public static function prepare_array_values( $array, $type = '%s' ) {
640 559 $placeholders = array_fill( 0, count( $array ), $type );
641 560
@@ -646,9 +565,8 @@
646 565 * @since 2.05.06
647 566 *
648 567 * @param string $starts_with
649 568 * @param array|string $where
650 - *
651 569 * @return string
652 570 */
653 571 public static function prepend_and_or_where( $starts_with = ' WHERE ', $where = '' ) {
654 572 if ( empty( $where ) ) {
@@ -675,12 +593,10 @@
675 593 /**
676 594 * Prepare and save settings in styles and actions
677 595 *
678 596 * @since 2.05.06
679 - *
680 - * @param array|object $settings
681 - * @param string $group
682 - *
597 + * @param array $settings
598 + * @param string $group
683 599 * @return int|WP_Error
684 600 */
685 601 public static function save_settings( $settings, $group ) {
686 602 $settings = (array) $settings;
@@ -705,14 +621,12 @@
705 621 *
706 622 * @since 2.05.06
707 623 *
708 624 * @param array $settings
709 - *
710 625 * @return int|WP_Error
711 626 */
712 627 public static function save_json_post( $settings ) {
713 628 global $wp_filter;
714 -
715 629 if ( isset( $wp_filter['content_save_pre'] ) ) {
716 630 $filters = $wp_filter['content_save_pre'];
717 631 }
718 632
@@ -737,23 +651,20 @@
737 651 * @param string $cache_key The unique name for this cache.
738 652 * @param string $group The name of the cache group.
739 653 * @param string $query If blank, don't run a db call.
740 654 * @param string $type The wpdb function to use with this query.
741 - * @param int $time Cache expiration time in seconds.
742 655 *
743 656 * @return mixed $results The cache or query results
744 657 */
745 658 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 ) ) {
659 + $results = wp_cache_get( $cache_key, $group );
660 + if ( ! FrmAppHelper::is_empty_value( $results, false ) || empty( $query ) ) {
750 661 return $results;
751 662 }
752 663
753 664 if ( 'get_posts' == $type ) {
754 665 $results = get_posts( $query );
755 - } elseif ( 'get_associative_results' === $type ) {
666 + } elseif ( 'get_associative_results' == $type ) {
756 667 global $wpdb;
757 668 $results = $wpdb->get_results( $query, OBJECT_K ); // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared
758 669 } else {
759 670 global $wpdb;
@@ -766,15 +677,8 @@
766 677 }
767 678
768 679 /**
769 680 * @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 681 */
778 682 public static function set_cache( $cache_key, $results, $group = '', $time = 300 ) {
779 683 if ( ! FrmAppHelper::prevent_caching() ) {
780 684 self::add_key_to_group_cache( $cache_key, $group );
@@ -786,13 +690,8 @@
786 690 * Keep track of the keys cached in each group so they can be deleted
787 691 * in Redis and Memcache
788 692 *
789 693 * @since 2.05.06
790 - *
791 - * @param string $key Cache key.
792 - * @param string $group Cache group name.
793 - *
794 - * @return void
795 694 */
796 695 public static function add_key_to_group_cache( $key, $group ) {
797 696 $cached = self::get_group_cached_keys( $group );
798 697 $cached[ $key ] = $key;
@@ -800,16 +699,11 @@
800 699 }
801 700
802 701 /**
803 702 * @since 2.05.06
804 - *
805 - * @param string $group Cache group name.
806 - *
807 - * @return array
808 703 */
809 704 public static function get_group_cached_keys( $group ) {
810 705 $cached = wp_cache_get( 'cached_keys', $group );
811 -
812 706 if ( ! $cached || ! is_array( $cached ) ) {
813 707 $cached = array();
814 708 }
815 709
@@ -818,12 +712,9 @@
818 712
819 713 /**
820 714 * @since 2.05.06
821 715 *
822 - * @param string $cache_key Cache key to delete.
823 - * @param string $group Cache group name.
824 - *
825 - * @return void
716 + * @param string $cache_key
826 717 */
827 718 public static function delete_cache_and_transient( $cache_key, $group = 'default' ) {
828 719 delete_transient( $cache_key );
829 720 wp_cache_delete( $cache_key, $group );
@@ -834,10 +725,8 @@
834 725 *
835 726 * @since 2.05.06
836 727 *
837 728 * @param string $group The name of the cache group.
838 - *
839 - * @return void
840 729 */
841 730 public static function cache_delete_group( $group ) {
842 731 $cached_keys = self::get_group_cached_keys( $group );
843 732
@@ -856,9 +745,8 @@
856 745 * @since 6.7
857 746 *
858 747 * @param string $table Table name without `$wpdb->prefix`.
859 748 * @param string $column Column name.
860 - *
861 749 * @return bool
862 750 */
863 751 public static function db_column_exists( $table, $column ) {
864 752 global $wpdb;