PluginProbe
Formidable Forms – WordPress Form Builder for Contact Forms, Calculators, Quizzes & More / 5.0
Formidable Forms – WordPress Form Builder for Contact Forms, Calculators, Quizzes & More v5.0
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 +72 -381 6.265.0 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'] ) {
@@ -98,13 +68,9 @@
98 68 if ( ! $entry_exists || ! isset( $values['item_meta'] ) ) {
99 69 return false;
100 70 }
101 71
102 - global $frm_vars;
103 - $frm_vars['checking_duplicates'] = true;
104 -
105 72 $is_duplicate = false;
106 -
107 73 foreach ( $entry_exists as $entry_exist ) {
108 74 $is_duplicate = true;
109 75
110 76 // make sure it's a duplicate
@@ -109,13 +75,9 @@
109 75
110 76 // make sure it's a duplicate
111 77 $metas = FrmEntryMeta::get_entry_meta_info( $entry_exist );
112 78 $field_metas = array();
113 -
114 79 foreach ( $metas as $meta ) {
115 - if ( 0 === (int) $meta->field_id ) {
116 - continue;
117 - }
118 80 $field_metas[ $meta->field_id ] = $meta->meta_value;
119 81 }
120 82
121 83 $filtered_vals = array_filter( $values['item_meta'] );
@@ -141,12 +103,12 @@
141 103 continue;
142 104 }
143 105
144 106 $diff = array_diff_assoc( $field_metas, $new_meta );
145 -
146 - foreach ( $diff as $meta_value ) {
107 + foreach ( $diff as $field_id => $meta_value ) {
147 108 if ( ! empty( $meta_value ) ) {
148 109 $is_duplicate = false;
110 + continue;
149 111 }
150 112 }
151 113
152 114 if ( $is_duplicate ) {
@@ -151,96 +113,27 @@
151 113
152 114 if ( $is_duplicate ) {
153 115 break;
154 116 }
155 - }//end foreach
117 + }
156 118
157 - $frm_vars['checking_duplicates'] = false;
158 -
159 119 return $is_duplicate;
160 120 }
161 121
162 122 /**
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 123 * Convert form data to the actual value that would be saved into the database.
228 124 * 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 125 *
230 126 * @param array $filter_vals
231 127 * @param int $entry_id
232 - *
233 128 * @return array
234 129 */
235 130 private static function convert_values_to_their_saved_value( $filter_vals, $entry_id ) {
236 131 $reduced = array();
237 -
238 132 foreach ( $filter_vals as $field_id => $value ) {
239 133 $field = FrmFieldFactory::get_field_object( $field_id );
240 134 $reduced[ $field_id ] = $field->get_value_to_save( $value, array( 'entry_id' => $entry_id ) );
241 135 $reduced[ $field_id ] = $field->set_value_before_save( $reduced[ $field_id ] );
242 -
243 136 if ( '' === $reduced[ $field_id ] || ( is_array( $reduced[ $field_id ] ) && 0 === count( $reduced[ $field_id ] ) ) ) {
244 137 unset( $reduced[ $field_id ] );
245 138 }
246 139 }
@@ -252,9 +145,9 @@
252 145 *
253 146 * @since 2.0.23
254 147 *
255 148 * @param array $values
256 - * @param int $duplicate_entry_time
149 + * @param int $duplicate_entry_time
257 150 *
258 151 * @return bool
259 152 */
260 153 private static function is_duplicate_check_needed( $values, $duplicate_entry_time ) {
@@ -275,13 +168,8 @@
275 168
276 169 return true;
277 170 }
278 171
279 - /**
280 - * @param int|string $id
281 - *
282 - * @return false|int
283 - */
284 172 public static function duplicate( $id ) {
285 173 global $wpdb;
286 174
287 175 $values = self::getOne( $id );
@@ -296,9 +184,8 @@
296 184 $new_values['created_at'] = current_time( 'mysql', 1 );
297 185 $new_values['updated_at'] = $new_values['created_at'];
298 186
299 187 $query_results = $wpdb->insert( $wpdb->prefix . 'frm_items', $new_values );
300 -
301 188 if ( ! $query_results ) {
302 189 return false;
303 190 }
304 191
@@ -304,9 +191,8 @@
304 191
305 192 $entry_id = $wpdb->insert_id;
306 193
307 194 global $frm_vars;
308 -
309 195 if ( ! isset( $frm_vars['saved_entries'] ) ) {
310 196 $frm_vars['saved_entries'] = array();
311 197 }
312 198 $frm_vars['saved_entries'][] = (int) $entry_id;
@@ -321,12 +207,12 @@
321 207
322 208 /**
323 209 * Update an entry (not via XML)
324 210 *
325 - * @param int $id
211 + * @param int $id
326 212 * @param array $values
327 213 *
328 - * @return bool|int $update_results
214 + * @return boolean|int $update_results
329 215 */
330 216 public static function update( $id, $values ) {
331 217 $update_results = self::update_entry( $id, $values, 'standard' );
332 218
@@ -337,19 +223,17 @@
337 223 * Update an entry with some differences depending on the update type
338 224 *
339 225 * @since 2.0.16
340 226 *
341 - * @param int $id
342 - * @param array $values
343 - * @param string $update_type
227 + * @param int $id
228 + * @param array $values
344 229 *
345 - * @return bool|int $query_results
230 + * @return boolean|int $query_results
346 231 */
347 232 private static function update_entry( $id, $values, $update_type ) {
348 233 global $wpdb;
349 234
350 235 $update = self::before_update_entry( $id, $values, $update_type );
351 -
352 236 if ( ! $update ) {
353 237 return false;
354 238 }
355 239
@@ -361,33 +245,19 @@
361 245
362 246 return $query_results;
363 247 }
364 248
365 - /**
366 - * Delete an entry.
367 - *
368 - * @param int|string $id
369 - *
370 - * @return bool True on success, false if nothing was deleted.
371 - */
372 249 public static function destroy( $id ) {
373 250 global $wpdb;
374 251 $id = (int) $id;
375 252
376 - // Item meta is required for conditional logic in actions with 'delete' events.
377 - $entry = self::getOne( $id, true );
378 -
253 + $entry = self::getOne( $id );
379 254 if ( ! $entry ) {
380 255 $result = false;
256 +
381 257 return $result;
382 258 }
383 259
384 - /**
385 - * Trigger an action to run custom logic before the entry is deleted.
386 - *
387 - * @param int $id The id of the entry that was destroyed.
388 - * @param stdClass $entry The entry object.
389 - */
390 260 do_action( 'frm_before_destroy_entry', $id, $entry );
391 261
392 262 $wpdb->query( $wpdb->prepare( 'DELETE FROM ' . $wpdb->prefix . 'frm_item_metas WHERE item_id=%d', $id ) );
393 263 $result = $wpdb->query( $wpdb->prepare( 'DELETE FROM ' . $wpdb->prefix . 'frm_items WHERE id=%d', $id ) );
@@ -393,34 +263,15 @@
393 263 $result = $wpdb->query( $wpdb->prepare( 'DELETE FROM ' . $wpdb->prefix . 'frm_items WHERE id=%d', $id ) );
394 264
395 265 self::clear_cache();
396 266
397 - /**
398 - * Trigger an action to run custom logic after the entry is deleted.
399 - * Use this hook if you need to update caching after an entry is deleted.
400 - *
401 - * @since 5.4.1
402 - *
403 - * @param int $id The id of the entry that was destroyed.
404 - * @param stdClass $entry The entry object.
405 - */
406 - do_action( 'frm_after_destroy_entry', $id, $entry );
407 -
408 267 return $result;
409 268 }
410 269
411 - /**
412 - * @param int $id
413 - * @param mixed $value
414 - * @param int|string $form_id
415 - *
416 - * @return false|int
417 - */
418 270 public static function update_form( $id, $value, $form_id ) {
419 271 global $wpdb;
420 272 $form_id = isset( $value ) ? $form_id : null;
421 273 $result = $wpdb->update( $wpdb->prefix . 'frm_items', array( 'form_id' => $form_id ), array( 'id' => $id ) );
422 -
423 274 if ( $result ) {
424 275 self::clear_cache();
425 276 }
426 277
@@ -431,10 +282,8 @@
431 282 * Clear entry caching
432 283 * Called when an entry is changed
433 284 *
434 285 * @since 2.0.5
435 - *
436 - * @return void
437 286 */
438 287 public static function clear_cache() {
439 288 FrmDb::cache_delete_group( 'frm_entry' );
440 289 FrmDb::cache_delete_group( 'frm_item' );
@@ -446,17 +295,11 @@
446 295 * After switching to the wp_loaded hook for processing entries,
447 296 * we can no longer use 'name', but check it as a fallback
448 297 *
449 298 * @since 2.0.11
450 - *
451 - * @param array $values
452 - * @param array|string $default
453 - *
454 - * @return string
455 299 */
456 300 public static function get_new_entry_name( $values, $default = '' ) {
457 - $name = $values['item_name'] ?? $values['name'] ?? $default;
458 -
301 + $name = isset( $values['item_name'] ) ? $values['item_name'] : ( isset( $values['name'] ) ? $values['name'] : $default );
459 302 if ( is_array( $name ) ) {
460 303 $name = reset( $name );
461 304 }
462 305
@@ -465,13 +308,11 @@
465 308
466 309 /**
467 310 * If $entry is numeric, get the entry object
468 311 *
312 + * @param int|object $entry by reference
313 + *
469 314 * @since 2.0.9
470 - *
471 - * @param int|object $entry By reference.
472 - *
473 - * @return void
474 315 */
475 316 public static function maybe_get_entry( &$entry ) {
476 317 if ( $entry && is_numeric( $entry ) ) {
477 318 $entry = self::getOne( $entry );
@@ -479,14 +320,8 @@
479 320 $entry = false;
480 321 }
481 322 }
482 323
483 - /**
484 - * @param int|string $id
485 - * @param bool $meta
486 - *
487 - * @return object|null
488 - */
489 324 public static function getOne( $id, $meta = false ) {
490 325 global $wpdb;
491 326
492 327 $query = "SELECT it.*, fr.name as form_name, fr.form_key as form_key FROM {$wpdb->prefix}frm_items it
@@ -491,11 +326,11 @@
491 326
492 327 $query = "SELECT it.*, fr.name as form_name, fr.form_key as form_key FROM {$wpdb->prefix}frm_items it
493 328 LEFT OUTER JOIN {$wpdb->prefix}frm_forms fr ON it.form_id=fr.id WHERE ";
494 329
495 - $query .= is_numeric( $id ) ? 'it.id=%d' : 'it.item_key=%s';
330 + $query .= is_numeric( $id ) ? 'it.id=%d' : 'it.item_key=%s';
496 331 $query_args = array( $id );
497 - $query = $wpdb->prepare( $query, $query_args ); // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared
332 + $query = $wpdb->prepare( $query, $query_args ); // WPCS: unprepared SQL ok.
498 333
499 334 if ( ! $meta ) {
500 335 $entry = FrmDb::check_cache( $id . '_nometa', 'frm_entry', $query, 'get_row' );
501 336 self::prepare_entry( $entry );
@@ -502,15 +337,14 @@
502 337 return $entry;
503 338 }
504 339
505 340 $entry = FrmDb::check_cache( $id, 'frm_entry' );
506 -
507 341 if ( $entry !== false ) {
508 342 self::prepare_entry( $entry );
509 343 return $entry;
510 344 }
511 345
512 - $entry = $wpdb->get_row( $query ); // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared
346 + $entry = $wpdb->get_row( $query ); // WPCS: unprepared SQL ok.
513 347 $entry = self::get_meta( $entry );
514 348 self::prepare_entry( $entry );
515 349
516 350 return $entry;
@@ -519,10 +353,8 @@
519 353 /**
520 354 * @since 4.02.03
521 355 *
522 356 * @param object $entry
523 - *
524 - * @return void
525 357 */
526 358 private static function prepare_entry( &$entry ) {
527 359 if ( empty( $entry ) ) {
528 360 return;
@@ -528,10 +360,9 @@
528 360 return;
529 361 }
530 362
531 363 FrmAppHelper::unserialize_or_decode( $entry->description );
532 - // TODO: Remove slashes on input only, not output.
533 - $entry = wp_unslash( $entry );
364 + $entry = wp_unslash( $entry ); // TODO: Remove slashes on input only, not output.
534 365 }
535 366
536 367 /**
537 368 * @since 4.02.03
@@ -536,10 +367,8 @@
536 367 /**
537 368 * @since 4.02.03
538 369 *
539 370 * @param array $entries
540 - *
541 - * @return void
542 371 */
543 372 private static function prepare_entries( &$entries ) {
544 373 foreach ( $entries as $k => $entry ) {
545 374 self::prepare_entry( $entry );
@@ -546,13 +375,8 @@
546 375 $entries[ $k ] = $entry;
547 376 }
548 377 }
549 378
550 - /**
551 - * @param object|null $entry
552 - *
553 - * @return object|null
554 - */
555 379 public static function get_meta( $entry ) {
556 380 if ( ! $entry ) {
557 381 return $entry;
558 382 }
@@ -563,21 +387,18 @@
563 387 array(
564 388 'item_id' => $entry->id,
565 389 'field_id !' => 0,
566 390 ),
567 - 'field_id, meta_value, field_key, item_id, f.type'
391 + 'field_id, meta_value, field_key, item_id'
568 392 );
569 393
570 394 $entry->metas = array();
571 395
572 396 $include_key = apply_filters( 'frm_include_meta_keys', false, array( 'form_id' => $entry->form_id ) );
573 -
574 397 foreach ( $metas as $meta_val ) {
575 - FrmFieldsHelper::prepare_field_value( $meta_val->meta_value, $meta_val->type );
576 -
577 398 if ( $meta_val->item_id == $entry->id ) {
399 + FrmAppHelper::unserialize_or_decode( $meta_val->meta_value );
578 400 $entry->metas[ $meta_val->field_id ] = $meta_val->meta_value;
579 -
580 401 if ( $include_key ) {
581 402 $entry->metas[ $meta_val->field_key ] = $entry->metas[ $meta_val->field_id ];
582 403 }
583 404 continue;
@@ -587,12 +408,13 @@
587 408 if ( ! isset( $entry->metas[ $meta_val->field_id ] ) ) {
588 409 $entry->metas[ $meta_val->field_id ] = array();
589 410 }
590 411
412 + FrmAppHelper::unserialize_or_decode( $meta_val->meta_value );
591 413 $entry->metas[ $meta_val->field_id ][] = $meta_val->meta_value;
592 414
593 415 unset( $meta_val );
594 - }//end foreach
416 + }
595 417 unset( $metas );
596 418
597 419 FrmDb::set_cache( $entry->id, $entry, 'frm_entry' );
598 420
@@ -600,10 +422,8 @@
600 422 }
601 423
602 424 /**
603 425 * @param string $id
604 - *
605 - * @return bool
606 426 */
607 427 public static function exists( $id ) {
608 428 global $wpdb;
609 429
@@ -617,23 +437,13 @@
617 437 $where = array( 'id' => $id );
618 438 } else {
619 439 $where = array( 'item_key' => $id );
620 440 }
621 -
622 441 $id = FrmDb::get_var( $wpdb->prefix . 'frm_items', $where );
623 442
624 - return $id && $id > 0;
443 + return ( $id && $id > 0 );
625 444 }
626 445
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 446 public static function getAll( $where, $order_by = '', $limit = '', $meta = false, $inc_form = true ) {
637 447 global $wpdb;
638 448
639 449 $limit = FrmDb::esc_limit( $limit );
@@ -641,29 +451,31 @@
641 451 $cache_key = FrmAppHelper::maybe_json_encode( $where ) . $order_by . $limit . $inc_form;
642 452 $entries = wp_cache_get( $cache_key, 'frm_entry' );
643 453
644 454 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';
455 + $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 456 $table = $wpdb->prefix . 'frm_items it ';
647 457
648 458 if ( $inc_form ) {
649 459 $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 ';
460 + $table .= 'LEFT OUTER JOIN ' . $wpdb->prefix . 'frm_forms fr ON it.form_id=fr.id ';
651 461 }
652 462
653 463 if ( preg_match( '/ meta_([0-9]+)/', $order_by, $order_matches ) ) {
654 - $fields .= self::sort_by_field( $order_matches[1] );
655 - unset( $order_matches );
464 + // sort by a requested field
465 + $field_id = (int) $order_matches[1];
466 + $fields .= ', (SELECT meta_value FROM ' . $wpdb->prefix . 'frm_item_metas WHERE field_id = ' . $field_id . ' AND item_id = it.id) as meta_' . $field_id;
467 + unset( $order_matches, $field_id );
656 468 }
657 469
658 470 // prepare the query
659 471 $query = 'SELECT ' . $fields . ' FROM ' . $table . FrmDb::prepend_and_or_where( ' WHERE ', $where ) . $order_by . $limit;
660 472
661 - $entries = $wpdb->get_results( $query, OBJECT_K ); // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared
473 + $entries = $wpdb->get_results( $query, OBJECT_K ); // WPCS: unprepared SQL ok.
662 474 unset( $query );
663 475
664 476 FrmDb::set_cache( $cache_key, $entries, 'frm_entry' );
665 - }//end if
477 + }
666 478
667 479 if ( ! $meta || ! $entries ) {
668 480 self::prepare_entries( $entries );
669 481 return $entries;
@@ -674,9 +486,8 @@
674 486 $where = array( 'it.form_id' => substr( $where, 11 ) );
675 487 }
676 488
677 489 $meta_where = array( 'field_id !' => 0 );
678 -
679 490 if ( $limit == '' && is_array( $where ) && count( $where ) == 1 && isset( $where['it.form_id'] ) ) {
680 491 $meta_where['fi.form_id'] = $where['it.form_id'];
681 492 } else {
682 493 $meta_where['item_id'] = array_keys( $entries );
@@ -681,13 +492,9 @@
681 492 } else {
682 493 $meta_where['item_id'] = array_keys( $entries );
683 494 }
684 495
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 - );
496 + $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 497
691 498 unset( $meta_where );
692 499
693 500 if ( ! $metas ) {
@@ -703,9 +510,9 @@
703 510 if ( ! isset( $entries[ $meta_val->item_id ]->metas ) ) {
704 511 $entries[ $meta_val->item_id ]->metas = array();
705 512 }
706 513
707 - FrmFieldsHelper::prepare_field_value( $meta_val->meta_value, $meta_val->type );
514 + FrmAppHelper::unserialize_or_decode( $meta_val->meta_value );
708 515 $entries[ $meta_val->item_id ]->metas[ $meta_val->field_id ] = $meta_val->meta_value;
709 516 unset( $m_key, $meta_val );
710 517 }
711 518
@@ -719,38 +526,15 @@
719 526 self::prepare_entries( $entries );
720 527 return $entries;
721 528 }
722 529
723 - /**
724 - * @param int|string $field_id
725 - *
726 - * @return string
727 - */
728 - private static function sort_by_field( $field_id ) {
729 - global $wpdb;
730 - $field_id = (int) $field_id;
731 -
732 - $field_options = FrmDb::get_var( 'frm_fields', array( 'id' => $field_id ), 'field_options' );
733 - FrmAppHelper::unserialize_or_decode( $field_options );
734 -
735 - if ( empty( $field_options['post_field'] ) ) {
736 - $sort = ', (SELECT meta_value FROM ' . $wpdb->prefix . 'frm_item_metas WHERE field_id = ' . $field_id . ' AND item_id = it.id) as meta_' . $field_id;
737 - } else {
738 - $sort = '';
739 - }
740 -
741 - return apply_filters( 'frm_handle_field_column_sort', $sort, $field_id, $field_options );
742 - }
743 -
744 530 // Pagination Methods
745 531 /**
746 - * @param array|int|string $where If int, use the form id.
747 - *
748 - * @return int|string
532 + * @param int|array|string If int, use the form id.
749 533 */
750 534 public static function getRecordCount( $where = '' ) {
751 535 global $wpdb;
752 - $table_join = $wpdb->prefix . 'frm_items it JOIN ' . $wpdb->prefix . 'frm_forms fr ON it.form_id=fr.id';
536 + $table_join = $wpdb->prefix . 'frm_items it LEFT OUTER JOIN ' . $wpdb->prefix . 'frm_forms fr ON it.form_id=fr.id';
753 537
754 538 if ( is_numeric( $where ) ) {
755 539 $table_join = 'frm_items';
756 540 $where = array( 'form_id' => $where );
@@ -766,23 +550,15 @@
766 550
767 551 return $count;
768 552 }
769 553
770 - /**
771 - * @param int|string $p_size
772 - * @param array|int|string $where
773 - *
774 - * @return int
775 - */
776 554 public static function getPageCount( $p_size, $where = '' ) {
777 555 $p_size = (int) $p_size;
778 556 $count = 1;
779 -
780 557 if ( $p_size ) {
781 558 if ( ! is_numeric( $where ) ) {
782 559 $where = self::getRecordCount( $where );
783 560 }
784 -
785 561 $count = ceil( (int) $where / $p_size );
786 562 }
787 563
788 564 return $count;
@@ -792,9 +568,9 @@
792 568 * Prepare the data before inserting it into the database
793 569 *
794 570 * @since 2.0.16
795 571 *
796 - * @param array $values
572 + * @param array $values
797 573 * @param string $type
798 574 *
799 575 * @return array $new_values
800 576 */
@@ -801,9 +577,9 @@
801 577 private static function before_insert_entry_in_database( &$values, $type ) {
802 578
803 579 self::sanitize_entry_post( $values );
804 580
805 - if ( $type !== 'xml' ) {
581 + if ( $type != 'xml' ) {
806 582 $values = apply_filters( 'frm_pre_create_entry', $values );
807 583 }
808 584
809 585 $new_values = self::package_entry_data( $values );
@@ -818,13 +594,12 @@
818 594 *
819 595 * @param array $values
820 596 * @param array $new_values
821 597 *
822 - * @return bool|int $entry_id
598 + * @return boolean|int $entry_id
823 599 */
824 600 private static function continue_to_create_entry( $values, $new_values ) {
825 601 $entry_id = self::insert_entry_into_database( $new_values );
826 -
827 602 if ( ! $entry_id ) {
828 603 return false;
829 604 }
830 605
@@ -837,11 +612,9 @@
837 612 * Sanitize the POST values before we use them
838 613 *
839 614 * @since 2.0
840 615 *
841 - * @param array $values The POST values by reference.
842 - *
843 - * @return void
616 + * @param array $values The POST values by reference
844 617 */
845 618 public static function sanitize_entry_post( &$values ) {
846 619 $sanitize_method = array(
847 620 'form_id' => 'absint',
@@ -890,22 +663,15 @@
890 663 'description' => self::get_entry_description( $values ),
891 664 'user_id' => self::get_entry_user_id( $values ),
892 665 );
893 666
894 - $new_values['updated_by'] = $values['updated_by'] ?? $new_values['user_id'];
667 + $new_values['updated_by'] = isset( $values['updated_by'] ) ? $values['updated_by'] : $new_values['user_id'];
895 668
896 669 return $new_values;
897 670 }
898 671
899 - /**
900 - * @param array $values
901 - * @param string $name
902 - * @param mixed $default
903 - *
904 - * @return mixed
905 - */
906 672 private static function get_entry_value( $values, $name, $default ) {
907 - return $values[ $name ] ?? $default;
673 + return isset( $values[ $name ] ) ? $values[ $name ] : $default;
908 674 }
909 675
910 676 /**
911 677 * Get the ip for a new entry.
@@ -922,9 +688,8 @@
922 688 return '';
923 689 }
924 690
925 691 $ip = FrmAppHelper::get_ip_address();
926 -
927 692 if ( defined( 'WP_IMPORTING' ) && WP_IMPORTING ) {
928 693 $ip = self::get_entry_value( $values, 'ip', $ip );
929 694 }
930 695
@@ -940,13 +705,9 @@
940 705 *
941 706 * @return int
942 707 */
943 708 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;
709 + return ( ( isset( $values['frm_saving_draft'] ) && $values['frm_saving_draft'] == 1 ) || ( isset( $values['is_draft'] ) && $values['is_draft'] == 1 ) ) ? 1 : 0;
949 710 }
950 711
951 712 /**
952 713 * Get the created_at value for a new entry
@@ -989,9 +750,9 @@
989 750 *
990 751 * @return string
991 752 */
992 753 private static function get_entry_description( $values ) {
993 - if ( ! empty( $values['description'] ) ) {
754 + if ( isset( $values['description'] ) && ! empty( $values['description'] ) ) {
994 755 $description = FrmAppHelper::maybe_json_encode( $values['description'] );
995 756 } else {
996 757 $description = json_encode(
997 758 array(
@@ -1030,9 +791,9 @@
1030 791 * @since 2.0.16
1031 792 *
1032 793 * @param array $new_values
1033 794 *
1034 - * @return bool|int $entry_id
795 + * @return int | boolean $entry_id
1035 796 */
1036 797 private static function insert_entry_into_database( $new_values ) {
1037 798 global $wpdb;
1038 799
@@ -1051,11 +812,9 @@
1051 812 * Add the new entry to global $frm_vars
1052 813 *
1053 814 * @since 2.0.16
1054 815 *
1055 - * @param int|string $entry_id
1056 - *
1057 - * @return void
816 + * @param int $entry_id
1058 817 */
1059 818 private static function add_new_entry_to_frm_vars( $entry_id ) {
1060 819 global $frm_vars;
1061 820
@@ -1070,75 +829,28 @@
1070 829 * Add entry metas, if there are any
1071 830 *
1072 831 * @since 2.0.16
1073 832 *
1074 - * @param array $values
1075 - * @param int|string $entry_id
1076 - *
1077 - * @return void
833 + * @param array $values
834 + * @param int $entry_id
1078 835 */
1079 836 private static function maybe_add_entry_metas( $values, $entry_id ) {
1080 837 if ( isset( $values['item_meta'] ) ) {
1081 838 FrmEntryMeta::update_entry_metas( $entry_id, $values['item_meta'] );
1082 - self::maybe_add_unique_id_meta( $values, $entry_id );
1083 839 }
1084 - self::maybe_add_captcha_meta( (int) $values['form_id'], (int) $entry_id );
1085 840 }
1086 841
1087 842 /**
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 - * @since 5.0.15
1112 - *
1113 - * @param int $form_id
1114 - * @param int $entry_id
1115 - *
1116 - * @return void
1117 - */
1118 - private static function maybe_add_captcha_meta( $form_id, $entry_id ) {
1119 - global $frm_vars;
1120 -
1121 - if ( array_key_exists( 'captcha_scores', $frm_vars ) && array_key_exists( $form_id, $frm_vars['captcha_scores'] ) ) {
1122 - $captcha_score_meta = array( 'captcha_score' => $frm_vars['captcha_scores'][ $form_id ] );
1123 - FrmEntryMeta::add_entry_meta( $entry_id, 0, '', maybe_serialize( $captcha_score_meta ) );
1124 - }
1125 - }
1126 -
1127 - /**
1128 843 * Trigger frm_after_create_entry hooks
1129 844 *
1130 845 * @since 2.0.16
1131 846 *
1132 - * @param int $entry_id
1133 - * @param array $values
847 + * @param int $entry_id
1134 848 * @param array $new_values
1135 - *
1136 - * @return void
1137 849 */
1138 850 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' );
851 + // this is a child entry
852 + $is_child = isset( $values['parent_form_id'] ) && isset( $values['parent_nonce'] ) && ! empty( $values['parent_form_id'] ) && wp_verify_nonce( $values['parent_nonce'], 'parent' );
1141 853
1142 854 do_action( 'frm_after_create_entry', $entry_id, $new_values['form_id'], compact( 'is_child' ) );
1143 855 do_action( 'frm_after_create_entry_' . $new_values['form_id'], $entry_id, compact( 'is_child' ) );
1144 856 }
@@ -1149,11 +861,9 @@
1149 861 * @since 2.0.16
1150 862 *
1151 863 * @param array $values
1152 864 * @param array $new_values
1153 - * @param int $entry_id
1154 - *
1155 - * @return void
865 + * @param int $entry_id
1156 866 */
1157 867 private static function after_insert_entry_in_database( $values, $new_values, $entry_id ) {
1158 868
1159 869 self::add_new_entry_to_frm_vars( $entry_id );
@@ -1169,13 +879,13 @@
1169 879 * Perform some actions right before updating an entry
1170 880 *
1171 881 * @since 2.0.16
1172 882 *
1173 - * @param int|string $id
1174 - * @param array $values
1175 - * @param string $update_type
883 + * @param int $id
884 + * @param array $values
885 + * @param string $update_type
1176 886 *
1177 - * @return bool $update
887 + * @return boolean $update
1178 888 */
1179 889 private static function before_update_entry( $id, &$values, $update_type ) {
1180 890 $update = true;
1181 891
@@ -1180,13 +890,13 @@
1180 890 $update = true;
1181 891
1182 892 global $frm_vars;
1183 893
1184 - if ( isset( $frm_vars['saved_entries'] ) && is_array( $frm_vars['saved_entries'] ) && in_array( (int) $id, $frm_vars['saved_entries'] ) ) {
894 + if ( isset( $frm_vars['saved_entries'] ) && is_array( $frm_vars['saved_entries'] ) && in_array( (int) $id, (array) $frm_vars['saved_entries'] ) ) {
1185 895 $update = false;
1186 896 }
1187 897
1188 - if ( $update && $update_type !== 'xml' ) {
898 + if ( $update && $update_type != 'xml' ) {
1189 899 $values = apply_filters( 'frm_pre_update_entry', $values, $id );
1190 900 }
1191 901
1192 902 return $update;
@@ -1196,9 +906,9 @@
1196 906 * Package the entry data for updating
1197 907 *
1198 908 * @since 2.0.16
1199 909 *
1200 - * @param int $id
910 + * @param int $id
1201 911 * @param array $values
1202 912 *
1203 913 * @return array $new_values
1204 914 */
@@ -1209,9 +919,9 @@
1209 919 'name' => self::get_new_entry_name( $values ),
1210 920 'form_id' => (int) self::get_entry_value( $values, 'form_id', null ),
1211 921 'is_draft' => self::get_is_draft_value( $values ),
1212 922 'updated_at' => current_time( 'mysql', 1 ),
1213 - 'updated_by' => $values['updated_by'] ?? get_current_user_id(),
923 + 'updated_by' => isset( $values['updated_by'] ) ? $values['updated_by'] : get_current_user_id(),
1214 924 );
1215 925
1216 926 if ( isset( $values['post_id'] ) ) {
1217 927 $new_values['post_id'] = (int) $values['post_id'];
@@ -1238,14 +948,12 @@
1238 948 * Perform some actions right after updating an entry
1239 949 *
1240 950 * @since 2.0.16
1241 951 *
1242 - * @param bool|int $query_results
1243 - * @param int|string $id
1244 - * @param array $values
1245 - * @param array $new_values
1246 - *
1247 - * @return void
952 + * @param boolean|int $query_results
953 + * @param int $id
954 + * @param array $values
955 + * @param array $new_values
1248 956 */
1249 957 private static function after_update_entry( $query_results, $id, $values, $new_values ) {
1250 958 if ( $query_results ) {
1251 959 self::clear_cache();
@@ -1251,9 +959,8 @@
1251 959 self::clear_cache();
1252 960 }
1253 961
1254 962 global $frm_vars;
1255 -
1256 963 if ( ! isset( $frm_vars['saved_entries'] ) ) {
1257 964 $frm_vars['saved_entries'] = array();
1258 965 }
1259 966
@@ -1274,9 +981,9 @@
1274 981 * @since 2.0.16
1275 982 *
1276 983 * @param array $values
1277 984 *
1278 - * @return bool|int $entry_id
985 + * @return int | boolean $entry_id
1279 986 */
1280 987 public static function create_entry_from_xml( $values ) {
1281 988 $entry_id = self::create_entry( $values, 'xml' );
1282 989
@@ -1288,12 +995,12 @@
1288 995 * Certain actions aren't necessary when importing (like saving sub entries and modifying other vals)
1289 996 *
1290 997 * @since 2.0.16
1291 998 *
1292 - * @param int $id
999 + * @param int $id
1293 1000 * @param array $values
1294 1001 *
1295 - * @return bool|int $updated
1002 + * @return int | boolean $updated
1296 1003 */
1297 1004 public static function update_entry_from_xml( $id, $values ) {
1298 1005 $updated = self::update_entry( $id, $values, 'xml' );
1299 1006
@@ -1308,22 +1015,6 @@
1308 1015 public static function get_id_by_key( $key ) {
1309 1016 $entry_id = FrmDb::get_var( 'frm_items', array( 'item_key' => sanitize_title( $key ) ) );
1310 1017
1311 1018 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 1019 }
1329 1020 }