Completed
Push — master ( 1305f3...115b63 )
by Stephanie
02:57
created

FrmFormsController::enqueue_scripts()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

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

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

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

}

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

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

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

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

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

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

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

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

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