Completed
Branch uploads (08d8c9)
by Stephanie
04:20
created

FrmFormsController::show_form()   B

Complexity

Conditions 6
Paths 10

Size

Total Lines 37
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 6
eloc 20
nc 10
nop 5
dl 0
loc 37
rs 8.439
c 0
b 0
f 0
1
<?php
2
3
class FrmFormsController {
4
5
    public static function menu() {
6
		$menu_label = __( 'Forms', 'formidable' );
7
		if ( ! FrmAppHelper::pro_is_installed() ) {
8
			$menu_label .= ' (Lite)';
9
		}
10
		add_submenu_page('formidable', 'Formidable | ' . $menu_label, $menu_label, 'frm_view_forms', 'formidable', 'FrmFormsController::route' );
11
12
		self::maybe_load_listing_hooks();
13
    }
14
15
	public static function maybe_load_listing_hooks() {
16
		$action = FrmAppHelper::simple_get( 'frm_action', 'sanitize_title' );
17
		if ( ! empty( $action ) && ! in_array( $action, array( 'list', 'trash', 'untrash' ) ) ) {
18
			return;
19
		}
20
21
		add_filter('get_user_option_managetoplevel_page_formidablecolumnshidden', 'FrmFormsController::hidden_columns' );
22
23
		add_filter('manage_toplevel_page_formidable_columns', 'FrmFormsController::get_columns', 0 );
24
		add_filter('manage_toplevel_page_formidable_sortable_columns', 'FrmFormsController::get_sortable_columns' );
25
	}
26
27
    public static function head() {
28
        wp_enqueue_script('formidable-editinplace');
29
30
        if ( wp_is_mobile() ) {
31
    		wp_enqueue_script( 'jquery-touch-punch' );
32
    	}
33
    }
34
35
    public static function register_widgets() {
36
        require_once(FrmAppHelper::plugin_path() . '/classes/widgets/FrmShowForm.php');
37
        register_widget('FrmShowForm');
38
    }
39
40
    public static function list_form() {
41
        FrmAppHelper::permission_check('frm_view_forms');
42
43
		$params = FrmForm::list_page_params();
44
        $errors = self::process_bulk_form_actions( array());
45
        $errors = apply_filters('frm_admin_list_form_action', $errors);
46
47
		return self::display_forms_list( $params, '', $errors );
48
    }
49
50
	public static function new_form( $values = array() ) {
51
        FrmAppHelper::permission_check('frm_edit_forms');
52
53
        global $frm_vars;
54
55
        $action = isset($_REQUEST['frm_action']) ? 'frm_action' : 'action';
0 ignored issues
show
introduced by
Detected access of super global var $_REQUEST, probably need manual inspection.
Loading history...
56
		$action = empty( $values ) ? FrmAppHelper::get_param( $action, '', 'get', 'sanitize_title' ) : $values[ $action ];
57
58
		if ( $action == 'create' ) {
59
            return self::create($values);
60
		} else if ( $action == 'new' ) {
61
			$frm_field_selection = FrmField::field_selection();
62
            $values = FrmFormsHelper::setup_new_vars($values);
63
            $id = FrmForm::create( $values );
64
            $form = FrmForm::getOne($id);
65
66
            // add default email notification
67
            $action_control = FrmFormActionsController::get_form_actions( 'email' );
68
            $action_control->create($form->id);
69
70
			$all_templates = FrmForm::getAll( array( 'is_template' => 1 ), 'name' );
71
72
            $values['id'] = $id;
73
			require( FrmAppHelper::plugin_path() . '/classes/views/frm-forms/new.php' );
74
        }
75
    }
76
77
	public static function create( $values = array() ) {
78
        FrmAppHelper::permission_check('frm_edit_forms');
79
80
        global $frm_vars;
81
        if ( empty( $values ) ) {
82
            $values = $_POST;
0 ignored issues
show
introduced by
Detected access of super global var $_POST, probably need manual inspection.
Loading history...
introduced by
Detected usage of a non-sanitized input variable: $_POST
Loading history...
83
        }
84
85
        //Set radio button and checkbox meta equal to "other" value
86
        if ( FrmAppHelper::pro_is_installed() ) {
87
            $values = FrmProEntry::mod_other_vals( $values, 'back' );
88
        }
89
90
		$id = isset($values['id']) ? absint( $values['id'] ) : FrmAppHelper::get_param( 'id', '', 'get', 'absint' );
91
92
        if ( ! current_user_can( 'frm_edit_forms' ) || ( $_POST && ( ! isset( $values['frm_save_form'] ) || ! wp_verify_nonce( $values['frm_save_form'], 'frm_save_form_nonce' ) ) ) ) {
93
            $frm_settings = FrmAppHelper::get_settings();
94
            $errors = array( 'form' => $frm_settings->admin_permission );
95
        } else {
96
            $errors = FrmForm::validate($values);
97
        }
98
99
        if ( count($errors) > 0 ) {
100
            $hide_preview = true;
101
			$frm_field_selection = FrmField::field_selection();
102
            $form = FrmForm::getOne( $id );
103
            $fields = FrmField::get_all_for_form($id);
104
105
            $values = FrmAppHelper::setup_edit_vars($form, 'forms', $fields, true);
106
			$all_templates = FrmForm::getAll( array( 'is_template' => 1 ), 'name' );
107
108
			require( FrmAppHelper::plugin_path() . '/classes/views/frm-forms/new.php' );
109
        } else {
110
            FrmForm::update( $id, $values, true );
111
			$url = admin_url( 'admin.php?page=formidable&frm_action=settings&id=' . $id );
112
			die( FrmAppHelper::js_redirect( $url ) );
113
        }
114
    }
115
116
    public static function edit( $values = false ) {
117
        FrmAppHelper::permission_check('frm_edit_forms');
118
119
		$id = isset( $values['id'] ) ? absint( $values['id'] ) : FrmAppHelper::get_param( 'id', '', 'get', 'absint' );
120
        return self::get_edit_vars($id);
121
    }
122
123
    public static function settings( $id = false, $message = '' ) {
124
        FrmAppHelper::permission_check('frm_edit_forms');
125
126
        if ( ! $id || ! is_numeric($id) ) {
127
			$id = FrmAppHelper::get_param( 'id', '', 'get', 'absint' );
128
        }
129
		return self::get_settings_vars( $id, array(), $message );
130
    }
131
132
    public static function update_settings() {
133
        FrmAppHelper::permission_check('frm_edit_forms');
134
135
		$id = FrmAppHelper::get_param( 'id', '', 'get', 'absint' );
136
137
        $errors = FrmForm::validate($_POST);
0 ignored issues
show
introduced by
Detected access of super global var $_POST, probably need manual inspection.
Loading history...
138
        if ( count($errors) > 0 ) {
139
            return self::get_settings_vars($id, $errors);
140
        }
141
142
        do_action('frm_before_update_form_settings', $id);
143
144
		FrmForm::update( $id, $_POST );
0 ignored issues
show
introduced by
Detected access of super global var $_POST, probably need manual inspection.
Loading history...
145
146
        $message = __( 'Settings Successfully Updated', 'formidable' );
147
		return self::get_settings_vars( $id, array(), $message );
148
    }
149
150
	public static function edit_key() {
151
		$values = self::edit_in_place_value( 'form_key' );
152
		echo wp_kses( stripslashes( FrmForm::getKeyById( $values['form_id'] ) ), array() );
0 ignored issues
show
Documentation introduced by
$values['form_id'] is of type string|array, but the function expects a integer.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
153
		wp_die();
154
	}
155
156
	public static function edit_description() {
157
		$values = self::edit_in_place_value( 'description' );
158
		echo wp_kses_post( FrmAppHelper::use_wpautop( stripslashes( $values['description'] ) ) );
159
		wp_die();
160
	}
161
162
	private static function edit_in_place_value( $field ) {
163
		check_ajax_referer( 'frm_ajax', 'nonce' );
164
		FrmAppHelper::permission_check('frm_edit_forms', 'hide');
165
166
		$form_id = FrmAppHelper::get_post_param( 'form_id', '', 'absint' );
167
		$value = FrmAppHelper::get_post_param( 'update_value', '', 'wp_filter_post_kses' );
168
169
		$values = array( $field => trim( $value ) );
170
		FrmForm::update( $form_id, $values );
171
		$values['form_id'] = $form_id;
172
173
		return $values;
174
	}
175
176
	public static function update( $values = array() ) {
177
		if ( empty( $values ) ) {
178
            $values = $_POST;
0 ignored issues
show
introduced by
Detected access of super global var $_POST, probably need manual inspection.
Loading history...
introduced by
Detected usage of a non-sanitized input variable: $_POST
Loading history...
179
        }
180
181
        //Set radio button and checkbox meta equal to "other" value
182
        if ( FrmAppHelper::pro_is_installed() ) {
183
            $values = FrmProEntry::mod_other_vals( $values, 'back' );
184
        }
185
186
        $errors = FrmForm::validate( $values );
187
        $permission_error = FrmAppHelper::permission_nonce_error( 'frm_edit_forms', 'frm_save_form', 'frm_save_form_nonce' );
188
        if ( $permission_error !== false ) {
189
            $errors['form'] = $permission_error;
190
        }
191
192
		$id = isset( $values['id'] ) ? absint( $values['id'] ) : FrmAppHelper::get_param( 'id', '', 'get', 'absint' );
193
194
		if ( count( $errors ) > 0 ) {
195
            return self::get_edit_vars( $id, $errors );
196
		} else {
197
            FrmForm::update( $id, $values );
198
            $message = __( 'Form was Successfully Updated', 'formidable' );
199
            if ( defined( 'DOING_AJAX' ) ) {
200
				wp_die( $message );
201
            }
202
			return self::get_edit_vars( $id, array(), $message );
203
        }
204
    }
205
206
    public static function bulk_create_template( $ids ) {
207
        FrmAppHelper::permission_check( 'frm_edit_forms' );
208
209
        foreach ( $ids as $id ) {
210
            FrmForm::duplicate( $id, true, true );
211
        }
212
213
        return __( 'Form template was Successfully Created', 'formidable' );
214
    }
215
216
	/**
217
	 * Redirect to the url for creating from a template
218
	 * Also delete the current form
219
	 * @since 2.0
220
	 */
221
	public static function _create_from_template() {
222
		FrmAppHelper::permission_check('frm_edit_forms');
223
		check_ajax_referer( 'frm_ajax', 'nonce' );
224
225
		$current_form = FrmAppHelper::get_param( 'this_form', '', 'get', 'absint' );
226
		$template_id = FrmAppHelper::get_param( 'id', '', 'get', 'absint' );
227
228
		if ( $current_form ) {
229
			FrmForm::destroy( $current_form );
230
		}
231
232
		echo esc_url_raw( admin_url( 'admin.php?page=formidable&action=duplicate&id=' . $template_id ) );
233
		wp_die();
234
	}
235
236
    public static function duplicate() {
237
        FrmAppHelper::permission_check('frm_edit_forms');
238
239
		$params = FrmForm::list_page_params();
240
        $form = FrmForm::duplicate( $params['id'], $params['template'], true );
241
        $message = ($params['template']) ? __( 'Form template was Successfully Created', 'formidable' ) : __( 'Form was Successfully Copied', 'formidable' );
242
        if ( $form ) {
243
			return self::get_edit_vars( $form, array(), $message, true );
244
        } else {
245
            return self::display_forms_list($params, __( 'There was a problem creating the new template.', 'formidable' ));
246
        }
247
    }
248
249
    public static function page_preview() {
250
		$params = FrmForm::list_page_params();
251
        if ( ! $params['form'] ) {
252
            return;
253
        }
254
255
        $form = FrmForm::getOne( $params['form'] );
256
        if ( ! $form ) {
257
            return;
258
        }
259
        return self::show_form( $form->id, '', true, true );
260
    }
261
262
    public static function preview() {
263
        do_action( 'frm_wp' );
264
265
        global $frm_vars;
266
        $frm_vars['preview'] = true;
267
268
        if ( ! defined( 'ABSPATH' ) && ! defined( 'XMLRPC_REQUEST' ) ) {
269
            global $wp;
270
            $root = dirname( dirname( dirname( dirname( __FILE__ ) ) ) );
271
			include_once( $root . '/wp-config.php' );
272
            $wp->init();
273
            $wp->register_globals();
274
        }
275
276
		self::register_pro_scripts();
277
278
		header( 'Content-Type: text/html; charset=' . get_option( 'blog_charset' ) );
279
280
		$key = FrmAppHelper::simple_get( 'form', 'sanitize_title' );
281
		if ( $key == '' ) {
282
			$key = FrmAppHelper::get_post_param( 'form', '', 'sanitize_title' );
283
		}
284
285
		$form = FrmForm::getAll( array( 'form_key' => $key ), '', 1 );
286
		if ( empty( $form ) ) {
287
			$form = FrmForm::getAll( array(), '', 1 );
288
        }
289
290
		require( FrmAppHelper::plugin_path() . '/classes/views/frm-entries/direct.php' );
291
        wp_die();
292
    }
293
294
	public static function register_pro_scripts() {
295
		if ( FrmAppHelper::pro_is_installed() ) {
296
			wp_register_script( 'jquery-frm-rating', FrmAppHelper::plugin_url() . '/pro/js/jquery.rating.min.js', array( 'jquery' ), '4.11', true );
297
			wp_register_script( 'jquery-maskedinput', FrmAppHelper::plugin_url() . '/pro/js/jquery.maskedinput.min.js', array( 'jquery' ), '1.4', true );
298
			wp_register_script( 'jquery-chosen', FrmAppHelper::plugin_url() . '/pro/js/chosen.jquery.min.js', array( 'jquery' ), '1.5.1', true );
299
			wp_register_script( 'dropzone', FrmAppHelper::plugin_url() . '/pro/js/dropzone.js', array( 'jquery' ), '4.3.0', true );
300
		}
301
	}
302
303
    public static function untrash() {
304
		self::change_form_status( 'untrash' );
305
    }
306
307
	public static function bulk_untrash( $ids ) {
308
        FrmAppHelper::permission_check('frm_edit_forms');
309
310
        $count = FrmForm::set_status( $ids, 'published' );
311
312
        $message = sprintf(_n( '%1$s form restored from the Trash.', '%1$s forms restored from the Trash.', $count, 'formidable' ), 1 );
313
        return $message;
314
    }
315
316
    public static function trash() {
317
		self::change_form_status( 'trash' );
318
    }
319
320
	/**
321
	 * @param string $status
322
	 *
323
	 * @return int The number of forms changed
324
	 */
325
	public static function change_form_status( $status ) {
326
		$available_status = array(
327
			'untrash' => array( 'permission' => 'frm_edit_forms', 'new_status' => 'published' ),
328
			'trash'   => array( 'permission' => 'frm_delete_forms', 'new_status' => 'trash' ),
329
		);
330
331
		if ( ! isset( $available_status[ $status ] ) ) {
332
			return;
333
		}
334
335
		FrmAppHelper::permission_check( $available_status[ $status ]['permission'] );
336
337
		$params = FrmForm::list_page_params();
338
339
		//check nonce url
340
		check_admin_referer( $status . '_form_' . $params['id'] );
341
342
		$count = 0;
343
		if ( FrmForm::set_status( $params['id'], $available_status[ $status ]['new_status'] ) ) {
344
			$count++;
345
		}
346
347
		$available_status['untrash']['message'] = sprintf(_n( '%1$s form restored from the Trash.', '%1$s forms restored from the Trash.', $count, 'formidable' ), $count );
348
		$available_status['trash']['message'] = sprintf( _n( '%1$s form moved to the Trash. %2$sUndo%3$s', '%1$s forms moved to the Trash. %2$sUndo%3$s', $count, 'formidable' ), $count, '<a href="' . esc_url( wp_nonce_url( '?page=formidable&frm_action=untrash&form_type=' . ( isset( $_REQUEST['form_type'] ) ? sanitize_title( $_REQUEST['form_type'] ) : '' ) . '&id=' . $params['id'], 'untrash_form_' . $params['id'] ) ) . '">', '</a>' );
0 ignored issues
show
introduced by
Detected access of super global var $_REQUEST, probably need manual inspection.
Loading history...
349
350
		$message = $available_status[ $status ]['message'];
351
352
		self::display_forms_list( $params, $message );
353
	}
354
355
	public static function bulk_trash( $ids ) {
356
        FrmAppHelper::permission_check('frm_delete_forms');
357
358
        $count = 0;
359
        foreach ( $ids as $id ) {
360
            if ( FrmForm::trash( $id ) ) {
361
                $count++;
362
            }
363
        }
364
365
        $current_page = isset( $_REQUEST['form_type'] ) ? $_REQUEST['form_type'] : '';
0 ignored issues
show
introduced by
Detected access of super global var $_REQUEST, probably need manual inspection.
Loading history...
introduced by
Detected usage of a non-sanitized input variable: $_REQUEST
Loading history...
366
		$message = sprintf( _n( '%1$s form moved to the Trash. %2$sUndo%3$s', '%1$s forms moved to the Trash. %2$sUndo%3$s', $count, 'formidable' ), $count, '<a href="' . esc_url( wp_nonce_url( '?page=formidable&frm_action=list&action=bulk_untrash&form_type=' . $current_page . '&item-action=' . implode( ',', $ids ), 'bulk-toplevel_page_formidable' ) ) . '">', '</a>' );
367
368
        return $message;
369
    }
370
371
    public static function destroy() {
372
        FrmAppHelper::permission_check('frm_delete_forms');
373
374
		$params = FrmForm::list_page_params();
375
376
        //check nonce url
377
        check_admin_referer('destroy_form_' . $params['id']);
378
379
        $count = 0;
380
        if ( FrmForm::destroy( $params['id'] ) ) {
381
            $count++;
382
        }
383
384
        $message = sprintf(_n( '%1$s form permanently deleted.', '%1$s forms permanently deleted.', $count, 'formidable' ), $count);
385
386
		self::display_forms_list( $params, $message );
387
    }
388
389
	public static function bulk_destroy( $ids ) {
390
        FrmAppHelper::permission_check('frm_delete_forms');
391
392
        $count = 0;
393
        foreach ( $ids as $id ) {
394
            $d = FrmForm::destroy( $id );
395
            if ( $d ) {
396
                $count++;
397
            }
398
        }
399
400
        $message = sprintf(_n( '%1$s form permanently deleted.', '%1$s forms permanently deleted.', $count, 'formidable' ), $count);
401
402
        return $message;
403
    }
404
405
    private static function delete_all() {
406
        //check nonce url
407
        $permission_error = FrmAppHelper::permission_nonce_error('frm_delete_forms', '_wpnonce', 'bulk-toplevel_page_formidable');
408
        if ( $permission_error !== false ) {
409
			self::display_forms_list( array(), '', array( $permission_error ) );
410
            return;
411
        }
412
413
		$count = FrmForm::scheduled_delete( time() );
414
        $message = sprintf(_n( '%1$s form permanently deleted.', '%1$s forms permanently deleted.', $count, 'formidable' ), $count);
415
416
		self::display_forms_list( array(), $message );
417
    }
418
419
	public static function scheduled_delete( $delete_timestamp = '' ) {
420
		_deprecated_function( __FUNCTION__, '2.0.9', 'FrmForm::scheduled_delete' );
421
		return FrmForm::scheduled_delete( $delete_timestamp );
422
	}
423
424
	/**
425
	* Inserts Formidable button
426
	* Hook exists since 2.5.0
427
	*
428
	* @since 2.0.15
429
	*/
430
	public static function insert_form_button() {
431
		if ( current_user_can('frm_view_forms') ) {
432
			$menu_name = FrmAppHelper::get_menu_name();
433
			$content = '<a href="#TB_inline?width=50&height=50&inlineId=frm_insert_form" class="thickbox button add_media frm_insert_form" title="' . esc_attr__( 'Add forms and content', 'formidable' ) . '">
434
				<span class="frm-buttons-icon wp-media-buttons-icon"></span> ' .
435
				$menu_name . '</a>';
436
			echo wp_kses_post( $content );
437
		}
438
	}
439
440
    public static function insert_form_popup() {
441
		$page = basename( FrmAppHelper::get_server_value( 'PHP_SELF' ) );
442
		if ( ! in_array( $page, array( 'post.php', 'page.php', 'page-new.php', 'post-new.php' ) ) ) {
443
            return;
444
        }
445
446
        FrmAppHelper::load_admin_wide_js();
447
448
        $shortcodes = array(
449
			'formidable' => array( 'name' => __( 'Form', 'formidable' ), 'label' => __( 'Insert a Form', 'formidable' ) ),
450
        );
451
452
        $shortcodes = apply_filters('frm_popup_shortcodes', $shortcodes);
453
454
		include( FrmAppHelper::plugin_path() . '/classes/views/frm-forms/insert_form_popup.php' );
455
    }
456
457
    public static function get_shortcode_opts() {
458
		FrmAppHelper::permission_check('frm_view_forms');
459
        check_ajax_referer( 'frm_ajax', 'nonce' );
460
461
		$shortcode = FrmAppHelper::get_post_param( 'shortcode', '', 'sanitize_text_field' );
462
        if ( empty($shortcode) ) {
463
            wp_die();
464
        }
465
466
		echo '<div id="sc-opts-' . esc_attr( $shortcode ) . '" class="frm_shortcode_option">';
467
		echo '<input type="radio" name="frmsc" value="' . esc_attr( $shortcode ) . '" id="sc-' . esc_attr( $shortcode ) . '" class="frm_hidden" />';
468
469
        $form_id = '';
470
        $opts = array();
471
		switch ( $shortcode ) {
472
            case 'formidable':
473
                $opts = array(
474
					'form_id'       => 'id',
475
                    //'key' => ',
476
					'title'         => array( 'val' => 1, 'label' => __( 'Display form title', 'formidable' ) ),
477
					'description'   => array( 'val' => 1, 'label' => __( 'Display form description', 'formidable' ) ),
478
					'minimize'      => array( 'val' => 1, 'label' => __( 'Minimize form HTML', 'formidable' ) ),
479
                );
480
            break;
481
        }
482
        $opts = apply_filters('frm_sc_popup_opts', $opts, $shortcode);
483
484
		if ( isset( $opts['form_id'] ) && is_string( $opts['form_id'] ) ) {
485
			// allow other shortcodes to use the required form id option
486
			$form_id = $opts['form_id'];
487
			unset( $opts['form_id'] );
488
		}
489
490
		include( FrmAppHelper::plugin_path() . '/classes/views/frm-forms/shortcode_opts.php' );
491
492
        echo '</div>';
493
494
        wp_die();
495
    }
496
497
	public static function display_forms_list( $params = array(), $message = '', $errors = array(), $deprecated_errors = array() ) {
498
        FrmAppHelper::permission_check( 'frm_view_forms' );
499
		if ( ! empty( $deprecated_errors ) ) {
500
			$errors = $deprecated_errors;
501
			_deprecated_argument( 'errors', '2.0.8' );
502
		}
503
504
        global $wpdb, $frm_vars;
505
506
		if ( empty( $params ) ) {
507
			$params = FrmForm::list_page_params();
508
        }
509
510
        $wp_list_table = new FrmFormsListHelper( compact( 'params' ) );
511
512
        $pagenum = $wp_list_table->get_pagenum();
513
514
        $wp_list_table->prepare_items();
515
516
        $total_pages = $wp_list_table->get_pagination_arg( 'total_pages' );
517
        if ( $pagenum > $total_pages && $total_pages > 0 ) {
518
			wp_redirect( esc_url_raw( add_query_arg( 'paged', $total_pages ) ) );
519
            die();
520
        }
521
522
		require( FrmAppHelper::plugin_path() . '/classes/views/frm-forms/list.php' );
523
    }
524
525
	public static function get_columns( $columns ) {
526
	    $columns['cb'] = '<input type="checkbox" />';
527
	    $columns['id'] = 'ID';
528
529
        $type = isset( $_REQUEST['form_type'] ) ? $_REQUEST['form_type'] : 'published';
0 ignored issues
show
introduced by
Detected access of super global var $_REQUEST, probably need manual inspection.
Loading history...
introduced by
Detected usage of a non-sanitized input variable: $_REQUEST
Loading history...
530
531
        if ( 'template' == $type ) {
532
            $columns['name']        = __( 'Template Name', 'formidable' );
533
            $columns['type']        = __( 'Type', 'formidable' );
534
            $columns['form_key']    = __( 'Key', 'formidable' );
535
        } else {
536
            $columns['name']        = __( 'Form Title', 'formidable' );
537
            $columns['entries']     = __( 'Entries', 'formidable' );
538
            $columns['form_key']    = __( 'Key', 'formidable' );
539
            $columns['shortcode']   = __( 'Shortcodes', 'formidable' );
540
        }
541
542
        $columns['created_at'] = __( 'Date', 'formidable' );
543
544
		add_screen_option( 'per_page', array( 'label' => __( 'Forms', 'formidable' ), 'default' => 20, 'option' => 'formidable_page_formidable_per_page' ) );
545
546
        return $columns;
547
	}
548
549
	public static function get_sortable_columns() {
550
		return array(
551
			'id'            => 'id',
552
			'name'          => 'name',
553
			'description'   => 'description',
554
			'form_key'      => 'form_key',
555
			'created_at'    => 'created_at',
556
		);
557
	}
558
559
	public static function hidden_columns( $result ) {
560
        $return = false;
561
        foreach ( (array) $result as $r ) {
562
            if ( ! empty( $r ) ) {
563
                $return = true;
564
                break;
565
            }
566
        }
567
568
        if ( $return ) {
569
            return $result;
570
		}
571
572
        $type = isset( $_REQUEST['form_type'] ) ? $_REQUEST['form_type'] : '';
0 ignored issues
show
introduced by
Detected access of super global var $_REQUEST, probably need manual inspection.
Loading history...
introduced by
Detected usage of a non-sanitized input variable: $_REQUEST
Loading history...
573
574
        $result[] = 'created_at';
575
        if ( $type == 'template' ) {
576
            $result[] = 'id';
577
            $result[] = 'form_key';
578
        }
579
580
        return $result;
581
    }
582
583
	public static function save_per_page( $save, $option, $value ) {
584
        if ( $option == 'formidable_page_formidable_per_page' ) {
585
            $save = (int) $value;
586
        }
587
        return $save;
588
    }
589
590
	private static function get_edit_vars( $id, $errors = array(), $message = '', $create_link = false ) {
591
        global $frm_vars;
592
593
        $form = FrmForm::getOne( $id );
594
        if ( ! $form ) {
595
            wp_die( __( 'You are trying to edit a form that does not exist.', 'formidable' ) );
596
        }
597
598
        if ( $form->parent_form_id ) {
599
			wp_die( sprintf( __( 'You are trying to edit a child form. Please edit from %1$shere%2$s', 'formidable' ), '<a href="' . esc_url( admin_url( 'admin.php?page=formidable&frm_action=edit&id=' . $form->parent_form_id ) ) . '">', '</a>' ));
600
        }
601
602
		$frm_field_selection = FrmField::field_selection();
603
        $fields = FrmField::get_all_for_form($form->id);
604
605
        // Automatically add end section fields if they don't exist (2.0 migration)
606
        $reset_fields = false;
607
        FrmFormsHelper::auto_add_end_section_fields( $form, $fields, $reset_fields );
608
609
        if ( $reset_fields ) {
610
            $fields = FrmField::get_all_for_form( $form->id, '', 'exclude' );
611
        }
612
613
        unset($end_section_values, $last_order, $open, $reset_fields);
614
615
		$args = array( 'parent_form_id' => $form->id );
616
        $values = FrmAppHelper::setup_edit_vars( $form, 'forms', $fields, true, array(), $args );
617
618
        $edit_message = __( 'Form was Successfully Updated', 'formidable' );
619
        if ( $form->is_template && $message == $edit_message ) {
620
            $message = __( 'Template was Successfully Updated', 'formidable' );
621
        }
622
623
		$all_templates = FrmForm::getAll( array( 'is_template' => 1 ), 'name' );
624
625
        if ( $form->default_template ) {
626
            wp_die(__( 'That template cannot be edited', 'formidable' ));
627
        } else if ( defined('DOING_AJAX') ) {
628
            wp_die();
629
        } else if ( $create_link ) {
630
			require( FrmAppHelper::plugin_path() . '/classes/views/frm-forms/new.php' );
631
        } else {
632
			require( FrmAppHelper::plugin_path() . '/classes/views/frm-forms/edit.php' );
633
        }
634
    }
635
636
	public static function get_settings_vars( $id, $errors = array(), $message = '' ) {
637
		FrmAppHelper::permission_check( 'frm_edit_forms' );
638
639
        global $frm_vars;
640
641
        $form = FrmForm::getOne( $id );
642
643
        $fields = FrmField::get_all_for_form($id);
644
        $values = FrmAppHelper::setup_edit_vars($form, 'forms', $fields, true);
645
646
        if ( isset($values['default_template']) && $values['default_template'] ) {
647
            wp_die(__( 'That template cannot be edited', 'formidable' ));
648
        }
649
650
        $action_controls = FrmFormActionsController::get_form_actions();
651
652
        $sections = apply_filters('frm_add_form_settings_section', array(), $values);
653
        $pro_feature = FrmAppHelper::pro_is_installed() ? '' : ' class="pro_feature"';
654
655
        $styles = apply_filters('frm_get_style_opts', array());
656
657
		require( FrmAppHelper::plugin_path() . '/classes/views/frm-forms/settings.php' );
658
    }
659
660
    public static function mb_tags_box( $form_id, $class = '' ) {
661
        $fields = FrmField::get_all_for_form($form_id, '', 'include');
662
        $linked_forms = array();
663
        $col = 'one';
664
        $settings_tab = FrmAppHelper::is_admin_page('formidable' ) ? true : false;
665
666
		$cond_shortcodes = apply_filters( 'frm_conditional_shortcodes', array() );
667
		$adv_shortcodes = self::get_advanced_shortcodes();
668
		$user_fields = apply_filters( 'frm_user_shortcodes', array() );
669
		$entry_shortcodes = self::get_shortcode_helpers( $settings_tab );
670
671
		include( FrmAppHelper::plugin_path() . '/classes/views/shared/mb_adv_info.php' );
672
    }
673
674
	/**
675
	 * Get an array of the options to display in the advanced tab
676
	 * of the customization panel
677
	 * @since 2.0.6
678
	 */
679
	private static function get_advanced_shortcodes() {
680
		$adv_shortcodes = array(
681
			'sep=", "'       => array(
682
				'label' => __( 'Separator', 'formidable' ),
683
				'title' => __( 'Use a different separator for checkbox fields', 'formidable' ),
684
			),
685
			'format="d-m-Y"' => __( 'Date Format', 'formidable' ),
686
			'show="field_label"' => __( 'Field Label', 'formidable' ),
687
			'wpautop=0'      => array(
688
				'label' => __( 'No Auto P', 'formidable' ),
689
				'title' => __( 'Do not automatically add any paragraphs or line breaks', 'formidable' ),
690
			),
691
		);
692
		$adv_shortcodes = apply_filters( 'frm_advanced_shortcodes', $adv_shortcodes );
693
		// __( 'Leave blank instead of defaulting to User Login', 'formidable' ) : blank=1
694
695
		return $adv_shortcodes;
696
	}
697
698
	/**
699
	 * Get an array of the helper shortcodes to display in the customization panel
700
	 * @since 2.0.6
701
	 */
702
	private static function get_shortcode_helpers( $settings_tab ) {
703
		$entry_shortcodes = array(
704
			'id'        => __( 'Entry ID', 'formidable' ),
705
			'key'       => __( 'Entry Key', 'formidable' ),
706
			'post_id'   => __( 'Post ID', 'formidable' ),
707
			'ip'        => __( 'User IP', 'formidable' ),
708
			'created-at' => __( 'Entry created', 'formidable' ),
709
			'updated-at' => __( 'Entry updated', 'formidable' ),
710
			''          => '',
711
			'siteurl'   => __( 'Site URL', 'formidable' ),
712
			'sitename'  => __( 'Site Name', 'formidable' ),
713
        );
714
715
		if ( ! FrmAppHelper::pro_is_installed() ) {
716
			unset( $entry_shortcodes['post_id'] );
717
		}
718
719
		if ( $settings_tab ) {
720
			$entry_shortcodes['default-message'] = __( 'Default Msg', 'formidable' );
721
			$entry_shortcodes['default-html'] = __( 'Default HTML', 'formidable' );
722
			$entry_shortcodes['default-plain'] = __( 'Default Plain', 'formidable' );
723
		} else {
724
			$entry_shortcodes['detaillink'] = __( 'Detail Link', 'formidable' );
725
			$entry_shortcodes['editlink location="front" label="Edit" page_id=x'] = __( 'Edit Entry Link', 'formidable' );
726
			$entry_shortcodes['evenodd'] = __( 'Even/Odd', 'formidable' );
727
			$entry_shortcodes['entry_count'] = __( 'Entry Count', 'formidable' );
728
		}
729
730
		/**
731
		 * Use this hook to add or remove buttons in the helpers section
732
		 * in the customization panel
733
		 * @since 2.0.6
734
		 */
735
		$entry_shortcodes = apply_filters( 'frm_helper_shortcodes', $entry_shortcodes, $settings_tab );
736
737
		return $entry_shortcodes;
738
	}
739
740
    // Insert the form class setting into the form
741
	public static function form_classes( $form ) {
742
        if ( isset($form->options['form_class']) ) {
743
			echo esc_attr( sanitize_text_field( $form->options['form_class'] ) );
744
        }
745
    }
746
747
    public static function get_email_html() {
748
		FrmAppHelper::permission_check('frm_view_forms');
749
        check_ajax_referer( 'frm_ajax', 'nonce' );
750
		echo FrmEntryFormat::show_entry( array(
0 ignored issues
show
introduced by
Expected a sanitizing function (see Codex for 'Data Validation'), but instead saw 'FrmEntryFormat'
Loading history...
751
			'form_id'       => FrmAppHelper::get_post_param( 'form_id', '', 'absint' ),
752
	        'default_email' => true,
753
			'plain_text'    => FrmAppHelper::get_post_param( 'plain_text', '', 'absint' ),
754
	    ) );
755
	    wp_die();
756
	}
757
758
    public static function filter_content( $content, $form, $entry = false ) {
759
		self::get_entry_by_param( $entry );
760
        if ( ! $entry ) {
761
            return $content;
762
        }
763
764
        if ( is_object( $form ) ) {
765
            $form = $form->id;
766
        }
767
768
        $shortcodes = FrmFieldsHelper::get_shortcodes( $content, $form );
769
        $content = apply_filters( 'frm_replace_content_shortcodes', $content, $entry, $shortcodes );
770
771
        return $content;
772
    }
773
774
	private static function get_entry_by_param( &$entry ) {
775
		if ( ! $entry || ! is_object( $entry ) ) {
776
			if ( ! $entry || ! is_numeric( $entry ) ) {
777
				$entry = FrmAppHelper::get_post_param( 'id', false, 'sanitize_title' );
0 ignored issues
show
Documentation introduced by
false is of type boolean, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
778
			}
779
780
			FrmEntry::maybe_get_entry( $entry );
781
		}
782
	}
783
784
    public static function replace_content_shortcodes( $content, $entry, $shortcodes ) {
785
        return FrmFieldsHelper::replace_content_shortcodes( $content, $entry, $shortcodes );
786
    }
787
788
    public static function process_bulk_form_actions( $errors ) {
789
        if ( ! $_REQUEST ) {
0 ignored issues
show
introduced by
Detected access of super global var $_REQUEST, probably need manual inspection.
Loading history...
790
            return $errors;
791
        }
792
793
		$bulkaction = FrmAppHelper::get_param( 'action', '', 'get', 'sanitize_text_field' );
794
        if ( $bulkaction == -1 ) {
795
			$bulkaction = FrmAppHelper::get_param( 'action2', '', 'get', 'sanitize_title' );
796
        }
797
798
        if ( ! empty( $bulkaction ) && strpos( $bulkaction, 'bulk_' ) === 0 ) {
799
            FrmAppHelper::remove_get_action();
800
801
            $bulkaction = str_replace( 'bulk_', '', $bulkaction );
802
        }
803
804
        $ids = FrmAppHelper::get_param( 'item-action', '' );
805
        if ( empty( $ids ) ) {
806
            $errors[] = __( 'No forms were specified', 'formidable' );
807
            return $errors;
808
        }
809
810
        $permission_error = FrmAppHelper::permission_nonce_error( '', '_wpnonce', 'bulk-toplevel_page_formidable' );
811
        if ( $permission_error !== false ) {
812
            $errors[] = $permission_error;
813
            return $errors;
814
        }
815
816
        if ( ! is_array( $ids ) ) {
817
            $ids = explode( ',', $ids );
818
        }
819
820
        switch ( $bulkaction ) {
821
            case 'delete':
822
                $message = self::bulk_destroy( $ids );
823
            break;
824
            case 'trash':
825
                $message = self::bulk_trash( $ids );
826
            break;
827
            case 'untrash':
828
                $message = self::bulk_untrash( $ids );
829
            break;
830
            case 'create_template':
831
                $message = self::bulk_create_template( $ids );
832
            break;
833
        }
834
835
        if ( isset( $message ) && ! empty( $message ) ) {
836
			echo '<div id="message" class="updated frm_msg_padding">' . FrmAppHelper::kses( $message, array( 'a' ) ) . '</div>';
0 ignored issues
show
introduced by
Expected a sanitizing function (see Codex for 'Data Validation'), but instead saw 'FrmAppHelper'
Loading history...
837
        }
838
839
        return $errors;
840
    }
841
842
    public static function add_default_templates( $path, $default = true, $template = true ) {
843
        _deprecated_function( __FUNCTION__, '1.07.05', 'FrmXMLController::add_default_templates()' );
844
845
        $path = untrailingslashit(trim($path));
846
		$templates = glob( $path . '/*.php' );
847
848
		for ( $i = count( $templates ) - 1; $i >= 0; $i-- ) {
849
			$filename = str_replace( '.php', '', str_replace( $path . '/', '', $templates[ $i ] ) );
850
			$template_query = array( 'form_key' => $filename );
851
            if ( $template ) {
852
                $template_query['is_template'] = 1;
853
            }
854
            if ( $default ) {
855
                $template_query['default_template'] = 1;
856
            }
857
			$form = FrmForm::getAll( $template_query, '', 1 );
858
859
            $values = FrmFormsHelper::setup_new_vars();
860
            $values['form_key'] = $filename;
861
            $values['is_template'] = $template;
862
            $values['status'] = 'published';
863
            if ( $default ) {
864
                $values['default_template'] = 1;
865
            }
866
867
            include( $templates[ $i ] );
868
869
            //get updated form
870
            if ( isset($form) && ! empty($form) ) {
871
                $old_id = $form->id;
872
                $form = FrmForm::getOne($form->id);
873
            } else {
874
                $old_id = false;
875
				$form = FrmForm::getAll( $template_query, '', 1 );
876
            }
877
878
            if ( $form ) {
879
				do_action( 'frm_after_duplicate_form', $form->id, (array) $form, array( 'old_id' => $old_id ) );
880
            }
881
        }
882
    }
883
884
    public static function route() {
885
        $action = isset($_REQUEST['frm_action']) ? 'frm_action' : 'action';
0 ignored issues
show
introduced by
Detected access of super global var $_REQUEST, probably need manual inspection.
Loading history...
886
        $vars = array();
887
		if ( isset( $_POST['frm_compact_fields'] ) ) {
0 ignored issues
show
introduced by
Detected access of super global var $_POST, probably need manual inspection.
Loading history...
888
			FrmAppHelper::permission_check( 'frm_edit_forms' );
889
890
            $json_vars = htmlspecialchars_decode(nl2br(stripslashes(str_replace('&quot;', '\\\"', $_POST['frm_compact_fields'] ))));
0 ignored issues
show
introduced by
Detected access of super global var $_POST, probably need manual inspection.
Loading history...
introduced by
Detected usage of a non-sanitized input variable: $_POST
Loading history...
891
            $json_vars = json_decode($json_vars, true);
892
            if ( empty($json_vars) ) {
893
                // json decoding failed so we should return an error message
894
				$action = FrmAppHelper::get_param( $action, '', 'get', 'sanitize_title' );
895
                if ( 'edit' == $action ) {
896
                    $action = 'update';
897
                }
898
899
                add_filter('frm_validate_form', 'FrmFormsController::json_error');
900
            } else {
901
                $vars = FrmAppHelper::json_to_array($json_vars);
902
                $action = $vars[ $action ];
903
				unset( $_REQUEST['frm_compact_fields'], $_POST['frm_compact_fields'] );
0 ignored issues
show
introduced by
Detected access of super global var $_REQUEST, probably need manual inspection.
Loading history...
introduced by
Detected access of super global var $_POST, probably need manual inspection.
Loading history...
904
				$_REQUEST = array_merge( $_REQUEST, $vars );
0 ignored issues
show
introduced by
Detected access of super global var $_REQUEST, probably need manual inspection.
Loading history...
905
				$_POST = array_merge( $_POST, $_REQUEST );
0 ignored issues
show
introduced by
Detected access of super global var $_POST, probably need manual inspection.
Loading history...
introduced by
Detected access of super global var $_REQUEST, probably need manual inspection.
Loading history...
906
            }
907
        } else {
908
			$action = FrmAppHelper::get_param( $action, '', 'get', 'sanitize_title' );
909
    		if ( isset( $_REQUEST['delete_all'] ) ) {
910
                // override the action for this page
911
    			$action = 'delete_all';
912
            }
913
        }
914
915
		add_action( 'frm_load_form_hooks', 'FrmHooksController::trigger_load_form_hooks' );
916
        FrmAppHelper::trigger_hook_load( 'form' );
917
918
        switch ( $action ) {
919
            case 'new':
920
                return self::new_form($vars);
921
            case 'create':
922
            case 'edit':
923
            case 'update':
924
            case 'duplicate':
925
            case 'trash':
926
            case 'untrash':
927
            case 'destroy':
928
            case 'delete_all':
929
            case 'settings':
930
            case 'update_settings':
931
				return self::$action( $vars );
932
            default:
933
				do_action( 'frm_form_action_' . $action );
934
				if ( apply_filters( 'frm_form_stop_action_' . $action, false ) ) {
935
                    return;
936
                }
937
938
				$action = FrmAppHelper::get_param( 'action', '', 'get', 'sanitize_text_field' );
939
                if ( $action == -1 ) {
940
					$action = FrmAppHelper::get_param( 'action2', '', 'get', 'sanitize_title' );
941
                }
942
943
                if ( strpos($action, 'bulk_') === 0 ) {
944
                    FrmAppHelper::remove_get_action();
945
                    return self::list_form();
946
                }
947
948
                return self::display_forms_list();
949
        }
950
    }
951
952
    public static function json_error( $errors ) {
953
        $errors['json'] = __( 'Abnormal HTML characters prevented your form from saving correctly', 'formidable' );
954
        return $errors;
955
    }
956
957
958
    /* FRONT-END FORMS */
959
    public static function admin_bar_css() {
960
		if ( is_admin() || ! current_user_can( 'frm_edit_forms' ) ) {
961
            return;
962
        }
963
964
		add_action( 'wp_before_admin_bar_render', 'FrmFormsController::admin_bar_configure' );
965
		FrmAppHelper::load_font_style();
966
	}
967
968
	public static function admin_bar_configure() {
969
        global $frm_vars;
970
        if ( empty($frm_vars['forms_loaded']) ) {
971
            return;
972
        }
973
974
        $actions = array();
975
        foreach ( $frm_vars['forms_loaded'] as $form ) {
976
            if ( is_object($form) ) {
977
                $actions[ $form->id ] = $form->name;
978
            }
979
            unset($form);
980
        }
981
982
        if ( empty($actions) ) {
983
            return;
984
        }
985
986
        asort($actions);
987
988
        global $wp_admin_bar;
989
990
        if ( count($actions) == 1 ) {
991
            $wp_admin_bar->add_menu( array(
992
                'title' => 'Edit Form',
993
				'href'  => admin_url( 'admin.php?page=formidable&frm_action=edit&id=' . current( array_keys( $actions ) ) ),
994
                'id'    => 'frm-forms',
995
            ) );
996
        } else {
997
            $wp_admin_bar->add_menu( array(
998
        		'id'    => 'frm-forms',
999
        		'title' => '<span class="ab-icon"></span><span class="ab-label">' . __( 'Edit Forms', 'formidable' ) . '</span>',
1000
				'href'  => admin_url( 'admin.php?page=formidable&frm_action=edit&id=' . current( array_keys( $actions ) ) ),
0 ignored issues
show
introduced by
Expected a sanitizing function (see Codex for 'Data Validation'), but instead saw 'current'
Loading history...
1001
        		'meta'  => array(
1002
					'title' => __( 'Edit Forms', 'formidable' ),
1003
        		),
1004
        	) );
1005
1006
        	foreach ( $actions as $form_id => $name ) {
1007
1008
        		$wp_admin_bar->add_menu( array(
1009
        			'parent'    => 'frm-forms',
1010
					'id'        => 'edit_form_' . $form_id,
1011
        			'title'     => empty($name) ? __( '(no title)') : $name,
1012
					'href'      => admin_url( 'admin.php?page=formidable&frm_action=edit&id=' . $form_id ),
0 ignored issues
show
introduced by
Expected next thing to be a escaping function, not '$form_id'
Loading history...
1013
        		) );
1014
        	}
1015
        }
1016
    }
1017
1018
    //formidable shortcode
1019
	public static function get_form_shortcode( $atts ) {
1020
        global $frm_vars;
1021
        if ( isset($frm_vars['skip_shortcode']) && $frm_vars['skip_shortcode'] ) {
1022
            $sc = '[formidable';
1023
			if ( ! empty( $atts ) ) {
1024
				foreach ( $atts as $k => $v ) {
1025
					$sc .= ' ' . $k . '="' . esc_attr( $v ) . '"';
1026
				}
1027
			}
1028
			return $sc . ']';
1029
        }
1030
1031
        $shortcode_atts = shortcode_atts( array(
1032
            'id' => '', 'key' => '', 'title' => false, 'description' => false,
1033
            'readonly' => false, 'entry_id' => false, 'fields' => array(),
1034
            'exclude_fields' => array(), 'minimize' => false,
1035
        ), $atts);
1036
        do_action('formidable_shortcode_atts', $shortcode_atts, $atts);
1037
1038
        return self::show_form(
1039
            $shortcode_atts['id'], $shortcode_atts['key'], $shortcode_atts['title'],
1040
            $shortcode_atts['description'], $atts
1041
        );
1042
    }
1043
1044
    public static function show_form( $id = '', $key = '', $title = false, $description = false, $atts = array() ) {
1045
        if ( empty( $id ) ) {
1046
            $id = $key;
1047
        }
1048
1049
        $form = self::maybe_get_form_to_show( $id );
1050
        if ( ! $form ) {
1051
            return __( 'Please select a valid form', 'formidable' );
1052
        }
1053
1054
		add_action( 'frm_load_form_hooks', 'FrmHooksController::trigger_load_form_hooks' );
1055
        FrmAppHelper::trigger_hook_load( 'form', $form );
1056
1057
        $form = apply_filters( 'frm_pre_display_form', $form );
1058
1059
        $frm_settings = FrmAppHelper::get_settings();
1060
1061
		if ( self::is_viewable_draft_form( $form ) ) {
1062
			// don't show a draft form on a page
1063
			$form = __( 'Please select a valid form', 'formidable' );
1064
		} else if ( self::user_should_login( $form ) ) {
1065
			$form = do_shortcode( $frm_settings->login_msg );
1066
		} else if ( self::user_has_permission_to_view( $form ) ) {
1067
			$form = do_shortcode( $frm_settings->login_msg );
1068
		} else {
1069
			$form = self::get_form( $form, $title, $description, $atts );
1070
1071
			/**
1072
			 * Use this shortcode to check for external shortcodes that may span
1073
			 * across multiple fields in the customizable HTML
1074
			 * @since 2.0.8
1075
			 */
1076
			$form = apply_filters( 'frm_filter_final_form', $form );
1077
		}
1078
1079
		return $form;
1080
    }
1081
1082
	private static function maybe_get_form_to_show( $id ) {
1083
		$form = false;
1084
1085
		if ( ! empty( $id ) ) { // no form id or key set
1086
			$form = FrmForm::getOne( $id );
1087
			if ( ! $form || $form->parent_form_id || $form->status == 'trash' ) {
1088
				$form = false;
1089
			}
1090
		}
1091
1092
		return $form;
1093
	}
1094
1095
	private static function is_viewable_draft_form( $form ) {
1096
		global $post;
1097
		$frm_settings = FrmAppHelper::get_settings();
1098
		return $form->status == 'draft' && current_user_can( 'frm_edit_forms' ) && ( ! $post || $post->ID != $frm_settings->preview_page_id ) && ! FrmAppHelper::is_preview_page();
1099
	}
1100
1101
	private static function user_should_login( $form ) {
1102
		return $form->logged_in && ! is_user_logged_in();
1103
	}
1104
1105
	private static function user_has_permission_to_view( $form ) {
1106
		return $form->logged_in && get_current_user_id() && isset( $form->options['logged_in_role'] ) && $form->options['logged_in_role'] != '' && ! FrmAppHelper::user_has_permission( $form->options['logged_in_role'] );
1107
	}
1108
1109
    public static function get_form( $form, $title, $description, $atts = array() ) {
1110
        ob_start();
1111
1112
        self::get_form_contents( $form, $title, $description, $atts );
1113
		self::enqueue_scripts( FrmForm::get_params( $form ) );
1114
1115
        $contents = ob_get_contents();
1116
        ob_end_clean();
1117
1118
		self::maybe_minimize_form( $atts, $contents );
1119
1120
        return $contents;
1121
    }
1122
1123
	public static function enqueue_scripts( $params ) {
1124
		do_action( 'frm_enqueue_form_scripts', $params );
1125
	}
1126
1127
	public static function get_form_contents( $form, $title, $description, $atts ) {
1128
        global $frm_vars;
1129
1130
        $frm_settings = FrmAppHelper::get_settings();
1131
1132
        $submit = isset($form->options['submit_value']) ? $form->options['submit_value'] : $frm_settings->submit_value;
1133
1134
        $user_ID = get_current_user_id();
1135
		$params = FrmForm::get_params( $form );
1136
        $message = $errors = '';
1137
1138
        if ( $params['posted_form_id'] == $form->id && $_POST ) {
1139
            $errors = isset( $frm_vars['created_entries'][ $form->id ] ) ? $frm_vars['created_entries'][ $form->id ]['errors'] : array();
1140
        }
1141
1142
		$include_form_tag = apply_filters( 'frm_include_form_tag', true, $form );
1143
        $fields = FrmFieldsHelper::get_form_fields( $form->id, ( isset( $errors ) && ! empty( $errors ) ) );
1144
1145
        if ( $params['action'] != 'create' || $params['posted_form_id'] != $form->id || ! $_POST ) {
0 ignored issues
show
introduced by
Detected access of super global var $_POST, probably need manual inspection.
Loading history...
1146
            do_action('frm_display_form_action', $params, $fields, $form, $title, $description);
1147
            if ( apply_filters('frm_continue_to_new', true, $form->id, $params['action']) ) {
1148
                $values = FrmEntriesHelper::setup_new_vars($fields, $form);
1149
				include( FrmAppHelper::plugin_path() . '/classes/views/frm-entries/new.php' );
1150
            }
1151
            return;
1152
        }
1153
1154
        if ( ! empty($errors) ) {
1155
            $values = $fields ? FrmEntriesHelper::setup_new_vars($fields, $form) : array();
1156
			include( FrmAppHelper::plugin_path() . '/classes/views/frm-entries/new.php' );
1157
            return;
1158
        }
1159
1160
        do_action('frm_validate_form_creation', $params, $fields, $form, $title, $description);
1161
        if ( ! apply_filters('frm_continue_to_create', true, $form->id) ) {
1162
            return;
1163
        }
1164
1165
        $values = FrmEntriesHelper::setup_new_vars($fields, $form, true);
1166
        $created = self::just_created_entry( $form->id );
1167
        $conf_method = apply_filters('frm_success_filter', 'message', $form, 'create');
1168
1169
        if ( $created && is_numeric($created) && $conf_method != 'message' ) {
1170
            do_action('frm_success_action', $conf_method, $form, $form->options, $created);
1171
			do_action( 'frm_after_entry_processed', array( 'entry_id' => $created, 'form' => $form ) );
1172
            return;
1173
        }
1174
1175
        if ( $created && is_numeric($created) ) {
1176
            $message = isset($form->options['success_msg']) ? $form->options['success_msg'] : $frm_settings->success_msg;
1177
            $class = 'frm_message';
1178
        } else {
1179
            $message = $frm_settings->failed_msg;
1180
            $class = FrmFormsHelper::form_error_class();
1181
        }
1182
1183
		$message = FrmFormsHelper::get_success_message( array(
1184
			'message' => $message, 'form' => $form,
1185
			'entry_id' => $created, 'class' => $class,
1186
		) );
1187
        $message = apply_filters('frm_main_feedback', $message, $form, $created);
1188
1189
        if ( ! isset($form->options['show_form']) || $form->options['show_form'] ) {
1190
			require( FrmAppHelper::plugin_path() . '/classes/views/frm-entries/new.php' );
1191
        } else {
1192
            global $frm_vars;
1193
			self::maybe_load_css( $form, $values['custom_style'], $frm_vars['load_css'] );
1194
1195
			$include_extra_container = 'frm_forms' . FrmFormsHelper::get_form_style_class( $values );
1196
			include( FrmAppHelper::plugin_path() . '/classes/views/frm-entries/errors.php' );
1197
        }
1198
1199
		do_action( 'frm_after_entry_processed', array( 'entry_id' => $created, 'form' => $form ) );
1200
    }
1201
1202
	/**
1203
	 * @since 2.2.7
1204
	 */
1205
	public static function just_created_entry( $form_id ) {
1206
		global $frm_vars;
1207
		return ( isset( $frm_vars['created_entries'] ) && isset( $frm_vars['created_entries'][ $form_id ] ) && isset( $frm_vars['created_entries'][ $form_id ]['entry_id'] ) ) ? $frm_vars['created_entries'][ $form_id ]['entry_id'] : 0;
1208
	}
1209
1210
	public static function front_head() {
1211
		$version = FrmAppHelper::plugin_version();
1212
		$suffix = defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ? '' : '.min';
1213
		wp_register_script( 'formidable', FrmAppHelper::plugin_url() . "/js/formidable{$suffix}.js", array( 'jquery' ), $version, true );
1214
		wp_register_script( 'jquery-placeholder', FrmAppHelper::plugin_url() . '/js/jquery/jquery.placeholder.js', array( 'jquery' ), '2.0.7', true );
1215
		add_filter( 'script_loader_tag', 'FrmFormsController::defer_script_loading', 10, 2 );
1216
1217
		if ( FrmAppHelper::is_admin() ) {
1218
			// don't load this in back-end
1219
			return;
1220
		}
1221
1222
		FrmAppHelper::localize_script( 'front' );
1223
		FrmStylesController::enqueue_css( 'register' );
1224
	}
1225
1226
	public static function maybe_load_css( $form, $this_load, $global_load ) {
1227
		$load_css = FrmForm::is_form_loaded( $form, $this_load, $global_load );
1228
1229
		if ( $load_css ) {
1230
			global $frm_vars;
1231
			self::footer_js( 'header' );
1232
			$frm_vars['css_loaded'] = true;
1233
		}
1234
	}
1235
1236
	public static function defer_script_loading( $tag, $handle ) {
1237
	    if ( 'recaptcha-api' == $handle && ! strpos( $tag, 'defer' ) ) {
1238
	        $tag = str_replace( ' src', ' defer="defer" async="async" src', $tag );
1239
		}
1240
	    return $tag;
1241
	}
1242
1243
	public static function footer_js( $location = 'footer' ) {
1244
		global $frm_vars;
1245
1246
		FrmStylesController::enqueue_css();
1247
1248
		if ( ! FrmAppHelper::is_admin() && $location != 'header' && ! empty( $frm_vars['forms_loaded'] ) ) {
1249
			//load formidable js
1250
			wp_enqueue_script( 'formidable' );
1251
		}
1252
	}
1253
1254
	/**
1255
	 * @since 2.0.8
1256
	 */
1257
	private static function maybe_minimize_form( $atts, &$content ) {
1258
		// check if minimizing is turned on
1259
		if ( self::is_minification_on( $atts ) ) {
1260
			$content = str_replace( array( "\r\n", "\r", "\n", "\t", '    ' ), '', $content );
1261
		}
1262
	}
1263
1264
	/**
1265
	 * @since 2.0.8
1266
	 * @return boolean
1267
	 */
1268
	private static function is_minification_on( $atts ) {
1269
		return isset( $atts['minimize'] ) && ! empty( $atts['minimize'] );
1270
	}
1271
}
1272