PluginProbe
Formidable Forms – WordPress Form Builder for Contact Forms, Calculators, Quizzes & More / 5.5
Formidable Forms – WordPress Form Builder for Contact Forms, Calculators, Quizzes & More v5.5
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 +66 -313 6.265.5 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
@@ -27,18 +20,18 @@
27 20
28 21 /**
29 22 * Create a new entry with some differences depending on type
30 23 *
31 - * @param array $values
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,30 +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 - *
57 - * @return void
58 - */
59 - private static function flag_new_unique_key( $unique_id ) {
60 - if ( ! isset( self::$unique_id_match_checks[ $unique_id ] ) ) {
61 - self::$unique_id_match_checks[ $unique_id ] = false;
62 - }
63 - }
64 -
65 - /**
66 43 * Check for duplicate entries created in the last minute
67 44 *
68 - * @param array $new_values New values.
69 - * @param array $values Values.
70 - *
71 - * @return bool
45 + * @return boolean
72 46 */
73 47 public static function is_duplicate( $new_values, $values ) {
74 48 $duplicate_entry_time = apply_filters( 'frm_time_to_check_duplicates', 60, $new_values );
75 49
@@ -76,14 +50,10 @@
76 50 if ( false === self::is_duplicate_check_needed( $values, $duplicate_entry_time ) ) {
77 51 return false;
78 52 }
79 53
80 - if ( self::maybe_check_for_unique_id_match( $values, $new_values['created_at'] ) ) {
81 - return true;
82 - }
83 -
84 54 $check_val = $new_values;
85 - $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 ) ) );
86 56
87 57 unset( $check_val['created_at'], $check_val['updated_at'], $check_val['is_draft'], $check_val['id'], $check_val['item_key'] );
88 58
89 59 if ( $new_values['item_key'] == $new_values['name'] ) {
@@ -102,9 +72,8 @@
102 72 global $frm_vars;
103 73 $frm_vars['checking_duplicates'] = true;
104 74
105 75 $is_duplicate = false;
106 -
107 76 foreach ( $entry_exists as $entry_exist ) {
108 77 $is_duplicate = true;
109 78
110 79 // make sure it's a duplicate
@@ -109,13 +78,9 @@
109 78
110 79 // make sure it's a duplicate
111 80 $metas = FrmEntryMeta::get_entry_meta_info( $entry_exist );
112 81 $field_metas = array();
113 -
114 82 foreach ( $metas as $meta ) {
115 - if ( 0 === (int) $meta->field_id ) {
116 - continue;
117 - }
118 83 $field_metas[ $meta->field_id ] = $meta->meta_value;
119 84 }
120 85
121 86 $filtered_vals = array_filter( $values['item_meta'] );
@@ -141,12 +106,12 @@
141 106 continue;
142 107 }
143 108
144 109 $diff = array_diff_assoc( $field_metas, $new_meta );
145 -
146 - foreach ( $diff as $meta_value ) {
110 + foreach ( $diff as $field_id => $meta_value ) {
147 111 if ( ! empty( $meta_value ) ) {
148 112 $is_duplicate = false;
113 + continue;
149 114 }
150 115 }
151 116
152 117 if ( $is_duplicate ) {
@@ -151,9 +116,9 @@
151 116
152 117 if ( $is_duplicate ) {
153 118 break;
154 119 }
155 - }//end foreach
120 + }
156 121
157 122 $frm_vars['checking_duplicates'] = false;
158 123
159 124 return $is_duplicate;
@@ -159,88 +124,21 @@
159 124 return $is_duplicate;
160 125 }
161 126
162 127 /**
163 - * @since 6.16.3
164 - *
165 - * @param array $values POST request data.
166 - * @param string $created_at The timestamp of the entry we are checking for.
167 - *
168 - * @return bool
169 - */
170 - private static function maybe_check_for_unique_id_match( $values, $created_at ) {
171 - if ( ! self::should_check_for_unique_id_match() ) {
172 - return false;
173 - }
174 -
175 - if ( empty( $values['unique_id'] ) ) {
176 - return false;
177 - }
178 -
179 - $unique_id = sanitize_key( $values['unique_id'] );
180 -
181 - if ( ! $unique_id ) {
182 - // Only continue if a unique ID was generated on form submit.
183 - return false;
184 - }
185 -
186 - if ( isset( self::$unique_id_match_checks[ $unique_id ] ) ) {
187 - return self::$unique_id_match_checks[ $unique_id ];
188 - }
189 -
190 - $timestamp = strtotime( $created_at );
191 -
192 - if ( false === $timestamp ) {
193 - $timestamp = time();
194 - }
195 -
196 - self::$unique_id_match_checks[ $unique_id ] = (bool) FrmDb::get_var(
197 - 'frm_item_metas',
198 - array(
199 - 'field_id' => 0,
200 - 'meta_value' => serialize( compact( 'unique_id' ) ),
201 - 'created_at >' => gmdate( 'Y-m-d H:i:s', $timestamp - MONTH_IN_SECONDS ),
202 - ),
203 - 'id'
204 - );
205 -
206 - return self::$unique_id_match_checks[ $unique_id ];
207 - }
208 -
209 - /**
210 - * @since 6.16.3
211 - *
212 - * @return bool
213 - */
214 - private static function should_check_for_unique_id_match() {
215 - /**
216 - * Allow users to opt out of the DB query, in case it causes performance issues.
217 - *
218 - * @since 6.16.3
219 - *
220 - * @param bool $should_extend
221 - */
222 - $should_check = apply_filters( 'frm_check_for_unique_id_match', true );
223 - return (bool) $should_check;
224 - }
225 -
226 - /**
227 128 * Convert form data to the actual value that would be saved into the database.
228 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.
229 130 *
230 131 * @param array $filter_vals
231 132 * @param int $entry_id
232 - *
233 133 * @return array
234 134 */
235 135 private static function convert_values_to_their_saved_value( $filter_vals, $entry_id ) {
236 136 $reduced = array();
237 -
238 137 foreach ( $filter_vals as $field_id => $value ) {
239 138 $field = FrmFieldFactory::get_field_object( $field_id );
240 139 $reduced[ $field_id ] = $field->get_value_to_save( $value, array( 'entry_id' => $entry_id ) );
241 140 $reduced[ $field_id ] = $field->set_value_before_save( $reduced[ $field_id ] );
242 -
243 141 if ( '' === $reduced[ $field_id ] || ( is_array( $reduced[ $field_id ] ) && 0 === count( $reduced[ $field_id ] ) ) ) {
244 142 unset( $reduced[ $field_id ] );
245 143 }
246 144 }
@@ -252,9 +150,9 @@
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 ) {
@@ -275,13 +173,8 @@
275 173
276 174 return true;
277 175 }
278 176
279 - /**
280 - * @param int|string $id
281 - *
282 - * @return false|int
283 - */
284 177 public static function duplicate( $id ) {
285 178 global $wpdb;
286 179
287 180 $values = self::getOne( $id );
@@ -296,9 +189,8 @@
296 189 $new_values['created_at'] = current_time( 'mysql', 1 );
297 190 $new_values['updated_at'] = $new_values['created_at'];
298 191
299 192 $query_results = $wpdb->insert( $wpdb->prefix . 'frm_items', $new_values );
300 -
301 193 if ( ! $query_results ) {
302 194 return false;
303 195 }
304 196
@@ -304,9 +196,8 @@
304 196
305 197 $entry_id = $wpdb->insert_id;
306 198
307 199 global $frm_vars;
308 -
309 200 if ( ! isset( $frm_vars['saved_entries'] ) ) {
310 201 $frm_vars['saved_entries'] = array();
311 202 }
312 203 $frm_vars['saved_entries'][] = (int) $entry_id;
@@ -321,12 +212,12 @@
321 212
322 213 /**
323 214 * Update an entry (not via XML)
324 215 *
325 - * @param int $id
216 + * @param int $id
326 217 * @param array $values
327 218 *
328 - * @return bool|int $update_results
219 + * @return boolean|int $update_results
329 220 */
330 221 public static function update( $id, $values ) {
331 222 $update_results = self::update_entry( $id, $values, 'standard' );
332 223
@@ -337,19 +228,17 @@
337 228 * Update an entry with some differences depending on the update type
338 229 *
339 230 * @since 2.0.16
340 231 *
341 - * @param int $id
342 - * @param array $values
343 - * @param string $update_type
232 + * @param int $id
233 + * @param array $values
344 234 *
345 - * @return bool|int $query_results
235 + * @return boolean|int $query_results
346 236 */
347 237 private static function update_entry( $id, $values, $update_type ) {
348 238 global $wpdb;
349 239
350 240 $update = self::before_update_entry( $id, $values, $update_type );
351 -
352 241 if ( ! $update ) {
353 242 return false;
354 243 }
355 244
@@ -364,10 +253,9 @@
364 253
365 254 /**
366 255 * Delete an entry.
367 256 *
368 - * @param int|string $id
369 - *
257 + * @param string|int $id
370 258 * @return bool True on success, false if nothing was deleted.
371 259 */
372 260 public static function destroy( $id ) {
373 261 global $wpdb;
@@ -372,11 +260,9 @@
372 260 public static function destroy( $id ) {
373 261 global $wpdb;
374 262 $id = (int) $id;
375 263
376 - // Item meta is required for conditional logic in actions with 'delete' events.
377 - $entry = self::getOne( $id, true );
378 -
264 + $entry = self::getOne( $id, true ); // Item meta is required for conditional logic in actions with 'delete' events.
379 265 if ( ! $entry ) {
380 266 $result = false;
381 267 return $result;
382 268 }
@@ -407,20 +293,12 @@
407 293
408 294 return $result;
409 295 }
410 296
411 - /**
412 - * @param int $id
413 - * @param mixed $value
414 - * @param int|string $form_id
415 - *
416 - * @return false|int
417 - */
418 297 public static function update_form( $id, $value, $form_id ) {
419 298 global $wpdb;
420 299 $form_id = isset( $value ) ? $form_id : null;
421 300 $result = $wpdb->update( $wpdb->prefix . 'frm_items', array( 'form_id' => $form_id ), array( 'id' => $id ) );
422 -
423 301 if ( $result ) {
424 302 self::clear_cache();
425 303 }
426 304
@@ -431,10 +309,8 @@
431 309 * Clear entry caching
432 310 * Called when an entry is changed
433 311 *
434 312 * @since 2.0.5
435 - *
436 - * @return void
437 313 */
438 314 public static function clear_cache() {
439 315 FrmDb::cache_delete_group( 'frm_entry' );
440 316 FrmDb::cache_delete_group( 'frm_item' );
@@ -446,17 +322,11 @@
446 322 * After switching to the wp_loaded hook for processing entries,
447 323 * we can no longer use 'name', but check it as a fallback
448 324 *
449 325 * @since 2.0.11
450 - *
451 - * @param array $values
452 - * @param array|string $default
453 - *
454 - * @return string
455 326 */
456 327 public static function get_new_entry_name( $values, $default = '' ) {
457 - $name = $values['item_name'] ?? $values['name'] ?? $default;
458 -
328 + $name = isset( $values['item_name'] ) ? $values['item_name'] : ( isset( $values['name'] ) ? $values['name'] : $default );
459 329 if ( is_array( $name ) ) {
460 330 $name = reset( $name );
461 331 }
462 332
@@ -465,13 +335,11 @@
465 335
466 336 /**
467 337 * If $entry is numeric, get the entry object
468 338 *
339 + * @param int|object $entry by reference
340 + *
469 341 * @since 2.0.9
470 - *
471 - * @param int|object $entry By reference.
472 - *
473 - * @return void
474 342 */
475 343 public static function maybe_get_entry( &$entry ) {
476 344 if ( $entry && is_numeric( $entry ) ) {
477 345 $entry = self::getOne( $entry );
@@ -479,14 +347,8 @@
479 347 $entry = false;
480 348 }
481 349 }
482 350
483 - /**
484 - * @param int|string $id
485 - * @param bool $meta
486 - *
487 - * @return object|null
488 - */
489 351 public static function getOne( $id, $meta = false ) {
490 352 global $wpdb;
491 353
492 354 $query = "SELECT it.*, fr.name as form_name, fr.form_key as form_key FROM {$wpdb->prefix}frm_items it
@@ -491,9 +353,9 @@
491 353
492 354 $query = "SELECT it.*, fr.name as form_name, fr.form_key as form_key FROM {$wpdb->prefix}frm_items it
493 355 LEFT OUTER JOIN {$wpdb->prefix}frm_forms fr ON it.form_id=fr.id WHERE ";
494 356
495 - $query .= is_numeric( $id ) ? 'it.id=%d' : 'it.item_key=%s';
357 + $query .= is_numeric( $id ) ? 'it.id=%d' : 'it.item_key=%s';
496 358 $query_args = array( $id );
497 359 $query = $wpdb->prepare( $query, $query_args ); // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared
498 360
499 361 if ( ! $meta ) {
@@ -502,9 +364,8 @@
502 364 return $entry;
503 365 }
504 366
505 367 $entry = FrmDb::check_cache( $id, 'frm_entry' );
506 -
507 368 if ( $entry !== false ) {
508 369 self::prepare_entry( $entry );
509 370 return $entry;
510 371 }
@@ -519,10 +380,8 @@
519 380 /**
520 381 * @since 4.02.03
521 382 *
522 383 * @param object $entry
523 - *
524 - * @return void
525 384 */
526 385 private static function prepare_entry( &$entry ) {
527 386 if ( empty( $entry ) ) {
528 387 return;
@@ -528,10 +387,9 @@
528 387 return;
529 388 }
530 389
531 390 FrmAppHelper::unserialize_or_decode( $entry->description );
532 - // TODO: Remove slashes on input only, not output.
533 - $entry = wp_unslash( $entry );
391 + $entry = wp_unslash( $entry ); // TODO: Remove slashes on input only, not output.
534 392 }
535 393
536 394 /**
537 395 * @since 4.02.03
@@ -536,10 +394,8 @@
536 394 /**
537 395 * @since 4.02.03
538 396 *
539 397 * @param array $entries
540 - *
541 - * @return void
542 398 */
543 399 private static function prepare_entries( &$entries ) {
544 400 foreach ( $entries as $k => $entry ) {
545 401 self::prepare_entry( $entry );
@@ -546,13 +402,8 @@
546 402 $entries[ $k ] = $entry;
547 403 }
548 404 }
549 405
550 - /**
551 - * @param object|null $entry
552 - *
553 - * @return object|null
554 - */
555 406 public static function get_meta( $entry ) {
556 407 if ( ! $entry ) {
557 408 return $entry;
558 409 }
@@ -563,21 +414,18 @@
563 414 array(
564 415 'item_id' => $entry->id,
565 416 'field_id !' => 0,
566 417 ),
567 - 'field_id, meta_value, field_key, item_id, f.type'
418 + 'field_id, meta_value, field_key, item_id'
568 419 );
569 420
570 421 $entry->metas = array();
571 422
572 423 $include_key = apply_filters( 'frm_include_meta_keys', false, array( 'form_id' => $entry->form_id ) );
573 -
574 424 foreach ( $metas as $meta_val ) {
575 - FrmFieldsHelper::prepare_field_value( $meta_val->meta_value, $meta_val->type );
576 -
577 425 if ( $meta_val->item_id == $entry->id ) {
426 + FrmAppHelper::unserialize_or_decode( $meta_val->meta_value );
578 427 $entry->metas[ $meta_val->field_id ] = $meta_val->meta_value;
579 -
580 428 if ( $include_key ) {
581 429 $entry->metas[ $meta_val->field_key ] = $entry->metas[ $meta_val->field_id ];
582 430 }
583 431 continue;
@@ -587,12 +435,13 @@
587 435 if ( ! isset( $entry->metas[ $meta_val->field_id ] ) ) {
588 436 $entry->metas[ $meta_val->field_id ] = array();
589 437 }
590 438
439 + FrmAppHelper::unserialize_or_decode( $meta_val->meta_value );
591 440 $entry->metas[ $meta_val->field_id ][] = $meta_val->meta_value;
592 441
593 442 unset( $meta_val );
594 - }//end foreach
443 + }
595 444 unset( $metas );
596 445
597 446 FrmDb::set_cache( $entry->id, $entry, 'frm_entry' );
598 447
@@ -600,10 +449,8 @@
600 449 }
601 450
602 451 /**
603 452 * @param string $id
604 - *
605 - * @return bool
606 453 */
607 454 public static function exists( $id ) {
608 455 global $wpdb;
609 456
@@ -617,23 +464,13 @@
617 464 $where = array( 'id' => $id );
618 465 } else {
619 466 $where = array( 'item_key' => $id );
620 467 }
621 -
622 468 $id = FrmDb::get_var( $wpdb->prefix . 'frm_items', $where );
623 469
624 - return $id && $id > 0;
470 + return ( $id && $id > 0 );
625 471 }
626 472
627 - /**
628 - * @param array|string $where
629 - * @param string $order_by
630 - * @param string $limit
631 - * @param bool $meta
632 - * @param bool $inc_form
633 - *
634 - * @return array
635 - */
636 473 public static function getAll( $where, $order_by = '', $limit = '', $meta = false, $inc_form = true ) {
637 474 global $wpdb;
638 475
639 476 $limit = FrmDb::esc_limit( $limit );
@@ -641,14 +478,14 @@
641 478 $cache_key = FrmAppHelper::maybe_json_encode( $where ) . $order_by . $limit . $inc_form;
642 479 $entries = wp_cache_get( $cache_key, 'frm_entry' );
643 480
644 481 if ( false === $entries ) {
645 - $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';
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';
646 483 $table = $wpdb->prefix . 'frm_items it ';
647 484
648 485 if ( $inc_form ) {
649 486 $fields = 'it.*, fr.name as form_name,fr.form_key as form_key';
650 - $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 ';
651 488 }
652 489
653 490 if ( preg_match( '/ meta_([0-9]+)/', $order_by, $order_matches ) ) {
654 491 $fields .= self::sort_by_field( $order_matches[1] );
@@ -661,9 +498,9 @@
661 498 $entries = $wpdb->get_results( $query, OBJECT_K ); // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared
662 499 unset( $query );
663 500
664 501 FrmDb::set_cache( $cache_key, $entries, 'frm_entry' );
665 - }//end if
502 + }
666 503
667 504 if ( ! $meta || ! $entries ) {
668 505 self::prepare_entries( $entries );
669 506 return $entries;
@@ -674,9 +511,8 @@
674 511 $where = array( 'it.form_id' => substr( $where, 11 ) );
675 512 }
676 513
677 514 $meta_where = array( 'field_id !' => 0 );
678 -
679 515 if ( $limit == '' && is_array( $where ) && count( $where ) == 1 && isset( $where['it.form_id'] ) ) {
680 516 $meta_where['fi.form_id'] = $where['it.form_id'];
681 517 } else {
682 518 $meta_where['item_id'] = array_keys( $entries );
@@ -681,13 +517,9 @@
681 517 } else {
682 518 $meta_where['item_id'] = array_keys( $entries );
683 519 }
684 520
685 - $metas = FrmDb::get_results(
686 - $wpdb->prefix . 'frm_item_metas it LEFT OUTER JOIN ' . $wpdb->prefix . 'frm_fields fi ON it.field_id = fi.id',
687 - $meta_where,
688 - 'item_id, meta_value, field_id, field_key, form_id, fi.type'
689 - );
521 + $metas = FrmDb::get_results( $wpdb->prefix . 'frm_item_metas it LEFT OUTER JOIN ' . $wpdb->prefix . 'frm_fields fi ON (it.field_id = fi.id)', $meta_where, 'item_id, meta_value, field_id, field_key, form_id' );
690 522
691 523 unset( $meta_where );
692 524
693 525 if ( ! $metas ) {
@@ -703,9 +535,9 @@
703 535 if ( ! isset( $entries[ $meta_val->item_id ]->metas ) ) {
704 536 $entries[ $meta_val->item_id ]->metas = array();
705 537 }
706 538
707 - FrmFieldsHelper::prepare_field_value( $meta_val->meta_value, $meta_val->type );
539 + FrmAppHelper::unserialize_or_decode( $meta_val->meta_value );
708 540 $entries[ $meta_val->item_id ]->metas[ $meta_val->field_id ] = $meta_val->meta_value;
709 541 unset( $m_key, $meta_val );
710 542 }
711 543
@@ -720,10 +552,9 @@
720 552 return $entries;
721 553 }
722 554
723 555 /**
724 - * @param int|string $field_id
725 - *
556 + * @param int $field_id
726 557 * @return string
727 558 */
728 559 private static function sort_by_field( $field_id ) {
729 560 global $wpdb;
@@ -742,15 +573,13 @@
742 573 }
743 574
744 575 // Pagination Methods
745 576 /**
746 - * @param array|int|string $where If int, use the form id.
747 - *
748 - * @return int|string
577 + * @param int|array|string If int, use the form id.
749 578 */
750 579 public static function getRecordCount( $where = '' ) {
751 580 global $wpdb;
752 - $table_join = $wpdb->prefix . 'frm_items it JOIN ' . $wpdb->prefix . 'frm_forms fr ON it.form_id=fr.id';
581 + $table_join = $wpdb->prefix . 'frm_items it LEFT OUTER JOIN ' . $wpdb->prefix . 'frm_forms fr ON it.form_id=fr.id';
753 582
754 583 if ( is_numeric( $where ) ) {
755 584 $table_join = 'frm_items';
756 585 $where = array( 'form_id' => $where );
@@ -766,23 +595,15 @@
766 595
767 596 return $count;
768 597 }
769 598
770 - /**
771 - * @param int|string $p_size
772 - * @param array|int|string $where
773 - *
774 - * @return int
775 - */
776 599 public static function getPageCount( $p_size, $where = '' ) {
777 600 $p_size = (int) $p_size;
778 601 $count = 1;
779 -
780 602 if ( $p_size ) {
781 603 if ( ! is_numeric( $where ) ) {
782 604 $where = self::getRecordCount( $where );
783 605 }
784 -
785 606 $count = ceil( (int) $where / $p_size );
786 607 }
787 608
788 609 return $count;
@@ -792,9 +613,9 @@
792 613 * Prepare the data before inserting it into the database
793 614 *
794 615 * @since 2.0.16
795 616 *
796 - * @param array $values
617 + * @param array $values
797 618 * @param string $type
798 619 *
799 620 * @return array $new_values
800 621 */
@@ -801,9 +622,9 @@
801 622 private static function before_insert_entry_in_database( &$values, $type ) {
802 623
803 624 self::sanitize_entry_post( $values );
804 625
805 - if ( $type !== 'xml' ) {
626 + if ( $type != 'xml' ) {
806 627 $values = apply_filters( 'frm_pre_create_entry', $values );
807 628 }
808 629
809 630 $new_values = self::package_entry_data( $values );
@@ -818,13 +639,12 @@
818 639 *
819 640 * @param array $values
820 641 * @param array $new_values
821 642 *
822 - * @return bool|int $entry_id
643 + * @return boolean|int $entry_id
823 644 */
824 645 private static function continue_to_create_entry( $values, $new_values ) {
825 646 $entry_id = self::insert_entry_into_database( $new_values );
826 -
827 647 if ( ! $entry_id ) {
828 648 return false;
829 649 }
830 650
@@ -837,11 +657,9 @@
837 657 * Sanitize the POST values before we use them
838 658 *
839 659 * @since 2.0
840 660 *
841 - * @param array $values The POST values by reference.
842 - *
843 - * @return void
661 + * @param array $values The POST values by reference
844 662 */
845 663 public static function sanitize_entry_post( &$values ) {
846 664 $sanitize_method = array(
847 665 'form_id' => 'absint',
@@ -890,22 +708,15 @@
890 708 'description' => self::get_entry_description( $values ),
891 709 'user_id' => self::get_entry_user_id( $values ),
892 710 );
893 711
894 - $new_values['updated_by'] = $values['updated_by'] ?? $new_values['user_id'];
712 + $new_values['updated_by'] = isset( $values['updated_by'] ) ? $values['updated_by'] : $new_values['user_id'];
895 713
896 714 return $new_values;
897 715 }
898 716
899 - /**
900 - * @param array $values
901 - * @param string $name
902 - * @param mixed $default
903 - *
904 - * @return mixed
905 - */
906 717 private static function get_entry_value( $values, $name, $default ) {
907 - return $values[ $name ] ?? $default;
718 + return isset( $values[ $name ] ) ? $values[ $name ] : $default;
908 719 }
909 720
910 721 /**
911 722 * Get the ip for a new entry.
@@ -922,9 +733,8 @@
922 733 return '';
923 734 }
924 735
925 736 $ip = FrmAppHelper::get_ip_address();
926 -
927 737 if ( defined( 'WP_IMPORTING' ) && WP_IMPORTING ) {
928 738 $ip = self::get_entry_value( $values, 'ip', $ip );
929 739 }
930 740
@@ -940,13 +750,9 @@
940 750 *
941 751 * @return int
942 752 */
943 753 private static function get_is_draft_value( $values ) {
944 - if ( isset( $values['frm_saving_draft'] ) && FrmEntriesHelper::DRAFT_ENTRY_STATUS === (int) $values['frm_saving_draft'] ) {
945 - return FrmEntriesHelper::DRAFT_ENTRY_STATUS;
946 - }
947 -
948 - return isset( $values['is_draft'] ) ? absint( $values['is_draft'] ) : FrmEntriesHelper::SUBMITTED_ENTRY_STATUS;
754 + return ( ( isset( $values['frm_saving_draft'] ) && $values['frm_saving_draft'] == 1 ) || ( isset( $values['is_draft'] ) && $values['is_draft'] == 1 ) ) ? 1 : 0;
949 755 }
950 756
951 757 /**
952 758 * Get the created_at value for a new entry
@@ -989,9 +795,9 @@
989 795 *
990 796 * @return string
991 797 */
992 798 private static function get_entry_description( $values ) {
993 - if ( ! empty( $values['description'] ) ) {
799 + if ( isset( $values['description'] ) && ! empty( $values['description'] ) ) {
994 800 $description = FrmAppHelper::maybe_json_encode( $values['description'] );
995 801 } else {
996 802 $description = json_encode(
997 803 array(
@@ -1030,9 +836,9 @@
1030 836 * @since 2.0.16
1031 837 *
1032 838 * @param array $new_values
1033 839 *
1034 - * @return bool|int $entry_id
840 + * @return int | boolean $entry_id
1035 841 */
1036 842 private static function insert_entry_into_database( $new_values ) {
1037 843 global $wpdb;
1038 844
@@ -1051,11 +857,9 @@
1051 857 * Add the new entry to global $frm_vars
1052 858 *
1053 859 * @since 2.0.16
1054 860 *
1055 - * @param int|string $entry_id
1056 - *
1057 - * @return void
861 + * @param int $entry_id
1058 862 */
1059 863 private static function add_new_entry_to_frm_vars( $entry_id ) {
1060 864 global $frm_vars;
1061 865
@@ -1070,55 +874,28 @@
1070 874 * Add entry metas, if there are any
1071 875 *
1072 876 * @since 2.0.16
1073 877 *
1074 - * @param array $values
1075 - * @param int|string $entry_id
1076 - *
878 + * @param array $values
879 + * @param int $entry_id
1077 880 * @return void
1078 881 */
1079 882 private static function maybe_add_entry_metas( $values, $entry_id ) {
1080 883 if ( isset( $values['item_meta'] ) ) {
1081 884 FrmEntryMeta::update_entry_metas( $entry_id, $values['item_meta'] );
1082 - self::maybe_add_unique_id_meta( $values, $entry_id );
1083 885 }
1084 886 self::maybe_add_captcha_meta( (int) $values['form_id'], (int) $entry_id );
1085 887 }
1086 888
1087 889 /**
1088 - * @since 6.16.3
1089 - *
1090 - * @param array $values
1091 - * @param int $entry_id
1092 - *
1093 - * @return void
1094 - */
1095 - private static function maybe_add_unique_id_meta( $values, $entry_id ) {
1096 - if ( ! empty( $values['parent_form_id'] ) || empty( $values['unique_id'] ) || ! self::should_check_for_unique_id_match() ) {
1097 - return;
1098 - }
1099 -
1100 - // This unique ID is inserted with JS on form submit.
1101 - // It is used to check for duplicate entries.
1102 - $unique_id = sanitize_key( $values['unique_id'] );
1103 -
1104 - if ( $unique_id ) {
1105 - FrmEntryMeta::add_entry_meta( $entry_id, 0, '', compact( 'unique_id' ) );
1106 - self::flag_new_unique_key( $unique_id );
1107 - }
1108 - }
1109 -
1110 - /**
1111 890 * @since 5.0.15
1112 891 *
1113 892 * @param int $form_id
1114 893 * @param int $entry_id
1115 - *
1116 894 * @return void
1117 895 */
1118 896 private static function maybe_add_captcha_meta( $form_id, $entry_id ) {
1119 897 global $frm_vars;
1120 -
1121 898 if ( array_key_exists( 'captcha_scores', $frm_vars ) && array_key_exists( $form_id, $frm_vars['captcha_scores'] ) ) {
1122 899 $captcha_score_meta = array( 'captcha_score' => $frm_vars['captcha_scores'][ $form_id ] );
1123 900 FrmEntryMeta::add_entry_meta( $entry_id, 0, '', maybe_serialize( $captcha_score_meta ) );
1124 901 }
@@ -1128,17 +905,14 @@
1128 905 * Trigger frm_after_create_entry hooks
1129 906 *
1130 907 * @since 2.0.16
1131 908 *
1132 - * @param int $entry_id
1133 - * @param array $values
909 + * @param int $entry_id
1134 910 * @param array $new_values
1135 - *
1136 - * @return void
1137 911 */
1138 912 private static function after_entry_created_actions( $entry_id, $values, $new_values ) {
1139 - // This is a child entry.
1140 - $is_child = isset( $values['parent_nonce'] ) && ! empty( $values['parent_form_id'] ) && wp_verify_nonce( $values['parent_nonce'], 'parent' );
913 + // this is a child entry
914 + $is_child = isset( $values['parent_form_id'] ) && isset( $values['parent_nonce'] ) && ! empty( $values['parent_form_id'] ) && wp_verify_nonce( $values['parent_nonce'], 'parent' );
1141 915
1142 916 do_action( 'frm_after_create_entry', $entry_id, $new_values['form_id'], compact( 'is_child' ) );
1143 917 do_action( 'frm_after_create_entry_' . $new_values['form_id'], $entry_id, compact( 'is_child' ) );
1144 918 }
@@ -1149,11 +923,9 @@
1149 923 * @since 2.0.16
1150 924 *
1151 925 * @param array $values
1152 926 * @param array $new_values
1153 - * @param int $entry_id
1154 - *
1155 - * @return void
927 + * @param int $entry_id
1156 928 */
1157 929 private static function after_insert_entry_in_database( $values, $new_values, $entry_id ) {
1158 930
1159 931 self::add_new_entry_to_frm_vars( $entry_id );
@@ -1169,13 +941,13 @@
1169 941 * Perform some actions right before updating an entry
1170 942 *
1171 943 * @since 2.0.16
1172 944 *
1173 - * @param int|string $id
1174 - * @param array $values
1175 - * @param string $update_type
945 + * @param int $id
946 + * @param array $values
947 + * @param string $update_type
1176 948 *
1177 - * @return bool $update
949 + * @return boolean $update
1178 950 */
1179 951 private static function before_update_entry( $id, &$values, $update_type ) {
1180 952 $update = true;
1181 953
@@ -1180,13 +952,13 @@
1180 952 $update = true;
1181 953
1182 954 global $frm_vars;
1183 955
1184 - if ( isset( $frm_vars['saved_entries'] ) && is_array( $frm_vars['saved_entries'] ) && in_array( (int) $id, $frm_vars['saved_entries'] ) ) {
956 + if ( isset( $frm_vars['saved_entries'] ) && is_array( $frm_vars['saved_entries'] ) && in_array( (int) $id, (array) $frm_vars['saved_entries'] ) ) {
1185 957 $update = false;
1186 958 }
1187 959
1188 - if ( $update && $update_type !== 'xml' ) {
960 + if ( $update && $update_type != 'xml' ) {
1189 961 $values = apply_filters( 'frm_pre_update_entry', $values, $id );
1190 962 }
1191 963
1192 964 return $update;
@@ -1196,9 +968,9 @@
1196 968 * Package the entry data for updating
1197 969 *
1198 970 * @since 2.0.16
1199 971 *
1200 - * @param int $id
972 + * @param int $id
1201 973 * @param array $values
1202 974 *
1203 975 * @return array $new_values
1204 976 */
@@ -1209,9 +981,9 @@
1209 981 'name' => self::get_new_entry_name( $values ),
1210 982 'form_id' => (int) self::get_entry_value( $values, 'form_id', null ),
1211 983 'is_draft' => self::get_is_draft_value( $values ),
1212 984 'updated_at' => current_time( 'mysql', 1 ),
1213 - 'updated_by' => $values['updated_by'] ?? get_current_user_id(),
985 + 'updated_by' => isset( $values['updated_by'] ) ? $values['updated_by'] : get_current_user_id(),
1214 986 );
1215 987
1216 988 if ( isset( $values['post_id'] ) ) {
1217 989 $new_values['post_id'] = (int) $values['post_id'];
@@ -1238,14 +1010,12 @@
1238 1010 * Perform some actions right after updating an entry
1239 1011 *
1240 1012 * @since 2.0.16
1241 1013 *
1242 - * @param bool|int $query_results
1243 - * @param int|string $id
1244 - * @param array $values
1245 - * @param array $new_values
1246 - *
1247 - * @return void
1014 + * @param boolean|int $query_results
1015 + * @param int $id
1016 + * @param array $values
1017 + * @param array $new_values
1248 1018 */
1249 1019 private static function after_update_entry( $query_results, $id, $values, $new_values ) {
1250 1020 if ( $query_results ) {
1251 1021 self::clear_cache();
@@ -1251,9 +1021,8 @@
1251 1021 self::clear_cache();
1252 1022 }
1253 1023
1254 1024 global $frm_vars;
1255 -
1256 1025 if ( ! isset( $frm_vars['saved_entries'] ) ) {
1257 1026 $frm_vars['saved_entries'] = array();
1258 1027 }
1259 1028
@@ -1274,9 +1043,9 @@
1274 1043 * @since 2.0.16
1275 1044 *
1276 1045 * @param array $values
1277 1046 *
1278 - * @return bool|int $entry_id
1047 + * @return int | boolean $entry_id
1279 1048 */
1280 1049 public static function create_entry_from_xml( $values ) {
1281 1050 $entry_id = self::create_entry( $values, 'xml' );
1282 1051
@@ -1288,12 +1057,12 @@
1288 1057 * Certain actions aren't necessary when importing (like saving sub entries and modifying other vals)
1289 1058 *
1290 1059 * @since 2.0.16
1291 1060 *
1292 - * @param int $id
1061 + * @param int $id
1293 1062 * @param array $values
1294 1063 *
1295 - * @return bool|int $updated
1064 + * @return int | boolean $updated
1296 1065 */
1297 1066 public static function update_entry_from_xml( $id, $values ) {
1298 1067 $updated = self::update_entry( $id, $values, 'xml' );
1299 1068
@@ -1308,22 +1077,6 @@
1308 1077 public static function get_id_by_key( $key ) {
1309 1078 $entry_id = FrmDb::get_var( 'frm_items', array( 'item_key' => sanitize_title( $key ) ) );
1310 1079
1311 1080 return (int) $entry_id;
1312 - }
1313 -
1314 - /**
1315 - * Get entries count.
1316 - *
1317 - * @since 6.8
1318 - *
1319 - * @return int|string
1320 - */
1321 - public static function get_entries_count() {
1322 - $args = array(
1323 - 'or' => 1,
1324 - 'parent_form_id' => null,
1325 - 'parent_form_id <' => 1,
1326 - );
1327 - return self::getRecordCount( $args );
1328 1081 }
1329 1082 }