Passed
Push — master ( ec16eb...0357f3 )
by Stephanie
03:05
created

FrmEntriesController::destroy()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 15
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 4
eloc 8
nc 4
nop 0
dl 0
loc 15
rs 9.2
c 1
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' );
0 ignored issues
show
Bug introduced by
The function add_submenu_page was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

8
		/** @scrutinizer ignore-call */ 
9
  add_submenu_page( 'formidable', 'Formidable | ' . __( 'Entries', 'formidable' ), __( 'Entries', 'formidable' ), 'frm_view_entries', 'formidable-entries', 'FrmEntriesController::route' );
Loading history...
Bug introduced by
The function __ was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

8
		add_submenu_page( 'formidable', 'Formidable | ' . /** @scrutinizer ignore-call */ __( 'Entries', 'formidable' ), __( 'Entries', 'formidable' ), 'frm_view_entries', 'formidable-entries', 'FrmEntriesController::route' );
Loading history...
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' );
0 ignored issues
show
Bug introduced by
The function add_filter was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

21
			/** @scrutinizer ignore-call */ 
22
   add_filter( 'manage_' . $base . '_columns', 'FrmEntriesController::manage_columns' );
Loading history...
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 );
0 ignored issues
show
Bug introduced by
The function do_action was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

40
                /** @scrutinizer ignore-call */ 
41
                do_action( 'frm_entry_action_route', $action );
Loading history...
41
                if ( apply_filters( 'frm_entry_stop_action_route', false, $action ) ) {
0 ignored issues
show
Bug introduced by
The function apply_filters was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

41
                if ( /** @scrutinizer ignore-call */ apply_filters( 'frm_entry_stop_action_route', false, $action ) ) {
Loading history...
42
                    return;
43
                }
44
45
                return self::display_list();
0 ignored issues
show
Bug introduced by
Are you sure the usage of self::display_list() targeting FrmEntriesController::display_list() seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
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' ),
0 ignored issues
show
Bug introduced by
The function __ was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

66
            'title'   => /** @scrutinizer ignore-call */ __( 'Overview', 'formidable' ),
Loading history...
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>',
0 ignored issues
show
Bug introduced by
The function esc_html__ was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

67
			'content' => '<p>' . /** @scrutinizer ignore-call */ 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>',
Loading history...
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>' .
0 ignored issues
show
Bug introduced by
The function esc_url was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

72
			'<p><a href="' . /** @scrutinizer ignore-call */ 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>' .
Loading history...
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() );
0 ignored issues
show
Bug introduced by
The function sanitize_title was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

86
		$menu_name = /** @scrutinizer ignore-call */ sanitize_title( FrmAppHelper::get_menu_name() );
Loading history...
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' );
0 ignored issues
show
Bug introduced by
The function esc_html__ was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

99
		$columns[ $form_id . '_item_key' ] = /** @scrutinizer ignore-call */ esc_html__( 'Entry Key', 'formidable' );
Loading history...
100
101
		if ( $form_id ) {
102
			self::get_columns_for_form( $form_id, $columns );
103
		} else {
104
			$columns[ $form_id . '_form_id' ] = __( 'Form', 'formidable' );
0 ignored issues
show
Bug introduced by
The function __ was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

104
			$columns[ $form_id . '_form_id' ] = /** @scrutinizer ignore-call */ __( 'Form', 'formidable' );
Loading history...
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(
0 ignored issues
show
Bug introduced by
The function add_screen_option was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

117
			/** @scrutinizer ignore-call */ 
118
   add_screen_option( 'per_page', array(
Loading history...
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 );
0 ignored issues
show
Bug introduced by
The function get_metadata was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

178
			$prev_value = /** @scrutinizer ignore-call */ get_metadata( 'user', $object_id, $meta_key, true );
Loading history...
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();
0 ignored issues
show
Bug introduced by
The function get_current_user_id was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

231
			$user_id = /** @scrutinizer ignore-call */ get_current_user_id();
Loading history...
232
			update_user_option( $user_id, $this_page_name, $meta_value, true );
0 ignored issues
show
Bug introduced by
The function update_user_option was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

232
			/** @scrutinizer ignore-call */ 
233
   update_user_option( $user_id, $this_page_name, $meta_value, true );
Loading history...
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';
0 ignored issues
show
Bug introduced by
The function sanitize_title was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

251
		return /** @scrutinizer ignore-call */ sanitize_title( $menu_name ) . '_page_formidable-entries';
Loading history...
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 );
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' );
0 ignored issues
show
Bug introduced by
The function apply_filters was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

371
        $table_class = /** @scrutinizer ignore-call */ apply_filters( 'frm_entries_list_class', 'FrmEntriesListHelper' );
Loading history...
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 );
0 ignored issues
show
Bug introduced by
The function add_query_arg was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

381
			$url = /** @scrutinizer ignore-call */ add_query_arg( 'paged', $total_pages );
Loading history...
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 ) );
0 ignored issues
show
Bug introduced by
The function esc_url_raw was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

385
                wp_redirect( /** @scrutinizer ignore-call */ esc_url_raw( $url ) );
Loading history...
Bug introduced by
The function wp_redirect was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

385
                /** @scrutinizer ignore-call */ 
386
                wp_redirect( esc_url_raw( $url ) );
Loading history...
386
            }
387
            die();
0 ignored issues
show
Best Practice introduced by
Using exit here is not recommended.

In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.

Loading history...
388
        }
389
390
		if ( empty( $message ) && isset( $_GET['import-message'] ) ) {
391
            $message = __( 'Your import is complete', 'formidable' );
0 ignored issues
show
Bug introduced by
The function __ was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

391
            $message = /** @scrutinizer ignore-call */ __( 'Your import is complete', 'formidable' );
Loading history...
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 );
0 ignored issues
show
Bug introduced by
The constant DAY_IN_SECONDS was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
Bug introduced by
The constant EMPTY_TRASH_DAYS was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
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 );
0 ignored issues
show
Bug introduced by
The function __ was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

401
			$errors['trash'] = sprintf( /** @scrutinizer ignore-call */ __( 'This form is in the trash and is scheduled to be deleted permanently in %s along with any entries.', 'formidable' ), $time_to_delete );
Loading history...
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...
Bug introduced by
The function __ was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

420
				/** @scrutinizer ignore-call */ 
421
    __( 'You are trying to view an entry that does not exist.', 'formidable' ) .
Loading history...
421
				'</div>';
422
			return;
423
		}
424
425
		$data = maybe_unserialize( $entry->description );
0 ignored issues
show
Bug introduced by
The function maybe_unserialize was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

425
		$data = /** @scrutinizer ignore-call */ maybe_unserialize( $entry->description );
Loading history...
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' );
0 ignored issues
show
Bug introduced by
The function __ was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

448
            $message = /** @scrutinizer ignore-call */ __( 'Entry was Successfully Destroyed', 'formidable' );
Loading history...
449
        }
450
451
        self::display_list( $message );
452
    }
453
454
    public static function destroy_all() {
455
        if ( ! current_user_can( 'frm_delete_entries' ) ) {
0 ignored issues
show
Bug introduced by
The function current_user_can was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

455
        if ( ! /** @scrutinizer ignore-call */ current_user_can( 'frm_delete_entries' ) ) {
Loading history...
456
            $frm_settings = FrmAppHelper::get_settings();
457
            wp_die( $frm_settings->admin_permission );
0 ignored issues
show
Bug introduced by
The function wp_die was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

457
            /** @scrutinizer ignore-call */ 
458
            wp_die( $frm_settings->admin_permission );
Loading history...
458
        }
459
460
        global $wpdb;
461
		$params = FrmForm::get_admin_params();
462
        $message = '';
463
        $errors = array();
464
        $form_id = (int) $params['form'];
465
466
        if ( $form_id ) {
467
            $entry_ids = FrmDb::get_col( 'frm_items', array( 'form_id' => $form_id ) );
468
			$action = FrmFormAction::get_action_for_form( $form_id, 'wppost', 1 );
469
470
            if ( $action ) {
471
                // this action takes a while, so only trigger it if there are posts to delete
472
                foreach ( $entry_ids as $entry_id ) {
473
                    do_action( 'frm_before_destroy_entry', $entry_id );
0 ignored issues
show
Bug introduced by
The function do_action was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

473
                    /** @scrutinizer ignore-call */ 
474
                    do_action( 'frm_before_destroy_entry', $entry_id );
Loading history...
474
                    unset( $entry_id );
475
                }
476
            }
477
478
            $wpdb->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) and form_id=%d", $form_id ) );
479
            $results = $wpdb->query( $wpdb->prepare( "DELETE FROM {$wpdb->prefix}frm_items WHERE form_id=%d", $form_id ) );
480
            if ( $results ) {
481
				FrmEntry::clear_cache();
482
                $message = __( 'Entries were Successfully Destroyed', 'formidable' );
0 ignored issues
show
Bug introduced by
The function __ was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

482
                $message = /** @scrutinizer ignore-call */ __( 'Entries were Successfully Destroyed', 'formidable' );
Loading history...
483
            }
484
        } else {
485
            $errors = __( 'No entries were specified', 'formidable' );
486
        }
487
488
        self::display_list( $message, $errors );
489
    }
490
491
    public static function show_form( $id = '', $key = '', $title = false, $description = false ) {
492
        _deprecated_function( __FUNCTION__, '1.07.05', 'FrmFormsController::show_form()' );
0 ignored issues
show
Bug introduced by
The function _deprecated_function was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

492
        /** @scrutinizer ignore-call */ 
493
        _deprecated_function( __FUNCTION__, '1.07.05', 'FrmFormsController::show_form()' );
Loading history...
493
        return FrmFormsController::show_form( $id, $key, $title, $description );
494
    }
495
496
    public static function get_form( $filename, $form, $title, $description ) {
497
        _deprecated_function( __FUNCTION__, '1.07.05', 'FrmFormsController::get_form()' );
0 ignored issues
show
Bug introduced by
The function _deprecated_function was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

497
        /** @scrutinizer ignore-call */ 
498
        _deprecated_function( __FUNCTION__, '1.07.05', 'FrmFormsController::get_form()' );
Loading history...
498
        return FrmFormsController::get_form( $form, $title, $description );
499
    }
500
501
    public static function process_entry( $errors = '', $ajax = false ) {
502
		$form_id = FrmAppHelper::get_post_param( 'form_id', '', 'absint' );
503
		if ( FrmAppHelper::is_admin() || empty( $_POST ) || empty( $form_id ) || ! isset( $_POST['item_key'] ) ) {
504
            return;
505
        }
506
507
        global $frm_vars;
508
509
		$form = FrmForm::getOne( $form_id );
510
        if ( ! $form ) {
0 ignored issues
show
introduced by
The condition ! $form can never be false.
Loading history...
511
            return;
512
        }
513
514
		$params = FrmForm::get_params( $form );
515
516
        if ( ! isset( $frm_vars['form_params'] ) ) {
517
            $frm_vars['form_params'] = array();
518
        }
519
		$frm_vars['form_params'][ $form->id ] = $params;
520
521
		if ( isset( $frm_vars['created_entries'][ $form_id ] ) ) {
522
            return;
523
        }
524
525
        if ( $errors == '' && ! $ajax ) {
526
			$errors = FrmEntryValidate::validate( $_POST );
527
        }
528
529
		/**
530
		 * Use this filter to add trigger actions and add errors after
531
		 * all other errors have been processed
532
		 * @since 2.0.6
533
		 */
534
		$errors = apply_filters( 'frm_entries_before_create', $errors, $form );
0 ignored issues
show
Bug introduced by
The function apply_filters was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

534
		$errors = /** @scrutinizer ignore-call */ apply_filters( 'frm_entries_before_create', $errors, $form );
Loading history...
535
536
		$frm_vars['created_entries'][ $form_id ] = array( 'errors' => $errors );
537
538
        if ( empty( $errors ) ) {
539
			$_POST['frm_skip_cookie'] = 1;
540
			$do_success = false;
541
            if ( $params['action'] == 'create' ) {
542
				if ( apply_filters( 'frm_continue_to_create', true, $form_id ) && ! isset( $frm_vars['created_entries'][ $form_id ]['entry_id'] ) ) {
543
					$frm_vars['created_entries'][ $form_id ]['entry_id'] = FrmEntry::create( $_POST );
544
					$params['id'] = $frm_vars['created_entries'][ $form_id ]['entry_id'];
545
					$do_success = true;
546
                }
547
            }
548
549
            do_action( 'frm_process_entry', $params, $errors, $form, array( 'ajax' => $ajax ) );
0 ignored issues
show
Bug introduced by
The function do_action was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

549
            /** @scrutinizer ignore-call */ 
550
            do_action( 'frm_process_entry', $params, $errors, $form, array( 'ajax' => $ajax ) );
Loading history...
550
			if ( $do_success ) {
551
				FrmFormsController::maybe_trigger_redirect( $form, $params, array( 'ajax' => $ajax ) );
552
			}
553
			unset( $_POST['frm_skip_cookie'] );
554
        }
555
    }
556
557
	/**
558
	 * Escape url entities before redirect
559
	 *
560
	 * @since 3.0
561
	 *
562
	 * @param string $url
563
	 * @return string
564
	 */
565
	public static function prepare_redirect_url( $url ) {
566
		return str_replace( array( ' ', '[', ']', '|', '@' ), array( '%20', '%5B', '%5D', '%7C', '%40' ), $url );
567
	}
568
569
    public static function delete_entry_before_redirect( $url, $form, $atts ) {
570
        self::_delete_entry( $atts['id'], $form );
571
        return $url;
572
    }
573
574
    //Delete entry if not redirected
575
    public static function delete_entry_after_save( $atts ) {
576
        self::_delete_entry( $atts['entry_id'], $atts['form'] );
577
    }
578
579
    private static function _delete_entry( $entry_id, $form ) {
580
        if ( ! $form ) {
581
            return;
582
        }
583
584
        $form->options = maybe_unserialize( $form->options );
0 ignored issues
show
Bug introduced by
The function maybe_unserialize was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

584
        $form->options = /** @scrutinizer ignore-call */ maybe_unserialize( $form->options );
Loading history...
585
        if ( isset( $form->options['no_save'] ) && $form->options['no_save'] ) {
586
			self::unlink_post( $entry_id );
587
            FrmEntry::destroy( $entry_id );
588
        }
589
    }
590
591
	/**
592
	 * unlink entry from post
593
	 */
594
	private static function unlink_post( $entry_id ) {
595
		global $wpdb;
596
		$wpdb->update( $wpdb->prefix . 'frm_items', array( 'post_id' => '' ), array( 'id' => $entry_id ) );
597
		FrmEntry::clear_cache();
598
	}
599
600
	/**
601
	 * @param $atts
602
	 *
603
	 * @return array|string
604
	 */
605
	public static function show_entry_shortcode( $atts ) {
606
		$defaults = apply_filters( 'frm_show_entry_defaults', array(
0 ignored issues
show
Bug introduced by
The function apply_filters was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

606
		$defaults = /** @scrutinizer ignore-call */ apply_filters( 'frm_show_entry_defaults', array(
Loading history...
607
			'id'             => false,
608
			'entry'          => false,
609
			'fields'         => false,
610
			'plain_text'     => false,
611
			'user_info'      => false,
612
			'include_blank'  => false,
613
			'default_email'  => false,
614
			'form_id'        => false,
615
			'format'         => 'text',
616
			'array_key'      => 'key',
617
			'direction'      => 'ltr',
618
			'font_size'      => '',
619
			'text_color'     => '',
620
			'border_width'   => '',
621
			'border_color'   => '',
622
			'bg_color'       => '',
623
			'alt_bg_color'   => '',
624
			'clickable'      => false,
625
			'exclude_fields' => '',
626
			'include_fields' => '',
627
			'include_extras' => '',
628
			'inline_style'   => 1,
629
			'child_array'    => false, // return embedded fields as nested array
630
		) );
631
632
		$atts = shortcode_atts( $defaults, $atts );
0 ignored issues
show
Bug introduced by
The function shortcode_atts was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

632
		$atts = /** @scrutinizer ignore-call */ shortcode_atts( $defaults, $atts );
Loading history...
633
634
		if ( $atts['default_email'] ) {
635
			$shortcode_atts = array(
636
				'format'     => $atts['format'],
637
				'plain_text' => $atts['plain_text'],
638
			);
639
			$entry_shortcode_formatter = FrmEntryFactory::entry_shortcode_formatter_instance( $atts['form_id'], $shortcode_atts );
640
			$formatted_entry = $entry_shortcode_formatter->content();
641
642
		} else {
643
644
			$entry_formatter = FrmEntryFactory::entry_formatter_instance( $atts );
645
			$formatted_entry = $entry_formatter->get_formatted_entry_values();
646
647
		}
648
649
		return $formatted_entry;
650
	}
651
652
	public static function entry_sidebar( $entry ) {
653
		$data = maybe_unserialize( $entry->description );
0 ignored issues
show
Bug introduced by
The function maybe_unserialize was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

653
		$data = /** @scrutinizer ignore-call */ maybe_unserialize( $entry->description );
Loading history...
654
		$date_format = get_option( 'date_format' );
0 ignored issues
show
Bug introduced by
The function get_option was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

654
		$date_format = /** @scrutinizer ignore-call */ get_option( 'date_format' );
Loading history...
655
		$time_format = get_option( 'time_format' );
656
		if ( isset( $data['browser'] ) ) {
657
			$browser = FrmEntriesHelper::get_browser( $data['browser'] );
658
		}
659
660
		include( FrmAppHelper::plugin_path() . '/classes/views/frm-entries/sidebar-shared.php' );
661
    }
662
}
663