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