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