Completed
Push — master ( bd734d...a354bc )
by Jamie
02:51
created

FrmFormsController::create_default_email_action()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 5
nc 2
nop 1
dl 0
loc 8
rs 9.4285
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
			self::create($values);
60
			return;
61
		} else if ( $action == 'new' ) {
62
			$frm_field_selection = FrmField::field_selection();
63
            $values = FrmFormsHelper::setup_new_vars($values);
64
            $id = FrmForm::create( $values );
65
            $form = FrmForm::getOne($id);
66
67
			self::create_default_email_action( $form );
68
69
			$all_templates = FrmForm::getAll( array( 'is_template' => 1 ), 'name' );
70
71
            $values['id'] = $id;
72
			require( FrmAppHelper::plugin_path() . '/classes/views/frm-forms/new.php' );
73
        }
74
    }
75
76
	/**
77
	 * Create the default email action
78
	 *
79
	 * @since 2.02.11
80
	 *
81
	 * @param object $form
82
	 */
83
    private static function create_default_email_action( $form ) {
84
    	$create_email = apply_filters( 'frm_create_default_email_action', true, $form );
85
86
	    if ( $create_email ) {
87
		    $action_control = FrmFormActionsController::get_form_actions( 'email' );
88
		    $action_control->create( $form->id );
89
	    }
90
    }
91
92
	public static function create( $values = array() ) {
93
        FrmAppHelper::permission_check('frm_edit_forms');
94
95
        global $frm_vars;
96
        if ( empty( $values ) ) {
97
            $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...
98
        }
99
100
        //Set radio button and checkbox meta equal to "other" value
101
        if ( FrmAppHelper::pro_is_installed() ) {
102
            $values = FrmProEntry::mod_other_vals( $values, 'back' );
103
        }
104
105
		$id = isset($values['id']) ? absint( $values['id'] ) : FrmAppHelper::get_param( 'id', '', 'get', 'absint' );
106
107
        if ( ! current_user_can( 'frm_edit_forms' ) || ( $_POST && ( ! isset( $values['frm_save_form'] ) || ! wp_verify_nonce( $values['frm_save_form'], 'frm_save_form_nonce' ) ) ) ) {
108
            $frm_settings = FrmAppHelper::get_settings();
109
            $errors = array( 'form' => $frm_settings->admin_permission );
110
        } else {
111
            $errors = FrmForm::validate($values);
112
        }
113
114
        if ( count($errors) > 0 ) {
115
            $hide_preview = true;
116
			$frm_field_selection = FrmField::field_selection();
117
            $form = FrmForm::getOne( $id );
118
            $fields = FrmField::get_all_for_form($id);
119
120
            $values = FrmAppHelper::setup_edit_vars($form, 'forms', $fields, true);
121
			$all_templates = FrmForm::getAll( array( 'is_template' => 1 ), 'name' );
122
123
			require( FrmAppHelper::plugin_path() . '/classes/views/frm-forms/new.php' );
124
        } else {
125
            FrmForm::update( $id, $values, true );
126
			$url = admin_url( 'admin.php?page=formidable&frm_action=settings&id=' . $id );
127
			die( FrmAppHelper::js_redirect( $url ) );
128
        }
129
    }
130
131
    public static function edit( $values = false ) {
132
        FrmAppHelper::permission_check('frm_edit_forms');
133
134
		$id = isset( $values['id'] ) ? absint( $values['id'] ) : FrmAppHelper::get_param( 'id', '', 'get', 'absint' );
135
        return self::get_edit_vars($id);
136
    }
137
138
    public static function settings( $id = false, $message = '' ) {
139
        FrmAppHelper::permission_check('frm_edit_forms');
140
141
        if ( ! $id || ! is_numeric($id) ) {
142
			$id = FrmAppHelper::get_param( 'id', '', 'get', 'absint' );
143
        }
144
		return self::get_settings_vars( $id, array(), $message );
145
    }
146
147
    public static function update_settings() {
148
        FrmAppHelper::permission_check('frm_edit_forms');
149
150
		$id = FrmAppHelper::get_param( 'id', '', 'get', 'absint' );
151
152
        $errors = FrmForm::validate($_POST);
0 ignored issues
show
introduced by
Detected access of super global var $_POST, probably need manual inspection.
Loading history...
153
        if ( count($errors) > 0 ) {
154
            return self::get_settings_vars($id, $errors);
155
        }
156
157
        do_action('frm_before_update_form_settings', $id);
158
159
		FrmForm::update( $id, $_POST );
0 ignored issues
show
introduced by
Detected access of super global var $_POST, probably need manual inspection.
Loading history...
160
161
        $message = __( 'Settings Successfully Updated', 'formidable' );
162
		return self::get_settings_vars( $id, array(), $message );
163
    }
164
165
	public static function edit_key() {
166
		$values = self::edit_in_place_value( 'form_key' );
167
		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...
168
		wp_die();
169
	}
170
171
	public static function edit_description() {
172
		$values = self::edit_in_place_value( 'description' );
173
		echo wp_kses_post( FrmAppHelper::use_wpautop( stripslashes( $values['description'] ) ) );
174
		wp_die();
175
	}
176
177
	private static function edit_in_place_value( $field ) {
178
		check_ajax_referer( 'frm_ajax', 'nonce' );
179
		FrmAppHelper::permission_check('frm_edit_forms', 'hide');
180
181
		$form_id = FrmAppHelper::get_post_param( 'form_id', '', 'absint' );
182
		$value = FrmAppHelper::get_post_param( 'update_value', '', 'wp_filter_post_kses' );
183
184
		$values = array( $field => trim( $value ) );
185
		FrmForm::update( $form_id, $values );
186
		$values['form_id'] = $form_id;
187
188
		return $values;
189
	}
190
191
	public static function update( $values = array() ) {
192
		if ( empty( $values ) ) {
193
            $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...
194
        }
195
196
        //Set radio button and checkbox meta equal to "other" value
197
        if ( FrmAppHelper::pro_is_installed() ) {
198
            $values = FrmProEntry::mod_other_vals( $values, 'back' );
199
        }
200
201
        $errors = FrmForm::validate( $values );
202
        $permission_error = FrmAppHelper::permission_nonce_error( 'frm_edit_forms', 'frm_save_form', 'frm_save_form_nonce' );
203
        if ( $permission_error !== false ) {
204
            $errors['form'] = $permission_error;
205
        }
206
207
		$id = isset( $values['id'] ) ? absint( $values['id'] ) : FrmAppHelper::get_param( 'id', '', 'get', 'absint' );
208
209
		if ( count( $errors ) > 0 ) {
210
            return self::get_edit_vars( $id, $errors );
211
		} else {
212
            FrmForm::update( $id, $values );
213
            $message = __( 'Form was Successfully Updated', 'formidable' );
214
            if ( defined( 'DOING_AJAX' ) ) {
215
				wp_die( $message );
216
            }
217
			return self::get_edit_vars( $id, array(), $message );
218
        }
219
    }
220
221
    public static function bulk_create_template( $ids ) {
222
        FrmAppHelper::permission_check( 'frm_edit_forms' );
223
224
        foreach ( $ids as $id ) {
225
            FrmForm::duplicate( $id, true, true );
226
        }
227
228
        return __( 'Form template was Successfully Created', 'formidable' );
229
    }
230
231
	/**
232
	 * Redirect to the url for creating from a template
233
	 * Also delete the current form
234
	 * @since 2.0
235
	 */
236
	public static function _create_from_template() {
237
		FrmAppHelper::permission_check('frm_edit_forms');
238
		check_ajax_referer( 'frm_ajax', 'nonce' );
239
240
		$current_form = FrmAppHelper::get_param( 'this_form', '', 'get', 'absint' );
241
		$template_id = FrmAppHelper::get_param( 'id', '', 'get', 'absint' );
242
243
		if ( $current_form ) {
244
			FrmForm::destroy( $current_form );
245
		}
246
247
		echo esc_url_raw( admin_url( 'admin.php?page=formidable&action=duplicate&id=' . $template_id ) );
248
		wp_die();
249
	}
250
251
    public static function duplicate() {
252
        FrmAppHelper::permission_check('frm_edit_forms');
253
254
		$params = FrmForm::list_page_params();
255
        $form = FrmForm::duplicate( $params['id'], $params['template'], true );
256
        $message = ($params['template']) ? __( 'Form template was Successfully Created', 'formidable' ) : __( 'Form was Successfully Copied', 'formidable' );
257
        if ( $form ) {
258
			return self::get_edit_vars( $form, array(), $message, true );
259
        } else {
260
            return self::display_forms_list($params, __( 'There was a problem creating the new template.', 'formidable' ));
261
        }
262
    }
263
264
    public static function page_preview() {
265
		$params = FrmForm::list_page_params();
266
        if ( ! $params['form'] ) {
267
            return;
268
        }
269
270
        $form = FrmForm::getOne( $params['form'] );
271
        if ( ! $form ) {
272
            return;
273
        }
274
        return self::show_form( $form->id, '', true, true );
275
    }
276
277
    public static function preview() {
278
        do_action( 'frm_wp' );
279
280
        global $frm_vars;
281
        $frm_vars['preview'] = true;
282
283
        if ( ! defined( 'ABSPATH' ) && ! defined( 'XMLRPC_REQUEST' ) ) {
284
            global $wp;
285
            $root = dirname( dirname( dirname( dirname( __FILE__ ) ) ) );
286
			include_once( $root . '/wp-config.php' );
287
            $wp->init();
288
            $wp->register_globals();
289
        }
290
291
		self::register_pro_scripts();
292
293
		header( 'Content-Type: text/html; charset=' . get_option( 'blog_charset' ) );
294
295
		$key = FrmAppHelper::simple_get( 'form', 'sanitize_title' );
296
		if ( $key == '' ) {
297
			$key = FrmAppHelper::get_post_param( 'form', '', 'sanitize_title' );
298
		}
299
300
		$form = FrmForm::getAll( array( 'form_key' => $key ), '', 1 );
301
		if ( empty( $form ) ) {
302
			$form = FrmForm::getAll( array(), '', 1 );
303
        }
304
305
		require( FrmAppHelper::plugin_path() . '/classes/views/frm-entries/direct.php' );
306
        wp_die();
307
    }
308
309
	public static function register_pro_scripts() {
310
		if ( FrmAppHelper::pro_is_installed() ) {
311
			wp_register_script( 'jquery-frm-rating', FrmAppHelper::plugin_url() . '/pro/js/jquery.rating.min.js', array( 'jquery' ), '4.11', true );
312
			wp_register_script( 'jquery-maskedinput', FrmAppHelper::plugin_url() . '/pro/js/jquery.maskedinput.min.js', array( 'jquery' ), '1.4', true );
313
			wp_register_script( 'jquery-chosen', FrmAppHelper::plugin_url() . '/pro/js/chosen.jquery.min.js', array( 'jquery' ), '1.5.1', true );
314
			wp_register_script( 'dropzone', FrmAppHelper::plugin_url() . '/pro/js/dropzone.js', array( 'jquery' ), '4.3.0', true );
315
		}
316
	}
317
318
    public static function untrash() {
319
		self::change_form_status( 'untrash' );
320
    }
321
322
	public static function bulk_untrash( $ids ) {
323
        FrmAppHelper::permission_check('frm_edit_forms');
324
325
        $count = FrmForm::set_status( $ids, 'published' );
326
327
        $message = sprintf(_n( '%1$s form restored from the Trash.', '%1$s forms restored from the Trash.', $count, 'formidable' ), 1 );
328
        return $message;
329
    }
330
331
    public static function trash() {
332
		self::change_form_status( 'trash' );
333
    }
334
335
	/**
336
	 * @param string $status
337
	 *
338
	 * @return int The number of forms changed
339
	 */
340
	public static function change_form_status( $status ) {
341
		$available_status = array(
342
			'untrash' => array( 'permission' => 'frm_edit_forms', 'new_status' => 'published' ),
343
			'trash'   => array( 'permission' => 'frm_delete_forms', 'new_status' => 'trash' ),
344
		);
345
346
		if ( ! isset( $available_status[ $status ] ) ) {
347
			return;
348
		}
349
350
		FrmAppHelper::permission_check( $available_status[ $status ]['permission'] );
351
352
		$params = FrmForm::list_page_params();
353
354
		//check nonce url
355
		check_admin_referer( $status . '_form_' . $params['id'] );
356
357
		$count = 0;
358
		if ( FrmForm::set_status( $params['id'], $available_status[ $status ]['new_status'] ) ) {
359
			$count++;
360
		}
361
362
		$available_status['untrash']['message'] = sprintf(_n( '%1$s form restored from the Trash.', '%1$s forms restored from the Trash.', $count, 'formidable' ), $count );
363
		$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...
364
365
		$message = $available_status[ $status ]['message'];
366
367
		self::display_forms_list( $params, $message );
368
	}
369
370
	public static function bulk_trash( $ids ) {
371
        FrmAppHelper::permission_check('frm_delete_forms');
372
373
        $count = 0;
374
        foreach ( $ids as $id ) {
375
            if ( FrmForm::trash( $id ) ) {
376
                $count++;
377
            }
378
        }
379
380
        $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...
381
		$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>' );
382
383
        return $message;
384
    }
385
386
    public static function destroy() {
387
        FrmAppHelper::permission_check('frm_delete_forms');
388
389
		$params = FrmForm::list_page_params();
390
391
        //check nonce url
392
        check_admin_referer('destroy_form_' . $params['id']);
393
394
        $count = 0;
395
        if ( FrmForm::destroy( $params['id'] ) ) {
396
            $count++;
397
        }
398
399
        $message = sprintf(_n( '%1$s form permanently deleted.', '%1$s forms permanently deleted.', $count, 'formidable' ), $count);
400
401
		self::display_forms_list( $params, $message );
402
    }
403
404
	public static function bulk_destroy( $ids ) {
405
        FrmAppHelper::permission_check('frm_delete_forms');
406
407
        $count = 0;
408
        foreach ( $ids as $id ) {
409
            $d = FrmForm::destroy( $id );
410
            if ( $d ) {
411
                $count++;
412
            }
413
        }
414
415
        $message = sprintf(_n( '%1$s form permanently deleted.', '%1$s forms permanently deleted.', $count, 'formidable' ), $count);
416
417
        return $message;
418
    }
419
420
    private static function delete_all() {
421
        //check nonce url
422
        $permission_error = FrmAppHelper::permission_nonce_error('frm_delete_forms', '_wpnonce', 'bulk-toplevel_page_formidable');
423
        if ( $permission_error !== false ) {
424
			self::display_forms_list( array(), '', array( $permission_error ) );
425
            return;
426
        }
427
428
		$count = FrmForm::scheduled_delete( time() );
429
        $message = sprintf(_n( '%1$s form permanently deleted.', '%1$s forms permanently deleted.', $count, 'formidable' ), $count);
430
431
		self::display_forms_list( array(), $message );
432
    }
433
434
	public static function scheduled_delete( $delete_timestamp = '' ) {
435
		_deprecated_function( __FUNCTION__, '2.0.9', 'FrmForm::scheduled_delete' );
436
		return FrmForm::scheduled_delete( $delete_timestamp );
437
	}
438
439
	/**
440
	* Inserts Formidable button
441
	* Hook exists since 2.5.0
442
	*
443
	* @since 2.0.15
444
	*/
445
	public static function insert_form_button() {
446
		if ( current_user_can('frm_view_forms') ) {
447
			$menu_name = FrmAppHelper::get_menu_name();
448
			$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' ) . '">
449
				<span class="frm-buttons-icon wp-media-buttons-icon"></span> ' .
450
				$menu_name . '</a>';
451
			echo wp_kses_post( $content );
452
		}
453
	}
454
455
    public static function insert_form_popup() {
456
		$page = basename( FrmAppHelper::get_server_value( 'PHP_SELF' ) );
457
		if ( ! in_array( $page, array( 'post.php', 'page.php', 'page-new.php', 'post-new.php' ) ) ) {
458
            return;
459
        }
460
461
        FrmAppHelper::load_admin_wide_js();
462
463
        $shortcodes = array(
464
			'formidable' => array( 'name' => __( 'Form', 'formidable' ), 'label' => __( 'Insert a Form', 'formidable' ) ),
465
        );
466
467
        $shortcodes = apply_filters('frm_popup_shortcodes', $shortcodes);
468
469
		include( FrmAppHelper::plugin_path() . '/classes/views/frm-forms/insert_form_popup.php' );
470
    }
471
472
    public static function get_shortcode_opts() {
473
		FrmAppHelper::permission_check('frm_view_forms');
474
        check_ajax_referer( 'frm_ajax', 'nonce' );
475
476
		$shortcode = FrmAppHelper::get_post_param( 'shortcode', '', 'sanitize_text_field' );
477
        if ( empty($shortcode) ) {
478
            wp_die();
479
        }
480
481
		echo '<div id="sc-opts-' . esc_attr( $shortcode ) . '" class="frm_shortcode_option">';
482
		echo '<input type="radio" name="frmsc" value="' . esc_attr( $shortcode ) . '" id="sc-' . esc_attr( $shortcode ) . '" class="frm_hidden" />';
483
484
        $form_id = '';
485
        $opts = array();
486
		switch ( $shortcode ) {
487
            case 'formidable':
488
                $opts = array(
489
					'form_id'       => 'id',
490
                    //'key' => ',
491
					'title'         => array( 'val' => 1, 'label' => __( 'Display form title', 'formidable' ) ),
492
					'description'   => array( 'val' => 1, 'label' => __( 'Display form description', 'formidable' ) ),
493
					'minimize'      => array( 'val' => 1, 'label' => __( 'Minimize form HTML', 'formidable' ) ),
494
                );
495
            break;
496
        }
497
        $opts = apply_filters('frm_sc_popup_opts', $opts, $shortcode);
498
499
		if ( isset( $opts['form_id'] ) && is_string( $opts['form_id'] ) ) {
500
			// allow other shortcodes to use the required form id option
501
			$form_id = $opts['form_id'];
502
			unset( $opts['form_id'] );
503
		}
504
505
		include( FrmAppHelper::plugin_path() . '/classes/views/frm-forms/shortcode_opts.php' );
506
507
        echo '</div>';
508
509
        wp_die();
510
    }
511
512
	public static function display_forms_list( $params = array(), $message = '', $errors = array(), $deprecated_errors = array() ) {
513
        FrmAppHelper::permission_check( 'frm_view_forms' );
514
		if ( ! empty( $deprecated_errors ) ) {
515
			$errors = $deprecated_errors;
516
			_deprecated_argument( 'errors', '2.0.8' );
517
		}
518
519
        global $wpdb, $frm_vars;
520
521
		if ( empty( $params ) ) {
522
			$params = FrmForm::list_page_params();
523
        }
524
525
        $wp_list_table = new FrmFormsListHelper( compact( 'params' ) );
526
527
        $pagenum = $wp_list_table->get_pagenum();
528
529
        $wp_list_table->prepare_items();
530
531
        $total_pages = $wp_list_table->get_pagination_arg( 'total_pages' );
532
        if ( $pagenum > $total_pages && $total_pages > 0 ) {
533
			wp_redirect( esc_url_raw( add_query_arg( 'paged', $total_pages ) ) );
534
            die();
535
        }
536
537
		require( FrmAppHelper::plugin_path() . '/classes/views/frm-forms/list.php' );
538
    }
539
540
	public static function get_columns( $columns ) {
541
	    $columns['cb'] = '<input type="checkbox" />';
542
	    $columns['id'] = 'ID';
543
544
        $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...
545
546
        if ( 'template' == $type ) {
547
            $columns['name']        = __( 'Template Name', 'formidable' );
548
            $columns['type']        = __( 'Type', 'formidable' );
549
            $columns['form_key']    = __( 'Key', 'formidable' );
550
        } else {
551
            $columns['name']        = __( 'Form Title', 'formidable' );
552
            $columns['entries']     = __( 'Entries', 'formidable' );
553
            $columns['form_key']    = __( 'Key', 'formidable' );
554
            $columns['shortcode']   = __( 'Shortcodes', 'formidable' );
555
        }
556
557
        $columns['created_at'] = __( 'Date', 'formidable' );
558
559
		add_screen_option( 'per_page', array( 'label' => __( 'Forms', 'formidable' ), 'default' => 20, 'option' => 'formidable_page_formidable_per_page' ) );
560
561
        return $columns;
562
	}
563
564
	public static function get_sortable_columns() {
565
		return array(
566
			'id'            => 'id',
567
			'name'          => 'name',
568
			'description'   => 'description',
569
			'form_key'      => 'form_key',
570
			'created_at'    => 'created_at',
571
		);
572
	}
573
574
	public static function hidden_columns( $result ) {
575
        $return = false;
576
        foreach ( (array) $result as $r ) {
577
            if ( ! empty( $r ) ) {
578
                $return = true;
579
                break;
580
            }
581
        }
582
583
        if ( $return ) {
584
            return $result;
585
		}
586
587
        $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...
588
589
        $result[] = 'created_at';
590
        if ( $type == 'template' ) {
591
            $result[] = 'id';
592
            $result[] = 'form_key';
593
        }
594
595
        return $result;
596
    }
597
598
	public static function save_per_page( $save, $option, $value ) {
599
        if ( $option == 'formidable_page_formidable_per_page' ) {
600
            $save = (int) $value;
601
        }
602
        return $save;
603
    }
604
605
	private static function get_edit_vars( $id, $errors = array(), $message = '', $create_link = false ) {
606
        global $frm_vars;
607
608
        $form = FrmForm::getOne( $id );
609
        if ( ! $form ) {
610
            wp_die( __( 'You are trying to edit a form that does not exist.', 'formidable' ) );
611
        }
612
613
        if ( $form->parent_form_id ) {
614
			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>' ));
615
        }
616
617
		$frm_field_selection = FrmField::field_selection();
618
        $fields = FrmField::get_all_for_form($form->id);
619
620
        // Automatically add end section fields if they don't exist (2.0 migration)
621
        $reset_fields = false;
622
        FrmFormsHelper::auto_add_end_section_fields( $form, $fields, $reset_fields );
623
624
        if ( $reset_fields ) {
625
            $fields = FrmField::get_all_for_form( $form->id, '', 'exclude' );
626
        }
627
628
        unset($end_section_values, $last_order, $open, $reset_fields);
629
630
		$args = array( 'parent_form_id' => $form->id );
631
        $values = FrmAppHelper::setup_edit_vars( $form, 'forms', $fields, true, array(), $args );
632
633
        $edit_message = __( 'Form was Successfully Updated', 'formidable' );
634
        if ( $form->is_template && $message == $edit_message ) {
635
            $message = __( 'Template was Successfully Updated', 'formidable' );
636
        }
637
638
		$all_templates = FrmForm::getAll( array( 'is_template' => 1 ), 'name' );
639
640
        if ( $form->default_template ) {
641
            wp_die(__( 'That template cannot be edited', 'formidable' ));
642
        } else if ( defined('DOING_AJAX') ) {
643
            wp_die();
644
        } else if ( $create_link ) {
645
			require( FrmAppHelper::plugin_path() . '/classes/views/frm-forms/new.php' );
646
        } else {
647
			require( FrmAppHelper::plugin_path() . '/classes/views/frm-forms/edit.php' );
648
        }
649
    }
650
651
	public static function get_settings_vars( $id, $errors = array(), $message = '' ) {
652
		FrmAppHelper::permission_check( 'frm_edit_forms' );
653
654
        global $frm_vars;
655
656
        $form = FrmForm::getOne( $id );
657
658
        $fields = FrmField::get_all_for_form($id);
659
        $values = FrmAppHelper::setup_edit_vars($form, 'forms', $fields, true);
660
661
        if ( isset($values['default_template']) && $values['default_template'] ) {
662
            wp_die(__( 'That template cannot be edited', 'formidable' ));
663
        }
664
665
        $action_controls = FrmFormActionsController::get_form_actions();
666
667
        $sections = apply_filters('frm_add_form_settings_section', array(), $values);
668
        $pro_feature = FrmAppHelper::pro_is_installed() ? '' : ' class="pro_feature"';
669
670
        $styles = apply_filters('frm_get_style_opts', array());
671
672
		require( FrmAppHelper::plugin_path() . '/classes/views/frm-forms/settings.php' );
673
    }
674
675
    public static function mb_tags_box( $form_id, $class = '' ) {
676
        $fields = FrmField::get_all_for_form($form_id, '', 'include');
677
        $linked_forms = array();
678
        $col = 'one';
679
        $settings_tab = FrmAppHelper::is_admin_page('formidable' ) ? true : false;
680
681
		$cond_shortcodes = apply_filters( 'frm_conditional_shortcodes', array() );
682
		$adv_shortcodes = self::get_advanced_shortcodes();
683
		$user_fields = apply_filters( 'frm_user_shortcodes', array() );
684
		$entry_shortcodes = self::get_shortcode_helpers( $settings_tab );
685
686
		include( FrmAppHelper::plugin_path() . '/classes/views/shared/mb_adv_info.php' );
687
    }
688
689
	/**
690
	 * Get an array of the options to display in the advanced tab
691
	 * of the customization panel
692
	 * @since 2.0.6
693
	 */
694
	private static function get_advanced_shortcodes() {
695
		$adv_shortcodes = array(
696
			'sep=", "'       => array(
697
				'label' => __( 'Separator', 'formidable' ),
698
				'title' => __( 'Use a different separator for checkbox fields', 'formidable' ),
699
			),
700
			'format="d-m-Y"' => __( 'Date Format', 'formidable' ),
701
			'show="field_label"' => __( 'Field Label', 'formidable' ),
702
			'wpautop=0'      => array(
703
				'label' => __( 'No Auto P', 'formidable' ),
704
				'title' => __( 'Do not automatically add any paragraphs or line breaks', 'formidable' ),
705
			),
706
		);
707
		$adv_shortcodes = apply_filters( 'frm_advanced_shortcodes', $adv_shortcodes );
708
		// __( 'Leave blank instead of defaulting to User Login', 'formidable' ) : blank=1
709
710
		return $adv_shortcodes;
711
	}
712
713
	/**
714
	 * Get an array of the helper shortcodes to display in the customization panel
715
	 * @since 2.0.6
716
	 */
717
	private static function get_shortcode_helpers( $settings_tab ) {
718
		$entry_shortcodes = array(
719
			'id'        => __( 'Entry ID', 'formidable' ),
720
			'key'       => __( 'Entry Key', 'formidable' ),
721
			'post_id'   => __( 'Post ID', 'formidable' ),
722
			'ip'        => __( 'User IP', 'formidable' ),
723
			'created-at' => __( 'Entry created', 'formidable' ),
724
			'updated-at' => __( 'Entry updated', 'formidable' ),
725
			''          => '',
726
			'siteurl'   => __( 'Site URL', 'formidable' ),
727
			'sitename'  => __( 'Site Name', 'formidable' ),
728
        );
729
730
		if ( ! FrmAppHelper::pro_is_installed() ) {
731
			unset( $entry_shortcodes['post_id'] );
732
		}
733
734
		if ( $settings_tab ) {
735
			$entry_shortcodes['default-message'] = __( 'Default Msg', 'formidable' );
736
			$entry_shortcodes['default-html'] = __( 'Default HTML', 'formidable' );
737
			$entry_shortcodes['default-plain'] = __( 'Default Plain', 'formidable' );
738
		} else {
739
			$entry_shortcodes['detaillink'] = __( 'Detail Link', 'formidable' );
740
			$entry_shortcodes['editlink location="front" label="Edit" page_id=x'] = __( 'Edit Entry Link', 'formidable' );
741
			$entry_shortcodes['evenodd'] = __( 'Even/Odd', 'formidable' );
742
			$entry_shortcodes['entry_count'] = __( 'Entry Count', 'formidable' );
743
		}
744
745
		/**
746
		 * Use this hook to add or remove buttons in the helpers section
747
		 * in the customization panel
748
		 * @since 2.0.6
749
		 */
750
		$entry_shortcodes = apply_filters( 'frm_helper_shortcodes', $entry_shortcodes, $settings_tab );
751
752
		return $entry_shortcodes;
753
	}
754
755
    // Insert the form class setting into the form
756
	public static function form_classes( $form ) {
757
        if ( isset($form->options['form_class']) ) {
758
			echo esc_attr( sanitize_text_field( $form->options['form_class'] ) );
759
        }
760
    }
761
762
    public static function get_email_html() {
763
		FrmAppHelper::permission_check('frm_view_forms');
764
        check_ajax_referer( 'frm_ajax', 'nonce' );
765
		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...
766
			'form_id'       => FrmAppHelper::get_post_param( 'form_id', '', 'absint' ),
767
	        'default_email' => true,
768
			'plain_text'    => FrmAppHelper::get_post_param( 'plain_text', '', 'absint' ),
769
	    ) );
770
	    wp_die();
771
	}
772
773
    public static function filter_content( $content, $form, $entry = false ) {
774
		self::get_entry_by_param( $entry );
775
        if ( ! $entry ) {
776
            return $content;
777
        }
778
779
        if ( is_object( $form ) ) {
780
            $form = $form->id;
781
        }
782
783
        $shortcodes = FrmFieldsHelper::get_shortcodes( $content, $form );
784
        $content = apply_filters( 'frm_replace_content_shortcodes', $content, $entry, $shortcodes );
785
786
        return $content;
787
    }
788
789
	private static function get_entry_by_param( &$entry ) {
790
		if ( ! $entry || ! is_object( $entry ) ) {
791
			if ( ! $entry || ! is_numeric( $entry ) ) {
792
				$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...
793
			}
794
795
			FrmEntry::maybe_get_entry( $entry );
796
		}
797
	}
798
799
    public static function replace_content_shortcodes( $content, $entry, $shortcodes ) {
800
        return FrmFieldsHelper::replace_content_shortcodes( $content, $entry, $shortcodes );
801
    }
802
803
    public static function process_bulk_form_actions( $errors ) {
804
        if ( ! $_REQUEST ) {
0 ignored issues
show
introduced by
Detected access of super global var $_REQUEST, probably need manual inspection.
Loading history...
805
            return $errors;
806
        }
807
808
		$bulkaction = FrmAppHelper::get_param( 'action', '', 'get', 'sanitize_text_field' );
809
        if ( $bulkaction == -1 ) {
810
			$bulkaction = FrmAppHelper::get_param( 'action2', '', 'get', 'sanitize_title' );
811
        }
812
813
        if ( ! empty( $bulkaction ) && strpos( $bulkaction, 'bulk_' ) === 0 ) {
814
            FrmAppHelper::remove_get_action();
815
816
            $bulkaction = str_replace( 'bulk_', '', $bulkaction );
817
        }
818
819
        $ids = FrmAppHelper::get_param( 'item-action', '' );
820
        if ( empty( $ids ) ) {
821
            $errors[] = __( 'No forms were specified', 'formidable' );
822
            return $errors;
823
        }
824
825
        $permission_error = FrmAppHelper::permission_nonce_error( '', '_wpnonce', 'bulk-toplevel_page_formidable' );
826
        if ( $permission_error !== false ) {
827
            $errors[] = $permission_error;
828
            return $errors;
829
        }
830
831
        if ( ! is_array( $ids ) ) {
832
            $ids = explode( ',', $ids );
833
        }
834
835
        switch ( $bulkaction ) {
836
            case 'delete':
837
                $message = self::bulk_destroy( $ids );
838
            break;
839
            case 'trash':
840
                $message = self::bulk_trash( $ids );
841
            break;
842
            case 'untrash':
843
                $message = self::bulk_untrash( $ids );
844
            break;
845
            case 'create_template':
846
                $message = self::bulk_create_template( $ids );
847
            break;
848
        }
849
850
        if ( isset( $message ) && ! empty( $message ) ) {
851
			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...
852
        }
853
854
        return $errors;
855
    }
856
857
    public static function add_default_templates( $path, $default = true, $template = true ) {
858
        _deprecated_function( __FUNCTION__, '1.07.05', 'FrmXMLController::add_default_templates()' );
859
860
        $path = untrailingslashit(trim($path));
861
		$templates = glob( $path . '/*.php' );
862
863
		for ( $i = count( $templates ) - 1; $i >= 0; $i-- ) {
864
			$filename = str_replace( '.php', '', str_replace( $path . '/', '', $templates[ $i ] ) );
865
			$template_query = array( 'form_key' => $filename );
866
            if ( $template ) {
867
                $template_query['is_template'] = 1;
868
            }
869
            if ( $default ) {
870
                $template_query['default_template'] = 1;
871
            }
872
			$form = FrmForm::getAll( $template_query, '', 1 );
873
874
            $values = FrmFormsHelper::setup_new_vars();
875
            $values['form_key'] = $filename;
876
            $values['is_template'] = $template;
877
            $values['status'] = 'published';
878
            if ( $default ) {
879
                $values['default_template'] = 1;
880
            }
881
882
            include( $templates[ $i ] );
883
884
            //get updated form
885
            if ( isset($form) && ! empty($form) ) {
886
                $old_id = $form->id;
887
                $form = FrmForm::getOne($form->id);
888
            } else {
889
                $old_id = false;
890
				$form = FrmForm::getAll( $template_query, '', 1 );
891
            }
892
893
            if ( $form ) {
894
				do_action( 'frm_after_duplicate_form', $form->id, (array) $form, array( 'old_id' => $old_id ) );
895
            }
896
        }
897
    }
898
899
    public static function route() {
900
        $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...
901
        $vars = array();
902
		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...
903
			FrmAppHelper::permission_check( 'frm_edit_forms' );
904
905
            $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...
906
            $json_vars = json_decode($json_vars, true);
907
            if ( empty($json_vars) ) {
908
                // json decoding failed so we should return an error message
909
				$action = FrmAppHelper::get_param( $action, '', 'get', 'sanitize_title' );
910
                if ( 'edit' == $action ) {
911
                    $action = 'update';
912
                }
913
914
                add_filter('frm_validate_form', 'FrmFormsController::json_error');
915
            } else {
916
                $vars = FrmAppHelper::json_to_array($json_vars);
917
                $action = $vars[ $action ];
918
				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...
919
				$_REQUEST = array_merge( $_REQUEST, $vars );
0 ignored issues
show
introduced by
Detected access of super global var $_REQUEST, probably need manual inspection.
Loading history...
920
				$_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...
921
            }
922
        } else {
923
			$action = FrmAppHelper::get_param( $action, '', 'get', 'sanitize_title' );
924
    		if ( isset( $_REQUEST['delete_all'] ) ) {
925
                // override the action for this page
926
    			$action = 'delete_all';
927
            }
928
        }
929
930
		add_action( 'frm_load_form_hooks', 'FrmHooksController::trigger_load_form_hooks' );
931
        FrmAppHelper::trigger_hook_load( 'form' );
932
933
        switch ( $action ) {
934
            case 'new':
935
                return self::new_form($vars);
936
            case 'create':
937
            case 'edit':
938
            case 'update':
939
            case 'duplicate':
940
            case 'trash':
941
            case 'untrash':
942
            case 'destroy':
943
            case 'delete_all':
944
            case 'settings':
945
            case 'update_settings':
946
				return self::$action( $vars );
947
            default:
948
				do_action( 'frm_form_action_' . $action );
949
				if ( apply_filters( 'frm_form_stop_action_' . $action, false ) ) {
950
                    return;
951
                }
952
953
				$action = FrmAppHelper::get_param( 'action', '', 'get', 'sanitize_text_field' );
954
                if ( $action == -1 ) {
955
					$action = FrmAppHelper::get_param( 'action2', '', 'get', 'sanitize_title' );
956
                }
957
958
                if ( strpos($action, 'bulk_') === 0 ) {
959
                    FrmAppHelper::remove_get_action();
960
                    return self::list_form();
961
                }
962
963
                return self::display_forms_list();
964
        }
965
    }
966
967
    public static function json_error( $errors ) {
968
        $errors['json'] = __( 'Abnormal HTML characters prevented your form from saving correctly', 'formidable' );
969
        return $errors;
970
    }
971
972
973
    /* FRONT-END FORMS */
974
    public static function admin_bar_css() {
975
		if ( is_admin() || ! current_user_can( 'frm_edit_forms' ) ) {
976
            return;
977
        }
978
979
		add_action( 'wp_before_admin_bar_render', 'FrmFormsController::admin_bar_configure' );
980
		FrmAppHelper::load_font_style();
981
	}
982
983
	public static function admin_bar_configure() {
984
        global $frm_vars;
985
        if ( empty($frm_vars['forms_loaded']) ) {
986
            return;
987
        }
988
989
        $actions = array();
990
        foreach ( $frm_vars['forms_loaded'] as $form ) {
991
            if ( is_object($form) ) {
992
                $actions[ $form->id ] = $form->name;
993
            }
994
            unset($form);
995
        }
996
997
        if ( empty($actions) ) {
998
            return;
999
        }
1000
1001
        asort($actions);
1002
1003
        global $wp_admin_bar;
1004
1005
        if ( count($actions) == 1 ) {
1006
            $wp_admin_bar->add_menu( array(
1007
                'title' => 'Edit Form',
1008
				'href'  => admin_url( 'admin.php?page=formidable&frm_action=edit&id=' . current( array_keys( $actions ) ) ),
1009
                'id'    => 'frm-forms',
1010
            ) );
1011
        } else {
1012
            $wp_admin_bar->add_menu( array(
1013
        		'id'    => 'frm-forms',
1014
        		'title' => '<span class="ab-icon"></span><span class="ab-label">' . __( 'Edit Forms', 'formidable' ) . '</span>',
1015
				'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...
1016
        		'meta'  => array(
1017
					'title' => __( 'Edit Forms', 'formidable' ),
1018
        		),
1019
        	) );
1020
1021
        	foreach ( $actions as $form_id => $name ) {
1022
1023
        		$wp_admin_bar->add_menu( array(
1024
        			'parent'    => 'frm-forms',
1025
					'id'        => 'edit_form_' . $form_id,
1026
        			'title'     => empty($name) ? __( '(no title)') : $name,
1027
					'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...
1028
        		) );
1029
        	}
1030
        }
1031
    }
1032
1033
    //formidable shortcode
1034
	public static function get_form_shortcode( $atts ) {
1035
        global $frm_vars;
1036
        if ( isset($frm_vars['skip_shortcode']) && $frm_vars['skip_shortcode'] ) {
1037
            $sc = '[formidable';
1038
			if ( ! empty( $atts ) ) {
1039
				foreach ( $atts as $k => $v ) {
1040
					$sc .= ' ' . $k . '="' . esc_attr( $v ) . '"';
1041
				}
1042
			}
1043
			return $sc . ']';
1044
        }
1045
1046
        $shortcode_atts = shortcode_atts( array(
1047
            'id' => '', 'key' => '', 'title' => false, 'description' => false,
1048
            'readonly' => false, 'entry_id' => false, 'fields' => array(),
1049
            'exclude_fields' => array(), 'minimize' => false,
1050
        ), $atts);
1051
        do_action('formidable_shortcode_atts', $shortcode_atts, $atts);
1052
1053
        return self::show_form(
1054
            $shortcode_atts['id'], $shortcode_atts['key'], $shortcode_atts['title'],
1055
            $shortcode_atts['description'], $atts
1056
        );
1057
    }
1058
1059
    public static function show_form( $id = '', $key = '', $title = false, $description = false, $atts = array() ) {
1060
        if ( empty( $id ) ) {
1061
            $id = $key;
1062
        }
1063
1064
        $form = self::maybe_get_form_to_show( $id );
1065
        if ( ! $form ) {
1066
            return __( 'Please select a valid form', 'formidable' );
1067
        }
1068
1069
		add_action( 'frm_load_form_hooks', 'FrmHooksController::trigger_load_form_hooks' );
1070
        FrmAppHelper::trigger_hook_load( 'form', $form );
1071
1072
        $form = apply_filters( 'frm_pre_display_form', $form );
1073
1074
        $frm_settings = FrmAppHelper::get_settings();
1075
1076
		if ( self::is_viewable_draft_form( $form ) ) {
1077
			// don't show a draft form on a page
1078
			$form = __( 'Please select a valid form', 'formidable' );
1079
		} else if ( self::user_should_login( $form ) ) {
1080
			$form = do_shortcode( $frm_settings->login_msg );
1081
		} else if ( self::user_has_permission_to_view( $form ) ) {
1082
			$form = do_shortcode( $frm_settings->login_msg );
1083
		} else {
1084
			$form = self::get_form( $form, $title, $description, $atts );
1085
1086
			/**
1087
			 * Use this shortcode to check for external shortcodes that may span
1088
			 * across multiple fields in the customizable HTML
1089
			 * @since 2.0.8
1090
			 */
1091
			$form = apply_filters( 'frm_filter_final_form', $form );
1092
		}
1093
1094
		return $form;
1095
    }
1096
1097
	private static function maybe_get_form_to_show( $id ) {
1098
		$form = false;
1099
1100
		if ( ! empty( $id ) ) { // no form id or key set
1101
			$form = FrmForm::getOne( $id );
1102
			if ( ! $form || $form->parent_form_id || $form->status == 'trash' ) {
1103
				$form = false;
1104
			}
1105
		}
1106
1107
		return $form;
1108
	}
1109
1110
	private static function is_viewable_draft_form( $form ) {
1111
		global $post;
1112
		$frm_settings = FrmAppHelper::get_settings();
1113
		return $form->status == 'draft' && current_user_can( 'frm_edit_forms' ) && ( ! $post || $post->ID != $frm_settings->preview_page_id ) && ! FrmAppHelper::is_preview_page();
1114
	}
1115
1116
	private static function user_should_login( $form ) {
1117
		return $form->logged_in && ! is_user_logged_in();
1118
	}
1119
1120
	private static function user_has_permission_to_view( $form ) {
1121
		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'] );
1122
	}
1123
1124
    public static function get_form( $form, $title, $description, $atts = array() ) {
1125
        ob_start();
1126
1127
        self::get_form_contents( $form, $title, $description, $atts );
1128
		self::enqueue_scripts( FrmForm::get_params( $form ) );
1129
1130
        $contents = ob_get_contents();
1131
        ob_end_clean();
1132
1133
		self::maybe_minimize_form( $atts, $contents );
1134
1135
        return $contents;
1136
    }
1137
1138
	public static function enqueue_scripts( $params ) {
1139
		do_action( 'frm_enqueue_form_scripts', $params );
1140
	}
1141
1142
	public static function get_form_contents( $form, $title, $description, $atts ) {
1143
        global $frm_vars;
1144
1145
        $frm_settings = FrmAppHelper::get_settings();
1146
1147
        $submit = isset($form->options['submit_value']) ? $form->options['submit_value'] : $frm_settings->submit_value;
1148
1149
        $user_ID = get_current_user_id();
1150
		$params = FrmForm::get_params( $form );
1151
        $message = $errors = '';
1152
1153
        if ( $params['posted_form_id'] == $form->id && $_POST ) {
1154
            $errors = isset( $frm_vars['created_entries'][ $form->id ] ) ? $frm_vars['created_entries'][ $form->id ]['errors'] : array();
1155
        }
1156
1157
		$include_form_tag = apply_filters( 'frm_include_form_tag', true, $form );
1158
        $fields = FrmFieldsHelper::get_form_fields( $form->id, ( isset( $errors ) && ! empty( $errors ) ) );
1159
1160
        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...
1161
            do_action('frm_display_form_action', $params, $fields, $form, $title, $description);
1162
            if ( apply_filters('frm_continue_to_new', true, $form->id, $params['action']) ) {
1163
                $values = FrmEntriesHelper::setup_new_vars($fields, $form);
1164
				include( FrmAppHelper::plugin_path() . '/classes/views/frm-entries/new.php' );
1165
            }
1166
            return;
1167
        }
1168
1169
        if ( ! empty($errors) ) {
1170
            $values = $fields ? FrmEntriesHelper::setup_new_vars($fields, $form) : array();
1171
			include( FrmAppHelper::plugin_path() . '/classes/views/frm-entries/new.php' );
1172
            return;
1173
        }
1174
1175
        do_action('frm_validate_form_creation', $params, $fields, $form, $title, $description);
1176
        if ( ! apply_filters('frm_continue_to_create', true, $form->id) ) {
1177
            return;
1178
        }
1179
1180
        $values = FrmEntriesHelper::setup_new_vars($fields, $form, true);
1181
        $created = self::just_created_entry( $form->id );
1182
        $conf_method = apply_filters('frm_success_filter', 'message', $form, $form->options, 'create');
1183
1184
        if ( $created && is_numeric($created) && $conf_method != 'message' ) {
1185
            do_action('frm_success_action', $conf_method, $form, $form->options, $created);
1186
			do_action( 'frm_after_entry_processed', array( 'entry_id' => $created, 'form' => $form ) );
1187
            return;
1188
        }
1189
1190
        if ( $created && is_numeric($created) ) {
1191
            $message = isset($form->options['success_msg']) ? $form->options['success_msg'] : $frm_settings->success_msg;
1192
            $class = 'frm_message';
1193
        } else {
1194
            $message = $frm_settings->failed_msg;
1195
            $class = FrmFormsHelper::form_error_class();
1196
        }
1197
1198
		$message = FrmFormsHelper::get_success_message( array(
1199
			'message' => $message, 'form' => $form,
1200
			'entry_id' => $created, 'class' => $class,
1201
		) );
1202
        $message = apply_filters('frm_main_feedback', $message, $form, $created);
1203
1204
        if ( ! isset($form->options['show_form']) || $form->options['show_form'] ) {
1205
			require( FrmAppHelper::plugin_path() . '/classes/views/frm-entries/new.php' );
1206
        } else {
1207
            global $frm_vars;
1208
			self::maybe_load_css( $form, $values['custom_style'], $frm_vars['load_css'] );
1209
1210
			$include_extra_container = 'frm_forms' . FrmFormsHelper::get_form_style_class( $values );
1211
			include( FrmAppHelper::plugin_path() . '/classes/views/frm-entries/errors.php' );
1212
        }
1213
1214
		do_action( 'frm_after_entry_processed', array( 'entry_id' => $created, 'form' => $form ) );
1215
    }
1216
1217
	/**
1218
	 * @since 2.2.7
1219
	 */
1220
	public static function just_created_entry( $form_id ) {
1221
		global $frm_vars;
1222
		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;
1223
	}
1224
1225
	public static function front_head() {
1226
		$version = FrmAppHelper::plugin_version();
1227
		$suffix = defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ? '' : '.min';
1228
		wp_register_script( 'formidable', FrmAppHelper::plugin_url() . "/js/formidable{$suffix}.js", array( 'jquery' ), $version, true );
1229
		wp_register_script( 'jquery-placeholder', FrmAppHelper::plugin_url() . '/js/jquery/jquery.placeholder.min.js', array( 'jquery' ), '2.0.7', true );
1230
		add_filter( 'script_loader_tag', 'FrmFormsController::defer_script_loading', 10, 2 );
1231
1232
		if ( FrmAppHelper::is_admin() ) {
1233
			// don't load this in back-end
1234
			return;
1235
		}
1236
1237
		FrmAppHelper::localize_script( 'front' );
1238
		FrmStylesController::enqueue_css( 'register' );
1239
	}
1240
1241
	public static function maybe_load_css( $form, $this_load, $global_load ) {
1242
		$load_css = FrmForm::is_form_loaded( $form, $this_load, $global_load );
1243
1244
		if ( $load_css ) {
1245
			global $frm_vars;
1246
			self::footer_js( 'header' );
1247
			$frm_vars['css_loaded'] = true;
1248
		}
1249
	}
1250
1251
	public static function defer_script_loading( $tag, $handle ) {
1252
	    if ( 'recaptcha-api' == $handle && ! strpos( $tag, 'defer' ) ) {
1253
	        $tag = str_replace( ' src', ' defer="defer" async="async" src', $tag );
1254
		}
1255
	    return $tag;
1256
	}
1257
1258
	public static function footer_js( $location = 'footer' ) {
1259
		global $frm_vars;
1260
1261
		FrmStylesController::enqueue_css();
1262
1263
		if ( ! FrmAppHelper::is_admin() && $location != 'header' && ! empty( $frm_vars['forms_loaded'] ) ) {
1264
			//load formidable js
1265
			wp_enqueue_script( 'formidable' );
1266
		}
1267
	}
1268
1269
	/**
1270
	 * @since 2.0.8
1271
	 */
1272
	private static function maybe_minimize_form( $atts, &$content ) {
1273
		// check if minimizing is turned on
1274
		if ( self::is_minification_on( $atts ) ) {
1275
			$content = str_replace( array( "\r\n", "\r", "\n", "\t", '    ' ), '', $content );
1276
		}
1277
	}
1278
1279
	/**
1280
	 * @since 2.0.8
1281
	 * @return boolean
1282
	 */
1283
	private static function is_minification_on( $atts ) {
1284
		return isset( $atts['minimize'] ) && ! empty( $atts['minimize'] );
1285
	}
1286
}