Completed
Push — master ( a29595...fd1ed9 )
by Stephanie
02:55
created

FrmEntriesController::delete_entry_after_save()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 3
rs 10
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
			if ( $form_col->type == 'form' && isset( $form_col->field_options['form_select'] ) && ! empty( $form_col->field_options['form_select'] ) ) {
136
				$sub_form_cols = FrmField::get_all_for_form( $form_col->field_options['form_select'] );
137
138
				if ( $sub_form_cols ) {
139
					foreach ( $sub_form_cols as $k => $sub_form_col ) {
140
						if ( FrmField::is_no_save_field( $sub_form_col->type ) ) {
141
							unset( $sub_form_cols[ $k ] );
142
							continue;
143
						}
144
						$columns[ $form_id . '_' . $sub_form_col->field_key . '-_-' . $form_col->id ] = FrmAppHelper::truncate( $sub_form_col->name, 35 );
145
						unset( $sub_form_col );
146
					}
147
				}
148
				unset( $sub_form_cols );
149
			} else {
150
				$col_id = $form_col->field_key;
151
				if ( $form_col->form_id != $form_id ) {
152
					$col_id .= '-_-form' . $form_col->form_id;
153
				}
154
155
				$has_separate_value = ! FrmField::is_option_empty( $form_col, 'separate_value' );
156
				$is_post_status     = FrmField::is_option_true( $form_col, 'post_field' ) && $form_col->field_options['post_field'] == 'post_status';
157
				if ( $has_separate_value && ! $is_post_status ) {
158
					$columns[ $form_id . '_frmsep_' . $col_id ] = FrmAppHelper::truncate( $form_col->name, 35 );
159
				}
160
				$columns[ $form_id . '_' . $col_id ] = FrmAppHelper::truncate( $form_col->name, 35 );
161
			}
162
		}
163
	}
164
165
	private static function maybe_add_ip_col( $form_id, &$columns ) {
166
		if ( FrmAppHelper::ips_saved() ) {
167
			$columns[ $form_id . '_ip' ] = 'IP';
168
		}
169
	}
170
171
	public static function check_hidden_cols( $check, $object_id, $meta_key, $meta_value, $prev_value ) {
172
		$this_page_name = self::hidden_column_key();
173
		if ( $meta_key != $this_page_name || $meta_value == $prev_value ) {
174
            return $check;
175
        }
176
177
		if ( empty( $prev_value ) ) {
178
			$prev_value = get_metadata( 'user', $object_id, $meta_key, true );
179
		}
180
181
        global $frm_vars;
182
		//add a check so we don't create a loop
183
		$frm_vars['prev_hidden_cols'] = ( isset( $frm_vars['prev_hidden_cols'] ) && $frm_vars['prev_hidden_cols'] ) ? false : $prev_value;
184
185
        return $check;
186
    }
187
188
    //add hidden columns back from other forms
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
		return 'manage' . $base . 'columnshidden';
242
	}
243
244
	/**
245
	 * @since 2.05.07
246
	 */
247
	private static function base_column_key( $menu_name = '' ) {
248
		if ( empty( $menu_name ) ) {
249
			$menu_name = FrmAppHelper::get_menu_name();
250
		}
251
		return sanitize_title( $menu_name ) . '_page_formidable-entries';
252
	}
253
254
	public static function save_per_page( $save, $option, $value ) {
255
        if ( $option == 'formidable_page_formidable_entries_per_page' ) {
256
            $save = (int) $value;
257
        }
258
        return $save;
259
    }
260
261
	public static function sortable_columns() {
262
		$form_id = FrmForm::get_current_form_id();
263
		$fields = FrmField::get_all_for_form( $form_id );
264
265
		$columns = array(
266
			$form_id . '_id'         => 'id',
267
			$form_id . '_created_at' => 'created_at',
268
			$form_id . '_updated_at' => 'updated_at',
269
			$form_id . '_ip'         => 'ip',
270
			$form_id . '_item_key'   => 'item_key',
271
			$form_id . '_is_draft'   => 'is_draft',
272
		);
273
274
		foreach ( $fields as $field ) {
275
			if ( $field->type != 'checkbox' && ( ! isset( $field->field_options['post_field'] ) || $field->field_options['post_field'] == '' ) ) {
276
				// Can't sort on checkboxes because they are stored serialized, or post fields
277
				$columns[ $form_id . '_' . $field->field_key ] = 'meta_' . $field->id;
278
			}
279
		}
280
281
		return $columns;
282
	}
283
284
	public static function hidden_columns( $result ) {
285
		$form_id = FrmForm::get_current_form_id();
286
287
		$hidden = self::user_hidden_columns_for_form( $form_id, $result );
288
289
		global $frm_vars;
290
		$i = isset( $frm_vars['cols'] ) ? count( $frm_vars['cols'] ) : 0;
291
292
		if ( ! empty( $hidden ) ) {
293
			$result = $hidden;
294
			$i = $i - count( $result );
295
			$max_columns = 11;
296
		} else {
297
			$max_columns = 8;
298
		}
299
300
		if ( $i <= $max_columns ) {
301
			return $result;
302
		}
303
304
		self::remove_excess_cols( compact( 'i', 'max_columns', 'form_id' ), $result );
305
306
		return $result;
307
	}
308
309
	/**
310
	 * @since 2.05.07
311
	 */
312
	private static function user_hidden_columns_for_form( $form_id, $result ) {
313
		$hidden = array();
314
		foreach ( (array) $result as $r ) {
315
			if ( ! empty( $r ) ) {
316
				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...
317
318
				if ( (int) $form_prefix == (int) $form_id ) {
319
					$hidden[] = $r;
320
				}
321
322
				unset( $form_prefix );
323
			}
324
		}
325
		return $hidden;
326
	}
327
328
	/**
329
	 * Remove some columns by default when there are too many
330
	 *
331
	 * @since 2.05.07
332
	 */
333
	private static function remove_excess_cols( $atts, &$result ) {
334
		global $frm_vars;
335
336
		$remove_first = array(
337
			$atts['form_id'] . '_item_key' => '',
338
			$atts['form_id'] . '_id'       => '',
339
		);
340
		$cols = $remove_first + array_reverse( $frm_vars['cols'], true );
341
342
		$i = $atts['i'];
343
344
		foreach ( $cols as $col_key => $col ) {
345
			if ( $i <= $atts['max_columns'] ) {
346
				break;
347
			}
348
349
			if ( empty( $result ) || ! in_array( $col_key, $result, true ) ) {
350
				$result[] = $col_key;
351
				$i--;
352
			}
353
354
			unset( $col_key, $col );
355
		}
356
	}
357
358
	public static function display_list( $message = '', $errors = array() ) {
359
        global $wpdb, $frm_vars;
360
361
		$form = FrmForm::maybe_get_current_form();
362
		$params = FrmForm::get_admin_params( $form );
363
364
        if ( $form ) {
365
            $params['form'] = $form->id;
366
            $frm_vars['current_form'] = $form;
367
368
			self::get_delete_form_time( $form, $errors );
369
		}
370
371
        $table_class = apply_filters( 'frm_entries_list_class', 'FrmEntriesListHelper' );
372
373
        $wp_list_table = new $table_class( array( 'params' => $params ) );
374
375
        $pagenum = $wp_list_table->get_pagenum();
376
377
        $wp_list_table->prepare_items();
378
379
        $total_pages = $wp_list_table->get_pagination_arg( 'total_pages' );
380
        if ( $pagenum > $total_pages && $total_pages > 0 ) {
381
			$url = add_query_arg( 'paged', $total_pages );
382
            if ( headers_sent() ) {
383
				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...
384
            } else {
385
                wp_redirect( esc_url_raw( $url ) );
386
            }
387
            die();
388
        }
389
390
		if ( empty( $message ) && isset( $_GET['import-message'] ) ) {
391
            $message = __( 'Your import is complete', 'formidable' );
392
        }
393
394
		require( FrmAppHelper::plugin_path() . '/classes/views/frm-entries/list.php' );
395
    }
396
397
	private static function get_delete_form_time( $form, &$errors ) {
398
		if ( 'trash' == $form->status ) {
399
			$delete_timestamp = time() - ( DAY_IN_SECONDS * EMPTY_TRASH_DAYS );
400
			$time_to_delete = FrmAppHelper::human_time_diff( $delete_timestamp, ( isset( $form->options['trash_time'] ) ? ( $form->options['trash_time'] ) : time() ) );
401
			$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 );
402
		}
403
	}
404
405
    /* Back End CRUD */
406
	public static function show( $id = 0 ) {
407
		FrmAppHelper::permission_check( 'frm_view_entries' );
408
409
        if ( ! $id ) {
410
			$id = FrmAppHelper::get_param( 'id', 0, 'get', 'absint' );
411
412
            if ( ! $id ) {
413
				$id = FrmAppHelper::get_param( 'item_id', 0, 'get', 'absint' );
414
            }
415
        }
416
417
		$entry = FrmEntry::getOne( $id, true );
418
		if ( ! $entry ) {
419
			echo '<div id="form_show_entry_page" class="wrap">' .
420
				__( '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...
421
				'</div>';
422
			return;
423
		}
424
425
		$data = maybe_unserialize( $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
        $to_emails = array();
432
		$form = FrmForm::getOne( $entry->form_id );
433
434
		include( FrmAppHelper::plugin_path() . '/classes/views/frm-entries/show.php' );
435
    }
436
437
    public static function destroy() {
438
		FrmAppHelper::permission_check( 'frm_delete_entries' );
439
440
		$params = FrmForm::get_admin_params();
441
442
		if ( isset( $params['keep_post'] ) && $params['keep_post'] ) {
443
			self::unlink_post( $params['id'] );
444
        }
445
446
        $message = '';
447
        if ( FrmEntry::destroy( $params['id'] ) ) {
448
            $message = __( 'Entry was Successfully Destroyed', 'formidable' );
449
        }
450
451
        self::display_list( $message );
452
    }
453
454
    public static function destroy_all() {
455
        if ( ! current_user_can( 'frm_delete_entries' ) ) {
456
            $frm_settings = FrmAppHelper::get_settings();
457
            wp_die( $frm_settings->admin_permission );
458
        }
459
460
		$params = FrmForm::get_admin_params();
461
        $message = '';
462
        $errors = array();
463
        $form_id = (int) $params['form'];
464
465
        if ( $form_id ) {
466
            $entry_ids = FrmDb::get_col( 'frm_items', array( 'form_id' => $form_id ) );
467
			$action = FrmFormAction::get_action_for_form( $form_id, 'wppost', 1 );
468
469
            if ( $action ) {
470
                // this action takes a while, so only trigger it if there are posts to delete
471
                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...
472
                    do_action( 'frm_before_destroy_entry', $entry_id );
473
                    unset( $entry_id );
474
                }
475
            }
476
477
            $results = self::delete_form_entries( $form_id );
478
            if ( $results ) {
479
				FrmEntry::clear_cache();
480
                $message = __( 'Entries were Successfully Destroyed', 'formidable' );
481
            }
482
        } else {
483
            $errors = __( 'No entries were specified', 'formidable' );
484
        }
485
486
        self::display_list( $message, $errors );
487
    }
488
489
	/**
490
	 * @since 3.0.07
491
	 * @param int $form_id
492
	 */
493
	private static function delete_form_entries( $form_id ) {
494
		global $wpdb;
495
496
		$form_ids = self::get_child_form_ids( $form_id );
497
498
		$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 );
499
		$entry_query = $wpdb->prepare( "DELETE FROM {$wpdb->prefix}frm_items WHERE form_id=%d", $form_id );
500
501
		if ( ! empty( $form_ids ) ) {
502
			$form_query = ' OR form_id in (' . $form_ids . ')';
503
			$meta_query .= $form_query;
504
			$entry_query .= $form_query;
505
		}
506
507
		$wpdb->query( $meta_query );
508
		return $wpdb->query( $entry_query );
509
	}
510
511
	/**
512
	 * @since 3.0.07
513
	 * @param int $form_id
514
	 * @param bool|string $implode
515
	 */
516
	private static function get_child_form_ids( $form_id, $implode = ',' ) {
517
		$form_ids = array();
518
		$child_form_ids = FrmDb::get_col( 'frm_forms', array( 'parent_form_id' => $form_id ) );
519
		if ( $child_form_ids ) {
520
			$form_ids = $child_form_ids;
521
		}
522
		$form_ids = array_filter( $form_ids, 'is_numeric' );
523
		if ( $implode ) {
524
			$form_ids = implode( $implode, $form_ids );
525
		}
526
		return $form_ids;
527
	}
528
529
	/**
530
	 * @deprecated 1.07.05
531
	 * @codeCoverageIgnore
532
	 */
533
    public static function show_form( $id = '', $key = '', $title = false, $description = false ) {
534
        _deprecated_function( __FUNCTION__, '1.07.05', 'FrmFormsController::show_form()' );
535
        return FrmFormsController::show_form( $id, $key, $title, $description );
536
    }
537
538
	/**
539
	 * @deprecated 1.07.05
540
	 * @codeCoverageIgnore
541
	 */
542
    public static function get_form( $filename, $form, $title, $description ) {
543
        _deprecated_function( __FUNCTION__, '1.07.05', 'FrmFormsController::get_form()' );
544
        return FrmFormsController::get_form( $form, $title, $description );
545
    }
546
547
    public static function process_entry( $errors = '', $ajax = false ) {
548
		$form_id = FrmAppHelper::get_post_param( 'form_id', '', 'absint' );
549
		if ( FrmAppHelper::is_admin() || empty( $_POST ) || empty( $form_id ) || ! isset( $_POST['item_key'] ) ) {
550
            return;
551
        }
552
553
        global $frm_vars;
554
555
		$form = FrmForm::getOne( $form_id );
556
        if ( ! $form ) {
557
            return;
558
        }
559
560
		$params = FrmForm::get_params( $form );
561
562
        if ( ! isset( $frm_vars['form_params'] ) ) {
563
            $frm_vars['form_params'] = array();
564
        }
565
		$frm_vars['form_params'][ $form->id ] = $params;
566
567
		if ( isset( $frm_vars['created_entries'][ $form_id ] ) ) {
568
            return;
569
        }
570
571
        if ( $errors == '' && ! $ajax ) {
572
			$errors = FrmEntryValidate::validate( $_POST );
573
        }
574
575
		/**
576
		 * Use this filter to add trigger actions and add errors after
577
		 * all other errors have been processed
578
		 * @since 2.0.6
579
		 */
580
		$errors = apply_filters( 'frm_entries_before_create', $errors, $form );
581
582
		$frm_vars['created_entries'][ $form_id ] = array( 'errors' => $errors );
583
584
        if ( empty( $errors ) ) {
585
			$_POST['frm_skip_cookie'] = 1;
586
			$do_success = false;
587
            if ( $params['action'] == 'create' ) {
588
				if ( apply_filters( 'frm_continue_to_create', true, $form_id ) && ! isset( $frm_vars['created_entries'][ $form_id ]['entry_id'] ) ) {
589
					$frm_vars['created_entries'][ $form_id ]['entry_id'] = FrmEntry::create( $_POST );
590
					$params['id'] = $frm_vars['created_entries'][ $form_id ]['entry_id'];
591
					$do_success = true;
592
                }
593
            }
594
595
            do_action( 'frm_process_entry', $params, $errors, $form, array( 'ajax' => $ajax ) );
596
			if ( $do_success ) {
597
				FrmFormsController::maybe_trigger_redirect( $form, $params, array( 'ajax' => $ajax ) );
598
			}
599
			unset( $_POST['frm_skip_cookie'] );
600
        }
601
    }
602
603
	/**
604
	 * Escape url entities before redirect
605
	 *
606
	 * @since 3.0
607
	 *
608
	 * @param string $url
609
	 * @return string
610
	 */
611
	public static function prepare_redirect_url( $url ) {
612
		return str_replace( array( ' ', '[', ']', '|', '@' ), array( '%20', '%5B', '%5D', '%7C', '%40' ), $url );
613
	}
614
615
    public static function delete_entry_before_redirect( $url, $form, $atts ) {
616
        self::_delete_entry( $atts['id'], $form );
617
        return $url;
618
    }
619
620
    //Delete entry if not redirected
621
    public static function delete_entry_after_save( $atts ) {
622
        self::_delete_entry( $atts['entry_id'], $atts['form'] );
623
    }
624
625
    private static function _delete_entry( $entry_id, $form ) {
626
        if ( ! $form ) {
627
            return;
628
        }
629
630
        $form->options = maybe_unserialize( $form->options );
631
        if ( isset( $form->options['no_save'] ) && $form->options['no_save'] ) {
632
			self::unlink_post( $entry_id );
633
            FrmEntry::destroy( $entry_id );
634
        }
635
    }
636
637
	/**
638
	 * unlink entry from post
639
	 */
640
	private static function unlink_post( $entry_id ) {
641
		global $wpdb;
642
		$wpdb->update( $wpdb->prefix . 'frm_items', array( 'post_id' => '' ), array( 'id' => $entry_id ) );
643
		FrmEntry::clear_cache();
644
	}
645
646
	/**
647
	 * @param $atts
648
	 *
649
	 * @return array|string
650
	 */
651
	public static function show_entry_shortcode( $atts ) {
652
		$defaults = apply_filters( 'frm_show_entry_defaults', array(
653
			'id'             => false,
654
			'entry'          => false,
655
			'fields'         => false,
656
			'plain_text'     => false,
657
			'user_info'      => false,
658
			'include_blank'  => false,
659
			'default_email'  => false,
660
			'form_id'        => false,
661
			'format'         => 'text',
662
			'array_key'      => 'key',
663
			'direction'      => 'ltr',
664
			'font_size'      => '',
665
			'text_color'     => '',
666
			'border_width'   => '',
667
			'border_color'   => '',
668
			'bg_color'       => '',
669
			'alt_bg_color'   => '',
670
			'clickable'      => false,
671
			'exclude_fields' => '',
672
			'include_fields' => '',
673
			'include_extras' => '',
674
			'inline_style'   => 1,
675
			'child_array'    => false, // return embedded fields as nested array
676
		) );
677
678
		$atts = shortcode_atts( $defaults, $atts );
679
680
		if ( $atts['default_email'] ) {
681
			$shortcode_atts = array(
682
				'format'     => $atts['format'],
683
				'plain_text' => $atts['plain_text'],
684
			);
685
			$entry_shortcode_formatter = FrmEntryFactory::entry_shortcode_formatter_instance( $atts['form_id'], $shortcode_atts );
686
			$formatted_entry = $entry_shortcode_formatter->content();
687
688
		} else {
689
690
			$entry_formatter = FrmEntryFactory::entry_formatter_instance( $atts );
691
			$formatted_entry = $entry_formatter->get_formatted_entry_values();
692
693
		}
694
695
		return $formatted_entry;
696
	}
697
698
	public static function entry_sidebar( $entry ) {
699
		$data = maybe_unserialize( $entry->description );
700
		$date_format = get_option( 'date_format' );
701
		$time_format = get_option( 'time_format' );
702
		if ( isset( $data['browser'] ) ) {
703
			$browser = FrmEntriesHelper::get_browser( $data['browser'] );
704
		}
705
706
		include( FrmAppHelper::plugin_path() . '/classes/views/frm-entries/sidebar-shared.php' );
707
    }
708
}
709