Completed
Pull Request — master (#100)
by Stephanie
03:13
created

FrmFormsController   F

Complexity

Total Complexity 292

Size/Duplication

Total Lines 1691
Duplicated Lines 0 %

Coupling/Cohesion

Components 5
Dependencies 14

Importance

Changes 0
Metric Value
dl 0
loc 1691
rs 0.8
c 0
b 0
f 0
wmc 292
lcom 5
cbo 14

96 Methods

Rating   Name   Duplication   Size   Complexity  
A menu() 0 9 2
A maybe_load_listing_hooks() 0 11 3
A head() 0 7 2
A register_widgets() 0 4 1
A prevent_divi_conflict() 0 4 1
A list_form() 0 9 1
A new_form() 0 25 5
A create_default_email_action() 0 8 2
B create() 0 39 9
A edit() 0 6 2
A settings() 0 8 3
A update_settings() 0 17 2
B update() 0 29 7
A _create_from_template() 0 14 2
A duplicate() 0 12 3
A page_preview() 0 11 3
A show_page_preview() 0 3 1
A preview() 0 18 2
A load_wp() 0 9 3
A set_preview_query() 0 20 2
A load_theme_preview() 0 8 1
A preview_page_title() 0 6 2
A preview_title() 0 3 1
A preview_content() 0 6 2
A load_direct_preview() 0 15 3
A untrash() 0 3 1
A bulk_untrash() 0 8 1
A trash() 0 3 1
A change_form_status() 0 42 3
A bulk_trash() 0 25 3
A destroy() 0 17 2
A bulk_destroy() 0 15 3
A delete_all() 0 13 2
A insert_form_button() 0 9 2
A insert_form_popup() 0 19 2
B get_shortcode_opts() 0 47 5
A display_forms_list() 0 23 4
A get_columns() 0 36 2
A get_sortable_columns() 0 9 1
A hidden_columns() 0 15 2
A save_per_page() 0 6 2
B get_edit_vars() 0 46 9
A get_settings_vars() 0 27 4
A clean_submit_html() 0 5 3
A mb_tags_box() 0 13 2
A advanced_helpers() 0 29 3
A get_advanced_shortcodes() 0 18 1
A user_shortcodes() 0 13 1
A get_shortcode_helpers() 0 32 3
A form_classes() 0 9 4
A get_email_html() 0 13 1
A filter_content() 0 15 3
A get_entry_by_param() 0 9 5
A replace_content_shortcodes() 0 3 1
C process_bulk_form_actions() 0 49 13
F route() 0 67 20
A json_error() 0 4 1
A admin_bar_css() 0 8 3
A admin_bar_configure() 0 21 5
A add_menu_to_admin_bar() 0 14 1
A add_forms_to_admin_bar() 0 17 3
A get_form_shortcode() 0 30 5
B show_form() 0 40 6
A maybe_get_form_to_show() 0 12 5
A is_viewable_draft_form() 0 5 3
A user_should_login() 0 3 2
A user_has_permission_to_view() 0 3 5
A get_form() 0 15 1
A enqueue_scripts() 0 3 1
B get_form_contents() 0 39 7
A get_saved_errors() 0 10 4
A just_created_entry() 0 4 4
A get_confirmation_method() 0 11 6
A maybe_trigger_redirect() 0 17 3
A trigger_redirect() 0 14 2
A run_success_action() 0 16 6
A load_page_after_submit() 0 12 3
B redirect_after_submit() 0 35 6
A get_redirect_message() 0 12 1
A show_message_after_save() 0 9 3
A show_form_after_submit() 0 23 3
A fill_atts_for_form_display() 0 12 1
A show_lone_success_messsage() 0 12 1
A prepare_submit_message() 0 14 4
A front_head() 0 20 4
A has_combo_js_file() 0 3 1
A maybe_load_css() 0 9 2
A defer_script_loading() 0 6 3
A footer_js() 0 10 4
A maybe_minimize_form() 0 6 2
A is_minification_on() 0 3 2
A add_default_templates() 0 3 1
A bulk_create_template() 0 3 1
A register_pro_scripts() 0 3 1
A edit_key() 0 3 1
A edit_description() 0 3 1

How to fix   Complexity   

Complex Class

Complex classes like FrmFormsController often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

While breaking up the class, it is a good idea to analyze how other classes use FrmFormsController, and based on these observations, apply Extract Interface, too.

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', 'destroy' ) ) ) {
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
	/**
41
	 * By default, Divi processes form shortcodes on the edit post page.
42
	 * Now that won't do.
43
	 *
44
	 * @since 3.01
45
	 */
46
	public static function prevent_divi_conflict( $shortcodes ) {
47
		$shortcodes[] = 'formidable';
48
		return $shortcodes;
49
	}
50
51
	public static function list_form() {
52
		FrmAppHelper::permission_check( 'frm_view_forms' );
53
54
		$params = FrmForm::list_page_params();
55
		$errors = self::process_bulk_form_actions( array() );
56
		$errors = apply_filters( 'frm_admin_list_form_action', $errors );
57
58
		return self::display_forms_list( $params, '', $errors );
59
	}
60
61
	public static function new_form( $values = array() ) {
62
		FrmAppHelper::permission_check( 'frm_edit_forms' );
63
64
        global $frm_vars;
65
66
		$action = isset( $_REQUEST['frm_action'] ) ? 'frm_action' : 'action';
67
		$action = empty( $values ) ? FrmAppHelper::get_param( $action, '', 'get', 'sanitize_title' ) : $values[ $action ];
68
69
		if ( $action == 'create' ) {
70
			self::create( $values );
71
			return;
72
		} else if ( $action == 'new' ) {
73
			$frm_field_selection = FrmField::field_selection();
74
			$values = FrmFormsHelper::setup_new_vars( $values );
75
            $id = FrmForm::create( $values );
76
			$form = FrmForm::getOne( $id );
77
78
			self::create_default_email_action( $form );
79
80
			$all_templates = FrmForm::getAll( array( 'is_template' => 1 ), 'name' );
81
82
            $values['id'] = $id;
83
			require( FrmAppHelper::plugin_path() . '/classes/views/frm-forms/new.php' );
84
        }
85
    }
86
87
	/**
88
	 * Create the default email action
89
	 *
90
	 * @since 2.02.11
91
	 *
92
	 * @param object $form
93
	 */
94
    private static function create_default_email_action( $form ) {
95
    	$create_email = apply_filters( 'frm_create_default_email_action', true, $form );
96
97
	    if ( $create_email ) {
98
		    $action_control = FrmFormActionsController::get_form_actions( 'email' );
99
		    $action_control->create( $form->id );
100
	    }
101
    }
102
103
	public static function create( $values = array() ) {
104
		FrmAppHelper::permission_check( 'frm_edit_forms' );
105
106
        global $frm_vars;
107
        if ( empty( $values ) ) {
108
            $values = $_POST;
109
        }
110
111
        //Set radio button and checkbox meta equal to "other" value
112
        if ( FrmAppHelper::pro_is_installed() ) {
113
            $values = FrmProEntry::mod_other_vals( $values, 'back' );
114
        }
115
116
		$id = isset( $values['id'] ) ? absint( $values['id'] ) : FrmAppHelper::get_param( 'id', '', 'get', 'absint' );
117
118
        if ( ! current_user_can( 'frm_edit_forms' ) || ( $_POST && ( ! isset( $values['frm_save_form'] ) || ! wp_verify_nonce( $values['frm_save_form'], 'frm_save_form_nonce' ) ) ) ) {
119
            $frm_settings = FrmAppHelper::get_settings();
120
            $errors = array( 'form' => $frm_settings->admin_permission );
121
        } else {
122
			$errors = FrmForm::validate( $values );
123
        }
124
125
		if ( count( $errors ) > 0 ) {
126
            $hide_preview = true;
127
			$frm_field_selection = FrmField::field_selection();
128
            $form = FrmForm::getOne( $id );
129
			$fields = FrmField::get_all_for_form( $id );
130
131
			$values = FrmAppHelper::setup_edit_vars( $form, 'forms', '', true );
132
			$values['fields'] = $fields;
133
			$all_templates = FrmForm::getAll( array( 'is_template' => 1 ), 'name' );
134
135
			require( FrmAppHelper::plugin_path() . '/classes/views/frm-forms/new.php' );
136
        } else {
137
            FrmForm::update( $id, $values, true );
138
			$url = admin_url( 'admin.php?page=formidable&frm_action=settings&id=' . $id );
139
			die( FrmAppHelper::js_redirect( $url ) ); // WPCS: XSS ok.
140
        }
141
    }
142
143
    public static function edit( $values = false ) {
144
		FrmAppHelper::permission_check( 'frm_edit_forms' );
145
146
		$id = isset( $values['id'] ) ? absint( $values['id'] ) : FrmAppHelper::get_param( 'id', '', 'get', 'absint' );
147
		return self::get_edit_vars( $id );
148
    }
149
150
    public static function settings( $id = false, $message = '' ) {
151
		FrmAppHelper::permission_check( 'frm_edit_forms' );
152
153
		if ( ! $id || ! is_numeric( $id ) ) {
154
			$id = FrmAppHelper::get_param( 'id', '', 'get', 'absint' );
155
        }
156
		return self::get_settings_vars( $id, array(), $message );
157
    }
158
159
    public static function update_settings() {
160
		FrmAppHelper::permission_check( 'frm_edit_forms' );
161
162
		$id = FrmAppHelper::get_param( 'id', '', 'get', 'absint' );
163
164
		$errors = FrmForm::validate( $_POST );
165
		if ( count( $errors ) > 0 ) {
166
			return self::get_settings_vars( $id, $errors );
167
		}
168
169
		do_action( 'frm_before_update_form_settings', $id );
170
171
		FrmForm::update( $id, $_POST );
172
173
        $message = __( 'Settings Successfully Updated', 'formidable' );
174
		return self::get_settings_vars( $id, array(), $message );
175
    }
176
177
	public static function update( $values = array() ) {
178
		if ( empty( $values ) ) {
179
            $values = $_POST;
180
        }
181
182
        //Set radio button and checkbox meta equal to "other" value
183
        if ( FrmAppHelper::pro_is_installed() ) {
184
            $values = FrmProEntry::mod_other_vals( $values, 'back' );
185
        }
186
187
        $errors = FrmForm::validate( $values );
188
        $permission_error = FrmAppHelper::permission_nonce_error( 'frm_edit_forms', 'frm_save_form', 'frm_save_form_nonce' );
189
        if ( $permission_error !== false ) {
190
            $errors['form'] = $permission_error;
191
        }
192
193
		$id = isset( $values['id'] ) ? absint( $values['id'] ) : FrmAppHelper::get_param( 'id', '', 'get', 'absint' );
194
195
		if ( count( $errors ) > 0 ) {
196
            return self::get_edit_vars( $id, $errors );
197
		} else {
198
            FrmForm::update( $id, $values );
199
            $message = __( 'Form was Successfully Updated', 'formidable' );
200
            if ( defined( 'DOING_AJAX' ) ) {
201
				wp_die( esc_html( $message ) );
202
            }
203
			return self::get_edit_vars( $id, array(), $message );
204
        }
205
    }
206
207
	/**
208
	 * Redirect to the url for creating from a template
209
	 * Also delete the current form
210
	 * @since 2.0
211
	 */
212
	public static function _create_from_template() {
213
		FrmAppHelper::permission_check( 'frm_edit_forms' );
214
		check_ajax_referer( 'frm_ajax', 'nonce' );
215
216
		$current_form = FrmAppHelper::get_param( 'this_form', '', 'get', 'absint' );
217
		$template_id = FrmAppHelper::get_param( 'id', '', 'get', 'absint' );
218
219
		if ( $current_form ) {
220
			FrmForm::destroy( $current_form );
221
		}
222
223
		echo esc_url_raw( admin_url( 'admin.php?page=formidable&frm_action=duplicate&id=' . absint( $template_id ) ) );
224
		wp_die();
225
	}
226
227
    public static function duplicate() {
228
		FrmAppHelper::permission_check( 'frm_edit_forms' );
229
230
		$params = FrmForm::list_page_params();
231
        $form = FrmForm::duplicate( $params['id'], $params['template'], true );
232
        $message = $params['template'] ? __( 'Form template was Successfully Created', 'formidable' ) : __( 'Form was Successfully Copied', 'formidable' );
233
        if ( $form ) {
234
			return self::get_edit_vars( $form, array(), $message, true );
235
        } else {
236
			return self::display_forms_list( $params, __( 'There was a problem creating the new template.', 'formidable' ) );
237
        }
238
    }
239
240
    public static function page_preview() {
241
		$params = FrmForm::list_page_params();
242
        if ( ! $params['form'] ) {
243
            return;
244
        }
245
246
        $form = FrmForm::getOne( $params['form'] );
247
		if ( $form ) {
248
			return self::show_form( $form->id, '', true, true );
249
		}
250
    }
251
252
	/**
253
	 * @since 3.0
254
	 */
255
	public static function show_page_preview() {
256
		echo self::page_preview(); // WPCS: XSS ok.
257
	}
258
259
    public static function preview() {
260
        do_action( 'frm_wp' );
261
262
        global $frm_vars;
263
        $frm_vars['preview'] = true;
264
265
		self::load_wp();
266
267
		$include_theme = FrmAppHelper::get_param( 'theme', '', 'get', 'absint' );
268
		if ( $include_theme ) {
269
			self::set_preview_query();
270
			self::load_theme_preview();
271
		} else {
272
			self::load_direct_preview();
273
		}
274
275
		wp_die();
276
	}
277
278
	/**
279
	 * @since 3.0
280
	 */
281
	private static function load_wp() {
282
		if ( ! defined( 'ABSPATH' ) && ! defined( 'XMLRPC_REQUEST' ) ) {
283
			global $wp;
284
			$root = dirname( dirname( dirname( dirname( __FILE__ ) ) ) );
285
			include_once( $root . '/wp-config.php' );
286
			$wp->init();
287
			$wp->register_globals();
288
		}
289
	}
290
291
	private static function set_preview_query() {
292
		$random_page = get_posts(
293
			array(
294
				'numberposts' => 1,
295
				'orderby'     => 'date',
296
				'order'       => 'ASC',
297
				'post_type'   => 'page',
298
			)
299
		);
300
301
		if ( ! empty( $random_page ) ) {
302
			$random_page = reset( $random_page );
303
			query_posts(
0 ignored issues
show
Coding Style introduced by
The use of function query_posts() is discouraged; use WP_Query() instead
Loading history...
304
				array(
305
					'post_type' => 'page',
306
					'page_id'   => $random_page->ID,
307
				)
308
			);
309
		}
310
	}
311
312
	/**
313
	 * @since 3.0
314
	 */
315
	private static function load_theme_preview() {
316
		add_filter( 'wp_title', 'FrmFormsController::preview_title', 9999 );
317
		add_filter( 'the_title', 'FrmFormsController::preview_page_title', 9999 );
318
		add_filter( 'the_content', 'FrmFormsController::preview_content', 9999 );
319
		add_action( 'loop_no_results', 'FrmFormsController::show_page_preview' );
320
		add_filter( 'is_active_sidebar', '__return_false' );
321
		get_template_part( 'page' );
322
	}
323
324
325
	/**
326
	 * Set the page title for the theme preview page
327
	 *
328
	 * @since 3.0
329
	 */
330
	public static function preview_page_title( $title ) {
331
		if ( in_the_loop() ) {
332
			$title = self::preview_title( $title );
333
		}
334
		return $title;
335
	}
336
337
	/**
338
	 * Set the page title for the theme preview page
339
	 *
340
	 * @since 3.0
341
	 */
342
	public static function preview_title( $title ) {
343
		return __( 'Form Preview', 'formidable' );
344
	}
345
346
	/**
347
	 * Set the page content for the theme preview page
348
	 *
349
	 * @since 3.0
350
	 */
351
	public static function preview_content( $content ) {
352
		if ( in_the_loop() ) {
353
			$content = FrmFormsController::show_page_preview();
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $content is correct as \FrmFormsController::show_page_preview() (which targets FrmFormsController::show_page_preview()) seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

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

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

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

Loading history...
354
		}
355
		return $content;
356
	}
357
358
	/**
359
	 * @since 3.0
360
	 */
361
	private static function load_direct_preview() {
362
		header( 'Content-Type: text/html; charset=' . get_option( 'blog_charset' ) );
363
364
		$key = FrmAppHelper::simple_get( 'form', 'sanitize_title' );
365
		if ( $key == '' ) {
366
			$key = FrmAppHelper::get_post_param( 'form', '', 'sanitize_title' );
367
		}
368
369
		$form = FrmForm::getAll( array( 'form_key' => $key ), '', 1 );
370
		if ( empty( $form ) ) {
371
			$form = FrmForm::getAll( array(), '', 1 );
372
		}
373
374
		require( FrmAppHelper::plugin_path() . '/classes/views/frm-entries/direct.php' );
375
	}
376
377
    public static function untrash() {
378
		self::change_form_status( 'untrash' );
379
    }
380
381
	public static function bulk_untrash( $ids ) {
382
		FrmAppHelper::permission_check( 'frm_edit_forms' );
383
384
        $count = FrmForm::set_status( $ids, 'published' );
385
386
		$message = sprintf( _n( '%1$s form restored from the Trash.', '%1$s forms restored from the Trash.', $count, 'formidable' ), 1 );
387
        return $message;
388
    }
389
390
    public static function trash() {
391
		self::change_form_status( 'trash' );
392
    }
393
394
	/**
395
	 * @param string $status
396
	 *
397
	 * @return int The number of forms changed
398
	 */
399
	public static function change_form_status( $status ) {
400
		$available_status = array(
401
			'untrash' => array(
402
				'permission' => 'frm_edit_forms',
403
				'new_status' => 'published',
404
			),
405
			'trash'   => array(
406
				'permission' => 'frm_delete_forms',
407
				'new_status' => 'trash',
408
			),
409
		);
410
411
		if ( ! isset( $available_status[ $status ] ) ) {
412
			return;
413
		}
414
415
		FrmAppHelper::permission_check( $available_status[ $status ]['permission'] );
416
417
		$params = FrmForm::list_page_params();
418
419
		//check nonce url
420
		check_admin_referer( $status . '_form_' . $params['id'] );
421
422
		$count = 0;
423
		if ( FrmForm::set_status( $params['id'], $available_status[ $status ]['new_status'] ) ) {
424
			$count++;
425
		}
426
427
		$form_type = FrmAppHelper::get_simple_request(
428
			array(
429
				'param' => 'form_type',
430
				'type' => 'request',
431
			)
432
		);
433
434
		$available_status['untrash']['message'] = sprintf( _n( '%1$s form restored from the Trash.', '%1$s forms restored from the Trash.', $count, 'formidable' ), $count );
435
		$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=' . $form_type . '&id=' . $params['id'], 'untrash_form_' . $params['id'] ) ) . '">', '</a>' );
436
437
		$message = $available_status[ $status ]['message'];
438
439
		self::display_forms_list( $params, $message );
440
	}
441
442
	public static function bulk_trash( $ids ) {
443
		FrmAppHelper::permission_check( 'frm_delete_forms' );
444
445
        $count = 0;
446
        foreach ( $ids as $id ) {
447
            if ( FrmForm::trash( $id ) ) {
448
                $count++;
449
            }
450
        }
451
452
		$current_page = FrmAppHelper::get_simple_request(
453
			array(
454
				'param' => 'form_type',
455
				'type' => 'request',
456
			)
457
		);
458
		$message = sprintf(
459
			_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' ),
460
			$count,
461
			'<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' ) ) . '">',
462
			'</a>'
463
		);
464
465
        return $message;
466
    }
467
468
    public static function destroy() {
469
		FrmAppHelper::permission_check( 'frm_delete_forms' );
470
471
		$params = FrmForm::list_page_params();
472
473
        //check nonce url
474
		check_admin_referer( 'destroy_form_' . $params['id'] );
475
476
        $count = 0;
477
        if ( FrmForm::destroy( $params['id'] ) ) {
478
            $count++;
479
        }
480
481
		$message = sprintf( _n( '%1$s form permanently deleted.', '%1$s forms permanently deleted.', $count, 'formidable' ), $count );
482
483
		self::display_forms_list( $params, $message );
484
    }
485
486
	public static function bulk_destroy( $ids ) {
487
		FrmAppHelper::permission_check( 'frm_delete_forms' );
488
489
        $count = 0;
490
        foreach ( $ids as $id ) {
491
            $d = FrmForm::destroy( $id );
492
            if ( $d ) {
493
                $count++;
494
            }
495
        }
496
497
		$message = sprintf( _n( '%1$s form permanently deleted.', '%1$s forms permanently deleted.', $count, 'formidable' ), $count );
498
499
        return $message;
500
    }
501
502
    private static function delete_all() {
503
        //check nonce url
504
		$permission_error = FrmAppHelper::permission_nonce_error( 'frm_delete_forms', '_wpnonce', 'bulk-toplevel_page_formidable' );
505
        if ( $permission_error !== false ) {
506
			self::display_forms_list( array(), '', array( $permission_error ) );
507
            return;
508
        }
509
510
		$count = FrmForm::scheduled_delete( time() );
511
		$message = sprintf( _n( '%1$s form permanently deleted.', '%1$s forms permanently deleted.', $count, 'formidable' ), $count );
512
513
		self::display_forms_list( array(), $message );
514
    }
515
516
	/**
517
	* Inserts Formidable button
518
	* Hook exists since 2.5.0
519
	*
520
	* @since 2.0.15
521
	*/
522
	public static function insert_form_button() {
523
		if ( current_user_can( 'frm_view_forms' ) ) {
524
			$menu_name = FrmAppHelper::get_menu_name();
525
			$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' ) . '">
526
				<span class="frm-buttons-icon wp-media-buttons-icon"></span> ' .
527
				$menu_name . '</a>';
528
			echo wp_kses_post( $content );
529
		}
530
	}
531
532
    public static function insert_form_popup() {
533
		$page = basename( FrmAppHelper::get_server_value( 'PHP_SELF' ) );
534
		if ( ! in_array( $page, array( 'post.php', 'page.php', 'page-new.php', 'post-new.php' ) ) ) {
535
            return;
536
        }
537
538
        FrmAppHelper::load_admin_wide_js();
539
540
        $shortcodes = array(
541
			'formidable' => array(
542
				'name'  => __( 'Form', 'formidable' ),
543
				'label' => __( 'Insert a Form', 'formidable' ),
544
			),
545
        );
546
547
		$shortcodes = apply_filters( 'frm_popup_shortcodes', $shortcodes );
548
549
		include( FrmAppHelper::plugin_path() . '/classes/views/frm-forms/insert_form_popup.php' );
550
    }
551
552
    public static function get_shortcode_opts() {
553
		FrmAppHelper::permission_check( 'frm_view_forms' );
554
        check_ajax_referer( 'frm_ajax', 'nonce' );
555
556
		$shortcode = FrmAppHelper::get_post_param( 'shortcode', '', 'sanitize_text_field' );
557
		if ( empty( $shortcode ) ) {
558
            wp_die();
559
        }
560
561
		echo '<div id="sc-opts-' . esc_attr( $shortcode ) . '" class="frm_shortcode_option">';
562
		echo '<input type="radio" name="frmsc" value="' . esc_attr( $shortcode ) . '" id="sc-' . esc_attr( $shortcode ) . '" class="frm_hidden" />';
563
564
        $form_id = '';
565
        $opts = array();
566
		switch ( $shortcode ) {
567
            case 'formidable':
568
                $opts = array(
569
					'form_id'       => 'id',
570
                    //'key' => ',
571
					'title'         => array(
572
						'val'   => 1,
573
						'label' => __( 'Display form title', 'formidable' ),
574
					),
575
					'description'   => array(
576
						'val'   => 1,
577
						'label' => __( 'Display form description', 'formidable' ),
578
					),
579
					'minimize'      => array(
580
						'val'   => 1,
581
						'label' => __( 'Minimize form HTML', 'formidable' ),
582
					),
583
                );
584
        }
585
		$opts = apply_filters( 'frm_sc_popup_opts', $opts, $shortcode );
586
587
		if ( isset( $opts['form_id'] ) && is_string( $opts['form_id'] ) ) {
588
			// allow other shortcodes to use the required form id option
589
			$form_id = $opts['form_id'];
590
			unset( $opts['form_id'] );
591
		}
592
593
		include( FrmAppHelper::plugin_path() . '/classes/views/frm-forms/shortcode_opts.php' );
594
595
        echo '</div>';
596
597
        wp_die();
598
    }
599
600
	public static function display_forms_list( $params = array(), $message = '', $errors = array() ) {
601
        FrmAppHelper::permission_check( 'frm_view_forms' );
602
603
        global $wpdb, $frm_vars;
604
605
		if ( empty( $params ) ) {
606
			$params = FrmForm::list_page_params();
607
        }
608
609
        $wp_list_table = new FrmFormsListHelper( compact( 'params' ) );
610
611
        $pagenum = $wp_list_table->get_pagenum();
612
613
        $wp_list_table->prepare_items();
614
615
        $total_pages = $wp_list_table->get_pagination_arg( 'total_pages' );
616
        if ( $pagenum > $total_pages && $total_pages > 0 ) {
617
			wp_redirect( esc_url_raw( add_query_arg( 'paged', $total_pages ) ) );
618
            die();
619
        }
620
621
		require( FrmAppHelper::plugin_path() . '/classes/views/frm-forms/list.php' );
622
    }
623
624
	public static function get_columns( $columns ) {
625
	    $columns['cb'] = '<input type="checkbox" />';
626
	    $columns['id'] = 'ID';
627
628
		$type = FrmAppHelper::get_simple_request(
629
			array(
630
				'param'   => 'form_type',
631
				'type'    => 'request',
632
				'default' => 'published',
633
			)
634
		);
635
636
        if ( 'template' == $type ) {
637
            $columns['name']        = __( 'Template Name', 'formidable' );
638
            $columns['type']        = __( 'Type', 'formidable' );
639
            $columns['form_key']    = __( 'Key', 'formidable' );
640
        } else {
641
            $columns['name']        = __( 'Form Title', 'formidable' );
642
            $columns['entries']     = __( 'Entries', 'formidable' );
643
            $columns['form_key']    = __( 'Key', 'formidable' );
644
            $columns['shortcode']   = __( 'Shortcodes', 'formidable' );
645
        }
646
647
        $columns['created_at'] = __( 'Date', 'formidable' );
648
649
		add_screen_option(
650
			'per_page',
651
			array(
652
				'label'   => __( 'Forms', 'formidable' ),
653
				'default' => 20,
654
				'option'  => 'formidable_page_formidable_per_page',
655
			)
656
		);
657
658
        return $columns;
659
	}
660
661
	public static function get_sortable_columns() {
662
		return array(
663
			'id'            => 'id',
664
			'name'          => 'name',
665
			'description'   => 'description',
666
			'form_key'      => 'form_key',
667
			'created_at'    => 'created_at',
668
		);
669
	}
670
671
	public static function hidden_columns( $hidden_columns ) {
672
		$type = FrmAppHelper::get_simple_request(
673
			array(
674
				'param' => 'form_type',
675
				'type'  => 'request',
676
			)
677
		);
678
679
		if ( $type === 'template' ) {
680
			$hidden_columns[] = 'id';
681
			$hidden_columns[] = 'form_key';
682
		}
683
684
		return $hidden_columns;
685
	}
686
687
	public static function save_per_page( $save, $option, $value ) {
688
        if ( $option == 'formidable_page_formidable_per_page' ) {
689
            $save = (int) $value;
690
        }
691
        return $save;
692
    }
693
694
	private static function get_edit_vars( $id, $errors = array(), $message = '', $create_link = false ) {
695
        global $frm_vars;
696
697
        $form = FrmForm::getOne( $id );
698
        if ( ! $form ) {
699
            wp_die( esc_html__( 'You are trying to edit a form that does not exist.', 'formidable' ) );
700
        }
701
702
        if ( $form->parent_form_id ) {
703
			wp_die( sprintf( esc_html__( '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>' ) );
704
        }
705
706
		$frm_field_selection = FrmField::field_selection();
707
		$fields = FrmField::get_all_for_form( $form->id );
708
709
        // Automatically add end section fields if they don't exist (2.0 migration)
710
        $reset_fields = false;
711
        FrmFormsHelper::auto_add_end_section_fields( $form, $fields, $reset_fields );
712
713
        if ( $reset_fields ) {
714
            $fields = FrmField::get_all_for_form( $form->id, '', 'exclude' );
715
        }
716
717
		unset( $end_section_values, $last_order, $open, $reset_fields );
718
719
		$args = array( 'parent_form_id' => $form->id );
720
		$values = FrmAppHelper::setup_edit_vars( $form, 'forms', '', true, array(), $args );
721
		$values['fields'] = $fields;
722
723
        $edit_message = __( 'Form was Successfully Updated', 'formidable' );
724
        if ( $form->is_template && $message == $edit_message ) {
725
            $message = __( 'Template was Successfully Updated', 'formidable' );
726
        }
727
728
		$all_templates = FrmForm::getAll( array( 'is_template' => 1 ), 'name' );
729
730
        if ( $form->default_template ) {
731
			wp_die( esc_html__( 'That template cannot be edited', 'formidable' ) );
732
		} elseif ( defined( 'DOING_AJAX' ) ) {
733
            wp_die();
734
        } else if ( $create_link ) {
735
			require( FrmAppHelper::plugin_path() . '/classes/views/frm-forms/new.php' );
736
        } else {
737
			require( FrmAppHelper::plugin_path() . '/classes/views/frm-forms/edit.php' );
738
        }
739
    }
740
741
	public static function get_settings_vars( $id, $errors = array(), $message = '' ) {
742
		FrmAppHelper::permission_check( 'frm_edit_forms' );
743
744
        global $frm_vars;
745
746
        $form = FrmForm::getOne( $id );
747
748
		$fields = FrmField::get_all_for_form( $id );
749
		$values = FrmAppHelper::setup_edit_vars( $form, 'forms', $fields, true );
750
751
		if ( isset( $values['default_template'] ) && $values['default_template'] ) {
752
			wp_die( esc_html__( 'That template cannot be edited', 'formidable' ) );
753
		}
754
755
		self::clean_submit_html( $values );
756
757
        $action_controls = FrmFormActionsController::get_form_actions();
758
759
		$sections = apply_filters( 'frm_add_form_settings_section', array(), $values );
760
        $pro_feature = FrmAppHelper::pro_is_installed() ? '' : ' class="pro_feature"';
761
762
		$styles = apply_filters( 'frm_get_style_opts', array() );
763
764
		$first_h3 = 'frm_first_h3';
765
766
		require( FrmAppHelper::plugin_path() . '/classes/views/frm-forms/settings.php' );
767
    }
768
769
	/**
770
	 * Replace old Submit Button href with new href to avoid errors in Chrome
771
	 *
772
	 * @since 2.03.08
773
	 *
774
	 * @param array|boolean $values
775
	 */
776
	private static function clean_submit_html( &$values ) {
777
		if ( is_array( $values ) && isset( $values['submit_html'] ) ) {
778
			$values['submit_html'] = str_replace( 'javascript:void(0)', '#', $values['submit_html'] );
779
		}
780
	}
781
782
    public static function mb_tags_box( $form_id, $class = '' ) {
783
		$fields = FrmField::get_all_for_form( $form_id, '', 'include' );
784
        $linked_forms = array();
785
        $col = 'one';
786
		$settings_tab = FrmAppHelper::is_admin_page( 'formidable' ) ? true : false;
787
788
		$cond_shortcodes = apply_filters( 'frm_conditional_shortcodes', array() );
789
		$entry_shortcodes = self::get_shortcode_helpers( $settings_tab );
790
791
		$advanced_helpers = self::advanced_helpers( compact( 'fields', 'form_id' ) );
792
793
		include( FrmAppHelper::plugin_path() . '/classes/views/shared/mb_adv_info.php' );
794
    }
795
796
	/**
797
	 * @since 3.04.01
798
	 */
799
	private static function advanced_helpers( $atts ) {
800
		$advanced_helpers = array(
801
			'default' => array(
802
				'heading' => __( 'Customize the field values with the following parameters. Click to see a sample.', 'formidable' ),
803
				'codes'   => self::get_advanced_shortcodes(),
804
			),
805
		);
806
807
		$user_fields = self::user_shortcodes();
808
		if ( ! empty( $user_fields ) ) {
809
			$user_helpers = array();
810
			foreach ( $user_fields as $uk => $uf ) {
811
				$user_helpers[ '|user_id| show="' . $uk . '"' ] = $uf;
812
				unset( $uk, $uf );
813
			}
814
815
			$advanced_helpers['user_id'] = array(
816
				'heading' => __( 'Insert user information', 'formidable' ),
817
				'codes'   => $user_helpers,
818
			);
819
		}
820
821
		/**
822
		 * Add extra helper shortcodes on the Advanced tab in form settings and views
823
		 * @since 3.04.01
824
		 * @param array $atts - Includes fields and form_id
825
		 */
826
		return apply_filters( 'frm_advanced_helpers', $advanced_helpers, $atts );
827
	}
828
829
	/**
830
	 * Get an array of the options to display in the advanced tab
831
	 * of the customization panel
832
	 * @since 2.0.6
833
	 */
834
	private static function get_advanced_shortcodes() {
835
		$adv_shortcodes = array(
836
			'x sep=", "'           => array(
837
				'label' => __( 'Separator', 'formidable' ),
838
				'title' => __( 'Use a different separator for checkbox fields', 'formidable' ),
839
			),
840
			'x format="d-m-Y"'     => __( 'Date Format', 'formidable' ),
841
			'x show="field_label"' => __( 'Field Label', 'formidable' ),
842
			'x wpautop=0'          => array(
843
				'label' => __( 'No Auto P', 'formidable' ),
844
				'title' => __( 'Do not automatically add any paragraphs or line breaks', 'formidable' ),
845
			),
846
		);
847
		$adv_shortcodes = apply_filters( 'frm_advanced_shortcodes', $adv_shortcodes );
848
		// __( 'Leave blank instead of defaulting to User Login', 'formidable' ) : blank=1
849
850
		return $adv_shortcodes;
851
	}
852
853
	/**
854
	 * @since 3.04.01
855
	 */
856
	private static function user_shortcodes() {
857
		$options = array(
858
			'ID'            => __( 'User ID', 'formidable' ),
859
			'first_name'    => __( 'First Name', 'formidable' ),
860
			'last_name'     => __( 'Last Name', 'formidable' ),
861
			'display_name'  => __( 'Display Name', 'formidable' ),
862
			'user_login'    => __( 'User Login', 'formidable' ),
863
			'user_email'    => __( 'Email', 'formidable' ),
864
			'avatar'        => __( 'Avatar', 'formidable' ),
865
			'author_link'   => __( 'Author Link', 'formidable' ),
866
		);
867
		return apply_filters( 'frm_user_shortcodes', $options );
868
	}
869
870
	/**
871
	 * Get an array of the helper shortcodes to display in the customization panel
872
	 * @since 2.0.6
873
	 */
874
	private static function get_shortcode_helpers( $settings_tab ) {
875
		$entry_shortcodes = array(
876
			'id'        => __( 'Entry ID', 'formidable' ),
877
			'key'       => __( 'Entry Key', 'formidable' ),
878
			'post_id'   => __( 'Post ID', 'formidable' ),
879
			'ip'        => __( 'User IP', 'formidable' ),
880
			'created-at' => __( 'Entry created', 'formidable' ),
881
			'updated-at' => __( 'Entry updated', 'formidable' ),
882
			''          => '',
883
			'siteurl'   => __( 'Site URL', 'formidable' ),
884
			'sitename'  => __( 'Site Name', 'formidable' ),
885
        );
886
887
		if ( ! FrmAppHelper::pro_is_installed() ) {
888
			unset( $entry_shortcodes['post_id'] );
889
		}
890
891
		if ( $settings_tab ) {
892
			$entry_shortcodes['default-message'] = __( 'Default Msg', 'formidable' );
893
			$entry_shortcodes['default-html'] = __( 'Default HTML', 'formidable' );
894
			$entry_shortcodes['default-plain'] = __( 'Default Plain', 'formidable' );
895
		}
896
897
		/**
898
		 * Use this hook to add or remove buttons in the helpers section
899
		 * in the customization panel
900
		 * @since 2.0.6
901
		 */
902
		$entry_shortcodes = apply_filters( 'frm_helper_shortcodes', $entry_shortcodes, $settings_tab );
903
904
		return $entry_shortcodes;
905
	}
906
907
	/**
908
	 * Insert the form class setting into the form
909
	 */
910
	public static function form_classes( $form ) {
911
		if ( isset( $form->options['form_class'] ) ) {
912
			echo esc_attr( sanitize_text_field( $form->options['form_class'] ) );
913
		}
914
915
		if ( isset( $form->options['js_validate'] ) && $form->options['js_validate'] ) {
916
			echo ' frm_js_validate ';
917
		}
918
	}
919
920
	public static function get_email_html() {
921
		FrmAppHelper::permission_check( 'frm_view_forms' );
922
		check_ajax_referer( 'frm_ajax', 'nonce' );
923
924
		echo FrmEntriesController::show_entry_shortcode( // WPCS: XSS ok.
925
			array(
926
				'form_id'       => FrmAppHelper::get_post_param( 'form_id', '', 'absint' ),
927
				'default_email' => true,
928
				'plain_text'    => FrmAppHelper::get_post_param( 'plain_text', '', 'absint' ),
929
			)
930
		);
931
		wp_die();
932
	}
933
934
    public static function filter_content( $content, $form, $entry = false ) {
935
		self::get_entry_by_param( $entry );
936
        if ( ! $entry ) {
937
            return $content;
938
        }
939
940
        if ( is_object( $form ) ) {
941
            $form = $form->id;
942
        }
943
944
        $shortcodes = FrmFieldsHelper::get_shortcodes( $content, $form );
945
        $content = apply_filters( 'frm_replace_content_shortcodes', $content, $entry, $shortcodes );
946
947
        return $content;
948
    }
949
950
	private static function get_entry_by_param( &$entry ) {
951
		if ( ! $entry || ! is_object( $entry ) ) {
952
			if ( ! $entry || ! is_numeric( $entry ) ) {
953
				$entry = FrmAppHelper::get_post_param( 'id', false, 'sanitize_title' );
954
			}
955
956
			FrmEntry::maybe_get_entry( $entry );
957
		}
958
	}
959
960
    public static function replace_content_shortcodes( $content, $entry, $shortcodes ) {
961
        return FrmFieldsHelper::replace_content_shortcodes( $content, $entry, $shortcodes );
962
    }
963
964
    public static function process_bulk_form_actions( $errors ) {
965
        if ( ! $_REQUEST ) {
966
            return $errors;
967
        }
968
969
		$bulkaction = FrmAppHelper::get_param( 'action', '', 'get', 'sanitize_text_field' );
970
        if ( $bulkaction == -1 ) {
971
			$bulkaction = FrmAppHelper::get_param( 'action2', '', 'get', 'sanitize_title' );
972
        }
973
974
        if ( ! empty( $bulkaction ) && strpos( $bulkaction, 'bulk_' ) === 0 ) {
975
            FrmAppHelper::remove_get_action();
976
977
            $bulkaction = str_replace( 'bulk_', '', $bulkaction );
978
        }
979
980
		$ids = FrmAppHelper::get_param( 'item-action', '', 'get', 'sanitize_text_field' );
981
        if ( empty( $ids ) ) {
982
            $errors[] = __( 'No forms were specified', 'formidable' );
983
            return $errors;
984
        }
985
986
        $permission_error = FrmAppHelper::permission_nonce_error( '', '_wpnonce', 'bulk-toplevel_page_formidable' );
987
        if ( $permission_error !== false ) {
988
            $errors[] = $permission_error;
989
            return $errors;
990
        }
991
992
        if ( ! is_array( $ids ) ) {
993
            $ids = explode( ',', $ids );
994
        }
995
996
        switch ( $bulkaction ) {
997
            case 'delete':
998
                $message = self::bulk_destroy( $ids );
999
				break;
1000
            case 'trash':
1001
                $message = self::bulk_trash( $ids );
1002
				break;
1003
            case 'untrash':
1004
                $message = self::bulk_untrash( $ids );
1005
        }
1006
1007
        if ( isset( $message ) && ! empty( $message ) ) {
1008
			echo '<div id="message" class="frm_updated_message">' . FrmAppHelper::kses( $message, array( 'a' ) ) . '</div>'; // WPCS: XSS ok.
1009
        }
1010
1011
        return $errors;
1012
    }
1013
1014
    public static function route() {
1015
		$action = isset( $_REQUEST['frm_action'] ) ? 'frm_action' : 'action';
1016
        $vars = array();
1017
		if ( isset( $_POST['frm_compact_fields'] ) ) {
1018
			FrmAppHelper::permission_check( 'frm_edit_forms' );
1019
1020
			$json_vars = htmlspecialchars_decode( nl2br( stripslashes( str_replace( '&quot;', '\\\"', $_POST['frm_compact_fields'] ) ) ) );
1021
			$json_vars = json_decode( $json_vars, true );
1022
			if ( empty( $json_vars ) ) {
1023
                // json decoding failed so we should return an error message
1024
				$action = FrmAppHelper::get_param( $action, '', 'get', 'sanitize_title' );
1025
                if ( 'edit' == $action ) {
1026
                    $action = 'update';
1027
                }
1028
1029
				add_filter( 'frm_validate_form', 'FrmFormsController::json_error' );
1030
            } else {
1031
				$vars = FrmAppHelper::json_to_array( $json_vars );
1032
                $action = $vars[ $action ];
1033
				unset( $_REQUEST['frm_compact_fields'], $_POST['frm_compact_fields'] );
1034
				$_REQUEST = array_merge( $_REQUEST, $vars );
1035
				$_POST = array_merge( $_POST, $_REQUEST );
1036
            }
1037
        } else {
1038
			$action = FrmAppHelper::get_param( $action, '', 'get', 'sanitize_title' );
1039
    		if ( isset( $_REQUEST['delete_all'] ) ) {
1040
                // override the action for this page
1041
    			$action = 'delete_all';
1042
            }
1043
        }
1044
1045
		add_action( 'frm_load_form_hooks', 'FrmHooksController::trigger_load_form_hooks' );
1046
        FrmAppHelper::trigger_hook_load( 'form' );
1047
1048
        switch ( $action ) {
1049
            case 'new':
1050
				return self::new_form( $vars );
1051
            case 'create':
1052
            case 'edit':
1053
            case 'update':
1054
            case 'duplicate':
1055
            case 'trash':
1056
            case 'untrash':
1057
            case 'destroy':
1058
            case 'delete_all':
1059
            case 'settings':
1060
            case 'update_settings':
1061
				return self::$action( $vars );
1062
            default:
1063
				do_action( 'frm_form_action_' . $action );
1064
				if ( apply_filters( 'frm_form_stop_action_' . $action, false ) ) {
1065
                    return;
1066
                }
1067
1068
				$action = FrmAppHelper::get_param( 'action', '', 'get', 'sanitize_text_field' );
1069
                if ( $action == -1 ) {
1070
					$action = FrmAppHelper::get_param( 'action2', '', 'get', 'sanitize_title' );
1071
                }
1072
1073
				if ( strpos( $action, 'bulk_' ) === 0 ) {
1074
                    FrmAppHelper::remove_get_action();
1075
                    return self::list_form();
1076
                }
1077
1078
                return self::display_forms_list();
1079
        }
1080
    }
1081
1082
    public static function json_error( $errors ) {
1083
        $errors['json'] = __( 'Abnormal HTML characters prevented your form from saving correctly', 'formidable' );
1084
        return $errors;
1085
    }
1086
1087
1088
    /* FRONT-END FORMS */
1089
    public static function admin_bar_css() {
1090
		if ( is_admin() || ! current_user_can( 'frm_edit_forms' ) ) {
1091
            return;
1092
        }
1093
1094
		add_action( 'wp_before_admin_bar_render', 'FrmFormsController::admin_bar_configure' );
1095
		FrmAppHelper::load_font_style();
1096
	}
1097
1098
	public static function admin_bar_configure() {
1099
        global $frm_vars;
1100
		if ( empty( $frm_vars['forms_loaded'] ) ) {
1101
            return;
1102
        }
1103
1104
        $actions = array();
1105
		foreach ( $frm_vars['forms_loaded'] as $form ) {
1106
			if ( is_object( $form ) ) {
1107
				$actions[ $form->id ] = $form->name;
1108
			}
1109
			unset( $form );
1110
		}
1111
1112
		if ( empty( $actions ) ) {
1113
			return;
1114
		}
1115
1116
		self::add_menu_to_admin_bar();
1117
		self::add_forms_to_admin_bar( $actions );
1118
	}
1119
1120
	/**
1121
	 * @since 2.05.07
1122
	 */
1123
	public static function add_menu_to_admin_bar() {
1124
		global $wp_admin_bar;
1125
1126
		$wp_admin_bar->add_node(
1127
			array(
1128
				'id'    => 'frm-forms',
1129
				'title' => '<span class="ab-icon"></span><span class="ab-label">' . FrmAppHelper::get_menu_name() . '</span>',
1130
				'href'  => admin_url( 'admin.php?page=formidable' ),
1131
				'meta'  => array(
1132
					'title' => FrmAppHelper::get_menu_name(),
1133
				),
1134
			)
1135
		);
1136
	}
1137
1138
	/**
1139
	 * @since 2.05.07
1140
	 */
1141
	private static function add_forms_to_admin_bar( $actions ) {
1142
		global $wp_admin_bar;
1143
1144
		asort( $actions );
1145
1146
		foreach ( $actions as $form_id => $name ) {
1147
1148
			$wp_admin_bar->add_node(
1149
				array(
1150
					'parent'    => 'frm-forms',
1151
					'id'        => 'edit_form_' . $form_id,
1152
					'title'     => empty( $name ) ? __( '(no title)' ) : $name,
1153
					'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...
1154
				)
1155
			);
1156
		}
1157
	}
1158
1159
    //formidable shortcode
1160
	public static function get_form_shortcode( $atts ) {
1161
        global $frm_vars;
1162
		if ( isset( $frm_vars['skip_shortcode'] ) && $frm_vars['skip_shortcode'] ) {
1163
            $sc = '[formidable';
1164
			if ( ! empty( $atts ) ) {
1165
				foreach ( $atts as $k => $v ) {
1166
					$sc .= ' ' . $k . '="' . esc_attr( $v ) . '"';
1167
				}
1168
			}
1169
			return $sc . ']';
1170
        }
1171
1172
		$shortcode_atts = shortcode_atts(
1173
			array(
1174
				'id'          => '',
1175
				'key'         => '',
1176
				'title'       => false,
1177
				'description' => false,
1178
				'readonly'    => false,
1179
				'entry_id'    => false,
1180
				'fields'      => array(),
1181
				'exclude_fields' => array(),
1182
				'minimize'    => false,
1183
			),
1184
			$atts
1185
		);
1186
		do_action( 'formidable_shortcode_atts', $shortcode_atts, $atts );
1187
1188
        return self::show_form( $shortcode_atts['id'], $shortcode_atts['key'], $shortcode_atts['title'], $shortcode_atts['description'], $atts );
1189
    }
1190
1191
    public static function show_form( $id = '', $key = '', $title = false, $description = false, $atts = array() ) {
1192
        if ( empty( $id ) ) {
1193
            $id = $key;
1194
        }
1195
1196
        $form = self::maybe_get_form_to_show( $id );
1197
        if ( ! $form ) {
1198
            return __( 'Please select a valid form', 'formidable' );
1199
        }
1200
1201
		FrmAppController::maybe_update_styles();
1202
1203
		add_action( 'frm_load_form_hooks', 'FrmHooksController::trigger_load_form_hooks' );
1204
        FrmAppHelper::trigger_hook_load( 'form', $form );
1205
1206
        $form = apply_filters( 'frm_pre_display_form', $form );
1207
1208
        $frm_settings = FrmAppHelper::get_settings();
1209
1210
		if ( self::is_viewable_draft_form( $form ) ) {
1211
			// don't show a draft form on a page
1212
			$form = __( 'Please select a valid form', 'formidable' );
1213
		} else if ( self::user_should_login( $form ) ) {
1214
			$form = do_shortcode( $frm_settings->login_msg );
1215
		} else if ( self::user_has_permission_to_view( $form ) ) {
1216
			$form = do_shortcode( $frm_settings->login_msg );
1217
		} else {
1218
			do_action( 'frm_pre_get_form', $form );
1219
			$form = self::get_form( $form, $title, $description, $atts );
1220
1221
			/**
1222
			 * Use this shortcode to check for external shortcodes that may span
1223
			 * across multiple fields in the customizable HTML
1224
			 * @since 2.0.8
1225
			 */
1226
			$form = apply_filters( 'frm_filter_final_form', $form );
1227
		}
1228
1229
		return $form;
1230
    }
1231
1232
	private static function maybe_get_form_to_show( $id ) {
1233
		$form = false;
1234
1235
		if ( ! empty( $id ) ) { // no form id or key set
1236
			$form = FrmForm::getOne( $id );
1237
			if ( ! $form || $form->parent_form_id || $form->status == 'trash' ) {
1238
				$form = false;
1239
			}
1240
		}
1241
1242
		return $form;
1243
	}
1244
1245
	private static function is_viewable_draft_form( $form ) {
1246
		global $post;
1247
		$frm_settings = FrmAppHelper::get_settings();
0 ignored issues
show
Unused Code introduced by
$frm_settings is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
1248
		return $form->status == 'draft' && current_user_can( 'frm_edit_forms' ) && ! FrmAppHelper::is_preview_page();
1249
	}
1250
1251
	private static function user_should_login( $form ) {
1252
		return $form->logged_in && ! is_user_logged_in();
1253
	}
1254
1255
	private static function user_has_permission_to_view( $form ) {
1256
		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'] );
1257
	}
1258
1259
    public static function get_form( $form, $title, $description, $atts = array() ) {
1260
		ob_start();
1261
1262
		do_action( 'frm_before_get_form', $atts );
1263
1264
        self::get_form_contents( $form, $title, $description, $atts );
1265
		self::enqueue_scripts( FrmForm::get_params( $form ) );
1266
1267
        $contents = ob_get_contents();
1268
        ob_end_clean();
1269
1270
		self::maybe_minimize_form( $atts, $contents );
1271
1272
        return $contents;
1273
    }
1274
1275
	public static function enqueue_scripts( $params ) {
1276
		do_action( 'frm_enqueue_form_scripts', $params );
1277
	}
1278
1279
	public static function get_form_contents( $form, $title, $description, $atts ) {
1280
		$params = FrmForm::get_params( $form );
1281
		$errors = self::get_saved_errors( $form, $params );
1282
		$fields = FrmFieldsHelper::get_form_fields( $form->id, $errors );
1283
		$reset = false;
1284
		$pass_args = compact( 'form', 'fields', 'errors', 'title', 'description', 'reset' );
1285
1286
		$handle_process_here = $params['action'] == 'create' && $params['posted_form_id'] == $form->id && $_POST;
1287
1288
		if ( ! $handle_process_here ) {
1289
			do_action( 'frm_display_form_action', $params, $fields, $form, $title, $description );
1290
			if ( apply_filters( 'frm_continue_to_new', true, $form->id, $params['action'] ) ) {
1291
				self::show_form_after_submit( $pass_args );
1292
			}
1293
		} elseif ( ! empty( $errors ) ) {
1294
			self::show_form_after_submit( $pass_args );
1295
1296
		} else {
1297
1298
			do_action( 'frm_validate_form_creation', $params, $fields, $form, $title, $description );
1299
1300
			if ( apply_filters( 'frm_continue_to_create', true, $form->id ) ) {
1301
				$entry_id = self::just_created_entry( $form->id );
1302
				$pass_args['entry_id'] = $entry_id;
1303
				$pass_args['reset'] = true;
1304
				$pass_args['conf_method'] = self::get_confirmation_method( compact( 'form', 'entry_id' ) );
1305
1306
				self::run_success_action( $pass_args );
1307
1308
				do_action(
1309
					'frm_after_entry_processed',
1310
					array(
1311
						'entry_id' => $entry_id,
1312
						'form'     => $form,
1313
					)
1314
				);
1315
			}
1316
		}
1317
	}
1318
1319
	/**
1320
	 * If the form was processed earlier (init), get the generated errors
1321
	 * @since 2.05
1322
	 */
1323
	private static function get_saved_errors( $form, $params ) {
1324
		global $frm_vars;
1325
1326
		if ( $params['posted_form_id'] == $form->id && $_POST && isset( $frm_vars['created_entries'][ $form->id ] ) ) {
1327
			$errors = $frm_vars['created_entries'][ $form->id ]['errors'];
1328
		} else {
1329
			$errors = array();
1330
		}
1331
		return $errors;
1332
	}
1333
1334
	/**
1335
	 * @since 2.2.7
1336
	 */
1337
	public static function just_created_entry( $form_id ) {
1338
		global $frm_vars;
1339
		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;
1340
	}
1341
1342
	/**
1343
	 * @since 3.0
1344
	 */
1345
	private static function get_confirmation_method( $atts ) {
1346
		$opt = 'success_action';
1347
		$method = ( isset( $atts['form']->options[ $opt ] ) && ! empty( $atts['form']->options[ $opt ] ) ) ? $atts['form']->options[ $opt ] : 'message';
1348
		$method = apply_filters( 'frm_success_filter', $method, $atts['form'], 'create' );
1349
1350
		if ( $method != 'message' && ( ! $atts['entry_id'] || ! is_numeric( $atts['entry_id'] ) ) ) {
1351
			$method = 'message';
1352
		}
1353
1354
		return $method;
1355
	}
1356
1357
	public static function maybe_trigger_redirect( $form, $params, $args ) {
1358
		if ( ! isset( $params['id'] ) ) {
1359
			global $frm_vars;
1360
			$params['id'] = $frm_vars['created_entries'][ $form->id ]['entry_id'];
1361
		}
1362
1363
		$conf_method = self::get_confirmation_method(
1364
			array(
1365
				'form'     => $form,
1366
				'entry_id' => $params['id'],
1367
			)
1368
		);
1369
1370
		if ( 'redirect' === $conf_method ) {
1371
			self::trigger_redirect( $form, $params, $args );
1372
		}
1373
	}
1374
1375
	public static function trigger_redirect( $form, $params, $args ) {
1376
		$success_args = array(
1377
			'action'      => $params['action'],
1378
			'conf_method' => 'redirect',
1379
			'form'        => $form,
1380
			'entry_id'    => $params['id'],
1381
		);
1382
1383
		if ( isset( $args['ajax'] ) ) {
1384
			$success_args['ajax'] = $args['ajax'];
1385
		}
1386
1387
		self::run_success_action( $success_args );
1388
	}
1389
1390
	/**
1391
	 * Used when the success action is not 'message'
1392
	 * @since 2.05
1393
	 */
1394
	public static function run_success_action( $args ) {
1395
		$extra_args = $args;
1396
		unset( $extra_args['form'] );
1397
1398
		do_action( 'frm_success_action', $args['conf_method'], $args['form'], $args['form']->options, $args['entry_id'], $extra_args );
1399
1400
		$opt = ( ! isset( $args['action'] ) || $args['action'] == 'create' ) ? 'success' : 'edit';
1401
		$args['success_opt'] = $opt;
1402
		if ( $args['conf_method'] == 'page' && is_numeric( $args['form']->options[ $opt . '_page_id' ] ) ) {
1403
			self::load_page_after_submit( $args );
1404
		} elseif ( $args['conf_method'] == 'redirect' ) {
1405
			self::redirect_after_submit( $args );
1406
		} else {
1407
			self::show_message_after_save( $args );
1408
		}
1409
	}
1410
1411
	/**
1412
	 * @since 3.0
1413
	 */
1414
	private static function load_page_after_submit( $args ) {
1415
		global $post;
1416
		$opt = $args['success_opt'];
1417
		if ( ! $post || $args['form']->options[ $opt . '_page_id' ] != $post->ID ) {
1418
			$page = get_post( $args['form']->options[ $opt . '_page_id' ] );
1419
			$old_post = $post;
1420
			$post = $page;
0 ignored issues
show
introduced by
Overridding WordPress globals is prohibited
Loading history...
1421
			$content = apply_filters( 'frm_content', $page->post_content, $args['form'], $args['entry_id'] );
1422
			echo apply_filters( 'the_content', $content ); // WPCS: XSS ok.
1423
			$post = $old_post;
0 ignored issues
show
introduced by
Overridding WordPress globals is prohibited
Loading history...
1424
		}
1425
	}
1426
1427
	/**
1428
	 * @since 3.0
1429
	 */
1430
	private static function redirect_after_submit( $args ) {
1431
		global $frm_vars;
1432
1433
		add_filter( 'frm_use_wpautop', '__return_false' );
1434
1435
		$opt = $args['success_opt'];
1436
		$success_url = trim( $args['form']->options[ $opt . '_url' ] );
1437
		$success_url = apply_filters( 'frm_content', $success_url, $args['form'], $args['entry_id'] );
1438
		$success_url = do_shortcode( $success_url );
1439
1440
		$success_msg = isset( $args['form']->options[ $opt . '_msg' ] ) ? $args['form']->options[ $opt . '_msg' ] : __( 'Please wait while you are redirected.', 'formidable' );
1441
1442
		$redirect_msg = self::get_redirect_message( $success_url, $success_msg, $args );
1443
1444
		$args['id'] = $args['entry_id'];
1445
		FrmEntriesController::delete_entry_before_redirect( $success_url, $args['form'], $args );
1446
1447
		add_filter( 'frm_redirect_url', 'FrmEntriesController::prepare_redirect_url' );
1448
		$success_url = apply_filters( 'frm_redirect_url', $success_url, $args['form'], $args );
1449
1450
		$doing_ajax = FrmAppHelper::doing_ajax();
1451
1452
		if ( isset( $args['ajax'] ) && $args['ajax'] && $doing_ajax ) {
1453
			echo json_encode( array( 'redirect' => $success_url ) );
1454
			wp_die();
1455
		} elseif ( ! headers_sent() ) {
1456
			wp_redirect( esc_url_raw( $success_url ) );
1457
			die(); // do not use wp_die or redirect fails
1458
		} else {
1459
			add_filter( 'frm_use_wpautop', '__return_true' );
1460
1461
			echo $redirect_msg; // WPCS: XSS ok.
1462
			echo "<script type='text/javascript'>window.onload = function(){setTimeout(window.location='" . esc_url_raw( $success_url ) . "', 8000);}</script>";
1463
		}
1464
	}
1465
1466
	/**
1467
	 * @since 3.0
1468
	 * @param string $success_url
1469
	 * @param string $success_msg
1470
	 * @param array $args
1471
	 */
1472
	private static function get_redirect_message( $success_url, $success_msg, $args ) {
1473
		$redirect_msg = '<div class="' . esc_attr( FrmFormsHelper::get_form_style_class( $args['form'] ) ) . '"><div class="frm-redirect-msg frm_message">' . $success_msg . '<br/>' .
1474
			sprintf( __( '%1$sClick here%2$s if you are not automatically redirected.', 'formidable' ), '<a href="' . esc_url( $success_url ) . '">', '</a>' ) .
1475
			'</div></div>';
1476
1477
		$redirect_args = array(
1478
			'entry_id' => $args['entry_id'],
1479
			'form_id'  => $args['form']->id,
1480
			'form'     => $args['form'],
1481
		);
1482
		return apply_filters( 'frm_redirect_msg', $redirect_msg, $redirect_args );
1483
	}
1484
1485
	/**
1486
	 * Prepare to show the success message and empty form after submit
1487
	 * @since 2.05
1488
	 */
1489
	public static function show_message_after_save( $atts ) {
1490
		$atts['message'] = self::prepare_submit_message( $atts['form'], $atts['entry_id'] );
1491
1492
		if ( ! isset( $atts['form']->options['show_form'] ) || $atts['form']->options['show_form'] ) {
1493
			self::show_form_after_submit( $atts );
1494
		} else {
1495
			self::show_lone_success_messsage( $atts );
1496
		}
1497
	}
1498
1499
	/**
1500
	 * Show an empty form
1501
	 * @since 2.05
1502
	 */
1503
	private static function show_form_after_submit( $args ) {
1504
		self::fill_atts_for_form_display( $args );
1505
1506
		$errors = $args['errors'];
1507
		$message = $args['message'];
1508
		$form = $args['form'];
1509
		$title = $args['title'];
1510
		$description = $args['description'];
1511
1512
		if ( empty( $args['fields'] ) ) {
1513
			$values = array();
1514
		} else {
1515
			$values = FrmEntriesHelper::setup_new_vars( $args['fields'], $form, $args['reset'] );
1516
		}
1517
		unset( $args );
1518
1519
		$include_form_tag = apply_filters( 'frm_include_form_tag', true, $form );
1520
1521
		$frm_settings = FrmAppHelper::get_settings();
1522
		$submit = isset( $form->options['submit_value'] ) ? $form->options['submit_value'] : $frm_settings->submit_value;
1523
1524
		include( FrmAppHelper::plugin_path() . '/classes/views/frm-entries/new.php' );
1525
	}
1526
1527
	/**
1528
	 * Get all the values needed on the new.php entry page
1529
	 * @since 2.05
1530
	 */
1531
	private static function fill_atts_for_form_display( &$args ) {
1532
		$defaults = array(
1533
			'errors'  => array(),
1534
			'message' => '',
1535
			'fields'  => array(),
1536
			'form'    => array(),
1537
			'title'   => true,
1538
			'description' => false,
1539
			'reset'   => false,
1540
		);
1541
		$args = wp_parse_args( $args, $defaults );
1542
	}
1543
1544
	/**
1545
	 * Show the success message without the form
1546
	 * @since 2.05
1547
	 */
1548
	private static function show_lone_success_messsage( $atts ) {
1549
		global $frm_vars;
1550
		$values = FrmEntriesHelper::setup_new_vars( $atts['fields'], $atts['form'], true );
1551
		self::maybe_load_css( $atts['form'], $values['custom_style'], $frm_vars['load_css'] );
1552
1553
		$include_extra_container = 'frm_forms' . FrmFormsHelper::get_form_style_class( $values );
1554
		$errors = array();
1555
		$form = $atts['form'];
1556
		$message = $atts['message'];
1557
1558
		include( FrmAppHelper::plugin_path() . '/classes/views/frm-entries/errors.php' );
1559
	}
1560
1561
	/**
1562
	 * Prepare the success message before it's shown
1563
	 * @since 2.05
1564
	 */
1565
	private static function prepare_submit_message( $form, $entry_id ) {
1566
		$frm_settings = FrmAppHelper::get_settings();
1567
1568
		if ( $entry_id && is_numeric( $entry_id ) ) {
1569
			$message = isset( $form->options['success_msg'] ) ? $form->options['success_msg'] : $frm_settings->success_msg;
1570
			$class = 'frm_message';
1571
		} else {
1572
			$message = $frm_settings->failed_msg;
1573
			$class = FrmFormsHelper::form_error_class();
1574
		}
1575
1576
		$message = FrmFormsHelper::get_success_message( compact( 'message', 'form', 'entry_id', 'class' ) );
1577
		return apply_filters( 'frm_main_feedback', $message, $form, $entry_id );
1578
	}
1579
1580
	public static function front_head() {
1581
		$version = FrmAppHelper::plugin_version();
1582
		$suffix = FrmAppHelper::js_suffix();
1583
1584
		if ( ! empty( $suffix ) && self::has_combo_js_file() ) {
1585
			wp_register_script( 'formidable', FrmAppHelper::plugin_url() . '/js/frm.min.js', array( 'jquery' ), $version, true );
1586
		} else {
1587
			wp_register_script( 'formidable', FrmAppHelper::plugin_url() . "/js/formidable{$suffix}.js", array( 'jquery' ), $version, true );
1588
		}
1589
1590
		add_filter( 'script_loader_tag', 'FrmFormsController::defer_script_loading', 10, 2 );
1591
1592
		if ( FrmAppHelper::is_admin() ) {
1593
			// don't load this in back-end
1594
			return;
1595
		}
1596
1597
		FrmAppHelper::localize_script( 'front' );
1598
		FrmStylesController::enqueue_css( 'register' );
1599
	}
1600
1601
	/**
1602
	 * @since 3.0
1603
	 */
1604
	public static function has_combo_js_file() {
1605
		return is_readable( FrmAppHelper::plugin_path() . '/js/frm.min.js' );
1606
	}
1607
1608
	public static function maybe_load_css( $form, $this_load, $global_load ) {
1609
		$load_css = FrmForm::is_form_loaded( $form, $this_load, $global_load );
1610
1611
		if ( $load_css ) {
1612
			global $frm_vars;
1613
			self::footer_js( 'header' );
1614
			$frm_vars['css_loaded'] = true;
1615
		}
1616
	}
1617
1618
	public static function defer_script_loading( $tag, $handle ) {
1619
	    if ( 'recaptcha-api' == $handle && ! strpos( $tag, 'defer' ) ) {
1620
	        $tag = str_replace( ' src', ' defer="defer" async="async" src', $tag );
1621
		}
1622
	    return $tag;
1623
	}
1624
1625
	public static function footer_js( $location = 'footer' ) {
1626
		global $frm_vars;
1627
1628
		FrmStylesController::enqueue_css();
1629
1630
		if ( ! FrmAppHelper::is_admin() && $location != 'header' && ! empty( $frm_vars['forms_loaded'] ) ) {
1631
			//load formidable js
1632
			wp_enqueue_script( 'formidable' );
1633
		}
1634
	}
1635
1636
	/**
1637
	 * @since 2.0.8
1638
	 */
1639
	private static function maybe_minimize_form( $atts, &$content ) {
1640
		// check if minimizing is turned on
1641
		if ( self::is_minification_on( $atts ) ) {
1642
			$content = str_replace( array( "\r\n", "\r", "\n", "\t", '    ' ), '', $content );
1643
		}
1644
	}
1645
1646
	/**
1647
	 * @since 2.0.8
1648
	 * @return boolean
1649
	 */
1650
	private static function is_minification_on( $atts ) {
1651
		return isset( $atts['minimize'] ) && ! empty( $atts['minimize'] );
1652
	}
1653
1654
	/**
1655
	 * @deprecated 1.07.05
1656
	 * @codeCoverageIgnore
1657
	 */
1658
	public static function add_default_templates( $path, $default = true, $template = true ) {
1659
		FrmDeprecated::add_default_templates( $path, $default, $template );
1 ignored issue
show
Deprecated Code introduced by
The method FrmDeprecated::add_default_templates() has been deprecated with message: 1.07.05

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
1660
	}
1661
1662
	/**
1663
	 * @deprecated 3.0
1664
	 * @codeCoverageIgnore
1665
	 */
1666
	public static function bulk_create_template( $ids ) {
1667
		return FrmDeprecated::bulk_create_template( $ids );
1 ignored issue
show
Deprecated Code introduced by
The method FrmDeprecated::bulk_create_template() has been deprecated with message: 3.0

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
1668
	}
1669
1670
	/**
1671
	 * @deprecated 2.03
1672
	 * @codeCoverageIgnore
1673
	 */
1674
	public static function register_pro_scripts() {
1675
		FrmDeprecated::register_pro_scripts();
1 ignored issue
show
Deprecated Code introduced by
The method FrmDeprecated::register_pro_scripts() has been deprecated with message: 2.03

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
1676
	}
1677
1678
	/**
1679
	 * @deprecated 3.0
1680
	 * @codeCoverageIgnore
1681
	 */
1682
	public static function edit_key() {
1683
		FrmDeprecated::edit_key();
1 ignored issue
show
Deprecated Code introduced by
The method FrmDeprecated::edit_key() has been deprecated with message: 3.0

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
1684
	}
1685
1686
	/**
1687
	 * @deprecated 3.0
1688
	 * @codeCoverageIgnore
1689
	 */
1690
	public static function edit_description() {
1691
		FrmDeprecated::edit_description();
1 ignored issue
show
Deprecated Code introduced by
The method FrmDeprecated::edit_description() has been deprecated with message: 3.0

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
1692
	}
1693
}
1694