PluginProbe
Formidable Forms – WordPress Form Builder for Contact Forms, Calculators, Quizzes & More / 4.01.02
Formidable Forms – WordPress Form Builder for Contact Forms, Calculators, Quizzes & More v4.01.02
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
formidable / classes / models / FrmEntry.php

FrmEntry.php in Formidable Forms – WordPress Form Builder for Contact Forms, Calculators, Quizzes & More 4.01.02, at classes/models/FrmEntry.php

964 lines 24.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 if ( ! defined( 'ABSPATH' ) ) {
3 die( 'You are not allowed to call this page directly.' );
4 }
5
6 class FrmEntry {
7
8 /**
9 * Create a new entry
10 *
11 * @param array $values
12 *
13 * @return int | boolean $entry_id
14 */
15 public static function create( $values ) {
16 $entry_id = self::create_entry( $values, 'standard' );
17
18 return $entry_id;
19 }
20
21 /**
22 * Create a new entry with some differences depending on type
23 *
24 * @param array $values
25 * @param string $type
26 *
27 * @return int | boolean $entry_id
28 */
29 private static function create_entry( $values, $type ) {
30 $new_values = self::before_insert_entry_in_database( $values, $type );
31
32 // Don't check XML entries for duplicates
33 if ( $type != 'xml' && self::is_duplicate( $new_values, $values ) ) {
34 return false;
35 }
36
37 $entry_id = self::continue_to_create_entry( $values, $new_values );
38
39 return $entry_id;
40 }
41
42 /**
43 * Check for duplicate entries created in the last minute
44 *
45 * @return boolean
46 */
47 public static function is_duplicate( $new_values, $values ) {
48 $duplicate_entry_time = apply_filters( 'frm_time_to_check_duplicates', 60, $new_values );
49
50 if ( false === self::is_duplicate_check_needed( $values, $duplicate_entry_time ) ) {
51 return false;
52 }
53
54 $check_val = $new_values;
55 $check_val['created_at >'] = date( 'Y-m-d H:i:s', ( strtotime( $new_values['created_at'] ) - absint( $duplicate_entry_time ) ) );
56
57 unset( $check_val['created_at'], $check_val['updated_at'] );
58 unset( $check_val['is_draft'], $check_val['id'], $check_val['item_key'] );
59
60 if ( $new_values['item_key'] == $new_values['name'] ) {
61 unset( $check_val['name'] );
62 }
63
64 global $wpdb;
65 $entry_exists = FrmDb::get_col( $wpdb->prefix . 'frm_items', $check_val, 'id', array( 'order_by' => 'created_at DESC' ) );
66
67 if ( ! $entry_exists || empty( $entry_exists ) || ! isset( $values['item_meta'] ) ) {
68 return false;
69 }
70
71 $is_duplicate = false;
72 foreach ( $entry_exists as $entry_exist ) {
73 $is_duplicate = true;
74
75 // make sure it's a duplicate
76 $metas = FrmEntryMeta::get_entry_meta_info( $entry_exist );
77 $field_metas = array();
78 foreach ( $metas as $meta ) {
79 $field_metas[ $meta->field_id ] = $meta->meta_value;
80 }
81
82 // If prev entry is empty and current entry is not, they are not duplicates
83 $filtered_vals = array_filter( $values['item_meta'] );
84 $field_metas = array_filter( $field_metas );
85 if ( empty( $field_metas ) && ! empty( $filtered_vals ) ) {
86 return false;
87 }
88
89 // compare serialized values and not arrays
90 $new_meta = array_map( 'maybe_serialize', $filtered_vals );
91
92 if ( $field_metas === $new_meta ) {
93 $is_duplicate = true;
94 break;
95 }
96
97 if ( count( $field_metas ) !== count( $new_meta ) ) {
98 // TODO: compare values saved in the post also
99 $is_duplicate = false;
100 continue;
101 }
102
103 $diff = array_diff_assoc( $field_metas, $new_meta );
104 foreach ( $diff as $field_id => $meta_value ) {
105 if ( ! empty( $meta_value ) ) {
106 $is_duplicate = false;
107 continue;
108 }
109 }
110
111 if ( $is_duplicate ) {
112 break;
113 }
114 }
115
116 return $is_duplicate;
117 }
118
119 /**
120 * Determine if an entry needs to be checked as a possible duplicate
121 *
122 * @since 2.0.23
123 *
124 * @param array $values
125 * @param int $duplicate_entry_time
126 *
127 * @return bool
128 */
129 private static function is_duplicate_check_needed( $values, $duplicate_entry_time ) {
130 // If time for checking duplicates is set to an empty value, don't check for duplicates
131 if ( empty( $duplicate_entry_time ) ) {
132 return false;
133 }
134
135 // If CSV is importing, don't check for duplicates
136 if ( defined( 'WP_IMPORTING' ) && WP_IMPORTING ) {
137 return false;
138 }
139
140 // If repeating field entries are getting created, don't check for duplicates
141 if ( isset( $values['parent_form_id'] ) && $values['parent_form_id'] ) {
142 return false;
143 }
144
145 return true;
146 }
147
148 public static function duplicate( $id ) {
149 global $wpdb;
150
151 $values = self::getOne( $id );
152
153 $new_values = array();
154 $new_values['item_key'] = FrmAppHelper::get_unique_key( '', $wpdb->prefix . 'frm_items', 'item_key' );
155 $new_values['name'] = $values->name;
156 $new_values['is_draft'] = $values->is_draft;
157 $new_values['user_id'] = (int) $values->user_id;
158 $new_values['updated_by'] = (int) $values->user_id;
159 $new_values['form_id'] = $values->form_id ? (int) $values->form_id : null;
160 $new_values['created_at'] = current_time( 'mysql', 1 );
161 $new_values['updated_at'] = $new_values['created_at'];
162
163 $query_results = $wpdb->insert( $wpdb->prefix . 'frm_items', $new_values );
164 if ( ! $query_results ) {
165 return false;
166 }
167
168 $entry_id = $wpdb->insert_id;
169
170 global $frm_vars;
171 if ( ! isset( $frm_vars['saved_entries'] ) ) {
172 $frm_vars['saved_entries'] = array();
173 }
174 $frm_vars['saved_entries'][] = (int) $entry_id;
175
176 FrmEntryMeta::duplicate_entry_metas( $id, $entry_id );
177 self::clear_cache();
178
179 do_action( 'frm_after_duplicate_entry', $entry_id, $new_values['form_id'], array( 'old_id' => $id ) );
180
181 return $entry_id;
182 }
183
184 /**
185 * Update an entry (not via XML)
186 *
187 * @param int $id
188 * @param array $values
189 *
190 * @return boolean|int $update_results
191 */
192 public static function update( $id, $values ) {
193 $update_results = self::update_entry( $id, $values, 'standard' );
194
195 return $update_results;
196 }
197
198 /**
199 * Update an entry with some differences depending on the update type
200 *
201 * @since 2.0.16
202 *
203 * @param int $id
204 * @param array $values
205 *
206 * @return boolean|int $query_results
207 */
208 private static function update_entry( $id, $values, $update_type ) {
209 global $wpdb;
210
211 $update = self::before_update_entry( $id, $values, $update_type );
212 if ( ! $update ) {
213 return false;
214 }
215
216 $new_values = self::package_entry_to_update( $id, $values );
217
218 $query_results = $wpdb->update( $wpdb->prefix . 'frm_items', $new_values, compact( 'id' ) );
219
220 self::after_update_entry( $query_results, $id, $values, $new_values );
221
222 return $query_results;
223 }
224
225 public static function destroy( $id ) {
226 global $wpdb;
227 $id = (int) $id;
228
229 $entry = self::getOne( $id );
230 if ( ! $entry ) {
231 $result = false;
232
233 return $result;
234 }
235
236 do_action( 'frm_before_destroy_entry', $id, $entry );
237
238 $wpdb->query( $wpdb->prepare( 'DELETE FROM ' . $wpdb->prefix . 'frm_item_metas WHERE item_id=%d', $id ) );
239 $result = $wpdb->query( $wpdb->prepare( 'DELETE FROM ' . $wpdb->prefix . 'frm_items WHERE id=%d', $id ) );
240
241 self::clear_cache();
242
243 return $result;
244 }
245
246 public static function update_form( $id, $value, $form_id ) {
247 global $wpdb;
248 $form_id = isset( $value ) ? $form_id : null;
249 $result = $wpdb->update( $wpdb->prefix . 'frm_items', array( 'form_id' => $form_id ), array( 'id' => $id ) );
250 if ( $result ) {
251 self::clear_cache();
252 }
253
254 return $result;
255 }
256
257 /**
258 * Clear entry caching
259 * Called when an entry is changed
260 *
261 * @since 2.0.5
262 */
263 public static function clear_cache() {
264 FrmDb::cache_delete_group( 'frm_entry' );
265 FrmDb::cache_delete_group( 'frm_item' );
266 FrmDb::cache_delete_group( 'frm_entry_meta' );
267 FrmDb::cache_delete_group( 'frm_item_meta' );
268 }
269
270 /**
271 * After switching to the wp_loaded hook for processing entries,
272 * we can no longer use 'name', but check it as a fallback
273 *
274 * @since 2.0.11
275 */
276 public static function get_new_entry_name( $values, $default = '' ) {
277 $name = isset( $values['item_name'] ) ? $values['item_name'] : ( isset( $values['name'] ) ? $values['name'] : $default );
278 if ( is_array( $name ) ) {
279 $name = reset( $name );
280 }
281
282 return $name;
283 }
284
285 /**
286 * If $entry is numeric, get the entry object
287 *
288 * @param int|object $entry by reference
289 *
290 * @since 2.0.9
291 */
292 public static function maybe_get_entry( &$entry ) {
293 if ( $entry && is_numeric( $entry ) ) {
294 $entry = self::getOne( $entry );
295 } elseif ( empty( $entry ) ) {
296 $entry = false;
297 }
298 }
299
300 public static function getOne( $id, $meta = false ) {
301 global $wpdb;
302
303 $query = "SELECT it.*, fr.name as form_name, fr.form_key as form_key FROM {$wpdb->prefix}frm_items it
304 LEFT OUTER JOIN {$wpdb->prefix}frm_forms fr ON it.form_id=fr.id WHERE ";
305
306 $query .= is_numeric( $id ) ? 'it.id=%d' : 'it.item_key=%s';
307 $query_args = array( $id );
308 $query = $wpdb->prepare( $query, $query_args ); // WPCS: unprepared SQL ok.
309
310 if ( ! $meta ) {
311 $entry = FrmDb::check_cache( $id . '_nometa', 'frm_entry', $query, 'get_row' );
312
313 return wp_unslash( $entry );
314 }
315
316 $entry = FrmDb::check_cache( $id, 'frm_entry' );
317 if ( $entry !== false ) {
318 return wp_unslash( $entry );
319 }
320
321 $entry = $wpdb->get_row( $query ); // WPCS: unprepared SQL ok.
322 $entry = self::get_meta( $entry );
323
324 return wp_unslash( $entry );
325 }
326
327 public static function get_meta( $entry ) {
328 if ( ! $entry ) {
329 return $entry;
330 }
331
332 global $wpdb;
333 $metas = FrmDb::get_results(
334 $wpdb->prefix . 'frm_item_metas m LEFT JOIN ' . $wpdb->prefix . 'frm_fields f ON m.field_id=f.id',
335 array(
336 'item_id' => $entry->id,
337 'field_id !' => 0,
338 ),
339 'field_id, meta_value, field_key, item_id'
340 );
341
342 $entry->metas = array();
343
344 $include_key = apply_filters( 'frm_include_meta_keys', false, array( 'form_id' => $entry->form_id ) );
345 foreach ( $metas as $meta_val ) {
346 if ( $meta_val->item_id == $entry->id ) {
347 $entry->metas[ $meta_val->field_id ] = maybe_unserialize( $meta_val->meta_value );
348 if ( $include_key ) {
349 $entry->metas[ $meta_val->field_key ] = $entry->metas[ $meta_val->field_id ];
350 }
351 continue;
352 }
353
354 // include sub entries in an array
355 if ( ! isset( $entry_metas[ $meta_val->field_id ] ) ) {
356 $entry->metas[ $meta_val->field_id ] = array();
357 }
358
359 $entry->metas[ $meta_val->field_id ][] = maybe_unserialize( $meta_val->meta_value );
360
361 unset( $meta_val );
362 }
363 unset( $metas );
364
365 FrmDb::set_cache( $entry->id, $entry, 'frm_entry' );
366
367 return $entry;
368 }
369
370 /**
371 * @param string $id
372 */
373 public static function exists( $id ) {
374 global $wpdb;
375
376 if ( FrmDb::check_cache( $id, 'frm_entry' ) ) {
377 $exists = true;
378
379 return $exists;
380 }
381
382 if ( is_numeric( $id ) ) {
383 $where = array( 'id' => $id );
384 } else {
385 $where = array( 'item_key' => $id );
386 }
387 $id = FrmDb::get_var( $wpdb->prefix . 'frm_items', $where );
388
389 return ( $id && $id > 0 );
390 }
391
392 public static function getAll( $where, $order_by = '', $limit = '', $meta = false, $inc_form = true ) {
393 global $wpdb;
394
395 $limit = FrmDb::esc_limit( $limit );
396
397 $cache_key = maybe_serialize( $where ) . $order_by . $limit . $inc_form;
398 $entries = wp_cache_get( $cache_key, 'frm_entry' );
399
400 if ( false === $entries ) {
401 $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';
402 $table = $wpdb->prefix . 'frm_items it ';
403
404 if ( $inc_form ) {
405 $fields = 'it.*, fr.name as form_name,fr.form_key as form_key';
406 $table .= 'LEFT OUTER JOIN ' . $wpdb->prefix . 'frm_forms fr ON it.form_id=fr.id ';
407 }
408
409 if ( preg_match( '/ meta_([0-9]+)/', $order_by, $order_matches ) ) {
410 // sort by a requested field
411 $field_id = (int) $order_matches[1];
412 $fields .= ', (SELECT meta_value FROM ' . $wpdb->prefix . 'frm_item_metas WHERE field_id = ' . $field_id . ' AND item_id = it.id) as meta_' . $field_id;
413 unset( $order_matches, $field_id );
414 }
415
416 // prepare the query
417 $query = 'SELECT ' . $fields . ' FROM ' . $table . FrmDb::prepend_and_or_where( ' WHERE ', $where ) . $order_by . $limit;
418
419 $entries = $wpdb->get_results( $query, OBJECT_K ); // WPCS: unprepared SQL ok.
420 unset( $query );
421
422 FrmDb::set_cache( $cache_key, $entries, 'frm_entry' );
423 }
424
425 if ( ! $meta || ! $entries ) {
426 return wp_unslash( $entries );
427 }
428 unset( $meta );
429
430 if ( ! is_array( $where ) && preg_match( '/^it\.form_id=\d+$/', $where ) ) {
431 $where = array( 'it.form_id' => substr( $where, 11 ) );
432 }
433
434 $meta_where = array( 'field_id !' => 0 );
435 if ( $limit == '' && is_array( $where ) && count( $where ) == 1 && isset( $where['it.form_id'] ) ) {
436 $meta_where['fi.form_id'] = $where['it.form_id'];
437 } else {
438 $meta_where['item_id'] = array_keys( $entries );
439 }
440
441 $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' );
442
443 unset( $meta_where );
444
445 if ( ! $metas ) {
446 return wp_unslash( $entries );
447 }
448
449 foreach ( $metas as $m_key => $meta_val ) {
450 if ( ! isset( $entries[ $meta_val->item_id ] ) ) {
451 continue;
452 }
453
454 if ( ! isset( $entries[ $meta_val->item_id ]->metas ) ) {
455 $entries[ $meta_val->item_id ]->metas = array();
456 }
457
458 $entries[ $meta_val->item_id ]->metas[ $meta_val->field_id ] = maybe_unserialize( $meta_val->meta_value );
459
460 unset( $m_key, $meta_val );
461 }
462
463 if ( ! FrmAppHelper::prevent_caching() ) {
464 foreach ( $entries as $entry ) {
465 FrmDb::set_cache( $entry->id, $entry, 'frm_entry' );
466 unset( $entry );
467 }
468 }
469
470 return wp_unslash( $entries );
471 }
472
473 // Pagination Methods
474 /**
475 * @param int|array|string If int, use the form id.
476 */
477 public static function getRecordCount( $where = '' ) {
478 global $wpdb;
479 $table_join = $wpdb->prefix . 'frm_items it LEFT OUTER JOIN ' . $wpdb->prefix . 'frm_forms fr ON it.form_id=fr.id';
480
481 if ( is_numeric( $where ) ) {
482 $table_join = 'frm_items';
483 $where = array( 'form_id' => $where );
484 }
485
486 if ( is_array( $where ) ) {
487 $count = FrmDb::get_count( $table_join, $where );
488 } else {
489 $cache_key = 'count_' . maybe_serialize( $where );
490 $query = 'SELECT COUNT(*) FROM ' . $table_join . FrmDb::prepend_and_or_where( ' WHERE ', $where );
491 $count = FrmDb::check_cache( $cache_key, 'frm_entry', $query, 'get_var' );
492 }
493
494 return $count;
495 }
496
497 public static function getPageCount( $p_size, $where = '' ) {
498 $p_size = (int) $p_size;
499 $count = 1;
500 if ( $p_size ) {
501 if ( ! is_numeric( $where ) ) {
502 $where = self::getRecordCount( $where );
503 }
504 $count = ceil( (int) $where / $p_size );
505 }
506
507 return $count;
508 }
509
510 /**
511 * Prepare the data before inserting it into the database
512 *
513 * @since 2.0.16
514 *
515 * @param array $values
516 * @param string $type
517 *
518 * @return array $new_values
519 */
520 private static function before_insert_entry_in_database( &$values, $type ) {
521
522 self::sanitize_entry_post( $values );
523
524 if ( $type != 'xml' ) {
525 $values = apply_filters( 'frm_pre_create_entry', $values );
526 }
527
528 $new_values = self::package_entry_data( $values );
529
530 return $new_values;
531 }
532
533 /**
534 * Create an entry and perform after create actions
535 *
536 * @since 2.0.16
537 *
538 * @param array $values
539 * @param array $new_values
540 *
541 * @return boolean|int $entry_id
542 */
543 private static function continue_to_create_entry( $values, $new_values ) {
544 $entry_id = self::insert_entry_into_database( $new_values );
545 if ( ! $entry_id ) {
546 return false;
547 }
548
549 self::after_insert_entry_in_database( $values, $new_values, $entry_id );
550
551 return $entry_id;
552 }
553
554 /**
555 * Sanitize the POST values before we use them
556 *
557 * @since 2.0
558 *
559 * @param array $values The POST values by reference
560 */
561 public static function sanitize_entry_post( &$values ) {
562 $sanitize_method = array(
563 'form_id' => 'absint',
564 'frm_action' => 'sanitize_title',
565 'form_key' => 'sanitize_title',
566 'item_key' => 'sanitize_title',
567 'item_name' => 'sanitize_text_field',
568 'frm_saving_draft' => 'absint',
569 'is_draft' => 'absint',
570 'post_id' => 'absint',
571 'parent_item_id' => 'absint',
572 'created_at' => 'sanitize_text_field',
573 'updated_at' => 'sanitize_text_field',
574 );
575
576 FrmAppHelper::sanitize_request( $sanitize_method, $values );
577 }
578
579 /**
580 * Prepare the new values for inserting into the database
581 *
582 * @since 2.0.16
583 *
584 * @param array $values
585 *
586 * @return array $new_values
587 */
588 private static function package_entry_data( &$values ) {
589 global $wpdb;
590
591 if ( ! isset( $values['item_key'] ) ) {
592 $values['item_key'] = '';
593 }
594
595 $item_name = self::get_new_entry_name( $values, $values['item_key'] );
596 $new_values = array(
597 'item_key' => FrmAppHelper::get_unique_key( $values['item_key'], $wpdb->prefix . 'frm_items', 'item_key' ),
598 'name' => FrmAppHelper::truncate( $item_name, 255, 1, '' ),
599 'ip' => self::get_ip( $values ),
600 'is_draft' => self::get_is_draft_value( $values ),
601 'form_id' => (int) self::get_entry_value( $values, 'form_id', null ),
602 'post_id' => (int) self::get_entry_value( $values, 'post_id', 0 ),
603 'parent_item_id' => (int) self::get_entry_value( $values, 'parent_item_id', 0 ),
604 'created_at' => self::get_created_at( $values ),
605 'updated_at' => self::get_updated_at( $values ),
606 'description' => self::get_entry_description( $values ),
607 'user_id' => self::get_entry_user_id( $values ),
608 );
609
610 $new_values['updated_by'] = isset( $values['updated_by'] ) ? $values['updated_by'] : $new_values['user_id'];
611
612 return $new_values;
613 }
614
615 private static function get_entry_value( $values, $name, $default ) {
616 return isset( $values[ $name ] ) ? $values[ $name ] : $default;
617 }
618
619 /**
620 * Get the ip for a new entry.
621 * Allow the import to override the value.
622 *
623 * @since 2.03.10
624 *
625 * @param array $values
626 *
627 * @return string
628 */
629 private static function get_ip( $values ) {
630 if ( ! FrmAppHelper::ips_saved() ) {
631 return '';
632 }
633
634 $ip = FrmAppHelper::get_ip_address();
635 if ( defined( 'WP_IMPORTING' ) && WP_IMPORTING ) {
636 $ip = self::get_entry_value( $values, 'ip', $ip );
637 }
638
639 return $ip;
640 }
641
642 /**
643 * Get the is_draft value for a new entry
644 *
645 * @since 2.0.16
646 *
647 * @param array $values
648 *
649 * @return int
650 */
651 private static function get_is_draft_value( $values ) {
652 return ( ( isset( $values['frm_saving_draft'] ) && $values['frm_saving_draft'] == 1 ) || ( isset( $values['is_draft'] ) && $values['is_draft'] == 1 ) ) ? 1 : 0;
653 }
654
655 /**
656 * Get the created_at value for a new entry
657 *
658 * @since 2.0.16
659 *
660 * @param array $values
661 *
662 * @return string
663 */
664 private static function get_created_at( $values ) {
665 return self::get_entry_value( $values, 'created_at', current_time( 'mysql', 1 ) );
666 }
667
668 /**
669 * Get the updated_at value for a new entry
670 *
671 * @since 2.0.16
672 *
673 * @param array $values
674 *
675 * @return string
676 */
677 private static function get_updated_at( $values ) {
678 if ( isset( $values['updated_at'] ) ) {
679 $updated_at = $values['updated_at'];
680 } else {
681 $updated_at = self::get_created_at( $values );
682 }
683
684 return $updated_at;
685 }
686
687 /**
688 * Get the description value for a new entry
689 *
690 * @since 2.0.16
691 *
692 * @param array $values
693 *
694 * @return string
695 */
696 private static function get_entry_description( $values ) {
697 if ( isset( $values['description'] ) && ! empty( $values['description'] ) ) {
698 $description = maybe_serialize( $values['description'] );
699 } else {
700 $description = serialize(
701 array(
702 'browser' => FrmAppHelper::get_server_value( 'HTTP_USER_AGENT' ),
703 'referrer' => FrmAppHelper::get_server_value( 'HTTP_REFERER' ),
704 )
705 );
706 }
707
708 return $description;
709 }
710
711 /**
712 * Get the user_id value for a new entry
713 *
714 * @since 2.0.16
715 *
716 * @param array $values
717 *
718 * @return int
719 */
720 private static function get_entry_user_id( $values ) {
721 if ( isset( $values['frm_user_id'] ) && ( is_numeric( $values['frm_user_id'] ) || FrmAppHelper::is_admin() ) ) {
722 $user_id = $values['frm_user_id'];
723 } else {
724 $current_user_id = get_current_user_id();
725 $user_id = $current_user_id ? $current_user_id : 0;
726 }
727
728 return $user_id;
729 }
730
731 /**
732 * Insert new entry into the database
733 *
734 * @since 2.0.16
735 *
736 * @param array $new_values
737 *
738 * @return int | boolean $entry_id
739 */
740 private static function insert_entry_into_database( $new_values ) {
741 global $wpdb;
742
743 $query_results = $wpdb->insert( $wpdb->prefix . 'frm_items', $new_values );
744
745 if ( ! $query_results ) {
746 $entry_id = false;
747 } else {
748 $entry_id = $wpdb->insert_id;
749 }
750
751 return $entry_id;
752 }
753
754 /**
755 * Add the new entry to global $frm_vars
756 *
757 * @since 2.0.16
758 *
759 * @param int $entry_id
760 */
761 private static function add_new_entry_to_frm_vars( $entry_id ) {
762 global $frm_vars;
763
764 if ( ! isset( $frm_vars['saved_entries'] ) ) {
765 $frm_vars['saved_entries'] = array();
766 }
767
768 $frm_vars['saved_entries'][] = (int) $entry_id;
769 }
770
771 /**
772 * Add entry metas, if there are any
773 *
774 * @since 2.0.16
775 *
776 * @param array $values
777 * @param int $entry_id
778 */
779 private static function maybe_add_entry_metas( $values, $entry_id ) {
780 if ( isset( $values['item_meta'] ) ) {
781 FrmEntryMeta::update_entry_metas( $entry_id, $values['item_meta'] );
782 }
783 }
784
785 /**
786 * Trigger frm_after_create_entry hooks
787 *
788 * @since 2.0.16
789 *
790 * @param int $entry_id
791 * @param array $new_values
792 */
793 private static function after_entry_created_actions( $entry_id, $values, $new_values ) {
794 // this is a child entry
795 $is_child = isset( $values['parent_form_id'] ) && isset( $values['parent_nonce'] ) && ! empty( $values['parent_form_id'] ) && wp_verify_nonce( $values['parent_nonce'], 'parent' );
796
797 do_action( 'frm_after_create_entry', $entry_id, $new_values['form_id'], compact( 'is_child' ) );
798 do_action( 'frm_after_create_entry_' . $new_values['form_id'], $entry_id, compact( 'is_child' ) );
799 }
800
801 /**
802 * Actions to perform immediately after an entry is inserted in the frm_items database
803 *
804 * @since 2.0.16
805 *
806 * @param array $values
807 * @param array $new_values
808 * @param int $entry_id
809 */
810 private static function after_insert_entry_in_database( $values, $new_values, $entry_id ) {
811
812 self::add_new_entry_to_frm_vars( $entry_id );
813
814 self::maybe_add_entry_metas( $values, $entry_id );
815
816 self::clear_cache();
817
818 self::after_entry_created_actions( $entry_id, $values, $new_values );
819 }
820
821 /**
822 * Perform some actions right before updating an entry
823 *
824 * @since 2.0.16
825 *
826 * @param int $id
827 * @param array $values
828 * @param string $update_type
829 *
830 * @return boolean $update
831 */
832 private static function before_update_entry( $id, &$values, $update_type ) {
833 $update = true;
834
835 global $frm_vars;
836
837 if ( isset( $frm_vars['saved_entries'] ) && is_array( $frm_vars['saved_entries'] ) && in_array( (int) $id, (array) $frm_vars['saved_entries'] ) ) {
838 $update = false;
839 }
840
841 if ( $update && $update_type != 'xml' ) {
842 $values = apply_filters( 'frm_pre_update_entry', $values, $id );
843 }
844
845 return $update;
846 }
847
848 /**
849 * Package the entry data for updating
850 *
851 * @since 2.0.16
852 *
853 * @param int $id
854 * @param array $values
855 *
856 * @return array $new_values
857 */
858 private static function package_entry_to_update( $id, $values ) {
859 global $wpdb;
860
861 $new_values = array(
862 'name' => self::get_new_entry_name( $values ),
863 'form_id' => (int) self::get_entry_value( $values, 'form_id', null ),
864 'is_draft' => self::get_is_draft_value( $values ),
865 'updated_at' => current_time( 'mysql', 1 ),
866 'updated_by' => isset( $values['updated_by'] ) ? $values['updated_by'] : get_current_user_id(),
867 );
868
869 if ( isset( $values['post_id'] ) ) {
870 $new_values['post_id'] = (int) $values['post_id'];
871 }
872
873 if ( isset( $values['item_key'] ) ) {
874 $new_values['item_key'] = FrmAppHelper::get_unique_key( $values['item_key'], $wpdb->prefix . 'frm_items', 'item_key', $id );
875 }
876
877 if ( isset( $values['parent_item_id'] ) ) {
878 $new_values['parent_item_id'] = (int) $values['parent_item_id'];
879 }
880
881 if ( isset( $values['frm_user_id'] ) && is_numeric( $values['frm_user_id'] ) ) {
882 $new_values['user_id'] = $values['frm_user_id'];
883 }
884
885 $new_values = apply_filters( 'frm_update_entry', $new_values, $id );
886
887 return $new_values;
888 }
889
890 /**
891 * Perform some actions right after updating an entry
892 *
893 * @since 2.0.16
894 *
895 * @param boolean|int $query_results
896 * @param int $id
897 * @param array $values
898 * @param array $new_values
899 */
900 private static function after_update_entry( $query_results, $id, $values, $new_values ) {
901 if ( $query_results ) {
902 self::clear_cache();
903 }
904
905 global $frm_vars;
906 if ( ! isset( $frm_vars['saved_entries'] ) ) {
907 $frm_vars['saved_entries'] = array();
908 }
909
910 $frm_vars['saved_entries'][] = (int) $id;
911
912 if ( isset( $values['item_meta'] ) ) {
913 FrmEntryMeta::update_entry_metas( $id, $values['item_meta'] );
914 }
915
916 do_action( 'frm_after_update_entry', $id, $new_values['form_id'] );
917 do_action( 'frm_after_update_entry_' . $new_values['form_id'], $id );
918 }
919
920 /**
921 * Create entry from an XML import
922 * Certain actions aren't necessary when importing (like saving sub entries, checking for duplicates, etc.)
923 *
924 * @since 2.0.16
925 *
926 * @param array $values
927 *
928 * @return int | boolean $entry_id
929 */
930 public static function create_entry_from_xml( $values ) {
931 $entry_id = self::create_entry( $values, 'xml' );
932
933 return $entry_id;
934 }
935
936 /**
937 * Update entry from an XML import
938 * Certain actions aren't necessary when importing (like saving sub entries and modifying other vals)
939 *
940 * @since 2.0.16
941 *
942 * @param int $id
943 * @param array $values
944 *
945 * @return int | boolean $updated
946 */
947 public static function update_entry_from_xml( $id, $values ) {
948 $updated = self::update_entry( $id, $values, 'xml' );
949
950 return $updated;
951 }
952
953 /**
954 * @param string $key
955 *
956 * @return int entry_id
957 */
958 public static function get_id_by_key( $key ) {
959 $entry_id = FrmDb::get_var( 'frm_items', array( 'item_key' => sanitize_title( $key ) ) );
960
961 return (int) $entry_id;
962 }
963 }
964