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

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