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