Completed
Push — master ( eb5e35...a86bd1 )
by Stephanie
24s queued 11s
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
		if ( wp_is_mobile() ) {
29
			wp_enqueue_script( 'jquery-touch-punch' );
30
		}
31
	}
32
33
	public static function register_widgets() {
34
		require_once( FrmAppHelper::plugin_path() . '/classes/widgets/FrmShowForm.php' );
35
		register_widget( 'FrmShowForm' );
36
	}
37
38
	/**
39
	 * By default, Divi processes form shortcodes on the edit post page.
40
	 * Now that won't do.
41
	 *
42
	 * @since 3.01
43
	 */
44
	public static function prevent_divi_conflict( $shortcodes ) {
45
		$shortcodes[] = 'formidable';
46
47
		return $shortcodes;
48
	}
49
50
	public static function list_form() {
51
		FrmAppHelper::permission_check( 'frm_view_forms' );
52
53
		$message = '';
54
		$params  = FrmForm::list_page_params();
55
		$errors  = self::process_bulk_form_actions( array() );
56
		if ( isset( $errors['message'] ) ) {
57
			$message = $errors['message'];
58
			unset( $errors['message'] );
59
		}
60
		$errors = apply_filters( 'frm_admin_list_form_action', $errors );
61
62
		return self::display_forms_list( $params, $message, $errors );
63
	}
64
65
	/**
66
	 * Choose which type of form to create
67
	 *
68
	 * @since 3.06
69
	 */
70
	public static function add_new() {
71
		self::list_templates();
72
	}
73
74
	/**
75
	 * Load the scripts before a modal can be triggered.
76
	 *
77
	 * @since 4.0
78
	 */
79
	private static function init_modal() {
80
		wp_enqueue_script( 'jquery-ui-dialog' );
81
		wp_enqueue_style( 'jquery-ui-dialog' );
82
	}
83
84
	/**
85
	 * Create the default email action
86
	 *
87
	 * @since 2.02.11
88
	 *
89
	 * @param object $form
90
	 */
91
	private static function create_default_email_action( $form ) {
92
		FrmForm::maybe_get_form( $form );
93
		$create_email = apply_filters( 'frm_create_default_email_action', true, $form );
94
95
		if ( $create_email ) {
96
			$action_control = FrmFormActionsController::get_form_actions( 'email' );
97
			$action_control->create( $form->id );
98
		}
99
	}
100
101
	public static function edit( $values = false ) {
102
		FrmAppHelper::permission_check( 'frm_edit_forms' );
103
104
		$id = isset( $values['id'] ) ? absint( $values['id'] ) : FrmAppHelper::get_param( 'id', '', 'get', 'absint' );
105
106
		return self::get_edit_vars( $id );
107
	}
108
109
	public static function settings( $id = false, $message = '' ) {
110
		FrmAppHelper::permission_check( 'frm_edit_forms' );
111
112
		if ( ! $id || ! is_numeric( $id ) ) {
113
			$id = FrmAppHelper::get_param( 'id', '', 'get', 'absint' );
114
		}
115
116
		return self::get_settings_vars( $id, array(), $message );
117
	}
118
119
	public static function update_settings() {
120
		FrmAppHelper::permission_check( 'frm_edit_forms' );
121
122
		$id = FrmAppHelper::get_param( 'id', '', 'get', 'absint' );
123
124
		$errors = FrmForm::validate( $_POST );
125
		$warnings = self::check_for_warnings( $_POST );
126
127
		if ( count( $errors ) > 0 ) {
128
			return self::get_settings_vars( $id, $errors, array( 'warnings' => $warnings ) );
129
		}
130
131
		do_action( 'frm_before_update_form_settings', $id );
132
133
		FrmForm::update( $id, $_POST );
134
135
		$message = __( 'Settings Successfully Updated', 'formidable' );
136
137
		$args = array(
138
			'message'  => $message,
139
			'warnings' => $warnings,
140
		);
141
142
		return self::get_settings_vars( $id, array(), $args );
143
	}
144
145
	/**
146
	 * Checks for warnings to be displayed after form settings are saved.
147
	 *
148
	 * @param array $values The $_POST array, which contains values submitted in a form.
149
	 *
150
	 * @return array An array of warnings or an empty array.
151
	 */
152
	public static function check_for_warnings( $values ) {
153
		$warnings = array();
154
155
		$redirect_warning = self::check_redirect_url_for_unsafe_params( $values );
156
157
		if ( $redirect_warning ) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $redirect_warning of type false|string is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== false instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
158
			$warnings[] = $redirect_warning;
159
		}
160
161
		return apply_filters( 'frm_check_for_warnings', $warnings, $values );
162
	}
163
164
	/**
165
	 * Checks the redirect URL for params whose names are reserved words.
166
	 *
167
	 * @param array $values The $_POST array, which contains the values submitted in a form.
168
	 *
169
	 * @return bool|string A warning message about unsafe params or false.
170
	 */
171
	private static function check_redirect_url_for_unsafe_params( $values ) {
172
		if ( ! isset( $values['options'] ) ) {
173
			return false;
174
		}
175
176
		$options = $values['options'];
177
		FrmAppHelper::sanitize_with_html( $options );
178
179
		if ( ( ! isset( $options['success_action'] ) ) || $options['success_action'] !== 'redirect' || ! isset( $options['success_url'] ) ) {
180
			return false;
181
		}
182
183
		$unsafe_params_in_redirect = self::get_unsafe_params( $options['success_url'] );
184
185
		return self::create_unsafe_param_warning( $unsafe_params_in_redirect );
186
	}
187
188
	/**
189
	 * Returns an array of params whose names are reserved words in the specified URL.
190
	 *
191
	 * @param string $url The URL whose params are being checked.
192
	 *
193
	 * @return array An array of params whose names are reserved words or an empty array.
194
	 */
195
	private static function get_unsafe_params( $url ) {
196
		$redirect_components = parse_url( $url );
197
		parse_str( $redirect_components['query'], $redirect_params );
198
		$redirect_param_names      = array_keys( $redirect_params );
199
		$reserved_words            = FrmFormsHelper::reserved_words();
200
		$unsafe_params_in_redirect = array_intersect( $redirect_param_names, $reserved_words );
201
202
		return array_values( $unsafe_params_in_redirect );
203
	}
204
205
	/**
206
	 * Returns a warning if reserved words have been used as param names in the redirect URL.
207
	 *
208
	 * @param array $unsafe_params_in_redirect Array of params from the redirect URL whose names are reserved words.
209
	 *
210
	 * @return bool|string A string with an unsafe param message or false.
211
	 */
212
	private static function create_unsafe_param_warning( $unsafe_params_in_redirect ) {
213
		$count                = count( $unsafe_params_in_redirect );
214
		$caution              = esc_html__( 'Is this intentional?', 'formidable' );
215
		$reserved_words_intro = esc_html__( 'See the list of reserved words in WordPress.', 'formidable' );
216
		$reserved_words_link  = '<a href="https://codex.wordpress.org/WordPress_Query_Vars" target="_blank"> ' . $reserved_words_intro . '</a>';
217
218
		if ( $count === 0 ) {
219
			return false;
220
		}
221
222
		if ( $count == 1 ) {
223
			/* translators: %s: the name of a single parameter in the redirect URL */
224
			return sprintf( esc_html__( 'The redirect URL is using the parameter "%s", which is reserved by WordPress. ', 'formidable' ), $unsafe_params_in_redirect[0] ) . $caution . $reserved_words_link;
225
		}
226
227
		$unsafe_params_string = implode( '", "', $unsafe_params_in_redirect );
228
229
		/* translators: %s: the names of two or more parameters in the redirect URL, separated by commas */
230
		return sprintf( esc_html__( 'The redirect URL is using the parameters "%s", which are reserved by WordPress. ', 'formidable' ), $unsafe_params_string ) . $caution . $reserved_words_link;
231
	}
232
233
	public static function update( $values = array() ) {
234
		if ( empty( $values ) ) {
235
			$values = $_POST;
236
		}
237
238
		// Set radio button and checkbox meta equal to "other" value.
239
		if ( FrmAppHelper::pro_is_installed() ) {
240
			$values = FrmProEntry::mod_other_vals( $values, 'back' );
241
		}
242
243
		$errors           = FrmForm::validate( $values );
244
		$permission_error = FrmAppHelper::permission_nonce_error( 'frm_edit_forms', 'frm_save_form', 'frm_save_form_nonce' );
245
		if ( $permission_error !== false ) {
246
			$errors['form'] = $permission_error;
247
		}
248
249
		$id = isset( $values['id'] ) ? absint( $values['id'] ) : FrmAppHelper::get_param( 'id', '', 'get', 'absint' );
250
251
		if ( count( $errors ) > 0 ) {
252
			return self::get_edit_vars( $id, $errors );
253
		} else {
254
			FrmForm::update( $id, $values );
255
			$message = __( 'Form was successfully updated.', 'formidable' );
256
257
			if ( self::is_too_long( $values ) ) {
258
				$message .= '<br/> ' . sprintf(
259
					/* translators: %1$s: Start link HTML, %2$s: end link HTML */
260
					__( 'However, your form is very long and may be %1$sreaching server limits%2$s.', 'formidable' ),
261
					'<a href="https://formidableforms.com/knowledgebase/i-have-a-long-form-why-did-the-options-at-the-end-of-the-form-stop-saving/?utm_source=WordPress&utm_medium=builder&utm_campaign=liteplugin" target="_blank" rel="noopener">',
262
					'</a>'
263
				);
264
			}
265
266
			if ( defined( 'DOING_AJAX' ) ) {
267
				wp_die( FrmAppHelper::kses( $message, array( 'a' ) ) ); // WPCS: XSS ok.
268
			}
269
270
			return self::get_edit_vars( $id, array(), $message );
271
		}
272
	}
273
274
	/**
275
	 * Check if the value at the end of the form was included.
276
	 * If it's missing, it means other values at the end of the form
277
	 * were likely not saved either.
278
	 *
279
	 * @since 3.06.01
280
	 */
281
	private static function is_too_long( $values ) {
282
		return ( ! isset( $values['frm_end'] ) ) || empty( $values['frm_end'] );
283
	}
284
285
	/**
286
	 * Redirect to the url for creating from a template
287
	 * Also delete the current form
288
	 *
289
	 * @since 2.0
290
	 * @deprecated 3.06
291
	 */
292
	public static function _create_from_template() {
293
		_deprecated_function( __FUNCTION__, '3.06' );
294
295
		FrmAppHelper::permission_check( 'frm_edit_forms' );
296
		check_ajax_referer( 'frm_ajax', 'nonce' );
297
298
		$current_form = FrmAppHelper::get_param( 'this_form', '', 'get', 'absint' );
299
		$template_id  = FrmAppHelper::get_param( 'id', '', 'get', 'absint' );
300
301
		if ( $current_form ) {
302
			FrmForm::destroy( $current_form );
303
		}
304
305
		echo esc_url_raw( admin_url( 'admin.php?page=formidable&frm_action=duplicate&id=' . absint( $template_id ) ) );
306
		wp_die();
307
	}
308
309
	public static function duplicate() {
310
		FrmAppHelper::permission_check( 'frm_edit_forms' );
311
312
		$params  = FrmForm::list_page_params();
313
		$form    = FrmForm::duplicate( $params['id'], $params['template'], true );
314
		$message = $params['template'] ? __( 'Form template was Successfully Created', 'formidable' ) : __( 'Form was Successfully Copied', 'formidable' );
315
		if ( $form ) {
316
			return self::get_edit_vars( $form, array(), $message, true );
317
		} else {
318
			return self::display_forms_list( $params, __( 'There was a problem creating the new template.', 'formidable' ) );
319
		}
320
	}
321
322
	public static function page_preview() {
323
		$params = FrmForm::list_page_params();
324
		if ( ! $params['form'] ) {
325
			return;
326
		}
327
328
		$form = FrmForm::getOne( $params['form'] );
329
		if ( $form ) {
330
			return self::show_form( $form->id, '', true, true );
331
		}
332
	}
333
334
	/**
335
	 * @since 3.0
336
	 */
337
	public static function show_page_preview() {
338
		echo self::page_preview(); // WPCS: XSS ok.
339
	}
340
341
	public static function preview() {
342
		do_action( 'frm_wp' );
343
344
		global $frm_vars;
345
		$frm_vars['preview'] = true;
346
347
		self::load_wp();
348
349
		$include_theme = FrmAppHelper::get_param( 'theme', '', 'get', 'absint' );
350
		if ( $include_theme ) {
351
			self::set_preview_query();
352
			self::load_theme_preview();
353
		} else {
354
			self::load_direct_preview();
355
		}
356
357
		wp_die();
358
	}
359
360
	/**
361
	 * @since 3.0
362
	 */
363
	private static function load_wp() {
364
		if ( ! defined( 'ABSPATH' ) && ! defined( 'XMLRPC_REQUEST' ) ) {
365
			global $wp;
366
			$root = dirname( dirname( dirname( dirname( __FILE__ ) ) ) );
367
			include_once( $root . '/wp-config.php' );
368
			$wp->init();
369
			$wp->register_globals();
370
		}
371
	}
372
373
	private static function set_preview_query() {
374
		$random_page = get_posts(
375
			array(
376
				'numberposts' => 1,
377
				'orderby'     => 'date',
378
				'order'       => 'ASC',
379
				'post_type'   => 'page',
380
			)
381
		);
382
383
		if ( ! empty( $random_page ) ) {
384
			$random_page = reset( $random_page );
385
			query_posts(
386
				array(
387
					'post_type' => 'page',
388
					'page_id'   => $random_page->ID,
389
				)
390
			);
391
		}
392
	}
393
394
	/**
395
	 * @since 3.0
396
	 */
397
	private static function load_theme_preview() {
398
		add_filter( 'wp_title', 'FrmFormsController::preview_title', 9999 );
399
		add_filter( 'the_title', 'FrmFormsController::preview_page_title', 9999 );
400
		add_filter( 'the_content', 'FrmFormsController::preview_content', 9999 );
401
		add_action( 'loop_no_results', 'FrmFormsController::show_page_preview' );
402
		add_filter( 'is_active_sidebar', '__return_false' );
403
		FrmStylesController::enqueue_css( 'enqueue', true );
404
		get_template_part( 'page' );
405
	}
406
407
	/**
408
	 * Set the page title for the theme preview page
409
	 *
410
	 * @since 3.0
411
	 */
412
	public static function preview_page_title( $title ) {
413
		if ( in_the_loop() ) {
414
			$title = self::preview_title( $title );
415
		}
416
417
		return $title;
418
	}
419
420
	/**
421
	 * Set the page title for the theme preview page
422
	 *
423
	 * @since 3.0
424
	 */
425
	public static function preview_title( $title ) {
426
		return __( 'Form Preview', 'formidable' );
427
	}
428
429
	/**
430
	 * Set the page content for the theme preview page
431
	 *
432
	 * @since 3.0
433
	 */
434
	public static function preview_content( $content ) {
435
		if ( in_the_loop() ) {
436
			$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...
437
		}
438
439
		return $content;
440
	}
441
442
	/**
443
	 * @since 3.0
444
	 */
445
	private static function load_direct_preview() {
446
		header( 'Content-Type: text/html; charset=' . get_option( 'blog_charset' ) );
447
448
		$key = FrmAppHelper::simple_get( 'form', 'sanitize_title' );
449
		if ( $key == '' ) {
450
			$key = FrmAppHelper::get_post_param( 'form', '', 'sanitize_title' );
451
		}
452
453
		$form = FrmForm::getAll( array( 'form_key' => $key ), '', 1 );
454
		if ( empty( $form ) ) {
455
			$form = FrmForm::getAll( array(), '', 1 );
456
		}
457
458
		require( FrmAppHelper::plugin_path() . '/classes/views/frm-entries/direct.php' );
459
	}
460
461
	public static function untrash() {
462
		self::change_form_status( 'untrash' );
463
	}
464
465
	public static function bulk_untrash( $ids ) {
466
		FrmAppHelper::permission_check( 'frm_edit_forms' );
467
468
		$count = FrmForm::set_status( $ids, 'published' );
469
470
		/* translators: %1$s: Number of forms */
471
		$message = sprintf( _n( '%1$s form restored from the Trash.', '%1$s forms restored from the Trash.', $count, 'formidable' ), 1 );
472
473
		return $message;
474
	}
475
476
	/**
477
	 * @since 3.06
478
	 */
479
	public static function ajax_trash() {
480
		FrmAppHelper::permission_check( 'frm_delete_forms' );
481
		check_ajax_referer( 'frm_ajax', 'nonce' );
482
		$form_id = FrmAppHelper::get_param( 'id', '', 'post', 'absint' );
483
		FrmForm::set_status( $form_id, 'trash' );
484
		wp_die();
485
	}
486
487
	public static function trash() {
488
		self::change_form_status( 'trash' );
489
	}
490
491
	/**
492
	 * @param string $status
493
	 *
494
	 * @return int The number of forms changed
495
	 */
496
	public static function change_form_status( $status ) {
497
		$available_status = array(
498
			'untrash' => array(
499
				'permission' => 'frm_edit_forms',
500
				'new_status' => 'published',
501
			),
502
			'trash'   => array(
503
				'permission' => 'frm_delete_forms',
504
				'new_status' => 'trash',
505
			),
506
		);
507
508
		if ( ! isset( $available_status[ $status ] ) ) {
509
			return;
510
		}
511
512
		FrmAppHelper::permission_check( $available_status[ $status ]['permission'] );
513
514
		$params = FrmForm::list_page_params();
515
516
		//check nonce url
517
		check_admin_referer( $status . '_form_' . $params['id'] );
518
519
		$count = 0;
520
		if ( FrmForm::set_status( $params['id'], $available_status[ $status ]['new_status'] ) ) {
521
			$count ++;
522
		}
523
524
		$form_type = FrmAppHelper::get_simple_request(
525
			array(
526
				'param' => 'form_type',
527
				'type'  => 'request',
528
			)
529
		);
530
531
		/* translators: %1$s: Number of forms */
532
		$available_status['untrash']['message'] = sprintf( _n( '%1$s form restored from the Trash.', '%1$s forms restored from the Trash.', $count, 'formidable' ), $count );
533
534
		/* translators: %1$s: Number of forms, %2$s: Start link HTML, %3$s: End link HTML */
535
		$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>' );
536
537
		$message = $available_status[ $status ]['message'];
538
539
		self::display_forms_list( $params, $message );
540
	}
541
542
	public static function bulk_trash( $ids ) {
543
		FrmAppHelper::permission_check( 'frm_delete_forms' );
544
545
		$count = 0;
546
		foreach ( $ids as $id ) {
547
			if ( FrmForm::trash( $id ) ) {
548
				$count ++;
549
			}
550
		}
551
552
		$current_page = FrmAppHelper::get_simple_request(
553
			array(
554
				'param' => 'form_type',
555
				'type'  => 'request',
556
			)
557
		);
558
		$message      = sprintf(
559
			/* translators: %1$s: Number of forms, %2$s: Start link HTML, %3$s: End link HTML */
560
			_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' ),
561
			$count,
562
			'<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' ) ) . '">',
563
			'</a>'
564
		);
565
566
		return $message;
567
	}
568
569
	public static function destroy() {
570
		FrmAppHelper::permission_check( 'frm_delete_forms' );
571
572
		$params = FrmForm::list_page_params();
573
574
		// Check nonce url.
575
		check_admin_referer( 'destroy_form_' . $params['id'] );
576
577
		$count = 0;
578
		if ( FrmForm::destroy( $params['id'] ) ) {
579
			$count ++;
580
		}
581
582
		/* translators: %1$s: Number of forms */
583
		$message = sprintf( _n( '%1$s Form Permanently Deleted', '%1$s Forms Permanently Deleted', $count, 'formidable' ), $count );
584
585
		self::display_forms_list( $params, $message );
586
	}
587
588
	public static function bulk_destroy( $ids ) {
589
		FrmAppHelper::permission_check( 'frm_delete_forms' );
590
591
		$count = 0;
592
		foreach ( $ids as $id ) {
593
			$d = FrmForm::destroy( $id );
594
			if ( $d ) {
595
				$count ++;
596
			}
597
		}
598
599
		/* translators: %1$s: Number of forms */
600
		$message = sprintf( _n( '%1$s form permanently deleted.', '%1$s forms permanently deleted.', $count, 'formidable' ), $count );
601
602
		return $message;
603
	}
604
605
	private static function delete_all() {
606
		// Check nonce url.
607
		$permission_error = FrmAppHelper::permission_nonce_error( 'frm_delete_forms', '_wpnonce', 'bulk-toplevel_page_formidable' );
608
		if ( $permission_error !== false ) {
609
			self::display_forms_list( array(), '', array( $permission_error ) );
610
611
			return;
612
		}
613
614
		$count   = FrmForm::scheduled_delete( time() );
615
616
		/* translators: %1$s: Number of forms */
617
		$message = sprintf( _n( '%1$s form permanently deleted.', '%1$s forms permanently deleted.', $count, 'formidable' ), $count );
618
619
		self::display_forms_list( array(), $message );
620
	}
621
622
	/**
623
	 * Create a new form from the modal.
624
	 *
625
	 * @since 4.0
626
	 */
627
	public static function build_new_form() {
628
		global $wpdb;
629
630
		FrmAppHelper::permission_check( 'frm_edit_forms' );
631
		check_ajax_referer( 'frm_ajax', 'nonce' );
632
633
		$new_values             = self::get_modal_values();
634
		$new_values['form_key'] = $new_values['name'];
635
636
		$form_id = FrmForm::create( $new_values );
637
638
		self::create_default_email_action( $form_id );
639
640
		$response = array(
641
			'redirect' => FrmForm::get_edit_link( $form_id ),
0 ignored issues
show
Bug introduced by
It seems like $form_id defined by \FrmForm::create($new_values) on line 636 can also be of type boolean; however, FrmForm::get_edit_link() does only seem to accept integer, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
642
		);
643
644
		echo wp_json_encode( $response );
645
		wp_die();
646
	}
647
648
	/**
649
	 * Create a custom template from a form
650
	 *
651
	 * @since 3.06
652
	 */
653
	public static function build_template() {
654
		global $wpdb;
655
656
		FrmAppHelper::permission_check( 'frm_edit_forms' );
657
		check_ajax_referer( 'frm_ajax', 'nonce' );
658
659
		$form_id     = FrmAppHelper::get_param( 'xml', '', 'post', 'absint' );
660
		$new_form_id = FrmForm::duplicate( $form_id, 1, true );
661
		if ( empty( $new_form_id ) ) {
662
			$response = array(
663
				'message' => __( 'There was an error creating a template.', 'formidable' ),
664
			);
665
		} else {
666
			$new_values    = self::get_modal_values();
667
			$query_results = $wpdb->update( $wpdb->prefix . 'frm_forms', $new_values, array( 'id' => $new_form_id ) );
668
			if ( $query_results ) {
669
				FrmForm::clear_form_cache();
670
			}
671
672
			$response = array(
673
				'redirect' => admin_url( 'admin.php?page=formidable&frm_action=list_templates' ),
674
			);
675
		}
676
677
		echo wp_json_encode( $response );
678
		wp_die();
679
	}
680
681
	/**
682
	 * Before creating a new form, get the name and description from the modal.
683
	 *
684
	 * @since 4.0
685
	 */
686
	private static function get_modal_values() {
687
		$name = FrmAppHelper::get_param( 'name', '', 'post', 'sanitize_text_field' );
688
		$desc = FrmAppHelper::get_param( 'desc', '', 'post', 'sanitize_textarea_field' );
689
690
		return array(
691
			'name'        => $name,
692
			'description' => $desc,
693
		);
694
	}
695
696
	/**
697
	 * Inserts Formidable button
698
	 * Hook exists since 2.5.0
699
	 *
700
	 * @since 2.0.15
701
	 */
702
	public static function insert_form_button() {
703
		if ( current_user_can( 'frm_view_forms' ) ) {
704
			FrmAppHelper::load_admin_wide_js();
705
			$menu_name = FrmAppHelper::get_menu_name();
706
			$icon      = apply_filters( 'frm_media_icon', FrmAppHelper::svg_logo() );
707
			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' ) . '">' .
708
				FrmAppHelper::kses( $icon, 'all' ) .
709
				' ' . esc_html( $menu_name ) . '</a>'; // WPCS: XSS ok.
710
		}
711
	}
712
713
	public static function insert_form_popup() {
714
		$page = basename( FrmAppHelper::get_server_value( 'PHP_SELF' ) );
715
		if ( ! in_array( $page, array( 'post.php', 'page.php', 'page-new.php', 'post-new.php' ) ) ) {
716
			return;
717
		}
718
719
		FrmAppHelper::load_admin_wide_js();
720
721
		$shortcodes = array(
722
			'formidable' => array(
723
				'name'  => __( 'Form', 'formidable' ),
724
				'label' => __( 'Insert a Form', 'formidable' ),
725
			),
726
		);
727
728
		$shortcodes = apply_filters( 'frm_popup_shortcodes', $shortcodes );
729
730
		include( FrmAppHelper::plugin_path() . '/classes/views/frm-forms/insert_form_popup.php' );
731
	}
732
733
	public static function get_shortcode_opts() {
734
		FrmAppHelper::permission_check( 'frm_view_forms' );
735
		check_ajax_referer( 'frm_ajax', 'nonce' );
736
737
		$shortcode = FrmAppHelper::get_post_param( 'shortcode', '', 'sanitize_text_field' );
738
		if ( empty( $shortcode ) ) {
739
			wp_die();
740
		}
741
742
		echo '<div id="sc-opts-' . esc_attr( $shortcode ) . '" class="frm_shortcode_option">';
743
		echo '<input type="radio" name="frmsc" value="' . esc_attr( $shortcode ) . '" id="sc-' . esc_attr( $shortcode ) . '" class="frm_hidden" />';
744
745
		$form_id = '';
746
		$opts    = array();
747
		switch ( $shortcode ) {
748
			case 'formidable':
749
				$opts = array(
750
					'form_id'     => 'id',
751
					'title'       => array(
752
						'val'   => 1,
753
						'label' => __( 'Display form title', 'formidable' ),
754
					),
755
					'description' => array(
756
						'val'   => 1,
757
						'label' => __( 'Display form description', 'formidable' ),
758
					),
759
					'minimize'    => array(
760
						'val'   => 1,
761
						'label' => __( 'Minimize form HTML', 'formidable' ),
762
					),
763
				);
764
		}
765
		$opts = apply_filters( 'frm_sc_popup_opts', $opts, $shortcode );
766
767
		if ( isset( $opts['form_id'] ) && is_string( $opts['form_id'] ) ) {
768
			// allow other shortcodes to use the required form id option
769
			$form_id = $opts['form_id'];
770
			unset( $opts['form_id'] );
771
		}
772
773
		include( FrmAppHelper::plugin_path() . '/classes/views/frm-forms/shortcode_opts.php' );
774
775
		echo '</div>';
776
777
		wp_die();
778
	}
779
780
	public static function display_forms_list( $params = array(), $message = '', $errors = array() ) {
781
		FrmAppHelper::permission_check( 'frm_view_forms' );
782
783
		global $wpdb, $frm_vars;
784
785
		if ( empty( $params ) ) {
786
			$params = FrmForm::list_page_params();
787
		}
788
789
		$wp_list_table = new FrmFormsListHelper( compact( 'params' ) );
790
791
		$pagenum = $wp_list_table->get_pagenum();
792
793
		$wp_list_table->prepare_items();
794
795
		$total_pages = $wp_list_table->get_pagination_arg( 'total_pages' );
796
		if ( $pagenum > $total_pages && $total_pages > 0 ) {
797
			wp_redirect( esc_url_raw( add_query_arg( 'paged', $total_pages ) ) );
798
			die();
799
		}
800
801
		require( FrmAppHelper::plugin_path() . '/classes/views/frm-forms/list.php' );
802
	}
803
804
	public static function get_columns( $columns ) {
805
		$columns['cb'] = '<input type="checkbox" />';
806
		$columns['id'] = 'ID';
807
808
		$type = FrmAppHelper::get_simple_request(
809
			array(
810
				'param'   => 'form_type',
811
				'type'    => 'request',
812
				'default' => 'published',
813
			)
814
		);
815
816
		if ( 'template' == $type ) {
817
			$columns['name']     = __( 'Template Name', 'formidable' );
818
			$columns['type']     = __( 'Type', 'formidable' );
819
			$columns['form_key'] = __( 'Key', 'formidable' );
820
		} else {
821
			$columns['name']      = __( 'Form Title', 'formidable' );
822
			$columns['entries']   = __( 'Entries', 'formidable' );
823
			$columns['form_key']  = __( 'Key', 'formidable' );
824
			$columns['shortcode'] = __( 'Shortcodes', 'formidable' );
825
		}
826
827
		$columns['created_at'] = __( 'Date', 'formidable' );
828
829
		add_screen_option(
830
			'per_page',
831
			array(
832
				'label'   => __( 'Forms', 'formidable' ),
833
				'default' => 20,
834
				'option'  => 'formidable_page_formidable_per_page',
835
			)
836
		);
837
838
		return $columns;
839
	}
840
841
	public static function get_sortable_columns() {
842
		return array(
843
			'id'          => 'id',
844
			'name'        => 'name',
845
			'description' => 'description',
846
			'form_key'    => 'form_key',
847
			'created_at'  => 'created_at',
848
		);
849
	}
850
851
	public static function hidden_columns( $hidden_columns ) {
852
		$type = FrmAppHelper::get_simple_request(
853
			array(
854
				'param' => 'form_type',
855
				'type'  => 'request',
856
			)
857
		);
858
859
		if ( $type === 'template' ) {
860
			$hidden_columns[] = 'id';
861
			$hidden_columns[] = 'form_key';
862
		}
863
864
		return $hidden_columns;
865
	}
866
867
	public static function save_per_page( $save, $option, $value ) {
868
		if ( $option == 'formidable_page_formidable_per_page' ) {
869
			$save = (int) $value;
870
		}
871
872
		return $save;
873
	}
874
875
	/**
876
	 * Show the template listing page
877
	 *
878
	 * @since 3.06
879
	 */
880
	private static function list_templates() {
881
		self::init_modal();
882
883
		$where = apply_filters( 'frm_forms_dropdown', array(), '' );
884
		$forms = FrmForm::get_published_forms( $where );
885
886
		$api       = new FrmFormTemplateApi();
887
		$templates = $api->get_api_info();
888
889
		$custom_templates = array();
890
		self::add_user_templates( $custom_templates );
891
892
		$error   = '';
893
		$expired = false;
894
		$license_type = '';
895
		if ( isset( $templates['error'] ) ) {
896
			$error   = $templates['error']['message'];
897
			$error   = str_replace( 'utm_medium=addons', 'utm_medium=form-templates', $error );
898
			$expired = ( $templates['error']['code'] === 'expired' );
899
900
			$license_type = isset( $templates['error']['type'] ) ? $templates['error']['type'] : '';
901
			unset( $templates['error'] );
902
		}
903
904
		$pricing = FrmAppHelper::admin_upgrade_link( 'form-templates' );
905
906
		$categories = self::get_template_categories( $templates );
907
908
		require( FrmAppHelper::plugin_path() . '/classes/views/frm-forms/list-templates.php' );
909
	}
910
911
	/**
912
	 * @since 4.03.01
913
	 */
914
	private static function get_template_categories( $templates ) {
915
		$categories = array();
916
		foreach ( $templates as $template ) {
917
			if ( isset( $template['categories'] ) ) {
918
				$categories = array_merge( $categories, $template['categories'] );
919
			}
920
		}
921
		$exclude_cats = FrmFormsHelper::ignore_template_categories();
922
		$categories = array_unique( $categories );
923
		$categories = array_diff( $categories, $exclude_cats );
924
		sort( $categories );
925
		return $categories;
926
	}
927
928
	private static function add_user_templates( &$templates ) {
929
		$user_templates = array(
930
			'is_template'      => 1,
931
			'default_template' => 0,
932
		);
933
		$user_templates = FrmForm::getAll( $user_templates, 'name' );
934
		foreach ( $user_templates as $template ) {
935
			$template = array(
936
				'id'          => $template->id,
937
				'name'        => $template->name,
938
				'key'         => $template->form_key,
939
				'description' => $template->description,
940
				'url'         => admin_url( 'admin.php?page=formidable&frm_action=duplicate&id=' . absint( $template->id ) ),
941
				'released'    => $template->created_at,
942
				'installed'   => 1,
943
			);
944
			array_unshift( $templates, $template );
945
			unset( $template );
946
		}
947
	}
948
949
	private static function get_edit_vars( $id, $errors = array(), $message = '', $create_link = false ) {
950
		global $frm_vars;
951
952
		$form = FrmForm::getOne( $id );
953
		if ( ! $form ) {
954
			wp_die( esc_html__( 'You are trying to edit a form that does not exist.', 'formidable' ) );
955
		}
956
957
		if ( $form->parent_form_id ) {
958
			/* translators: %1$s: Start link HTML, %2$s: End link HTML */
959
			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( FrmForm::get_edit_link( $form->parent_form_id ) ) . '">', '</a>' ) );
960
		}
961
962
		$frm_field_selection = FrmField::field_selection();
963
964
		$fields = FrmField::get_all_for_form( $form->id );
965
966
		// Automatically add end section fields if they don't exist (2.0 migration).
967
		$reset_fields = false;
968
		FrmFormsHelper::auto_add_end_section_fields( $form, $fields, $reset_fields );
969
970
		if ( $reset_fields ) {
971
			$fields = FrmField::get_all_for_form( $form->id, '', 'exclude' );
972
		}
973
974
		unset( $end_section_values, $last_order, $open, $reset_fields );
975
976
		$args             = array( 'parent_form_id' => $form->id );
977
		$values           = FrmAppHelper::setup_edit_vars( $form, 'forms', '', true, array(), $args );
978
		$values['fields'] = $fields;
979
980
		$edit_message = __( 'Form was successfully updated.', 'formidable' );
981
		if ( $form->is_template && $message == $edit_message ) {
982
			$message = __( 'Template was successfully updated.', 'formidable' );
983
		}
984
985
		$all_templates = FrmForm::getAll( array( 'is_template' => 1 ), 'name' );
986
		$has_fields    = isset( $values['fields'] ) && ! empty( $values['fields'] );
987
988
		if ( defined( 'DOING_AJAX' ) ) {
989
			wp_die();
990
		} else {
991
			require( FrmAppHelper::plugin_path() . '/classes/views/frm-forms/edit.php' );
992
		}
993
	}
994
995
	public static function get_settings_vars( $id, $errors = array(), $args = array() ) {
996
		if ( ! is_array( $args ) ) {
997
			$message = $args;
998
		} else {
999
			$defaults = array(
1000
				'message' => '',
1001
				'warnings' => array(),
1002
			);
1003
			$args = array_merge( $defaults, $args );
1004
			$message = $args['message'];
1005
			$warnings = $args['warnings'];
1006
		}
1007
1008
		FrmAppHelper::permission_check( 'frm_edit_forms' );
1009
1010
		global $frm_vars;
1011
1012
		$form   = FrmForm::getOne( $id );
1013
		$fields = FrmField::get_all_for_form( $id );
1014
		$values = FrmAppHelper::setup_edit_vars( $form, 'forms', $fields, true );
1015
1016
		self::clean_submit_html( $values );
1017
1018
		$sections = self::get_settings_tabs( $values );
0 ignored issues
show
Bug introduced by
It seems like $values defined by \FrmAppHelper::setup_edi...'forms', $fields, true) on line 1014 can also be of type boolean; however, FrmFormsController::get_settings_tabs() does only seem to accept array, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
1019
		$current  = FrmAppHelper::simple_get( 't', 'sanitize_title', 'advanced_settings' );
1020
1021
		require( FrmAppHelper::plugin_path() . '/classes/views/frm-forms/settings.php' );
1022
	}
1023
1024
	/**
1025
	 * @since 4.0
1026
	 */
1027
	public static function form_publish_button( $atts ) {
1028
		$values = $atts['values'];
1029
		include( FrmAppHelper::plugin_path() . '/classes/views/frm-forms/_publish_box.php' );
1030
	}
1031
1032
	/**
1033
	 * Get a list of all the settings tabs for the form settings page.
1034
	 *
1035
	 * @since 4.0
1036
	 *
1037
	 * @param array $values
1038
	 * @return array
1039
	 */
1040
	private static function get_settings_tabs( $values ) {
1041
		$sections = array(
1042
			'advanced'    => array(
1043
				'name'     => __( 'General', 'formidable' ),
1044
				'title'    => __( 'General Form Settings', 'formidable' ),
1045
				'function' => array( __CLASS__, 'advanced_settings' ),
1046
				'icon'     => 'frm_icon_font frm_settings_icon',
1047
			),
1048
			'email'       => array(
1049
				'name'     => __( 'Actions & Notifications', 'formidable' ),
1050
				'function' => array( 'FrmFormActionsController', 'email_settings' ),
1051
				'id'       => 'frm_notification_settings',
1052
				'icon'     => 'frm_icon_font frm_mail_bulk_icon',
1053
			),
1054
			'permissions' => array(
1055
				'name'     => __( 'Form Permissions', 'formidable' ),
1056
				'icon'     => 'frm_icon_font frm_lock_icon',
1057
				'html_class' => 'frm_show_upgrade frm_noallow',
1058
				'data'     => array(
1059
					'medium'  => 'permissions',
1060
					'upgrade' => __( 'Form Permissions', 'formidable' ),
1061
				),
1062
			),
1063
			'scheduling' => array(
1064
				'name'     => __( 'Form Scheduling', 'formidable' ),
1065
				'icon'     => 'frm_icon_font frm_calendar_icon',
1066
				'html_class' => 'frm_show_upgrade frm_noallow',
1067
				'data'     => array(
1068
					'medium'  => 'scheduling',
1069
					'upgrade' => __( 'Form scheduling settings', 'formidable' ),
1070
				),
1071
			),
1072
			'buttons'     => array(
1073
				'name'     => __( 'Styling & Buttons', 'formidable' ),
1074
				'class'    => __CLASS__,
1075
				'function' => 'buttons_settings',
1076
				'icon'     => 'frm_icon_font frm_pallet_icon',
1077
			),
1078
			'html'        => array(
1079
				'name'     => __( 'Customize HTML', 'formidable' ),
1080
				'class'    => __CLASS__,
1081
				'function' => 'html_settings',
1082
				'icon'     => 'frm_icon_font frm_code_icon',
1083
			),
1084
		);
1085
1086
		$sections = apply_filters( 'frm_add_form_settings_section', $sections, $values );
1087
1088
		if ( FrmAppHelper::pro_is_installed() && ! FrmAppHelper::meets_min_pro_version( '4.0' ) ) {
1089
			// Prevent settings from showing in 2 spots.
1090
			unset( $sections['permissions'], $sections['scheduling'] );
1091
		}
1092
1093
		foreach ( $sections as $key => $section ) {
1094
			$defaults = array(
1095
				'html_class' => '',
1096
				'name'       => ucfirst( $key ),
1097
				'icon'       => 'frm_icon_font frm_settings_icon',
1098
			);
1099
1100
			$section = array_merge( $defaults, $section );
1101
1102
			if ( ! isset( $section['anchor'] ) ) {
1103
				$section['anchor'] = $key;
1104
			}
1105
			$section['anchor'] .= '_settings';
1106
1107
			if ( ! isset( $section['title'] ) ) {
1108
				$section['title'] = $section['name'];
1109
			}
1110
1111
			if ( ! isset( $section['id'] ) ) {
1112
				$section['id'] = $section['anchor'];
1113
			}
1114
1115
			$sections[ $key ] = $section;
1116
		}
1117
1118
		return $sections;
1119
	}
1120
1121
	/**
1122
	 * @since 4.0
1123
	 *
1124
	 * @param array $values
1125
	 */
1126
	public static function advanced_settings( $values ) {
1127
		$first_h3 = 'frm_first_h3';
1128
1129
		include( FrmAppHelper::plugin_path() . '/classes/views/frm-forms/settings-advanced.php' );
1130
	}
1131
1132
	/**
1133
	 * @since 4.0
1134
	 *
1135
	 * @param array $values
1136
	 */
1137
	public static function buttons_settings( $values ) {
1138
		$styles = apply_filters( 'frm_get_style_opts', array() );
1139
1140
		$frm_settings    = FrmAppHelper::get_settings();
1141
		$no_global_style = $frm_settings->load_style === 'none';
1142
1143
		include( FrmAppHelper::plugin_path() . '/classes/views/frm-forms/settings-buttons.php' );
1144
	}
1145
1146
	/**
1147
	 * @since 4.0
1148
	 *
1149
	 * @param array $values
1150
	 */
1151
	public static function html_settings( $values ) {
1152
		include( FrmAppHelper::plugin_path() . '/classes/views/frm-forms/settings-html.php' );
1153
	}
1154
1155
	/**
1156
	 * Replace old Submit Button href with new href to avoid errors in Chrome
1157
	 *
1158
	 * @since 2.03.08
1159
	 *
1160
	 * @param array|boolean $values
1161
	 */
1162
	private static function clean_submit_html( &$values ) {
1163
		if ( is_array( $values ) && isset( $values['submit_html'] ) ) {
1164
			$values['submit_html'] = str_replace( 'javascript:void(0)', '#', $values['submit_html'] );
1165
		}
1166
	}
1167
1168
	public static function mb_tags_box( $form_id, $class = '' ) {
1169
		$fields       = FrmField::get_all_for_form( $form_id, '', 'include' );
1170
		$linked_forms = array();
1171
		$col          = 'one';
1172
		$settings_tab = FrmAppHelper::is_admin_page( 'formidable' ) ? true : false;
1173
1174
		$cond_shortcodes  = apply_filters( 'frm_conditional_shortcodes', array() );
1175
		$entry_shortcodes = self::get_shortcode_helpers( $settings_tab );
1176
1177
		$advanced_helpers = self::advanced_helpers( compact( 'fields', 'form_id' ) );
1178
1179
		include( FrmAppHelper::plugin_path() . '/classes/views/shared/mb_adv_info.php' );
1180
	}
1181
1182
	/**
1183
	 * @since 3.04.01
1184
	 */
1185
	private static function advanced_helpers( $atts ) {
1186
		$advanced_helpers = array(
1187
			'default' => array(
1188
				'heading' => __( 'Customize field values with the following parameters.', 'formidable' ),
1189
				'codes'   => self::get_advanced_shortcodes(),
1190
			),
1191
		);
1192
1193
		$user_fields = self::user_shortcodes();
1194
		if ( ! empty( $user_fields ) ) {
1195
			$user_helpers = array();
1196
			foreach ( $user_fields as $uk => $uf ) {
1197
				$user_helpers[ '|user_id| show="' . $uk . '"' ] = $uf;
1198
				unset( $uk, $uf );
1199
			}
1200
1201
			$advanced_helpers['user_id'] = array(
1202
				'codes'   => $user_helpers,
1203
			);
1204
		}
1205
1206
		/**
1207
		 * Add extra helper shortcodes on the Advanced tab in form settings and views
1208
		 *
1209
		 * @since 3.04.01
1210
		 *
1211
		 * @param array $atts - Includes fields and form_id
1212
		 */
1213
		return apply_filters( 'frm_advanced_helpers', $advanced_helpers, $atts );
1214
	}
1215
1216
	/**
1217
	 * Get an array of the options to display in the advanced tab
1218
	 * of the customization panel
1219
	 *
1220
	 * @since 2.0.6
1221
	 */
1222
	private static function get_advanced_shortcodes() {
1223
		$adv_shortcodes = array(
1224
			'x sep=", "'           => array(
1225
				'label' => __( 'Separator', 'formidable' ),
1226
				'title' => __( 'Use a different separator for checkbox fields', 'formidable' ),
1227
			),
1228
			'x format="d-m-Y"'     => array(
1229
				'label' => __( 'Date Format', 'formidable' ),
1230
			),
1231
			'x show="field_label"' => array(
1232
				'label' => __( 'Field Label', 'formidable' ),
1233
			),
1234
			'x wpautop=0'          => array(
1235
				'label' => __( 'No Auto P', 'formidable' ),
1236
				'title' => __( 'Do not automatically add any paragraphs or line breaks', 'formidable' ),
1237
			),
1238
		);
1239
		$adv_shortcodes = apply_filters( 'frm_advanced_shortcodes', $adv_shortcodes );
1240
1241
		// __( 'Leave blank instead of defaulting to User Login', 'formidable' ) : blank=1
1242
1243
		return $adv_shortcodes;
1244
	}
1245
1246
	/**
1247
	 * @since 3.04.01
1248
	 */
1249
	private static function user_shortcodes() {
1250
		$options = array(
1251
			'ID'           => __( 'User ID', 'formidable' ),
1252
			'first_name'   => __( 'First Name', 'formidable' ),
1253
			'last_name'    => __( 'Last Name', 'formidable' ),
1254
			'display_name' => __( 'Display Name', 'formidable' ),
1255
			'user_login'   => __( 'User Login', 'formidable' ),
1256
			'user_email'   => __( 'Email', 'formidable' ),
1257
			'avatar'       => __( 'Avatar', 'formidable' ),
1258
			'author_link'  => __( 'Author Link', 'formidable' ),
1259
		);
1260
1261
		return apply_filters( 'frm_user_shortcodes', $options );
1262
	}
1263
1264
	/**
1265
	 * Get an array of the helper shortcodes to display in the customization panel
1266
	 *
1267
	 * @since 2.0.6
1268
	 */
1269
	private static function get_shortcode_helpers( $settings_tab ) {
1270
		$entry_shortcodes = array(
1271
			'id'         => __( 'Entry ID', 'formidable' ),
1272
			'key'        => __( 'Entry Key', 'formidable' ),
1273
			'post_id'    => __( 'Post ID', 'formidable' ),
1274
			'ip'         => __( 'User IP', 'formidable' ),
1275
			'created-at' => __( 'Entry created', 'formidable' ),
1276
			'updated-at' => __( 'Entry updated', 'formidable' ),
1277
			''           => '',
1278
			'siteurl'    => __( 'Site URL', 'formidable' ),
1279
			'sitename'   => __( 'Site Name', 'formidable' ),
1280
		);
1281
1282
		if ( ! FrmAppHelper::pro_is_installed() ) {
1283
			unset( $entry_shortcodes['post_id'] );
1284
		}
1285
1286
		if ( $settings_tab ) {
1287
			$entry_shortcodes['default-message'] = __( 'Default Msg', 'formidable' );
1288
			$entry_shortcodes['default-html']    = __( 'Default HTML', 'formidable' );
1289
			$entry_shortcodes['default-plain']   = __( 'Default Plain', 'formidable' );
1290
		}
1291
1292
		/**
1293
		 * Use this hook to add or remove buttons in the helpers section
1294
		 * in the customization panel
1295
		 *
1296
		 * @since 2.0.6
1297
		 */
1298
		$entry_shortcodes = apply_filters( 'frm_helper_shortcodes', $entry_shortcodes, $settings_tab );
1299
1300
		return $entry_shortcodes;
1301
	}
1302
1303
	/**
1304
	 * Insert the form class setting into the form
1305
	 */
1306
	public static function form_classes( $form ) {
1307
		if ( isset( $form->options['form_class'] ) ) {
1308
			echo esc_attr( sanitize_text_field( $form->options['form_class'] ) );
1309
		}
1310
1311
		if ( isset( $form->options['js_validate'] ) && $form->options['js_validate'] ) {
1312
			echo ' frm_js_validate ';
1313
		}
1314
	}
1315
1316
	public static function get_email_html() {
1317
		FrmAppHelper::permission_check( 'frm_view_forms' );
1318
		check_ajax_referer( 'frm_ajax', 'nonce' );
1319
1320
		echo FrmEntriesController::show_entry_shortcode( // WPCS: XSS ok.
1321
			array(
1322
				'form_id'       => FrmAppHelper::get_post_param( 'form_id', '', 'absint' ),
1323
				'default_email' => true,
1324
				'plain_text'    => FrmAppHelper::get_post_param( 'plain_text', '', 'absint' ),
1325
			)
1326
		);
1327
		wp_die();
1328
	}
1329
1330
	public static function filter_content( $content, $form, $entry = false ) {
1331
		self::get_entry_by_param( $entry );
1332
		if ( ! $entry ) {
1333
			return $content;
1334
		}
1335
1336
		if ( is_object( $form ) ) {
1337
			$form = $form->id;
1338
		}
1339
1340
		$shortcodes = FrmFieldsHelper::get_shortcodes( $content, $form );
1341
		$content    = apply_filters( 'frm_replace_content_shortcodes', $content, $entry, $shortcodes );
1342
1343
		return $content;
1344
	}
1345
1346
	private static function get_entry_by_param( &$entry ) {
1347
		if ( ! $entry || ! is_object( $entry ) ) {
1348
			if ( ! $entry || ! is_numeric( $entry ) ) {
1349
				$entry = FrmAppHelper::get_post_param( 'id', false, 'sanitize_title' );
1350
			}
1351
1352
			FrmEntry::maybe_get_entry( $entry );
1353
		}
1354
	}
1355
1356
	public static function replace_content_shortcodes( $content, $entry, $shortcodes ) {
1357
		return FrmFieldsHelper::replace_content_shortcodes( $content, $entry, $shortcodes );
1358
	}
1359
1360
	public static function process_bulk_form_actions( $errors ) {
1361
		if ( ! $_REQUEST ) {
1362
			return $errors;
1363
		}
1364
1365
		$bulkaction = FrmAppHelper::get_param( 'action', '', 'get', 'sanitize_text_field' );
1366
		if ( $bulkaction == - 1 ) {
1367
			$bulkaction = FrmAppHelper::get_param( 'action2', '', 'get', 'sanitize_title' );
1368
		}
1369
1370
		if ( ! empty( $bulkaction ) && strpos( $bulkaction, 'bulk_' ) === 0 ) {
1371
			FrmAppHelper::remove_get_action();
1372
1373
			$bulkaction = str_replace( 'bulk_', '', $bulkaction );
1374
		}
1375
1376
		$ids = FrmAppHelper::get_param( 'item-action', '', 'get', 'sanitize_text_field' );
1377
		if ( empty( $ids ) ) {
1378
			$errors[] = __( 'No forms were specified', 'formidable' );
1379
1380
			return $errors;
1381
		}
1382
1383
		$permission_error = FrmAppHelper::permission_nonce_error( '', '_wpnonce', 'bulk-toplevel_page_formidable' );
1384
		if ( $permission_error !== false ) {
1385
			$errors[] = $permission_error;
1386
1387
			return $errors;
1388
		}
1389
1390
		if ( ! is_array( $ids ) ) {
1391
			$ids = explode( ',', $ids );
1392
		}
1393
1394
		switch ( $bulkaction ) {
1395
			case 'delete':
1396
				$message = self::bulk_destroy( $ids );
1397
				break;
1398
			case 'trash':
1399
				$message = self::bulk_trash( $ids );
1400
				break;
1401
			case 'untrash':
1402
				$message = self::bulk_untrash( $ids );
1403
		}
1404
1405
		if ( isset( $message ) && ! empty( $message ) ) {
1406
			$errors['message'] = $message;
1407
		}
1408
1409
		return $errors;
1410
	}
1411
1412
	public static function route() {
1413
		$action = isset( $_REQUEST['frm_action'] ) ? 'frm_action' : 'action';
1414
		$vars   = array();
1415
		FrmAppHelper::include_svg();
1416
1417
		if ( isset( $_POST['frm_compact_fields'] ) ) {
1418
			FrmAppHelper::permission_check( 'frm_edit_forms' );
1419
1420
			// Javascript needs to be allowed in some field settings.
1421
			// phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
1422
			$json_vars = htmlspecialchars_decode( nl2br( str_replace( '&quot;', '"', wp_unslash( $_POST['frm_compact_fields'] ) ) ) );
1423
			$json_vars = json_decode( $json_vars, true );
1424
			if ( empty( $json_vars ) ) {
1425
				// json decoding failed so we should return an error message.
1426
				$action = FrmAppHelper::get_param( $action, '', 'get', 'sanitize_title' );
1427
				if ( 'edit' == $action ) {
1428
					$action = 'update';
1429
				}
1430
1431
				add_filter( 'frm_validate_form', 'FrmFormsController::json_error' );
1432
			} else {
1433
				$vars   = FrmAppHelper::json_to_array( $json_vars );
1434
				$action = $vars[ $action ];
1435
				unset( $_REQUEST['frm_compact_fields'], $_POST['frm_compact_fields'] );
1436
				$_REQUEST = array_merge( $_REQUEST, $vars );
1437
				$_POST    = array_merge( $_POST, $_REQUEST );
1438
			}
1439
		} else {
1440
			$action = FrmAppHelper::get_param( $action, '', 'get', 'sanitize_title' );
1441
			if ( isset( $_REQUEST['delete_all'] ) ) {
1442
				// Override the action for this page.
1443
				$action = 'delete_all';
1444
			}
1445
		}
1446
1447
		add_action( 'frm_load_form_hooks', 'FrmHooksController::trigger_load_form_hooks' );
1448
		FrmAppHelper::trigger_hook_load( 'form' );
1449
1450
		switch ( $action ) {
1451
			case 'new':
1452
				return self::new_form( $vars );
0 ignored issues
show
Deprecated Code introduced by
The method FrmFormsController::new_form() has been deprecated with message: 4.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...
1453
			case 'add_new':
1454
			case 'list_templates':
1455
				return self::list_templates();
1456
			case 'create':
1457
			case 'edit':
1458
			case 'update':
1459
			case 'duplicate':
1460
			case 'trash':
1461
			case 'untrash':
1462
			case 'destroy':
1463
			case 'delete_all':
1464
			case 'settings':
1465
			case 'update_settings':
1466
				return self::$action( $vars );
1467
			default:
1468
				do_action( 'frm_form_action_' . $action );
1469
				if ( apply_filters( 'frm_form_stop_action_' . $action, false ) ) {
1470
					return;
1471
				}
1472
1473
				$action = FrmAppHelper::get_param( 'action', '', 'get', 'sanitize_text_field' );
1474
				if ( $action == - 1 ) {
1475
					$action = FrmAppHelper::get_param( 'action2', '', 'get', 'sanitize_title' );
1476
				}
1477
1478
				if ( strpos( $action, 'bulk_' ) === 0 ) {
1479
					FrmAppHelper::remove_get_action();
1480
1481
					return self::list_form();
1482
				}
1483
1484
				return self::display_forms_list();
1485
		}
1486
	}
1487
1488
	public static function json_error( $errors ) {
1489
		$errors['json'] = __( 'Abnormal HTML characters prevented your form from saving correctly', 'formidable' );
1490
1491
		return $errors;
1492
	}
1493
1494
	/* FRONT-END FORMS */
1495
	public static function admin_bar_css() {
1496
		if ( is_admin() || ! current_user_can( 'frm_edit_forms' ) ) {
1497
			return;
1498
		}
1499
1500
		add_action( 'wp_before_admin_bar_render', 'FrmFormsController::admin_bar_configure' );
1501
		FrmAppHelper::load_font_style();
1502
	}
1503
1504
	public static function admin_bar_configure() {
1505
		global $frm_vars;
1506
		if ( empty( $frm_vars['forms_loaded'] ) ) {
1507
			return;
1508
		}
1509
1510
		$actions = array();
1511
		foreach ( $frm_vars['forms_loaded'] as $form ) {
1512
			if ( is_object( $form ) ) {
1513
				$actions[ $form->id ] = $form->name;
1514
			}
1515
			unset( $form );
1516
		}
1517
1518
		if ( empty( $actions ) ) {
1519
			return;
1520
		}
1521
1522
		self::add_menu_to_admin_bar();
1523
		self::add_forms_to_admin_bar( $actions );
1524
	}
1525
1526
	/**
1527
	 * @since 2.05.07
1528
	 */
1529
	public static function add_menu_to_admin_bar() {
1530
		global $wp_admin_bar;
1531
1532
		$wp_admin_bar->add_node(
1533
			array(
1534
				'id'    => 'frm-forms',
1535
				'title' => '<span class="ab-icon"></span><span class="ab-label">' . FrmAppHelper::get_menu_name() . '</span>',
1536
				'href'  => admin_url( 'admin.php?page=formidable' ),
1537
				'meta'  => array(
1538
					'title' => FrmAppHelper::get_menu_name(),
1539
				),
1540
			)
1541
		);
1542
	}
1543
1544
	/**
1545
	 * @since 2.05.07
1546
	 */
1547
	private static function add_forms_to_admin_bar( $actions ) {
1548
		global $wp_admin_bar;
1549
1550
		asort( $actions );
1551
1552
		foreach ( $actions as $form_id => $name ) {
1553
1554
			$wp_admin_bar->add_node(
1555
				array(
1556
					'parent' => 'frm-forms',
1557
					'id'     => 'edit_form_' . $form_id,
1558
					'title'  => empty( $name ) ? __( '(no title)', 'formidable' ) : $name,
1559
					'href'   => FrmForm::get_edit_link( $form_id ),
1560
				)
1561
			);
1562
		}
1563
	}
1564
1565
	/**
1566
	 * The formidable shortcode
1567
	 *
1568
	 * @param array $atts The params from the shortcode.
1569
	 */
1570
	public static function get_form_shortcode( $atts ) {
1571
		global $frm_vars;
1572
		if ( isset( $frm_vars['skip_shortcode'] ) && $frm_vars['skip_shortcode'] ) {
1573
			$sc = '[formidable';
1574
			$sc .= FrmAppHelper::array_to_html_params( $atts );
1575
			return $sc . ']';
1576
		}
1577
1578
		$shortcode_atts = shortcode_atts(
1579
			array(
1580
				'id'             => '',
1581
				'key'            => '',
1582
				'title'          => false,
1583
				'description'    => false,
1584
				'readonly'       => false,
1585
				'entry_id'       => false,
1586
				'fields'         => array(),
1587
				'exclude_fields' => array(),
1588
				'minimize'       => false,
1589
			),
1590
			$atts
1591
		);
1592
		do_action( 'formidable_shortcode_atts', $shortcode_atts, $atts );
1593
1594
		return self::show_form( $shortcode_atts['id'], $shortcode_atts['key'], $shortcode_atts['title'], $shortcode_atts['description'], $atts );
1595
	}
1596
1597
	public static function show_form( $id = '', $key = '', $title = false, $description = false, $atts = array() ) {
1598
		if ( empty( $id ) ) {
1599
			$id = $key;
1600
		}
1601
1602
		$form = self::maybe_get_form_to_show( $id );
1603
		if ( ! $form ) {
1604
			return __( 'Please select a valid form', 'formidable' );
1605
		}
1606
1607
		FrmAppController::maybe_update_styles();
1608
1609
		add_action( 'frm_load_form_hooks', 'FrmHooksController::trigger_load_form_hooks' );
1610
		FrmAppHelper::trigger_hook_load( 'form', $form );
1611
1612
		$form = apply_filters( 'frm_pre_display_form', $form );
1613
1614
		$frm_settings = FrmAppHelper::get_settings( array( 'current_form' => $form->id ) );
1615
1616
		if ( self::is_viewable_draft_form( $form ) ) {
1617
			// don't show a draft form on a page
1618
			$form = __( 'Please select a valid form', 'formidable' );
1619
		} elseif ( self::user_should_login( $form ) ) {
1620
			$form = do_shortcode( $frm_settings->login_msg );
1621
		} elseif ( self::user_has_permission_to_view( $form ) ) {
1622
			$form = do_shortcode( $frm_settings->login_msg );
1623
		} else {
1624
			do_action( 'frm_pre_get_form', $form );
1625
			$form = self::get_form( $form, $title, $description, $atts );
1626
1627
			/**
1628
			 * Use this shortcode to check for external shortcodes that may span
1629
			 * across multiple fields in the customizable HTML
1630
			 *
1631
			 * @since 2.0.8
1632
			 */
1633
			$form = apply_filters( 'frm_filter_final_form', $form );
1634
		}
1635
1636
		return $form;
1637
	}
1638
1639
	private static function maybe_get_form_to_show( $id ) {
1640
		$form = false;
1641
1642
		if ( ! empty( $id ) ) { // no form id or key set
1643
			$form = FrmForm::getOne( $id );
1644
			if ( ! $form || $form->parent_form_id || $form->status == 'trash' ) {
1645
				$form = false;
1646
			}
1647
		}
1648
1649
		return $form;
1650
	}
1651
1652
	private static function is_viewable_draft_form( $form ) {
1653
		return $form->status == 'draft' && current_user_can( 'frm_edit_forms' ) && ! FrmAppHelper::is_preview_page();
1654
	}
1655
1656
	private static function user_should_login( $form ) {
1657
		return $form->logged_in && ! is_user_logged_in();
1658
	}
1659
1660
	private static function user_has_permission_to_view( $form ) {
1661
		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'] );
1662
	}
1663
1664
	public static function get_form( $form, $title, $description, $atts = array() ) {
1665
		ob_start();
1666
1667
		do_action( 'frm_before_get_form', $atts );
1668
1669
		self::get_form_contents( $form, $title, $description, $atts );
1670
		self::enqueue_scripts( FrmForm::get_params( $form ) );
1671
1672
		$contents = ob_get_contents();
1673
		ob_end_clean();
1674
1675
		self::maybe_minimize_form( $atts, $contents );
1676
1677
		return $contents;
1678
	}
1679
1680
	public static function enqueue_scripts( $params ) {
1681
		do_action( 'frm_enqueue_form_scripts', $params );
1682
	}
1683
1684
	public static function get_form_contents( $form, $title, $description, $atts ) {
1685
		$params    = FrmForm::get_params( $form );
1686
		$errors    = self::get_saved_errors( $form, $params );
1687
		$fields    = FrmFieldsHelper::get_form_fields( $form->id, $errors );
1688
		$reset     = false;
1689
		$pass_args = compact( 'form', 'fields', 'errors', 'title', 'description', 'reset' );
1690
1691
		$handle_process_here = $params['action'] == 'create' && $params['posted_form_id'] == $form->id && $_POST;
1692
1693
		if ( ! $handle_process_here ) {
1694
			do_action( 'frm_display_form_action', $params, $fields, $form, $title, $description );
1695
			if ( apply_filters( 'frm_continue_to_new', true, $form->id, $params['action'] ) ) {
1696
				self::show_form_after_submit( $pass_args );
1697
			}
1698
		} elseif ( ! empty( $errors ) ) {
1699
			self::show_form_after_submit( $pass_args );
1700
1701
		} else {
1702
1703
			do_action( 'frm_validate_form_creation', $params, $fields, $form, $title, $description );
1704
1705
			if ( apply_filters( 'frm_continue_to_create', true, $form->id ) ) {
1706
				$entry_id                 = self::just_created_entry( $form->id );
1707
				$pass_args['entry_id']    = $entry_id;
1708
				$pass_args['reset']       = true;
1709
				$pass_args['conf_method'] = self::get_confirmation_method( compact( 'form', 'entry_id' ) );
1710
1711
				self::run_success_action( $pass_args );
1712
1713
				do_action(
1714
					'frm_after_entry_processed',
1715
					array(
1716
						'entry_id' => $entry_id,
1717
						'form'     => $form,
1718
					)
1719
				);
1720
			}
1721
		}
1722
	}
1723
1724
	/**
1725
	 * If the form was processed earlier (init), get the generated errors
1726
	 *
1727
	 * @since 2.05
1728
	 */
1729
	private static function get_saved_errors( $form, $params ) {
1730
		global $frm_vars;
1731
1732
		if ( $params['posted_form_id'] == $form->id && $_POST && isset( $frm_vars['created_entries'][ $form->id ] ) ) {
1733
			$errors = $frm_vars['created_entries'][ $form->id ]['errors'];
1734
		} else {
1735
			$errors = array();
1736
		}
1737
1738
		return $errors;
1739
	}
1740
1741
	/**
1742
	 * @since 2.2.7
1743
	 */
1744
	public static function just_created_entry( $form_id ) {
1745
		global $frm_vars;
1746
1747
		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;
1748
	}
1749
1750
	/**
1751
	 * @since 3.0
1752
	 */
1753
	private static function get_confirmation_method( $atts ) {
1754
		$opt    = 'success_action';
1755
		$method = ( isset( $atts['form']->options[ $opt ] ) && ! empty( $atts['form']->options[ $opt ] ) ) ? $atts['form']->options[ $opt ] : 'message';
1756
		$method = apply_filters( 'frm_success_filter', $method, $atts['form'], 'create' );
1757
1758
		if ( $method != 'message' && ( ! $atts['entry_id'] || ! is_numeric( $atts['entry_id'] ) ) ) {
1759
			$method = 'message';
1760
		}
1761
1762
		return $method;
1763
	}
1764
1765
	public static function maybe_trigger_redirect( $form, $params, $args ) {
1766
		if ( ! isset( $params['id'] ) ) {
1767
			global $frm_vars;
1768
			$params['id'] = $frm_vars['created_entries'][ $form->id ]['entry_id'];
1769
		}
1770
1771
		$conf_method = self::get_confirmation_method(
1772
			array(
1773
				'form'     => $form,
1774
				'entry_id' => $params['id'],
1775
			)
1776
		);
1777
1778
		if ( 'redirect' === $conf_method ) {
1779
			self::trigger_redirect( $form, $params, $args );
1780
		}
1781
	}
1782
1783
	public static function trigger_redirect( $form, $params, $args ) {
1784
		$success_args = array(
1785
			'action'      => $params['action'],
1786
			'conf_method' => 'redirect',
1787
			'form'        => $form,
1788
			'entry_id'    => $params['id'],
1789
		);
1790
1791
		if ( isset( $args['ajax'] ) ) {
1792
			$success_args['ajax'] = $args['ajax'];
1793
		}
1794
1795
		self::run_success_action( $success_args );
1796
	}
1797
1798
	/**
1799
	 * Used when the success action is not 'message'
1800
	 *
1801
	 * @since 2.05
1802
	 */
1803
	public static function run_success_action( $args ) {
1804
		$extra_args = $args;
1805
		unset( $extra_args['form'] );
1806
1807
		do_action( 'frm_success_action', $args['conf_method'], $args['form'], $args['form']->options, $args['entry_id'], $extra_args );
1808
1809
		$opt = ( ! isset( $args['action'] ) || $args['action'] == 'create' ) ? 'success' : 'edit';
1810
1811
		$args['success_opt'] = $opt;
1812
		if ( $args['conf_method'] == 'page' && is_numeric( $args['form']->options[ $opt . '_page_id' ] ) ) {
1813
			self::load_page_after_submit( $args );
1814
		} elseif ( $args['conf_method'] == 'redirect' ) {
1815
			self::redirect_after_submit( $args );
1816
		} else {
1817
			self::show_message_after_save( $args );
1818
		}
1819
	}
1820
1821
	/**
1822
	 * @since 3.0
1823
	 */
1824
	private static function load_page_after_submit( $args ) {
1825
		global $post;
1826
		$opt = $args['success_opt'];
1827
		if ( ! $post || $args['form']->options[ $opt . '_page_id' ] != $post->ID ) {
1828
			$page     = get_post( $args['form']->options[ $opt . '_page_id' ] );
1829
			$old_post = $post;
1830
			$post     = $page;
1831
			$content  = apply_filters( 'frm_content', $page->post_content, $args['form'], $args['entry_id'] );
1832
			echo apply_filters( 'the_content', $content ); // WPCS: XSS ok.
1833
			$post = $old_post;
1834
		}
1835
	}
1836
1837
	/**
1838
	 * @since 3.0
1839
	 */
1840
	private static function redirect_after_submit( $args ) {
1841
		global $frm_vars;
1842
1843
		add_filter( 'frm_use_wpautop', '__return_false' );
1844
1845
		$opt         = $args['success_opt'];
1846
		$success_url = trim( $args['form']->options[ $opt . '_url' ] );
1847
		$success_url = apply_filters( 'frm_content', $success_url, $args['form'], $args['entry_id'] );
1848
		$success_url = do_shortcode( $success_url );
1849
1850
		$success_msg = isset( $args['form']->options[ $opt . '_msg' ] ) ? $args['form']->options[ $opt . '_msg' ] : __( 'Please wait while you are redirected.', 'formidable' );
1851
1852
		$redirect_msg = self::get_redirect_message( $success_url, $success_msg, $args );
1853
1854
		$args['id'] = $args['entry_id'];
1855
		FrmEntriesController::delete_entry_before_redirect( $success_url, $args['form'], $args );
1856
1857
		add_filter( 'frm_redirect_url', 'FrmEntriesController::prepare_redirect_url' );
1858
		$success_url = apply_filters( 'frm_redirect_url', $success_url, $args['form'], $args );
1859
1860
		$doing_ajax = FrmAppHelper::doing_ajax();
1861
1862
		if ( isset( $args['ajax'] ) && $args['ajax'] && $doing_ajax ) {
1863
			echo json_encode( array( 'redirect' => $success_url ) );
1864
			wp_die();
1865
		} elseif ( ! headers_sent() ) {
1866
			wp_redirect( esc_url_raw( $success_url ) );
1867
			die(); // do not use wp_die or redirect fails
1868
		} else {
1869
			add_filter( 'frm_use_wpautop', '__return_true' );
1870
1871
			echo $redirect_msg; // WPCS: XSS ok.
1872
			echo "<script type='text/javascript'>window.onload = function(){setTimeout(window.location='" . esc_url_raw( $success_url ) . "', 8000);}</script>";
1873
		}
1874
	}
1875
1876
	/**
1877
	 * @since 3.0
1878
	 *
1879
	 * @param string $success_url
1880
	 * @param string $success_msg
1881
	 * @param array $args
1882
	 */
1883
	private static function get_redirect_message( $success_url, $success_msg, $args ) {
1884
		$redirect_msg = '<div class="' . esc_attr( FrmFormsHelper::get_form_style_class( $args['form'] ) ) . '"><div class="frm-redirect-msg frm_message">' . $success_msg . '<br/>' .
1885
			/* translators: %1$s: Start link HTML, %2$s: End link HTML */
1886
			sprintf( __( '%1$sClick here%2$s if you are not automatically redirected.', 'formidable' ), '<a href="' . esc_url( $success_url ) . '">', '</a>' ) .
1887
			'</div></div>';
1888
1889
		$redirect_args = array(
1890
			'entry_id' => $args['entry_id'],
1891
			'form_id'  => $args['form']->id,
1892
			'form'     => $args['form'],
1893
		);
1894
1895
		return apply_filters( 'frm_redirect_msg', $redirect_msg, $redirect_args );
1896
	}
1897
1898
	/**
1899
	 * Prepare to show the success message and empty form after submit
1900
	 *
1901
	 * @since 2.05
1902
	 */
1903
	public static function show_message_after_save( $atts ) {
1904
		$atts['message'] = self::prepare_submit_message( $atts['form'], $atts['entry_id'] );
1905
1906
		if ( ! isset( $atts['form']->options['show_form'] ) || $atts['form']->options['show_form'] ) {
1907
			self::show_form_after_submit( $atts );
1908
		} else {
1909
			self::show_lone_success_messsage( $atts );
1910
		}
1911
	}
1912
1913
	/**
1914
	 * Show an empty form
1915
	 *
1916
	 * @since 2.05
1917
	 */
1918
	private static function show_form_after_submit( $args ) {
1919
		self::fill_atts_for_form_display( $args );
1920
1921
		$errors      = $args['errors'];
1922
		$message     = $args['message'];
1923
		$form        = $args['form'];
1924
		$title       = $args['title'];
1925
		$description = $args['description'];
1926
1927
		if ( empty( $args['fields'] ) ) {
1928
			$values = array();
1929
		} else {
1930
			$values = FrmEntriesHelper::setup_new_vars( $args['fields'], $form, $args['reset'] );
1931
		}
1932
		unset( $args );
1933
1934
		$include_form_tag = apply_filters( 'frm_include_form_tag', true, $form );
1935
1936
		$frm_settings = FrmAppHelper::get_settings();
1937
		$submit       = isset( $form->options['submit_value'] ) ? $form->options['submit_value'] : $frm_settings->submit_value;
1938
1939
		global $frm_vars;
1940
		self::maybe_load_css( $form, $values['custom_style'], $frm_vars['load_css'] );
1941
1942
		include( FrmAppHelper::plugin_path() . '/classes/views/frm-entries/new.php' );
1943
	}
1944
1945
	/**
1946
	 * Get all the values needed on the new.php entry page
1947
	 *
1948
	 * @since 2.05
1949
	 */
1950
	private static function fill_atts_for_form_display( &$args ) {
1951
		$defaults = array(
1952
			'errors'      => array(),
1953
			'message'     => '',
1954
			'fields'      => array(),
1955
			'form'        => array(),
1956
			'title'       => true,
1957
			'description' => false,
1958
			'reset'       => false,
1959
		);
1960
		$args     = wp_parse_args( $args, $defaults );
1961
	}
1962
1963
	/**
1964
	 * Show the success message without the form
1965
	 *
1966
	 * @since 2.05
1967
	 */
1968
	private static function show_lone_success_messsage( $atts ) {
1969
		global $frm_vars;
1970
		$values = FrmEntriesHelper::setup_new_vars( $atts['fields'], $atts['form'], true );
1971
		self::maybe_load_css( $atts['form'], $values['custom_style'], $frm_vars['load_css'] );
1972
1973
		$include_extra_container = 'frm_forms' . FrmFormsHelper::get_form_style_class( $values );
1974
1975
		$errors  = array();
1976
		$form    = $atts['form'];
1977
		$message = $atts['message'];
1978
1979
		include( FrmAppHelper::plugin_path() . '/classes/views/frm-entries/errors.php' );
1980
	}
1981
1982
	/**
1983
	 * Prepare the success message before it's shown
1984
	 *
1985
	 * @since 2.05
1986
	 */
1987
	private static function prepare_submit_message( $form, $entry_id ) {
1988
		$frm_settings = FrmAppHelper::get_settings( array( 'current_form' => $form->id ) );
1989
1990
		if ( $entry_id && is_numeric( $entry_id ) ) {
1991
			$message = isset( $form->options['success_msg'] ) ? $form->options['success_msg'] : $frm_settings->success_msg;
1992
			$class   = 'frm_message';
1993
		} else {
1994
			$message = $frm_settings->failed_msg;
1995
			$class   = FrmFormsHelper::form_error_class();
1996
		}
1997
1998
		$message = FrmFormsHelper::get_success_message( compact( 'message', 'form', 'entry_id', 'class' ) );
1999
2000
		return apply_filters( 'frm_main_feedback', $message, $form, $entry_id );
2001
	}
2002
2003
	public static function front_head() {
2004
		$version = FrmAppHelper::plugin_version();
2005
		$suffix  = FrmAppHelper::js_suffix();
2006
2007
		if ( ! empty( $suffix ) && self::has_combo_js_file() ) {
2008
			wp_register_script( 'formidable', FrmAppHelper::plugin_url() . '/js/frm.min.js', array( 'jquery' ), $version, true );
2009
		} else {
2010
			wp_register_script( 'formidable', FrmAppHelper::plugin_url() . "/js/formidable{$suffix}.js", array( 'jquery' ), $version, true );
2011
		}
2012
2013
		add_filter( 'script_loader_tag', 'FrmFormsController::defer_script_loading', 10, 2 );
2014
2015
		if ( FrmAppHelper::is_admin() ) {
2016
			// don't load this in back-end
2017
			return;
2018
		}
2019
2020
		FrmAppHelper::localize_script( 'front' );
2021
		FrmStylesController::enqueue_css( 'register' );
2022
	}
2023
2024
	/**
2025
	 * @since 3.0
2026
	 */
2027
	public static function has_combo_js_file() {
2028
		return is_readable( FrmAppHelper::plugin_path() . '/js/frm.min.js' );
2029
	}
2030
2031
	public static function maybe_load_css( $form, $this_load, $global_load ) {
2032
		$load_css = FrmForm::is_form_loaded( $form, $this_load, $global_load );
2033
2034
		if ( ! $load_css ) {
2035
			return;
2036
		}
2037
2038
		global $frm_vars;
2039
		self::footer_js( 'header' );
2040
		$frm_vars['css_loaded'] = true;
2041
2042
		self::load_late_css();
2043
	}
2044
2045
	/**
2046
	 * If css is loaded only on applicable pages, include it before the form loads
2047
	 * to prevent a flash of unstyled form.
2048
	 *
2049
	 * @since 4.01
2050
	 */
2051
	private static function load_late_css() {
2052
		$frm_settings = FrmAppHelper::get_settings();
2053
		$late_css = $frm_settings->load_style === 'dynamic';
2054
		if ( ! $late_css ) {
2055
			return;
2056
		}
2057
2058
		global $wp_styles;
2059
		if ( is_array( $wp_styles->queue ) && in_array( 'formidable', $wp_styles->queue ) ) {
2060
			wp_print_styles( 'formidable' );
2061
		}
2062
	}
2063
2064
	public static function defer_script_loading( $tag, $handle ) {
2065
		if ( 'recaptcha-api' == $handle && ! strpos( $tag, 'defer' ) ) {
2066
			$tag = str_replace( ' src', ' defer="defer" async="async" src', $tag );
2067
		}
2068
2069
		return $tag;
2070
	}
2071
2072
	public static function footer_js( $location = 'footer' ) {
2073
		global $frm_vars;
2074
2075
		FrmStylesController::enqueue_css();
2076
2077
		if ( ! FrmAppHelper::is_admin() && $location != 'header' && ! empty( $frm_vars['forms_loaded'] ) ) {
2078
			// load formidable js
2079
			wp_enqueue_script( 'formidable' );
2080
		}
2081
	}
2082
2083
	/**
2084
	 * @since 2.0.8
2085
	 */
2086
	private static function maybe_minimize_form( $atts, &$content ) {
2087
		// check if minimizing is turned on
2088
		if ( self::is_minification_on( $atts ) ) {
2089
			$content = str_replace( array( "\r\n", "\r", "\n", "\t", '    ' ), '', $content );
2090
		}
2091
	}
2092
2093
	/**
2094
	 * @since 2.0.8
2095
	 * @return boolean
2096
	 */
2097
	private static function is_minification_on( $atts ) {
2098
		return isset( $atts['minimize'] ) && ! empty( $atts['minimize'] );
2099
	}
2100
2101
	/**
2102
	 * @deprecated 4.0
2103
	 */
2104
	public static function new_form( $values = array() ) {
2105
		FrmDeprecated::new_form( $values );
2106
	}
2107
2108
	/**
2109
	 * @deprecated 4.0
2110
	 */
2111
	public static function create( $values = array() ) {
2112
		_deprecated_function( __METHOD__, '4.0', 'FrmFormsController::update' );
2113
		self::update( $values );
2114
	}
2115
2116
	/**
2117
	 * @deprecated 1.07.05
2118
	 * @codeCoverageIgnore
2119
	 */
2120
	public static function add_default_templates( $path, $default = true, $template = true ) {
2121
		FrmDeprecated::add_default_templates( $path, $default, $template );
2122
	}
2123
2124
	/**
2125
	 * @deprecated 3.0
2126
	 * @codeCoverageIgnore
2127
	 */
2128
	public static function bulk_create_template( $ids ) {
2129
		return FrmDeprecated::bulk_create_template( $ids );
2130
	}
2131
2132
	/**
2133
	 * @deprecated 2.03
2134
	 * @codeCoverageIgnore
2135
	 */
2136
	public static function register_pro_scripts() {
2137
		FrmDeprecated::register_pro_scripts();
2138
	}
2139
2140
	/**
2141
	 * @deprecated 3.0
2142
	 * @codeCoverageIgnore
2143
	 */
2144
	public static function edit_key() {
2145
		FrmDeprecated::edit_key();
2146
	}
2147
2148
	/**
2149
	 * @deprecated 3.0
2150
	 * @codeCoverageIgnore
2151
	 */
2152
	public static function edit_description() {
2153
		FrmDeprecated::edit_description();
2154
	}
2155
}
2156