Completed
Push — master ( 3f265f...9a1967 )
by Stephanie
04:15
created

FrmFormsController::footer_js()   A

Complexity

Conditions 4
Paths 2

Size

Total Lines 10
Code Lines 5

Duplication

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