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

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