Completed
Push — master ( 4f1b77...ce9b57 )
by Stephanie
02:42 queued 10s
created

FrmFormsController::untrash()   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
	 * Show a message about conditional logic
40
	 *
41
	 * @since 4.06.03
42
	 */
43
	public static function logic_tip() {
44
		echo '<a href="javascript:void(0)" class="frm_noallow frm_show_upgrade frm_add_logic_link" data-upgrade="' . esc_attr__( 'Conditional Logic options', 'formidable' ) . '" data-message="' . esc_attr__( 'Only show the fields you need and create branching forms. Upgrade to get conditional logic and question branching.', 'formidable' ) . esc_attr( ' <img src="https://cdn.formidableforms.com/wp-content/themes/fp2015git/images/survey/survey-logic.png" srcset="https://cdn.formidableforms.com/wp-content/themes/fp2015git/images/survey/[email protected] 2x" alt="Conditional Logic options"/>' ) . '" data-medium="builder" data-content="logic">';
45
		FrmAppHelper::icon_by_class( 'frmfont frm_swap_icon' );
46
		esc_html_e( 'Add Conditional Logic', 'formidable' );
47
		echo '</a>';
48
	}
49
50
	/**
51
	 * By default, Divi processes form shortcodes on the edit post page.
52
	 * Now that won't do.
53
	 *
54
	 * @since 3.01
55
	 */
56
	public static function prevent_divi_conflict( $shortcodes ) {
57
		$shortcodes[] = 'formidable';
58
59
		return $shortcodes;
60
	}
61
62
	public static function list_form() {
63
		FrmAppHelper::permission_check( 'frm_view_forms' );
64
65
		$message = '';
66
		$params  = FrmForm::list_page_params();
67
		$errors  = self::process_bulk_form_actions( array() );
68
		if ( isset( $errors['message'] ) ) {
69
			$message = $errors['message'];
70
			unset( $errors['message'] );
71
		}
72
		$errors = apply_filters( 'frm_admin_list_form_action', $errors );
73
74
		return self::display_forms_list( $params, $message, $errors );
75
	}
76
77
	/**
78
	 * Choose which type of form to create
79
	 *
80
	 * @since 3.06
81
	 */
82
	public static function add_new() {
83
		self::list_templates();
84
	}
85
86
	/**
87
	 * Load the scripts before a modal can be triggered.
88
	 *
89
	 * @since 4.0
90
	 */
91
	private static function init_modal() {
92
		wp_enqueue_script( 'jquery-ui-dialog' );
93
		wp_enqueue_style( 'jquery-ui-dialog' );
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
		FrmForm::maybe_get_form( $form );
105
		$create_email = apply_filters( 'frm_create_default_email_action', true, $form );
106
107
		if ( $create_email ) {
108
			$action_control = FrmFormActionsController::get_form_actions( 'email' );
109
			$action_control->create( $form->id );
110
		}
111
	}
112
113
	public static function edit( $values = false ) {
114
		FrmAppHelper::permission_check( 'frm_edit_forms' );
115
116
		$id = isset( $values['id'] ) ? absint( $values['id'] ) : FrmAppHelper::get_param( 'id', '', 'get', 'absint' );
117
118
		return self::get_edit_vars( $id );
119
	}
120
121
	public static function settings( $id = false, $message = '' ) {
122
		FrmAppHelper::permission_check( 'frm_edit_forms' );
123
124
		if ( ! $id || ! is_numeric( $id ) ) {
125
			$id = FrmAppHelper::get_param( 'id', '', 'get', 'absint' );
126
		}
127
128
		return self::get_settings_vars( $id, array(), $message );
129
	}
130
131
	public static function update_settings() {
132
		FrmAppHelper::permission_check( 'frm_edit_forms' );
133
134
		$id = FrmAppHelper::get_param( 'id', '', 'get', 'absint' );
135
136
		$errors = FrmForm::validate( $_POST );
137
		$warnings = FrmFormsHelper::check_for_warnings( $_POST );
138
139
		if ( count( $errors ) > 0 ) {
140
			return self::get_settings_vars( $id, $errors, compact( 'warnings' ) );
141
		}
142
143
		do_action( 'frm_before_update_form_settings', $id );
144
145
		FrmForm::update( $id, $_POST );
146
147
		$message = __( 'Settings Successfully Updated', 'formidable' );
148
149
		return self::get_settings_vars( $id, array(), compact( 'message', 'warnings' ) );
150
	}
151
152
	public static function update( $values = array() ) {
153
		if ( empty( $values ) ) {
154
			$values = $_POST;
155
		}
156
157
		// Set radio button and checkbox meta equal to "other" value.
158
		if ( FrmAppHelper::pro_is_installed() ) {
159
			$values = FrmProEntry::mod_other_vals( $values, 'back' );
160
		}
161
162
		$errors           = FrmForm::validate( $values );
163
		$permission_error = FrmAppHelper::permission_nonce_error( 'frm_edit_forms', 'frm_save_form', 'frm_save_form_nonce' );
164
		if ( $permission_error !== false ) {
165
			$errors['form'] = $permission_error;
166
		}
167
168
		$id = isset( $values['id'] ) ? absint( $values['id'] ) : FrmAppHelper::get_param( 'id', '', 'get', 'absint' );
169
170
		if ( count( $errors ) > 0 ) {
171
			return self::get_edit_vars( $id, $errors );
172
		} else {
173
			FrmForm::update( $id, $values );
174
			$message = __( 'Form was successfully updated.', 'formidable' );
175
176
			if ( self::is_too_long( $values ) ) {
177
				$message .= '<br/> ' . sprintf(
178
					/* translators: %1$s: Start link HTML, %2$s: end link HTML */
179
					__( 'However, your form is very long and may be %1$sreaching server limits%2$s.', 'formidable' ),
180
					'<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">',
181
					'</a>'
182
				);
183
			}
184
185
			if ( defined( 'DOING_AJAX' ) ) {
186
				wp_die( FrmAppHelper::kses( $message, array( 'a' ) ) ); // WPCS: XSS ok.
187
			}
188
189
			return self::get_edit_vars( $id, array(), $message );
190
		}
191
	}
192
193
	/**
194
	 * Check if the value at the end of the form was included.
195
	 * If it's missing, it means other values at the end of the form
196
	 * were likely not saved either.
197
	 *
198
	 * @since 3.06.01
199
	 */
200
	private static function is_too_long( $values ) {
201
		return ( ! isset( $values['frm_end'] ) ) || empty( $values['frm_end'] );
202
	}
203
204
	/**
205
	 * Redirect to the url for creating from a template
206
	 * Also delete the current form
207
	 *
208
	 * @since 2.0
209
	 * @deprecated 3.06
210
	 */
211
	public static function _create_from_template() {
212
		_deprecated_function( __FUNCTION__, '3.06' );
213
214
		FrmAppHelper::permission_check( 'frm_edit_forms' );
215
		check_ajax_referer( 'frm_ajax', 'nonce' );
216
217
		$current_form = FrmAppHelper::get_param( 'this_form', '', 'get', 'absint' );
218
		$template_id  = FrmAppHelper::get_param( 'id', '', 'get', 'absint' );
219
220
		if ( $current_form ) {
221
			FrmForm::destroy( $current_form );
222
		}
223
224
		echo esc_url_raw( admin_url( 'admin.php?page=formidable&frm_action=duplicate&id=' . absint( $template_id ) ) );
225
		wp_die();
226
	}
227
228
	public static function duplicate() {
229
		FrmAppHelper::permission_check( 'frm_edit_forms' );
230
231
		$params  = FrmForm::list_page_params();
232
		$form    = FrmForm::duplicate( $params['id'], $params['template'], true );
233
		$message = $params['template'] ? __( 'Form template was Successfully Created', 'formidable' ) : __( 'Form was Successfully Copied', 'formidable' );
234
		if ( $form ) {
235
			return self::get_edit_vars( $form, array(), $message, true );
236
		} else {
237
			return self::display_forms_list( $params, __( 'There was a problem creating the new template.', 'formidable' ) );
238
		}
239
	}
240
241
	public static function page_preview() {
242
		$params = FrmForm::list_page_params();
243
		if ( ! $params['form'] ) {
244
			return;
245
		}
246
247
		$form = FrmForm::getOne( $params['form'] );
248
		if ( $form ) {
249
			return self::show_form( $form->id, '', true, true );
250
		}
251
	}
252
253
	/**
254
	 * @since 3.0
255
	 */
256
	public static function show_page_preview() {
257
		echo self::page_preview(); // WPCS: XSS ok.
258
	}
259
260
	public static function preview() {
261
		do_action( 'frm_wp' );
262
263
		global $frm_vars;
264
		$frm_vars['preview'] = true;
265
266
		self::load_wp();
267
268
		$include_theme = FrmAppHelper::get_param( 'theme', '', 'get', 'absint' );
269
		if ( $include_theme ) {
270
			self::set_preview_query();
271
			self::load_theme_preview();
272
		} else {
273
			self::load_direct_preview();
274
		}
275
276
		wp_die();
277
	}
278
279
	/**
280
	 * @since 3.0
281
	 */
282
	private static function load_wp() {
283
		if ( ! defined( 'ABSPATH' ) && ! defined( 'XMLRPC_REQUEST' ) ) {
284
			global $wp;
285
			$root = dirname( dirname( dirname( dirname( __FILE__ ) ) ) );
286
			include_once( $root . '/wp-config.php' );
287
			$wp->init();
288
			$wp->register_globals();
289
		}
290
	}
291
292
	private static function set_preview_query() {
293
		$random_page = get_posts(
294
			array(
295
				'numberposts' => 1,
296
				'orderby'     => 'date',
297
				'order'       => 'ASC',
298
				'post_type'   => 'page',
299
			)
300
		);
301
302
		if ( ! empty( $random_page ) ) {
303
			$random_page = reset( $random_page );
304
			query_posts(
305
				array(
306
					'post_type' => 'page',
307
					'page_id'   => $random_page->ID,
308
				)
309
			);
310
		}
311
	}
312
313
	/**
314
	 * @since 3.0
315
	 */
316
	private static function load_theme_preview() {
317
		add_filter( 'wp_title', 'FrmFormsController::preview_title', 9999 );
318
		add_filter( 'the_title', 'FrmFormsController::preview_page_title', 9999 );
319
		add_filter( 'the_content', 'FrmFormsController::preview_content', 9999 );
320
		add_action( 'loop_no_results', 'FrmFormsController::show_page_preview' );
321
		add_filter( 'is_active_sidebar', '__return_false' );
322
		FrmStylesController::enqueue_css( 'enqueue', true );
323
		get_template_part( 'page' );
324
	}
325
326
	/**
327
	 * Set the page title for the theme preview page
328
	 *
329
	 * @since 3.0
330
	 */
331
	public static function preview_page_title( $title ) {
332
		if ( in_the_loop() ) {
333
			$title = self::preview_title( $title );
334
		}
335
336
		return $title;
337
	}
338
339
	/**
340
	 * Set the page title for the theme preview page
341
	 *
342
	 * @since 3.0
343
	 */
344
	public static function preview_title( $title ) {
345
		return __( 'Form Preview', 'formidable' );
346
	}
347
348
	/**
349
	 * Set the page content for the theme preview page
350
	 *
351
	 * @since 3.0
352
	 */
353
	public static function preview_content( $content ) {
354
		if ( in_the_loop() ) {
355
			$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...
356
		}
357
358
		return $content;
359
	}
360
361
	/**
362
	 * @since 3.0
363
	 */
364
	private static function load_direct_preview() {
365
		header( 'Content-Type: text/html; charset=' . get_option( 'blog_charset' ) );
366
367
		$key = FrmAppHelper::simple_get( 'form', 'sanitize_title' );
368
		if ( $key == '' ) {
369
			$key = FrmAppHelper::get_post_param( 'form', '', 'sanitize_title' );
370
		}
371
372
		$form = FrmForm::getAll( array( 'form_key' => $key ), '', 1 );
373
		if ( empty( $form ) ) {
374
			$form = FrmForm::getAll( array(), '', 1 );
375
		}
376
377
		require( FrmAppHelper::plugin_path() . '/classes/views/frm-entries/direct.php' );
378
	}
379
380
	public static function untrash() {
381
		self::change_form_status( 'untrash' );
382
	}
383
384
	public static function bulk_untrash( $ids ) {
385
		FrmAppHelper::permission_check( 'frm_edit_forms' );
386
387
		$count = FrmForm::set_status( $ids, 'published' );
388
389
		/* translators: %1$s: Number of forms */
390
		$message = sprintf( _n( '%1$s form restored from the Trash.', '%1$s forms restored from the Trash.', $count, 'formidable' ), 1 );
391
392
		return $message;
393
	}
394
395
	/**
396
	 * @since 3.06
397
	 */
398
	public static function ajax_trash() {
399
		FrmAppHelper::permission_check( 'frm_delete_forms' );
400
		check_ajax_referer( 'frm_ajax', 'nonce' );
401
		$form_id = FrmAppHelper::get_param( 'id', '', 'post', 'absint' );
402
		FrmForm::set_status( $form_id, 'trash' );
403
		wp_die();
404
	}
405
406
	public static function trash() {
407
		self::change_form_status( 'trash' );
408
	}
409
410
	/**
411
	 * @param string $status
412
	 *
413
	 * @return int The number of forms changed
414
	 */
415
	public static function change_form_status( $status ) {
416
		$available_status = array(
417
			'untrash' => array(
418
				'permission' => 'frm_edit_forms',
419
				'new_status' => 'published',
420
			),
421
			'trash'   => array(
422
				'permission' => 'frm_delete_forms',
423
				'new_status' => 'trash',
424
			),
425
		);
426
427
		if ( ! isset( $available_status[ $status ] ) ) {
428
			return;
429
		}
430
431
		FrmAppHelper::permission_check( $available_status[ $status ]['permission'] );
432
433
		$params = FrmForm::list_page_params();
434
435
		//check nonce url
436
		check_admin_referer( $status . '_form_' . $params['id'] );
437
438
		$count = 0;
439
		if ( FrmForm::set_status( $params['id'], $available_status[ $status ]['new_status'] ) ) {
440
			$count ++;
441
		}
442
443
		$form_type = FrmAppHelper::get_simple_request(
444
			array(
445
				'param' => 'form_type',
446
				'type'  => 'request',
447
			)
448
		);
449
450
		/* translators: %1$s: Number of forms */
451
		$available_status['untrash']['message'] = sprintf( _n( '%1$s form restored from the Trash.', '%1$s forms restored from the Trash.', $count, 'formidable' ), $count );
452
453
		/* translators: %1$s: Number of forms, %2$s: Start link HTML, %3$s: End link HTML */
454
		$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>' );
455
456
		$message = $available_status[ $status ]['message'];
457
458
		self::display_forms_list( $params, $message );
459
	}
460
461
	public static function bulk_trash( $ids ) {
462
		FrmAppHelper::permission_check( 'frm_delete_forms' );
463
464
		$count = 0;
465
		foreach ( $ids as $id ) {
466
			if ( FrmForm::trash( $id ) ) {
467
				$count ++;
468
			}
469
		}
470
471
		$current_page = FrmAppHelper::get_simple_request(
472
			array(
473
				'param' => 'form_type',
474
				'type'  => 'request',
475
			)
476
		);
477
		$message      = sprintf(
478
			/* translators: %1$s: Number of forms, %2$s: Start link HTML, %3$s: End link HTML */
479
			_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' ),
480
			$count,
481
			'<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' ) ) . '">',
482
			'</a>'
483
		);
484
485
		return $message;
486
	}
487
488
	public static function destroy() {
489
		FrmAppHelper::permission_check( 'frm_delete_forms' );
490
491
		$params = FrmForm::list_page_params();
492
493
		// Check nonce url.
494
		check_admin_referer( 'destroy_form_' . $params['id'] );
495
496
		$count = 0;
497
		if ( FrmForm::destroy( $params['id'] ) ) {
498
			$count ++;
499
		}
500
501
		/* translators: %1$s: Number of forms */
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
		/* translators: %1$s: Number of forms */
519
		$message = sprintf( _n( '%1$s form permanently deleted.', '%1$s forms permanently deleted.', $count, 'formidable' ), $count );
520
521
		return $message;
522
	}
523
524
	private static function delete_all() {
525
		// Check nonce url.
526
		$permission_error = FrmAppHelper::permission_nonce_error( 'frm_delete_forms', '_wpnonce', 'bulk-toplevel_page_formidable' );
527
		if ( $permission_error !== false ) {
528
			self::display_forms_list( array(), '', array( $permission_error ) );
529
530
			return;
531
		}
532
533
		$count   = FrmForm::scheduled_delete( time() );
534
535
		/* translators: %1$s: Number of forms */
536
		$message = sprintf( _n( '%1$s form permanently deleted.', '%1$s forms permanently deleted.', $count, 'formidable' ), $count );
537
538
		self::display_forms_list( array(), $message );
539
	}
540
541
	/**
542
	 * Create a new form from the modal.
543
	 *
544
	 * @since 4.0
545
	 */
546
	public static function build_new_form() {
547
		global $wpdb;
548
549
		FrmAppHelper::permission_check( 'frm_edit_forms' );
550
		check_ajax_referer( 'frm_ajax', 'nonce' );
551
552
		$new_values             = self::get_modal_values();
553
		$new_values['form_key'] = $new_values['name'];
554
555
		$form_id = FrmForm::create( $new_values );
556
557
		self::create_default_email_action( $form_id );
558
559
		$response = array(
560
			'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 555 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...
561
		);
562
563
		echo wp_json_encode( $response );
564
		wp_die();
565
	}
566
567
	/**
568
	 * Create a custom template from a form
569
	 *
570
	 * @since 3.06
571
	 */
572
	public static function build_template() {
573
		global $wpdb;
574
575
		FrmAppHelper::permission_check( 'frm_edit_forms' );
576
		check_ajax_referer( 'frm_ajax', 'nonce' );
577
578
		$form_id     = FrmAppHelper::get_param( 'xml', '', 'post', 'absint' );
579
		$new_form_id = FrmForm::duplicate( $form_id, 1, true );
580
		if ( empty( $new_form_id ) ) {
581
			$response = array(
582
				'message' => __( 'There was an error creating a template.', 'formidable' ),
583
			);
584
		} else {
585
			$new_values    = self::get_modal_values();
586
			$query_results = $wpdb->update( $wpdb->prefix . 'frm_forms', $new_values, array( 'id' => $new_form_id ) );
587
			if ( $query_results ) {
588
				FrmForm::clear_form_cache();
589
			}
590
591
			$response = array(
592
				'redirect' => admin_url( 'admin.php?page=formidable&frm_action=list_templates' ),
593
			);
594
		}
595
596
		echo wp_json_encode( $response );
597
		wp_die();
598
	}
599
600
	/**
601
	 * Before creating a new form, get the name and description from the modal.
602
	 *
603
	 * @since 4.0
604
	 */
605
	private static function get_modal_values() {
606
		$name = FrmAppHelper::get_param( 'name', '', 'post', 'sanitize_text_field' );
607
		$desc = FrmAppHelper::get_param( 'desc', '', 'post', 'sanitize_textarea_field' );
608
609
		return array(
610
			'name'        => $name,
611
			'description' => $desc,
612
		);
613
	}
614
615
	/**
616
	 * Inserts Formidable button
617
	 * Hook exists since 2.5.0
618
	 *
619
	 * @since 2.0.15
620
	 */
621
	public static function insert_form_button() {
622
		if ( current_user_can( 'frm_view_forms' ) ) {
623
			FrmAppHelper::load_admin_wide_js();
624
			$menu_name = FrmAppHelper::get_menu_name();
625
			$icon      = apply_filters( 'frm_media_icon', FrmAppHelper::svg_logo() );
626
			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' ) . '">' .
627
				FrmAppHelper::kses( $icon, 'all' ) .
628
				' ' . esc_html( $menu_name ) . '</a>'; // WPCS: XSS ok.
629
		}
630
	}
631
632
	public static function insert_form_popup() {
633
		$page = basename( FrmAppHelper::get_server_value( 'PHP_SELF' ) );
634
		if ( ! in_array( $page, array( 'post.php', 'page.php', 'page-new.php', 'post-new.php' ) ) ) {
635
			return;
636
		}
637
638
		FrmAppHelper::load_admin_wide_js();
639
640
		$shortcodes = array(
641
			'formidable' => array(
642
				'name'  => __( 'Form', 'formidable' ),
643
				'label' => __( 'Insert a Form', 'formidable' ),
644
			),
645
		);
646
647
		$shortcodes = apply_filters( 'frm_popup_shortcodes', $shortcodes );
648
649
		include( FrmAppHelper::plugin_path() . '/classes/views/frm-forms/insert_form_popup.php' );
650
	}
651
652
	public static function get_shortcode_opts() {
653
		FrmAppHelper::permission_check( 'frm_view_forms' );
654
		check_ajax_referer( 'frm_ajax', 'nonce' );
655
656
		$shortcode = FrmAppHelper::get_post_param( 'shortcode', '', 'sanitize_text_field' );
657
		if ( empty( $shortcode ) ) {
658
			wp_die();
659
		}
660
661
		echo '<div id="sc-opts-' . esc_attr( $shortcode ) . '" class="frm_shortcode_option">';
662
		echo '<input type="radio" name="frmsc" value="' . esc_attr( $shortcode ) . '" id="sc-' . esc_attr( $shortcode ) . '" class="frm_hidden" />';
663
664
		$form_id = '';
665
		$opts    = array();
666
		switch ( $shortcode ) {
667
			case 'formidable':
668
				$opts = array(
669
					'form_id'     => 'id',
670
					'title'       => array(
671
						'val'   => 1,
672
						'label' => __( 'Display form title', 'formidable' ),
673
					),
674
					'description' => array(
675
						'val'   => 1,
676
						'label' => __( 'Display form description', 'formidable' ),
677
					),
678
					'minimize'    => array(
679
						'val'   => 1,
680
						'label' => __( 'Minimize form HTML', 'formidable' ),
681
					),
682
				);
683
		}
684
		$opts = apply_filters( 'frm_sc_popup_opts', $opts, $shortcode );
685
686
		if ( isset( $opts['form_id'] ) && is_string( $opts['form_id'] ) ) {
687
			// allow other shortcodes to use the required form id option
688
			$form_id = $opts['form_id'];
689
			unset( $opts['form_id'] );
690
		}
691
692
		include( FrmAppHelper::plugin_path() . '/classes/views/frm-forms/shortcode_opts.php' );
693
694
		echo '</div>';
695
696
		wp_die();
697
	}
698
699
	public static function display_forms_list( $params = array(), $message = '', $errors = array() ) {
700
		FrmAppHelper::permission_check( 'frm_view_forms' );
701
702
		global $wpdb, $frm_vars;
703
704
		if ( empty( $params ) ) {
705
			$params = FrmForm::list_page_params();
706
		}
707
708
		$wp_list_table = new FrmFormsListHelper( compact( 'params' ) );
709
710
		$pagenum = $wp_list_table->get_pagenum();
711
712
		$wp_list_table->prepare_items();
713
714
		$total_pages = $wp_list_table->get_pagination_arg( 'total_pages' );
715
		if ( $pagenum > $total_pages && $total_pages > 0 ) {
716
			wp_redirect( esc_url_raw( add_query_arg( 'paged', $total_pages ) ) );
717
			die();
718
		}
719
720
		require( FrmAppHelper::plugin_path() . '/classes/views/frm-forms/list.php' );
721
	}
722
723
	public static function get_columns( $columns ) {
724
		$columns['cb'] = '<input type="checkbox" />';
725
		$columns['id'] = 'ID';
726
727
		$type = FrmAppHelper::get_simple_request(
728
			array(
729
				'param'   => 'form_type',
730
				'type'    => 'request',
731
				'default' => 'published',
732
			)
733
		);
734
735
		if ( 'template' == $type ) {
736
			$columns['name']     = __( 'Template Name', 'formidable' );
737
			$columns['type']     = __( 'Type', 'formidable' );
738
			$columns['form_key'] = __( 'Key', 'formidable' );
739
		} else {
740
			$columns['name']      = __( 'Form Title', 'formidable' );
741
			$columns['entries']   = __( 'Entries', 'formidable' );
742
			$columns['form_key']  = __( 'Key', 'formidable' );
743
			$columns['shortcode'] = __( 'Shortcodes', 'formidable' );
744
		}
745
746
		$columns['created_at'] = __( 'Date', 'formidable' );
747
748
		add_screen_option(
749
			'per_page',
750
			array(
751
				'label'   => __( 'Forms', 'formidable' ),
752
				'default' => 20,
753
				'option'  => 'formidable_page_formidable_per_page',
754
			)
755
		);
756
757
		return $columns;
758
	}
759
760
	public static function get_sortable_columns() {
761
		return array(
762
			'id'          => 'id',
763
			'name'        => 'name',
764
			'description' => 'description',
765
			'form_key'    => 'form_key',
766
			'created_at'  => 'created_at',
767
		);
768
	}
769
770
	public static function hidden_columns( $hidden_columns ) {
771
		$type = FrmAppHelper::get_simple_request(
772
			array(
773
				'param' => 'form_type',
774
				'type'  => 'request',
775
			)
776
		);
777
778
		if ( $type === 'template' ) {
779
			$hidden_columns[] = 'id';
780
			$hidden_columns[] = 'form_key';
781
		}
782
783
		return $hidden_columns;
784
	}
785
786
	public static function save_per_page( $save, $option, $value ) {
787
		if ( $option == 'formidable_page_formidable_per_page' ) {
788
			$save = (int) $value;
789
		}
790
791
		return $save;
792
	}
793
794
	/**
795
	 * Show the template listing page
796
	 *
797
	 * @since 3.06
798
	 */
799
	private static function list_templates() {
800
		self::init_modal();
801
802
		$where = apply_filters( 'frm_forms_dropdown', array(), '' );
803
		$forms = FrmForm::get_published_forms( $where );
804
805
		$api       = new FrmFormTemplateApi();
806
		$templates = $api->get_api_info();
807
808
		$custom_templates = array();
809
		self::add_user_templates( $custom_templates );
810
811
		$error   = '';
812
		$expired = false;
813
		$license_type = '';
814
		if ( isset( $templates['error'] ) ) {
815
			$error   = $templates['error']['message'];
816
			$error   = str_replace( 'utm_medium=addons', 'utm_medium=form-templates', $error );
817
			$expired = ( $templates['error']['code'] === 'expired' );
818
819
			$license_type = isset( $templates['error']['type'] ) ? $templates['error']['type'] : '';
820
			unset( $templates['error'] );
821
		}
822
823
		$pricing = FrmAppHelper::admin_upgrade_link( 'form-templates' );
824
825
		$categories = self::get_template_categories( $templates );
826
827
		require( FrmAppHelper::plugin_path() . '/classes/views/frm-forms/list-templates.php' );
828
	}
829
830
	/**
831
	 * @since 4.03.01
832
	 */
833
	private static function get_template_categories( $templates ) {
834
		$categories = array();
835
		foreach ( $templates as $template ) {
836
			if ( isset( $template['categories'] ) ) {
837
				$categories = array_merge( $categories, $template['categories'] );
838
			}
839
		}
840
		$exclude_cats = FrmFormsHelper::ignore_template_categories();
841
		$categories = array_unique( $categories );
842
		$categories = array_diff( $categories, $exclude_cats );
843
		sort( $categories );
844
		return $categories;
845
	}
846
847
	private static function add_user_templates( &$templates ) {
848
		$user_templates = array(
849
			'is_template'      => 1,
850
			'default_template' => 0,
851
		);
852
		$user_templates = FrmForm::getAll( $user_templates, 'name' );
853
		foreach ( $user_templates as $template ) {
854
			$template = array(
855
				'id'          => $template->id,
856
				'name'        => $template->name,
857
				'key'         => $template->form_key,
858
				'description' => $template->description,
859
				'url'         => admin_url( 'admin.php?page=formidable&frm_action=duplicate&id=' . absint( $template->id ) ),
860
				'released'    => $template->created_at,
861
				'installed'   => 1,
862
			);
863
			array_unshift( $templates, $template );
864
			unset( $template );
865
		}
866
	}
867
868
	private static function get_edit_vars( $id, $errors = array(), $message = '', $create_link = false ) {
869
		global $frm_vars;
870
871
		$form = FrmForm::getOne( $id );
872
		if ( ! $form ) {
873
			wp_die( esc_html__( 'You are trying to edit a form that does not exist.', 'formidable' ) );
874
		}
875
876
		if ( $form->parent_form_id ) {
877
			/* translators: %1$s: Start link HTML, %2$s: End link HTML */
878
			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>' ) );
879
		}
880
881
		$frm_field_selection = FrmField::field_selection();
882
883
		$fields = FrmField::get_all_for_form( $form->id );
884
885
		// Automatically add end section fields if they don't exist (2.0 migration).
886
		$reset_fields = false;
887
		FrmFormsHelper::auto_add_end_section_fields( $form, $fields, $reset_fields );
888
889
		if ( $reset_fields ) {
890
			$fields = FrmField::get_all_for_form( $form->id, '', 'exclude' );
891
		}
892
893
		unset( $end_section_values, $last_order, $open, $reset_fields );
894
895
		$args             = array( 'parent_form_id' => $form->id );
896
		$values           = FrmAppHelper::setup_edit_vars( $form, 'forms', '', true, array(), $args );
897
		$values['fields'] = $fields;
898
899
		$edit_message = __( 'Form was successfully updated.', 'formidable' );
900
		if ( $form->is_template && $message == $edit_message ) {
901
			$message = __( 'Template was successfully updated.', 'formidable' );
902
		}
903
904
		$all_templates = FrmForm::getAll( array( 'is_template' => 1 ), 'name' );
905
		$has_fields    = isset( $values['fields'] ) && ! empty( $values['fields'] );
906
907
		if ( defined( 'DOING_AJAX' ) ) {
908
			wp_die();
909
		} else {
910
			require( FrmAppHelper::plugin_path() . '/classes/views/frm-forms/edit.php' );
911
		}
912
	}
913
914
	public static function get_settings_vars( $id, $errors = array(), $args = array() ) {
915
		FrmAppHelper::permission_check( 'frm_edit_forms' );
916
917
		global $frm_vars;
918
919
		if ( ! is_array( $args ) ) {
920
			// For reverse compatibility.
921
			$args = array(
922
				'message' => $args,
923
			);
924
		}
925
926
		$defaults = array(
927
			'message'  => '',
928
			'warnings' => array(),
929
		);
930
		$args     = array_merge( $defaults, $args );
931
		$message  = $args['message'];
932
		$warnings = $args['warnings'];
933
934
		$form   = FrmForm::getOne( $id );
935
		$fields = FrmField::get_all_for_form( $id );
936
		$values = FrmAppHelper::setup_edit_vars( $form, 'forms', $fields, true );
937
938
		self::clean_submit_html( $values );
939
940
		$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 936 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...
941
		$current  = FrmAppHelper::simple_get( 't', 'sanitize_title', 'advanced_settings' );
942
943
		require( FrmAppHelper::plugin_path() . '/classes/views/frm-forms/settings.php' );
944
	}
945
946
	/**
947
	 * @since 4.0
948
	 */
949
	public static function form_publish_button( $atts ) {
950
		$values = $atts['values'];
951
		include( FrmAppHelper::plugin_path() . '/classes/views/frm-forms/_publish_box.php' );
952
	}
953
954
	/**
955
	 * Get a list of all the settings tabs for the form settings page.
956
	 *
957
	 * @since 4.0
958
	 *
959
	 * @param array $values
960
	 * @return array
961
	 */
962
	private static function get_settings_tabs( $values ) {
963
		$sections = array(
964
			'advanced'    => array(
965
				'name'     => __( 'General', 'formidable' ),
966
				'title'    => __( 'General Form Settings', 'formidable' ),
967
				'function' => array( __CLASS__, 'advanced_settings' ),
968
				'icon'     => 'frm_icon_font frm_settings_icon',
969
			),
970
			'email'       => array(
971
				'name'     => __( 'Actions & Notifications', 'formidable' ),
972
				'function' => array( 'FrmFormActionsController', 'email_settings' ),
973
				'id'       => 'frm_notification_settings',
974
				'icon'     => 'frm_icon_font frm_mail_bulk_icon',
975
			),
976
			'permissions' => array(
977
				'name'     => __( 'Form Permissions', 'formidable' ),
978
				'icon'     => 'frm_icon_font frm_lock_icon',
979
				'html_class' => 'frm_show_upgrade frm_noallow',
980
				'data'     => array(
981
					'medium'  => 'permissions',
982
					'upgrade' => __( 'Form Permissions', 'formidable' ),
983
				),
984
			),
985
			'scheduling' => array(
986
				'name'     => __( 'Form Scheduling', 'formidable' ),
987
				'icon'     => 'frm_icon_font frm_calendar_icon',
988
				'html_class' => 'frm_show_upgrade frm_noallow',
989
				'data'     => array(
990
					'medium'  => 'scheduling',
991
					'upgrade' => __( 'Form scheduling settings', 'formidable' ),
992
				),
993
			),
994
			'buttons'     => array(
995
				'name'     => __( 'Styling & Buttons', 'formidable' ),
996
				'class'    => __CLASS__,
997
				'function' => 'buttons_settings',
998
				'icon'     => 'frm_icon_font frm_pallet_icon',
999
			),
1000
			'html'        => array(
1001
				'name'     => __( 'Customize HTML', 'formidable' ),
1002
				'class'    => __CLASS__,
1003
				'function' => 'html_settings',
1004
				'icon'     => 'frm_icon_font frm_code_icon',
1005
			),
1006
		);
1007
1008
		$sections = apply_filters( 'frm_add_form_settings_section', $sections, $values );
1009
1010
		if ( FrmAppHelper::pro_is_installed() && ! FrmAppHelper::meets_min_pro_version( '4.0' ) ) {
1011
			// Prevent settings from showing in 2 spots.
1012
			unset( $sections['permissions'], $sections['scheduling'] );
1013
		}
1014
1015
		foreach ( $sections as $key => $section ) {
1016
			$defaults = array(
1017
				'html_class' => '',
1018
				'name'       => ucfirst( $key ),
1019
				'icon'       => 'frm_icon_font frm_settings_icon',
1020
			);
1021
1022
			$section = array_merge( $defaults, $section );
1023
1024
			if ( ! isset( $section['anchor'] ) ) {
1025
				$section['anchor'] = $key;
1026
			}
1027
			$section['anchor'] .= '_settings';
1028
1029
			if ( ! isset( $section['title'] ) ) {
1030
				$section['title'] = $section['name'];
1031
			}
1032
1033
			if ( ! isset( $section['id'] ) ) {
1034
				$section['id'] = $section['anchor'];
1035
			}
1036
1037
			$sections[ $key ] = $section;
1038
		}
1039
1040
		return $sections;
1041
	}
1042
1043
	/**
1044
	 * @since 4.0
1045
	 *
1046
	 * @param array $values
1047
	 */
1048
	public static function advanced_settings( $values ) {
1049
		$first_h3 = 'frm_first_h3';
1050
1051
		include( FrmAppHelper::plugin_path() . '/classes/views/frm-forms/settings-advanced.php' );
1052
	}
1053
1054
	/**
1055
	 * @since 4.0
1056
	 *
1057
	 * @param array $values
1058
	 */
1059
	public static function buttons_settings( $values ) {
1060
		$styles = apply_filters( 'frm_get_style_opts', array() );
1061
1062
		$frm_settings    = FrmAppHelper::get_settings();
1063
		$no_global_style = $frm_settings->load_style === 'none';
1064
1065
		include( FrmAppHelper::plugin_path() . '/classes/views/frm-forms/settings-buttons.php' );
1066
	}
1067
1068
	/**
1069
	 * @since 4.0
1070
	 *
1071
	 * @param array $values
1072
	 */
1073
	public static function html_settings( $values ) {
1074
		include( FrmAppHelper::plugin_path() . '/classes/views/frm-forms/settings-html.php' );
1075
	}
1076
1077
	/**
1078
	 * Replace old Submit Button href with new href to avoid errors in Chrome
1079
	 *
1080
	 * @since 2.03.08
1081
	 *
1082
	 * @param array|boolean $values
1083
	 */
1084
	private static function clean_submit_html( &$values ) {
1085
		if ( is_array( $values ) && isset( $values['submit_html'] ) ) {
1086
			$values['submit_html'] = str_replace( 'javascript:void(0)', '#', $values['submit_html'] );
1087
		}
1088
	}
1089
1090
	public static function mb_tags_box( $form_id, $class = '' ) {
1091
		$fields       = FrmField::get_all_for_form( $form_id, '', 'include' );
1092
		$linked_forms = array();
1093
		$col          = 'one';
1094
		$settings_tab = FrmAppHelper::is_admin_page( 'formidable' ) ? true : false;
1095
1096
		$cond_shortcodes  = apply_filters( 'frm_conditional_shortcodes', array() );
1097
		$entry_shortcodes = self::get_shortcode_helpers( $settings_tab );
1098
1099
		$advanced_helpers = self::advanced_helpers( compact( 'fields', 'form_id' ) );
1100
1101
		include( FrmAppHelper::plugin_path() . '/classes/views/shared/mb_adv_info.php' );
1102
	}
1103
1104
	/**
1105
	 * @since 3.04.01
1106
	 */
1107
	private static function advanced_helpers( $atts ) {
1108
		$advanced_helpers = array(
1109
			'default' => array(
1110
				'heading' => __( 'Customize field values with the following parameters.', 'formidable' ),
1111
				'codes'   => self::get_advanced_shortcodes(),
1112
			),
1113
		);
1114
1115
		$user_fields = self::user_shortcodes();
1116
		if ( ! empty( $user_fields ) ) {
1117
			$user_helpers = array();
1118
			foreach ( $user_fields as $uk => $uf ) {
1119
				$user_helpers[ '|user_id| show="' . $uk . '"' ] = $uf;
1120
				unset( $uk, $uf );
1121
			}
1122
1123
			$advanced_helpers['user_id'] = array(
1124
				'codes'   => $user_helpers,
1125
			);
1126
		}
1127
1128
		/**
1129
		 * Add extra helper shortcodes on the Advanced tab in form settings and views
1130
		 *
1131
		 * @since 3.04.01
1132
		 *
1133
		 * @param array $atts - Includes fields and form_id
1134
		 */
1135
		return apply_filters( 'frm_advanced_helpers', $advanced_helpers, $atts );
1136
	}
1137
1138
	/**
1139
	 * Get an array of the options to display in the advanced tab
1140
	 * of the customization panel
1141
	 *
1142
	 * @since 2.0.6
1143
	 */
1144
	private static function get_advanced_shortcodes() {
1145
		$adv_shortcodes = array(
1146
			'x sep=", "'           => array(
1147
				'label' => __( 'Separator', 'formidable' ),
1148
				'title' => __( 'Use a different separator for checkbox fields', 'formidable' ),
1149
			),
1150
			'x format="d-m-Y"'     => array(
1151
				'label' => __( 'Date Format', 'formidable' ),
1152
			),
1153
			'x show="field_label"' => array(
1154
				'label' => __( 'Field Label', 'formidable' ),
1155
			),
1156
			'x wpautop=0'          => array(
1157
				'label' => __( 'No Auto P', 'formidable' ),
1158
				'title' => __( 'Do not automatically add any paragraphs or line breaks', 'formidable' ),
1159
			),
1160
		);
1161
		$adv_shortcodes = apply_filters( 'frm_advanced_shortcodes', $adv_shortcodes );
1162
1163
		// __( 'Leave blank instead of defaulting to User Login', 'formidable' ) : blank=1
1164
1165
		return $adv_shortcodes;
1166
	}
1167
1168
	/**
1169
	 * @since 3.04.01
1170
	 */
1171
	private static function user_shortcodes() {
1172
		$options = array(
1173
			'ID'           => __( 'User ID', 'formidable' ),
1174
			'first_name'   => __( 'First Name', 'formidable' ),
1175
			'last_name'    => __( 'Last Name', 'formidable' ),
1176
			'display_name' => __( 'Display Name', 'formidable' ),
1177
			'user_login'   => __( 'User Login', 'formidable' ),
1178
			'user_email'   => __( 'Email', 'formidable' ),
1179
			'avatar'       => __( 'Avatar', 'formidable' ),
1180
			'author_link'  => __( 'Author Link', 'formidable' ),
1181
		);
1182
1183
		return apply_filters( 'frm_user_shortcodes', $options );
1184
	}
1185
1186
	/**
1187
	 * Get an array of the helper shortcodes to display in the customization panel
1188
	 *
1189
	 * @since 2.0.6
1190
	 */
1191
	private static function get_shortcode_helpers( $settings_tab ) {
1192
		$entry_shortcodes = array(
1193
			'id'         => __( 'Entry ID', 'formidable' ),
1194
			'key'        => __( 'Entry Key', 'formidable' ),
1195
			'post_id'    => __( 'Post ID', 'formidable' ),
1196
			'ip'         => __( 'User IP', 'formidable' ),
1197
			'created-at' => __( 'Entry created', 'formidable' ),
1198
			'updated-at' => __( 'Entry updated', 'formidable' ),
1199
			''           => '',
1200
			'siteurl'    => __( 'Site URL', 'formidable' ),
1201
			'sitename'   => __( 'Site Name', 'formidable' ),
1202
		);
1203
1204
		if ( ! FrmAppHelper::pro_is_installed() ) {
1205
			unset( $entry_shortcodes['post_id'] );
1206
		}
1207
1208
		if ( $settings_tab ) {
1209
			$entry_shortcodes['default-message'] = __( 'Default Msg', 'formidable' );
1210
			$entry_shortcodes['default-html']    = __( 'Default HTML', 'formidable' );
1211
			$entry_shortcodes['default-plain']   = __( 'Default Plain', 'formidable' );
1212
		}
1213
1214
		/**
1215
		 * Use this hook to add or remove buttons in the helpers section
1216
		 * in the customization panel
1217
		 *
1218
		 * @since 2.0.6
1219
		 */
1220
		$entry_shortcodes = apply_filters( 'frm_helper_shortcodes', $entry_shortcodes, $settings_tab );
1221
1222
		return $entry_shortcodes;
1223
	}
1224
1225
	/**
1226
	 * Insert the form class setting into the form
1227
	 */
1228
	public static function form_classes( $form ) {
1229
		if ( isset( $form->options['form_class'] ) ) {
1230
			echo esc_attr( sanitize_text_field( $form->options['form_class'] ) );
1231
		}
1232
1233
		if ( isset( $form->options['js_validate'] ) && $form->options['js_validate'] ) {
1234
			echo ' frm_js_validate ';
1235
		}
1236
	}
1237
1238
	public static function get_email_html() {
1239
		FrmAppHelper::permission_check( 'frm_view_forms' );
1240
		check_ajax_referer( 'frm_ajax', 'nonce' );
1241
1242
		echo FrmEntriesController::show_entry_shortcode( // WPCS: XSS ok.
1243
			array(
1244
				'form_id'       => FrmAppHelper::get_post_param( 'form_id', '', 'absint' ),
1245
				'default_email' => true,
1246
				'plain_text'    => FrmAppHelper::get_post_param( 'plain_text', '', 'absint' ),
1247
			)
1248
		);
1249
		wp_die();
1250
	}
1251
1252
	public static function filter_content( $content, $form, $entry = false ) {
1253
		self::get_entry_by_param( $entry );
1254
		if ( ! $entry ) {
1255
			return $content;
1256
		}
1257
1258
		if ( is_object( $form ) ) {
1259
			$form = $form->id;
1260
		}
1261
1262
		$shortcodes = FrmFieldsHelper::get_shortcodes( $content, $form );
1263
		$content    = apply_filters( 'frm_replace_content_shortcodes', $content, $entry, $shortcodes );
1264
1265
		return $content;
1266
	}
1267
1268
	private static function get_entry_by_param( &$entry ) {
1269
		if ( ! $entry || ! is_object( $entry ) ) {
1270
			if ( ! $entry || ! is_numeric( $entry ) ) {
1271
				$entry = FrmAppHelper::get_post_param( 'id', false, 'sanitize_title' );
1272
			}
1273
1274
			FrmEntry::maybe_get_entry( $entry );
1275
		}
1276
	}
1277
1278
	public static function replace_content_shortcodes( $content, $entry, $shortcodes ) {
1279
		return FrmFieldsHelper::replace_content_shortcodes( $content, $entry, $shortcodes );
1280
	}
1281
1282
	public static function process_bulk_form_actions( $errors ) {
1283
		if ( ! $_REQUEST ) {
1284
			return $errors;
1285
		}
1286
1287
		$bulkaction = FrmAppHelper::get_param( 'action', '', 'get', 'sanitize_text_field' );
1288
		if ( $bulkaction == - 1 ) {
1289
			$bulkaction = FrmAppHelper::get_param( 'action2', '', 'get', 'sanitize_title' );
1290
		}
1291
1292
		if ( ! empty( $bulkaction ) && strpos( $bulkaction, 'bulk_' ) === 0 ) {
1293
			FrmAppHelper::remove_get_action();
1294
1295
			$bulkaction = str_replace( 'bulk_', '', $bulkaction );
1296
		}
1297
1298
		$ids = FrmAppHelper::get_param( 'item-action', '', 'get', 'sanitize_text_field' );
1299
		if ( empty( $ids ) ) {
1300
			$errors[] = __( 'No forms were specified', 'formidable' );
1301
1302
			return $errors;
1303
		}
1304
1305
		$permission_error = FrmAppHelper::permission_nonce_error( '', '_wpnonce', 'bulk-toplevel_page_formidable' );
1306
		if ( $permission_error !== false ) {
1307
			$errors[] = $permission_error;
1308
1309
			return $errors;
1310
		}
1311
1312
		if ( ! is_array( $ids ) ) {
1313
			$ids = explode( ',', $ids );
1314
		}
1315
1316
		switch ( $bulkaction ) {
1317
			case 'delete':
1318
				$message = self::bulk_destroy( $ids );
1319
				break;
1320
			case 'trash':
1321
				$message = self::bulk_trash( $ids );
1322
				break;
1323
			case 'untrash':
1324
				$message = self::bulk_untrash( $ids );
1325
		}
1326
1327
		if ( isset( $message ) && ! empty( $message ) ) {
1328
			$errors['message'] = $message;
1329
		}
1330
1331
		return $errors;
1332
	}
1333
1334
	public static function route() {
1335
		$action = isset( $_REQUEST['frm_action'] ) ? 'frm_action' : 'action';
1336
		$vars   = array();
1337
		FrmAppHelper::include_svg();
1338
1339
		if ( isset( $_POST['frm_compact_fields'] ) ) {
1340
			FrmAppHelper::permission_check( 'frm_edit_forms' );
1341
1342
			// Javascript needs to be allowed in some field settings.
1343
			// phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
1344
			$json_vars = htmlspecialchars_decode( nl2br( str_replace( '&quot;', '"', wp_unslash( $_POST['frm_compact_fields'] ) ) ) );
1345
			$json_vars = json_decode( $json_vars, true );
1346
			if ( empty( $json_vars ) ) {
1347
				// json decoding failed so we should return an error message.
1348
				$action = FrmAppHelper::get_param( $action, '', 'get', 'sanitize_title' );
1349
				if ( 'edit' == $action ) {
1350
					$action = 'update';
1351
				}
1352
1353
				add_filter( 'frm_validate_form', 'FrmFormsController::json_error' );
1354
			} else {
1355
				$vars   = FrmAppHelper::json_to_array( $json_vars );
1356
				$action = $vars[ $action ];
1357
				unset( $_REQUEST['frm_compact_fields'], $_POST['frm_compact_fields'] );
1358
				$_REQUEST = array_merge( $_REQUEST, $vars );
1359
				$_POST    = array_merge( $_POST, $_REQUEST );
1360
			}
1361
		} else {
1362
			$action = FrmAppHelper::get_param( $action, '', 'get', 'sanitize_title' );
1363
			if ( isset( $_REQUEST['delete_all'] ) ) {
1364
				// Override the action for this page.
1365
				$action = 'delete_all';
1366
			}
1367
		}
1368
1369
		add_action( 'frm_load_form_hooks', 'FrmHooksController::trigger_load_form_hooks' );
1370
		FrmAppHelper::trigger_hook_load( 'form' );
1371
1372
		switch ( $action ) {
1373
			case 'new':
1374
				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...
1375
			case 'add_new':
1376
			case 'list_templates':
1377
				return self::list_templates();
1378
			case 'create':
1379
			case 'edit':
1380
			case 'update':
1381
			case 'duplicate':
1382
			case 'trash':
1383
			case 'untrash':
1384
			case 'destroy':
1385
			case 'delete_all':
1386
			case 'settings':
1387
			case 'update_settings':
1388
				return self::$action( $vars );
1389
			case 'lite-reports':
1390
				return self::no_reports( $vars );
1391
			case 'views':
1392
				return self::no_views( $vars );
1393
			default:
1394
				do_action( 'frm_form_action_' . $action );
1395
				if ( apply_filters( 'frm_form_stop_action_' . $action, false ) ) {
1396
					return;
1397
				}
1398
1399
				$action = FrmAppHelper::get_param( 'action', '', 'get', 'sanitize_text_field' );
1400
				if ( $action == - 1 ) {
1401
					$action = FrmAppHelper::get_param( 'action2', '', 'get', 'sanitize_title' );
1402
				}
1403
1404
				if ( strpos( $action, 'bulk_' ) === 0 ) {
1405
					FrmAppHelper::remove_get_action();
1406
1407
					return self::list_form();
1408
				}
1409
1410
				return self::display_forms_list();
1411
		}
1412
	}
1413
1414
	public static function json_error( $errors ) {
1415
		$errors['json'] = __( 'Abnormal HTML characters prevented your form from saving correctly', 'formidable' );
1416
1417
		return $errors;
1418
	}
1419
1420
	/**
1421
	 * Education for premium features.
1422
	 *
1423
	 * @since 4.05
1424
	 */
1425
	public static function add_form_style_tab_options() {
1426
		include( FrmAppHelper::plugin_path() . '/classes/views/frm-forms/add_form_style_options.php' );
1427
	}
1428
1429
	/**
1430
	 * Add education about views.
1431
	 *
1432
	 * @since 4.07
1433
	 */
1434
	public static function no_views( $values = array() ) {
1435
		FrmAppHelper::include_svg();
1436
		$id   = FrmAppHelper::get_param( 'form', '', 'get', 'absint' );
1437
		$form = $id ? FrmForm::getOne( $id ) : false;
1438
1439
		include FrmAppHelper::plugin_path() . '/classes/views/shared/views-info.php';
1440
	}
1441
1442
	/**
1443
	 * Add education about reports.
1444
	 *
1445
	 * @since 4.07
1446
	 */
1447
	public static function no_reports( $values = array() ) {
1448
		$id   = FrmAppHelper::get_param( 'form', '', 'get', 'absint' );
1449
		$form = $id ? FrmForm::getOne( $id ) : false;
1450
1451
		include FrmAppHelper::plugin_path() . '/classes/views/shared/reports-info.php';
1452
	}
1453
1454
	/* FRONT-END FORMS */
1455
	public static function admin_bar_css() {
1456
		if ( is_admin() || ! current_user_can( 'frm_edit_forms' ) ) {
1457
			return;
1458
		}
1459
1460
		self::move_menu_to_footer();
1461
1462
		add_action( 'wp_before_admin_bar_render', 'FrmFormsController::admin_bar_configure' );
1463
		FrmAppHelper::load_font_style();
1464
	}
1465
1466
	/**
1467
	 * @since 4.05.02
1468
	 */
1469
	private static function move_menu_to_footer() {
1470
		$settings = FrmAppHelper::get_settings();
1471
		if ( empty( $settings->admin_bar ) ) {
1472
			remove_action( 'wp_body_open', 'wp_admin_bar_render', 0 );
1473
		}
1474
	}
1475
1476
	public static function admin_bar_configure() {
1477
		global $frm_vars;
1478
		if ( empty( $frm_vars['forms_loaded'] ) ) {
1479
			return;
1480
		}
1481
1482
		$actions = array();
1483
		foreach ( $frm_vars['forms_loaded'] as $form ) {
1484
			if ( is_object( $form ) ) {
1485
				$actions[ $form->id ] = $form->name;
1486
			}
1487
			unset( $form );
1488
		}
1489
1490
		if ( empty( $actions ) ) {
1491
			return;
1492
		}
1493
1494
		self::add_menu_to_admin_bar();
1495
		self::add_forms_to_admin_bar( $actions );
1496
	}
1497
1498
	/**
1499
	 * @since 2.05.07
1500
	 */
1501
	public static function add_menu_to_admin_bar() {
1502
		global $wp_admin_bar;
1503
1504
		$wp_admin_bar->add_node(
1505
			array(
1506
				'id'    => 'frm-forms',
1507
				'title' => '<span class="ab-icon"></span><span class="ab-label">' . FrmAppHelper::get_menu_name() . '</span>',
1508
				'href'  => admin_url( 'admin.php?page=formidable' ),
1509
				'meta'  => array(
1510
					'title' => FrmAppHelper::get_menu_name(),
1511
				),
1512
			)
1513
		);
1514
	}
1515
1516
	/**
1517
	 * @since 2.05.07
1518
	 */
1519
	private static function add_forms_to_admin_bar( $actions ) {
1520
		global $wp_admin_bar;
1521
1522
		asort( $actions );
1523
1524
		foreach ( $actions as $form_id => $name ) {
1525
1526
			$wp_admin_bar->add_node(
1527
				array(
1528
					'parent' => 'frm-forms',
1529
					'id'     => 'edit_form_' . $form_id,
1530
					'title'  => empty( $name ) ? __( '(no title)', 'formidable' ) : $name,
1531
					'href'   => FrmForm::get_edit_link( $form_id ),
1532
				)
1533
			);
1534
		}
1535
	}
1536
1537
	/**
1538
	 * The formidable shortcode
1539
	 *
1540
	 * @param array $atts The params from the shortcode.
1541
	 */
1542
	public static function get_form_shortcode( $atts ) {
1543
		global $frm_vars;
1544
		if ( isset( $frm_vars['skip_shortcode'] ) && $frm_vars['skip_shortcode'] ) {
1545
			$sc = '[formidable';
1546
			$sc .= FrmAppHelper::array_to_html_params( $atts );
1547
			return $sc . ']';
1548
		}
1549
1550
		$shortcode_atts = shortcode_atts(
1551
			array(
1552
				'id'             => '',
1553
				'key'            => '',
1554
				'title'          => false,
1555
				'description'    => false,
1556
				'readonly'       => false,
1557
				'entry_id'       => false,
1558
				'fields'         => array(),
1559
				'exclude_fields' => array(),
1560
				'minimize'       => false,
1561
			),
1562
			$atts
1563
		);
1564
		do_action( 'formidable_shortcode_atts', $shortcode_atts, $atts );
1565
1566
		return self::show_form( $shortcode_atts['id'], $shortcode_atts['key'], $shortcode_atts['title'], $shortcode_atts['description'], $atts );
1567
	}
1568
1569
	public static function show_form( $id = '', $key = '', $title = false, $description = false, $atts = array() ) {
1570
		if ( empty( $id ) ) {
1571
			$id = $key;
1572
		}
1573
1574
		$form = self::maybe_get_form_to_show( $id );
1575
		if ( ! $form ) {
1576
			return __( 'Please select a valid form', 'formidable' );
1577
		}
1578
1579
		FrmAppController::maybe_update_styles();
1580
1581
		add_action( 'frm_load_form_hooks', 'FrmHooksController::trigger_load_form_hooks' );
1582
		FrmAppHelper::trigger_hook_load( 'form', $form );
1583
1584
		$form = apply_filters( 'frm_pre_display_form', $form );
1585
1586
		$frm_settings = FrmAppHelper::get_settings( array( 'current_form' => $form->id ) );
1587
1588
		if ( self::is_viewable_draft_form( $form ) ) {
1589
			// don't show a draft form on a page
1590
			$form = __( 'Please select a valid form', 'formidable' );
1591
		} elseif ( ! FrmForm::is_visible_to_user( $form ) ) {
1592
			$form = do_shortcode( $frm_settings->login_msg );
1593
		} else {
1594
			do_action( 'frm_pre_get_form', $form );
1595
			$form = self::get_form( $form, $title, $description, $atts );
1596
1597
			/**
1598
			 * Use this shortcode to check for external shortcodes that may span
1599
			 * across multiple fields in the customizable HTML
1600
			 *
1601
			 * @since 2.0.8
1602
			 */
1603
			$form = apply_filters( 'frm_filter_final_form', $form );
1604
		}
1605
1606
		return $form;
1607
	}
1608
1609
	private static function maybe_get_form_to_show( $id ) {
1610
		$form = false;
1611
1612
		if ( ! empty( $id ) ) { // no form id or key set
1613
			$form = FrmForm::getOne( $id );
1614
			if ( ! $form || $form->parent_form_id || $form->status == 'trash' ) {
1615
				$form = false;
1616
			}
1617
		}
1618
1619
		return $form;
1620
	}
1621
1622
	private static function is_viewable_draft_form( $form ) {
1623
		return $form->status == 'draft' && current_user_can( 'frm_edit_forms' ) && ! FrmAppHelper::is_preview_page();
1624
	}
1625
1626
	public static function get_form( $form, $title, $description, $atts = array() ) {
1627
		ob_start();
1628
1629
		do_action( 'frm_before_get_form', $atts );
1630
1631
		self::get_form_contents( $form, $title, $description, $atts );
1632
		self::enqueue_scripts( FrmForm::get_params( $form ) );
1633
1634
		$contents = ob_get_contents();
1635
		ob_end_clean();
1636
1637
		self::maybe_minimize_form( $atts, $contents );
1638
1639
		return $contents;
1640
	}
1641
1642
	public static function enqueue_scripts( $params ) {
1643
		do_action( 'frm_enqueue_form_scripts', $params );
1644
	}
1645
1646
	public static function get_form_contents( $form, $title, $description, $atts ) {
1647
		$params    = FrmForm::get_params( $form );
1648
		$errors    = self::get_saved_errors( $form, $params );
1649
		$fields    = FrmFieldsHelper::get_form_fields( $form->id, $errors );
1650
		$reset     = false;
1651
		$pass_args = compact( 'form', 'fields', 'errors', 'title', 'description', 'reset' );
1652
1653
		$handle_process_here = $params['action'] == 'create' && $params['posted_form_id'] == $form->id && $_POST;
1654
1655
		if ( ! $handle_process_here ) {
1656
			do_action( 'frm_display_form_action', $params, $fields, $form, $title, $description );
1657
			if ( apply_filters( 'frm_continue_to_new', true, $form->id, $params['action'] ) ) {
1658
				self::show_form_after_submit( $pass_args );
1659
			}
1660
		} elseif ( ! empty( $errors ) ) {
1661
			self::show_form_after_submit( $pass_args );
1662
1663
		} else {
1664
1665
			do_action( 'frm_validate_form_creation', $params, $fields, $form, $title, $description );
1666
1667
			if ( apply_filters( 'frm_continue_to_create', true, $form->id ) ) {
1668
				$entry_id                 = self::just_created_entry( $form->id );
1669
				$pass_args['entry_id']    = $entry_id;
1670
				$pass_args['reset']       = true;
1671
				$pass_args['conf_method'] = self::get_confirmation_method( compact( 'form', 'entry_id' ) );
1672
1673
				self::run_success_action( $pass_args );
1674
1675
				do_action(
1676
					'frm_after_entry_processed',
1677
					array(
1678
						'entry_id' => $entry_id,
1679
						'form'     => $form,
1680
					)
1681
				);
1682
			}
1683
		}
1684
	}
1685
1686
	/**
1687
	 * If the form was processed earlier (init), get the generated errors
1688
	 *
1689
	 * @since 2.05
1690
	 */
1691
	private static function get_saved_errors( $form, $params ) {
1692
		global $frm_vars;
1693
1694
		if ( $params['posted_form_id'] == $form->id && $_POST && isset( $frm_vars['created_entries'][ $form->id ] ) ) {
1695
			$errors = $frm_vars['created_entries'][ $form->id ]['errors'];
1696
		} else {
1697
			$errors = array();
1698
		}
1699
1700
		return $errors;
1701
	}
1702
1703
	/**
1704
	 * @since 2.2.7
1705
	 */
1706
	public static function just_created_entry( $form_id ) {
1707
		global $frm_vars;
1708
1709
		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;
1710
	}
1711
1712
	/**
1713
	 * @since 3.0
1714
	 */
1715
	private static function get_confirmation_method( $atts ) {
1716
		$opt    = 'success_action';
1717
		$method = ( isset( $atts['form']->options[ $opt ] ) && ! empty( $atts['form']->options[ $opt ] ) ) ? $atts['form']->options[ $opt ] : 'message';
1718
		$method = apply_filters( 'frm_success_filter', $method, $atts['form'], 'create' );
1719
1720
		if ( $method != 'message' && ( ! $atts['entry_id'] || ! is_numeric( $atts['entry_id'] ) ) ) {
1721
			$method = 'message';
1722
		}
1723
1724
		return $method;
1725
	}
1726
1727
	public static function maybe_trigger_redirect( $form, $params, $args ) {
1728
		if ( ! isset( $params['id'] ) ) {
1729
			global $frm_vars;
1730
			$params['id'] = $frm_vars['created_entries'][ $form->id ]['entry_id'];
1731
		}
1732
1733
		$conf_method = self::get_confirmation_method(
1734
			array(
1735
				'form'     => $form,
1736
				'entry_id' => $params['id'],
1737
			)
1738
		);
1739
1740
		if ( 'redirect' === $conf_method ) {
1741
			self::trigger_redirect( $form, $params, $args );
1742
		}
1743
	}
1744
1745
	public static function trigger_redirect( $form, $params, $args ) {
1746
		$success_args = array(
1747
			'action'      => $params['action'],
1748
			'conf_method' => 'redirect',
1749
			'form'        => $form,
1750
			'entry_id'    => $params['id'],
1751
		);
1752
1753
		if ( isset( $args['ajax'] ) ) {
1754
			$success_args['ajax'] = $args['ajax'];
1755
		}
1756
1757
		self::run_success_action( $success_args );
1758
	}
1759
1760
	/**
1761
	 * Used when the success action is not 'message'
1762
	 *
1763
	 * @since 2.05
1764
	 */
1765
	public static function run_success_action( $args ) {
1766
		$extra_args = $args;
1767
		unset( $extra_args['form'] );
1768
1769
		do_action( 'frm_success_action', $args['conf_method'], $args['form'], $args['form']->options, $args['entry_id'], $extra_args );
1770
1771
		$opt = ( ! isset( $args['action'] ) || $args['action'] == 'create' ) ? 'success' : 'edit';
1772
1773
		$args['success_opt'] = $opt;
1774
		if ( $args['conf_method'] == 'page' && is_numeric( $args['form']->options[ $opt . '_page_id' ] ) ) {
1775
			self::load_page_after_submit( $args );
1776
		} elseif ( $args['conf_method'] == 'redirect' ) {
1777
			self::redirect_after_submit( $args );
1778
		} else {
1779
			self::show_message_after_save( $args );
1780
		}
1781
	}
1782
1783
	/**
1784
	 * @since 3.0
1785
	 */
1786
	private static function load_page_after_submit( $args ) {
1787
		global $post;
1788
		$opt = $args['success_opt'];
1789
		if ( ! $post || $args['form']->options[ $opt . '_page_id' ] != $post->ID ) {
1790
			$page     = get_post( $args['form']->options[ $opt . '_page_id' ] );
1791
			$old_post = $post;
1792
			$post     = $page;
1793
			$content  = apply_filters( 'frm_content', $page->post_content, $args['form'], $args['entry_id'] );
1794
			echo apply_filters( 'the_content', $content ); // WPCS: XSS ok.
1795
			$post = $old_post;
1796
		}
1797
	}
1798
1799
	/**
1800
	 * @since 3.0
1801
	 */
1802
	private static function redirect_after_submit( $args ) {
1803
		global $frm_vars;
1804
1805
		add_filter( 'frm_use_wpautop', '__return_false' );
1806
1807
		$opt         = $args['success_opt'];
1808
		$success_url = trim( $args['form']->options[ $opt . '_url' ] );
1809
		$success_url = apply_filters( 'frm_content', $success_url, $args['form'], $args['entry_id'] );
1810
		$success_url = do_shortcode( $success_url );
1811
1812
		$success_msg = isset( $args['form']->options[ $opt . '_msg' ] ) ? $args['form']->options[ $opt . '_msg' ] : __( 'Please wait while you are redirected.', 'formidable' );
1813
1814
		$redirect_msg = self::get_redirect_message( $success_url, $success_msg, $args );
1815
1816
		$args['id'] = $args['entry_id'];
1817
		FrmEntriesController::delete_entry_before_redirect( $success_url, $args['form'], $args );
1818
1819
		add_filter( 'frm_redirect_url', 'FrmEntriesController::prepare_redirect_url' );
1820
		$success_url = apply_filters( 'frm_redirect_url', $success_url, $args['form'], $args );
1821
1822
		$doing_ajax = FrmAppHelper::doing_ajax();
1823
1824
		if ( isset( $args['ajax'] ) && $args['ajax'] && $doing_ajax ) {
1825
			echo json_encode( array( 'redirect' => $success_url ) );
1826
			wp_die();
1827
		} elseif ( ! headers_sent() ) {
1828
			wp_redirect( esc_url_raw( $success_url ) );
1829
			die(); // do not use wp_die or redirect fails
1830
		} else {
1831
			add_filter( 'frm_use_wpautop', '__return_true' );
1832
1833
			echo $redirect_msg; // WPCS: XSS ok.
1834
			echo "<script type='text/javascript'>window.onload = function(){setTimeout(window.location='" . esc_url_raw( $success_url ) . "', 8000);}</script>";
1835
		}
1836
	}
1837
1838
	/**
1839
	 * @since 3.0
1840
	 *
1841
	 * @param string $success_url
1842
	 * @param string $success_msg
1843
	 * @param array $args
1844
	 */
1845
	private static function get_redirect_message( $success_url, $success_msg, $args ) {
1846
		$redirect_msg = '<div class="' . esc_attr( FrmFormsHelper::get_form_style_class( $args['form'] ) ) . '"><div class="frm-redirect-msg frm_message" role="status">' . $success_msg . '<br/>' .
1847
			/* translators: %1$s: Start link HTML, %2$s: End link HTML */
1848
			sprintf( __( '%1$sClick here%2$s if you are not automatically redirected.', 'formidable' ), '<a href="' . esc_url( $success_url ) . '">', '</a>' ) .
1849
			'</div></div>';
1850
1851
		$redirect_args = array(
1852
			'entry_id' => $args['entry_id'],
1853
			'form_id'  => $args['form']->id,
1854
			'form'     => $args['form'],
1855
		);
1856
1857
		return apply_filters( 'frm_redirect_msg', $redirect_msg, $redirect_args );
1858
	}
1859
1860
	/**
1861
	 * Prepare to show the success message and empty form after submit
1862
	 *
1863
	 * @since 2.05
1864
	 */
1865
	public static function show_message_after_save( $atts ) {
1866
		$atts['message'] = self::prepare_submit_message( $atts['form'], $atts['entry_id'] );
1867
1868
		if ( ! isset( $atts['form']->options['show_form'] ) || $atts['form']->options['show_form'] ) {
1869
			self::show_form_after_submit( $atts );
1870
		} else {
1871
			self::show_lone_success_messsage( $atts );
1872
		}
1873
	}
1874
1875
	/**
1876
	 * Show an empty form
1877
	 *
1878
	 * @since 2.05
1879
	 */
1880
	private static function show_form_after_submit( $args ) {
1881
		self::fill_atts_for_form_display( $args );
1882
1883
		$errors      = $args['errors'];
1884
		$message     = $args['message'];
1885
		$form        = $args['form'];
1886
		$title       = $args['title'];
1887
		$description = $args['description'];
1888
1889
		if ( empty( $args['fields'] ) ) {
1890
			$values = array();
1891
		} else {
1892
			$values = FrmEntriesHelper::setup_new_vars( $args['fields'], $form, $args['reset'] );
1893
		}
1894
		unset( $args );
1895
1896
		$include_form_tag = apply_filters( 'frm_include_form_tag', true, $form );
1897
1898
		$frm_settings = FrmAppHelper::get_settings();
1899
		$submit       = isset( $form->options['submit_value'] ) ? $form->options['submit_value'] : $frm_settings->submit_value;
1900
1901
		global $frm_vars;
1902
		self::maybe_load_css( $form, $values['custom_style'], $frm_vars['load_css'] );
1903
1904
		$message_placement = self::message_placement( $form, $message );
1905
1906
		include( FrmAppHelper::plugin_path() . '/classes/views/frm-entries/new.php' );
1907
	}
1908
1909
	/**
1910
	 * @return string - 'before', 'after', or 'submit'
1911
	 *
1912
	 * @since 4.05.02
1913
	 */
1914
	private static function message_placement( $form, $message ) {
1915
		$place = 'before';
1916
1917
		if ( $message && isset( $form->options['form_class'] ) ) {
1918
			if ( strpos( $form->options['form_class'], 'frm_below_success' ) !== false ) {
1919
				$place = 'after';
1920
			} elseif ( strpos( $form->options['form_class'], 'frm_inline_success' ) !== false ) {
1921
				$place = 'submit';
1922
			}
1923
		}
1924
1925
		/**
1926
		 * @return string - 'before' or 'after'
1927
		 *
1928
		 * @since 4.05.02
1929
		 */
1930
		return apply_filters( 'frm_message_placement', $place, compact( 'form', 'message' ) );
1931
	}
1932
1933
	/**
1934
	 * Get all the values needed on the new.php entry page
1935
	 *
1936
	 * @since 2.05
1937
	 */
1938
	private static function fill_atts_for_form_display( &$args ) {
1939
		$defaults = array(
1940
			'errors'      => array(),
1941
			'message'     => '',
1942
			'fields'      => array(),
1943
			'form'        => array(),
1944
			'title'       => true,
1945
			'description' => false,
1946
			'reset'       => false,
1947
		);
1948
		$args     = wp_parse_args( $args, $defaults );
1949
	}
1950
1951
	/**
1952
	 * Show the success message without the form
1953
	 *
1954
	 * @since 2.05
1955
	 */
1956
	private static function show_lone_success_messsage( $atts ) {
1957
		global $frm_vars;
1958
		$values = FrmEntriesHelper::setup_new_vars( $atts['fields'], $atts['form'], true );
1959
		self::maybe_load_css( $atts['form'], $values['custom_style'], $frm_vars['load_css'] );
1960
1961
		$include_extra_container = 'frm_forms' . FrmFormsHelper::get_form_style_class( $values );
1962
1963
		$errors  = array();
1964
		$form    = $atts['form'];
1965
		$message = $atts['message'];
1966
1967
		include( FrmAppHelper::plugin_path() . '/classes/views/frm-entries/errors.php' );
1968
	}
1969
1970
	/**
1971
	 * Prepare the success message before it's shown
1972
	 *
1973
	 * @since 2.05
1974
	 */
1975
	private static function prepare_submit_message( $form, $entry_id ) {
1976
		$frm_settings = FrmAppHelper::get_settings( array( 'current_form' => $form->id ) );
1977
1978
		if ( $entry_id && is_numeric( $entry_id ) ) {
1979
			$message = isset( $form->options['success_msg'] ) ? $form->options['success_msg'] : $frm_settings->success_msg;
1980
			$class   = 'frm_message';
1981
		} else {
1982
			$message = $frm_settings->failed_msg;
1983
			$class   = FrmFormsHelper::form_error_class();
1984
		}
1985
1986
		$message = FrmFormsHelper::get_success_message( compact( 'message', 'form', 'entry_id', 'class' ) );
1987
1988
		return apply_filters( 'frm_main_feedback', $message, $form, $entry_id );
1989
	}
1990
1991
	public static function front_head() {
1992
		$version = FrmAppHelper::plugin_version();
1993
		$suffix  = FrmAppHelper::js_suffix();
1994
1995
		if ( ! empty( $suffix ) && self::has_combo_js_file() ) {
1996
			wp_register_script( 'formidable', FrmAppHelper::plugin_url() . '/js/frm.min.js', array( 'jquery' ), $version, true );
1997
		} else {
1998
			wp_register_script( 'formidable', FrmAppHelper::plugin_url() . "/js/formidable{$suffix}.js", array( 'jquery' ), $version, true );
1999
		}
2000
2001
		add_filter( 'script_loader_tag', 'FrmFormsController::defer_script_loading', 10, 2 );
2002
2003
		if ( FrmAppHelper::is_admin() ) {
2004
			// don't load this in back-end
2005
			return;
2006
		}
2007
2008
		FrmAppHelper::localize_script( 'front' );
2009
		FrmStylesController::enqueue_css( 'register' );
2010
	}
2011
2012
	/**
2013
	 * @since 3.0
2014
	 */
2015
	public static function has_combo_js_file() {
2016
		return is_readable( FrmAppHelper::plugin_path() . '/js/frm.min.js' );
2017
	}
2018
2019
	public static function maybe_load_css( $form, $this_load, $global_load ) {
2020
		$load_css = FrmForm::is_form_loaded( $form, $this_load, $global_load );
2021
2022
		if ( ! $load_css ) {
2023
			return;
2024
		}
2025
2026
		global $frm_vars;
2027
		self::footer_js( 'header' );
2028
		$frm_vars['css_loaded'] = true;
2029
2030
		self::load_late_css();
2031
	}
2032
2033
	/**
2034
	 * If css is loaded only on applicable pages, include it before the form loads
2035
	 * to prevent a flash of unstyled form.
2036
	 *
2037
	 * @since 4.01
2038
	 */
2039
	private static function load_late_css() {
2040
		$frm_settings = FrmAppHelper::get_settings();
2041
		$late_css = $frm_settings->load_style === 'dynamic';
2042
		if ( ! $late_css ) {
2043
			return;
2044
		}
2045
2046
		global $wp_styles;
2047
		if ( is_array( $wp_styles->queue ) && in_array( 'formidable', $wp_styles->queue ) ) {
2048
			wp_print_styles( 'formidable' );
2049
		}
2050
	}
2051
2052
	public static function defer_script_loading( $tag, $handle ) {
2053
		if ( 'recaptcha-api' == $handle && ! strpos( $tag, 'defer' ) ) {
2054
			$tag = str_replace( ' src', ' defer="defer" async="async" src', $tag );
2055
		}
2056
2057
		return $tag;
2058
	}
2059
2060
	public static function footer_js( $location = 'footer' ) {
2061
		global $frm_vars;
2062
2063
		FrmStylesController::enqueue_css();
2064
2065
		if ( ! FrmAppHelper::is_admin() && $location != 'header' && ! empty( $frm_vars['forms_loaded'] ) ) {
2066
			// load formidable js
2067
			wp_enqueue_script( 'formidable' );
2068
		}
2069
	}
2070
2071
	/**
2072
	 * @since 2.0.8
2073
	 */
2074
	private static function maybe_minimize_form( $atts, &$content ) {
2075
		// check if minimizing is turned on
2076
		if ( self::is_minification_on( $atts ) ) {
2077
			$content = str_replace( array( "\r\n", "\r", "\n", "\t", '    ' ), '', $content );
2078
		}
2079
	}
2080
2081
	/**
2082
	 * @since 2.0.8
2083
	 * @return boolean
2084
	 */
2085
	private static function is_minification_on( $atts ) {
2086
		return isset( $atts['minimize'] ) && ! empty( $atts['minimize'] );
2087
	}
2088
2089
	/**
2090
	 * @deprecated 4.0
2091
	 */
2092
	public static function new_form( $values = array() ) {
2093
		FrmDeprecated::new_form( $values );
2094
	}
2095
2096
	/**
2097
	 * @deprecated 4.0
2098
	 */
2099
	public static function create( $values = array() ) {
2100
		_deprecated_function( __METHOD__, '4.0', 'FrmFormsController::update' );
2101
		self::update( $values );
2102
	}
2103
2104
	/**
2105
	 * @deprecated 1.07.05
2106
	 * @codeCoverageIgnore
2107
	 */
2108
	public static function add_default_templates( $path, $default = true, $template = true ) {
2109
		FrmDeprecated::add_default_templates( $path, $default, $template );
2110
	}
2111
2112
	/**
2113
	 * @deprecated 3.0
2114
	 * @codeCoverageIgnore
2115
	 */
2116
	public static function bulk_create_template( $ids ) {
2117
		return FrmDeprecated::bulk_create_template( $ids );
2118
	}
2119
2120
	/**
2121
	 * @deprecated 2.03
2122
	 * @codeCoverageIgnore
2123
	 */
2124
	public static function register_pro_scripts() {
2125
		FrmDeprecated::register_pro_scripts();
2126
	}
2127
2128
	/**
2129
	 * @deprecated 3.0
2130
	 * @codeCoverageIgnore
2131
	 */
2132
	public static function edit_key() {
2133
		FrmDeprecated::edit_key();
2134
	}
2135
2136
	/**
2137
	 * @deprecated 3.0
2138
	 * @codeCoverageIgnore
2139
	 */
2140
	public static function edit_description() {
2141
		FrmDeprecated::edit_description();
2142
	}
2143
}
2144