Formidable Forms – Form Builder for WordPress - Version 2.05.06

Version Description

  • Tweak: Move database functions from FrmAppHelper to FrmDb
  • Tweak: Move install/update/migrate functions from FrmDb to FrmMigrate
  • Tweak: Remove unused check_cache_and_transient function
  • Fix: Rename ja_JP translation to ja
  • Pro Version Forms
  • New: Add frm_load_ajax_field_scripts hook to allow custom field types to load scripts on the first page of the ajax form
  • Tweak: Only check for shortcodes in calculations if it includes a bracket [ to reduce processing time
  • Tweak: Speed up load time for long forms with field calculations by preventing the calculations from running when the total field is not on the current page.
  • Fix: Better sanitizing before running view query
  • Fix: Save the HTML in the "no entries" message for views
  • Fix: Save all filter settings correctly in the view
  • Tweak: A few adjustments for the entry shortcode array. Use the child entry id in the returned entry array and include the child form id in the entry array. This fixes issues with API entry updating.
Download this release

Release Info

Developer sswells
Plugin Icon 128x128 Formidable Forms – Form Builder for WordPress
Version 2.05.06
Comparing to
See all releases

Code changes from version 2.05.05 to 2.05.06

classes/controllers/FrmAppController.php CHANGED
@@ -361,13 +361,13 @@ class FrmAppController {
361
  }
362
 
363
  public static function activation_install() {
364
- FrmAppHelper::delete_cache_and_transient( 'frm_plugin_version' );
365
  FrmFormActionsController::actions_init();
366
  self::install();
367
  }
368
 
369
  public static function install( $old_db_version = false ) {
370
- $frmdb = new FrmDb();
371
  $frmdb->upgrade( $old_db_version );
372
  }
373
 
@@ -375,7 +375,7 @@ class FrmAppController {
375
  FrmAppHelper::permission_check('administrator');
376
  check_ajax_referer( 'frm_ajax', 'nonce' );
377
 
378
- $frmdb = new FrmDb();
379
  $frmdb->uninstall();
380
 
381
  //disable the plugin and redirect after uninstall so the tables don't get added right back
361
  }
362
 
363
  public static function activation_install() {
364
+ FrmDb::delete_cache_and_transient( 'frm_plugin_version' );
365
  FrmFormActionsController::actions_init();
366
  self::install();
367
  }
368
 
369
  public static function install( $old_db_version = false ) {
370
+ $frmdb = new FrmMigrate();
371
  $frmdb->upgrade( $old_db_version );
372
  }
373
 
375
  FrmAppHelper::permission_check('administrator');
376
  check_ajax_referer( 'frm_ajax', 'nonce' );
377
 
378
+ $frmdb = new FrmMigrate();
379
  $frmdb->uninstall();
380
 
381
  //disable the plugin and redirect after uninstall so the tables don't get added right back
classes/controllers/FrmFormActionsController.php CHANGED
@@ -229,7 +229,7 @@ class FrmFormActionsController {
229
  foreach ( $old_actions as $old_id ) {
230
  wp_delete_post( $old_id );
231
  }
232
- FrmAppHelper::cache_delete_group( 'frm_actions' );
233
  }
234
  }
235
 
229
  foreach ( $old_actions as $old_id ) {
230
  wp_delete_post( $old_id );
231
  }
232
+ FrmDb::cache_delete_group( 'frm_actions' );
233
  }
234
  }
235
 
classes/helpers/FrmAppHelper.php CHANGED
@@ -10,7 +10,7 @@ class FrmAppHelper {
10
  /**
11
  * @since 2.0
12
  */
13
- public static $plug_version = '2.05.05';
14
 
15
  /**
16
  * @since 1.07.02
@@ -514,114 +514,6 @@ class FrmAppHelper {
514
  }
515
  }
516
 
517
- /**
518
- * Check cache before fetching values and saving to cache
519
- *
520
- * @since 2.0
521
- *
522
- * @param string $cache_key The unique name for this cache
523
- * @param string $group The name of the cache group
524
- * @param string $query If blank, don't run a db call
525
- * @param string $type The wpdb function to use with this query
526
- * @return mixed $results The cache or query results
527
- */
528
- public static function check_cache( $cache_key, $group = '', $query = '', $type = 'get_var', $time = 300 ) {
529
- $results = wp_cache_get($cache_key, $group);
530
- if ( ! self::is_empty_value( $results, false ) || empty($query) ) {
531
- return $results;
532
- }
533
-
534
- if ( 'get_posts' == $type ) {
535
- $results = get_posts($query);
536
- } else if ( 'get_associative_results' == $type ) {
537
- global $wpdb;
538
- $results = $wpdb->get_results( $query, OBJECT_K );
539
- } else {
540
- global $wpdb;
541
- $results = $wpdb->{$type}($query);
542
- }
543
-
544
- self::set_cache( $cache_key, $results, $group, $time );
545
-
546
- return $results;
547
- }
548
-
549
- public static function set_cache( $cache_key, $results, $group = '', $time = 300 ) {
550
- if ( ! self::prevent_caching() ) {
551
- self::add_key_to_group_cache( $cache_key, $group );
552
- wp_cache_set( $cache_key, $results, $group, $time );
553
- }
554
- }
555
-
556
- /**
557
- * Keep track of the keys cached in each group so they can be deleted
558
- * in Redis and Memcache
559
- */
560
- public static function add_key_to_group_cache( $key, $group ) {
561
- $cached = self::get_group_cached_keys( $group );
562
- $cached[ $key ] = $key;
563
- wp_cache_set( 'cached_keys', $cached, $group, 300 );
564
- }
565
-
566
- public static function get_group_cached_keys( $group ) {
567
- $cached = wp_cache_get( 'cached_keys', $group );
568
- if ( ! $cached || ! is_array( $cached ) ) {
569
- $cached = array();
570
- }
571
-
572
- return $cached;
573
- }
574
-
575
- /**
576
- * Data that should be stored for a long time can be stored in a transient.
577
- * First check the cache, then check the transient
578
- * @since 2.0
579
- * @return mixed The cached value or false
580
- */
581
- public static function check_cache_and_transient( $cache_key ) {
582
- // check caching layer first
583
- $results = self::check_cache( $cache_key );
584
- if ( $results ) {
585
- return $results;
586
- }
587
-
588
- // then check the transient
589
- $results = get_transient($cache_key);
590
- if ( $results ) {
591
- wp_cache_set($cache_key, $results);
592
- }
593
-
594
- return $results;
595
- }
596
-
597
- /**
598
- * @since 2.0
599
- * @param string $cache_key
600
- */
601
- public static function delete_cache_and_transient( $cache_key, $group = 'default' ) {
602
- delete_transient($cache_key);
603
- wp_cache_delete( $cache_key, $group );
604
- }
605
-
606
- /**
607
- * Delete all caching in a single group
608
- *
609
- * @since 2.0
610
- *
611
- * @param string $group The name of the cache group
612
- */
613
- public static function cache_delete_group( $group ) {
614
- $cached_keys = self::get_group_cached_keys( $group );
615
-
616
- if ( ! empty( $cached_keys ) ) {
617
- foreach ( $cached_keys as $key ) {
618
- wp_cache_delete( $key, $group );
619
- }
620
-
621
- wp_cache_delete( 'cached_keys', $group );
622
- }
623
- }
624
-
625
  /**
626
  * Check a value from a shortcode to see if true or false.
627
  * True when value is 1, true, 'true', 'yes'
@@ -881,7 +773,7 @@ class FrmAppHelper {
881
  if ( is_array( $value ) ) {
882
  $original_function = $function;
883
  if ( count( $value ) ) {
884
- $function = explode( ', ', self::prepare_array_values( $value, $function ) );
885
  } else {
886
  $function = array( $function );
887
  }
@@ -993,6 +885,7 @@ class FrmAppHelper {
993
  return $user_id;
994
  }
995
 
 
996
  if ( $user_id == 'current' ) {
997
  $user_id = get_current_user_id();
998
  } else {
@@ -1422,121 +1315,6 @@ class FrmAppHelper {
1422
  );
1423
  }
1424
 
1425
- /**
1426
- * Added for < WP 4.0 compatability
1427
- *
1428
- * @since 1.07.10
1429
- *
1430
- * @param string $term The value to escape
1431
- * @return string The escaped value
1432
- */
1433
- public static function esc_like( $term ) {
1434
- global $wpdb;
1435
- if ( method_exists($wpdb, 'esc_like') ) {
1436
- // WP 4.0
1437
- $term = $wpdb->esc_like( $term );
1438
- } else {
1439
- $term = like_escape( $term );
1440
- }
1441
-
1442
- return $term;
1443
- }
1444
-
1445
- /**
1446
- * @param string $order_query
1447
- */
1448
- public static function esc_order( $order_query ) {
1449
- if ( empty($order_query) ) {
1450
- return '';
1451
- }
1452
-
1453
- // remove ORDER BY before santizing
1454
- $order_query = strtolower($order_query);
1455
- if ( strpos($order_query, 'order by') !== false ) {
1456
- $order_query = str_replace('order by', '', $order_query);
1457
- }
1458
-
1459
- $order_query = explode(' ', trim($order_query));
1460
-
1461
- $order_fields = array(
1462
- 'id', 'form_key', 'name', 'description',
1463
- 'parent_form_id', 'logged_in', 'is_template',
1464
- 'default_template', 'status', 'created_at',
1465
- );
1466
-
1467
- $order = trim(trim(reset($order_query), ','));
1468
- if ( ! in_array($order, $order_fields) ) {
1469
- return '';
1470
- }
1471
-
1472
- $order_by = '';
1473
- if ( count($order_query) > 1 ) {
1474
- $order_by = end( $order_query );
1475
- self::esc_order_by( $order_by );
1476
- }
1477
-
1478
- return ' ORDER BY ' . $order . ' ' . $order_by;
1479
- }
1480
-
1481
- /**
1482
- * Make sure this is ordering by either ASC or DESC
1483
- */
1484
- public static function esc_order_by( &$order_by ) {
1485
- $sort_options = array( 'asc', 'desc' );
1486
- if ( ! in_array( strtolower( $order_by ), $sort_options ) ) {
1487
- $order_by = 'asc';
1488
- }
1489
- }
1490
-
1491
- /**
1492
- * @param string $limit
1493
- */
1494
- public static function esc_limit( $limit ) {
1495
- if ( empty($limit) ) {
1496
- return '';
1497
- }
1498
-
1499
- $limit = trim(str_replace(' limit', '', strtolower($limit)));
1500
- if ( is_numeric($limit) ) {
1501
- return ' LIMIT ' . $limit;
1502
- }
1503
-
1504
- $limit = explode(',', trim($limit));
1505
- foreach ( $limit as $k => $l ) {
1506
- if ( is_numeric( $l ) ) {
1507
- $limit[ $k ] = $l;
1508
- }
1509
- }
1510
-
1511
- $limit = implode(',', $limit);
1512
- return ' LIMIT ' . $limit;
1513
- }
1514
-
1515
- /**
1516
- * Get an array of values ready to go through $wpdb->prepare
1517
- * @since 2.0
1518
- */
1519
- public static function prepare_array_values( $array, $type = '%s' ) {
1520
- $placeholders = array_fill(0, count($array), $type);
1521
- return implode(', ', $placeholders);
1522
- }
1523
-
1524
- public static function prepend_and_or_where( $starts_with = ' WHERE ', $where = '' ) {
1525
- if ( empty($where) ) {
1526
- return '';
1527
- }
1528
-
1529
- if ( is_array( $where ) ) {
1530
- global $wpdb;
1531
- FrmDb::get_where_clause_and_values( $where, $starts_with );
1532
- $where = $wpdb->prepare( $where['where'], $where['values'] );
1533
- } else {
1534
- $where = $starts_with . $where;
1535
- }
1536
-
1537
- return $where;
1538
- }
1539
-
1540
  // Pagination Methods
1541
 
1542
  /**
@@ -1715,52 +1493,6 @@ class FrmAppHelper {
1715
  }
1716
  }
1717
 
1718
- /**
1719
- * Prepare and save settings in styles and actions
1720
- *
1721
- * @param array $settings
1722
- * @param string $group
1723
- *
1724
- * @since 2.0.6
1725
- */
1726
- public static function save_settings( $settings, $group ) {
1727
- $settings = (array) $settings;
1728
- $settings['post_content'] = FrmAppHelper::prepare_and_encode( $settings['post_content'] );
1729
-
1730
- if ( empty( $settings['ID'] ) ) {
1731
- unset( $settings['ID']);
1732
- }
1733
-
1734
- // delete all caches for this group
1735
- self::cache_delete_group( $group );
1736
-
1737
- return self::save_json_post( $settings );
1738
- }
1739
-
1740
- /**
1741
- * Since actions are JSON encoded, we don't want any filters messing with it.
1742
- * Remove the filters and then add them back in case any posts or views are
1743
- * also being imported.
1744
- *
1745
- * Used when saving form actions and styles
1746
- *
1747
- * @since 2.0.4
1748
- */
1749
- public static function save_json_post( $settings ) {
1750
- global $wp_filter;
1751
- $filters = $wp_filter['content_save_pre'];
1752
-
1753
- // Remove the balanceTags filter in case WordPress is trying to validate the XHTML
1754
- remove_all_filters( 'content_save_pre' );
1755
-
1756
- $post = wp_insert_post( $settings );
1757
-
1758
- // add the content filters back for views or posts
1759
- $wp_filter['content_save_pre'] = $filters;
1760
-
1761
- return $post;
1762
- }
1763
-
1764
  public static function maybe_json_decode( $string ) {
1765
  if ( is_array($string) ) {
1766
  return $string;
@@ -1963,4 +1695,163 @@ class FrmAppHelper {
1963
 
1964
  return $locales;
1965
  }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1966
  }
10
  /**
11
  * @since 2.0
12
  */
13
+ public static $plug_version = '2.05.06';
14
 
15
  /**
16
  * @since 1.07.02
514
  }
515
  }
516
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
517
  /**
518
  * Check a value from a shortcode to see if true or false.
519
  * True when value is 1, true, 'true', 'yes'
773
  if ( is_array( $value ) ) {
774
  $original_function = $function;
775
  if ( count( $value ) ) {
776
+ $function = explode( ', ', FrmDb::prepare_array_values( $value, $function ) );
777
  } else {
778
  $function = array( $function );
779
  }
885
  return $user_id;
886
  }
887
 
888
+ $user_id = sanitize_text_field( $user_id );
889
  if ( $user_id == 'current' ) {
890
  $user_id = get_current_user_id();
891
  } else {
1315
  );
1316
  }
1317
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1318
  // Pagination Methods
1319
 
1320
  /**
1493
  }
1494
  }
1495
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1496
  public static function maybe_json_decode( $string ) {
1497
  if ( is_array($string) ) {
1498
  return $string;
1695
 
1696
  return $locales;
1697
  }
1698
+
1699
+ /**
1700
+ * Prepare and save settings in styles and actions
1701
+ *
1702
+ * @param array $settings
1703
+ * @param string $group
1704
+ *
1705
+ * @since 2.0.6
1706
+ * @deprecated 2.05.06
1707
+ */
1708
+ public static function save_settings( $settings, $group ) {
1709
+ _deprecated_function( __METHOD__, '2.05.06', 'FrmDb::' . __FUNCTION__ );
1710
+ return FrmDb::save_settings( $settings, $group );
1711
+ }
1712
+
1713
+ /**
1714
+ * Since actions are JSON encoded, we don't want any filters messing with it.
1715
+ * Remove the filters and then add them back in case any posts or views are
1716
+ * also being imported.
1717
+ *
1718
+ * Used when saving form actions and styles
1719
+ *
1720
+ * @since 2.0.4
1721
+ * @deprecated 2.05.06
1722
+ */
1723
+ public static function save_json_post( $settings ) {
1724
+ _deprecated_function( __METHOD__, '2.05.06', 'FrmDb::' . __FUNCTION__ );
1725
+ return FrmDb::save_json_post( $settings );
1726
+ }
1727
+
1728
+ /**
1729
+ * Check cache before fetching values and saving to cache
1730
+ *
1731
+ * @since 2.0
1732
+ * @deprecated 2.05.06
1733
+ *
1734
+ * @param string $cache_key The unique name for this cache
1735
+ * @param string $group The name of the cache group
1736
+ * @param string $query If blank, don't run a db call
1737
+ * @param string $type The wpdb function to use with this query
1738
+ * @return mixed $results The cache or query results
1739
+ */
1740
+ public static function check_cache( $cache_key, $group = '', $query = '', $type = 'get_var', $time = 300 ) {
1741
+ _deprecated_function( __METHOD__, '2.05.06', 'FrmDb::' . __FUNCTION__ );
1742
+ return FrmDb::check_cache( $cache_key, $group, $query, $type, $time );
1743
+ }
1744
+
1745
+ /**
1746
+ * @deprecated 2.05.06
1747
+ */
1748
+ public static function set_cache( $cache_key, $results, $group = '', $time = 300 ) {
1749
+ _deprecated_function( __METHOD__, '2.05.06', 'FrmDb::' . __FUNCTION__ );
1750
+ FrmDb::set_cache( $cache_key, $results, $group, $time );
1751
+ }
1752
+
1753
+ /**
1754
+ * Keep track of the keys cached in each group so they can be deleted
1755
+ * in Redis and Memcache
1756
+ * @deprecated 2.05.06
1757
+ */
1758
+ public static function add_key_to_group_cache( $key, $group ) {
1759
+ _deprecated_function( __METHOD__, '2.05.06', 'FrmDb::' . __FUNCTION__ );
1760
+ FrmDb::add_key_to_group_cache( $key, $group );
1761
+ }
1762
+
1763
+ public static function get_group_cached_keys( $group ) {
1764
+ _deprecated_function( __METHOD__, '2.05.06', 'FrmDb::' . __FUNCTION__ );
1765
+ return FrmDb::get_group_cached_keys( $group );
1766
+ }
1767
+
1768
+ /**
1769
+ * @since 2.0
1770
+ * @deprecated 2.05.06
1771
+ * @return mixed The cached value or false
1772
+ */
1773
+ public static function check_cache_and_transient( $cache_key ) {
1774
+ _deprecated_function( __METHOD__, '2.05.06', 'FrmDb::' . __FUNCTION__ );
1775
+ return FrmDb::check_cache( $cache_key );
1776
+ }
1777
+
1778
+ /**
1779
+ * @since 2.0
1780
+ * @deprecated 2.05.06
1781
+ * @param string $cache_key
1782
+ */
1783
+ public static function delete_cache_and_transient( $cache_key, $group = 'default' ) {
1784
+ _deprecated_function( __METHOD__, '2.05.06', 'FrmDb::' . __FUNCTION__ );
1785
+ FrmDb::delete_cache_and_transient( $cache_key, $group );
1786
+ }
1787
+
1788
+ /**
1789
+ * @since 2.0
1790
+ * @deprecated 2.05.06
1791
+ *
1792
+ * @param string $group The name of the cache group
1793
+ */
1794
+ public static function cache_delete_group( $group ) {
1795
+ _deprecated_function( __METHOD__, '2.05.06', 'FrmDb::' . __FUNCTION__ );
1796
+ FrmDb::cache_delete_group( $group );
1797
+ }
1798
+
1799
+ /**
1800
+ * Added for < WP 4.0 compatability
1801
+ *
1802
+ * @since 1.07.10
1803
+ * @deprecated 2.05.06
1804
+ *
1805
+ * @param string $term The value to escape
1806
+ * @return string The escaped value
1807
+ */
1808
+ public static function esc_like( $term ) {
1809
+ _deprecated_function( __METHOD__, '2.05.06', 'FrmDb::' . __FUNCTION__ );
1810
+ return FrmDb::esc_like( $term );
1811
+ }
1812
+
1813
+ /**
1814
+ * @param string $order_query
1815
+ * @deprecated 2.05.06
1816
+ */
1817
+ public static function esc_order( $order_query ) {
1818
+ _deprecated_function( __METHOD__, '2.05.06', 'FrmDb::' . __FUNCTION__ );
1819
+ return FrmDb::esc_order( $order_query );
1820
+ }
1821
+
1822
+ /**
1823
+ * Make sure this is ordering by either ASC or DESC
1824
+ * @deprecated 2.05.06
1825
+ */
1826
+ public static function esc_order_by( &$order_by ) {
1827
+ _deprecated_function( __METHOD__, '2.05.06', 'FrmDb::' . __FUNCTION__ );
1828
+ FrmDb::esc_order_by( $order_by );
1829
+ }
1830
+
1831
+ /**
1832
+ * @param string $limit
1833
+ * @deprecated 2.05.06
1834
+ */
1835
+ public static function esc_limit( $limit ) {
1836
+ _deprecated_function( __METHOD__, '2.05.06', 'FrmDb::' . __FUNCTION__ );
1837
+ return FrmDb::esc_limit( $limit );
1838
+ }
1839
+
1840
+ /**
1841
+ * Get an array of values ready to go through $wpdb->prepare
1842
+ * @since 2.0
1843
+ * @deprecated 2.05.06
1844
+ */
1845
+ public static function prepare_array_values( $array, $type = '%s' ) {
1846
+ _deprecated_function( __METHOD__, '2.05.06', 'FrmDb::' . __FUNCTION__ );
1847
+ return FrmDb::prepare_array_values( $array, $type );
1848
+ }
1849
+
1850
+ /**
1851
+ * @deprecated 2.05.06
1852
+ */
1853
+ public static function prepend_and_or_where( $starts_with = ' WHERE ', $where = '' ) {
1854
+ _deprecated_function( __METHOD__, '2.05.06', 'FrmDb::' . __FUNCTION__ );
1855
+ return FrmDb::prepend_and_or_where( $starts_with, $where );
1856
+ }
1857
  }
classes/helpers/FrmEntriesListHelper.php CHANGED
@@ -44,7 +44,8 @@ class FrmEntriesListHelper extends FrmListHelper {
44
  $page = $this->get_pagenum();
45
  $start = (int) isset( $_REQUEST['start'] ) ? absint( $_REQUEST['start'] ) : ( ( $page - 1 ) * $per_page );
46
 
47
- $this->items = FrmEntry::getAll( $s_query, $order, ' LIMIT ' . $start . ',' . $per_page, true, $join_form_in_query );
 
48
  $total_items = FrmEntry::getRecordCount($s_query);
49
 
50
  $this->set_pagination_args( array(
44
  $page = $this->get_pagenum();
45
  $start = (int) isset( $_REQUEST['start'] ) ? absint( $_REQUEST['start'] ) : ( ( $page - 1 ) * $per_page );
46
 
47
+ $limit = FrmDb::esc_limit( $start . ',' . $per_page );
48
+ $this->items = FrmEntry::getAll( $s_query, $order, $limit, true, $join_form_in_query );
49
  $total_items = FrmEntry::getRecordCount($s_query);
50
 
51
  $this->set_pagination_args( array(
classes/helpers/FrmXMLHelper.php CHANGED
@@ -983,7 +983,7 @@ class FrmXMLHelper {
983
 
984
  if ( ! $exists ) {
985
  // this isn't an email, but we need to use a class that will always be included
986
- FrmAppHelper::save_json_post( $new_action );
987
  $imported['imported']['actions']++;
988
  }
989
  }
@@ -1073,7 +1073,7 @@ class FrmXMLHelper {
1073
  ) );
1074
 
1075
  if ( empty($exists) ) {
1076
- FrmAppHelper::save_json_post( $new_notification );
1077
  $imported['imported']['actions']++;
1078
  }
1079
  unset($new_notification);
983
 
984
  if ( ! $exists ) {
985
  // this isn't an email, but we need to use a class that will always be included
986
+ FrmDb::save_json_post( $new_action );
987
  $imported['imported']['actions']++;
988
  }
989
  }
1073
  ) );
1074
 
1075
  if ( empty($exists) ) {
1076
+ FrmDb::save_json_post( $new_notification );
1077
  $imported['imported']['actions']++;
1078
  }
1079
  unset($new_notification);
classes/models/FrmDb.php CHANGED
@@ -1,16 +1,17 @@
1
  <?php
2
 
3
  class FrmDb {
4
- var $fields;
5
- var $forms;
6
- var $entries;
7
- var $entry_metas;
8
 
9
  public function __construct() {
10
  if ( ! defined('ABSPATH') ) {
11
  die('You are not allowed to call this page directly.');
12
  }
13
 
 
14
  global $wpdb;
15
  $this->fields = $wpdb->prefix . 'frm_fields';
16
  $this->forms = $wpdb->prefix . 'frm_forms';
@@ -18,179 +19,6 @@ class FrmDb {
18
  $this->entry_metas = $wpdb->prefix . 'frm_item_metas';
19
  }
20
 
21
- public function upgrade( $old_db_version = false ) {
22
- do_action( 'frm_before_install' );
23
-
24
- global $wpdb;
25
- //$frm_db_version is the version of the database we're moving to
26
- $frm_db_version = FrmAppHelper::$db_version;
27
- $old_db_version = (float) $old_db_version;
28
- if ( ! $old_db_version ) {
29
- $old_db_version = get_option('frm_db_version');
30
- }
31
-
32
- if ( $frm_db_version != $old_db_version ) {
33
- // update rewrite rules for views and other custom post types
34
- flush_rewrite_rules();
35
-
36
- require_once( ABSPATH . 'wp-admin/includes/upgrade.php' );
37
-
38
- $this->create_tables();
39
- $this->migrate_data($frm_db_version, $old_db_version);
40
-
41
- /***** SAVE DB VERSION *****/
42
- update_option('frm_db_version', $frm_db_version);
43
-
44
- /**** ADD/UPDATE DEFAULT TEMPLATES ****/
45
- FrmXMLController::add_default_templates();
46
-
47
- if ( ! $old_db_version ) {
48
- $this->maybe_create_contact_form();
49
- }
50
- }
51
-
52
- do_action('frm_after_install');
53
-
54
- /**** update the styling settings ****/
55
- if ( is_admin() && function_exists( 'get_filesystem_method' ) ) {
56
- $frm_style = new FrmStyle();
57
- $frm_style->update( 'default' );
58
- }
59
- }
60
-
61
- public function collation() {
62
- global $wpdb;
63
- if ( ! $wpdb->has_cap( 'collation' ) ) {
64
- return '';
65
- }
66
-
67
- $charset_collate = '';
68
- if ( ! empty( $wpdb->charset ) ) {
69
- $charset_collate .= ' DEFAULT CHARACTER SET ' . $wpdb->charset;
70
- }
71
-
72
- if ( ! empty( $wpdb->collate ) ) {
73
- $charset_collate .= ' COLLATE ' . $wpdb->collate;
74
- }
75
-
76
- return $charset_collate;
77
- }
78
-
79
- private function create_tables() {
80
- $charset_collate = $this->collation();
81
- $sql = array();
82
-
83
- /* Create/Upgrade Fields Table */
84
- $sql[] = 'CREATE TABLE ' . $this->fields . ' (
85
- id BIGINT(20) NOT NULL auto_increment,
86
- field_key varchar(100) default NULL,
87
- name text default NULL,
88
- description longtext default NULL,
89
- type text default NULL,
90
- default_value longtext default NULL,
91
- options longtext default NULL,
92
- field_order int(11) default 0,
93
- required int(1) default NULL,
94
- field_options longtext default NULL,
95
- form_id int(11) default NULL,
96
- created_at datetime NOT NULL,
97
- PRIMARY KEY (id),
98
- KEY form_id (form_id),
99
- UNIQUE KEY field_key (field_key)
100
- )';
101
-
102
- /* Create/Upgrade Forms Table */
103
- $sql[] = 'CREATE TABLE ' . $this->forms . ' (
104
- id int(11) NOT NULL auto_increment,
105
- form_key varchar(100) default NULL,
106
- name varchar(255) default NULL,
107
- description text default NULL,
108
- parent_form_id int(11) default 0,
109
- logged_in tinyint(1) default NULL,
110
- editable tinyint(1) default NULL,
111
- is_template tinyint(1) default 0,
112
- default_template tinyint(1) default 0,
113
- status varchar(255) default NULL,
114
- options longtext default NULL,
115
- created_at datetime NOT NULL,
116
- PRIMARY KEY (id),
117
- UNIQUE KEY form_key (form_key)
118
- )';
119
-
120
- /* Create/Upgrade Items Table */
121
- $sql[] = 'CREATE TABLE ' . $this->entries . ' (
122
- id BIGINT(20) NOT NULL auto_increment,
123
- item_key varchar(100) default NULL,
124
- name varchar(255) default NULL,
125
- description text default NULL,
126
- ip text default NULL,
127
- form_id BIGINT(20) default NULL,
128
- post_id BIGINT(20) default NULL,
129
- user_id BIGINT(20) default NULL,
130
- parent_item_id BIGINT(20) default 0,
131
- is_draft tinyint(1) default 0,
132
- updated_by BIGINT(20) default NULL,
133
- created_at datetime NOT NULL,
134
- updated_at datetime NOT NULL,
135
- PRIMARY KEY (id),
136
- KEY form_id (form_id),
137
- KEY post_id (post_id),
138
- KEY user_id (user_id),
139
- KEY parent_item_id (parent_item_id),
140
- UNIQUE KEY item_key (item_key)
141
- )';
142
-
143
- /* Create/Upgrade Meta Table */
144
- $sql[] = 'CREATE TABLE ' . $this->entry_metas . ' (
145
- id BIGINT(20) NOT NULL auto_increment,
146
- meta_value longtext default NULL,
147
- field_id BIGINT(20) NOT NULL,
148
- item_id BIGINT(20) NOT NULL,
149
- created_at datetime NOT NULL,
150
- PRIMARY KEY (id),
151
- KEY field_id (field_id),
152
- KEY item_id (item_id)
153
- )';
154
-
155
- foreach ( $sql as $q ) {
156
- if ( function_exists( 'dbDelta' ) ) {
157
- dbDelta( $q . $charset_collate . ';' );
158
- } else {
159
- global $wpdb;
160
- $wpdb->query( $q . $charset_collate );
161
- }
162
- unset($q);
163
- }
164
- }
165
-
166
- private function maybe_create_contact_form() {
167
- $template_id = FrmForm::getIdByKey( 'contact' );
168
- if ( $template_id ) {
169
- $form_id = FrmForm::duplicate( $template_id, false, true );
170
- if ( $form_id ) {
171
- $values = array(
172
- 'status' => 'published',
173
- 'form_key' => 'contact-form',
174
- );
175
- FrmForm::update( $form_id, $values );
176
- }
177
- }
178
- }
179
-
180
- /**
181
- * @param integer $frm_db_version
182
- * @param int $old_db_version
183
- */
184
- private function migrate_data( $frm_db_version, $old_db_version ) {
185
- $migrations = array( 4, 6, 11, 16, 17, 23, 25 );
186
- foreach ( $migrations as $migration ) {
187
- if ( $frm_db_version >= $migration && $old_db_version < $migration ) {
188
- $function_name = 'migrate_to_' . $migration;
189
- $this->$function_name();
190
- }
191
- }
192
- }
193
-
194
  /**
195
  * Change array into format $wpdb->prepare can use
196
  *
@@ -281,11 +109,11 @@ class FrmDb {
281
  }
282
  $start = false;
283
  $where .= $key . ' %s';
284
- $values[] = '%' . FrmAppHelper::esc_like( $v ) . '%';
285
  }
286
  $where .= ')';
287
  } else if ( ! empty( $value ) ) {
288
- $where .= ' in (' . FrmAppHelper::prepare_array_values( $value, '%s' ) . ')';
289
  $values = array_merge( $values, $value );
290
  }
291
  } else if ( strpos( $lowercase_key, 'like' ) !== false ) {
@@ -306,7 +134,7 @@ class FrmDb {
306
  }
307
 
308
  $where .= ' %s';
309
- $values[] = $start . FrmAppHelper::esc_like( $value ) . $end;
310
 
311
  } else if ( $value === null ) {
312
  $where .= ' IS NULL';
@@ -368,7 +196,7 @@ class FrmDb {
368
  $query = self::generate_query_string_from_pieces( $field, $table, $where, $args );
369
 
370
  $cache_key = self::generate_cache_key( $where, $args, $field, $type );
371
- $results = FrmAppHelper::check_cache( $cache_key, $group, $query, 'get_' . $type );
372
  return $results;
373
  }
374
 
@@ -516,7 +344,7 @@ class FrmDb {
516
  }
517
 
518
  if ( $k == 'limit' ) {
519
- $args[ $k ] = FrmAppHelper::esc_limit( $v );
520
  }
521
  $db_name = strtoupper( str_replace( '_', ' ', $k ) );
522
  if ( strpos( $v, $db_name ) === false ) {
@@ -548,7 +376,7 @@ class FrmDb {
548
  $query = self::generate_query_string_from_pieces( $columns, $table, $where );
549
 
550
  $cache_key = str_replace( array( ' ', ',' ), '_', trim( implode( '_', FrmAppHelper::array_flatten( $where ) ) . $columns . '_results_ARRAY_A' , ' WHERE' ) );
551
- $results = FrmAppHelper::check_cache( $cache_key, $group, $query, 'get_associative_results' );
552
 
553
  return $results;
554
  }
@@ -583,276 +411,293 @@ class FrmDb {
583
  return $query;
584
  }
585
 
586
- public function uninstall() {
587
- if ( ! current_user_can( 'administrator' ) ) {
588
- $frm_settings = FrmAppHelper::get_settings();
589
- wp_die($frm_settings->admin_permission);
 
 
 
 
 
 
 
 
 
 
 
590
  }
591
 
592
- global $wpdb, $wp_roles;
593
-
594
- $wpdb->query( 'DROP TABLE IF EXISTS ' . $this->fields );
595
- $wpdb->query( 'DROP TABLE IF EXISTS ' . $this->forms );
596
- $wpdb->query( 'DROP TABLE IF EXISTS ' . $this->entries );
597
- $wpdb->query( 'DROP TABLE IF EXISTS ' . $this->entry_metas );
598
-
599
- delete_option('frm_options');
600
- delete_option('frm_db_version');
601
-
602
- //delete roles
603
- $frm_roles = FrmAppHelper::frm_capabilities();
604
- $roles = get_editable_roles();
605
- foreach ( $frm_roles as $frm_role => $frm_role_description ) {
606
- foreach ( $roles as $role => $details ) {
607
- $wp_roles->remove_cap( $role, $frm_role );
608
- unset($role, $details);
609
- }
610
- unset($frm_role, $frm_role_description);
611
  }
612
- unset($roles, $frm_roles);
613
 
614
- // delete actions, views, and styles
615
 
616
- // prevent the post deletion from triggering entries to be deleted
617
- remove_action( 'before_delete_post', 'FrmProDisplaysController::before_delete_post' );
618
- remove_action( 'deleted_post', 'FrmProEntriesController::delete_entry' );
 
 
619
 
620
- $post_ids = $wpdb->get_col( $wpdb->prepare( 'SELECT ID FROM ' . $wpdb->posts . ' WHERE post_type in (%s, %s, %s)', FrmFormActionsController::$action_post_type, FrmStylesController::$post_type, 'frm_display' ) );
621
- foreach ( $post_ids as $post_id ) {
622
- // Delete's each post.
623
- wp_delete_post( $post_id, true );
624
  }
625
- unset( $post_ids );
626
 
627
- // delete transients
628
- delete_transient( 'frmpro_css' );
629
- delete_transient( 'frm_options' );
630
- delete_transient( 'frmpro_options' );
 
631
 
632
- $wpdb->query( $wpdb->prepare( 'DELETE FROM ' . $wpdb->options . ' WHERE option_name LIKE %s OR option_name LIKE %s', '_transient_timeout_frm_form_fields_%', '_transient_frm_form_fields_%' ) );
 
633
 
634
- do_action('frm_after_uninstall');
635
- return true;
636
- }
 
 
 
 
 
 
 
637
 
638
  /**
639
- * Migrate old styling settings. If sites are using the old
640
- * default 400px field width, switch it to 100%
641
- *
642
- * @since 2.0.4
643
  */
644
- private function migrate_to_25() {
645
- // get the style that was created with the style migration
646
- $frm_style = new FrmStyle();
647
- $styles = $frm_style->get_all( 'post_date', 'ASC', 1 );
648
- if ( empty( $styles ) ) {
649
- return;
 
 
650
  }
651
 
652
- foreach ( $styles as $style ) {
653
- if ( $style->post_content['field_width'] == '400px' ) {
654
- $style->post_content['field_width'] = '100%';
655
- $frm_style->save( (array) $style );
656
- return;
657
  }
658
  }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
659
  }
660
 
661
  /**
662
- * Check if the parent_form_id columns exists.
663
- * If not, try and add it again
664
  *
665
- * @since 2.0.2
 
 
 
666
  */
667
- private function migrate_to_23() {
668
- global $wpdb;
669
- $exists = $wpdb->get_row( 'SHOW COLUMNS FROM ' . $this->forms . ' LIKE "parent_form_id"' );
670
- if ( empty( $exists ) ) {
671
- $wpdb->query( 'ALTER TABLE ' . $this->forms . ' ADD parent_form_id int(11) default 0' );
 
672
  }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
673
  }
674
 
675
  /**
676
- * Change field size from character to pixel -- Multiply by 9
 
 
 
 
 
 
 
 
677
  */
678
- private function migrate_to_17() {
679
- global $wpdb;
680
- $pixel_conversion = 9;
 
 
681
 
682
- // Get query arguments
683
- $field_types = array( 'textarea', 'text', 'number', 'email', 'url', 'rte', 'date', 'phone', 'password', 'image', 'tag', 'file' );
684
- $query = array( 'type' => $field_types, 'field_options like' => 's:4:"size";', 'field_options not like' => 's:4:"size";s:0:' );
 
 
 
 
 
 
685
 
686
- // Get results
687
- $fields = FrmDb::get_results( $this->fields, $query, 'id, field_options' );
688
 
689
- $updated = 0;
690
- foreach ( $fields as $f ) {
691
- $f->field_options = maybe_unserialize($f->field_options);
692
- if ( empty($f->field_options['size']) || ! is_numeric($f->field_options['size']) ) {
693
- continue;
694
- }
695
 
696
- $f->field_options['size'] = round( $pixel_conversion * (int) $f->field_options['size'] );
697
- $f->field_options['size'] .= 'px';
698
- $u = FrmField::update( $f->id, array( 'field_options' => $f->field_options ) );
699
- if ( $u ) {
700
- $updated++;
701
- }
702
- unset($f);
703
- }
 
704
 
705
- // Change the characters in widgets to pixels
706
- $widgets = get_option('widget_frm_show_form');
707
- if ( empty($widgets) ) {
708
- return;
709
- }
 
 
 
 
 
710
 
711
- $widgets = maybe_unserialize($widgets);
712
- foreach ( $widgets as $k => $widget ) {
713
- if ( ! is_array($widget) || ! isset($widget['size']) ) {
714
- continue;
715
- }
716
- $size = round( $pixel_conversion * (int) $widget['size'] );
717
- $size .= 'px';
718
- $widgets[ $k ]['size'] = $size;
719
- }
720
- update_option('widget_frm_show_form', $widgets);
721
- }
 
 
 
 
 
 
 
 
 
722
 
723
  /**
724
- * Migrate post and email notification settings into actions
 
 
 
 
725
  */
726
- private function migrate_to_16() {
727
- global $wpdb;
728
 
729
- $forms = FrmDb::get_results( $this->forms, array(), 'id, options, is_template, default_template' );
730
-
731
- /**
732
- * Old email settings format:
733
- * email_to: Email or field id
734
- * also_email_to: array of fields ids
735
- * reply_to: Email, field id, 'custom'
736
- * cust_reply_to: string
737
- * reply_to_name: field id, 'custom'
738
- * cust_reply_to_name: string
739
- * plain_text: 0|1
740
- * email_message: string or ''
741
- * email_subject: string or ''
742
- * inc_user_info: 0|1
743
- * update_email: 0, 1, 2
744
- *
745
- * Old autoresponder settings format:
746
- * auto_responder: 0|1
747
- * ar_email_message: string or ''
748
- * ar_email_to: field id
749
- * ar_plain_text: 0|1
750
- * ar_reply_to_name: string
751
- * ar_reply_to: string
752
- * ar_email_subject: string
753
- * ar_update_email: 0, 1, 2
754
- *
755
- * New email settings:
756
- * post_content: json settings
757
- * post_title: form id
758
- * post_excerpt: message
759
- *
760
- */
761
-
762
- foreach ( $forms as $form ) {
763
- if ( $form->is_template && $form->default_template ) {
764
- // don't migrate the default templates since the email will be added anyway
765
- continue;
766
  }
767
 
768
- // Format form options
769
- $form_options = maybe_unserialize($form->options);
770
-
771
- // Migrate settings to actions
772
- FrmXMLHelper::migrate_form_settings_to_actions( $form_options, $form->id );
773
- }
774
- }
775
 
776
- private function migrate_to_11() {
777
- global $wpdb;
 
 
 
778
 
779
- $forms = FrmDb::get_results( $this->forms, array(), 'id, options');
780
-
781
- $sending = __( 'Sending', 'formidable' );
782
- $img = FrmAppHelper::plugin_url() . '/images/ajax_loader.gif';
783
- $old_default_html = <<<DEFAULT_HTML
784
- <div class="frm_submit">
785
- [if back_button]<input type="submit" value="[back_label]" name="frm_prev_page" formnovalidate="formnovalidate" [back_hook] />[/if back_button]
786
- <input type="submit" value="[button_label]" [button_action] />
787
- <img class="frm_ajax_loading" src="$img" alt="$sending" style="visibility:hidden;" />
788
- </div>
789
- DEFAULT_HTML;
790
- unset($sending, $img);
791
-
792
- $new_default_html = FrmFormsHelper::get_default_html('submit');
793
- $draft_link = FrmFormsHelper::get_draft_link();
794
- foreach ( $forms as $form ) {
795
- $form->options = maybe_unserialize($form->options);
796
- if ( ! isset($form->options['submit_html']) || empty($form->options['submit_html']) ) {
797
- continue;
798
- }
799
 
800
- if ( $form->options['submit_html'] != $new_default_html && $form->options['submit_html'] == $old_default_html ) {
801
- $form->options['submit_html'] = $new_default_html;
802
- $wpdb->update( $this->forms, array( 'options' => serialize( $form->options ) ), array( 'id' => $form->id ) );
803
- } else if ( ! strpos( $form->options['submit_html'], 'save_draft' ) ) {
804
- $form->options['submit_html'] = preg_replace( '~\<\/div\>(?!.*\<\/div\>)~', $draft_link . "\r\n</div>", $form->options['submit_html'] );
805
- $wpdb->update( $this->forms, array( 'options' => serialize( $form->options ) ), array( 'id' => $form->id ) );
806
- }
807
- unset($form);
808
- }
809
- unset($forms);
810
- }
811
 
812
- private function migrate_to_6() {
813
- global $wpdb;
 
814
 
815
- $no_save = array_merge( FrmField::no_save_fields(), array( 'form', 'hidden', 'user_id' ) );
816
- $fields = FrmDb::get_results( $this->fields, array( 'type NOT' => $no_save ), 'id, field_options' );
817
-
818
- $default_html = <<<DEFAULT_HTML
819
- <div id="frm_field_[id]_container" class="form-field [required_class] [error_class]">
820
- <label class="frm_pos_[label_position]">[field_name]
821
- <span class="frm_required">[required_label]</span>
822
- </label>
823
- [input]
824
- [if description]<div class="frm_description">[description]</div>[/if description]
825
- </div>
826
- DEFAULT_HTML;
827
-
828
- $old_default_html = <<<DEFAULT_HTML
829
- <div id="frm_field_[id]_container" class="form-field [required_class] [error_class]">
830
- <label class="frm_pos_[label_position]">[field_name]
831
- <span class="frm_required">[required_label]</span>
832
- </label>
833
- [input]
834
- [if description]<p class="frm_description">[description]</p>[/if description]
835
- </div>
836
- DEFAULT_HTML;
837
-
838
- $new_default_html = FrmFieldsHelper::get_default_html('text');
839
- foreach ( $fields as $field ) {
840
- $field->field_options = maybe_unserialize($field->field_options);
841
- $html = FrmField::get_option( $field, 'custom_html' );
842
- if ( $html == $default_html || $html == $old_default_html ) {
843
- $field->field_options['custom_html'] = $new_default_html;
844
- $wpdb->update( $this->fields, array( 'field_options' => maybe_serialize( $field->field_options ) ), array( 'id' => $field->id ) );
845
- }
846
- unset($field);
847
- }
848
- unset($default_html, $old_default_html, $fields);
849
- }
850
 
851
- private function migrate_to_4() {
852
- global $wpdb;
853
- $user_ids = FrmEntryMeta::getAll( array( 'fi.type' => 'user_id' ) );
854
- foreach ( $user_ids as $user_id ) {
855
- $wpdb->update( $this->entries, array( 'user_id' => $user_id->meta_value ), array( 'id' => $user_id->item_id ) );
856
- }
857
- }
858
  }
1
  <?php
2
 
3
  class FrmDb {
4
+ public $fields;
5
+ public $forms;
6
+ public $entries;
7
+ public $entry_metas;
8
 
9
  public function __construct() {
10
  if ( ! defined('ABSPATH') ) {
11
  die('You are not allowed to call this page directly.');
12
  }
13
 
14
+ _deprecated_function( __METHOD__, '2.05.06', 'FrmMigrate' );
15
  global $wpdb;
16
  $this->fields = $wpdb->prefix . 'frm_fields';
17
  $this->forms = $wpdb->prefix . 'frm_forms';
19
  $this->entry_metas = $wpdb->prefix . 'frm_item_metas';
20
  }
21
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
22
  /**
23
  * Change array into format $wpdb->prepare can use
24
  *
109
  }
110
  $start = false;
111
  $where .= $key . ' %s';
112
+ $values[] = '%' . self::esc_like( $v ) . '%';
113
  }
114
  $where .= ')';
115
  } else if ( ! empty( $value ) ) {
116
+ $where .= ' in (' . self::prepare_array_values( $value, '%s' ) . ')';
117
  $values = array_merge( $values, $value );
118
  }
119
  } else if ( strpos( $lowercase_key, 'like' ) !== false ) {
134
  }
135
 
136
  $where .= ' %s';
137
+ $values[] = $start . self::esc_like( $value ) . $end;
138
 
139
  } else if ( $value === null ) {
140
  $where .= ' IS NULL';
196
  $query = self::generate_query_string_from_pieces( $field, $table, $where, $args );
197
 
198
  $cache_key = self::generate_cache_key( $where, $args, $field, $type );
199
+ $results = self::check_cache( $cache_key, $group, $query, 'get_' . $type );
200
  return $results;
201
  }
202
 
344
  }
345
 
346
  if ( $k == 'limit' ) {
347
+ $args[ $k ] = self::esc_limit( $v );
348
  }
349
  $db_name = strtoupper( str_replace( '_', ' ', $k ) );
350
  if ( strpos( $v, $db_name ) === false ) {
376
  $query = self::generate_query_string_from_pieces( $columns, $table, $where );
377
 
378
  $cache_key = str_replace( array( ' ', ',' ), '_', trim( implode( '_', FrmAppHelper::array_flatten( $where ) ) . $columns . '_results_ARRAY_A' , ' WHERE' ) );
379
+ $results = self::check_cache( $cache_key, $group, $query, 'get_associative_results' );
380
 
381
  return $results;
382
  }
411
  return $query;
412
  }
413
 
414
+ /**
415
+ * Added for < WP 4.0 compatability
416
+ *
417
+ * @since 2.05.06
418
+ *
419
+ * @param string $term The value to escape
420
+ * @return string The escaped value
421
+ */
422
+ public static function esc_like( $term ) {
423
+ global $wpdb;
424
+ if ( method_exists( $wpdb, 'esc_like' ) ) {
425
+ // WP 4.0
426
+ $term = $wpdb->esc_like( $term );
427
+ } else {
428
+ $term = like_escape( $term );
429
  }
430
 
431
+ return $term;
432
+ }
433
+
434
+ /**
435
+ * @since 2.05.06
436
+ * @param string $order_query
437
+ */
438
+ public static function esc_order( $order_query ) {
439
+ if ( empty( $order_query ) ) {
440
+ return '';
441
+ }
442
+
443
+ // remove ORDER BY before santizing
444
+ $order_query = strtolower( $order_query );
445
+ if ( strpos( $order_query, 'order by' ) !== false ) {
446
+ $order_query = str_replace( 'order by', '', $order_query );
 
 
 
447
  }
 
448
 
449
+ $order_query = explode( ' ', trim( $order_query ) );
450
 
451
+ $order_fields = array(
452
+ 'id', 'form_key', 'name', 'description',
453
+ 'parent_form_id', 'logged_in', 'is_template',
454
+ 'default_template', 'status', 'created_at',
455
+ );
456
 
457
+ $order = trim( trim( reset( $order_query ), ',' ) );
458
+ if ( ! in_array( $order, $order_fields ) ) {
459
+ return '';
 
460
  }
 
461
 
462
+ $order_by = '';
463
+ if ( count( $order_query ) > 1 ) {
464
+ $order_by = end( $order_query );
465
+ self::esc_order_by( $order_by );
466
+ }
467
 
468
+ return ' ORDER BY ' . $order . ' ' . $order_by;
469
+ }
470
 
471
+ /**
472
+ * Make sure this is ordering by either ASC or DESC
473
+ * @since 2.05.06
474
+ */
475
+ public static function esc_order_by( &$order_by ) {
476
+ $sort_options = array( 'asc', 'desc' );
477
+ if ( ! in_array( strtolower( $order_by ), $sort_options ) ) {
478
+ $order_by = 'asc';
479
+ }
480
+ }
481
 
482
  /**
483
+ * @param string $limit
484
+ * @since 2.05.06
 
 
485
  */
486
+ public static function esc_limit( $limit ) {
487
+ if ( empty( $limit ) ) {
488
+ return '';
489
+ }
490
+
491
+ $limit = trim( str_replace( ' limit', '', strtolower( $limit ) ) );
492
+ if ( is_numeric( $limit ) ) {
493
+ return ' LIMIT ' . $limit;
494
  }
495
 
496
+ $limit = explode( ',', trim( $limit ) );
497
+ foreach ( $limit as $k => $l ) {
498
+ if ( is_numeric( $l ) ) {
499
+ $limit[ $k ] = $l;
 
500
  }
501
  }
502
+
503
+ $limit = implode( ',', $limit );
504
+ return ' LIMIT ' . $limit;
505
+ }
506
+
507
+ /**
508
+ * Get an array of values ready to go through $wpdb->prepare
509
+ * @since 2.05.06
510
+ */
511
+ public static function prepare_array_values( $array, $type = '%s' ) {
512
+ $placeholders = array_fill( 0, count( $array ), $type );
513
+ return implode( ', ', $placeholders );
514
+ }
515
+
516
+ /**
517
+ * @since 2.05.06
518
+ */
519
+ public static function prepend_and_or_where( $starts_with = ' WHERE ', $where = '' ) {
520
+ if ( empty( $where ) ) {
521
+ return '';
522
+ }
523
+
524
+ if ( is_array( $where ) ) {
525
+ global $wpdb;
526
+ self::get_where_clause_and_values( $where, $starts_with );
527
+ $where = $wpdb->prepare( $where['where'], $where['values'] );
528
+ } else {
529
+ $where = $starts_with . $where;
530
+ }
531
+
532
+ return $where;
533
  }
534
 
535
  /**
536
+ * Prepare and save settings in styles and actions
 
537
  *
538
+ * @param array $settings
539
+ * @param string $group
540
+ *
541
+ * @since 2.05.06
542
  */
543
+ public static function save_settings( $settings, $group ) {
544
+ $settings = (array) $settings;
545
+ $settings['post_content'] = FrmAppHelper::prepare_and_encode( $settings['post_content'] );
546
+
547
+ if ( empty( $settings['ID'] ) ) {
548
+ unset( $settings['ID'] );
549
  }
550
+
551
+ // delete all caches for this group
552
+ self::cache_delete_group( $group );
553
+
554
+ return self::save_json_post( $settings );
555
+ }
556
+
557
+ /**
558
+ * Since actions are JSON encoded, we don't want any filters messing with it.
559
+ * Remove the filters and then add them back in case any posts or views are
560
+ * also being imported.
561
+ *
562
+ * Used when saving form actions and styles
563
+ *
564
+ * @since 2.05.06
565
+ */
566
+ public static function save_json_post( $settings ) {
567
+ global $wp_filter;
568
+ $filters = $wp_filter['content_save_pre'];
569
+
570
+ // Remove the balanceTags filter in case WordPress is trying to validate the XHTML
571
+ remove_all_filters( 'content_save_pre' );
572
+
573
+ $post = wp_insert_post( $settings );
574
+
575
+ // add the content filters back for views or posts
576
+ $wp_filter['content_save_pre'] = $filters;
577
+
578
+ return $post;
579
  }
580
 
581
  /**
582
+ * Check cache before fetching values and saving to cache
583
+ *
584
+ * @since 2.05.06
585
+ *
586
+ * @param string $cache_key The unique name for this cache
587
+ * @param string $group The name of the cache group
588
+ * @param string $query If blank, don't run a db call
589
+ * @param string $type The wpdb function to use with this query
590
+ * @return mixed $results The cache or query results
591
  */
592
+ public static function check_cache( $cache_key, $group = '', $query = '', $type = 'get_var', $time = 300 ) {
593
+ $results = wp_cache_get( $cache_key, $group );
594
+ if ( ! FrmAppHelper::is_empty_value( $results, false ) || empty( $query ) ) {
595
+ return $results;
596
+ }
597
 
598
+ if ( 'get_posts' == $type ) {
599
+ $results = get_posts( $query );
600
+ } elseif ( 'get_associative_results' == $type ) {
601
+ global $wpdb;
602
+ $results = $wpdb->get_results( $query, OBJECT_K );
603
+ } else {
604
+ global $wpdb;
605
+ $results = $wpdb->{$type}( $query );
606
+ }
607
 
608
+ self::set_cache( $cache_key, $results, $group, $time );
 
609
 
610
+ return $results;
611
+ }
 
 
 
 
612
 
613
+ /**
614
+ * @since 2.05.06
615
+ */
616
+ public static function set_cache( $cache_key, $results, $group = '', $time = 300 ) {
617
+ if ( ! FrmAppHelper::prevent_caching() ) {
618
+ self::add_key_to_group_cache( $cache_key, $group );
619
+ wp_cache_set( $cache_key, $results, $group, $time );
620
+ }
621
+ }
622
 
623
+ /**
624
+ * Keep track of the keys cached in each group so they can be deleted
625
+ * in Redis and Memcache
626
+ * @since 2.05.06
627
+ */
628
+ public static function add_key_to_group_cache( $key, $group ) {
629
+ $cached = self::get_group_cached_keys( $group );
630
+ $cached[ $key ] = $key;
631
+ wp_cache_set( 'cached_keys', $cached, $group, 300 );
632
+ }
633
 
634
+ /**
635
+ * @since 2.05.06
636
+ */
637
+ public static function get_group_cached_keys( $group ) {
638
+ $cached = wp_cache_get( 'cached_keys', $group );
639
+ if ( ! $cached || ! is_array( $cached ) ) {
640
+ $cached = array();
641
+ }
642
+
643
+ return $cached;
644
+ }
645
+
646
+ /**
647
+ * @since 2.05.06
648
+ * @param string $cache_key
649
+ */
650
+ public static function delete_cache_and_transient( $cache_key, $group = 'default' ) {
651
+ delete_transient( $cache_key );
652
+ wp_cache_delete( $cache_key, $group );
653
+ }
654
 
655
  /**
656
+ * Delete all caching in a single group
657
+ *
658
+ * @since 2.05.06
659
+ *
660
+ * @param string $group The name of the cache group
661
  */
662
+ public static function cache_delete_group( $group ) {
663
+ $cached_keys = self::get_group_cached_keys( $group );
664
 
665
+ if ( ! empty( $cached_keys ) ) {
666
+ foreach ( $cached_keys as $key ) {
667
+ wp_cache_delete( $key, $group );
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
668
  }
669
 
670
+ wp_cache_delete( 'cached_keys', $group );
671
+ }
672
+ }
 
 
 
 
673
 
674
+ /**
675
+ * @deprecated 2.05.06
676
+ */
677
+ public function upgrade( $old_db_version = false ) {
678
+ _deprecated_function( __METHOD__, '2.05.06', 'FrmMigrate::upgrade' );
679
 
680
+ $db = new FrmMigrate();
681
+ $db->upgrade( $old_db_version );
682
+ }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
683
 
684
+ /**
685
+ * @deprecated 2.05.06
686
+ */
687
+ public function collation() {
688
+ _deprecated_function( __METHOD__, '2.05.06', 'FrmMigrate::collation' );
 
 
 
 
 
 
689
 
690
+ $db = new FrmMigrate();
691
+ return $db->collation();
692
+ }
693
 
694
+ /**
695
+ * @deprecated 2.05.06
696
+ */
697
+ public function uninstall() {
698
+ _deprecated_function( __METHOD__, '2.05.06', 'FrmMigrate::uninstall' );
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
699
 
700
+ $db = new FrmMigrate();
701
+ $db->uninstall();
702
+ }
 
 
 
 
703
  }
classes/models/FrmDbDeprecated.php CHANGED
@@ -30,7 +30,7 @@ class FrmDbDeprecated {
30
  public function upgrade( $old_db_version = false ) {
31
  _deprecated_function( __FUNCTION__, '2.03.05', 'FrmDb::upgrade( $old_db_version )' );
32
 
33
- $db = new FrmDb();
34
  $db->upgrade( $old_db_version );
35
  }
36
 
@@ -40,8 +40,8 @@ class FrmDbDeprecated {
40
  public function collation() {
41
  _deprecated_function( __FUNCTION__, '2.03.05', 'FrmDb::collation()' );
42
 
43
- $db = new FrmDb();
44
- $db->collation();
45
  }
46
 
47
  /**
@@ -50,7 +50,7 @@ class FrmDbDeprecated {
50
  public function uninstall() {
51
  _deprecated_function( __FUNCTION__, '2.03.05', 'FrmDb::uninstall()' );
52
 
53
- $db = new FrmDb();
54
  $db->uninstall();
55
  }
56
 
30
  public function upgrade( $old_db_version = false ) {
31
  _deprecated_function( __FUNCTION__, '2.03.05', 'FrmDb::upgrade( $old_db_version )' );
32
 
33
+ $db = new FrmMigrate();
34
  $db->upgrade( $old_db_version );
35
  }
36
 
40
  public function collation() {
41
  _deprecated_function( __FUNCTION__, '2.03.05', 'FrmDb::collation()' );
42
 
43
+ $db = new FrmMigrate();
44
+ return $db->collation();
45
  }
46
 
47
  /**
50
  public function uninstall() {
51
  _deprecated_function( __FUNCTION__, '2.03.05', 'FrmDb::uninstall()' );
52
 
53
+ $db = new FrmMigrate();
54
  $db->uninstall();
55
  }
56
 
classes/models/FrmEntry.php CHANGED
@@ -236,10 +236,10 @@ class FrmEntry {
236
  * @since 2.0.5
237
  */
238
  public static function clear_cache() {
239
- FrmAppHelper::cache_delete_group( 'frm_entry' );
240
- FrmAppHelper::cache_delete_group( 'frm_item' );
241
- FrmAppHelper::cache_delete_group( 'frm_entry_meta' );
242
- FrmAppHelper::cache_delete_group( 'frm_item_meta' );
243
  }
244
 
245
  /**
@@ -273,11 +273,11 @@ class FrmEntry {
273
  $query = $wpdb->prepare( $query, $query_args );
274
 
275
  if ( ! $meta ) {
276
- $entry = FrmAppHelper::check_cache( $id . '_nometa', 'frm_entry', $query, 'get_row' );
277
  return stripslashes_deep($entry);
278
  }
279
 
280
- $entry = FrmAppHelper::check_cache( $id, 'frm_entry' );
281
  if ( $entry !== false ) {
282
  return stripslashes_deep($entry);
283
  }
@@ -319,7 +319,7 @@ class FrmEntry {
319
  }
320
  unset($metas);
321
 
322
- FrmAppHelper::set_cache( $entry->id, $entry, 'frm_entry' );
323
 
324
  return $entry;
325
  }
@@ -330,7 +330,7 @@ class FrmEntry {
330
  public static function exists( $id ) {
331
  global $wpdb;
332
 
333
- if ( FrmAppHelper::check_cache( $id, 'frm_entry' ) ) {
334
  $exists = true;
335
  return $exists;
336
  }
@@ -349,7 +349,7 @@ class FrmEntry {
349
  public static function getAll( $where, $order_by = '', $limit = '', $meta = false, $inc_form = true ) {
350
  global $wpdb;
351
 
352
- $limit = FrmAppHelper::esc_limit($limit);
353
 
354
  $cache_key = maybe_serialize($where) . $order_by . $limit . $inc_form;
355
  $entries = wp_cache_get($cache_key, 'frm_entry');
@@ -371,12 +371,12 @@ class FrmEntry {
371
  }
372
 
373
  // prepare the query
374
- $query = 'SELECT ' . $fields . ' FROM ' . $table . FrmAppHelper::prepend_and_or_where(' WHERE ', $where) . $order_by . $limit;
375
 
376
  $entries = $wpdb->get_results($query, OBJECT_K);
377
  unset($query);
378
 
379
- FrmAppHelper::set_cache( $cache_key, $entries, 'frm_entry' );
380
  }
381
 
382
  if ( ! $meta || ! $entries ) {
@@ -419,7 +419,7 @@ class FrmEntry {
419
 
420
  if ( ! FrmAppHelper::prevent_caching() ) {
421
  foreach ( $entries as $entry ) {
422
- FrmAppHelper::set_cache( $entry->id, $entry, 'frm_entry' );
423
  unset( $entry );
424
  }
425
  }
@@ -441,8 +441,8 @@ class FrmEntry {
441
  $count = FrmDb::get_count( $table_join, $where );
442
  } else {
443
  $cache_key = 'count_' . maybe_serialize( $where );
444
- $query = 'SELECT COUNT(*) FROM ' . $table_join . FrmAppHelper::prepend_and_or_where( ' WHERE ', $where );
445
- $count = FrmAppHelper::check_cache( $cache_key, 'frm_entry', $query, 'get_var' );
446
  }
447
 
448
  return $count;
236
  * @since 2.0.5
237
  */
238
  public static function clear_cache() {
239
+ FrmDb::cache_delete_group( 'frm_entry' );
240
+ FrmDb::cache_delete_group( 'frm_item' );
241
+ FrmDb::cache_delete_group( 'frm_entry_meta' );
242
+ FrmDb::cache_delete_group( 'frm_item_meta' );
243
  }
244
 
245
  /**
273
  $query = $wpdb->prepare( $query, $query_args );
274
 
275
  if ( ! $meta ) {
276
+ $entry = FrmDb::check_cache( $id . '_nometa', 'frm_entry', $query, 'get_row' );
277
  return stripslashes_deep($entry);
278
  }
279
 
280
+ $entry = FrmDb::check_cache( $id, 'frm_entry' );
281
  if ( $entry !== false ) {
282
  return stripslashes_deep($entry);
283
  }
319
  }
320
  unset($metas);
321
 
322
+ FrmDb::set_cache( $entry->id, $entry, 'frm_entry' );
323
 
324
  return $entry;
325
  }
330
  public static function exists( $id ) {
331
  global $wpdb;
332
 
333
+ if ( FrmDb::check_cache( $id, 'frm_entry' ) ) {
334
  $exists = true;
335
  return $exists;
336
  }
349
  public static function getAll( $where, $order_by = '', $limit = '', $meta = false, $inc_form = true ) {
350
  global $wpdb;
351
 
352
+ $limit = FrmDb::esc_limit($limit);
353
 
354
  $cache_key = maybe_serialize($where) . $order_by . $limit . $inc_form;
355
  $entries = wp_cache_get($cache_key, 'frm_entry');
371
  }
372
 
373
  // prepare the query
374
+ $query = 'SELECT ' . $fields . ' FROM ' . $table . FrmDb::prepend_and_or_where(' WHERE ', $where) . $order_by . $limit;
375
 
376
  $entries = $wpdb->get_results($query, OBJECT_K);
377
  unset($query);
378
 
379
+ FrmDb::set_cache( $cache_key, $entries, 'frm_entry' );
380
  }
381
 
382
  if ( ! $meta || ! $entries ) {
419
 
420
  if ( ! FrmAppHelper::prevent_caching() ) {
421
  foreach ( $entries as $entry ) {
422
+ FrmDb::set_cache( $entry->id, $entry, 'frm_entry' );
423
  unset( $entry );
424
  }
425
  }
441
  $count = FrmDb::get_count( $table_join, $where );
442
  } else {
443
  $cache_key = 'count_' . maybe_serialize( $where );
444
+ $query = 'SELECT COUNT(*) FROM ' . $table_join . FrmDb::prepend_and_or_where( ' WHERE ', $where );
445
+ $count = FrmDb::check_cache( $cache_key, 'frm_entry', $query, 'get_var' );
446
  }
447
 
448
  return $count;
classes/models/FrmEntryMeta.php CHANGED
@@ -133,8 +133,8 @@ class FrmEntryMeta {
133
  * @since 2.0.5
134
  */
135
  public static function clear_cache() {
136
- FrmAppHelper::cache_delete_group( 'frm_entry_meta' );
137
- FrmAppHelper::cache_delete_group( 'frm_item_meta' );
138
  }
139
 
140
  /**
@@ -157,7 +157,7 @@ class FrmEntryMeta {
157
  $cached = $entry;
158
  } else {
159
  $entry_id = (int) $entry_id;
160
- $cached = FrmAppHelper::check_cache( $entry_id, 'frm_entry' );
161
  }
162
 
163
  if ( $cached && isset( $cached->metas ) && isset( $cached->metas[ $field_id ] ) ) {
@@ -190,7 +190,7 @@ class FrmEntryMeta {
190
  $query = implode(' ', $query);
191
 
192
  $cache_key = 'entry_metas_for_field_' . $field_id . $order . $limit . maybe_serialize( $args );
193
- $values = FrmAppHelper::check_cache($cache_key, 'frm_entry', $query, 'get_col');
194
 
195
  if ( ! $args['stripslashes'] ) {
196
  return $values;
@@ -243,10 +243,10 @@ class FrmEntryMeta {
243
  $query = 'SELECT it.*, fi.type as field_type, fi.field_key as field_key,
244
  fi.required as required, fi.form_id as field_form_id, fi.name as field_name, fi.options as fi_options
245
  FROM ' . $wpdb->prefix . 'frm_item_metas it LEFT OUTER JOIN ' . $wpdb->prefix . 'frm_fields fi ON it.field_id=fi.id' .
246
- FrmAppHelper::prepend_and_or_where(' WHERE ', $where) . $order_by . $limit;
247
 
248
  $cache_key = 'all_' . maybe_serialize( $where ) . $order_by . $limit;
249
- $results = FrmAppHelper::check_cache($cache_key, 'frm_entry', $query, ($limit == ' LIMIT 1' ? 'get_row' : 'get_results'));
250
 
251
  if ( ! $results || ! $stripslashes ) {
252
  return $results;
@@ -273,7 +273,7 @@ class FrmEntryMeta {
273
  $query = implode(' ', $query);
274
 
275
  $cache_key = 'ids_' . maybe_serialize( $where ) . $order_by . 'l' . $limit . 'u' . $unique . maybe_serialize( $args );
276
- $results = FrmAppHelper::check_cache($cache_key, 'frm_entry', $query, ($limit == ' LIMIT 1' ? 'get_var' : 'get_col'));
277
 
278
  return $results;
279
  }
@@ -309,7 +309,7 @@ class FrmEntryMeta {
309
  if ( ! empty($args['user_id']) ) {
310
  $where['e.user_id'] = $args['user_id'];
311
  }
312
- $query[] = FrmAppHelper::prepend_and_or_where(' WHERE ', $where) . $order_by . $limit;
313
 
314
  if ( $args['group_by'] ) {
315
  $query[] = ' GROUP BY ' . sanitize_text_field( $args['group_by'] );
@@ -340,7 +340,7 @@ class FrmEntryMeta {
340
  }
341
 
342
  // The query has already been prepared
343
- $query[] = FrmAppHelper::prepend_and_or_where(' WHERE ', $where) . $order_by . $limit;
344
  }
345
 
346
  public static function search_entry_metas( $search, $field_id = '', $operator ) {
@@ -371,7 +371,7 @@ class FrmEntryMeta {
371
  $where .= $wpdb->prepare(' meta_value ' . $operator . ' %s and', $value );
372
  }
373
  $where .= $wpdb->prepare(' field_id=%d', $field_id);
374
- $query = 'SELECT DISTINCT item_id FROM ' . $wpdb->prefix . 'frm_item_metas' . FrmAppHelper::prepend_and_or_where( ' WHERE ', $where );
375
  } else {
376
  if ( $operator == 'LIKE' ) {
377
  $search = '%' . $search . '%';
@@ -380,7 +380,7 @@ class FrmEntryMeta {
380
  }
381
 
382
  $results = $wpdb->get_col($query, 0);
383
- FrmAppHelper::set_cache( $cache_key, $results, 'frm_entry' );
384
 
385
  return $results;
386
  }
133
  * @since 2.0.5
134
  */
135
  public static function clear_cache() {
136
+ FrmDb::cache_delete_group( 'frm_entry_meta' );
137
+ FrmDb::cache_delete_group( 'frm_item_meta' );
138
  }
139
 
140
  /**
157
  $cached = $entry;
158
  } else {
159
  $entry_id = (int) $entry_id;
160
+ $cached = FrmDb::check_cache( $entry_id, 'frm_entry' );
161
  }
162
 
163
  if ( $cached && isset( $cached->metas ) && isset( $cached->metas[ $field_id ] ) ) {
190
  $query = implode(' ', $query);
191
 
192
  $cache_key = 'entry_metas_for_field_' . $field_id . $order . $limit . maybe_serialize( $args );
193
+ $values = FrmDb::check_cache($cache_key, 'frm_entry', $query, 'get_col');
194
 
195
  if ( ! $args['stripslashes'] ) {
196
  return $values;
243
  $query = 'SELECT it.*, fi.type as field_type, fi.field_key as field_key,
244
  fi.required as required, fi.form_id as field_form_id, fi.name as field_name, fi.options as fi_options
245
  FROM ' . $wpdb->prefix . 'frm_item_metas it LEFT OUTER JOIN ' . $wpdb->prefix . 'frm_fields fi ON it.field_id=fi.id' .
246
+ FrmDb::prepend_and_or_where(' WHERE ', $where) . $order_by . $limit;
247
 
248
  $cache_key = 'all_' . maybe_serialize( $where ) . $order_by . $limit;
249
+ $results = FrmDb::check_cache($cache_key, 'frm_entry', $query, ($limit == ' LIMIT 1' ? 'get_row' : 'get_results'));
250
 
251
  if ( ! $results || ! $stripslashes ) {
252
  return $results;
273
  $query = implode(' ', $query);
274
 
275
  $cache_key = 'ids_' . maybe_serialize( $where ) . $order_by . 'l' . $limit . 'u' . $unique . maybe_serialize( $args );
276
+ $results = FrmDb::check_cache($cache_key, 'frm_entry', $query, ($limit == ' LIMIT 1' ? 'get_var' : 'get_col'));
277
 
278
  return $results;
279
  }
309
  if ( ! empty($args['user_id']) ) {
310
  $where['e.user_id'] = $args['user_id'];
311
  }
312
+ $query[] = FrmDb::prepend_and_or_where(' WHERE ', $where) . $order_by . $limit;
313
 
314
  if ( $args['group_by'] ) {
315
  $query[] = ' GROUP BY ' . sanitize_text_field( $args['group_by'] );
340
  }
341
 
342
  // The query has already been prepared
343
+ $query[] = FrmDb::prepend_and_or_where(' WHERE ', $where) . $order_by . $limit;
344
  }
345
 
346
  public static function search_entry_metas( $search, $field_id = '', $operator ) {
371
  $where .= $wpdb->prepare(' meta_value ' . $operator . ' %s and', $value );
372
  }
373
  $where .= $wpdb->prepare(' field_id=%d', $field_id);
374
+ $query = 'SELECT DISTINCT item_id FROM ' . $wpdb->prefix . 'frm_item_metas' . FrmDb::prepend_and_or_where( ' WHERE ', $where );
375
  } else {
376
  if ( $operator == 'LIKE' ) {
377
  $search = '%' . $search . '%';
380
  }
381
 
382
  $results = $wpdb->get_col($query, 0);
383
+ FrmDb::set_cache( $cache_key, $results, 'frm_entry' );
384
 
385
  return $results;
386
  }
classes/models/FrmField.php CHANGED
@@ -241,7 +241,7 @@ class FrmField {
241
  global $wpdb;
242
  $wpdb->query( $wpdb->prepare( 'DELETE FROM ' . $wpdb->options . ' WHERE option_name LIKE %s OR option_name LIKE %s OR option_name LIKE %s OR option_name LIKE %s', '_transient_timeout_frm_form_fields_' . $form_id . 'ex%', '_transient_frm_form_fields_' . $form_id . 'ex%', '_transient_timeout_frm_form_fields_' . $form_id . 'in%', '_transient_frm_form_fields_' . $form_id . 'in%' ) );
243
 
244
- FrmAppHelper::cache_delete_group( 'frm_field' );
245
 
246
  $form = FrmForm::getOne($form_id);
247
  if ( $form && $form->parent_form_id && $form->parent_form_id != $form_id ) {
@@ -268,16 +268,16 @@ class FrmField {
268
  $where = is_numeric($id) ? 'id=%d' : 'field_key=%s';
269
  $query = $wpdb->prepare( 'SELECT * FROM ' . $wpdb->prefix . 'frm_fields WHERE ' . $where, $id );
270
 
271
- $results = FrmAppHelper::check_cache( $id, 'frm_field', $query, 'get_row', 0 );
272
 
273
  if ( empty($results) ) {
274
  return $results;
275
  }
276
 
277
  if ( is_numeric($id) ) {
278
- FrmAppHelper::set_cache( $results->field_key, $results, 'frm_field' );
279
  } else if ( $results ) {
280
- FrmAppHelper::set_cache( $results->id, $results, 'frm_field' );
281
  }
282
 
283
  self::prepare_options( $results );
@@ -291,7 +291,7 @@ class FrmField {
291
  * @param mixed $col The name of the column in the fields database table
292
  */
293
  public static function get_type( $id, $col = 'type' ) {
294
- $field = FrmAppHelper::check_cache( $id, 'frm_field' );
295
  if ( $field ) {
296
  $type = $field->{$col};
297
  } else {
@@ -454,7 +454,7 @@ class FrmField {
454
  $order_by = ' ORDER BY ' . $order_by;
455
  }
456
 
457
- $limit = FrmAppHelper::esc_limit($limit);
458
 
459
  $query = "SELECT fi.*, fr.name as form_name FROM {$table_name} fi LEFT OUTER JOIN {$form_table_name} fr ON fi.form_id=fr.id";
460
  $query_type = ( $limit == ' LIMIT 1' || $limit == 1 ) ? 'row' : 'results';
@@ -463,7 +463,7 @@ class FrmField {
463
  $results = FrmDb::get_var( $table_name . ' fi LEFT OUTER JOIN ' . $form_table_name . ' fr ON fi.form_id=fr.id', $where, 'fi.*, fr.name as form_name', array( 'order_by' => $order_by, 'limit' => $limit ), '', $query_type );
464
  } else {
465
  // if the query is not an array, then it has already been prepared
466
- $query .= FrmAppHelper::prepend_and_or_where(' WHERE ', $where) . $order_by . $limit;
467
 
468
  $function_name = ( $query_type == 'row' ) ? 'get_row' : 'get_results';
469
  $results = $wpdb->$function_name( $query );
@@ -472,7 +472,7 @@ class FrmField {
472
 
473
  self::format_field_results( $results );
474
 
475
- FrmAppHelper::set_cache( $cache_key, $results, 'frm_field' );
476
 
477
  return stripslashes_deep( $results );
478
  }
@@ -483,8 +483,8 @@ class FrmField {
483
  private static function format_field_results( &$results ) {
484
  if ( is_array( $results ) ) {
485
  foreach ( $results as $r_key => $result ) {
486
- FrmAppHelper::set_cache( $result->id, $result, 'frm_field' );
487
- FrmAppHelper::set_cache( $result->field_key, $result, 'frm_field' );
488
 
489
  $results[ $r_key ]->field_options = maybe_unserialize( $result->field_options );
490
  $results[ $r_key ]->options = maybe_unserialize( $result->options );
@@ -493,8 +493,8 @@ class FrmField {
493
  unset( $r_key, $result );
494
  }
495
  } else if ( $results ) {
496
- FrmAppHelper::set_cache( $results->id, $results, 'frm_field' );
497
- FrmAppHelper::set_cache( $results->field_key,