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