PluginProbe
Formidable Forms – WordPress Form Builder for Contact Forms, Calculators, Quizzes & More / 6.8
Formidable Forms – WordPress Form Builder for Contact Forms, Calculators, Quizzes & More v6.8
6.35 6.34 6.33.1 6.33 6.32.1 6.32 6.31 6.25 6.25.1 6.26 6.26.1 6.27 6.28 6.29 6.3 6.3.1 6.3.2 6.30 6.4 6.4.1 6.4.2 6.5 6.5.1 6.5.2 6.5.3 All 141 releases
← All changes | classes/models/FrmEntry.php +36 -151 6.256.8 View file →
@@ -5,20 +5,13 @@
5 5
6 6 class FrmEntry {
7 7
8 8 /**
9 - * @since 6.16.3
10 - *
11 - * @var array
12 - */
13 - private static $unique_id_match_checks = array();
14 -
15 - /**
16 9 * Create a new entry
17 10 *
18 11 * @param array $values
19 12 *
20 - * @return bool|int $entry_id
13 + * @return int | boolean $entry_id
21 14 */
22 15 public static function create( $values ) {
23 16 $entry_id = self::create_entry( $values, 'standard' );
24 17
@@ -30,15 +23,15 @@
30 23 *
31 24 * @param array $values
32 25 * @param string $type
33 26 *
34 - * @return bool|int $entry_id
27 + * @return int | boolean $entry_id
35 28 */
36 29 private static function create_entry( $values, $type ) {
37 30 $new_values = self::before_insert_entry_in_database( $values, $type );
38 31
39 32 // Don't check XML entries for duplicates
40 - if ( $type !== 'xml' && self::is_duplicate( $new_values, $values ) ) {
33 + if ( $type != 'xml' && self::is_duplicate( $new_values, $values ) ) {
41 34 return false;
42 35 }
43 36
44 37 $entry_id = self::continue_to_create_entry( $values, $new_values );
@@ -46,26 +39,11 @@
46 39 return $entry_id;
47 40 }
48 41
49 42 /**
50 - * Flag the memoized unique id check after a new entry is created.
51 - * This prevents possibly DB requests and helps avoid issues when creating repeater entries.
52 - *
53 - * @since 6.16.3
54 - *
55 - * @param string $unique_id
56 - * @return void
57 - */
58 - private static function flag_new_unique_key( $unique_id ) {
59 - if ( ! isset( self::$unique_id_match_checks[ $unique_id ] ) ) {
60 - self::$unique_id_match_checks[ $unique_id ] = false;
61 - }
62 - }
63 -
64 - /**
65 43 * Check for duplicate entries created in the last minute
66 44 *
67 - * @return bool
45 + * @return boolean
68 46 */
69 47 public static function is_duplicate( $new_values, $values ) {
70 48 $duplicate_entry_time = apply_filters( 'frm_time_to_check_duplicates', 60, $new_values );
71 49
@@ -72,14 +50,10 @@
72 50 if ( false === self::is_duplicate_check_needed( $values, $duplicate_entry_time ) ) {
73 51 return false;
74 52 }
75 53
76 - if ( self::maybe_check_for_unique_id_match( $values, $new_values['created_at'] ) ) {
77 - return true;
78 - }
79 -
80 54 $check_val = $new_values;
81 - $check_val['created_at >'] = gmdate( 'Y-m-d H:i:s', strtotime( $new_values['created_at'] ) - absint( $duplicate_entry_time ) );
55 + $check_val['created_at >'] = gmdate( 'Y-m-d H:i:s', ( strtotime( $new_values['created_at'] ) - absint( $duplicate_entry_time ) ) );
82 56
83 57 unset( $check_val['created_at'], $check_val['updated_at'], $check_val['is_draft'], $check_val['id'], $check_val['item_key'] );
84 58
85 59 if ( $new_values['item_key'] == $new_values['name'] ) {
@@ -105,11 +79,8 @@
105 79 // make sure it's a duplicate
106 80 $metas = FrmEntryMeta::get_entry_meta_info( $entry_exist );
107 81 $field_metas = array();
108 82 foreach ( $metas as $meta ) {
109 - if ( 0 === (int) $meta->field_id ) {
110 - continue;
111 - }
112 83 $field_metas[ $meta->field_id ] = $meta->meta_value;
113 84 }
114 85
115 86 $filtered_vals = array_filter( $values['item_meta'] );
@@ -135,11 +106,12 @@
135 106 continue;
136 107 }
137 108
138 109 $diff = array_diff_assoc( $field_metas, $new_meta );
139 - foreach ( $diff as $meta_value ) {
110 + foreach ( $diff as $field_id => $meta_value ) {
140 111 if ( ! empty( $meta_value ) ) {
141 112 $is_duplicate = false;
113 + continue;
142 114 }
143 115 }
144 116
145 117 if ( $is_duplicate ) {
@@ -152,67 +124,8 @@
152 124 return $is_duplicate;
153 125 }
154 126
155 127 /**
156 - * @since 6.16.3
157 - *
158 - * @param array $values POST request data.
159 - * @param string $created_at The timestamp of the entry we are checking for.
160 - * @return bool
161 - */
162 - private static function maybe_check_for_unique_id_match( $values, $created_at ) {
163 - if ( ! self::should_check_for_unique_id_match() ) {
164 - return false;
165 - }
166 -
167 - if ( empty( $values['unique_id'] ) ) {
168 - return false;
169 - }
170 -
171 - $unique_id = sanitize_key( $values['unique_id'] );
172 - if ( ! $unique_id ) {
173 - // Only continue if a unique ID was generated on form submit.
174 - return false;
175 - }
176 -
177 - if ( isset( self::$unique_id_match_checks[ $unique_id ] ) ) {
178 - return self::$unique_id_match_checks[ $unique_id ];
179 - }
180 -
181 - $timestamp = strtotime( $created_at );
182 - if ( false === $timestamp ) {
183 - $timestamp = time();
184 - }
185 -
186 - self::$unique_id_match_checks[ $unique_id ] = (bool) FrmDb::get_var(
187 - 'frm_item_metas',
188 - array(
189 - 'field_id' => 0,
190 - 'meta_value' => serialize( compact( 'unique_id' ) ),
191 - 'created_at >' => gmdate( 'Y-m-d H:i:s', $timestamp - MONTH_IN_SECONDS ),
192 - ),
193 - 'id'
194 - );
195 -
196 - return self::$unique_id_match_checks[ $unique_id ];
197 - }
198 -
199 - /**
200 - * @since 6.16.3
201 - */
202 - private static function should_check_for_unique_id_match() {
203 - /**
204 - * Allow users to opt out of the DB query, in case it causes performance issues.
205 - *
206 - * @since 6.16.3
207 - *
208 - * @param bool $should_extend
209 - */
210 - $should_check = apply_filters( 'frm_check_for_unique_id_match', true );
211 - return (bool) $should_check;
212 - }
213 -
214 - /**
215 128 * Convert form data to the actual value that would be saved into the database.
216 129 * This is important for the duplicate check as something like 'a:2:{s:5:"typed";s:0:"";s:6:"output";s:0:"";}' (a signature value) is actually an empty string and does not get saved.
217 130 *
218 131 * @param array $filter_vals
@@ -302,9 +215,9 @@
302 215 *
303 216 * @param int $id
304 217 * @param array $values
305 218 *
306 - * @return bool|int $update_results
219 + * @return boolean|int $update_results
307 220 */
308 221 public static function update( $id, $values ) {
309 222 $update_results = self::update_entry( $id, $values, 'standard' );
310 223
@@ -318,9 +231,9 @@
318 231 *
319 232 * @param int $id
320 233 * @param array $values
321 234 *
322 - * @return bool|int $query_results
235 + * @return boolean|int $query_results
323 236 */
324 237 private static function update_entry( $id, $values, $update_type ) {
325 238 global $wpdb;
326 239
@@ -340,9 +253,9 @@
340 253
341 254 /**
342 255 * Delete an entry.
343 256 *
344 - * @param int|string $id
257 + * @param string|int $id
345 258 * @return bool True on success, false if nothing was deleted.
346 259 */
347 260 public static function destroy( $id ) {
348 261 global $wpdb;
@@ -412,9 +325,9 @@
412 325 *
413 326 * @since 2.0.11
414 327 */
415 328 public static function get_new_entry_name( $values, $default = '' ) {
416 - $name = $values['item_name'] ?? $values['name'] ?? $default;
329 + $name = isset( $values['item_name'] ) ? $values['item_name'] : ( isset( $values['name'] ) ? $values['name'] : $default );
417 330 if ( is_array( $name ) ) {
418 331 $name = reset( $name );
419 332 }
420 333
@@ -423,12 +336,11 @@
423 336
424 337 /**
425 338 * If $entry is numeric, get the entry object
426 339 *
340 + * @param int|object $entry By reference.
341 + *
427 342 * @since 2.0.9
428 - *
429 - * @param int|object $entry By reference.
430 - * @return void
431 343 */
432 344 public static function maybe_get_entry( &$entry ) {
433 345 if ( $entry && is_numeric( $entry ) ) {
434 346 $entry = self::getOne( $entry );
@@ -442,9 +354,9 @@
442 354
443 355 $query = "SELECT it.*, fr.name as form_name, fr.form_key as form_key FROM {$wpdb->prefix}frm_items it
444 356 LEFT OUTER JOIN {$wpdb->prefix}frm_forms fr ON it.form_id=fr.id WHERE ";
445 357
446 - $query .= is_numeric( $id ) ? 'it.id=%d' : 'it.item_key=%s';
358 + $query .= is_numeric( $id ) ? 'it.id=%d' : 'it.item_key=%s';
447 359 $query_args = array( $id );
448 360 $query = $wpdb->prepare( $query, $query_args ); // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared
449 361
450 362 if ( ! $meta ) {
@@ -556,9 +468,9 @@
556 468 $where = array( 'item_key' => $id );
557 469 }
558 470 $id = FrmDb::get_var( $wpdb->prefix . 'frm_items', $where );
559 471
560 - return $id && $id > 0;
472 + return ( $id && $id > 0 );
561 473 }
562 474
563 475 public static function getAll( $where, $order_by = '', $limit = '', $meta = false, $inc_form = true ) {
564 476 global $wpdb;
@@ -573,9 +485,9 @@
573 485 $table = $wpdb->prefix . 'frm_items it ';
574 486
575 487 if ( $inc_form ) {
576 488 $fields = 'it.*, fr.name as form_name,fr.form_key as form_key';
577 - $table .= 'LEFT OUTER JOIN ' . $wpdb->prefix . 'frm_forms fr ON it.form_id=fr.id ';
489 + $table .= 'LEFT OUTER JOIN ' . $wpdb->prefix . 'frm_forms fr ON it.form_id=fr.id ';
578 490 }
579 491
580 492 if ( preg_match( '/ meta_([0-9]+)/', $order_by, $order_matches ) ) {
581 493 $fields .= self::sort_by_field( $order_matches[1] );
@@ -667,10 +579,9 @@
667 579 }
668 580
669 581 // Pagination Methods
670 582 /**
671 - * @param array|int|string $where If int, use the form id.
672 - * @return int|string
583 + * @param int|array|string $where If int, use the form id.
673 584 */
674 585 public static function getRecordCount( $where = '' ) {
675 586 global $wpdb;
676 587 $table_join = $wpdb->prefix . 'frm_items it JOIN ' . $wpdb->prefix . 'frm_forms fr ON it.form_id=fr.id';
@@ -690,12 +601,8 @@
690 601
691 602 return $count;
692 603 }
693 604
694 - /**
695 - * @param int|string $p_size
696 - * @return int
697 - */
698 605 public static function getPageCount( $p_size, $where = '' ) {
699 606 $p_size = (int) $p_size;
700 607 $count = 1;
701 608 if ( $p_size ) {
@@ -721,9 +628,9 @@
721 628 private static function before_insert_entry_in_database( &$values, $type ) {
722 629
723 630 self::sanitize_entry_post( $values );
724 631
725 - if ( $type !== 'xml' ) {
632 + if ( $type != 'xml' ) {
726 633 $values = apply_filters( 'frm_pre_create_entry', $values );
727 634 }
728 635
729 636 $new_values = self::package_entry_data( $values );
@@ -738,9 +645,9 @@
738 645 *
739 646 * @param array $values
740 647 * @param array $new_values
741 648 *
742 - * @return bool|int $entry_id
649 + * @return boolean|int $entry_id
743 650 */
744 651 private static function continue_to_create_entry( $values, $new_values ) {
745 652 $entry_id = self::insert_entry_into_database( $new_values );
746 653 if ( ! $entry_id ) {
@@ -807,15 +714,15 @@
807 714 'description' => self::get_entry_description( $values ),
808 715 'user_id' => self::get_entry_user_id( $values ),
809 716 );
810 717
811 - $new_values['updated_by'] = $values['updated_by'] ?? $new_values['user_id'];
718 + $new_values['updated_by'] = isset( $values['updated_by'] ) ? $values['updated_by'] : $new_values['user_id'];
812 719
813 720 return $new_values;
814 721 }
815 722
816 723 private static function get_entry_value( $values, $name, $default ) {
817 - return $values[ $name ] ?? $default;
724 + return isset( $values[ $name ] ) ? $values[ $name ] : $default;
818 725 }
819 726
820 727 /**
821 728 * Get the ip for a new entry.
@@ -898,9 +805,9 @@
898 805 *
899 806 * @return string
900 807 */
901 808 private static function get_entry_description( $values ) {
902 - if ( ! empty( $values['description'] ) ) {
809 + if ( isset( $values['description'] ) && ! empty( $values['description'] ) ) {
903 810 $description = FrmAppHelper::maybe_json_encode( $values['description'] );
904 811 } else {
905 812 $description = json_encode(
906 813 array(
@@ -939,9 +846,9 @@
939 846 * @since 2.0.16
940 847 *
941 848 * @param array $new_values
942 849 *
943 - * @return bool|int $entry_id
850 + * @return int | boolean $entry_id
944 851 */
945 852 private static function insert_entry_into_database( $new_values ) {
946 853 global $wpdb;
947 854
@@ -984,35 +891,13 @@
984 891 */
985 892 private static function maybe_add_entry_metas( $values, $entry_id ) {
986 893 if ( isset( $values['item_meta'] ) ) {
987 894 FrmEntryMeta::update_entry_metas( $entry_id, $values['item_meta'] );
988 - self::maybe_add_unique_id_meta( $values, $entry_id );
989 895 }
990 896 self::maybe_add_captcha_meta( (int) $values['form_id'], (int) $entry_id );
991 897 }
992 898
993 899 /**
994 - * @since 6.16.3
995 - *
996 - * @param array $values
997 - * @param int $entry_id
998 - * @return void
999 - */
1000 - private static function maybe_add_unique_id_meta( $values, $entry_id ) {
1001 - if ( ! empty( $values['parent_form_id'] ) || empty( $values['unique_id'] ) || ! self::should_check_for_unique_id_match() ) {
1002 - return;
1003 - }
1004 -
1005 - // This unique ID is inserted with JS on form submit.
1006 - // It is used to check for duplicate entries.
1007 - $unique_id = sanitize_key( $values['unique_id'] );
1008 - if ( $unique_id ) {
1009 - FrmEntryMeta::add_entry_meta( $entry_id, 0, '', compact( 'unique_id' ) );
1010 - self::flag_new_unique_key( $unique_id );
1011 - }
1012 - }
1013 -
1014 - /**
1015 900 * @since 5.0.15
1016 901 *
1017 902 * @param int $form_id
1018 903 * @param int $entry_id
@@ -1035,10 +920,10 @@
1035 920 * @param array $values
1036 921 * @param array $new_values
1037 922 */
1038 923 private static function after_entry_created_actions( $entry_id, $values, $new_values ) {
1039 - // This is a child entry.
1040 - $is_child = isset( $values['parent_nonce'] ) && ! empty( $values['parent_form_id'] ) && wp_verify_nonce( $values['parent_nonce'], 'parent' );
924 + // this is a child entry
925 + $is_child = isset( $values['parent_form_id'] ) && isset( $values['parent_nonce'] ) && ! empty( $values['parent_form_id'] ) && wp_verify_nonce( $values['parent_nonce'], 'parent' );
1041 926
1042 927 do_action( 'frm_after_create_entry', $entry_id, $new_values['form_id'], compact( 'is_child' ) );
1043 928 do_action( 'frm_after_create_entry_' . $new_values['form_id'], $entry_id, compact( 'is_child' ) );
1044 929 }
@@ -1071,9 +956,9 @@
1071 956 * @param int $id
1072 957 * @param array $values
1073 958 * @param string $update_type
1074 959 *
1075 - * @return bool $update
960 + * @return boolean $update
1076 961 */
1077 962 private static function before_update_entry( $id, &$values, $update_type ) {
1078 963 $update = true;
1079 964
@@ -1078,13 +963,13 @@
1078 963 $update = true;
1079 964
1080 965 global $frm_vars;
1081 966
1082 - if ( isset( $frm_vars['saved_entries'] ) && is_array( $frm_vars['saved_entries'] ) && in_array( (int) $id, $frm_vars['saved_entries'] ) ) {
967 + if ( isset( $frm_vars['saved_entries'] ) && is_array( $frm_vars['saved_entries'] ) && in_array( (int) $id, (array) $frm_vars['saved_entries'] ) ) {
1083 968 $update = false;
1084 969 }
1085 970
1086 - if ( $update && $update_type !== 'xml' ) {
971 + if ( $update && $update_type != 'xml' ) {
1087 972 $values = apply_filters( 'frm_pre_update_entry', $values, $id );
1088 973 }
1089 974
1090 975 return $update;
@@ -1107,9 +992,9 @@
1107 992 'name' => self::get_new_entry_name( $values ),
1108 993 'form_id' => (int) self::get_entry_value( $values, 'form_id', null ),
1109 994 'is_draft' => self::get_is_draft_value( $values ),
1110 995 'updated_at' => current_time( 'mysql', 1 ),
1111 - 'updated_by' => $values['updated_by'] ?? get_current_user_id(),
996 + 'updated_by' => isset( $values['updated_by'] ) ? $values['updated_by'] : get_current_user_id(),
1112 997 );
1113 998
1114 999 if ( isset( $values['post_id'] ) ) {
1115 1000 $new_values['post_id'] = (int) $values['post_id'];
@@ -1136,12 +1021,12 @@
1136 1021 * Perform some actions right after updating an entry
1137 1022 *
1138 1023 * @since 2.0.16
1139 1024 *
1140 - * @param bool|int $query_results
1141 - * @param int $id
1142 - * @param array $values
1143 - * @param array $new_values
1025 + * @param boolean|int $query_results
1026 + * @param int $id
1027 + * @param array $values
1028 + * @param array $new_values
1144 1029 */
1145 1030 private static function after_update_entry( $query_results, $id, $values, $new_values ) {
1146 1031 if ( $query_results ) {
1147 1032 self::clear_cache();
@@ -1169,9 +1054,9 @@
1169 1054 * @since 2.0.16
1170 1055 *
1171 1056 * @param array $values
1172 1057 *
1173 - * @return bool|int $entry_id
1058 + * @return int | boolean $entry_id
1174 1059 */
1175 1060 public static function create_entry_from_xml( $values ) {
1176 1061 $entry_id = self::create_entry( $values, 'xml' );
1177 1062
@@ -1186,9 +1071,9 @@
1186 1071 *
1187 1072 * @param int $id
1188 1073 * @param array $values
1189 1074 *
1190 - * @return bool|int $updated
1075 + * @return int | boolean $updated
1191 1076 */
1192 1077 public static function update_entry_from_xml( $id, $values ) {
1193 1078 $updated = self::update_entry( $id, $values, 'xml' );
1194 1079
@@ -1210,9 +1095,9 @@
1210 1095 * Get entries count.
1211 1096 *
1212 1097 * @since 6.8
1213 1098 *
1214 - * @return int|string
1099 + * @return int
1215 1100 */
1216 1101 public static function get_entries_count() {
1217 1102 $args = array(
1218 1103 'or' => 1,