Completed
Push — master ( b21fa6...e7d730 )
by Stephanie
02:17
created

FrmFormsController::load_wp()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

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