Completed
Push — master ( 1466b8...74a3d9 )
by Stephanie
14s
created

FrmFormsController::add_new()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
class FrmFormsController {
4
5
    public static function menu() {
6
		$menu_label = __( 'Forms', 'formidable' );
7
		if ( ! FrmAppHelper::pro_is_installed() ) {
8
			$menu_label .= ' (Lite)';
9
		}
10
		add_submenu_page( 'formidable', 'Formidable | ' . $menu_label, $menu_label, 'frm_view_forms', 'formidable', 'FrmFormsController::route' );
11
12
		self::maybe_load_listing_hooks();
13
    }
14
15
	public static function maybe_load_listing_hooks() {
16
		$action = FrmAppHelper::simple_get( 'frm_action', 'sanitize_title' );
17
		if ( ! empty( $action ) && ! in_array( $action, array( 'list', 'trash', 'untrash', '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
	/**
62
	 * Choose which type of form to create
63
	 *
64
	 * @since 3.06
65
	 */
66
	public static function add_new() {
67
		require( FrmAppHelper::plugin_path() . '/classes/views/frm-forms/add-new.php' );
68
	}
69
70
	public static function new_form( $values = array() ) {
71
		FrmAppHelper::permission_check( 'frm_edit_forms' );
72
73
        global $frm_vars;
74
75
		$action = isset( $_REQUEST['frm_action'] ) ? 'frm_action' : 'action';
76
		$action = empty( $values ) ? FrmAppHelper::get_param( $action, '', 'get', 'sanitize_title' ) : $values[ $action ];
77
78
		if ( $action == 'create' ) {
79
			self::create( $values );
80
			return;
81
		} else if ( $action == 'new' ) {
82
			$frm_field_selection = FrmField::field_selection();
83
			$values = FrmFormsHelper::setup_new_vars( $values );
84
            $id = FrmForm::create( $values );
85
			$form = FrmForm::getOne( $id );
86
87
			self::create_default_email_action( $form );
88
89
			$all_templates = FrmForm::getAll( array( 'is_template' => 1 ), 'name' );
90
91
            $values['id'] = $id;
92
			require( FrmAppHelper::plugin_path() . '/classes/views/frm-forms/new.php' );
93
        }
94
    }
95
96
	/**
97
	 * Create the default email action
98
	 *
99
	 * @since 2.02.11
100
	 *
101
	 * @param object $form
102
	 */
103
    private static function create_default_email_action( $form ) {
104
    	$create_email = apply_filters( 'frm_create_default_email_action', true, $form );
105
106
	    if ( $create_email ) {
107
		    $action_control = FrmFormActionsController::get_form_actions( 'email' );
108
		    $action_control->create( $form->id );
109
	    }
110
    }
111
112
	public static function create( $values = array() ) {
113
		FrmAppHelper::permission_check( 'frm_edit_forms' );
114
115
        global $frm_vars;
116
        if ( empty( $values ) ) {
117
            $values = $_POST;
118
        }
119
120
        //Set radio button and checkbox meta equal to "other" value
121
        if ( FrmAppHelper::pro_is_installed() ) {
122
            $values = FrmProEntry::mod_other_vals( $values, 'back' );
123
        }
124
125
		$id = isset( $values['id'] ) ? absint( $values['id'] ) : FrmAppHelper::get_param( 'id', '', 'get', 'absint' );
126
127
        if ( ! current_user_can( 'frm_edit_forms' ) || ( $_POST && ( ! isset( $values['frm_save_form'] ) || ! wp_verify_nonce( $values['frm_save_form'], 'frm_save_form_nonce' ) ) ) ) {
128
            $frm_settings = FrmAppHelper::get_settings();
129
            $errors = array( 'form' => $frm_settings->admin_permission );
130
        } else {
131
			$errors = FrmForm::validate( $values );
132
        }
133
134
		if ( count( $errors ) > 0 ) {
135
            $hide_preview = true;
136
			$frm_field_selection = FrmField::field_selection();
137
            $form = FrmForm::getOne( $id );
138
			$fields = FrmField::get_all_for_form( $id );
139
140
			$values = FrmAppHelper::setup_edit_vars( $form, 'forms', '', true );
141
			$values['fields'] = $fields;
142
			$all_templates = FrmForm::getAll( array( 'is_template' => 1 ), 'name' );
143
144
			require( FrmAppHelper::plugin_path() . '/classes/views/frm-forms/new.php' );
145
        } else {
146
            FrmForm::update( $id, $values, true );
147
			$url = admin_url( 'admin.php?page=formidable&frm_action=settings&id=' . $id );
148
			die( FrmAppHelper::js_redirect( $url ) ); // WPCS: XSS ok.
149
        }
150
    }
151
152
    public static function edit( $values = false ) {
153
		FrmAppHelper::permission_check( 'frm_edit_forms' );
154
155
		$id = isset( $values['id'] ) ? absint( $values['id'] ) : FrmAppHelper::get_param( 'id', '', 'get', 'absint' );
156
		return self::get_edit_vars( $id );
157
    }
158
159
    public static function settings( $id = false, $message = '' ) {
160
		FrmAppHelper::permission_check( 'frm_edit_forms' );
161
162
		if ( ! $id || ! is_numeric( $id ) ) {
163
			$id = FrmAppHelper::get_param( 'id', '', 'get', 'absint' );
164
        }
165
		return self::get_settings_vars( $id, array(), $message );
166
    }
167
168
    public static function update_settings() {
169
		FrmAppHelper::permission_check( 'frm_edit_forms' );
170
171
		$id = FrmAppHelper::get_param( 'id', '', 'get', 'absint' );
172
173
		$errors = FrmForm::validate( $_POST );
174
		if ( count( $errors ) > 0 ) {
175
			return self::get_settings_vars( $id, $errors );
176
		}
177
178
		do_action( 'frm_before_update_form_settings', $id );
179
180
		FrmForm::update( $id, $_POST );
181
182
        $message = __( 'Settings Successfully Updated', 'formidable' );
183
		return self::get_settings_vars( $id, array(), $message );
184
    }
185
186
	public static function update( $values = array() ) {
187
		if ( empty( $values ) ) {
188
            $values = $_POST;
189
        }
190
191
        //Set radio button and checkbox meta equal to "other" value
192
        if ( FrmAppHelper::pro_is_installed() ) {
193
            $values = FrmProEntry::mod_other_vals( $values, 'back' );
194
        }
195
196
        $errors = FrmForm::validate( $values );
197
        $permission_error = FrmAppHelper::permission_nonce_error( 'frm_edit_forms', 'frm_save_form', 'frm_save_form_nonce' );
198
        if ( $permission_error !== false ) {
199
            $errors['form'] = $permission_error;
200
        }
201
202
		$id = isset( $values['id'] ) ? absint( $values['id'] ) : FrmAppHelper::get_param( 'id', '', 'get', 'absint' );
203
204
		if ( count( $errors ) > 0 ) {
205
            return self::get_edit_vars( $id, $errors );
206
		} else {
207
            FrmForm::update( $id, $values );
208
            $message = __( 'Form was Successfully Updated', 'formidable' );
209
            if ( defined( 'DOING_AJAX' ) ) {
210
				wp_die( esc_html( $message ) );
211
            }
212
			return self::get_edit_vars( $id, array(), $message );
213
        }
214
    }
215
216
	/**
217
	 * Redirect to the url for creating from a template
218
	 * Also delete the current form
219
	 *
220
	 * @since 2.0
221
	 */
222
	public static function _create_from_template() {
223
		FrmAppHelper::permission_check( 'frm_edit_forms' );
224
		check_ajax_referer( 'frm_ajax', 'nonce' );
225
226
		$current_form = FrmAppHelper::get_param( 'this_form', '', 'get', 'absint' );
227
		$template_id = FrmAppHelper::get_param( 'id', '', 'get', 'absint' );
228
229
		if ( $current_form ) {
230
			FrmForm::destroy( $current_form );
231
		}
232
233
		echo esc_url_raw( admin_url( 'admin.php?page=formidable&frm_action=duplicate&id=' . absint( $template_id ) ) );
234
		wp_die();
235
	}
236
237
    public static function duplicate() {
238
		FrmAppHelper::permission_check( 'frm_edit_forms' );
239
240
		$params = FrmForm::list_page_params();
241
        $form = FrmForm::duplicate( $params['id'], $params['template'], true );
242
        $message = $params['template'] ? __( 'Form template was Successfully Created', 'formidable' ) : __( 'Form was Successfully Copied', 'formidable' );
243
        if ( $form ) {
244
			return self::get_edit_vars( $form, array(), $message, true );
245
        } else {
246
			return self::display_forms_list( $params, __( 'There was a problem creating the new template.', 'formidable' ) );
247
        }
248
    }
249
250
    public static function page_preview() {
251
		$params = FrmForm::list_page_params();
252
        if ( ! $params['form'] ) {
253
            return;
254
        }
255
256
        $form = FrmForm::getOne( $params['form'] );
257
		if ( $form ) {
258
			return self::show_form( $form->id, '', true, true );
259
		}
260
    }
261
262
	/**
263
	 * @since 3.0
264
	 */
265
	public static function show_page_preview() {
266
		echo self::page_preview(); // WPCS: XSS ok.
267
	}
268
269
    public static function preview() {
270
        do_action( 'frm_wp' );
271
272
        global $frm_vars;
273
        $frm_vars['preview'] = true;
274
275
		self::load_wp();
276
277
		$include_theme = FrmAppHelper::get_param( 'theme', '', 'get', 'absint' );
278
		if ( $include_theme ) {
279
			self::set_preview_query();
280
			self::load_theme_preview();
281
		} else {
282
			self::load_direct_preview();
283
		}
284
285
		wp_die();
286
	}
287
288
	/**
289
	 * @since 3.0
290
	 */
291
	private static function load_wp() {
292
		if ( ! defined( 'ABSPATH' ) && ! defined( 'XMLRPC_REQUEST' ) ) {
293
			global $wp;
294
			$root = dirname( dirname( dirname( dirname( __FILE__ ) ) ) );
295
			include_once( $root . '/wp-config.php' );
296
			$wp->init();
297
			$wp->register_globals();
298
		}
299
	}
300
301
	private static function set_preview_query() {
302
		$random_page = get_posts(
303
			array(
304
				'numberposts' => 1,
305
				'orderby'     => 'date',
306
				'order'       => 'ASC',
307
				'post_type'   => 'page',
308
			)
309
		);
310
311
		if ( ! empty( $random_page ) ) {
312
			$random_page = reset( $random_page );
313
			query_posts(
0 ignored issues
show
Coding Style introduced by
The use of function query_posts() is discouraged; use WP_Query() instead
Loading history...
314
				array(
315
					'post_type' => 'page',
316
					'page_id'   => $random_page->ID,
317
				)
318
			);
319
		}
320
	}
321
322
	/**
323
	 * @since 3.0
324
	 */
325
	private static function load_theme_preview() {
326
		add_filter( 'wp_title', 'FrmFormsController::preview_title', 9999 );
327
		add_filter( 'the_title', 'FrmFormsController::preview_page_title', 9999 );
328
		add_filter( 'the_content', 'FrmFormsController::preview_content', 9999 );
329
		add_action( 'loop_no_results', 'FrmFormsController::show_page_preview' );
330
		add_filter( 'is_active_sidebar', '__return_false' );
331
		get_template_part( 'page' );
332
	}
333
334
335
	/**
336
	 * Set the page title for the theme preview page
337
	 *
338
	 * @since 3.0
339
	 */
340
	public static function preview_page_title( $title ) {
341
		if ( in_the_loop() ) {
342
			$title = self::preview_title( $title );
343
		}
344
		return $title;
345
	}
346
347
	/**
348
	 * Set the page title for the theme preview page
349
	 *
350
	 * @since 3.0
351
	 */
352
	public static function preview_title( $title ) {
353
		return __( 'Form Preview', 'formidable' );
354
	}
355
356
	/**
357
	 * Set the page content for the theme preview page
358
	 *
359
	 * @since 3.0
360
	 */
361
	public static function preview_content( $content ) {
362
		if ( in_the_loop() ) {
363
			$content = self::show_page_preview();
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $content is correct as self::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...
364
		}
365
		return $content;
366
	}
367
368
	/**
369
	 * @since 3.0
370
	 */
371
	private static function load_direct_preview() {
372
		header( 'Content-Type: text/html; charset=' . get_option( 'blog_charset' ) );
373
374
		$key = FrmAppHelper::simple_get( 'form', 'sanitize_title' );
375
		if ( $key == '' ) {
376
			$key = FrmAppHelper::get_post_param( 'form', '', 'sanitize_title' );
377
		}
378
379
		$form = FrmForm::getAll( array( 'form_key' => $key ), '', 1 );
380
		if ( empty( $form ) ) {
381
			$form = FrmForm::getAll( array(), '', 1 );
382
		}
383
384
		require( FrmAppHelper::plugin_path() . '/classes/views/frm-entries/direct.php' );
385
	}
386
387
    public static function untrash() {
388
		self::change_form_status( 'untrash' );
389
    }
390
391
	public static function bulk_untrash( $ids ) {
392
		FrmAppHelper::permission_check( 'frm_edit_forms' );
393
394
        $count = FrmForm::set_status( $ids, 'published' );
395
396
		$message = sprintf( _n( '%1$s form restored from the Trash.', '%1$s forms restored from the Trash.', $count, 'formidable' ), 1 );
397
        return $message;
398
    }
399
400
	/**
401
	 * @since 3.06
402
	 */
403
	public static function ajax_trash() {
404
		FrmAppHelper::permission_check( 'frm_delete_forms' );
405
		check_ajax_referer( 'frm_ajax', 'nonce' );
406
		$form_id = FrmAppHelper::get_param( 'id', '', 'post', 'absint' );
407
		FrmForm::set_status( $form_id, 'trash' );
408
		wp_die();
409
	}
410
411
    public static function trash() {
412
		self::change_form_status( 'trash' );
413
    }
414
415
	/**
416
	 * @param string $status
417
	 *
418
	 * @return int The number of forms changed
419
	 */
420
	public static function change_form_status( $status ) {
421
		$available_status = array(
422
			'untrash' => array(
423
				'permission' => 'frm_edit_forms',
424
				'new_status' => 'published',
425
			),
426
			'trash'   => array(
427
				'permission' => 'frm_delete_forms',
428
				'new_status' => 'trash',
429
			),
430
		);
431
432
		if ( ! isset( $available_status[ $status ] ) ) {
433
			return;
434
		}
435
436
		FrmAppHelper::permission_check( $available_status[ $status ]['permission'] );
437
438
		$params = FrmForm::list_page_params();
439
440
		//check nonce url
441
		check_admin_referer( $status . '_form_' . $params['id'] );
442
443
		$count = 0;
444
		if ( FrmForm::set_status( $params['id'], $available_status[ $status ]['new_status'] ) ) {
445
			$count++;
446
		}
447
448
		$form_type = FrmAppHelper::get_simple_request(
449
			array(
450
				'param' => 'form_type',
451
				'type' => 'request',
452
			)
453
		);
454
455
		$available_status['untrash']['message'] = sprintf( _n( '%1$s form restored from the Trash.', '%1$s forms restored from the Trash.', $count, 'formidable' ), $count );
456
		$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>' );
457
458
		$message = $available_status[ $status ]['message'];
459
460
		self::display_forms_list( $params, $message );
461
	}
462
463
	public static function bulk_trash( $ids ) {
464
		FrmAppHelper::permission_check( 'frm_delete_forms' );
465
466
        $count = 0;
467
        foreach ( $ids as $id ) {
468
            if ( FrmForm::trash( $id ) ) {
469
                $count++;
470
            }
471
        }
472
473
		$current_page = FrmAppHelper::get_simple_request(
474
			array(
475
				'param' => 'form_type',
476
				'type' => 'request',
477
			)
478
		);
479
		$message = sprintf(
480
			_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' ),
481
			$count,
482
			'<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' ) ) . '">',
483
			'</a>'
484
		);
485
486
        return $message;
487
    }
488
489
    public static function destroy() {
490
		FrmAppHelper::permission_check( 'frm_delete_forms' );
491
492
		$params = FrmForm::list_page_params();
493
494
        //check nonce url
495
		check_admin_referer( 'destroy_form_' . $params['id'] );
496
497
        $count = 0;
498
        if ( FrmForm::destroy( $params['id'] ) ) {
499
            $count++;
500
        }
501
502
		$message = sprintf( _n( '%1$s form permanently deleted.', '%1$s forms permanently deleted.', $count, 'formidable' ), $count );
503
504
		self::display_forms_list( $params, $message );
505
    }
506
507
	public static function bulk_destroy( $ids ) {
508
		FrmAppHelper::permission_check( 'frm_delete_forms' );
509
510
        $count = 0;
511
        foreach ( $ids as $id ) {
512
            $d = FrmForm::destroy( $id );
513
            if ( $d ) {
514
                $count++;
515
            }
516
        }
517
518
		$message = sprintf( _n( '%1$s form permanently deleted.', '%1$s forms permanently deleted.', $count, 'formidable' ), $count );
519
520
        return $message;
521
    }
522
523
    private static function delete_all() {
524
        //check nonce url
525
		$permission_error = FrmAppHelper::permission_nonce_error( 'frm_delete_forms', '_wpnonce', 'bulk-toplevel_page_formidable' );
526
        if ( $permission_error !== false ) {
527
			self::display_forms_list( array(), '', array( $permission_error ) );
528
            return;
529
        }
530
531
		$count = FrmForm::scheduled_delete( time() );
532
		$message = sprintf( _n( '%1$s form permanently deleted.', '%1$s forms permanently deleted.', $count, 'formidable' ), $count );
533
534
		self::display_forms_list( array(), $message );
535
    }
536
537
	/**
538
	 * Create a custom template from a form
539
	 *
540
	 * @since 3.06
541
	 */
542
	public static function build_template() {
543
		global $wpdb;
544
545
		FrmAppHelper::permission_check( 'frm_edit_forms' );
546
		check_ajax_referer( 'frm_ajax', 'nonce' );
547
548
		$form_id     = FrmAppHelper::get_param( 'xml', '', 'post', 'absint' );
549
		$new_form_id = FrmForm::duplicate( $form_id, 1, true );
550
		if ( empty( $new_form_id ) ) {
551
			$response = array(
552
				'message' => __( 'There was an error creating a template.', 'formidable' ),
553
			);
554
		} else {
555
			// Update the new form name and description.
556
			$name = FrmAppHelper::get_param( 'name', '', 'post', 'sanitize_text_field' );
557
			$desc = FrmAppHelper::get_param( 'desc', '', 'post', 'sanitize_textarea_field' );
558
559
			$new_values = array(
560
				'name' => $name,
561
				'description' => $desc,
562
			);
563
			$query_results = $wpdb->update( $wpdb->prefix . 'frm_forms', $new_values, array( 'id' => $new_form_id ) );
564
			if ( $query_results ) {
565
				FrmForm::clear_form_cache();
566
			}
567
568
			$response = array(
569
				'redirect' => admin_url( 'admin.php?page=formidable&frm_action=list_templates' ),
570
			);
571
		}
572
573
		echo wp_json_encode( $response );
574
		wp_die();
575
	}
576
577
	/**
578
	* Inserts Formidable button
579
	* Hook exists since 2.5.0
580
	*
581
	* @since 2.0.15
582
	*/
583
	public static function insert_form_button() {
584
		if ( current_user_can( 'frm_view_forms' ) ) {
585
			$menu_name = FrmAppHelper::get_menu_name();
586
			$icon = apply_filters( 'frm_media_icon', FrmAppHelper::svg_logo() );
587
			echo '<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' ) . '">' .
588
				FrmAppHelper::kses( $icon, 'all' ) .
589
				' ' . esc_html( $menu_name ) . '</a>'; // WPCS: XSS ok.
590
		}
591
	}
592
593
    public static function insert_form_popup() {
594
		$page = basename( FrmAppHelper::get_server_value( 'PHP_SELF' ) );
595
		if ( ! in_array( $page, array( 'post.php', 'page.php', 'page-new.php', 'post-new.php' ) ) ) {
596
            return;
597
        }
598
599
        FrmAppHelper::load_admin_wide_js();
600
601
        $shortcodes = array(
602
			'formidable' => array(
603
				'name'  => __( 'Form', 'formidable' ),
604
				'label' => __( 'Insert a Form', 'formidable' ),
605
			),
606
        );
607
608
		$shortcodes = apply_filters( 'frm_popup_shortcodes', $shortcodes );
609
610
		include( FrmAppHelper::plugin_path() . '/classes/views/frm-forms/insert_form_popup.php' );
611
    }
612
613
    public static function get_shortcode_opts() {
614
		FrmAppHelper::permission_check( 'frm_view_forms' );
615
        check_ajax_referer( 'frm_ajax', 'nonce' );
616
617
		$shortcode = FrmAppHelper::get_post_param( 'shortcode', '', 'sanitize_text_field' );
618
		if ( empty( $shortcode ) ) {
619
            wp_die();
620
        }
621
622
		echo '<div id="sc-opts-' . esc_attr( $shortcode ) . '" class="frm_shortcode_option">';
623
		echo '<input type="radio" name="frmsc" value="' . esc_attr( $shortcode ) . '" id="sc-' . esc_attr( $shortcode ) . '" class="frm_hidden" />';
624
625
        $form_id = '';
626
        $opts = array();
627
		switch ( $shortcode ) {
628
            case 'formidable':
629
                $opts = array(
630
					'form_id'       => 'id',
631
                    //'key' => ',
632
					'title'         => array(
633
						'val'   => 1,
634
						'label' => __( 'Display form title', 'formidable' ),
635
					),
636
					'description'   => array(
637
						'val'   => 1,
638
						'label' => __( 'Display form description', 'formidable' ),
639
					),
640
					'minimize'      => array(
641
						'val'   => 1,
642
						'label' => __( 'Minimize form HTML', 'formidable' ),
643
					),
644
                );
645
        }
646
		$opts = apply_filters( 'frm_sc_popup_opts', $opts, $shortcode );
647
648
		if ( isset( $opts['form_id'] ) && is_string( $opts['form_id'] ) ) {
649
			// allow other shortcodes to use the required form id option
650
			$form_id = $opts['form_id'];
651
			unset( $opts['form_id'] );
652
		}
653
654
		include( FrmAppHelper::plugin_path() . '/classes/views/frm-forms/shortcode_opts.php' );
655
656
        echo '</div>';
657
658
        wp_die();
659
    }
660
661
	public static function display_forms_list( $params = array(), $message = '', $errors = array() ) {
662
        FrmAppHelper::permission_check( 'frm_view_forms' );
663
664
        global $wpdb, $frm_vars;
665
666
		if ( empty( $params ) ) {
667
			$params = FrmForm::list_page_params();
668
        }
669
670
        $wp_list_table = new FrmFormsListHelper( compact( 'params' ) );
671
672
        $pagenum = $wp_list_table->get_pagenum();
673
674
        $wp_list_table->prepare_items();
675
676
        $total_pages = $wp_list_table->get_pagination_arg( 'total_pages' );
677
        if ( $pagenum > $total_pages && $total_pages > 0 ) {
678
			wp_redirect( esc_url_raw( add_query_arg( 'paged', $total_pages ) ) );
679
            die();
680
        }
681
682
		require( FrmAppHelper::plugin_path() . '/classes/views/frm-forms/list.php' );
683
    }
684
685
	public static function get_columns( $columns ) {
686
	    $columns['cb'] = '<input type="checkbox" />';
687
	    $columns['id'] = 'ID';
688
689
		$type = FrmAppHelper::get_simple_request(
690
			array(
691
				'param'   => 'form_type',
692
				'type'    => 'request',
693
				'default' => 'published',
694
			)
695
		);
696
697
        if ( 'template' == $type ) {
698
            $columns['name']        = __( 'Template Name', 'formidable' );
699
            $columns['type']        = __( 'Type', 'formidable' );
700
            $columns['form_key']    = __( 'Key', 'formidable' );
701
        } else {
702
            $columns['name']        = __( 'Form Title', 'formidable' );
703
            $columns['entries']     = __( 'Entries', 'formidable' );
704
            $columns['form_key']    = __( 'Key', 'formidable' );
705
            $columns['shortcode']   = __( 'Shortcodes', 'formidable' );
706
        }
707
708
        $columns['created_at'] = __( 'Date', 'formidable' );
709
710
		add_screen_option(
711
			'per_page',
712
			array(
713
				'label'   => __( 'Forms', 'formidable' ),
714
				'default' => 20,
715
				'option'  => 'formidable_page_formidable_per_page',
716
			)
717
		);
718
719
        return $columns;
720
	}
721
722
	public static function get_sortable_columns() {
723
		return array(
724
			'id'            => 'id',
725
			'name'          => 'name',
726
			'description'   => 'description',
727
			'form_key'      => 'form_key',
728
			'created_at'    => 'created_at',
729
		);
730
	}
731
732
	public static function hidden_columns( $hidden_columns ) {
733
		$type = FrmAppHelper::get_simple_request(
734
			array(
735
				'param' => 'form_type',
736
				'type'  => 'request',
737
			)
738
		);
739
740
		if ( $type === 'template' ) {
741
			$hidden_columns[] = 'id';
742
			$hidden_columns[] = 'form_key';
743
		}
744
745
		return $hidden_columns;
746
	}
747
748
	public static function save_per_page( $save, $option, $value ) {
749
        if ( $option == 'formidable_page_formidable_per_page' ) {
750
            $save = (int) $value;
751
        }
752
        return $save;
753
    }
754
755
	/**
756
	 * Show the template listing page
757
	 *
758
	 * @since 3.06
759
	 */
760
	private static function list_templates() {
761
		wp_enqueue_script( 'jquery-ui-dialog' );
762
		wp_enqueue_style( 'jquery-ui-dialog' );
763
764
		$where = apply_filters( 'frm_forms_dropdown', array(), '' );
765
		$forms = FrmForm::get_published_forms( $where );
766
767
		$api = new FrmFormTemplateApi();
768
		$templates = $api->get_api_info();
769
		self::add_user_templates( $templates );
770
771
		$pricing = FrmAppHelper::admin_upgrade_link( 'form-templates' );
772
		$plans = array( 'free', 'Personal', 'Business', 'Elite' );
773
774
		require( FrmAppHelper::plugin_path() . '/classes/views/frm-forms/list-templates.php' );
775
	}
776
777
	private static function add_user_templates( &$templates ) {
778
		$user_templates = array(
779
			'is_template'      => 1,
780
			'default_template' => 0,
781
		);
782
		$user_templates = FrmForm::getAll( $user_templates, 'name' );
783
		foreach ( $user_templates as $template ) {
784
			$template = array(
785
				'id'          => $template->id,
786
				'name'        => $template->name,
787
				'key'         => $template->form_key,
788
				'description' => $template->description,
789
				'url'         => admin_url( 'admin.php?page=formidable&frm_action=duplicate&id=' . absint( $template->id ) ),
790
				'released'    => $template->created_at,
791
				'installed'   => 1,
792
			);
793
			array_unshift( $templates, $template );
794
			unset( $template );
795
		}
796
	}
797
798
	private static function get_edit_vars( $id, $errors = array(), $message = '', $create_link = false ) {
799
        global $frm_vars;
800
801
        $form = FrmForm::getOne( $id );
802
        if ( ! $form ) {
803
            wp_die( esc_html__( 'You are trying to edit a form that does not exist.', 'formidable' ) );
804
        }
805
806
        if ( $form->parent_form_id ) {
807
			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>' ) );
808
        }
809
810
		$frm_field_selection = FrmField::field_selection();
811
		$fields = FrmField::get_all_for_form( $form->id );
812
813
        // Automatically add end section fields if they don't exist (2.0 migration)
814
        $reset_fields = false;
815
        FrmFormsHelper::auto_add_end_section_fields( $form, $fields, $reset_fields );
816
817
        if ( $reset_fields ) {
818
            $fields = FrmField::get_all_for_form( $form->id, '', 'exclude' );
819
        }
820
821
		unset( $end_section_values, $last_order, $open, $reset_fields );
822
823
		$args = array( 'parent_form_id' => $form->id );
824
		$values = FrmAppHelper::setup_edit_vars( $form, 'forms', '', true, array(), $args );
825
		$values['fields'] = $fields;
826
827
        $edit_message = __( 'Form was Successfully Updated', 'formidable' );
828
        if ( $form->is_template && $message == $edit_message ) {
829
            $message = __( 'Template was Successfully Updated', 'formidable' );
830
        }
831
832
		$all_templates = FrmForm::getAll( array( 'is_template' => 1 ), 'name' );
833
834
        if ( $form->default_template ) {
835
			wp_die( esc_html__( 'That template cannot be edited', 'formidable' ) );
836
		} elseif ( defined( 'DOING_AJAX' ) ) {
837
            wp_die();
838
        } else if ( $create_link ) {
839
			require( FrmAppHelper::plugin_path() . '/classes/views/frm-forms/new.php' );
840
        } else {
841
			require( FrmAppHelper::plugin_path() . '/classes/views/frm-forms/edit.php' );
842
        }
843
    }
844
845
	public static function get_settings_vars( $id, $errors = array(), $message = '' ) {
846
		FrmAppHelper::permission_check( 'frm_edit_forms' );
847
848
        global $frm_vars;
849
850
        $form = FrmForm::getOne( $id );
851
852
		$fields = FrmField::get_all_for_form( $id );
853
		$values = FrmAppHelper::setup_edit_vars( $form, 'forms', $fields, true );
854
855
		if ( isset( $values['default_template'] ) && $values['default_template'] ) {
856
			wp_die( esc_html__( 'That template cannot be edited', 'formidable' ) );
857
		}
858
859
		self::clean_submit_html( $values );
860
861
        $action_controls = FrmFormActionsController::get_form_actions();
862
863
		$sections = apply_filters( 'frm_add_form_settings_section', array(), $values );
864
        $pro_feature = FrmAppHelper::pro_is_installed() ? '' : ' class="pro_feature"';
865
866
		$styles = apply_filters( 'frm_get_style_opts', array() );
867
868
		$first_h3 = 'frm_first_h3';
869
870
		require( FrmAppHelper::plugin_path() . '/classes/views/frm-forms/settings.php' );
871
    }
872
873
	/**
874
	 * Replace old Submit Button href with new href to avoid errors in Chrome
875
	 *
876
	 * @since 2.03.08
877
	 *
878
	 * @param array|boolean $values
879
	 */
880
	private static function clean_submit_html( &$values ) {
881
		if ( is_array( $values ) && isset( $values['submit_html'] ) ) {
882
			$values['submit_html'] = str_replace( 'javascript:void(0)', '#', $values['submit_html'] );
883
		}
884
	}
885
886
    public static function mb_tags_box( $form_id, $class = '' ) {
887
		$fields = FrmField::get_all_for_form( $form_id, '', 'include' );
888
        $linked_forms = array();
889
        $col = 'one';
890
		$settings_tab = FrmAppHelper::is_admin_page( 'formidable' ) ? true : false;
891
892
		$cond_shortcodes = apply_filters( 'frm_conditional_shortcodes', array() );
893
		$entry_shortcodes = self::get_shortcode_helpers( $settings_tab );
894
895
		$advanced_helpers = self::advanced_helpers( compact( 'fields', 'form_id' ) );
896
897
		include( FrmAppHelper::plugin_path() . '/classes/views/shared/mb_adv_info.php' );
898
    }
899
900
	/**
901
	 * @since 3.04.01
902
	 */
903
	private static function advanced_helpers( $atts ) {
904
		$advanced_helpers = array(
905
			'default' => array(
906
				'heading' => __( 'Customize the field values with the following parameters. Click to see a sample.', 'formidable' ),
907
				'codes'   => self::get_advanced_shortcodes(),
908
			),
909
		);
910
911
		$user_fields = self::user_shortcodes();
912
		if ( ! empty( $user_fields ) ) {
913
			$user_helpers = array();
914
			foreach ( $user_fields as $uk => $uf ) {
915
				$user_helpers[ '|user_id| show="' . $uk . '"' ] = $uf;
916
				unset( $uk, $uf );
917
			}
918
919
			$advanced_helpers['user_id'] = array(
920
				'heading' => __( 'Insert user information', 'formidable' ),
921
				'codes'   => $user_helpers,
922
			);
923
		}
924
925
		/**
926
		 * Add extra helper shortcodes on the Advanced tab in form settings and views
927
		 *
928
		 * @since 3.04.01
929
		 * @param array $atts - Includes fields and form_id
930
		 */
931
		return apply_filters( 'frm_advanced_helpers', $advanced_helpers, $atts );
932
	}
933
934
	/**
935
	 * Get an array of the options to display in the advanced tab
936
	 * of the customization panel
937
	 *
938
	 * @since 2.0.6
939
	 */
940
	private static function get_advanced_shortcodes() {
941
		$adv_shortcodes = array(
942
			'x sep=", "'           => array(
943
				'label' => __( 'Separator', 'formidable' ),
944
				'title' => __( 'Use a different separator for checkbox fields', 'formidable' ),
945
			),
946
			'x format="d-m-Y"'     => __( 'Date Format', 'formidable' ),
947
			'x show="field_label"' => __( 'Field Label', 'formidable' ),
948
			'x wpautop=0'          => array(
949
				'label' => __( 'No Auto P', 'formidable' ),
950
				'title' => __( 'Do not automatically add any paragraphs or line breaks', 'formidable' ),
951
			),
952
		);
953
		$adv_shortcodes = apply_filters( 'frm_advanced_shortcodes', $adv_shortcodes );
954
		// __( 'Leave blank instead of defaulting to User Login', 'formidable' ) : blank=1
955
956
		return $adv_shortcodes;
957
	}
958
959
	/**
960
	 * @since 3.04.01
961
	 */
962
	private static function user_shortcodes() {
963
		$options = array(
964
			'ID'            => __( 'User ID', 'formidable' ),
965
			'first_name'    => __( 'First Name', 'formidable' ),
966
			'last_name'     => __( 'Last Name', 'formidable' ),
967
			'display_name'  => __( 'Display Name', 'formidable' ),
968
			'user_login'    => __( 'User Login', 'formidable' ),
969
			'user_email'    => __( 'Email', 'formidable' ),
970
			'avatar'        => __( 'Avatar', 'formidable' ),
971
			'author_link'   => __( 'Author Link', 'formidable' ),
972
		);
973
		return apply_filters( 'frm_user_shortcodes', $options );
974
	}
975
976
	/**
977
	 * Get an array of the helper shortcodes to display in the customization panel
978
	 *
979
	 * @since 2.0.6
980
	 */
981
	private static function get_shortcode_helpers( $settings_tab ) {
982
		$entry_shortcodes = array(
983
			'id'        => __( 'Entry ID', 'formidable' ),
984
			'key'       => __( 'Entry Key', 'formidable' ),
985
			'post_id'   => __( 'Post ID', 'formidable' ),
986
			'ip'        => __( 'User IP', 'formidable' ),
987
			'created-at' => __( 'Entry created', 'formidable' ),
988
			'updated-at' => __( 'Entry updated', 'formidable' ),
989
			''          => '',
990
			'siteurl'   => __( 'Site URL', 'formidable' ),
991
			'sitename'  => __( 'Site Name', 'formidable' ),
992
        );
993
994
		if ( ! FrmAppHelper::pro_is_installed() ) {
995
			unset( $entry_shortcodes['post_id'] );
996
		}
997
998
		if ( $settings_tab ) {
999
			$entry_shortcodes['default-message'] = __( 'Default Msg', 'formidable' );
1000
			$entry_shortcodes['default-html'] = __( 'Default HTML', 'formidable' );
1001
			$entry_shortcodes['default-plain'] = __( 'Default Plain', 'formidable' );
1002
		}
1003
1004
		/**
1005
		 * Use this hook to add or remove buttons in the helpers section
1006
		 * in the customization panel
1007
		 *
1008
		 * @since 2.0.6
1009
		 */
1010
		$entry_shortcodes = apply_filters( 'frm_helper_shortcodes', $entry_shortcodes, $settings_tab );
1011
1012
		return $entry_shortcodes;
1013
	}
1014
1015
	/**
1016
	 * Insert the form class setting into the form
1017
	 */
1018
	public static function form_classes( $form ) {
1019
		if ( isset( $form->options['form_class'] ) ) {
1020
			echo esc_attr( sanitize_text_field( $form->options['form_class'] ) );
1021
		}
1022
1023
		if ( isset( $form->options['js_validate'] ) && $form->options['js_validate'] ) {
1024
			echo ' frm_js_validate ';
1025
		}
1026
	}
1027
1028
	public static function get_email_html() {
1029
		FrmAppHelper::permission_check( 'frm_view_forms' );
1030
		check_ajax_referer( 'frm_ajax', 'nonce' );
1031
1032
		echo FrmEntriesController::show_entry_shortcode( // WPCS: XSS ok.
1033
			array(
1034
				'form_id'       => FrmAppHelper::get_post_param( 'form_id', '', 'absint' ),
1035
				'default_email' => true,
1036
				'plain_text'    => FrmAppHelper::get_post_param( 'plain_text', '', 'absint' ),
1037
			)
1038
		);
1039
		wp_die();
1040
	}
1041
1042
    public static function filter_content( $content, $form, $entry = false ) {
1043
		self::get_entry_by_param( $entry );
1044
        if ( ! $entry ) {
1045
            return $content;
1046
        }
1047
1048
        if ( is_object( $form ) ) {
1049
            $form = $form->id;
1050
        }
1051
1052
        $shortcodes = FrmFieldsHelper::get_shortcodes( $content, $form );
1053
        $content = apply_filters( 'frm_replace_content_shortcodes', $content, $entry, $shortcodes );
1054
1055
        return $content;
1056
    }
1057
1058
	private static function get_entry_by_param( &$entry ) {
1059
		if ( ! $entry || ! is_object( $entry ) ) {
1060
			if ( ! $entry || ! is_numeric( $entry ) ) {
1061
				$entry = FrmAppHelper::get_post_param( 'id', false, 'sanitize_title' );
1062
			}
1063
1064
			FrmEntry::maybe_get_entry( $entry );
1065
		}
1066
	}
1067
1068
    public static function replace_content_shortcodes( $content, $entry, $shortcodes ) {
1069
        return FrmFieldsHelper::replace_content_shortcodes( $content, $entry, $shortcodes );
1070
    }
1071
1072
    public static function process_bulk_form_actions( $errors ) {
1073
        if ( ! $_REQUEST ) {
1074
            return $errors;
1075
        }
1076
1077
		$bulkaction = FrmAppHelper::get_param( 'action', '', 'get', 'sanitize_text_field' );
1078
        if ( $bulkaction == -1 ) {
1079
			$bulkaction = FrmAppHelper::get_param( 'action2', '', 'get', 'sanitize_title' );
1080
        }
1081
1082
        if ( ! empty( $bulkaction ) && strpos( $bulkaction, 'bulk_' ) === 0 ) {
1083
            FrmAppHelper::remove_get_action();
1084
1085
            $bulkaction = str_replace( 'bulk_', '', $bulkaction );
1086
        }
1087
1088
		$ids = FrmAppHelper::get_param( 'item-action', '', 'get', 'sanitize_text_field' );
1089
        if ( empty( $ids ) ) {
1090
            $errors[] = __( 'No forms were specified', 'formidable' );
1091
            return $errors;
1092
        }
1093
1094
        $permission_error = FrmAppHelper::permission_nonce_error( '', '_wpnonce', 'bulk-toplevel_page_formidable' );
1095
        if ( $permission_error !== false ) {
1096
            $errors[] = $permission_error;
1097
            return $errors;
1098
        }
1099
1100
        if ( ! is_array( $ids ) ) {
1101
            $ids = explode( ',', $ids );
1102
        }
1103
1104
        switch ( $bulkaction ) {
1105
            case 'delete':
1106
                $message = self::bulk_destroy( $ids );
1107
				break;
1108
            case 'trash':
1109
                $message = self::bulk_trash( $ids );
1110
				break;
1111
            case 'untrash':
1112
                $message = self::bulk_untrash( $ids );
1113
        }
1114
1115
        if ( isset( $message ) && ! empty( $message ) ) {
1116
			echo '<div id="message" class="frm_updated_message">' . FrmAppHelper::kses( $message, array( 'a' ) ) . '</div>'; // WPCS: XSS ok.
1117
        }
1118
1119
        return $errors;
1120
    }
1121
1122
    public static function route() {
1123
		$action = isset( $_REQUEST['frm_action'] ) ? 'frm_action' : 'action';
1124
        $vars = array();
1125
		if ( isset( $_POST['frm_compact_fields'] ) ) {
1126
			FrmAppHelper::permission_check( 'frm_edit_forms' );
1127
1128
			$json_vars = htmlspecialchars_decode( nl2br( stripslashes( str_replace( '&quot;', '\\\"', $_POST['frm_compact_fields'] ) ) ) );
1129
			$json_vars = json_decode( $json_vars, true );
1130
			if ( empty( $json_vars ) ) {
1131
                // json decoding failed so we should return an error message
1132
				$action = FrmAppHelper::get_param( $action, '', 'get', 'sanitize_title' );
1133
                if ( 'edit' == $action ) {
1134
                    $action = 'update';
1135
                }
1136
1137
				add_filter( 'frm_validate_form', 'FrmFormsController::json_error' );
1138
            } else {
1139
				$vars = FrmAppHelper::json_to_array( $json_vars );
1140
                $action = $vars[ $action ];
1141
				unset( $_REQUEST['frm_compact_fields'], $_POST['frm_compact_fields'] );
1142
				$_REQUEST = array_merge( $_REQUEST, $vars );
1143
				$_POST = array_merge( $_POST, $_REQUEST );
1144
            }
1145
        } else {
1146
			$action = FrmAppHelper::get_param( $action, '', 'get', 'sanitize_title' );
1147
    		if ( isset( $_REQUEST['delete_all'] ) ) {
1148
                // override the action for this page
1149
    			$action = 'delete_all';
1150
            }
1151
        }
1152
1153
		add_action( 'frm_load_form_hooks', 'FrmHooksController::trigger_load_form_hooks' );
1154
        FrmAppHelper::trigger_hook_load( 'form' );
1155
1156
        switch ( $action ) {
1157
            case 'new':
1158
				return self::new_form( $vars );
1159
			case 'add_new':
1160
			case 'list_templates':
1161
            case 'create':
1162
            case 'edit':
1163
            case 'update':
1164
            case 'duplicate':
1165
            case 'trash':
1166
            case 'untrash':
1167
            case 'destroy':
1168
            case 'delete_all':
1169
            case 'settings':
1170
            case 'update_settings':
1171
				return self::$action( $vars );
1172
            default:
1173
				do_action( 'frm_form_action_' . $action );
1174
				if ( apply_filters( 'frm_form_stop_action_' . $action, false ) ) {
1175
                    return;
1176
                }
1177
1178
				$action = FrmAppHelper::get_param( 'action', '', 'get', 'sanitize_text_field' );
1179
                if ( $action == -1 ) {
1180
					$action = FrmAppHelper::get_param( 'action2', '', 'get', 'sanitize_title' );
1181
                }
1182
1183
				if ( strpos( $action, 'bulk_' ) === 0 ) {
1184
                    FrmAppHelper::remove_get_action();
1185
                    return self::list_form();
1186
                }
1187
1188
                return self::display_forms_list();
1189
        }
1190
    }
1191
1192
    public static function json_error( $errors ) {
1193
        $errors['json'] = __( 'Abnormal HTML characters prevented your form from saving correctly', 'formidable' );
1194
        return $errors;
1195
    }
1196
1197
1198
    /* FRONT-END FORMS */
1199
    public static function admin_bar_css() {
1200
		if ( is_admin() || ! current_user_can( 'frm_edit_forms' ) ) {
1201
            return;
1202
        }
1203
1204
		add_action( 'wp_before_admin_bar_render', 'FrmFormsController::admin_bar_configure' );
1205
		FrmAppHelper::load_font_style();
1206
	}
1207
1208
	public static function admin_bar_configure() {
1209
        global $frm_vars;
1210
		if ( empty( $frm_vars['forms_loaded'] ) ) {
1211
            return;
1212
        }
1213
1214
        $actions = array();
1215
		foreach ( $frm_vars['forms_loaded'] as $form ) {
1216
			if ( is_object( $form ) ) {
1217
				$actions[ $form->id ] = $form->name;
1218
			}
1219
			unset( $form );
1220
		}
1221
1222
		if ( empty( $actions ) ) {
1223
			return;
1224
		}
1225
1226
		self::add_menu_to_admin_bar();
1227
		self::add_forms_to_admin_bar( $actions );
1228
	}
1229
1230
	/**
1231
	 * @since 2.05.07
1232
	 */
1233
	public static function add_menu_to_admin_bar() {
1234
		global $wp_admin_bar;
1235
1236
		$wp_admin_bar->add_node(
1237
			array(
1238
				'id'    => 'frm-forms',
1239
				'title' => '<span class="ab-icon"></span><span class="ab-label">' . FrmAppHelper::get_menu_name() . '</span>',
1240
				'href'  => admin_url( 'admin.php?page=formidable' ),
1241
				'meta'  => array(
1242
					'title' => FrmAppHelper::get_menu_name(),
1243
				),
1244
			)
1245
		);
1246
	}
1247
1248
	/**
1249
	 * @since 2.05.07
1250
	 */
1251
	private static function add_forms_to_admin_bar( $actions ) {
1252
		global $wp_admin_bar;
1253
1254
		asort( $actions );
1255
1256
		foreach ( $actions as $form_id => $name ) {
1257
1258
			$wp_admin_bar->add_node(
1259
				array(
1260
					'parent'    => 'frm-forms',
1261
					'id'        => 'edit_form_' . $form_id,
1262
					'title'     => empty( $name ) ? __( '(no title)', 'formidable' ) : $name,
1263
					'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...
1264
				)
1265
			);
1266
		}
1267
	}
1268
1269
    //formidable shortcode
1270
	public static function get_form_shortcode( $atts ) {
1271
        global $frm_vars;
1272
		if ( isset( $frm_vars['skip_shortcode'] ) && $frm_vars['skip_shortcode'] ) {
1273
            $sc = '[formidable';
1274
			if ( ! empty( $atts ) ) {
1275
				foreach ( $atts as $k => $v ) {
1276
					$sc .= ' ' . $k . '="' . esc_attr( $v ) . '"';
1277
				}
1278
			}
1279
			return $sc . ']';
1280
        }
1281
1282
		$shortcode_atts = shortcode_atts(
1283
			array(
1284
				'id'          => '',
1285
				'key'         => '',
1286
				'title'       => false,
1287
				'description' => false,
1288
				'readonly'    => false,
1289
				'entry_id'    => false,
1290
				'fields'      => array(),
1291
				'exclude_fields' => array(),
1292
				'minimize'    => false,
1293
			),
1294
			$atts
1295
		);
1296
		do_action( 'formidable_shortcode_atts', $shortcode_atts, $atts );
1297
1298
        return self::show_form( $shortcode_atts['id'], $shortcode_atts['key'], $shortcode_atts['title'], $shortcode_atts['description'], $atts );
1299
    }
1300
1301
    public static function show_form( $id = '', $key = '', $title = false, $description = false, $atts = array() ) {
1302
        if ( empty( $id ) ) {
1303
            $id = $key;
1304
        }
1305
1306
        $form = self::maybe_get_form_to_show( $id );
1307
        if ( ! $form ) {
1308
            return __( 'Please select a valid form', 'formidable' );
1309
        }
1310
1311
		FrmAppController::maybe_update_styles();
1312
1313
		add_action( 'frm_load_form_hooks', 'FrmHooksController::trigger_load_form_hooks' );
1314
        FrmAppHelper::trigger_hook_load( 'form', $form );
1315
1316
        $form = apply_filters( 'frm_pre_display_form', $form );
1317
1318
        $frm_settings = FrmAppHelper::get_settings();
1319
1320
		if ( self::is_viewable_draft_form( $form ) ) {
1321
			// don't show a draft form on a page
1322
			$form = __( 'Please select a valid form', 'formidable' );
1323
		} else if ( self::user_should_login( $form ) ) {
1324
			$form = do_shortcode( $frm_settings->login_msg );
1325
		} else if ( self::user_has_permission_to_view( $form ) ) {
1326
			$form = do_shortcode( $frm_settings->login_msg );
1327
		} else {
1328
			do_action( 'frm_pre_get_form', $form );
1329
			$form = self::get_form( $form, $title, $description, $atts );
1330
1331
			/**
1332
			 * Use this shortcode to check for external shortcodes that may span
1333
			 * across multiple fields in the customizable HTML
1334
			 *
1335
			 * @since 2.0.8
1336
			 */
1337
			$form = apply_filters( 'frm_filter_final_form', $form );
1338
		}
1339
1340
		return $form;
1341
    }
1342
1343
	private static function maybe_get_form_to_show( $id ) {
1344
		$form = false;
1345
1346
		if ( ! empty( $id ) ) { // no form id or key set
1347
			$form = FrmForm::getOne( $id );
1348
			if ( ! $form || $form->parent_form_id || $form->status == 'trash' ) {
1349
				$form = false;
1350
			}
1351
		}
1352
1353
		return $form;
1354
	}
1355
1356
	private static function is_viewable_draft_form( $form ) {
1357
		global $post;
1358
		$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...
1359
		return $form->status == 'draft' && current_user_can( 'frm_edit_forms' ) && ! FrmAppHelper::is_preview_page();
1360
	}
1361
1362
	private static function user_should_login( $form ) {
1363
		return $form->logged_in && ! is_user_logged_in();
1364
	}
1365
1366
	private static function user_has_permission_to_view( $form ) {
1367
		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'] );
1368
	}
1369
1370
    public static function get_form( $form, $title, $description, $atts = array() ) {
1371
		ob_start();
1372
1373
		do_action( 'frm_before_get_form', $atts );
1374
1375
        self::get_form_contents( $form, $title, $description, $atts );
1376
		self::enqueue_scripts( FrmForm::get_params( $form ) );
1377
1378
        $contents = ob_get_contents();
1379
        ob_end_clean();
1380
1381
		self::maybe_minimize_form( $atts, $contents );
1382
1383
        return $contents;
1384
    }
1385
1386
	public static function enqueue_scripts( $params ) {
1387
		do_action( 'frm_enqueue_form_scripts', $params );
1388
	}
1389
1390
	public static function get_form_contents( $form, $title, $description, $atts ) {
1391
		$params = FrmForm::get_params( $form );
1392
		$errors = self::get_saved_errors( $form, $params );
1393
		$fields = FrmFieldsHelper::get_form_fields( $form->id, $errors );
1394
		$reset = false;
1395
		$pass_args = compact( 'form', 'fields', 'errors', 'title', 'description', 'reset' );
1396
1397
		$handle_process_here = $params['action'] == 'create' && $params['posted_form_id'] == $form->id && $_POST;
1398
1399
		if ( ! $handle_process_here ) {
1400
			do_action( 'frm_display_form_action', $params, $fields, $form, $title, $description );
1401
			if ( apply_filters( 'frm_continue_to_new', true, $form->id, $params['action'] ) ) {
1402
				self::show_form_after_submit( $pass_args );
1403
			}
1404
		} elseif ( ! empty( $errors ) ) {
1405
			self::show_form_after_submit( $pass_args );
1406
1407
		} else {
1408
1409
			do_action( 'frm_validate_form_creation', $params, $fields, $form, $title, $description );
1410
1411
			if ( apply_filters( 'frm_continue_to_create', true, $form->id ) ) {
1412
				$entry_id = self::just_created_entry( $form->id );
1413
				$pass_args['entry_id'] = $entry_id;
1414
				$pass_args['reset'] = true;
1415
				$pass_args['conf_method'] = self::get_confirmation_method( compact( 'form', 'entry_id' ) );
1416
1417
				self::run_success_action( $pass_args );
1418
1419
				do_action(
1420
					'frm_after_entry_processed',
1421
					array(
1422
						'entry_id' => $entry_id,
1423
						'form'     => $form,
1424
					)
1425
				);
1426
			}
1427
		}
1428
	}
1429
1430
	/**
1431
	 * If the form was processed earlier (init), get the generated errors
1432
	 *
1433
	 * @since 2.05
1434
	 */
1435
	private static function get_saved_errors( $form, $params ) {
1436
		global $frm_vars;
1437
1438
		if ( $params['posted_form_id'] == $form->id && $_POST && isset( $frm_vars['created_entries'][ $form->id ] ) ) {
1439
			$errors = $frm_vars['created_entries'][ $form->id ]['errors'];
1440
		} else {
1441
			$errors = array();
1442
		}
1443
		return $errors;
1444
	}
1445
1446
	/**
1447
	 * @since 2.2.7
1448
	 */
1449
	public static function just_created_entry( $form_id ) {
1450
		global $frm_vars;
1451
		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;
1452
	}
1453
1454
	/**
1455
	 * @since 3.0
1456
	 */
1457
	private static function get_confirmation_method( $atts ) {
1458
		$opt = 'success_action';
1459
		$method = ( isset( $atts['form']->options[ $opt ] ) && ! empty( $atts['form']->options[ $opt ] ) ) ? $atts['form']->options[ $opt ] : 'message';
1460
		$method = apply_filters( 'frm_success_filter', $method, $atts['form'], 'create' );
1461
1462
		if ( $method != 'message' && ( ! $atts['entry_id'] || ! is_numeric( $atts['entry_id'] ) ) ) {
1463
			$method = 'message';
1464
		}
1465
1466
		return $method;
1467
	}
1468
1469
	public static function maybe_trigger_redirect( $form, $params, $args ) {
1470
		if ( ! isset( $params['id'] ) ) {
1471
			global $frm_vars;
1472
			$params['id'] = $frm_vars['created_entries'][ $form->id ]['entry_id'];
1473
		}
1474
1475
		$conf_method = self::get_confirmation_method(
1476
			array(
1477
				'form'     => $form,
1478
				'entry_id' => $params['id'],
1479
			)
1480
		);
1481
1482
		if ( 'redirect' === $conf_method ) {
1483
			self::trigger_redirect( $form, $params, $args );
1484
		}
1485
	}
1486
1487
	public static function trigger_redirect( $form, $params, $args ) {
1488
		$success_args = array(
1489
			'action'      => $params['action'],
1490
			'conf_method' => 'redirect',
1491
			'form'        => $form,
1492
			'entry_id'    => $params['id'],
1493
		);
1494
1495
		if ( isset( $args['ajax'] ) ) {
1496
			$success_args['ajax'] = $args['ajax'];
1497
		}
1498
1499
		self::run_success_action( $success_args );
1500
	}
1501
1502
	/**
1503
	 * Used when the success action is not 'message'
1504
	 *
1505
	 * @since 2.05
1506
	 */
1507
	public static function run_success_action( $args ) {
1508
		$extra_args = $args;
1509
		unset( $extra_args['form'] );
1510
1511
		do_action( 'frm_success_action', $args['conf_method'], $args['form'], $args['form']->options, $args['entry_id'], $extra_args );
1512
1513
		$opt = ( ! isset( $args['action'] ) || $args['action'] == 'create' ) ? 'success' : 'edit';
1514
		$args['success_opt'] = $opt;
1515
		if ( $args['conf_method'] == 'page' && is_numeric( $args['form']->options[ $opt . '_page_id' ] ) ) {
1516
			self::load_page_after_submit( $args );
1517
		} elseif ( $args['conf_method'] == 'redirect' ) {
1518
			self::redirect_after_submit( $args );
1519
		} else {
1520
			self::show_message_after_save( $args );
1521
		}
1522
	}
1523
1524
	/**
1525
	 * @since 3.0
1526
	 */
1527
	private static function load_page_after_submit( $args ) {
1528
		global $post;
1529
		$opt = $args['success_opt'];
1530
		if ( ! $post || $args['form']->options[ $opt . '_page_id' ] != $post->ID ) {
1531
			$page = get_post( $args['form']->options[ $opt . '_page_id' ] );
1532
			$old_post = $post;
1533
			$post = $page;
0 ignored issues
show
introduced by
Overridding WordPress globals is prohibited
Loading history...
1534
			$content = apply_filters( 'frm_content', $page->post_content, $args['form'], $args['entry_id'] );
1535
			echo apply_filters( 'the_content', $content ); // WPCS: XSS ok.
1536
			$post = $old_post;
0 ignored issues
show
introduced by
Overridding WordPress globals is prohibited
Loading history...
1537
		}
1538
	}
1539
1540
	/**
1541
	 * @since 3.0
1542
	 */
1543
	private static function redirect_after_submit( $args ) {
1544
		global $frm_vars;
1545
1546
		add_filter( 'frm_use_wpautop', '__return_false' );
1547
1548
		$opt = $args['success_opt'];
1549
		$success_url = trim( $args['form']->options[ $opt . '_url' ] );
1550
		$success_url = apply_filters( 'frm_content', $success_url, $args['form'], $args['entry_id'] );
1551
		$success_url = do_shortcode( $success_url );
1552
1553
		$success_msg = isset( $args['form']->options[ $opt . '_msg' ] ) ? $args['form']->options[ $opt . '_msg' ] : __( 'Please wait while you are redirected.', 'formidable' );
1554
1555
		$redirect_msg = self::get_redirect_message( $success_url, $success_msg, $args );
1556
1557
		$args['id'] = $args['entry_id'];
1558
		FrmEntriesController::delete_entry_before_redirect( $success_url, $args['form'], $args );
1559
1560
		add_filter( 'frm_redirect_url', 'FrmEntriesController::prepare_redirect_url' );
1561
		$success_url = apply_filters( 'frm_redirect_url', $success_url, $args['form'], $args );
1562
1563
		$doing_ajax = FrmAppHelper::doing_ajax();
1564
1565
		if ( isset( $args['ajax'] ) && $args['ajax'] && $doing_ajax ) {
1566
			echo json_encode( array( 'redirect' => $success_url ) );
1567
			wp_die();
1568
		} elseif ( ! headers_sent() ) {
1569
			wp_redirect( esc_url_raw( $success_url ) );
1570
			die(); // do not use wp_die or redirect fails
1571
		} else {
1572
			add_filter( 'frm_use_wpautop', '__return_true' );
1573
1574
			echo $redirect_msg; // WPCS: XSS ok.
1575
			echo "<script type='text/javascript'>window.onload = function(){setTimeout(window.location='" . esc_url_raw( $success_url ) . "', 8000);}</script>";
1576
		}
1577
	}
1578
1579
	/**
1580
	 * @since 3.0
1581
	 * @param string $success_url
1582
	 * @param string $success_msg
1583
	 * @param array $args
1584
	 */
1585
	private static function get_redirect_message( $success_url, $success_msg, $args ) {
1586
		$redirect_msg = '<div class="' . esc_attr( FrmFormsHelper::get_form_style_class( $args['form'] ) ) . '"><div class="frm-redirect-msg frm_message">' . $success_msg . '<br/>' .
1587
			sprintf( __( '%1$sClick here%2$s if you are not automatically redirected.', 'formidable' ), '<a href="' . esc_url( $success_url ) . '">', '</a>' ) .
1588
			'</div></div>';
1589
1590
		$redirect_args = array(
1591
			'entry_id' => $args['entry_id'],
1592
			'form_id'  => $args['form']->id,
1593
			'form'     => $args['form'],
1594
		);
1595
		return apply_filters( 'frm_redirect_msg', $redirect_msg, $redirect_args );
1596
	}
1597
1598
	/**
1599
	 * Prepare to show the success message and empty form after submit
1600
	 *
1601
	 * @since 2.05
1602
	 */
1603
	public static function show_message_after_save( $atts ) {
1604
		$atts['message'] = self::prepare_submit_message( $atts['form'], $atts['entry_id'] );
1605
1606
		if ( ! isset( $atts['form']->options['show_form'] ) || $atts['form']->options['show_form'] ) {
1607
			self::show_form_after_submit( $atts );
1608
		} else {
1609
			self::show_lone_success_messsage( $atts );
1610
		}
1611
	}
1612
1613
	/**
1614
	 * Show an empty form
1615
	 *
1616
	 * @since 2.05
1617
	 */
1618
	private static function show_form_after_submit( $args ) {
1619
		self::fill_atts_for_form_display( $args );
1620
1621
		$errors = $args['errors'];
1622
		$message = $args['message'];
1623
		$form = $args['form'];
1624
		$title = $args['title'];
1625
		$description = $args['description'];
1626
1627
		if ( empty( $args['fields'] ) ) {
1628
			$values = array();
1629
		} else {
1630
			$values = FrmEntriesHelper::setup_new_vars( $args['fields'], $form, $args['reset'] );
1631
		}
1632
		unset( $args );
1633
1634
		$include_form_tag = apply_filters( 'frm_include_form_tag', true, $form );
1635
1636
		$frm_settings = FrmAppHelper::get_settings();
1637
		$submit = isset( $form->options['submit_value'] ) ? $form->options['submit_value'] : $frm_settings->submit_value;
1638
1639
		include( FrmAppHelper::plugin_path() . '/classes/views/frm-entries/new.php' );
1640
	}
1641
1642
	/**
1643
	 * Get all the values needed on the new.php entry page
1644
	 *
1645
	 * @since 2.05
1646
	 */
1647
	private static function fill_atts_for_form_display( &$args ) {
1648
		$defaults = array(
1649
			'errors'  => array(),
1650
			'message' => '',
1651
			'fields'  => array(),
1652
			'form'    => array(),
1653
			'title'   => true,
1654
			'description' => false,
1655
			'reset'   => false,
1656
		);
1657
		$args = wp_parse_args( $args, $defaults );
1658
	}
1659
1660
	/**
1661
	 * Show the success message without the form
1662
	 *
1663
	 * @since 2.05
1664
	 */
1665
	private static function show_lone_success_messsage( $atts ) {
1666
		global $frm_vars;
1667
		$values = FrmEntriesHelper::setup_new_vars( $atts['fields'], $atts['form'], true );
1668
		self::maybe_load_css( $atts['form'], $values['custom_style'], $frm_vars['load_css'] );
1669
1670
		$include_extra_container = 'frm_forms' . FrmFormsHelper::get_form_style_class( $values );
1671
		$errors = array();
1672
		$form = $atts['form'];
1673
		$message = $atts['message'];
1674
1675
		include( FrmAppHelper::plugin_path() . '/classes/views/frm-entries/errors.php' );
1676
	}
1677
1678
	/**
1679
	 * Prepare the success message before it's shown
1680
	 *
1681
	 * @since 2.05
1682
	 */
1683
	private static function prepare_submit_message( $form, $entry_id ) {
1684
		$frm_settings = FrmAppHelper::get_settings();
1685
1686
		if ( $entry_id && is_numeric( $entry_id ) ) {
1687
			$message = isset( $form->options['success_msg'] ) ? $form->options['success_msg'] : $frm_settings->success_msg;
1688
			$class = 'frm_message';
1689
		} else {
1690
			$message = $frm_settings->failed_msg;
1691
			$class = FrmFormsHelper::form_error_class();
1692
		}
1693
1694
		$message = FrmFormsHelper::get_success_message( compact( 'message', 'form', 'entry_id', 'class' ) );
1695
		return apply_filters( 'frm_main_feedback', $message, $form, $entry_id );
1696
	}
1697
1698
	public static function front_head() {
1699
		$version = FrmAppHelper::plugin_version();
1700
		$suffix = FrmAppHelper::js_suffix();
1701
1702
		if ( ! empty( $suffix ) && self::has_combo_js_file() ) {
1703
			wp_register_script( 'formidable', FrmAppHelper::plugin_url() . '/js/frm.min.js', array( 'jquery' ), $version, true );
1704
		} else {
1705
			wp_register_script( 'formidable', FrmAppHelper::plugin_url() . "/js/formidable{$suffix}.js", array( 'jquery' ), $version, true );
1706
		}
1707
1708
		add_filter( 'script_loader_tag', 'FrmFormsController::defer_script_loading', 10, 2 );
1709
1710
		if ( FrmAppHelper::is_admin() ) {
1711
			// don't load this in back-end
1712
			return;
1713
		}
1714
1715
		FrmAppHelper::localize_script( 'front' );
1716
		FrmStylesController::enqueue_css( 'register' );
1717
	}
1718
1719
	/**
1720
	 * @since 3.0
1721
	 */
1722
	public static function has_combo_js_file() {
1723
		return is_readable( FrmAppHelper::plugin_path() . '/js/frm.min.js' );
1724
	}
1725
1726
	public static function maybe_load_css( $form, $this_load, $global_load ) {
1727
		$load_css = FrmForm::is_form_loaded( $form, $this_load, $global_load );
1728
1729
		if ( $load_css ) {
1730
			global $frm_vars;
1731
			self::footer_js( 'header' );
1732
			$frm_vars['css_loaded'] = true;
1733
		}
1734
	}
1735
1736
	public static function defer_script_loading( $tag, $handle ) {
1737
	    if ( 'recaptcha-api' == $handle && ! strpos( $tag, 'defer' ) ) {
1738
	        $tag = str_replace( ' src', ' defer="defer" async="async" src', $tag );
1739
		}
1740
	    return $tag;
1741
	}
1742
1743
	public static function footer_js( $location = 'footer' ) {
1744
		global $frm_vars;
1745
1746
		FrmStylesController::enqueue_css();
1747
1748
		if ( ! FrmAppHelper::is_admin() && $location != 'header' && ! empty( $frm_vars['forms_loaded'] ) ) {
1749
			//load formidable js
1750
			wp_enqueue_script( 'formidable' );
1751
		}
1752
	}
1753
1754
	/**
1755
	 * @since 2.0.8
1756
	 */
1757
	private static function maybe_minimize_form( $atts, &$content ) {
1758
		// check if minimizing is turned on
1759
		if ( self::is_minification_on( $atts ) ) {
1760
			$content = str_replace( array( "\r\n", "\r", "\n", "\t", '    ' ), '', $content );
1761
		}
1762
	}
1763
1764
	/**
1765
	 * @since 2.0.8
1766
	 * @return boolean
1767
	 */
1768
	private static function is_minification_on( $atts ) {
1769
		return isset( $atts['minimize'] ) && ! empty( $atts['minimize'] );
1770
	}
1771
1772
	/**
1773
	 * @deprecated 1.07.05
1774
	 * @codeCoverageIgnore
1775
	 */
1776
	public static function add_default_templates( $path, $default = true, $template = true ) {
1777
		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...
1778
	}
1779
1780
	/**
1781
	 * @deprecated 3.0
1782
	 * @codeCoverageIgnore
1783
	 */
1784
	public static function bulk_create_template( $ids ) {
1785
		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...
1786
	}
1787
1788
	/**
1789
	 * @deprecated 2.03
1790
	 * @codeCoverageIgnore
1791
	 */
1792
	public static function register_pro_scripts() {
1793
		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...
1794
	}
1795
1796
	/**
1797
	 * @deprecated 3.0
1798
	 * @codeCoverageIgnore
1799
	 */
1800
	public static function edit_key() {
1801
		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...
1802
	}
1803
1804
	/**
1805
	 * @deprecated 3.0
1806
	 * @codeCoverageIgnore
1807
	 */
1808
	public static function edit_description() {
1809
		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...
1810
	}
1811
}
1812