PluginProbe
Formidable Forms – WordPress Form Builder for Contact Forms, Calculators, Quizzes & More / 4.08
Formidable Forms – WordPress Form Builder for Contact Forms, Calculators, Quizzes & More v4.08
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 / controllers / FrmEntriesController.php

FrmEntriesController.php in Formidable Forms – WordPress Form Builder for Contact Forms, Calculators, Quizzes & More 4.08, at classes/controllers/FrmEntriesController.php

675 lines 18.8 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 FrmEntriesController {
7
8 public static function menu() {
9 FrmAppHelper::force_capability( 'frm_view_entries' );
10
11 add_submenu_page( 'formidable', 'Formidable | ' . __( 'Entries', 'formidable' ), __( 'Entries', 'formidable' ), 'frm_view_entries', 'formidable-entries', 'FrmEntriesController::route' );
12
13 if ( ! FrmAppHelper::pro_is_installed() ) {
14 add_submenu_page( 'formidable', 'Formidable | ' . __( 'Views', 'formidable' ), __( 'Views', 'formidable' ), 'frm_view_entries', 'formidable-views', 'FrmFormsController::no_views' );
15 }
16
17 if ( FrmAppHelper::is_admin_page( 'formidable-entries' ) ) {
18 self::load_manage_entries_hooks();
19 }
20 }
21
22 /**
23 * @since 2.05.07
24 */
25 private static function load_manage_entries_hooks() {
26 if ( ! in_array( FrmAppHelper::simple_get( 'frm_action', 'sanitize_title' ), array( 'edit', 'show', 'new' ) ) ) {
27 $menu_name = FrmAppHelper::get_menu_name();
28 $base = self::base_column_key( $menu_name );
29
30 add_filter( 'manage_' . $base . '_columns', 'FrmEntriesController::manage_columns' );
31 add_filter( 'get_user_option_' . self::hidden_column_key( $menu_name ), 'FrmEntriesController::hidden_columns' );
32 add_filter( 'manage_' . $base . '_sortable_columns', 'FrmEntriesController::sortable_columns' );
33 } else {
34 add_filter( 'screen_options_show_screen', __CLASS__ . '::remove_screen_options', 10, 2 );
35 }
36 }
37
38 /* Display in Back End */
39 public static function route() {
40 $action = FrmAppHelper::get_param( 'frm_action', '', 'get', 'sanitize_title' );
41 FrmAppHelper::include_svg();
42
43 switch ( $action ) {
44 case 'show':
45 case 'destroy':
46 return self::$action();
47
48 default:
49 do_action( 'frm_entry_action_route', $action );
50 if ( apply_filters( 'frm_entry_stop_action_route', false, $action ) ) {
51 return;
52 }
53
54 return self::display_list();
55 }
56 }
57
58 /**
59 * Prevent the "screen options" tab from showing when
60 * editing or creating an entry
61 *
62 * @since 3.0
63 */
64 public static function remove_screen_options( $show_screen, $screen ) {
65 $menu_name = sanitize_title( FrmAppHelper::get_menu_name() );
66 if ( $screen->id == $menu_name . '_page_formidable-entries' ) {
67 $show_screen = false;
68 }
69
70 return $show_screen;
71 }
72
73 public static function manage_columns( $columns ) {
74 global $frm_vars;
75 $form_id = FrmForm::get_current_form_id();
76
77 $columns[ $form_id . '_id' ] = 'ID';
78 $columns[ $form_id . '_item_key' ] = esc_html__( 'Entry Key', 'formidable' );
79
80 if ( $form_id ) {
81 self::get_columns_for_form( $form_id, $columns );
82 } else {
83 $columns[ $form_id . '_form_id' ] = __( 'Form', 'formidable' );
84 $columns[ $form_id . '_name' ] = __( 'Entry Name', 'formidable' );
85 $columns[ $form_id . '_user_id' ] = __( 'Created By', 'formidable' );
86 }
87
88 $columns[ $form_id . '_created_at' ] = __( 'Entry creation date', 'formidable' );
89 $columns[ $form_id . '_updated_at' ] = __( 'Entry update date', 'formidable' );
90 self::maybe_add_ip_col( $form_id, $columns );
91
92 $frm_vars['cols'] = $columns;
93
94 $action = FrmAppHelper::simple_get( 'frm_action', 'sanitize_title' );
95 if ( FrmAppHelper::is_admin_page( 'formidable-entries' ) && in_array( $action, array( '', 'list', 'destroy' ) ) ) {
96 add_screen_option(
97 'per_page',
98 array(
99 'label' => __( 'Entries', 'formidable' ),
100 'default' => 20,
101 'option' => 'formidable_page_formidable_entries_per_page',
102 )
103 );
104 }
105
106 return $columns;
107 }
108
109 private static function get_columns_for_form( $form_id, &$columns ) {
110 $form_cols = FrmField::get_all_for_form( $form_id, '', 'include' );
111
112 foreach ( $form_cols as $form_col ) {
113 if ( FrmField::is_no_save_field( $form_col->type ) ) {
114 continue;
115 }
116
117 $has_child_fields = $form_col->type == 'form' && isset( $form_col->field_options['form_select'] ) && ! empty( $form_col->field_options['form_select'] );
118 if ( $has_child_fields ) {
119 self::add_subform_cols( $form_col, $form_id, $columns );
120 } else {
121 self::add_field_cols( $form_col, $form_id, $columns );
122 }
123 }
124 }
125
126 /**
127 * @since 3.01
128 */
129 private static function add_subform_cols( $field, $form_id, &$columns ) {
130 $sub_form_cols = FrmField::get_all_for_form( $field->field_options['form_select'] );
131 if ( empty( $sub_form_cols ) ) {
132 return;
133 }
134
135 foreach ( $sub_form_cols as $k => $sub_form_col ) {
136 if ( FrmField::is_no_save_field( $sub_form_col->type ) ) {
137 unset( $sub_form_cols[ $k ] );
138 continue;
139 }
140 $columns[ $form_id . '_' . $sub_form_col->field_key . '-_-' . $field->id ] = FrmAppHelper::truncate( $sub_form_col->name, 35 );
141 unset( $sub_form_col );
142 }
143 }
144
145 /**
146 * @since 3.01
147 */
148 private static function add_field_cols( $field, $form_id, &$columns ) {
149 $col_id = $field->field_key;
150 if ( $field->form_id != $form_id ) {
151 $col_id .= '-_-form' . $field->form_id;
152 }
153
154 $has_separate_value = ! FrmField::is_option_empty( $field, 'separate_value' );
155 $is_post_status = FrmField::is_option_true( $field, 'post_field' ) && $field->field_options['post_field'] == 'post_status';
156 if ( $has_separate_value && ! $is_post_status ) {
157 $columns[ $form_id . '_frmsep_' . $col_id ] = FrmAppHelper::truncate( $field->name, 35 );
158 }
159
160 $columns[ $form_id . '_' . $col_id ] = FrmAppHelper::truncate( $field->name, 35 );
161 }
162
163 private static function maybe_add_ip_col( $form_id, &$columns ) {
164 if ( FrmAppHelper::ips_saved() ) {
165 $columns[ $form_id . '_ip' ] = 'IP';
166 }
167 }
168
169 public static function check_hidden_cols( $check, $object_id, $meta_key, $meta_value, $prev_value ) {
170 $this_page_name = self::hidden_column_key();
171 if ( $meta_key != $this_page_name || $meta_value == $prev_value ) {
172 return $check;
173 }
174
175 if ( empty( $prev_value ) ) {
176 $prev_value = get_metadata( 'user', $object_id, $meta_key, true );
177 }
178
179 global $frm_vars;
180 //add a check so we don't create a loop
181 $frm_vars['prev_hidden_cols'] = ( isset( $frm_vars['prev_hidden_cols'] ) && $frm_vars['prev_hidden_cols'] ) ? false : $prev_value;
182
183 return $check;
184 }
185
186 /**
187 * Add hidden columns back from other forms
188 */
189 public static function update_hidden_cols( $meta_id, $object_id, $meta_key, $meta_value ) {
190 $this_page_name = self::hidden_column_key();
191 if ( $meta_key != $this_page_name ) {
192 return;
193 }
194
195 global $frm_vars;
196 if ( ! isset( $frm_vars['prev_hidden_cols'] ) || ! $frm_vars['prev_hidden_cols'] ) {
197 return; // Don't continue if there's no previous value.
198 }
199
200 foreach ( $meta_value as $mk => $mv ) {
201 // Remove blank values.
202 if ( empty( $mv ) ) {
203 unset( $meta_value[ $mk ] );
204 }
205 }
206
207 $cur_form_prefix = reset( $meta_value );
208 $cur_form_prefix = explode( '_', $cur_form_prefix );
209 $cur_form_prefix = $cur_form_prefix[0];
210 $save = false;
211
212 foreach ( (array) $frm_vars['prev_hidden_cols'] as $prev_hidden ) {
213 if ( empty( $prev_hidden ) || in_array( $prev_hidden, $meta_value ) ) {
214 // Don't add blank cols or process included cols.
215 continue;
216 }
217
218 $form_prefix = explode( '_', $prev_hidden );
219 $form_prefix = $form_prefix[0];
220 if ( $form_prefix == $cur_form_prefix ) {
221 // Don't add back columns that are meant to be hidden.
222 continue;
223 }
224
225 $meta_value[] = $prev_hidden;
226 $save = true;
227 unset( $form_prefix );
228 }
229
230 if ( $save ) {
231 $user_id = get_current_user_id();
232 update_user_option( $user_id, $this_page_name, $meta_value, true );
233 }
234 }
235
236 /**
237 * @since 2.05.07
238 */
239 private static function hidden_column_key( $menu_name = '' ) {
240 $base = self::base_column_key( $menu_name );
241
242 return 'manage' . $base . 'columnshidden';
243 }
244
245 /**
246 * @since 2.05.07
247 */
248 private static function base_column_key( $menu_name = '' ) {
249 if ( empty( $menu_name ) ) {
250 $menu_name = FrmAppHelper::get_menu_name();
251 }
252
253 return sanitize_title( $menu_name ) . '_page_formidable-entries';
254 }
255
256 public static function save_per_page( $save, $option, $value ) {
257 if ( $option == 'formidable_page_formidable_entries_per_page' ) {
258 $save = (int) $value;
259 }
260
261 return $save;
262 }
263
264 public static function sortable_columns() {
265 $form_id = FrmForm::get_current_form_id();
266 $fields = FrmField::get_all_for_form( $form_id );
267
268 $columns = array(
269 $form_id . '_id' => 'id',
270 $form_id . '_created_at' => 'created_at',
271 $form_id . '_updated_at' => 'updated_at',
272 $form_id . '_ip' => 'ip',
273 $form_id . '_item_key' => 'item_key',
274 $form_id . '_is_draft' => 'is_draft',
275 );
276
277 foreach ( $fields as $field ) {
278 if ( $field->type != 'checkbox' && ( ! isset( $field->field_options['post_field'] ) || $field->field_options['post_field'] == '' ) ) {
279 // Can't sort on checkboxes because they are stored serialized, or post fields
280 $columns[ $form_id . '_' . $field->field_key ] = 'meta_' . $field->id;
281 }
282 }
283
284 return $columns;
285 }
286
287 public static function hidden_columns( $result ) {
288 $form_id = FrmForm::get_current_form_id();
289
290 $hidden = self::user_hidden_columns_for_form( $form_id, $result );
291
292 global $frm_vars;
293 $i = isset( $frm_vars['cols'] ) ? count( $frm_vars['cols'] ) : 0;
294
295 if ( ! empty( $hidden ) ) {
296 $result = $hidden;
297 $i = $i - count( $result );
298 $max_columns = 11;
299 } else {
300 $max_columns = 8;
301 }
302
303 if ( $i <= $max_columns ) {
304 return $result;
305 }
306
307 self::remove_excess_cols( compact( 'i', 'max_columns', 'form_id' ), $result );
308
309 return $result;
310 }
311
312 /**
313 * @since 2.05.07
314 */
315 private static function user_hidden_columns_for_form( $form_id, $result ) {
316 $hidden = array();
317 foreach ( (array) $result as $r ) {
318 if ( ! empty( $r ) ) {
319 list( $form_prefix, $field_key ) = explode( '_', $r );
320
321 if ( (int) $form_prefix == (int) $form_id ) {
322 $hidden[] = $r;
323 }
324
325 unset( $form_prefix );
326 }
327 }
328
329 return $hidden;
330 }
331
332 /**
333 * Remove some columns by default when there are too many
334 *
335 * @since 2.05.07
336 */
337 private static function remove_excess_cols( $atts, &$result ) {
338 global $frm_vars;
339
340 $remove_first = array(
341 $atts['form_id'] . '_item_key' => '',
342 $atts['form_id'] . '_id' => '',
343 );
344 $cols = $remove_first + array_reverse( $frm_vars['cols'], true );
345
346 $i = $atts['i'];
347
348 foreach ( $cols as $col_key => $col ) {
349 if ( $i <= $atts['max_columns'] ) {
350 break;
351 }
352
353 if ( empty( $result ) || ! in_array( $col_key, $result, true ) ) {
354 $result[] = $col_key;
355 $i--;
356 }
357
358 unset( $col_key, $col );
359 }
360 }
361
362 public static function display_list( $message = '', $errors = array() ) {
363 global $wpdb, $frm_vars;
364
365 $form = FrmForm::maybe_get_current_form();
366 $params = FrmForm::get_admin_params( $form );
367
368 if ( $form ) {
369 $params['form'] = $form->id;
370 $frm_vars['current_form'] = $form;
371
372 self::get_delete_form_time( $form, $errors );
373 }
374
375 $table_class = apply_filters( 'frm_entries_list_class', 'FrmEntriesListHelper' );
376
377 $wp_list_table = new $table_class( array( 'params' => $params ) );
378
379 $pagenum = $wp_list_table->get_pagenum();
380
381 $wp_list_table->prepare_items();
382
383 $total_pages = $wp_list_table->get_pagination_arg( 'total_pages' );
384 if ( $pagenum > $total_pages && $total_pages > 0 ) {
385 $url = add_query_arg( 'paged', $total_pages );
386 if ( headers_sent() ) {
387 echo FrmAppHelper::js_redirect( $url ); // WPCS: XSS ok.
388 } else {
389 wp_redirect( esc_url_raw( $url ) );
390 }
391 die();
392 }
393
394 if ( empty( $message ) && isset( $_GET['import-message'] ) ) {
395 $message = __( 'Your import is complete', 'formidable' );
396 }
397
398 require( FrmAppHelper::plugin_path() . '/classes/views/frm-entries/list.php' );
399 }
400
401 private static function get_delete_form_time( $form, &$errors ) {
402 if ( 'trash' == $form->status ) {
403 $delete_timestamp = time() - ( DAY_IN_SECONDS * EMPTY_TRASH_DAYS );
404 $time_to_delete = FrmAppHelper::human_time_diff( $delete_timestamp, ( isset( $form->options['trash_time'] ) ? ( $form->options['trash_time'] ) : time() ) );
405
406 /* translators: %1$s: Time string */
407 $errors['trash'] = sprintf( __( 'This form is in the trash and is scheduled to be deleted permanently in %s along with any entries.', 'formidable' ), $time_to_delete );
408 }
409 }
410
411 /* Back End CRUD */
412 public static function show( $id = 0 ) {
413 FrmAppHelper::permission_check( 'frm_view_entries' );
414
415 if ( ! $id ) {
416 $id = FrmAppHelper::get_param( 'id', 0, 'get', 'absint' );
417
418 if ( ! $id ) {
419 $id = FrmAppHelper::get_param( 'item_id', 0, 'get', 'absint' );
420 }
421 }
422
423 $entry = FrmEntry::getOne( $id, true );
424 if ( ! $entry ) {
425 echo '<div id="form_show_entry_page" class="wrap">' .
426 esc_html__( 'You are trying to view an entry that does not exist.', 'formidable' ) .
427 '</div>';
428
429 return;
430 }
431
432 $data = $entry->description;
433 if ( ! is_array( $data ) || ! isset( $data['referrer'] ) ) {
434 $data = array( 'referrer' => $data );
435 }
436
437 $fields = FrmField::get_all_for_form( $entry->form_id, '', 'include' );
438 $form = FrmForm::getOne( $entry->form_id );
439
440 include( FrmAppHelper::plugin_path() . '/classes/views/frm-entries/show.php' );
441 }
442
443 public static function destroy() {
444 FrmAppHelper::permission_check( 'frm_delete_entries' );
445
446 $params = FrmForm::get_admin_params();
447
448 if ( isset( $params['keep_post'] ) && $params['keep_post'] ) {
449 self::unlink_post( $params['id'] );
450 }
451
452 $message = '';
453 if ( FrmEntry::destroy( $params['id'] ) ) {
454 $message = __( 'Entry was Successfully Deleted', 'formidable' );
455 }
456
457 self::display_list( $message );
458 }
459
460 /**
461 * @deprecated 4.02.04 - Moved to Pro since it was unused in Lite.
462 */
463 public static function destroy_all() {
464 _deprecated_function( __METHOD__, '4.02.04', 'FrmProEntriesController::destroy_all' );
465 if ( is_callable( 'FrmProEntriesController::destroy_all' ) ) {
466 FrmProEntriesController::destroy_all();
467 }
468 }
469
470 public static function process_entry( $errors = '', $ajax = false ) {
471 $form_id = FrmAppHelper::get_post_param( 'form_id', '', 'absint' );
472 if ( FrmAppHelper::is_admin() || empty( $_POST ) || empty( $form_id ) || ! isset( $_POST['item_key'] ) ) {
473 return;
474 }
475
476 global $frm_vars;
477
478 $form = FrmForm::getOne( $form_id );
479 if ( ! $form ) {
480 return;
481 }
482
483 $params = FrmForm::get_params( $form );
484
485 if ( ! isset( $frm_vars['form_params'] ) ) {
486 $frm_vars['form_params'] = array();
487 }
488 $frm_vars['form_params'][ $form->id ] = $params;
489
490 if ( isset( $frm_vars['created_entries'][ $form_id ] ) ) {
491 return;
492 }
493
494 if ( $errors == '' && ! $ajax ) {
495 $errors = FrmEntryValidate::validate( wp_unslash( $_POST ) );
496 }
497
498 /**
499 * Use this filter to add trigger actions and add errors after
500 * all other errors have been processed
501 *
502 * @since 2.0.6
503 */
504 $errors = apply_filters( 'frm_entries_before_create', $errors, $form );
505
506 $frm_vars['created_entries'][ $form_id ] = array( 'errors' => $errors );
507
508 if ( empty( $errors ) ) {
509 $_POST['frm_skip_cookie'] = 1;
510 $do_success = false;
511 if ( $params['action'] == 'create' ) {
512 if ( apply_filters( 'frm_continue_to_create', true, $form_id ) && ! isset( $frm_vars['created_entries'][ $form_id ]['entry_id'] ) ) {
513 $frm_vars['created_entries'][ $form_id ]['entry_id'] = FrmEntry::create( $_POST );
514
515 $params['id'] = $frm_vars['created_entries'][ $form_id ]['entry_id'];
516 $do_success = true;
517 }
518 }
519
520 do_action( 'frm_process_entry', $params, $errors, $form, array( 'ajax' => $ajax ) );
521 if ( $do_success ) {
522 FrmFormsController::maybe_trigger_redirect( $form, $params, array( 'ajax' => $ajax ) );
523 }
524 unset( $_POST['frm_skip_cookie'] );
525 }
526 }
527
528 /**
529 * Escape url entities before redirect
530 *
531 * @since 3.0
532 *
533 * @param string $url
534 *
535 * @return string
536 */
537 public static function prepare_redirect_url( $url ) {
538 return str_replace( array( ' ', '[', ']', '|', '@' ), array( '%20', '%5B', '%5D', '%7C', '%40' ), $url );
539 }
540
541 public static function delete_entry_before_redirect( $url, $form, $atts ) {
542 self::_delete_entry( $atts['id'], $form );
543
544 return $url;
545 }
546
547 /**
548 * Delete entry if not redirected.
549 *
550 * @param array $atts
551 */
552 public static function delete_entry_after_save( $atts ) {
553 self::_delete_entry( $atts['entry_id'], $atts['form'] );
554 }
555
556 private static function _delete_entry( $entry_id, $form ) {
557 if ( ! $form ) {
558 return;
559 }
560
561 FrmAppHelper::unserialize_or_decode( $form->options );
562 if ( isset( $form->options['no_save'] ) && $form->options['no_save'] ) {
563 self::unlink_post( $entry_id );
564 FrmEntry::destroy( $entry_id );
565 }
566 }
567
568 /**
569 * Unlink entry from post
570 */
571 private static function unlink_post( $entry_id ) {
572 global $wpdb;
573 $wpdb->update( $wpdb->prefix . 'frm_items', array( 'post_id' => '' ), array( 'id' => $entry_id ) );
574 FrmEntry::clear_cache();
575 }
576
577 /**
578 * @param $atts
579 *
580 * @return array|string
581 */
582 public static function show_entry_shortcode( $atts ) {
583 $defaults = array(
584 'id' => false,
585 'entry' => false,
586 'fields' => false,
587 'plain_text' => false,
588 'user_info' => false,
589 'include_blank' => false,
590 'default_email' => false,
591 'form_id' => false,
592 'format' => 'text',
593 'array_key' => 'key',
594 'direction' => 'ltr',
595 'font_size' => '',
596 'text_color' => '',
597 'border_width' => '',
598 'border_color' => '',
599 'bg_color' => '',
600 'alt_bg_color' => '',
601 'class' => '',
602 'clickable' => false,
603 'exclude_fields' => '',
604 'include_fields' => '',
605 'include_extras' => '',
606 'inline_style' => 1,
607 'child_array' => false, // return embedded fields as nested array
608 'line_breaks' => true,
609 );
610 $defaults = apply_filters( 'frm_show_entry_defaults', $defaults );
611
612 $atts = shortcode_atts( $defaults, $atts );
613
614 if ( $atts['default_email'] ) {
615 $shortcode_atts = array(
616 'format' => $atts['format'],
617 'plain_text' => $atts['plain_text'],
618 );
619
620 $entry_formatter = FrmEntryFactory::entry_shortcode_formatter_instance( $atts['form_id'], $shortcode_atts );
621 $formatted_entry = $entry_formatter->content();
622
623 } else {
624
625 $entry_formatter = FrmEntryFactory::entry_formatter_instance( $atts );
626 $formatted_entry = $entry_formatter->get_formatted_entry_values();
627
628 }
629
630 return $formatted_entry;
631 }
632
633 public static function entry_sidebar( $entry = false ) {
634 $data = array();
635 $id = 0;
636
637 $date_format = get_option( 'date_format' );
638 $time_format = get_option( 'time_format' );
639
640 if ( $entry ) {
641 $id = $entry->id;
642 $data = $entry->description;
643 if ( isset( $data['browser'] ) ) {
644 $browser = FrmEntriesHelper::get_browser( $data['browser'] );
645 }
646 }
647
648 include( FrmAppHelper::plugin_path() . '/classes/views/frm-entries/sidebar-shared.php' );
649 }
650
651 /**
652 * @deprecated 4.0
653 */
654 public static function contextual_help( $help, $screen_id, $screen ) {
655 _deprecated_function( __METHOD__, '4.0' );
656 return $help;
657 }
658
659 /**
660 * @deprecated 1.07.05
661 * @codeCoverageIgnore
662 */
663 public static function show_form( $id = '', $key = '', $title = false, $description = false ) {
664 return FrmDeprecated::show_form( $id, $key, $title, $description );
665 }
666
667 /**
668 * @deprecated 1.07.05
669 * @codeCoverageIgnore
670 */
671 public static function get_form( $filename, $form, $title, $description ) {
672 return FrmDeprecated::get_form( $filename, $form, $title, $description );
673 }
674 }
675