Completed
Push — master ( cc771e...1305f3 )
by Stephanie
02:46
created

FrmEntriesController::entry_sidebar()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 7
nc 2
nop 1
dl 0
loc 10
rs 9.4285
c 0
b 0
f 0
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
		self::load_manage_entries_hooks();
11
	}
12
13
	/**
14
	 * @since 2.05.07
15
	 */
16
	private static function load_manage_entries_hooks() {
17
		if ( ! in_array( FrmAppHelper::simple_get( 'frm_action', 'sanitize_title' ), array( 'edit', 'show', 'new' ) ) ) {
18
			$menu_name = FrmAppHelper::get_menu_name();
19
			$base = self::base_column_key( $menu_name );
20
21
			add_filter( 'manage_' . $base . '_columns', 'FrmEntriesController::manage_columns' );
22
			add_filter( 'get_user_option_' . self::hidden_column_key( $menu_name ), 'FrmEntriesController::hidden_columns' );
23
			add_filter( 'manage_' . $base . '_sortable_columns', 'FrmEntriesController::sortable_columns' );
24
		} else {
25
			add_filter( 'screen_options_show_screen', __CLASS__ . '::remove_screen_options', 10, 2 );
26
		}
27
	}
28
29
    /* Display in Back End */
30
    public static function route() {
31
		$action = FrmAppHelper::get_param( 'frm_action', '', 'get', 'sanitize_title' );
32
33
        switch ( $action ) {
34
            case 'show':
35
            case 'destroy':
36
            case 'destroy_all':
37
                return self::$action();
38
39
            default:
40
                do_action( 'frm_entry_action_route', $action );
41
                if ( apply_filters( 'frm_entry_stop_action_route', false, $action ) ) {
42
                    return;
43
                }
44
45
                return self::display_list();
46
        }
47
    }
48
49
	public static function contextual_help( $help, $screen_id, $screen ) {
50
        // Only add to certain screens. add_help_tab was introduced in WordPress 3.3
51
        if ( ! method_exists( $screen, 'add_help_tab' ) ) {
52
            return $help;
53
        }
54
55
		$action = FrmAppHelper::simple_get( 'frm_action', 'sanitize_title' );
56
		$page = FrmAppHelper::simple_get( 'page', 'sanitize_title' );
57
		$show_help = ( $page == 'formidable-entries' && ( empty( $action ) || $action == 'list' ) );
58
		if ( ! $show_help ) {
59
            return $help;
60
        }
61
62
		unset( $action, $page );
63
64
        $screen->add_help_tab( array(
65
            'id'      => 'formidable-entries-tab',
66
            'title'   => __( 'Overview', 'formidable' ),
67
			'content' => '<p>' . esc_html__( 'This screen provides access to all of your entries. You can customize the display of this screen to suit your workflow.', 'formidable' ) . '</p> <p>' . esc_html__( 'Hovering over a row in the entries list will display action links that allow you to manage your entry.', 'formidable' ) . '</p>',
68
        ));
69
70
        $screen->set_help_sidebar(
71
			'<p><strong>' . esc_html__( 'For more information:', 'formidable' ) . '</strong></p>' .
72
			'<p><a href="' . esc_url( FrmAppHelper::make_affiliate_url( 'https://formidableforms.com/knowledgebase/manage-entries-from-the-back-end/' ) ) . '" target="_blank">' . esc_html__( 'Documentation on Entries', 'formidable' ) . '</a></p>' .
73
			'<p><a href="' . esc_url( FrmAppHelper::make_affiliate_url( 'https://formidableforms.com/help-desk/' ) ) . '" target="_blank">' . esc_html__( 'Support', 'formidable' ) . '</a></p>'
74
    	);
75
76
        return $help;
77
    }
78
79
	/**
80
	 * Prevent the "screen options" tab from showing when
81
	 * editing or creating an entry
82
	 *
83
	 * @since 3.0
84
	 */
85
	public static function remove_screen_options( $show_screen, $screen ) {
86
		$menu_name = sanitize_title( FrmAppHelper::get_menu_name() );
87
		if ( $screen->id == $menu_name . '_page_formidable-entries' ) {
88
			$show_screen = false;
89
		}
90
91
		return $show_screen;
92
	}
93
94
	public static function manage_columns( $columns ) {
95
        global $frm_vars;
96
		$form_id = FrmForm::get_current_form_id();
97
98
		$columns[ $form_id . '_id' ] = 'ID';
99
		$columns[ $form_id . '_item_key' ] = esc_html__( 'Entry Key', 'formidable' );
100
101
		if ( $form_id ) {
102
			self::get_columns_for_form( $form_id, $columns );
103
		} else {
104
			$columns[ $form_id . '_form_id' ] = __( 'Form', 'formidable' );
105
			$columns[ $form_id . '_name' ] = __( 'Entry Name', 'formidable' );
106
			$columns[ $form_id . '_user_id' ] = __( 'Created By', 'formidable' );
107
		}
108
109
		$columns[ $form_id . '_created_at' ] = __( 'Entry creation date', 'formidable' );
110
		$columns[ $form_id . '_updated_at' ] = __( 'Entry update date', 'formidable' );
111
		self::maybe_add_ip_col( $form_id, $columns );
112
113
        $frm_vars['cols'] = $columns;
114
115
		$action = FrmAppHelper::simple_get( 'frm_action', 'sanitize_title' );
116
		if ( FrmAppHelper::is_admin_page( 'formidable-entries' ) && in_array( $action, array( '', 'list', 'destroy' ) ) ) {
117
			add_screen_option( 'per_page', array(
118
				'label'   => __( 'Entries', 'formidable' ),
119
				'default' => 20,
120
				'option'  => 'formidable_page_formidable_entries_per_page',
121
			) );
122
        }
123
124
        return $columns;
125
    }
126
127
	private static function get_columns_for_form( $form_id, &$columns ) {
128
		$form_cols = FrmField::get_all_for_form( $form_id, '', 'include' );
129
130
		foreach ( $form_cols as $form_col ) {
131
			if ( FrmField::is_no_save_field( $form_col->type ) ) {
132
				continue;
133
			}
134
135
			$has_child_fields = $form_col->type == 'form' && isset( $form_col->field_options['form_select'] ) && ! empty( $form_col->field_options['form_select'] );
136
			if ( $has_child_fields ) {
137
				self::add_subform_cols( $form_col, $form_id, $columns );
138
			} else {
139
				self::add_field_cols( $form_col, $form_id, $columns );
140
			}
141
		}
142
	}
143
144
	/**
145
	 * @since 3.0.07
146
	 */
147
	private static function add_subform_cols( $field, $form_id, &$columns ) {
148
		$sub_form_cols = FrmField::get_all_for_form( $field->field_options['form_select'] );
149
		if ( empty( $sub_form_cols ) ) {
150
			return;
151
		}
152
153
		foreach ( $sub_form_cols as $k => $sub_form_col ) {
154
			if ( FrmField::is_no_save_field( $sub_form_col->type ) ) {
155
				unset( $sub_form_cols[ $k ] );
156
				continue;
157
			}
158
			$columns[ $form_id . '_' . $sub_form_col->field_key . '-_-' . $field->id ] = FrmAppHelper::truncate( $sub_form_col->name, 35 );
159
			unset( $sub_form_col );
160
		}
161
	}
162
163
	/**
164
	 * @since 3.0.07
165
	 */
166
	private static function add_field_cols( $field, $form_id, &$columns ) {
167
		$col_id = $field->field_key;
168
		if ( $field->form_id != $form_id ) {
169
			$col_id .= '-_-form' . $field->form_id;
170
		}
171
172
		$has_separate_value = ! FrmField::is_option_empty( $field, 'separate_value' );
173
		$is_post_status     = FrmField::is_option_true( $field, 'post_field' ) && $field->field_options['post_field'] == 'post_status';
174
		if ( $has_separate_value && ! $is_post_status ) {
175
			$columns[ $form_id . '_frmsep_' . $col_id ] = FrmAppHelper::truncate( $field->name, 35 );
176
		}
177
178
		$columns[ $form_id . '_' . $col_id ] = FrmAppHelper::truncate( $field->name, 35 );
179
	}
180
181
	private static function maybe_add_ip_col( $form_id, &$columns ) {
182
		if ( FrmAppHelper::ips_saved() ) {
183
			$columns[ $form_id . '_ip' ] = 'IP';
184
		}
185
	}
186
187
	public static function check_hidden_cols( $check, $object_id, $meta_key, $meta_value, $prev_value ) {
188
		$this_page_name = self::hidden_column_key();
189
		if ( $meta_key != $this_page_name || $meta_value == $prev_value ) {
190
            return $check;
191
        }
192
193
		if ( empty( $prev_value ) ) {
194
			$prev_value = get_metadata( 'user', $object_id, $meta_key, true );
195
		}
196
197
        global $frm_vars;
198
		//add a check so we don't create a loop
199
		$frm_vars['prev_hidden_cols'] = ( isset( $frm_vars['prev_hidden_cols'] ) && $frm_vars['prev_hidden_cols'] ) ? false : $prev_value;
200
201
        return $check;
202
    }
203
204
    //add hidden columns back from other forms
205
	public static function update_hidden_cols( $meta_id, $object_id, $meta_key, $meta_value ) {
206
		$this_page_name = self::hidden_column_key();
207
		if ( $meta_key != $this_page_name ) {
208
            return;
209
        }
210
211
		global $frm_vars;
212
		if ( ! isset( $frm_vars['prev_hidden_cols'] ) || ! $frm_vars['prev_hidden_cols'] ) {
213
			return; //don't continue if there's no previous value
214
		}
215
216
        foreach ( $meta_value as $mk => $mv ) {
217
            //remove blank values
218
            if ( empty( $mv ) ) {
219
                unset( $meta_value[ $mk ] );
220
            }
221
        }
222
223
		$cur_form_prefix = reset( $meta_value );
224
		$cur_form_prefix = explode( '_', $cur_form_prefix );
225
        $cur_form_prefix = $cur_form_prefix[0];
226
        $save = false;
227
228
        foreach ( (array) $frm_vars['prev_hidden_cols'] as $prev_hidden ) {
229
			if ( empty( $prev_hidden ) || in_array( $prev_hidden, $meta_value ) ) {
230
                //don't add blank cols or process included cols
231
                continue;
232
            }
233
234
			$form_prefix = explode( '_', $prev_hidden );
235
            $form_prefix = $form_prefix[0];
236
            if ( $form_prefix == $cur_form_prefix ) {
237
                //don't add back columns that are meant to be hidden
238
                continue;
239
            }
240
241
            $meta_value[] = $prev_hidden;
242
            $save = true;
243
			unset( $form_prefix );
244
        }
245
246
		if ( $save ) {
247
			$user_id = get_current_user_id();
248
			update_user_option( $user_id, $this_page_name, $meta_value, true );
249
        }
250
    }
251
252
	/**
253
	 * @since 2.05.07
254
	 */
255
	private static function hidden_column_key( $menu_name = '' ) {
256
		$base = self::base_column_key( $menu_name );
257
		return 'manage' . $base . 'columnshidden';
258
	}
259
260
	/**
261
	 * @since 2.05.07
262
	 */
263
	private static function base_column_key( $menu_name = '' ) {
264
		if ( empty( $menu_name ) ) {
265
			$menu_name = FrmAppHelper::get_menu_name();
266
		}
267
		return sanitize_title( $menu_name ) . '_page_formidable-entries';
268
	}
269
270
	public static function save_per_page( $save, $option, $value ) {
271
        if ( $option == 'formidable_page_formidable_entries_per_page' ) {
272
            $save = (int) $value;
273
        }
274
        return $save;
275
    }
276
277
	public static function sortable_columns() {
278
		$form_id = FrmForm::get_current_form_id();
279
		$fields = FrmField::get_all_for_form( $form_id );
280
281
		$columns = array(
282
			$form_id . '_id'         => 'id',
283
			$form_id . '_created_at' => 'created_at',
284
			$form_id . '_updated_at' => 'updated_at',
285
			$form_id . '_ip'         => 'ip',
286
			$form_id . '_item_key'   => 'item_key',
287
			$form_id . '_is_draft'   => 'is_draft',
288
		);
289
290
		foreach ( $fields as $field ) {
291
			if ( $field->type != 'checkbox' && ( ! isset( $field->field_options['post_field'] ) || $field->field_options['post_field'] == '' ) ) {
292
				// Can't sort on checkboxes because they are stored serialized, or post fields
293
				$columns[ $form_id . '_' . $field->field_key ] = 'meta_' . $field->id;
294
			}
295
		}
296
297
		return $columns;
298
	}
299
300
	public static function hidden_columns( $result ) {
301
		$form_id = FrmForm::get_current_form_id();
302
303
		$hidden = self::user_hidden_columns_for_form( $form_id, $result );
304
305
		global $frm_vars;
306
		$i = isset( $frm_vars['cols'] ) ? count( $frm_vars['cols'] ) : 0;
307
308
		if ( ! empty( $hidden ) ) {
309
			$result = $hidden;
310
			$i = $i - count( $result );
311
			$max_columns = 11;
312
		} else {
313
			$max_columns = 8;
314
		}
315
316
		if ( $i <= $max_columns ) {
317
			return $result;
318
		}
319
320
		self::remove_excess_cols( compact( 'i', 'max_columns', 'form_id' ), $result );
321
322
		return $result;
323
	}
324
325
	/**
326
	 * @since 2.05.07
327
	 */
328
	private static function user_hidden_columns_for_form( $form_id, $result ) {
329
		$hidden = array();
330
		foreach ( (array) $result as $r ) {
331
			if ( ! empty( $r ) ) {
332
				list( $form_prefix, $field_key ) = explode( '_', $r );
0 ignored issues
show
Unused Code introduced by
The assignment to $field_key is unused. Consider omitting it like so list($first,,$third).

This checks looks for assignemnts to variables using the list(...) function, where not all assigned variables are subsequently used.

Consider the following code example.

<?php

function returnThreeValues() {
    return array('a', 'b', 'c');
}

list($a, $b, $c) = returnThreeValues();

print $a . " - " . $c;

Only the variables $a and $c are used. There was no need to assign $b.

Instead, the list call could have been.

list($a,, $c) = returnThreeValues();
Loading history...
333
334
				if ( (int) $form_prefix == (int) $form_id ) {
335
					$hidden[] = $r;
336
				}
337
338
				unset( $form_prefix );
339
			}
340
		}
341
		return $hidden;
342
	}
343
344
	/**
345
	 * Remove some columns by default when there are too many
346
	 *
347
	 * @since 2.05.07
348
	 */
349
	private static function remove_excess_cols( $atts, &$result ) {
350
		global $frm_vars;
351
352
		$remove_first = array(
353
			$atts['form_id'] . '_item_key' => '',
354
			$atts['form_id'] . '_id'       => '',
355
		);
356
		$cols = $remove_first + array_reverse( $frm_vars['cols'], true );
357
358
		$i = $atts['i'];
359
360
		foreach ( $cols as $col_key => $col ) {
361
			if ( $i <= $atts['max_columns'] ) {
362
				break;
363
			}
364
365
			if ( empty( $result ) || ! in_array( $col_key, $result, true ) ) {
366
				$result[] = $col_key;
367
				$i--;
368
			}
369
370
			unset( $col_key, $col );
371
		}
372
	}
373
374
	public static function display_list( $message = '', $errors = array() ) {
375
        global $wpdb, $frm_vars;
376
377
		$form = FrmForm::maybe_get_current_form();
378
		$params = FrmForm::get_admin_params( $form );
379
380
        if ( $form ) {
381
            $params['form'] = $form->id;
382
            $frm_vars['current_form'] = $form;
383
384
			self::get_delete_form_time( $form, $errors );
385
		}
386
387
        $table_class = apply_filters( 'frm_entries_list_class', 'FrmEntriesListHelper' );
388
389
        $wp_list_table = new $table_class( array( 'params' => $params ) );
390
391
        $pagenum = $wp_list_table->get_pagenum();
392
393
        $wp_list_table->prepare_items();
394
395
        $total_pages = $wp_list_table->get_pagination_arg( 'total_pages' );
396
        if ( $pagenum > $total_pages && $total_pages > 0 ) {
397
			$url = add_query_arg( 'paged', $total_pages );
398
            if ( headers_sent() ) {
399
				echo FrmAppHelper::js_redirect( $url );
0 ignored issues
show
introduced by
Expected a sanitizing function (see Codex for 'Data Validation'), but instead saw 'FrmAppHelper'
Loading history...
400
            } else {
401
                wp_redirect( esc_url_raw( $url ) );
402
            }
403
            die();
404
        }
405
406
		if ( empty( $message ) && isset( $_GET['import-message'] ) ) {
407
            $message = __( 'Your import is complete', 'formidable' );
408
        }
409
410
		require( FrmAppHelper::plugin_path() . '/classes/views/frm-entries/list.php' );
411
    }
412
413
	private static function get_delete_form_time( $form, &$errors ) {
414
		if ( 'trash' == $form->status ) {
415
			$delete_timestamp = time() - ( DAY_IN_SECONDS * EMPTY_TRASH_DAYS );
416
			$time_to_delete = FrmAppHelper::human_time_diff( $delete_timestamp, ( isset( $form->options['trash_time'] ) ? ( $form->options['trash_time'] ) : time() ) );
417
			$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 );
418
		}
419
	}
420
421
    /* Back End CRUD */
422
	public static function show( $id = 0 ) {
423
		FrmAppHelper::permission_check( 'frm_view_entries' );
424
425
        if ( ! $id ) {
426
			$id = FrmAppHelper::get_param( 'id', 0, 'get', 'absint' );
427
428
            if ( ! $id ) {
429
				$id = FrmAppHelper::get_param( 'item_id', 0, 'get', 'absint' );
430
            }
431
        }
432
433
		$entry = FrmEntry::getOne( $id, true );
434
		if ( ! $entry ) {
435
			echo '<div id="form_show_entry_page" class="wrap">' .
436
				__( 'You are trying to view an entry that does not exist.', 'formidable' ) .
0 ignored issues
show
introduced by
Expected a sanitizing function (see Codex for 'Data Validation'), but instead saw '__'
Loading history...
437
				'</div>';
438
			return;
439
		}
440
441
		$data = maybe_unserialize( $entry->description );
442
		if ( ! is_array( $data ) || ! isset( $data['referrer'] ) ) {
443
			$data = array( 'referrer' => $data );
444
		}
445
446
		$fields = FrmField::get_all_for_form( $entry->form_id, '', 'include' );
447
        $to_emails = array();
448
		$form = FrmForm::getOne( $entry->form_id );
449
450
		include( FrmAppHelper::plugin_path() . '/classes/views/frm-entries/show.php' );
451
    }
452
453
    public static function destroy() {
454
		FrmAppHelper::permission_check( 'frm_delete_entries' );
455
456
		$params = FrmForm::get_admin_params();
457
458
		if ( isset( $params['keep_post'] ) && $params['keep_post'] ) {
459
			self::unlink_post( $params['id'] );
460
        }
461
462
        $message = '';
463
        if ( FrmEntry::destroy( $params['id'] ) ) {
464
            $message = __( 'Entry was Successfully Destroyed', 'formidable' );
465
        }
466
467
        self::display_list( $message );
468
    }
469
470
    public static function destroy_all() {
471
        if ( ! current_user_can( 'frm_delete_entries' ) ) {
472
            $frm_settings = FrmAppHelper::get_settings();
473
            wp_die( $frm_settings->admin_permission );
474
        }
475
476
		$params = FrmForm::get_admin_params();
477
        $message = '';
478
        $errors = array();
479
        $form_id = (int) $params['form'];
480
481
        if ( $form_id ) {
482
            $entry_ids = FrmDb::get_col( 'frm_items', array( 'form_id' => $form_id ) );
483
			$action = FrmFormAction::get_action_for_form( $form_id, 'wppost', 1 );
484
485
            if ( $action ) {
486
                // this action takes a while, so only trigger it if there are posts to delete
487
                foreach ( $entry_ids as $entry_id ) {
0 ignored issues
show
Bug introduced by
The expression $entry_ids of type array|null|string|object is not guaranteed to be traversable. How about adding an additional type check?

There are different options of fixing this problem.

  1. If you want to be on the safe side, you can add an additional type-check:

    $collection = json_decode($data, true);
    if ( ! is_array($collection)) {
        throw new \RuntimeException('$collection must be an array.');
    }
    
    foreach ($collection as $item) { /** ... */ }
    
  2. If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:

    /** @var array $collection */
    $collection = json_decode($data, true);
    
    foreach ($collection as $item) { /** .. */ }
    
  3. Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.

Loading history...
488
                    do_action( 'frm_before_destroy_entry', $entry_id );
489
                    unset( $entry_id );
490
                }
491
            }
492
493
            $results = self::delete_form_entries( $form_id );
494
            if ( $results ) {
495
				FrmEntry::clear_cache();
496
                $message = __( 'Entries were Successfully Destroyed', 'formidable' );
497
            }
498
        } else {
499
            $errors = __( 'No entries were specified', 'formidable' );
500
        }
501
502
        self::display_list( $message, $errors );
503
    }
504
505
	/**
506
	 * @since 3.0.07
507
	 * @param int $form_id
508
	 */
509
	private static function delete_form_entries( $form_id ) {
510
		global $wpdb;
511
512
		$form_ids = self::get_child_form_ids( $form_id );
513
514
		$meta_query = $wpdb->prepare( "DELETE em.* FROM {$wpdb->prefix}frm_item_metas as em INNER JOIN {$wpdb->prefix}frm_items as e on (em.item_id=e.id) WHERE form_id=%d", $form_id );
515
		$entry_query = $wpdb->prepare( "DELETE FROM {$wpdb->prefix}frm_items WHERE form_id=%d", $form_id );
516
517
		if ( ! empty( $form_ids ) ) {
518
			$form_query = ' OR form_id in (' . $form_ids . ')';
519
			$meta_query .= $form_query;
520
			$entry_query .= $form_query;
521
		}
522
523
		$wpdb->query( $meta_query );
524
		return $wpdb->query( $entry_query );
525
	}
526
527
	/**
528
	 * @since 3.0.07
529
	 * @param int $form_id
530
	 * @param bool|string $implode
531
	 */
532
	private static function get_child_form_ids( $form_id, $implode = ',' ) {
533
		$form_ids = array();
534
		$child_form_ids = FrmDb::get_col( 'frm_forms', array( 'parent_form_id' => $form_id ) );
535
		if ( $child_form_ids ) {
536
			$form_ids = $child_form_ids;
537
		}
538
		$form_ids = array_filter( $form_ids, 'is_numeric' );
539
		if ( $implode ) {
540
			$form_ids = implode( $implode, $form_ids );
541
		}
542
		return $form_ids;
543
	}
544
545
	/**
546
	 * @deprecated 1.07.05
547
	 * @codeCoverageIgnore
548
	 */
549
    public static function show_form( $id = '', $key = '', $title = false, $description = false ) {
550
        _deprecated_function( __FUNCTION__, '1.07.05', 'FrmFormsController::show_form()' );
551
        return FrmFormsController::show_form( $id, $key, $title, $description );
552
    }
553
554
	/**
555
	 * @deprecated 1.07.05
556
	 * @codeCoverageIgnore
557
	 */
558
    public static function get_form( $filename, $form, $title, $description ) {
559
        _deprecated_function( __FUNCTION__, '1.07.05', 'FrmFormsController::get_form()' );
560
        return FrmFormsController::get_form( $form, $title, $description );
561
    }
562
563
    public static function process_entry( $errors = '', $ajax = false ) {
564
		$form_id = FrmAppHelper::get_post_param( 'form_id', '', 'absint' );
565
		if ( FrmAppHelper::is_admin() || empty( $_POST ) || empty( $form_id ) || ! isset( $_POST['item_key'] ) ) {
566
            return;
567
        }
568
569
        global $frm_vars;
570
571
		$form = FrmForm::getOne( $form_id );
572
        if ( ! $form ) {
573
            return;
574
        }
575
576
		$params = FrmForm::get_params( $form );
577
578
        if ( ! isset( $frm_vars['form_params'] ) ) {
579
            $frm_vars['form_params'] = array();
580
        }
581
		$frm_vars['form_params'][ $form->id ] = $params;
582
583
		if ( isset( $frm_vars['created_entries'][ $form_id ] ) ) {
584
            return;
585
        }
586
587
        if ( $errors == '' && ! $ajax ) {
588
			$errors = FrmEntryValidate::validate( $_POST );
589
        }
590
591
		/**
592
		 * Use this filter to add trigger actions and add errors after
593
		 * all other errors have been processed
594
		 * @since 2.0.6
595
		 */
596
		$errors = apply_filters( 'frm_entries_before_create', $errors, $form );
597
598
		$frm_vars['created_entries'][ $form_id ] = array( 'errors' => $errors );
599
600
        if ( empty( $errors ) ) {
601
			$_POST['frm_skip_cookie'] = 1;
602
			$do_success = false;
603
            if ( $params['action'] == 'create' ) {
604
				if ( apply_filters( 'frm_continue_to_create', true, $form_id ) && ! isset( $frm_vars['created_entries'][ $form_id ]['entry_id'] ) ) {
605
					$frm_vars['created_entries'][ $form_id ]['entry_id'] = FrmEntry::create( $_POST );
606
					$params['id'] = $frm_vars['created_entries'][ $form_id ]['entry_id'];
607
					$do_success = true;
608
                }
609
            }
610
611
            do_action( 'frm_process_entry', $params, $errors, $form, array( 'ajax' => $ajax ) );
612
			if ( $do_success ) {
613
				FrmFormsController::maybe_trigger_redirect( $form, $params, array( 'ajax' => $ajax ) );
614
			}
615
			unset( $_POST['frm_skip_cookie'] );
616
        }
617
    }
618
619
	/**
620
	 * Escape url entities before redirect
621
	 *
622
	 * @since 3.0
623
	 *
624
	 * @param string $url
625
	 * @return string
626
	 */
627
	public static function prepare_redirect_url( $url ) {
628
		return str_replace( array( ' ', '[', ']', '|', '@' ), array( '%20', '%5B', '%5D', '%7C', '%40' ), $url );
629
	}
630
631
    public static function delete_entry_before_redirect( $url, $form, $atts ) {
632
        self::_delete_entry( $atts['id'], $form );
633
        return $url;
634
    }
635
636
    //Delete entry if not redirected
637
    public static function delete_entry_after_save( $atts ) {
638
        self::_delete_entry( $atts['entry_id'], $atts['form'] );
639
    }
640
641
    private static function _delete_entry( $entry_id, $form ) {
642
        if ( ! $form ) {
643
            return;
644
        }
645
646
        $form->options = maybe_unserialize( $form->options );
647
        if ( isset( $form->options['no_save'] ) && $form->options['no_save'] ) {
648
			self::unlink_post( $entry_id );
649
            FrmEntry::destroy( $entry_id );
650
        }
651
    }
652
653
	/**
654
	 * unlink entry from post
655
	 */
656
	private static function unlink_post( $entry_id ) {
657
		global $wpdb;
658
		$wpdb->update( $wpdb->prefix . 'frm_items', array( 'post_id' => '' ), array( 'id' => $entry_id ) );
659
		FrmEntry::clear_cache();
660
	}
661
662
	/**
663
	 * @param $atts
664
	 *
665
	 * @return array|string
666
	 */
667
	public static function show_entry_shortcode( $atts ) {
668
		$defaults = apply_filters( 'frm_show_entry_defaults', array(
669
			'id'             => false,
670
			'entry'          => false,
671
			'fields'         => false,
672
			'plain_text'     => false,
673
			'user_info'      => false,
674
			'include_blank'  => false,
675
			'default_email'  => false,
676
			'form_id'        => false,
677
			'format'         => 'text',
678
			'array_key'      => 'key',
679
			'direction'      => 'ltr',
680
			'font_size'      => '',
681
			'text_color'     => '',
682
			'border_width'   => '',
683
			'border_color'   => '',
684
			'bg_color'       => '',
685
			'alt_bg_color'   => '',
686
			'clickable'      => false,
687
			'exclude_fields' => '',
688
			'include_fields' => '',
689
			'include_extras' => '',
690
			'inline_style'   => 1,
691
			'child_array'    => false, // return embedded fields as nested array
692
		) );
693
694
		$atts = shortcode_atts( $defaults, $atts );
695
696
		if ( $atts['default_email'] ) {
697
			$shortcode_atts = array(
698
				'format'     => $atts['format'],
699
				'plain_text' => $atts['plain_text'],
700
			);
701
			$entry_shortcode_formatter = FrmEntryFactory::entry_shortcode_formatter_instance( $atts['form_id'], $shortcode_atts );
702
			$formatted_entry = $entry_shortcode_formatter->content();
703
704
		} else {
705
706
			$entry_formatter = FrmEntryFactory::entry_formatter_instance( $atts );
707
			$formatted_entry = $entry_formatter->get_formatted_entry_values();
708
709
		}
710
711
		return $formatted_entry;
712
	}
713
714
	public static function entry_sidebar( $entry ) {
715
		$data = maybe_unserialize( $entry->description );
716
		$date_format = get_option( 'date_format' );
717
		$time_format = get_option( 'time_format' );
718
		if ( isset( $data['browser'] ) ) {
719
			$browser = FrmEntriesHelper::get_browser( $data['browser'] );
720
		}
721
722
		include( FrmAppHelper::plugin_path() . '/classes/views/frm-entries/sidebar-shared.php' );
723
    }
724
}
725