PluginProbe
Formidable Forms – WordPress Form Builder for Contact Forms, Calculators, Quizzes & More / 6.2
Formidable Forms – WordPress Form Builder for Contact Forms, Calculators, Quizzes & More v6.2
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 +150 -349 6.276.2 View file →
@@ -5,67 +5,45 @@
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 - return self::create_entry( $values, 'standard' );
16 + $entry_id = self::create_entry( $values, 'standard' );
17 +
18 + return $entry_id;
24 19 }
25 20
26 21 /**
27 22 * Create a new entry with some differences depending on type
28 23 *
29 - * @param array $values
24 + * @param array $values
30 25 * @param string $type
31 26 *
32 - * @return bool|int $entry_id
27 + * @return int | boolean $entry_id
33 28 */
34 29 private static function create_entry( $values, $type ) {
35 30 $new_values = self::before_insert_entry_in_database( $values, $type );
36 31
37 32 // Don't check XML entries for duplicates
38 - if ( $type !== 'xml' && self::is_duplicate( $new_values, $values ) ) {
33 + if ( $type != 'xml' && self::is_duplicate( $new_values, $values ) ) {
39 34 return false;
40 35 }
41 36
42 - return self::continue_to_create_entry( $values, $new_values );
43 - }
37 + $entry_id = self::continue_to_create_entry( $values, $new_values );
44 38
45 - /**
46 - * Flag the memoized unique id check after a new entry is created.
47 - * This prevents possibly DB requests and helps avoid issues when creating repeater entries.
48 - *
49 - * @since 6.16.3
50 - *
51 - * @param string $unique_id
52 - *
53 - * @return void
54 - */
55 - private static function flag_new_unique_key( $unique_id ) {
56 - if ( ! isset( self::$unique_id_match_checks[ $unique_id ] ) ) {
57 - self::$unique_id_match_checks[ $unique_id ] = false;
58 - }
39 + return $entry_id;
59 40 }
60 41
61 42 /**
62 43 * Check for duplicate entries created in the last minute
63 44 *
64 - * @param array $new_values New values.
65 - * @param array $values Values.
66 - *
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,18 +50,13 @@
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 - // phpcs:ignore Universal.Operators.StrictComparisons
86 59 if ( $new_values['item_key'] == $new_values['name'] ) {
87 60 unset( $check_val['name'] );
88 61 }
89 62
@@ -97,10 +70,10 @@
97 70 }
98 71
99 72 global $frm_vars;
100 73 $frm_vars['checking_duplicates'] = true;
101 - $is_duplicate = false;
102 74
75 + $is_duplicate = false;
103 76 foreach ( $entry_exists as $entry_exist ) {
104 77 $is_duplicate = true;
105 78
106 79 // make sure it's a duplicate
@@ -105,14 +78,9 @@
105 78
106 79 // make sure it's a duplicate
107 80 $metas = FrmEntryMeta::get_entry_meta_info( $entry_exist );
108 81 $field_metas = array();
109 -
110 82 foreach ( $metas as $meta ) {
111 - if ( 0 === (int) $meta->field_id ) {
112 - continue;
113 - }
114 -
115 83 $field_metas[ $meta->field_id ] = $meta->meta_value;
116 84 }
117 85
118 86 $filtered_vals = array_filter( $values['item_meta'] );
@@ -119,9 +87,9 @@
119 87 $filtered_vals = self::convert_values_to_their_saved_value( $filtered_vals, $entry_exist );
120 88 $field_metas = array_filter( $field_metas );
121 89
122 90 // If prev entry is empty and current entry is not, they are not duplicates
123 - if ( ! $field_metas && $filtered_vals ) {
91 + if ( empty( $field_metas ) && ! empty( $filtered_vals ) ) {
124 92 return false;
125 93 }
126 94
127 95 // compare serialized values and not arrays
@@ -138,12 +106,12 @@
138 106 continue;
139 107 }
140 108
141 109 $diff = array_diff_assoc( $field_metas, $new_meta );
142 -
143 - foreach ( $diff as $meta_value ) {
110 + foreach ( $diff as $field_id => $meta_value ) {
144 111 if ( ! empty( $meta_value ) ) {
145 112 $is_duplicate = false;
113 + continue;
146 114 }
147 115 }
148 116
149 117 if ( $is_duplicate ) {
@@ -148,9 +116,9 @@
148 116
149 117 if ( $is_duplicate ) {
150 118 break;
151 119 }
152 - }//end foreach
120 + }
153 121
154 122 $frm_vars['checking_duplicates'] = false;
155 123
156 124 return $is_duplicate;
@@ -156,95 +124,25 @@
156 124 return $is_duplicate;
157 125 }
158 126
159 127 /**
160 - * @since 6.16.3
161 - *
162 - * @param array $values POST request data.
163 - * @param string $created_at The timestamp of the entry we are checking for.
164 - *
165 - * @return bool
166 - */
167 - private static function maybe_check_for_unique_id_match( $values, $created_at ) {
168 - if ( ! self::should_check_for_unique_id_match() ) {
169 - return false;
170 - }
171 -
172 - if ( empty( $values['unique_id'] ) ) {
173 - return false;
174 - }
175 -
176 - $unique_id = sanitize_key( $values['unique_id'] );
177 -
178 - if ( ! $unique_id ) {
179 - // Only continue if a unique ID was generated on form submit.
180 - return false;
181 - }
182 -
183 - if ( isset( self::$unique_id_match_checks[ $unique_id ] ) ) {
184 - return self::$unique_id_match_checks[ $unique_id ];
185 - }
186 -
187 - $timestamp = strtotime( $created_at );
188 -
189 - if ( false === $timestamp ) {
190 - $timestamp = time();
191 - }
192 -
193 - self::$unique_id_match_checks[ $unique_id ] = (bool) FrmDb::get_var(
194 - 'frm_item_metas',
195 - array(
196 - 'field_id' => 0,
197 - 'meta_value' => serialize( compact( 'unique_id' ) ),
198 - 'created_at >' => gmdate( 'Y-m-d H:i:s', $timestamp - MONTH_IN_SECONDS ),
199 - ),
200 - 'id'
201 - );
202 -
203 - return self::$unique_id_match_checks[ $unique_id ];
204 - }
205 -
206 - /**
207 - * @since 6.16.3
208 - *
209 - * @return bool
210 - */
211 - private static function should_check_for_unique_id_match() {
212 - /**
213 - * Allow users to opt out of the DB query, in case it causes performance issues.
214 - *
215 - * @since 6.16.3
216 - *
217 - * @param bool $should_extend
218 - */
219 - $should_check = apply_filters( 'frm_check_for_unique_id_match', true );
220 - return (bool) $should_check;
221 - }
222 -
223 - /**
224 128 * Convert form data to the actual value that would be saved into the database.
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.
225 130 *
226 - * This is important for the duplicate check as something like 'a:2:{s:5:"typed";s:0:"";s:6:"output";s:0:"";}'
227 - * (a signature value) is actually an empty string and does not get saved.
228 - *
229 131 * @param array $filter_vals
230 132 * @param int $entry_id
231 - *
232 133 * @return array
233 134 */
234 135 private static function convert_values_to_their_saved_value( $filter_vals, $entry_id ) {
235 136 $reduced = array();
236 -
237 137 foreach ( $filter_vals as $field_id => $value ) {
238 138 $field = FrmFieldFactory::get_field_object( $field_id );
239 139 $reduced[ $field_id ] = $field->get_value_to_save( $value, array( 'entry_id' => $entry_id ) );
240 140 $reduced[ $field_id ] = $field->set_value_before_save( $reduced[ $field_id ] );
241 -
242 141 if ( '' === $reduced[ $field_id ] || ( is_array( $reduced[ $field_id ] ) && 0 === count( $reduced[ $field_id ] ) ) ) {
243 142 unset( $reduced[ $field_id ] );
244 143 }
245 144 }
246 -
247 145 return $reduced;
248 146 }
249 147
250 148 /**
@@ -252,15 +150,15 @@
252 150 *
253 151 * @since 2.0.23
254 152 *
255 153 * @param array $values
256 - * @param int $duplicate_entry_time
154 + * @param int $duplicate_entry_time
257 155 *
258 156 * @return bool
259 157 */
260 158 private static function is_duplicate_check_needed( $values, $duplicate_entry_time ) {
261 159 // If time for checking duplicates is set to an empty value, don't check for duplicates
262 - if ( ! $duplicate_entry_time ) {
160 + if ( empty( $duplicate_entry_time ) ) {
263 161 return false;
264 162 }
265 163
266 164 // If CSV is importing, don't check for duplicates
@@ -268,20 +166,20 @@
268 166 return false;
269 167 }
270 168
271 169 // If repeating field entries are getting created, don't check for duplicates
272 - return empty( $values['parent_form_id'] );
170 + if ( isset( $values['parent_form_id'] ) && $values['parent_form_id'] ) {
171 + return false;
172 + }
173 +
174 + return true;
273 175 }
274 176
275 - /**
276 - * @param int|string $id
277 - *
278 - * @return false|int
279 - */
280 177 public static function duplicate( $id ) {
281 178 global $wpdb;
282 179
283 - $values = self::getOne( $id );
180 + $values = self::getOne( $id );
181 +
284 182 $new_values = array();
285 183 $new_values['item_key'] = FrmAppHelper::get_unique_key( '', $wpdb->prefix . 'frm_items', 'item_key' );
286 184 $new_values['name'] = $values->name;
287 185 $new_values['is_draft'] = $values->is_draft;
@@ -291,9 +189,8 @@
291 189 $new_values['created_at'] = current_time( 'mysql', 1 );
292 190 $new_values['updated_at'] = $new_values['created_at'];
293 191
294 192 $query_results = $wpdb->insert( $wpdb->prefix . 'frm_items', $new_values );
295 -
296 193 if ( ! $query_results ) {
297 194 return false;
298 195 }
299 196
@@ -299,9 +196,8 @@
299 196
300 197 $entry_id = $wpdb->insert_id;
301 198
302 199 global $frm_vars;
303 -
304 200 if ( ! isset( $frm_vars['saved_entries'] ) ) {
305 201 $frm_vars['saved_entries'] = array();
306 202 }
307 203 $frm_vars['saved_entries'][] = (int) $entry_id;
@@ -316,15 +212,17 @@
316 212
317 213 /**
318 214 * Update an entry (not via XML)
319 215 *
320 - * @param int $id
216 + * @param int $id
321 217 * @param array $values
322 218 *
323 - * @return bool|int $update_results
219 + * @return boolean|int $update_results
324 220 */
325 221 public static function update( $id, $values ) {
326 - return self::update_entry( $id, $values, 'standard' );
222 + $update_results = self::update_entry( $id, $values, 'standard' );
223 +
224 + return $update_results;
327 225 }
328 226
329 227 /**
330 228 * Update an entry with some differences depending on the update type
@@ -330,24 +228,23 @@
330 228 * Update an entry with some differences depending on the update type
331 229 *
332 230 * @since 2.0.16
333 231 *
334 - * @param int $id
335 - * @param array $values
336 - * @param string $update_type
232 + * @param int $id
233 + * @param array $values
337 234 *
338 - * @return bool|int $query_results
235 + * @return boolean|int $query_results
339 236 */
340 237 private static function update_entry( $id, $values, $update_type ) {
341 238 global $wpdb;
342 239
343 240 $update = self::before_update_entry( $id, $values, $update_type );
344 -
345 241 if ( ! $update ) {
346 242 return false;
347 243 }
348 244
349 - $new_values = self::package_entry_to_update( $id, $values );
245 + $new_values = self::package_entry_to_update( $id, $values );
246 +
350 247 $query_results = $wpdb->update( $wpdb->prefix . 'frm_items', $new_values, compact( 'id' ) );
351 248
352 249 self::after_update_entry( $query_results, $id, $values, $new_values );
353 250
@@ -356,10 +253,9 @@
356 253
357 254 /**
358 255 * Delete an entry.
359 256 *
360 - * @param int|string $id
361 - *
257 + * @param string|int $id
362 258 * @return bool True on success, false if nothing was deleted.
363 259 */
364 260 public static function destroy( $id ) {
365 261 global $wpdb;
@@ -364,13 +260,12 @@
364 260 public static function destroy( $id ) {
365 261 global $wpdb;
366 262 $id = (int) $id;
367 263
368 - // Item meta is required for conditional logic in actions with 'delete' events.
369 - $entry = self::getOne( $id, true );
370 -
264 + $entry = self::getOne( $id, true ); // Item meta is required for conditional logic in actions with 'delete' events.
371 265 if ( ! $entry ) {
372 - return false;
266 + $result = false;
267 + return $result;
373 268 }
374 269
375 270 /**
376 271 * Trigger an action to run custom logic before the entry is deleted.
@@ -398,20 +293,12 @@
398 293
399 294 return $result;
400 295 }
401 296
402 - /**
403 - * @param int $id
404 - * @param mixed $value
405 - * @param int|string $form_id
406 - *
407 - * @return false|int
408 - */
409 297 public static function update_form( $id, $value, $form_id ) {
410 298 global $wpdb;
411 299 $form_id = isset( $value ) ? $form_id : null;
412 300 $result = $wpdb->update( $wpdb->prefix . 'frm_items', array( 'form_id' => $form_id ), array( 'id' => $id ) );
413 -
414 301 if ( $result ) {
415 302 self::clear_cache();
416 303 }
417 304
@@ -422,10 +309,8 @@
422 309 * Clear entry caching
423 310 * Called when an entry is changed
424 311 *
425 312 * @since 2.0.5
426 - *
427 - * @return void
428 313 */
429 314 public static function clear_cache() {
430 315 FrmDb::cache_delete_group( 'frm_entry' );
431 316 FrmDb::cache_delete_group( 'frm_item' );
@@ -437,17 +322,11 @@
437 322 * After switching to the wp_loaded hook for processing entries,
438 323 * we can no longer use 'name', but check it as a fallback
439 324 *
440 325 * @since 2.0.11
441 - *
442 - * @param array $values
443 - * @param array|string $default
444 - *
445 - * @return string
446 326 */
447 327 public static function get_new_entry_name( $values, $default = '' ) {
448 - $name = $values['item_name'] ?? $values['name'] ?? $default;
449 -
328 + $name = isset( $values['item_name'] ) ? $values['item_name'] : ( isset( $values['name'] ) ? $values['name'] : $default );
450 329 if ( is_array( $name ) ) {
451 330 $name = reset( $name );
452 331 }
453 332
@@ -456,28 +335,20 @@
456 335
457 336 /**
458 337 * If $entry is numeric, get the entry object
459 338 *
339 + * @param int|object $entry by reference
340 + *
460 341 * @since 2.0.9
461 - *
462 - * @param int|object $entry By reference.
463 - *
464 - * @return void
465 342 */
466 343 public static function maybe_get_entry( &$entry ) {
467 344 if ( $entry && is_numeric( $entry ) ) {
468 345 $entry = self::getOne( $entry );
469 - } elseif ( ! $entry || 'false' === $entry ) {
346 + } elseif ( empty( $entry ) || 'false' === $entry ) {
470 347 $entry = false;
471 348 }
472 349 }
473 350
474 - /**
475 - * @param int|string $id
476 - * @param bool $meta
477 - *
478 - * @return object|null
479 - */
480 351 public static function getOne( $id, $meta = false ) {
481 352 global $wpdb;
482 353
483 354 $query = "SELECT it.*, fr.name as form_name, fr.form_key as form_key FROM {$wpdb->prefix}frm_items it
@@ -482,9 +353,9 @@
482 353
483 354 $query = "SELECT it.*, fr.name as form_name, fr.form_key as form_key FROM {$wpdb->prefix}frm_items it
484 355 LEFT OUTER JOIN {$wpdb->prefix}frm_forms fr ON it.form_id=fr.id WHERE ";
485 356
486 - $query .= is_numeric( $id ) ? 'it.id=%d' : 'it.item_key=%s';
357 + $query .= is_numeric( $id ) ? 'it.id=%d' : 'it.item_key=%s';
487 358 $query_args = array( $id );
488 359 $query = $wpdb->prepare( $query, $query_args ); // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared
489 360
490 361 if ( ! $meta ) {
@@ -493,9 +364,8 @@
493 364 return $entry;
494 365 }
495 366
496 367 $entry = FrmDb::check_cache( $id, 'frm_entry' );
497 -
498 368 if ( $entry !== false ) {
499 369 self::prepare_entry( $entry );
500 370 return $entry;
501 371 }
@@ -510,19 +380,16 @@
510 380 /**
511 381 * @since 4.02.03
512 382 *
513 383 * @param object $entry
514 - *
515 - * @return void
516 384 */
517 385 private static function prepare_entry( &$entry ) {
518 - if ( ! $entry ) {
386 + if ( empty( $entry ) ) {
519 387 return;
520 388 }
521 389
522 390 FrmAppHelper::unserialize_or_decode( $entry->description );
523 - // TODO: Remove slashes on input only, not output.
524 - $entry = wp_unslash( $entry );
391 + $entry = wp_unslash( $entry ); // TODO: Remove slashes on input only, not output.
525 392 }
526 393
527 394 /**
528 395 * @since 4.02.03
@@ -527,10 +394,8 @@
527 394 /**
528 395 * @since 4.02.03
529 396 *
530 397 * @param array $entries
531 - *
532 - * @return void
533 398 */
534 399 private static function prepare_entries( &$entries ) {
535 400 foreach ( $entries as $k => $entry ) {
536 401 self::prepare_entry( $entry );
@@ -537,13 +402,8 @@
537 402 $entries[ $k ] = $entry;
538 403 }
539 404 }
540 405
541 - /**
542 - * @param object|null $entry
543 - *
544 - * @return object|null
545 - */
546 406 public static function get_meta( $entry ) {
547 407 if ( ! $entry ) {
548 408 return $entry;
549 409 }
@@ -560,15 +420,13 @@
560 420
561 421 $entry->metas = array();
562 422
563 423 $include_key = apply_filters( 'frm_include_meta_keys', false, array( 'form_id' => $entry->form_id ) );
564 -
565 424 foreach ( $metas as $meta_val ) {
566 425 FrmFieldsHelper::prepare_field_value( $meta_val->meta_value, $meta_val->type );
567 426
568 - if ( (int) $meta_val->item_id === (int) $entry->id ) {
427 + if ( $meta_val->item_id == $entry->id ) {
569 428 $entry->metas[ $meta_val->field_id ] = $meta_val->meta_value;
570 -
571 429 if ( $include_key ) {
572 430 $entry->metas[ $meta_val->field_key ] = $entry->metas[ $meta_val->field_id ];
573 431 }
574 432 continue;
@@ -581,9 +439,9 @@
581 439
582 440 $entry->metas[ $meta_val->field_id ][] = $meta_val->meta_value;
583 441
584 442 unset( $meta_val );
585 - }//end foreach
443 + }
586 444 unset( $metas );
587 445
588 446 FrmDb::set_cache( $entry->id, $entry, 'frm_entry' );
589 447
@@ -591,47 +449,43 @@
591 449 }
592 450
593 451 /**
594 452 * @param string $id
595 - *
596 - * @return bool
597 453 */
598 454 public static function exists( $id ) {
599 455 global $wpdb;
600 456
601 457 if ( FrmDb::check_cache( $id, 'frm_entry' ) ) {
602 - return true;
458 + $exists = true;
459 +
460 + return $exists;
603 461 }
604 462
605 - $where = is_numeric( $id ) ? array( 'id' => $id ) : array( 'item_key' => $id );
606 - $id = FrmDb::get_var( $wpdb->prefix . 'frm_items', $where );
463 + if ( is_numeric( $id ) ) {
464 + $where = array( 'id' => $id );
465 + } else {
466 + $where = array( 'item_key' => $id );
467 + }
468 + $id = FrmDb::get_var( $wpdb->prefix . 'frm_items', $where );
607 469
608 - return $id && $id > 0;
470 + return ( $id && $id > 0 );
609 471 }
610 472
611 - /**
612 - * @param array|string $where
613 - * @param string $order_by
614 - * @param string $limit
615 - * @param bool $meta
616 - * @param bool $inc_form
617 - *
618 - * @return array
619 - */
620 473 public static function getAll( $where, $order_by = '', $limit = '', $meta = false, $inc_form = true ) {
621 474 global $wpdb;
622 475
623 - $limit = FrmDb::esc_limit( $limit );
476 + $limit = FrmDb::esc_limit( $limit );
477 +
624 478 $cache_key = FrmAppHelper::maybe_json_encode( $where ) . $order_by . $limit . $inc_form;
625 479 $entries = wp_cache_get( $cache_key, 'frm_entry' );
626 480
627 481 if ( false === $entries ) {
628 - $fields = 'it.id, it.item_key, it.name, it.ip, it.form_id, it.post_id, it.user_id, it.parent_item_id, it.updated_by, it.created_at, it.updated_at, it.is_draft, it.description'; // phpcs:ignore SlevomatCodingStandard.Files.LineLength.LineTooLong
482 + $fields = 'it.id, it.item_key, it.name, it.ip, it.form_id, it.post_id, it.user_id, it.parent_item_id, it.updated_by, it.created_at, it.updated_at, it.is_draft, it.description';
629 483 $table = $wpdb->prefix . 'frm_items it ';
630 484
631 485 if ( $inc_form ) {
632 486 $fields = 'it.*, fr.name as form_name,fr.form_key as form_key';
633 - $table .= 'LEFT OUTER JOIN ' . $wpdb->prefix . 'frm_forms fr ON it.form_id=fr.id ';
487 + $table .= 'LEFT OUTER JOIN ' . $wpdb->prefix . 'frm_forms fr ON it.form_id=fr.id ';
634 488 }
635 489
636 490 if ( preg_match( '/ meta_([0-9]+)/', $order_by, $order_matches ) ) {
637 491 $fields .= self::sort_by_field( $order_matches[1] );
@@ -644,9 +498,9 @@
644 498 $entries = $wpdb->get_results( $query, OBJECT_K ); // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared
645 499 unset( $query );
646 500
647 501 FrmDb::set_cache( $cache_key, $entries, 'frm_entry' );
648 - }//end if
502 + }
649 503
650 504 if ( ! $meta || ! $entries ) {
651 505 self::prepare_entries( $entries );
652 506 return $entries;
@@ -657,11 +511,9 @@
657 511 $where = array( 'it.form_id' => substr( $where, 11 ) );
658 512 }
659 513
660 514 $meta_where = array( 'field_id !' => 0 );
661 -
662 - // phpcs:ignore Universal.Operators.StrictComparisons
663 - if ( $limit == '' && is_array( $where ) && count( $where ) === 1 && isset( $where['it.form_id'] ) ) {
515 + if ( $limit == '' && is_array( $where ) && count( $where ) == 1 && isset( $where['it.form_id'] ) ) {
664 516 $meta_where['fi.form_id'] = $where['it.form_id'];
665 517 } else {
666 518 $meta_where['item_id'] = array_keys( $entries );
667 519 }
@@ -704,15 +556,15 @@
704 556 return $entries;
705 557 }
706 558
707 559 /**
708 - * @param int|string $field_id
709 - *
560 + * @param int $field_id
710 561 * @return string
711 562 */
712 563 private static function sort_by_field( $field_id ) {
713 564 global $wpdb;
714 - $field_id = (int) $field_id;
565 + $field_id = (int) $field_id;
566 +
715 567 $field_options = FrmDb::get_var( 'frm_fields', array( 'id' => $field_id ), 'field_options' );
716 568 FrmAppHelper::unserialize_or_decode( $field_options );
717 569
718 570 if ( empty( $field_options['post_field'] ) ) {
@@ -725,15 +577,13 @@
725 577 }
726 578
727 579 // Pagination Methods
728 580 /**
729 - * @param array|int|string $where If int, use the form id.
730 - *
731 - * @return int|string
581 + * @param int|array|string If int, use the form id.
732 582 */
733 583 public static function getRecordCount( $where = '' ) {
734 584 global $wpdb;
735 - $table_join = $wpdb->prefix . 'frm_items it JOIN ' . $wpdb->prefix . 'frm_forms fr ON it.form_id=fr.id';
585 + $table_join = $wpdb->prefix . 'frm_items it LEFT OUTER JOIN ' . $wpdb->prefix . 'frm_forms fr ON it.form_id=fr.id';
736 586
737 587 if ( is_numeric( $where ) ) {
738 588 $table_join = 'frm_items';
739 589 $where = array( 'form_id' => $where );
@@ -739,32 +589,25 @@
739 589 $where = array( 'form_id' => $where );
740 590 }
741 591
742 592 if ( is_array( $where ) ) {
743 - return FrmDb::get_count( $table_join, $where );
593 + $count = FrmDb::get_count( $table_join, $where );
594 + } else {
595 + $cache_key = 'count_' . FrmAppHelper::maybe_json_encode( $where );
596 + $query = 'SELECT COUNT(*) FROM ' . $table_join . FrmDb::prepend_and_or_where( ' WHERE ', $where );
597 + $count = FrmDb::check_cache( $cache_key, 'frm_entry', $query, 'get_var' );
744 598 }
745 599
746 - $cache_key = 'count_' . FrmAppHelper::maybe_json_encode( $where );
747 - $query = 'SELECT COUNT(*) FROM ' . $table_join . FrmDb::prepend_and_or_where( ' WHERE ', $where );
748 -
749 - return FrmDb::check_cache( $cache_key, 'frm_entry', $query, 'get_var' );
600 + return $count;
750 601 }
751 602
752 - /**
753 - * @param int|string $p_size
754 - * @param array|int|string $where
755 - *
756 - * @return int
757 - */
758 603 public static function getPageCount( $p_size, $where = '' ) {
759 604 $p_size = (int) $p_size;
760 605 $count = 1;
761 -
762 606 if ( $p_size ) {
763 607 if ( ! is_numeric( $where ) ) {
764 608 $where = self::getRecordCount( $where );
765 609 }
766 -
767 610 $count = ceil( (int) $where / $p_size );
768 611 }
769 612
770 613 return $count;
@@ -774,21 +617,24 @@
774 617 * Prepare the data before inserting it into the database
775 618 *
776 619 * @since 2.0.16
777 620 *
778 - * @param array $values
621 + * @param array $values
779 622 * @param string $type
780 623 *
781 624 * @return array $new_values
782 625 */
783 626 private static function before_insert_entry_in_database( &$values, $type ) {
627 +
784 628 self::sanitize_entry_post( $values );
785 629
786 - if ( $type !== 'xml' ) {
630 + if ( $type != 'xml' ) {
787 631 $values = apply_filters( 'frm_pre_create_entry', $values );
788 632 }
789 633
790 - return self::package_entry_data( $values );
634 + $new_values = self::package_entry_data( $values );
635 +
636 + return $new_values;
791 637 }
792 638
793 639 /**
794 640 * Create an entry and perform after create actions
@@ -797,13 +643,12 @@
797 643 *
798 644 * @param array $values
799 645 * @param array $new_values
800 646 *
801 - * @return bool|int $entry_id
647 + * @return boolean|int $entry_id
802 648 */
803 649 private static function continue_to_create_entry( $values, $new_values ) {
804 650 $entry_id = self::insert_entry_into_database( $new_values );
805 -
806 651 if ( ! $entry_id ) {
807 652 return false;
808 653 }
809 654
@@ -816,11 +661,9 @@
816 661 * Sanitize the POST values before we use them
817 662 *
818 663 * @since 2.0
819 664 *
820 - * @param array $values The POST values by reference.
821 - *
822 - * @return void
665 + * @param array $values The POST values by reference
823 666 */
824 667 public static function sanitize_entry_post( &$values ) {
825 668 $sanitize_method = array(
826 669 'form_id' => 'absint',
@@ -869,22 +712,15 @@
869 712 'description' => self::get_entry_description( $values ),
870 713 'user_id' => self::get_entry_user_id( $values ),
871 714 );
872 715
873 - $new_values['updated_by'] = $values['updated_by'] ?? $new_values['user_id'];
716 + $new_values['updated_by'] = isset( $values['updated_by'] ) ? $values['updated_by'] : $new_values['user_id'];
874 717
875 718 return $new_values;
876 719 }
877 720
878 - /**
879 - * @param array $values
880 - * @param string $name
881 - * @param mixed $default
882 - *
883 - * @return mixed
884 - */
885 721 private static function get_entry_value( $values, $name, $default ) {
886 - return $values[ $name ] ?? $default;
722 + return isset( $values[ $name ] ) ? $values[ $name ] : $default;
887 723 }
888 724
889 725 /**
890 726 * Get the ip for a new entry.
@@ -901,9 +737,8 @@
901 737 return '';
902 738 }
903 739
904 740 $ip = FrmAppHelper::get_ip_address();
905 -
906 741 if ( defined( 'WP_IMPORTING' ) && WP_IMPORTING ) {
907 742 $ip = self::get_entry_value( $values, 'ip', $ip );
908 743 }
909 744
@@ -919,13 +754,9 @@
919 754 *
920 755 * @return int
921 756 */
922 757 private static function get_is_draft_value( $values ) {
923 - if ( isset( $values['frm_saving_draft'] ) && FrmEntriesHelper::DRAFT_ENTRY_STATUS === (int) $values['frm_saving_draft'] ) {
924 - return FrmEntriesHelper::DRAFT_ENTRY_STATUS;
925 - }
926 -
927 - return isset( $values['is_draft'] ) ? absint( $values['is_draft'] ) : FrmEntriesHelper::SUBMITTED_ENTRY_STATUS;
758 + return ( ( isset( $values['frm_saving_draft'] ) && $values['frm_saving_draft'] == 1 ) || ( isset( $values['is_draft'] ) && $values['is_draft'] == 1 ) ) ? 1 : 0;
928 759 }
929 760
930 761 /**
931 762 * Get the created_at value for a new entry
@@ -949,9 +780,15 @@
949 780 *
950 781 * @return string
951 782 */
952 783 private static function get_updated_at( $values ) {
953 - return $values['updated_at'] ?? self::get_created_at( $values );
784 + if ( isset( $values['updated_at'] ) ) {
785 + $updated_at = $values['updated_at'];
786 + } else {
787 + $updated_at = self::get_created_at( $values );
788 + }
789 +
790 + return $updated_at;
954 791 }
955 792
956 793 /**
957 794 * Get the description value for a new entry
@@ -962,18 +799,20 @@
962 799 *
963 800 * @return string
964 801 */
965 802 private static function get_entry_description( $values ) {
966 - if ( ! empty( $values['description'] ) ) {
967 - return FrmAppHelper::maybe_json_encode( $values['description'] );
803 + if ( isset( $values['description'] ) && ! empty( $values['description'] ) ) {
804 + $description = FrmAppHelper::maybe_json_encode( $values['description'] );
805 + } else {
806 + $description = json_encode(
807 + array(
808 + 'browser' => FrmAppHelper::get_server_value( 'HTTP_USER_AGENT' ),
809 + 'referrer' => FrmAppHelper::get_server_value( 'HTTP_REFERER' ),
810 + )
811 + );
968 812 }
969 813
970 - return json_encode(
971 - array(
972 - 'browser' => FrmAppHelper::get_server_value( 'HTTP_USER_AGENT' ),
973 - 'referrer' => FrmAppHelper::get_server_value( 'HTTP_REFERER' ),
974 - )
975 - );
814 + return $description;
976 815 }
977 816
978 817 /**
979 818 * Get the user_id value for a new entry
@@ -985,13 +824,15 @@
985 824 * @return int
986 825 */
987 826 private static function get_entry_user_id( $values ) {
988 827 if ( isset( $values['frm_user_id'] ) && ( is_numeric( $values['frm_user_id'] ) || FrmAppHelper::is_admin() ) ) {
989 - return $values['frm_user_id'];
828 + $user_id = $values['frm_user_id'];
829 + } else {
830 + $current_user_id = get_current_user_id();
831 + $user_id = $current_user_id ? $current_user_id : 0;
990 832 }
991 833
992 - $current_user_id = get_current_user_id();
993 - return $current_user_id ? $current_user_id : 0;
834 + return $user_id;
994 835 }
995 836
996 837 /**
997 838 * Insert new entry into the database
@@ -999,9 +840,9 @@
999 840 * @since 2.0.16
1000 841 *
1001 842 * @param array $new_values
1002 843 *
1003 - * @return bool|int $entry_id
844 + * @return int | boolean $entry_id
1004 845 */
1005 846 private static function insert_entry_into_database( $new_values ) {
1006 847 global $wpdb;
1007 848
@@ -1006,9 +847,15 @@
1006 847 global $wpdb;
1007 848
1008 849 $query_results = $wpdb->insert( $wpdb->prefix . 'frm_items', $new_values );
1009 850
1010 - return ! $query_results ? false : $wpdb->insert_id;
851 + if ( ! $query_results ) {
852 + $entry_id = false;
853 + } else {
854 + $entry_id = $wpdb->insert_id;
855 + }
856 +
857 + return $entry_id;
1011 858 }
1012 859
1013 860 /**
1014 861 * Add the new entry to global $frm_vars
@@ -1014,11 +861,9 @@
1014 861 * Add the new entry to global $frm_vars
1015 862 *
1016 863 * @since 2.0.16
1017 864 *
1018 - * @param int|string $entry_id
1019 - *
1020 - * @return void
865 + * @param int $entry_id
1021 866 */
1022 867 private static function add_new_entry_to_frm_vars( $entry_id ) {
1023 868 global $frm_vars;
1024 869
@@ -1033,55 +878,28 @@
1033 878 * Add entry metas, if there are any
1034 879 *
1035 880 * @since 2.0.16
1036 881 *
1037 - * @param array $values
1038 - * @param int|string $entry_id
1039 - *
882 + * @param array $values
883 + * @param int $entry_id
1040 884 * @return void
1041 885 */
1042 886 private static function maybe_add_entry_metas( $values, $entry_id ) {
1043 887 if ( isset( $values['item_meta'] ) ) {
1044 888 FrmEntryMeta::update_entry_metas( $entry_id, $values['item_meta'] );
1045 - self::maybe_add_unique_id_meta( $values, $entry_id );
1046 889 }
1047 890 self::maybe_add_captcha_meta( (int) $values['form_id'], (int) $entry_id );
1048 891 }
1049 892
1050 893 /**
1051 - * @since 6.16.3
1052 - *
1053 - * @param array $values
1054 - * @param int $entry_id
1055 - *
1056 - * @return void
1057 - */
1058 - private static function maybe_add_unique_id_meta( $values, $entry_id ) {
1059 - if ( ! empty( $values['parent_form_id'] ) || empty( $values['unique_id'] ) || ! self::should_check_for_unique_id_match() ) {
1060 - return;
1061 - }
1062 -
1063 - // This unique ID is inserted with JS on form submit.
1064 - // It is used to check for duplicate entries.
1065 - $unique_id = sanitize_key( $values['unique_id'] );
1066 -
1067 - if ( $unique_id ) {
1068 - FrmEntryMeta::add_entry_meta( $entry_id, 0, '', compact( 'unique_id' ) );
1069 - self::flag_new_unique_key( $unique_id );
1070 - }
1071 - }
1072 -
1073 - /**
1074 894 * @since 5.0.15
1075 895 *
1076 896 * @param int $form_id
1077 897 * @param int $entry_id
1078 - *
1079 898 * @return void
1080 899 */
1081 900 private static function maybe_add_captcha_meta( $form_id, $entry_id ) {
1082 901 global $frm_vars;
1083 -
1084 902 if ( array_key_exists( 'captcha_scores', $frm_vars ) && array_key_exists( $form_id, $frm_vars['captcha_scores'] ) ) {
1085 903 $captcha_score_meta = array( 'captcha_score' => $frm_vars['captcha_scores'][ $form_id ] );
1086 904 FrmEntryMeta::add_entry_meta( $entry_id, 0, '', maybe_serialize( $captcha_score_meta ) );
1087 905 }
@@ -1091,17 +909,14 @@
1091 909 * Trigger frm_after_create_entry hooks
1092 910 *
1093 911 * @since 2.0.16
1094 912 *
1095 - * @param int $entry_id
1096 - * @param array $values
913 + * @param int $entry_id
1097 914 * @param array $new_values
1098 - *
1099 - * @return void
1100 915 */
1101 916 private static function after_entry_created_actions( $entry_id, $values, $new_values ) {
1102 - // This is a child entry.
1103 - $is_child = isset( $values['parent_nonce'] ) && ! empty( $values['parent_form_id'] ) && wp_verify_nonce( $values['parent_nonce'], 'parent' );
917 + // this is a child entry
918 + $is_child = isset( $values['parent_form_id'] ) && isset( $values['parent_nonce'] ) && ! empty( $values['parent_form_id'] ) && wp_verify_nonce( $values['parent_nonce'], 'parent' );
1104 919
1105 920 do_action( 'frm_after_create_entry', $entry_id, $new_values['form_id'], compact( 'is_child' ) );
1106 921 do_action( 'frm_after_create_entry_' . $new_values['form_id'], $entry_id, compact( 'is_child' ) );
1107 922 }
@@ -1112,13 +927,12 @@
1112 927 * @since 2.0.16
1113 928 *
1114 929 * @param array $values
1115 930 * @param array $new_values
1116 - * @param int $entry_id
1117 - *
1118 - * @return void
931 + * @param int $entry_id
1119 932 */
1120 933 private static function after_insert_entry_in_database( $values, $new_values, $entry_id ) {
934 +
1121 935 self::add_new_entry_to_frm_vars( $entry_id );
1122 936
1123 937 self::maybe_add_entry_metas( $values, $entry_id );
1124 938
@@ -1131,13 +945,13 @@
1131 945 * Perform some actions right before updating an entry
1132 946 *
1133 947 * @since 2.0.16
1134 948 *
1135 - * @param int|string $id
1136 - * @param array $values
1137 - * @param string $update_type
949 + * @param int $id
950 + * @param array $values
951 + * @param string $update_type
1138 952 *
1139 - * @return bool $update
953 + * @return boolean $update
1140 954 */
1141 955 private static function before_update_entry( $id, &$values, $update_type ) {
1142 956 $update = true;
1143 957
@@ -1142,14 +956,13 @@
1142 956 $update = true;
1143 957
1144 958 global $frm_vars;
1145 959
1146 - // phpcs:ignore WordPress.PHP.StrictInArray.MissingTrueStrict
1147 - if ( isset( $frm_vars['saved_entries'] ) && is_array( $frm_vars['saved_entries'] ) && in_array( (int) $id, $frm_vars['saved_entries'] ) ) {
960 + if ( isset( $frm_vars['saved_entries'] ) && is_array( $frm_vars['saved_entries'] ) && in_array( (int) $id, (array) $frm_vars['saved_entries'] ) ) {
1148 961 $update = false;
1149 962 }
1150 963
1151 - if ( $update && $update_type !== 'xml' ) {
964 + if ( $update && $update_type != 'xml' ) {
1152 965 $values = apply_filters( 'frm_pre_update_entry', $values, $id );
1153 966 }
1154 967
1155 968 return $update;
@@ -1159,9 +972,9 @@
1159 972 * Package the entry data for updating
1160 973 *
1161 974 * @since 2.0.16
1162 975 *
1163 - * @param int $id
976 + * @param int $id
1164 977 * @param array $values
1165 978 *
1166 979 * @return array $new_values
1167 980 */
@@ -1172,9 +985,9 @@
1172 985 'name' => self::get_new_entry_name( $values ),
1173 986 'form_id' => (int) self::get_entry_value( $values, 'form_id', null ),
1174 987 'is_draft' => self::get_is_draft_value( $values ),
1175 988 'updated_at' => current_time( 'mysql', 1 ),
1176 - 'updated_by' => $values['updated_by'] ?? get_current_user_id(),
989 + 'updated_by' => isset( $values['updated_by'] ) ? $values['updated_by'] : get_current_user_id(),
1177 990 );
1178 991
1179 992 if ( isset( $values['post_id'] ) ) {
1180 993 $new_values['post_id'] = (int) $values['post_id'];
@@ -1191,9 +1004,11 @@
1191 1004 if ( isset( $values['frm_user_id'] ) && is_numeric( $values['frm_user_id'] ) ) {
1192 1005 $new_values['user_id'] = $values['frm_user_id'];
1193 1006 }
1194 1007
1195 - return apply_filters( 'frm_update_entry', $new_values, $id );
1008 + $new_values = apply_filters( 'frm_update_entry', $new_values, $id );
1009 +
1010 + return $new_values;
1196 1011 }
1197 1012
1198 1013 /**
1199 1014 * Perform some actions right after updating an entry
@@ -1199,14 +1014,12 @@
1199 1014 * Perform some actions right after updating an entry
1200 1015 *
1201 1016 * @since 2.0.16
1202 1017 *
1203 - * @param bool|int $query_results
1204 - * @param int|string $id
1205 - * @param array $values
1206 - * @param array $new_values
1207 - *
1208 - * @return void
1018 + * @param boolean|int $query_results
1019 + * @param int $id
1020 + * @param array $values
1021 + * @param array $new_values
1209 1022 */
1210 1023 private static function after_update_entry( $query_results, $id, $values, $new_values ) {
1211 1024 if ( $query_results ) {
1212 1025 self::clear_cache();
@@ -1212,9 +1025,8 @@
1212 1025 self::clear_cache();
1213 1026 }
1214 1027
1215 1028 global $frm_vars;
1216 -
1217 1029 if ( ! isset( $frm_vars['saved_entries'] ) ) {
1218 1030 $frm_vars['saved_entries'] = array();
1219 1031 }
1220 1032
@@ -1235,12 +1047,14 @@
1235 1047 * @since 2.0.16
1236 1048 *
1237 1049 * @param array $values
1238 1050 *
1239 - * @return bool|int $entry_id
1051 + * @return int | boolean $entry_id
1240 1052 */
1241 1053 public static function create_entry_from_xml( $values ) {
1242 - return self::create_entry( $values, 'xml' );
1054 + $entry_id = self::create_entry( $values, 'xml' );
1055 +
1056 + return $entry_id;
1243 1057 }
1244 1058
1245 1059 /**
1246 1060 * Update entry from an XML import
@@ -1247,15 +1061,17 @@
1247 1061 * Certain actions aren't necessary when importing (like saving sub entries and modifying other vals)
1248 1062 *
1249 1063 * @since 2.0.16
1250 1064 *
1251 - * @param int $id
1065 + * @param int $id
1252 1066 * @param array $values
1253 1067 *
1254 - * @return bool|int $updated
1068 + * @return int | boolean $updated
1255 1069 */
1256 1070 public static function update_entry_from_xml( $id, $values ) {
1257 - return self::update_entry( $id, $values, 'xml' );
1071 + $updated = self::update_entry( $id, $values, 'xml' );
1072 +
1073 + return $updated;
1258 1074 }
1259 1075
1260 1076 /**
1261 1077 * @param string $key
@@ -1263,23 +1079,8 @@
1263 1079 * @return int entry_id
1264 1080 */
1265 1081 public static function get_id_by_key( $key ) {
1266 1082 $entry_id = FrmDb::get_var( 'frm_items', array( 'item_key' => sanitize_title( $key ) ) );
1083 +
1267 1084 return (int) $entry_id;
1268 - }
1269 -
1270 - /**
1271 - * Get entries count.
1272 - *
1273 - * @since 6.8
1274 - *
1275 - * @return int|string
1276 - */
1277 - public static function get_entries_count() {
1278 - $args = array(
1279 - 'or' => 1,
1280 - 'parent_form_id' => null,
1281 - 'parent_form_id <' => 1,
1282 - );
1283 - return self::getRecordCount( $args );
1284 1085 }
1285 1086 }