PluginProbe
Formidable Forms – WordPress Form Builder for Contact Forms, Calculators, Quizzes & More / 6.7.2
Formidable Forms – WordPress Form Builder for Contact Forms, Calculators, Quizzes & More v6.7.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 +134 -326 6.276.7.2 View file →
@@ -5,23 +5,18 @@
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,44 +23,27 @@
28 23 *
29 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 ) {
@@ -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 /**
@@ -258,9 +156,9 @@
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;
@@ -319,12 +215,14 @@
319 215 *
320 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;
@@ -366,11 +262,11 @@
366 262 $id = (int) $id;
367 263
368 264 // Item meta is required for conditional logic in actions with 'delete' events.
369 265 $entry = self::getOne( $id, true );
370 -
371 266 if ( ! $entry ) {
372 - return false;
267 + $result = false;
268 + return $result;
373 269 }
374 270
375 271 /**
376 272 * Trigger an action to run custom logic before the entry is deleted.
@@ -398,20 +294,12 @@
398 294
399 295 return $result;
400 296 }
401 297
402 - /**
403 - * @param int $id
404 - * @param mixed $value
405 - * @param int|string $form_id
406 - *
407 - * @return false|int
408 - */
409 298 public static function update_form( $id, $value, $form_id ) {
410 299 global $wpdb;
411 300 $form_id = isset( $value ) ? $form_id : null;
412 301 $result = $wpdb->update( $wpdb->prefix . 'frm_items', array( 'form_id' => $form_id ), array( 'id' => $id ) );
413 -
414 302 if ( $result ) {
415 303 self::clear_cache();
416 304 }
417 305
@@ -422,10 +310,8 @@
422 310 * Clear entry caching
423 311 * Called when an entry is changed
424 312 *
425 313 * @since 2.0.5
426 - *
427 - * @return void
428 314 */
429 315 public static function clear_cache() {
430 316 FrmDb::cache_delete_group( 'frm_entry' );
431 317 FrmDb::cache_delete_group( 'frm_item' );
@@ -437,17 +323,11 @@
437 323 * After switching to the wp_loaded hook for processing entries,
438 324 * we can no longer use 'name', but check it as a fallback
439 325 *
440 326 * @since 2.0.11
441 - *
442 - * @param array $values
443 - * @param array|string $default
444 - *
445 - * @return string
446 327 */
447 328 public static function get_new_entry_name( $values, $default = '' ) {
448 - $name = $values['item_name'] ?? $values['name'] ?? $default;
449 -
329 + $name = isset( $values['item_name'] ) ? $values['item_name'] : ( isset( $values['name'] ) ? $values['name'] : $default );
450 330 if ( is_array( $name ) ) {
451 331 $name = reset( $name );
452 332 }
453 333
@@ -456,28 +336,20 @@
456 336
457 337 /**
458 338 * If $entry is numeric, get the entry object
459 339 *
460 - * @since 2.0.9
461 - *
462 340 * @param int|object $entry By reference.
463 341 *
464 - * @return void
342 + * @since 2.0.9
465 343 */
466 344 public static function maybe_get_entry( &$entry ) {
467 345 if ( $entry && is_numeric( $entry ) ) {
468 346 $entry = self::getOne( $entry );
469 - } elseif ( ! $entry || 'false' === $entry ) {
347 + } elseif ( empty( $entry ) || 'false' === $entry ) {
470 348 $entry = false;
471 349 }
472 350 }
473 351
474 - /**
475 - * @param int|string $id
476 - * @param bool $meta
477 - *
478 - * @return object|null
479 - */
480 352 public static function getOne( $id, $meta = false ) {
481 353 global $wpdb;
482 354
483 355 $query = "SELECT it.*, fr.name as form_name, fr.form_key as form_key FROM {$wpdb->prefix}frm_items it
@@ -482,9 +354,9 @@
482 354
483 355 $query = "SELECT it.*, fr.name as form_name, fr.form_key as form_key FROM {$wpdb->prefix}frm_items it
484 356 LEFT OUTER JOIN {$wpdb->prefix}frm_forms fr ON it.form_id=fr.id WHERE ";
485 357
486 - $query .= is_numeric( $id ) ? 'it.id=%d' : 'it.item_key=%s';
358 + $query .= is_numeric( $id ) ? 'it.id=%d' : 'it.item_key=%s';
487 359 $query_args = array( $id );
488 360 $query = $wpdb->prepare( $query, $query_args ); // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared
489 361
490 362 if ( ! $meta ) {
@@ -493,9 +365,8 @@
493 365 return $entry;
494 366 }
495 367
496 368 $entry = FrmDb::check_cache( $id, 'frm_entry' );
497 -
498 369 if ( $entry !== false ) {
499 370 self::prepare_entry( $entry );
500 371 return $entry;
501 372 }
@@ -510,13 +381,11 @@
510 381 /**
511 382 * @since 4.02.03
512 383 *
513 384 * @param object $entry
514 - *
515 - * @return void
516 385 */
517 386 private static function prepare_entry( &$entry ) {
518 - if ( ! $entry ) {
387 + if ( empty( $entry ) ) {
519 388 return;
520 389 }
521 390
522 391 FrmAppHelper::unserialize_or_decode( $entry->description );
@@ -527,10 +396,8 @@
527 396 /**
528 397 * @since 4.02.03
529 398 *
530 399 * @param array $entries
531 - *
532 - * @return void
533 400 */
534 401 private static function prepare_entries( &$entries ) {
535 402 foreach ( $entries as $k => $entry ) {
536 403 self::prepare_entry( $entry );
@@ -537,13 +404,8 @@
537 404 $entries[ $k ] = $entry;
538 405 }
539 406 }
540 407
541 - /**
542 - * @param object|null $entry
543 - *
544 - * @return object|null
545 - */
546 408 public static function get_meta( $entry ) {
547 409 if ( ! $entry ) {
548 410 return $entry;
549 411 }
@@ -560,15 +422,13 @@
560 422
561 423 $entry->metas = array();
562 424
563 425 $include_key = apply_filters( 'frm_include_meta_keys', false, array( 'form_id' => $entry->form_id ) );
564 -
565 426 foreach ( $metas as $meta_val ) {
566 427 FrmFieldsHelper::prepare_field_value( $meta_val->meta_value, $meta_val->type );
567 428
568 - if ( (int) $meta_val->item_id === (int) $entry->id ) {
429 + if ( $meta_val->item_id == $entry->id ) {
569 430 $entry->metas[ $meta_val->field_id ] = $meta_val->meta_value;
570 -
571 431 if ( $include_key ) {
572 432 $entry->metas[ $meta_val->field_key ] = $entry->metas[ $meta_val->field_id ];
573 433 }
574 434 continue;
@@ -581,9 +441,9 @@
581 441
582 442 $entry->metas[ $meta_val->field_id ][] = $meta_val->meta_value;
583 443
584 444 unset( $meta_val );
585 - }//end foreach
445 + }
586 446 unset( $metas );
587 447
588 448 FrmDb::set_cache( $entry->id, $entry, 'frm_entry' );
589 449
@@ -591,47 +451,43 @@
591 451 }
592 452
593 453 /**
594 454 * @param string $id
595 - *
596 - * @return bool
597 455 */
598 456 public static function exists( $id ) {
599 457 global $wpdb;
600 458
601 459 if ( FrmDb::check_cache( $id, 'frm_entry' ) ) {
602 - return true;
460 + $exists = true;
461 +
462 + return $exists;
603 463 }
604 464
605 - $where = is_numeric( $id ) ? array( 'id' => $id ) : array( 'item_key' => $id );
606 - $id = FrmDb::get_var( $wpdb->prefix . 'frm_items', $where );
465 + if ( is_numeric( $id ) ) {
466 + $where = array( 'id' => $id );
467 + } else {
468 + $where = array( 'item_key' => $id );
469 + }
470 + $id = FrmDb::get_var( $wpdb->prefix . 'frm_items', $where );
607 471
608 - return $id && $id > 0;
472 + return ( $id && $id > 0 );
609 473 }
610 474
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 475 public static function getAll( $where, $order_by = '', $limit = '', $meta = false, $inc_form = true ) {
621 476 global $wpdb;
622 477
623 - $limit = FrmDb::esc_limit( $limit );
478 + $limit = FrmDb::esc_limit( $limit );
479 +
624 480 $cache_key = FrmAppHelper::maybe_json_encode( $where ) . $order_by . $limit . $inc_form;
625 481 $entries = wp_cache_get( $cache_key, 'frm_entry' );
626 482
627 483 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
484 + $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 485 $table = $wpdb->prefix . 'frm_items it ';
630 486
631 487 if ( $inc_form ) {
632 488 $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 ';
489 + $table .= 'LEFT OUTER JOIN ' . $wpdb->prefix . 'frm_forms fr ON it.form_id=fr.id ';
634 490 }
635 491
636 492 if ( preg_match( '/ meta_([0-9]+)/', $order_by, $order_matches ) ) {
637 493 $fields .= self::sort_by_field( $order_matches[1] );
@@ -657,11 +513,9 @@
657 513 $where = array( 'it.form_id' => substr( $where, 11 ) );
658 514 }
659 515
660 516 $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'] ) ) {
517 + if ( $limit == '' && is_array( $where ) && count( $where ) == 1 && isset( $where['it.form_id'] ) ) {
664 518 $meta_where['fi.form_id'] = $where['it.form_id'];
665 519 } else {
666 520 $meta_where['item_id'] = array_keys( $entries );
667 521 }
@@ -704,15 +558,15 @@
704 558 return $entries;
705 559 }
706 560
707 561 /**
708 - * @param int|string $field_id
709 - *
562 + * @param int $field_id
710 563 * @return string
711 564 */
712 565 private static function sort_by_field( $field_id ) {
713 566 global $wpdb;
714 - $field_id = (int) $field_id;
567 + $field_id = (int) $field_id;
568 +
715 569 $field_options = FrmDb::get_var( 'frm_fields', array( 'id' => $field_id ), 'field_options' );
716 570 FrmAppHelper::unserialize_or_decode( $field_options );
717 571
718 572 if ( empty( $field_options['post_field'] ) ) {
@@ -725,11 +579,9 @@
725 579 }
726 580
727 581 // Pagination Methods
728 582 /**
729 - * @param array|int|string $where If int, use the form id.
730 - *
731 - * @return int|string
583 + * @param int|array|string $where If int, use the form id.
732 584 */
733 585 public static function getRecordCount( $where = '' ) {
734 586 global $wpdb;
735 587 $table_join = $wpdb->prefix . 'frm_items it JOIN ' . $wpdb->prefix . 'frm_forms fr ON it.form_id=fr.id';
@@ -739,32 +591,25 @@
739 591 $where = array( 'form_id' => $where );
740 592 }
741 593
742 594 if ( is_array( $where ) ) {
743 - return FrmDb::get_count( $table_join, $where );
595 + $count = FrmDb::get_count( $table_join, $where );
596 + } else {
597 + $cache_key = 'count_' . FrmAppHelper::maybe_json_encode( $where );
598 + $query = 'SELECT COUNT(*) FROM ' . $table_join . FrmDb::prepend_and_or_where( ' WHERE ', $where );
599 + $count = FrmDb::check_cache( $cache_key, 'frm_entry', $query, 'get_var' );
744 600 }
745 601
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' );
602 + return $count;
750 603 }
751 604
752 - /**
753 - * @param int|string $p_size
754 - * @param array|int|string $where
755 - *
756 - * @return int
757 - */
758 605 public static function getPageCount( $p_size, $where = '' ) {
759 606 $p_size = (int) $p_size;
760 607 $count = 1;
761 -
762 608 if ( $p_size ) {
763 609 if ( ! is_numeric( $where ) ) {
764 610 $where = self::getRecordCount( $where );
765 611 }
766 -
767 612 $count = ceil( (int) $where / $p_size );
768 613 }
769 614
770 615 return $count;
@@ -780,15 +625,18 @@
780 625 *
781 626 * @return array $new_values
782 627 */
783 628 private static function before_insert_entry_in_database( &$values, $type ) {
629 +
784 630 self::sanitize_entry_post( $values );
785 631
786 - if ( $type !== 'xml' ) {
632 + if ( $type != 'xml' ) {
787 633 $values = apply_filters( 'frm_pre_create_entry', $values );
788 634 }
789 635
790 - return self::package_entry_data( $values );
636 + $new_values = self::package_entry_data( $values );
637 +
638 + return $new_values;
791 639 }
792 640
793 641 /**
794 642 * Create an entry and perform after create actions
@@ -797,13 +645,12 @@
797 645 *
798 646 * @param array $values
799 647 * @param array $new_values
800 648 *
801 - * @return bool|int $entry_id
649 + * @return boolean|int $entry_id
802 650 */
803 651 private static function continue_to_create_entry( $values, $new_values ) {
804 652 $entry_id = self::insert_entry_into_database( $new_values );
805 -
806 653 if ( ! $entry_id ) {
807 654 return false;
808 655 }
809 656
@@ -817,10 +664,8 @@
817 664 *
818 665 * @since 2.0
819 666 *
820 667 * @param array $values The POST values by reference.
821 - *
822 - * @return void
823 668 */
824 669 public static function sanitize_entry_post( &$values ) {
825 670 $sanitize_method = array(
826 671 'form_id' => 'absint',
@@ -869,22 +714,15 @@
869 714 'description' => self::get_entry_description( $values ),
870 715 'user_id' => self::get_entry_user_id( $values ),
871 716 );
872 717
873 - $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'];
874 719
875 720 return $new_values;
876 721 }
877 722
878 - /**
879 - * @param array $values
880 - * @param string $name
881 - * @param mixed $default
882 - *
883 - * @return mixed
884 - */
885 723 private static function get_entry_value( $values, $name, $default ) {
886 - return $values[ $name ] ?? $default;
724 + return isset( $values[ $name ] ) ? $values[ $name ] : $default;
887 725 }
888 726
889 727 /**
890 728 * Get the ip for a new entry.
@@ -901,9 +739,8 @@
901 739 return '';
902 740 }
903 741
904 742 $ip = FrmAppHelper::get_ip_address();
905 -
906 743 if ( defined( 'WP_IMPORTING' ) && WP_IMPORTING ) {
907 744 $ip = self::get_entry_value( $values, 'ip', $ip );
908 745 }
909 746
@@ -949,9 +786,15 @@
949 786 *
950 787 * @return string
951 788 */
952 789 private static function get_updated_at( $values ) {
953 - return $values['updated_at'] ?? self::get_created_at( $values );
790 + if ( isset( $values['updated_at'] ) ) {
791 + $updated_at = $values['updated_at'];
792 + } else {
793 + $updated_at = self::get_created_at( $values );
794 + }
795 +
796 + return $updated_at;
954 797 }
955 798
956 799 /**
957 800 * Get the description value for a new entry
@@ -962,18 +805,20 @@
962 805 *
963 806 * @return string
964 807 */
965 808 private static function get_entry_description( $values ) {
966 - if ( ! empty( $values['description'] ) ) {
967 - return FrmAppHelper::maybe_json_encode( $values['description'] );
809 + if ( isset( $values['description'] ) && ! empty( $values['description'] ) ) {
810 + $description = FrmAppHelper::maybe_json_encode( $values['description'] );
811 + } else {
812 + $description = json_encode(
813 + array(
814 + 'browser' => FrmAppHelper::get_server_value( 'HTTP_USER_AGENT' ),
815 + 'referrer' => FrmAppHelper::get_server_value( 'HTTP_REFERER' ),
816 + )
817 + );
968 818 }
969 819
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 - );
820 + return $description;
976 821 }
977 822
978 823 /**
979 824 * Get the user_id value for a new entry
@@ -985,13 +830,15 @@
985 830 * @return int
986 831 */
987 832 private static function get_entry_user_id( $values ) {
988 833 if ( isset( $values['frm_user_id'] ) && ( is_numeric( $values['frm_user_id'] ) || FrmAppHelper::is_admin() ) ) {
989 - return $values['frm_user_id'];
834 + $user_id = $values['frm_user_id'];
835 + } else {
836 + $current_user_id = get_current_user_id();
837 + $user_id = $current_user_id ? $current_user_id : 0;
990 838 }
991 839
992 - $current_user_id = get_current_user_id();
993 - return $current_user_id ? $current_user_id : 0;
840 + return $user_id;
994 841 }
995 842
996 843 /**
997 844 * Insert new entry into the database
@@ -999,9 +846,9 @@
999 846 * @since 2.0.16
1000 847 *
1001 848 * @param array $new_values
1002 849 *
1003 - * @return bool|int $entry_id
850 + * @return int | boolean $entry_id
1004 851 */
1005 852 private static function insert_entry_into_database( $new_values ) {
1006 853 global $wpdb;
1007 854
@@ -1006,9 +853,15 @@
1006 853 global $wpdb;
1007 854
1008 855 $query_results = $wpdb->insert( $wpdb->prefix . 'frm_items', $new_values );
1009 856
1010 - return ! $query_results ? false : $wpdb->insert_id;
857 + if ( ! $query_results ) {
858 + $entry_id = false;
859 + } else {
860 + $entry_id = $wpdb->insert_id;
861 + }
862 +
863 + return $entry_id;
1011 864 }
1012 865
1013 866 /**
1014 867 * Add the new entry to global $frm_vars
@@ -1014,11 +867,9 @@
1014 867 * Add the new entry to global $frm_vars
1015 868 *
1016 869 * @since 2.0.16
1017 870 *
1018 - * @param int|string $entry_id
1019 - *
1020 - * @return void
871 + * @param int $entry_id
1021 872 */
1022 873 private static function add_new_entry_to_frm_vars( $entry_id ) {
1023 874 global $frm_vars;
1024 875
@@ -1033,55 +884,28 @@
1033 884 * Add entry metas, if there are any
1034 885 *
1035 886 * @since 2.0.16
1036 887 *
1037 - * @param array $values
1038 - * @param int|string $entry_id
1039 - *
888 + * @param array $values
889 + * @param int $entry_id
1040 890 * @return void
1041 891 */
1042 892 private static function maybe_add_entry_metas( $values, $entry_id ) {
1043 893 if ( isset( $values['item_meta'] ) ) {
1044 894 FrmEntryMeta::update_entry_metas( $entry_id, $values['item_meta'] );
1045 - self::maybe_add_unique_id_meta( $values, $entry_id );
1046 895 }
1047 896 self::maybe_add_captcha_meta( (int) $values['form_id'], (int) $entry_id );
1048 897 }
1049 898
1050 899 /**
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 900 * @since 5.0.15
1075 901 *
1076 902 * @param int $form_id
1077 903 * @param int $entry_id
1078 - *
1079 904 * @return void
1080 905 */
1081 906 private static function maybe_add_captcha_meta( $form_id, $entry_id ) {
1082 907 global $frm_vars;
1083 -
1084 908 if ( array_key_exists( 'captcha_scores', $frm_vars ) && array_key_exists( $form_id, $frm_vars['captcha_scores'] ) ) {
1085 909 $captcha_score_meta = array( 'captcha_score' => $frm_vars['captcha_scores'][ $form_id ] );
1086 910 FrmEntryMeta::add_entry_meta( $entry_id, 0, '', maybe_serialize( $captcha_score_meta ) );
1087 911 }
@@ -1094,14 +918,12 @@
1094 918 *
1095 919 * @param int $entry_id
1096 920 * @param array $values
1097 921 * @param array $new_values
1098 - *
1099 - * @return void
1100 922 */
1101 923 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' );
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' );
1104 926
1105 927 do_action( 'frm_after_create_entry', $entry_id, $new_values['form_id'], compact( 'is_child' ) );
1106 928 do_action( 'frm_after_create_entry_' . $new_values['form_id'], $entry_id, compact( 'is_child' ) );
1107 929 }
@@ -1113,12 +935,11 @@
1113 935 *
1114 936 * @param array $values
1115 937 * @param array $new_values
1116 938 * @param int $entry_id
1117 - *
1118 - * @return void
1119 939 */
1120 940 private static function after_insert_entry_in_database( $values, $new_values, $entry_id ) {
941 +
1121 942 self::add_new_entry_to_frm_vars( $entry_id );
1122 943
1123 944 self::maybe_add_entry_metas( $values, $entry_id );
1124 945
@@ -1131,13 +952,13 @@
1131 952 * Perform some actions right before updating an entry
1132 953 *
1133 954 * @since 2.0.16
1134 955 *
1135 - * @param int|string $id
1136 - * @param array $values
1137 - * @param string $update_type
956 + * @param int $id
957 + * @param array $values
958 + * @param string $update_type
1138 959 *
1139 - * @return bool $update
960 + * @return boolean $update
1140 961 */
1141 962 private static function before_update_entry( $id, &$values, $update_type ) {
1142 963 $update = true;
1143 964
@@ -1142,14 +963,13 @@
1142 963 $update = true;
1143 964
1144 965 global $frm_vars;
1145 966
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'] ) ) {
967 + if ( isset( $frm_vars['saved_entries'] ) && is_array( $frm_vars['saved_entries'] ) && in_array( (int) $id, (array) $frm_vars['saved_entries'] ) ) {
1148 968 $update = false;
1149 969 }
1150 970
1151 - if ( $update && $update_type !== 'xml' ) {
971 + if ( $update && $update_type != 'xml' ) {
1152 972 $values = apply_filters( 'frm_pre_update_entry', $values, $id );
1153 973 }
1154 974
1155 975 return $update;
@@ -1172,9 +992,9 @@
1172 992 'name' => self::get_new_entry_name( $values ),
1173 993 'form_id' => (int) self::get_entry_value( $values, 'form_id', null ),
1174 994 'is_draft' => self::get_is_draft_value( $values ),
1175 995 'updated_at' => current_time( 'mysql', 1 ),
1176 - 'updated_by' => $values['updated_by'] ?? get_current_user_id(),
996 + 'updated_by' => isset( $values['updated_by'] ) ? $values['updated_by'] : get_current_user_id(),
1177 997 );
1178 998
1179 999 if ( isset( $values['post_id'] ) ) {
1180 1000 $new_values['post_id'] = (int) $values['post_id'];
@@ -1191,9 +1011,11 @@
1191 1011 if ( isset( $values['frm_user_id'] ) && is_numeric( $values['frm_user_id'] ) ) {
1192 1012 $new_values['user_id'] = $values['frm_user_id'];
1193 1013 }
1194 1014
1195 - return apply_filters( 'frm_update_entry', $new_values, $id );
1015 + $new_values = apply_filters( 'frm_update_entry', $new_values, $id );
1016 +
1017 + return $new_values;
1196 1018 }
1197 1019
1198 1020 /**
1199 1021 * Perform some actions right after updating an entry
@@ -1199,14 +1021,12 @@
1199 1021 * Perform some actions right after updating an entry
1200 1022 *
1201 1023 * @since 2.0.16
1202 1024 *
1203 - * @param bool|int $query_results
1204 - * @param int|string $id
1205 - * @param array $values
1206 - * @param array $new_values
1207 - *
1208 - * @return void
1025 + * @param boolean|int $query_results
1026 + * @param int $id
1027 + * @param array $values
1028 + * @param array $new_values
1209 1029 */
1210 1030 private static function after_update_entry( $query_results, $id, $values, $new_values ) {
1211 1031 if ( $query_results ) {
1212 1032 self::clear_cache();
@@ -1212,9 +1032,8 @@
1212 1032 self::clear_cache();
1213 1033 }
1214 1034
1215 1035 global $frm_vars;
1216 -
1217 1036 if ( ! isset( $frm_vars['saved_entries'] ) ) {
1218 1037 $frm_vars['saved_entries'] = array();
1219 1038 }
1220 1039
@@ -1235,12 +1054,14 @@
1235 1054 * @since 2.0.16
1236 1055 *
1237 1056 * @param array $values
1238 1057 *
1239 - * @return bool|int $entry_id
1058 + * @return int | boolean $entry_id
1240 1059 */
1241 1060 public static function create_entry_from_xml( $values ) {
1242 - return self::create_entry( $values, 'xml' );
1061 + $entry_id = self::create_entry( $values, 'xml' );
1062 +
1063 + return $entry_id;
1243 1064 }
1244 1065
1245 1066 /**
1246 1067 * Update entry from an XML import
@@ -1250,12 +1071,14 @@
1250 1071 *
1251 1072 * @param int $id
1252 1073 * @param array $values
1253 1074 *
1254 - * @return bool|int $updated
1075 + * @return int | boolean $updated
1255 1076 */
1256 1077 public static function update_entry_from_xml( $id, $values ) {
1257 - return self::update_entry( $id, $values, 'xml' );
1078 + $updated = self::update_entry( $id, $values, 'xml' );
1079 +
1080 + return $updated;
1258 1081 }
1259 1082
1260 1083 /**
1261 1084 * @param string $key
@@ -1263,23 +1086,8 @@
1263 1086 * @return int entry_id
1264 1087 */
1265 1088 public static function get_id_by_key( $key ) {
1266 1089 $entry_id = FrmDb::get_var( 'frm_items', array( 'item_key' => sanitize_title( $key ) ) );
1090 +
1267 1091 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 1092 }
1285 1093 }