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