Completed
Push — master ( 202d0c...b3ff26 )
by Stephanie
02:35
created

FrmFormsController::register_widgets()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 0
dl 0
loc 4
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
    public static function list_form() {
41
        FrmAppHelper::permission_check('frm_view_forms');
42
43
		$params = FrmForm::list_page_params();
44
        $errors = self::process_bulk_form_actions( array());
45
        $errors = apply_filters('frm_admin_list_form_action', $errors);
46
47
		return self::display_forms_list( $params, '', $errors );
48
    }
49
50
	public static function new_form( $values = array() ) {
51
        FrmAppHelper::permission_check('frm_edit_forms');
52
53
        global $frm_vars;
54
55
        $action = isset($_REQUEST['frm_action']) ? 'frm_action' : 'action';
56
		$action = empty( $values ) ? FrmAppHelper::get_param( $action, '', 'get', 'sanitize_title' ) : $values[ $action ];
57
58
		if ( $action == 'create' ) {
59
			self::create($values);
60
			return;
61
		} else if ( $action == 'new' ) {
62
			$frm_field_selection = FrmField::field_selection();
63
            $values = FrmFormsHelper::setup_new_vars($values);
64
            $id = FrmForm::create( $values );
65
            $form = FrmForm::getOne($id);
66
67
			self::create_default_email_action( $form );
68
69
			$all_templates = FrmForm::getAll( array( 'is_template' => 1 ), 'name' );
70
71
            $values['id'] = $id;
72
			require( FrmAppHelper::plugin_path() . '/classes/views/frm-forms/new.php' );
73
        }
74
    }
75
76
	/**
77
	 * Create the default email action
78
	 *
79
	 * @since 2.02.11
80
	 *
81
	 * @param object $form
82
	 */
83
    private static function create_default_email_action( $form ) {
84
    	$create_email = apply_filters( 'frm_create_default_email_action', true, $form );
85
86
	    if ( $create_email ) {
87
		    $action_control = FrmFormActionsController::get_form_actions( 'email' );
88
		    $action_control->create( $form->id );
89
	    }
90
    }
91
92
	public static function create( $values = array() ) {
93
        FrmAppHelper::permission_check('frm_edit_forms');
94
95
        global $frm_vars;
96
        if ( empty( $values ) ) {
97
            $values = $_POST;
98
        }
99
100
        //Set radio button and checkbox meta equal to "other" value
101
        if ( FrmAppHelper::pro_is_installed() ) {
102
            $values = FrmProEntry::mod_other_vals( $values, 'back' );
103
        }
104
105
		$id = isset($values['id']) ? absint( $values['id'] ) : FrmAppHelper::get_param( 'id', '', 'get', 'absint' );
106
107
        if ( ! current_user_can( 'frm_edit_forms' ) || ( $_POST && ( ! isset( $values['frm_save_form'] ) || ! wp_verify_nonce( $values['frm_save_form'], 'frm_save_form_nonce' ) ) ) ) {
108
            $frm_settings = FrmAppHelper::get_settings();
109
            $errors = array( 'form' => $frm_settings->admin_permission );
110
        } else {
111
            $errors = FrmForm::validate($values);
112
        }
113
114
        if ( count($errors) > 0 ) {
115
            $hide_preview = true;
116
			$frm_field_selection = FrmField::field_selection();
117
            $form = FrmForm::getOne( $id );
118
            $fields = FrmField::get_all_for_form($id);
119
120
            $values = FrmAppHelper::setup_edit_vars($form, 'forms', $fields, true);
121
			$all_templates = FrmForm::getAll( array( 'is_template' => 1 ), 'name' );
122
123
			require( FrmAppHelper::plugin_path() . '/classes/views/frm-forms/new.php' );
124
        } else {
125
            FrmForm::update( $id, $values, true );
126
			$url = admin_url( 'admin.php?page=formidable&frm_action=settings&id=' . $id );
127
			die( FrmAppHelper::js_redirect( $url ) );
128
        }
129
    }
130
131
    public static function edit( $values = false ) {
132
        FrmAppHelper::permission_check('frm_edit_forms');
133
134
		$id = isset( $values['id'] ) ? absint( $values['id'] ) : FrmAppHelper::get_param( 'id', '', 'get', 'absint' );
135
        return self::get_edit_vars($id);
136
    }
137
138
    public static function settings( $id = false, $message = '' ) {
139
        FrmAppHelper::permission_check('frm_edit_forms');
140
141
        if ( ! $id || ! is_numeric($id) ) {
142
			$id = FrmAppHelper::get_param( 'id', '', 'get', 'absint' );
143
        }
144
		return self::get_settings_vars( $id, array(), $message );
145
    }
146
147
    public static function update_settings() {
148
        FrmAppHelper::permission_check('frm_edit_forms');
149
150
		$id = FrmAppHelper::get_param( 'id', '', 'get', 'absint' );
151
152
        $errors = FrmForm::validate($_POST);
153
        if ( count($errors) > 0 ) {
154
            return self::get_settings_vars($id, $errors);
155
        }
156
157
        do_action('frm_before_update_form_settings', $id);
158
159
		FrmForm::update( $id, $_POST );
160
161
        $message = __( 'Settings Successfully Updated', 'formidable' );
162
		return self::get_settings_vars( $id, array(), $message );
163
    }
164
165
	public static function edit_key() {
166
		$values = self::edit_in_place_value( 'form_key' );
167
		echo wp_kses( stripslashes( FrmForm::getKeyById( $values['form_id'] ) ), array() );
168
		wp_die();
169
	}
170
171
	public static function edit_description() {
172
		$values = self::edit_in_place_value( 'description' );
173
		echo wp_kses_post( FrmAppHelper::use_wpautop( stripslashes( $values['description'] ) ) );
174
		wp_die();
175
	}
176
177
	private static function edit_in_place_value( $field ) {
178
		check_ajax_referer( 'frm_ajax', 'nonce' );
179
		FrmAppHelper::permission_check('frm_edit_forms', 'hide');
180
181
		$form_id = FrmAppHelper::get_post_param( 'form_id', '', 'absint' );
182
		$value = FrmAppHelper::get_post_param( 'update_value', '', 'wp_filter_post_kses' );
183
184
		$values = array( $field => trim( $value ) );
185
		FrmForm::update( $form_id, $values );
186
		$values['form_id'] = $form_id;
187
188
		return $values;
189
	}
190
191
	public static function update( $values = array() ) {
192
		if ( empty( $values ) ) {
193
            $values = $_POST;
194
        }
195
196
        //Set radio button and checkbox meta equal to "other" value
197
        if ( FrmAppHelper::pro_is_installed() ) {
198
            $values = FrmProEntry::mod_other_vals( $values, 'back' );
199
        }
200
201
        $errors = FrmForm::validate( $values );
202
        $permission_error = FrmAppHelper::permission_nonce_error( 'frm_edit_forms', 'frm_save_form', 'frm_save_form_nonce' );
203
        if ( $permission_error !== false ) {
204
            $errors['form'] = $permission_error;
205
        }
206
207
		$id = isset( $values['id'] ) ? absint( $values['id'] ) : FrmAppHelper::get_param( 'id', '', 'get', 'absint' );
208
209
		if ( count( $errors ) > 0 ) {
210
            return self::get_edit_vars( $id, $errors );
211
		} else {
212
            FrmForm::update( $id, $values );
213
            $message = __( 'Form was Successfully Updated', 'formidable' );
214
            if ( defined( 'DOING_AJAX' ) ) {
215
				wp_die( $message );
216
            }
217
			return self::get_edit_vars( $id, array(), $message );
218
        }
219
    }
220
221
    public static function bulk_create_template( $ids ) {
222
        FrmAppHelper::permission_check( 'frm_edit_forms' );
223
224
        foreach ( $ids as $id ) {
225
            FrmForm::duplicate( $id, true, true );
226
        }
227
228
        return __( 'Form template was Successfully Created', 'formidable' );
229
    }
230
231
	/**
232
	 * Redirect to the url for creating from a template
233
	 * Also delete the current form
234
	 * @since 2.0
235
	 */
236
	public static function _create_from_template() {
237
		FrmAppHelper::permission_check('frm_edit_forms');
238
		check_ajax_referer( 'frm_ajax', 'nonce' );
239
240
		$current_form = FrmAppHelper::get_param( 'this_form', '', 'get', 'absint' );
241
		$template_id = FrmAppHelper::get_param( 'id', '', 'get', 'absint' );
242
243
		if ( $current_form ) {
244
			FrmForm::destroy( $current_form );
245
		}
246
247
		echo esc_url_raw( admin_url( 'admin.php?page=formidable&action=duplicate&id=' . $template_id ) );
248
		wp_die();
249
	}
250
251
    public static function duplicate() {
252
        FrmAppHelper::permission_check('frm_edit_forms');
253
254
		$params = FrmForm::list_page_params();
255
        $form = FrmForm::duplicate( $params['id'], $params['template'], true );
256
        $message = $params['template'] ? __( 'Form template was Successfully Created', 'formidable' ) : __( 'Form was Successfully Copied', 'formidable' );
257
        if ( $form ) {
258
			return self::get_edit_vars( $form, array(), $message, true );
259
        } else {
260
            return self::display_forms_list($params, __( 'There was a problem creating the new template.', 'formidable' ));
261
        }
262
    }
263
264
    public static function page_preview() {
265
		$params = FrmForm::list_page_params();
266
        if ( ! $params['form'] ) {
267
            return;
268
        }
269
270
        $form = FrmForm::getOne( $params['form'] );
271
        if ( ! $form ) {
272
            return;
273
        }
274
        return self::show_form( $form->id, '', true, true );
275
    }
276
277
    public static function preview() {
278
        do_action( 'frm_wp' );
279
280
        global $frm_vars;
281
        $frm_vars['preview'] = true;
282
283
        if ( ! defined( 'ABSPATH' ) && ! defined( 'XMLRPC_REQUEST' ) ) {
284
            global $wp;
285
            $root = dirname( dirname( dirname( dirname( __FILE__ ) ) ) );
286
			include_once( $root . '/wp-config.php' );
287
            $wp->init();
288
            $wp->register_globals();
289
        }
290
291
		header( 'Content-Type: text/html; charset=' . get_option( 'blog_charset' ) );
292
293
		$key = FrmAppHelper::simple_get( 'form', 'sanitize_title' );
294
		if ( $key == '' ) {
295
			$key = FrmAppHelper::get_post_param( 'form', '', 'sanitize_title' );
296
		}
297
298
		$form = FrmForm::getAll( array( 'form_key' => $key ), '', 1 );
299
		if ( empty( $form ) ) {
300
			$form = FrmForm::getAll( array(), '', 1 );
301
        }
302
303
		require( FrmAppHelper::plugin_path() . '/classes/views/frm-entries/direct.php' );
304
        wp_die();
305
    }
306
307
	public static function register_pro_scripts() {
308
		_deprecated_function( __FUNCTION__, '2.03', 'FrmProEntriesController::register_scripts' );
309
		if ( FrmAppHelper::pro_is_installed() ) {
310
			FrmProEntriesController::register_scripts();
311
		}
312
	}
313
314
    public static function untrash() {
315
		self::change_form_status( 'untrash' );
316
    }
317
318
	public static function bulk_untrash( $ids ) {
319
        FrmAppHelper::permission_check('frm_edit_forms');
320
321
        $count = FrmForm::set_status( $ids, 'published' );
322
323
        $message = sprintf(_n( '%1$s form restored from the Trash.', '%1$s forms restored from the Trash.', $count, 'formidable' ), 1 );
324
        return $message;
325
    }
326
327
    public static function trash() {
328
		self::change_form_status( 'trash' );
329
    }
330
331
	/**
332
	 * @param string $status
333
	 *
334
	 * @return int The number of forms changed
335
	 */
336
	public static function change_form_status( $status ) {
337
		$available_status = array(
338
			'untrash' => array(
339
				'permission' => 'frm_edit_forms',
340
				'new_status' => 'published',
341
			),
342
			'trash'   => array(
343
				'permission' => 'frm_delete_forms',
344
				'new_status' => 'trash',
345
			),
346
		);
347
348
		if ( ! isset( $available_status[ $status ] ) ) {
349
			return;
350
		}
351
352
		FrmAppHelper::permission_check( $available_status[ $status ]['permission'] );
353
354
		$params = FrmForm::list_page_params();
355
356
		//check nonce url
357
		check_admin_referer( $status . '_form_' . $params['id'] );
358
359
		$count = 0;
360
		if ( FrmForm::set_status( $params['id'], $available_status[ $status ]['new_status'] ) ) {
361
			$count++;
362
		}
363
364
		$form_type = FrmAppHelper::get_simple_request( array(
365
			'param' => 'form_type',
366
			'type' => 'request',
367
		) );
368
369
		$available_status['untrash']['message'] = sprintf(_n( '%1$s form restored from the Trash.', '%1$s forms restored from the Trash.', $count, 'formidable' ), $count );
370
		$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>' );
371
372
		$message = $available_status[ $status ]['message'];
373
374
		self::display_forms_list( $params, $message );
375
	}
376
377
	public static function bulk_trash( $ids ) {
378
        FrmAppHelper::permission_check('frm_delete_forms');
379
380
        $count = 0;
381
        foreach ( $ids as $id ) {
382
            if ( FrmForm::trash( $id ) ) {
383
                $count++;
384
            }
385
        }
386
387
		$current_page = FrmAppHelper::get_simple_request( array(
388
			'param' => 'form_type',
389
			'type' => 'request',
390
		) );
391
		$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>' );
392
393
        return $message;
394
    }
395
396
    public static function destroy() {
397
        FrmAppHelper::permission_check('frm_delete_forms');
398
399
		$params = FrmForm::list_page_params();
400
401
        //check nonce url
402
        check_admin_referer('destroy_form_' . $params['id']);
403
404
        $count = 0;
405
        if ( FrmForm::destroy( $params['id'] ) ) {
406
            $count++;
407
        }
408
409
        $message = sprintf(_n( '%1$s form permanently deleted.', '%1$s forms permanently deleted.', $count, 'formidable' ), $count);
410
411
		self::display_forms_list( $params, $message );
412
    }
413
414
	public static function bulk_destroy( $ids ) {
415
        FrmAppHelper::permission_check('frm_delete_forms');
416
417
        $count = 0;
418
        foreach ( $ids as $id ) {
419
            $d = FrmForm::destroy( $id );
420
            if ( $d ) {
421
                $count++;
422
            }
423
        }
424
425
        $message = sprintf(_n( '%1$s form permanently deleted.', '%1$s forms permanently deleted.', $count, 'formidable' ), $count);
426
427
        return $message;
428
    }
429
430
    private static function delete_all() {
431
        //check nonce url
432
        $permission_error = FrmAppHelper::permission_nonce_error('frm_delete_forms', '_wpnonce', 'bulk-toplevel_page_formidable');
433
        if ( $permission_error !== false ) {
434
			self::display_forms_list( array(), '', array( $permission_error ) );
435
            return;
436
        }
437
438
		$count = FrmForm::scheduled_delete( time() );
439
        $message = sprintf(_n( '%1$s form permanently deleted.', '%1$s forms permanently deleted.', $count, 'formidable' ), $count);
440
441
		self::display_forms_list( array(), $message );
442
    }
443
444
	public static function scheduled_delete( $delete_timestamp = '' ) {
445
		_deprecated_function( __FUNCTION__, '2.0.9', 'FrmForm::scheduled_delete' );
446
		return FrmForm::scheduled_delete( $delete_timestamp );
447
	}
448
449
	/**
450
	* Inserts Formidable button
451
	* Hook exists since 2.5.0
452
	*
453
	* @since 2.0.15
454
	*/
455
	public static function insert_form_button() {
456
		if ( current_user_can('frm_view_forms') ) {
457
			$menu_name = FrmAppHelper::get_menu_name();
458
			$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' ) . '">
459
				<span class="frm-buttons-icon wp-media-buttons-icon"></span> ' .
460
				$menu_name . '</a>';
461
			echo wp_kses_post( $content );
462
		}
463
	}
464
465
    public static function insert_form_popup() {
466
		$page = basename( FrmAppHelper::get_server_value( 'PHP_SELF' ) );
467
		if ( ! in_array( $page, array( 'post.php', 'page.php', 'page-new.php', 'post-new.php' ) ) ) {
468
            return;
469
        }
470
471
        FrmAppHelper::load_admin_wide_js();
472
473
        $shortcodes = array(
474
			'formidable' => array(
475
				'name'  => __( 'Form', 'formidable' ),
476
				'label' => __( 'Insert a Form', 'formidable' ),
477
			),
478
        );
479
480
        $shortcodes = apply_filters('frm_popup_shortcodes', $shortcodes);
481
482
		include( FrmAppHelper::plugin_path() . '/classes/views/frm-forms/insert_form_popup.php' );
483
    }
484
485
    public static function get_shortcode_opts() {
486
		FrmAppHelper::permission_check('frm_view_forms');
487
        check_ajax_referer( 'frm_ajax', 'nonce' );
488
489
		$shortcode = FrmAppHelper::get_post_param( 'shortcode', '', 'sanitize_text_field' );
490
        if ( empty($shortcode) ) {
491
            wp_die();
492
        }
493
494
		echo '<div id="sc-opts-' . esc_attr( $shortcode ) . '" class="frm_shortcode_option">';
495
		echo '<input type="radio" name="frmsc" value="' . esc_attr( $shortcode ) . '" id="sc-' . esc_attr( $shortcode ) . '" class="frm_hidden" />';
496
497
        $form_id = '';
498
        $opts = array();
499
		switch ( $shortcode ) {
500
            case 'formidable':
501
                $opts = array(
502
					'form_id'       => 'id',
503
                    //'key' => ',
504
					'title'         => array(
505
						'val'   => 1,
506
						'label' => __( 'Display form title', 'formidable' ),
507
					),
508
					'description'   => array(
509
						'val'   => 1,
510
						'label' => __( 'Display form description', 'formidable' ),
511
					),
512
					'minimize'      => array(
513
						'val'   => 1,
514
						'label' => __( 'Minimize form HTML', 'formidable' ),
515
					),
516
                );
517
        }
518
		$opts = apply_filters( 'frm_sc_popup_opts', $opts, $shortcode );
519
520
		if ( isset( $opts['form_id'] ) && is_string( $opts['form_id'] ) ) {
521
			// allow other shortcodes to use the required form id option
522
			$form_id = $opts['form_id'];
523
			unset( $opts['form_id'] );
524
		}
525
526
		include( FrmAppHelper::plugin_path() . '/classes/views/frm-forms/shortcode_opts.php' );
527
528
        echo '</div>';
529
530
        wp_die();
531
    }
532
533
	public static function display_forms_list( $params = array(), $message = '', $errors = array(), $deprecated_errors = array() ) {
534
        FrmAppHelper::permission_check( 'frm_view_forms' );
535
		if ( ! empty( $deprecated_errors ) ) {
536
			$errors = $deprecated_errors;
537
			_deprecated_argument( 'errors', '2.0.8' );
538
		}
539
540
        global $wpdb, $frm_vars;
541
542
		if ( empty( $params ) ) {
543
			$params = FrmForm::list_page_params();
544
        }
545
546
        $wp_list_table = new FrmFormsListHelper( compact( 'params' ) );
547
548
        $pagenum = $wp_list_table->get_pagenum();
549
550
        $wp_list_table->prepare_items();
551
552
        $total_pages = $wp_list_table->get_pagination_arg( 'total_pages' );
553
        if ( $pagenum > $total_pages && $total_pages > 0 ) {
554
			wp_redirect( esc_url_raw( add_query_arg( 'paged', $total_pages ) ) );
555
            die();
556
        }
557
558
		require( FrmAppHelper::plugin_path() . '/classes/views/frm-forms/list.php' );
559
    }
560
561
	public static function get_columns( $columns ) {
562
	    $columns['cb'] = '<input type="checkbox" />';
563
	    $columns['id'] = 'ID';
564
565
		$type = FrmAppHelper::get_simple_request( array(
566
			'param'   => 'form_type',
567
			'type'    => 'request',
568
			'default' => 'published',
569
		) );
570
571
        if ( 'template' == $type ) {
572
            $columns['name']        = __( 'Template Name', 'formidable' );
573
            $columns['type']        = __( 'Type', 'formidable' );
574
            $columns['form_key']    = __( 'Key', 'formidable' );
575
        } else {
576
            $columns['name']        = __( 'Form Title', 'formidable' );
577
            $columns['entries']     = __( 'Entries', 'formidable' );
578
            $columns['form_key']    = __( 'Key', 'formidable' );
579
            $columns['shortcode']   = __( 'Shortcodes', 'formidable' );
580
        }
581
582
        $columns['created_at'] = __( 'Date', 'formidable' );
583
584
		add_screen_option( 'per_page', array(
585
			'label'   => __( 'Forms', 'formidable' ),
586
			'default' => 20,
587
			'option'  => 'formidable_page_formidable_per_page',
588
		) );
589
590
        return $columns;
591
	}
592
593
	public static function get_sortable_columns() {
594
		return array(
595
			'id'            => 'id',
596
			'name'          => 'name',
597
			'description'   => 'description',
598
			'form_key'      => 'form_key',
599
			'created_at'    => 'created_at',
600
		);
601
	}
602
603
	public static function hidden_columns( $hidden_columns ) {
604
		$type = FrmAppHelper::get_simple_request( array(
605
			'param' => 'form_type',
606
			'type'  => 'request',
607
		) );
608
609
		if ( $type === 'template' ) {
610
			$hidden_columns[] = 'id';
611
			$hidden_columns[] = 'form_key';
612
		}
613
614
		return $hidden_columns;
615
	}
616
617
	public static function save_per_page( $save, $option, $value ) {
618
        if ( $option == 'formidable_page_formidable_per_page' ) {
619
            $save = (int) $value;
620
        }
621
        return $save;
622
    }
623
624
	private static function get_edit_vars( $id, $errors = array(), $message = '', $create_link = false ) {
625
        global $frm_vars;
626
627
        $form = FrmForm::getOne( $id );
628
        if ( ! $form ) {
629
            wp_die( __( 'You are trying to edit a form that does not exist.', 'formidable' ) );
630
        }
631
632
        if ( $form->parent_form_id ) {
633
			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>' ));
634
        }
635
636
		$frm_field_selection = FrmField::field_selection();
637
        $fields = FrmField::get_all_for_form($form->id);
638
639
        // Automatically add end section fields if they don't exist (2.0 migration)
640
        $reset_fields = false;
641
        FrmFormsHelper::auto_add_end_section_fields( $form, $fields, $reset_fields );
642
643
        if ( $reset_fields ) {
644
            $fields = FrmField::get_all_for_form( $form->id, '', 'exclude' );
645
        }
646
647
        unset($end_section_values, $last_order, $open, $reset_fields);
648
649
		$args = array( 'parent_form_id' => $form->id );
650
        $values = FrmAppHelper::setup_edit_vars( $form, 'forms', $fields, true, array(), $args );
651
652
        $edit_message = __( 'Form was Successfully Updated', 'formidable' );
653
        if ( $form->is_template && $message == $edit_message ) {
654
            $message = __( 'Template was Successfully Updated', 'formidable' );
655
        }
656
657
		$all_templates = FrmForm::getAll( array( 'is_template' => 1 ), 'name' );
658
659
        if ( $form->default_template ) {
660
            wp_die(__( 'That template cannot be edited', 'formidable' ));
661
        } else if ( defined('DOING_AJAX') ) {
662
            wp_die();
663
        } else if ( $create_link ) {
664
			require( FrmAppHelper::plugin_path() . '/classes/views/frm-forms/new.php' );
665
        } else {
666
			require( FrmAppHelper::plugin_path() . '/classes/views/frm-forms/edit.php' );
667
        }
668
    }
669
670
	public static function get_settings_vars( $id, $errors = array(), $message = '' ) {
671
		FrmAppHelper::permission_check( 'frm_edit_forms' );
672
673
        global $frm_vars;
674
675
        $form = FrmForm::getOne( $id );
676
677
        $fields = FrmField::get_all_for_form($id);
678
        $values = FrmAppHelper::setup_edit_vars($form, 'forms', $fields, true);
679
680
        if ( isset($values['default_template']) && $values['default_template'] ) {
681
            wp_die(__( 'That template cannot be edited', 'formidable' ));
682
        }
683
684
		self::clean_submit_html( $values );
685
686
        $action_controls = FrmFormActionsController::get_form_actions();
687
688
        $sections = apply_filters('frm_add_form_settings_section', array(), $values);
689
        $pro_feature = FrmAppHelper::pro_is_installed() ? '' : ' class="pro_feature"';
690
691
        $styles = apply_filters('frm_get_style_opts', array());
692
693
		require( FrmAppHelper::plugin_path() . '/classes/views/frm-forms/settings.php' );
694
    }
695
696
	/**
697
	 * Replace old Submit Button href with new href to avoid errors in Chrome
698
	 *
699
	 * @since 2.03.08
700
	 *
701
	 * @param array|boolean $values
702
	 */
703
	private static function clean_submit_html( &$values ) {
704
		if ( is_array( $values ) && isset( $values['submit_html'] ) ) {
705
			$values['submit_html'] = str_replace( 'javascript:void(0)', '#', $values['submit_html'] );
706
		}
707
	}
708
709
    public static function mb_tags_box( $form_id, $class = '' ) {
710
        $fields = FrmField::get_all_for_form($form_id, '', 'include');
711
        $linked_forms = array();
712
        $col = 'one';
713
        $settings_tab = FrmAppHelper::is_admin_page('formidable' ) ? true : false;
714
715
		$cond_shortcodes = apply_filters( 'frm_conditional_shortcodes', array() );
716
		$adv_shortcodes = self::get_advanced_shortcodes();
717
		$user_fields = apply_filters( 'frm_user_shortcodes', array() );
718
		$entry_shortcodes = self::get_shortcode_helpers( $settings_tab );
719
720
		include( FrmAppHelper::plugin_path() . '/classes/views/shared/mb_adv_info.php' );
721
    }
722
723
	/**
724
	 * Get an array of the options to display in the advanced tab
725
	 * of the customization panel
726
	 * @since 2.0.6
727
	 */
728
	private static function get_advanced_shortcodes() {
729
		$adv_shortcodes = array(
730
			'sep=", "'       => array(
731
				'label' => __( 'Separator', 'formidable' ),
732
				'title' => __( 'Use a different separator for checkbox fields', 'formidable' ),
733
			),
734
			'format="d-m-Y"' => __( 'Date Format', 'formidable' ),
735
			'show="field_label"' => __( 'Field Label', 'formidable' ),
736
			'wpautop=0'      => array(
737
				'label' => __( 'No Auto P', 'formidable' ),
738
				'title' => __( 'Do not automatically add any paragraphs or line breaks', 'formidable' ),
739
			),
740
		);
741
		$adv_shortcodes = apply_filters( 'frm_advanced_shortcodes', $adv_shortcodes );
742
		// __( 'Leave blank instead of defaulting to User Login', 'formidable' ) : blank=1
743
744
		return $adv_shortcodes;
745
	}
746
747
	/**
748
	 * Get an array of the helper shortcodes to display in the customization panel
749
	 * @since 2.0.6
750
	 */
751
	private static function get_shortcode_helpers( $settings_tab ) {
752
		$entry_shortcodes = array(
753
			'id'        => __( 'Entry ID', 'formidable' ),
754
			'key'       => __( 'Entry Key', 'formidable' ),
755
			'post_id'   => __( 'Post ID', 'formidable' ),
756
			'ip'        => __( 'User IP', 'formidable' ),
757
			'created-at' => __( 'Entry created', 'formidable' ),
758
			'updated-at' => __( 'Entry updated', 'formidable' ),
759
			''          => '',
760
			'siteurl'   => __( 'Site URL', 'formidable' ),
761
			'sitename'  => __( 'Site Name', 'formidable' ),
762
        );
763
764
		if ( ! FrmAppHelper::pro_is_installed() ) {
765
			unset( $entry_shortcodes['post_id'] );
766
		}
767
768
		if ( $settings_tab ) {
769
			$entry_shortcodes['default-message'] = __( 'Default Msg', 'formidable' );
770
			$entry_shortcodes['default-html'] = __( 'Default HTML', 'formidable' );
771
			$entry_shortcodes['default-plain'] = __( 'Default Plain', 'formidable' );
772
		} else {
773
			$entry_shortcodes['detaillink'] = __( 'Detail Link', 'formidable' );
774
			$entry_shortcodes['editlink location="front" label="Edit" page_id=x'] = __( 'Edit Entry Link', 'formidable' );
775
			$entry_shortcodes['evenodd'] = __( 'Even/Odd', 'formidable' );
776
			$entry_shortcodes['entry_count'] = __( 'Entry Count', 'formidable' );
777
			$entry_shortcodes['event_date format="Y-m-d"'] = __( 'Calendar Date', 'formidable' );
778
		}
779
780
		/**
781
		 * Use this hook to add or remove buttons in the helpers section
782
		 * in the customization panel
783
		 * @since 2.0.6
784
		 */
785
		$entry_shortcodes = apply_filters( 'frm_helper_shortcodes', $entry_shortcodes, $settings_tab );
786
787
		return $entry_shortcodes;
788
	}
789
790
    // Insert the form class setting into the form
791
	public static function form_classes( $form ) {
792
        if ( isset($form->options['form_class']) ) {
793
			echo esc_attr( sanitize_text_field( $form->options['form_class'] ) );
794
        }
795
    }
796
797
    public static function get_email_html() {
798
		FrmAppHelper::permission_check('frm_view_forms');
799
        check_ajax_referer( 'frm_ajax', 'nonce' );
800
		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...
801
			'form_id'       => FrmAppHelper::get_post_param( 'form_id', '', 'absint' ),
802
	        'default_email' => true,
803
			'plain_text'    => FrmAppHelper::get_post_param( 'plain_text', '', 'absint' ),
804
	    ) );
805
	    wp_die();
806
	}
807
808
    public static function filter_content( $content, $form, $entry = false ) {
809
		self::get_entry_by_param( $entry );
810
        if ( ! $entry ) {
811
            return $content;
812
        }
813
814
        if ( is_object( $form ) ) {
815
            $form = $form->id;
816
        }
817
818
        $shortcodes = FrmFieldsHelper::get_shortcodes( $content, $form );
819
        $content = apply_filters( 'frm_replace_content_shortcodes', $content, $entry, $shortcodes );
820
821
        return $content;
822
    }
823
824
	private static function get_entry_by_param( &$entry ) {
825
		if ( ! $entry || ! is_object( $entry ) ) {
826
			if ( ! $entry || ! is_numeric( $entry ) ) {
827
				$entry = FrmAppHelper::get_post_param( 'id', false, 'sanitize_title' );
828
			}
829
830
			FrmEntry::maybe_get_entry( $entry );
831
		}
832
	}
833
834
    public static function replace_content_shortcodes( $content, $entry, $shortcodes ) {
835
        return FrmFieldsHelper::replace_content_shortcodes( $content, $entry, $shortcodes );
836
    }
837
838
    public static function process_bulk_form_actions( $errors ) {
839
        if ( ! $_REQUEST ) {
840
            return $errors;
841
        }
842
843
		$bulkaction = FrmAppHelper::get_param( 'action', '', 'get', 'sanitize_text_field' );
844
        if ( $bulkaction == -1 ) {
845
			$bulkaction = FrmAppHelper::get_param( 'action2', '', 'get', 'sanitize_title' );
846
        }
847
848
        if ( ! empty( $bulkaction ) && strpos( $bulkaction, 'bulk_' ) === 0 ) {
849
            FrmAppHelper::remove_get_action();
850
851
            $bulkaction = str_replace( 'bulk_', '', $bulkaction );
852
        }
853
854
		$ids = FrmAppHelper::get_param( 'item-action', '', 'get', 'sanitize_text_field' );
855
        if ( empty( $ids ) ) {
856
            $errors[] = __( 'No forms were specified', 'formidable' );
857
            return $errors;
858
        }
859
860
        $permission_error = FrmAppHelper::permission_nonce_error( '', '_wpnonce', 'bulk-toplevel_page_formidable' );
861
        if ( $permission_error !== false ) {
862
            $errors[] = $permission_error;
863
            return $errors;
864
        }
865
866
        if ( ! is_array( $ids ) ) {
867
            $ids = explode( ',', $ids );
868
        }
869
870
        switch ( $bulkaction ) {
871
            case 'delete':
872
                $message = self::bulk_destroy( $ids );
873
				break;
874
            case 'trash':
875
                $message = self::bulk_trash( $ids );
876
				break;
877
            case 'untrash':
878
                $message = self::bulk_untrash( $ids );
879
				break;
880
            case 'create_template':
881
                $message = self::bulk_create_template( $ids );
882
        }
883
884
        if ( isset( $message ) && ! empty( $message ) ) {
885
			echo '<div id="message" class="updated frm_msg_padding">' . 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...
886
        }
887
888
        return $errors;
889
    }
890
891
    public static function add_default_templates( $path, $default = true, $template = true ) {
892
        _deprecated_function( __FUNCTION__, '1.07.05', 'FrmXMLController::add_default_templates()' );
893
894
        $path = untrailingslashit(trim($path));
895
		$templates = glob( $path . '/*.php' );
896
897
		for ( $i = count( $templates ) - 1; $i >= 0; $i-- ) {
898
			$filename = str_replace( '.php', '', str_replace( $path . '/', '', $templates[ $i ] ) );
899
			$template_query = array( 'form_key' => $filename );
900
            if ( $template ) {
901
                $template_query['is_template'] = 1;
902
            }
903
            if ( $default ) {
904
                $template_query['default_template'] = 1;
905
            }
906
			$form = FrmForm::getAll( $template_query, '', 1 );
907
908
            $values = FrmFormsHelper::setup_new_vars();
909
            $values['form_key'] = $filename;
910
            $values['is_template'] = $template;
911
            $values['status'] = 'published';
912
            if ( $default ) {
913
                $values['default_template'] = 1;
914
            }
915
916
            include( $templates[ $i ] );
917
918
            //get updated form
919
            if ( isset($form) && ! empty($form) ) {
920
                $old_id = $form->id;
921
                $form = FrmForm::getOne($form->id);
922
            } else {
923
                $old_id = false;
924
				$form = FrmForm::getAll( $template_query, '', 1 );
925
            }
926
927
            if ( $form ) {
928
				do_action( 'frm_after_duplicate_form', $form->id, (array) $form, array( 'old_id' => $old_id ) );
929
            }
930
        }
931
    }
932
933
    public static function route() {
934
        $action = isset($_REQUEST['frm_action']) ? 'frm_action' : 'action';
935
        $vars = array();
936
		if ( isset( $_POST['frm_compact_fields'] ) ) {
937
			FrmAppHelper::permission_check( 'frm_edit_forms' );
938
939
            $json_vars = htmlspecialchars_decode(nl2br(stripslashes(str_replace('&quot;', '\\\"', $_POST['frm_compact_fields'] ))));
940
            $json_vars = json_decode($json_vars, true);
941
            if ( empty($json_vars) ) {
942
                // json decoding failed so we should return an error message
943
				$action = FrmAppHelper::get_param( $action, '', 'get', 'sanitize_title' );
944
                if ( 'edit' == $action ) {
945
                    $action = 'update';
946
                }
947
948
                add_filter('frm_validate_form', 'FrmFormsController::json_error');
949
            } else {
950
                $vars = FrmAppHelper::json_to_array($json_vars);
951
                $action = $vars[ $action ];
952
				unset( $_REQUEST['frm_compact_fields'], $_POST['frm_compact_fields'] );
953
				$_REQUEST = array_merge( $_REQUEST, $vars );
954
				$_POST = array_merge( $_POST, $_REQUEST );
955
            }
956
        } else {
957
			$action = FrmAppHelper::get_param( $action, '', 'get', 'sanitize_title' );
958
    		if ( isset( $_REQUEST['delete_all'] ) ) {
959
                // override the action for this page
960
    			$action = 'delete_all';
961
            }
962
        }
963
964
		add_action( 'frm_load_form_hooks', 'FrmHooksController::trigger_load_form_hooks' );
965
        FrmAppHelper::trigger_hook_load( 'form' );
966
967
        switch ( $action ) {
968
            case 'new':
969
                return self::new_form($vars);
970
            case 'create':
971
            case 'edit':
972
            case 'update':
973
            case 'duplicate':
974
            case 'trash':
975
            case 'untrash':
976
            case 'destroy':
977
            case 'delete_all':
978
            case 'settings':
979
            case 'update_settings':
980
				return self::$action( $vars );
981
            default:
982
				do_action( 'frm_form_action_' . $action );
983
				if ( apply_filters( 'frm_form_stop_action_' . $action, false ) ) {
984
                    return;
985
                }
986
987
				$action = FrmAppHelper::get_param( 'action', '', 'get', 'sanitize_text_field' );
988
                if ( $action == -1 ) {
989
					$action = FrmAppHelper::get_param( 'action2', '', 'get', 'sanitize_title' );
990
                }
991
992
                if ( strpos($action, 'bulk_') === 0 ) {
993
                    FrmAppHelper::remove_get_action();
994
                    return self::list_form();
995
                }
996
997
                return self::display_forms_list();
998
        }
999
    }
1000
1001
    public static function json_error( $errors ) {
1002
        $errors['json'] = __( 'Abnormal HTML characters prevented your form from saving correctly', 'formidable' );
1003
        return $errors;
1004
    }
1005
1006
1007
    /* FRONT-END FORMS */
1008
    public static function admin_bar_css() {
1009
		if ( is_admin() || ! current_user_can( 'frm_edit_forms' ) ) {
1010
            return;
1011
        }
1012
1013
		add_action( 'wp_before_admin_bar_render', 'FrmFormsController::admin_bar_configure' );
1014
		FrmAppHelper::load_font_style();
1015
	}
1016
1017
	public static function admin_bar_configure() {
1018
        global $frm_vars;
1019
        if ( empty($frm_vars['forms_loaded']) ) {
1020
            return;
1021
        }
1022
1023
        $actions = array();
1024
        foreach ( $frm_vars['forms_loaded'] as $form ) {
1025
            if ( is_object($form) ) {
1026
                $actions[ $form->id ] = $form->name;
1027
            }
1028
            unset($form);
1029
        }
1030
1031
        if ( empty($actions) ) {
1032
            return;
1033
        }
1034
1035
		self::add_menu_to_admin_bar();
1036
		self::add_forms_to_admin_bar( $actions );
1037
	}
1038
1039
	/**
1040
	 * @since 2.05.07
1041
	 */
1042
	public static function add_menu_to_admin_bar() {
1043
		global $wp_admin_bar;
1044
1045
		$wp_admin_bar->add_node( array(
1046
			'id'    => 'frm-forms',
1047
			'title' => '<span class="ab-icon"></span><span class="ab-label">' . FrmAppHelper::get_menu_name() . '</span>',
1048
			'href'  => admin_url( 'admin.php?page=formidable' ),
1049
			'meta'  => array(
1050
				'title' => FrmAppHelper::get_menu_name(),
1051
			),
1052
		) );
1053
	}
1054
1055
	/**
1056
	 * @since 2.05.07
1057
	 */
1058
	private static function add_forms_to_admin_bar( $actions ) {
1059
		global $wp_admin_bar;
1060
1061
		asort( $actions );
1062
1063
		foreach ( $actions as $form_id => $name ) {
1064
1065
			$wp_admin_bar->add_node( array(
1066
				'parent'    => 'frm-forms',
1067
				'id'        => 'edit_form_' . $form_id,
1068
				'title'     => empty( $name ) ? __( '(no title)' ) : $name,
1069
				'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...
1070
			) );
1071
		}
1072
	}
1073
1074
    //formidable shortcode
1075
	public static function get_form_shortcode( $atts ) {
1076
        global $frm_vars;
1077
        if ( isset($frm_vars['skip_shortcode']) && $frm_vars['skip_shortcode'] ) {
1078
            $sc = '[formidable';
1079
			if ( ! empty( $atts ) ) {
1080
				foreach ( $atts as $k => $v ) {
1081
					$sc .= ' ' . $k . '="' . esc_attr( $v ) . '"';
1082
				}
1083
			}
1084
			return $sc . ']';
1085
        }
1086
1087
		$shortcode_atts = shortcode_atts( array(
1088
			'id'          => '',
1089
			'key'         => '',
1090
			'title'       => false,
1091
			'description' => false,
1092
			'readonly'    => false,
1093
			'entry_id'    => false,
1094
			'fields'      => array(),
1095
			'exclude_fields' => array(),
1096
			'minimize'    => false,
1097
		), $atts );
1098
		do_action( 'formidable_shortcode_atts', $shortcode_atts, $atts );
1099
1100
        return self::show_form(
1101
            $shortcode_atts['id'], $shortcode_atts['key'], $shortcode_atts['title'],
1102
            $shortcode_atts['description'], $atts
1103
        );
1104
    }
1105
1106
    public static function show_form( $id = '', $key = '', $title = false, $description = false, $atts = array() ) {
1107
        if ( empty( $id ) ) {
1108
            $id = $key;
1109
        }
1110
1111
        $form = self::maybe_get_form_to_show( $id );
1112
        if ( ! $form ) {
1113
            return __( 'Please select a valid form', 'formidable' );
1114
        }
1115
1116
		add_action( 'frm_load_form_hooks', 'FrmHooksController::trigger_load_form_hooks' );
1117
        FrmAppHelper::trigger_hook_load( 'form', $form );
1118
1119
        $form = apply_filters( 'frm_pre_display_form', $form );
1120
1121
        $frm_settings = FrmAppHelper::get_settings();
1122
1123
		if ( self::is_viewable_draft_form( $form ) ) {
1124
			// don't show a draft form on a page
1125
			$form = __( 'Please select a valid form', 'formidable' );
1126
		} else if ( self::user_should_login( $form ) ) {
1127
			$form = do_shortcode( $frm_settings->login_msg );
1128
		} else if ( self::user_has_permission_to_view( $form ) ) {
1129
			$form = do_shortcode( $frm_settings->login_msg );
1130
		} else {
1131
			$form = self::get_form( $form, $title, $description, $atts );
1132
1133
			/**
1134
			 * Use this shortcode to check for external shortcodes that may span
1135
			 * across multiple fields in the customizable HTML
1136
			 * @since 2.0.8
1137
			 */
1138
			$form = apply_filters( 'frm_filter_final_form', $form );
1139
		}
1140
1141
		return $form;
1142
    }
1143
1144
	private static function maybe_get_form_to_show( $id ) {
1145
		$form = false;
1146
1147
		if ( ! empty( $id ) ) { // no form id or key set
1148
			$form = FrmForm::getOne( $id );
1149
			if ( ! $form || $form->parent_form_id || $form->status == 'trash' ) {
1150
				$form = false;
1151
			}
1152
		}
1153
1154
		return $form;
1155
	}
1156
1157
	private static function is_viewable_draft_form( $form ) {
1158
		global $post;
1159
		$frm_settings = FrmAppHelper::get_settings();
1160
		return $form->status == 'draft' && current_user_can( 'frm_edit_forms' ) && ( ! $post || $post->ID != $frm_settings->preview_page_id ) && ! FrmAppHelper::is_preview_page();
1161
	}
1162
1163
	private static function user_should_login( $form ) {
1164
		return $form->logged_in && ! is_user_logged_in();
1165
	}
1166
1167
	private static function user_has_permission_to_view( $form ) {
1168
		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'] );
1169
	}
1170
1171
    public static function get_form( $form, $title, $description, $atts = array() ) {
1172
        ob_start();
1173
1174
        self::get_form_contents( $form, $title, $description, $atts );
1175
		self::enqueue_scripts( FrmForm::get_params( $form ) );
1176
1177
        $contents = ob_get_contents();
1178
        ob_end_clean();
1179
1180
		self::maybe_minimize_form( $atts, $contents );
1181
1182
        return $contents;
1183
    }
1184
1185
	public static function enqueue_scripts( $params ) {
1186
		do_action( 'frm_enqueue_form_scripts', $params );
1187
	}
1188
1189
	public static function get_form_contents( $form, $title, $description, $atts ) {
1190
		$params = FrmForm::get_params( $form );
1191
		$errors = self::get_saved_errors( $form, $params );
1192
		$fields = FrmFieldsHelper::get_form_fields( $form->id, $errors );
1193
		$reset = false;
1194
		$pass_args = compact( 'form', 'fields', 'errors', 'title', 'description', 'reset' );
1195
1196
		$handle_process_here = $params['action'] == 'create' && $params['posted_form_id'] == $form->id && $_POST;
1197
1198
		if ( ! $handle_process_here ) {
1199
			do_action( 'frm_display_form_action', $params, $fields, $form, $title, $description );
1200
			if ( apply_filters( 'frm_continue_to_new', true, $form->id, $params['action'] ) ) {
1201
				self::show_form_after_submit( $pass_args );
1202
			}
1203
		} elseif ( ! empty( $errors ) ) {
1204
			self::show_form_after_submit( $pass_args );
1205
1206
		} else {
1207
1208
			do_action( 'frm_validate_form_creation', $params, $fields, $form, $title, $description );
1209
1210
			if ( apply_filters( 'frm_continue_to_create', true, $form->id ) ) {
1211
				$entry_id = self::just_created_entry( $form->id );
1212
1213
				$conf_method = apply_filters( 'frm_success_filter', 'message', $form, 'create' );
1214
				if ( $entry_id && is_numeric( $entry_id ) && $conf_method != 'message' ) {
1215
					self::run_success_action( compact( 'entry_id', 'form', 'conf_method' ) );
1216
				} else {
1217
					$pass_args['reset'] = true;
1218
					$pass_args['entry_id'] = $entry_id;
1219
					self::show_message_after_save( $pass_args );
1220
				}
1221
				do_action( 'frm_after_entry_processed', array(
1222
					'entry_id' => $entry_id,
1223
					'form'     => $form,
1224
				) );
1225
			}
1226
		}
1227
	}
1228
1229
	/**
1230
	 * If the form was processed earlier (init), get the generated errors
1231
	 * @since 2.05
1232
	 */
1233
	private static function get_saved_errors( $form, $params ) {
1234
		global $frm_vars;
1235
1236
		if ( $params['posted_form_id'] == $form->id && $_POST && isset( $frm_vars['created_entries'][ $form->id ] ) ) {
1237
			$errors = $frm_vars['created_entries'][ $form->id ]['errors'];
1238
		} else {
1239
			$errors = array();
1240
		}
1241
		return $errors;
1242
	}
1243
1244
	/**
1245
	 * @since 2.2.7
1246
	 */
1247
	public static function just_created_entry( $form_id ) {
1248
		global $frm_vars;
1249
		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;
1250
	}
1251
1252
	/**
1253
	 * Used when the success action is not 'message'
1254
	 * @since 2.05
1255
	 */
1256
	public static function run_success_action( $args ) {
1257
		do_action( 'frm_success_action', $args['conf_method'], $args['form'], $args['form']->options, $args['entry_id'] );
1258
	}
1259
1260
	/**
1261
	 * Prepare to show the success message and empty form after submit
1262
	 * @since 2.05
1263
	 */
1264
	public static function show_message_after_save( $atts ) {
1265
		$atts['message'] = self::prepare_submit_message( $atts['form'], $atts['entry_id'] );
1266
1267
		if ( ! isset( $atts['form']->options['show_form'] ) || $atts['form']->options['show_form'] ) {
1268
			self::show_form_after_submit( $atts );
1269
		} else {
1270
			self::show_lone_success_messsage( $atts );
1271
		}
1272
	}
1273
1274
	/**
1275
	 * Show an empty form
1276
	 * @since 2.05
1277
	 */
1278
	private static function show_form_after_submit( $args ) {
1279
		self::fill_atts_for_form_display( $args );
1280
1281
		$errors = $args['errors'];
1282
		$message = $args['message'];
1283
		$form = $args['form'];
1284
		$title = $args['title'];
1285
		$description = $args['description'];
1286
1287
		if ( empty( $args['fields'] ) ) {
1288
			$values = array();
1289
		} else {
1290
			$values = FrmEntriesHelper::setup_new_vars( $args['fields'], $form, $args['reset'] );
1291
		}
1292
		unset( $args );
1293
1294
		$include_form_tag = apply_filters( 'frm_include_form_tag', true, $form );
1295
1296
		$frm_settings = FrmAppHelper::get_settings();
1297
		$submit = isset( $form->options['submit_value'] ) ? $form->options['submit_value'] : $frm_settings->submit_value;
1298
1299
		include( FrmAppHelper::plugin_path() . '/classes/views/frm-entries/new.php' );
1300
	}
1301
1302
	/**
1303
	 * Get all the values needed on the new.php entry page
1304
	 * @since 2.05
1305
	 */
1306
	private static function fill_atts_for_form_display( &$args ) {
1307
		$defaults = array(
1308
			'errors'  => array(),
1309
			'message' => '',
1310
			'fields'  => array(),
1311
			'form'    => array(),
1312
			'title'   => true,
1313
			'description' => false,
1314
			'reset'   => false,
1315
		);
1316
		$args = wp_parse_args( $args, $defaults );
1317
	}
1318
1319
	/**
1320
	 * Show the success message without the form
1321
	 * @since 2.05
1322
	 */
1323
	private static function show_lone_success_messsage( $atts ) {
1324
		global $frm_vars;
1325
		$values = FrmEntriesHelper::setup_new_vars( $atts['fields'], $atts['form'], true );
1326
		self::maybe_load_css( $atts['form'], $values['custom_style'], $frm_vars['load_css'] );
1327
1328
		$include_extra_container = 'frm_forms' . FrmFormsHelper::get_form_style_class( $values );
1329
		$errors = array();
1330
		$form = $atts['form'];
1331
		$message = $atts['message'];
1332
1333
		include( FrmAppHelper::plugin_path() . '/classes/views/frm-entries/errors.php' );
1334
	}
1335
1336
	/**
1337
	 * Prepare the success message before it's shown
1338
	 * @since 2.05
1339
	 */
1340
	private static function prepare_submit_message( $form, $entry_id ) {
1341
		$frm_settings = FrmAppHelper::get_settings();
1342
1343
		if ( $entry_id && is_numeric( $entry_id ) ) {
1344
			$message = isset( $form->options['success_msg'] ) ? $form->options['success_msg'] : $frm_settings->success_msg;
1345
			$class = 'frm_message';
1346
		} else {
1347
			$message = $frm_settings->failed_msg;
1348
			$class = FrmFormsHelper::form_error_class();
1349
		}
1350
1351
		$message = FrmFormsHelper::get_success_message( compact( 'message', 'form', 'entry_id', 'class' ) );
1352
		return apply_filters( 'frm_main_feedback', $message, $form, $entry_id );
1353
	}
1354
1355
	public static function front_head() {
1356
		$version = FrmAppHelper::plugin_version();
1357
		$suffix = defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ? '' : '.min';
1358
		wp_register_script( 'formidable', FrmAppHelper::plugin_url() . "/js/formidable{$suffix}.js", array( 'jquery' ), $version, true );
1359
		wp_register_script( 'jquery-placeholder', FrmAppHelper::plugin_url() . '/js/jquery/jquery.placeholder.min.js', array( 'jquery' ), '2.3.1', true );
1360
		add_filter( 'script_loader_tag', 'FrmFormsController::defer_script_loading', 10, 2 );
1361
1362
		if ( FrmAppHelper::is_admin() ) {
1363
			// don't load this in back-end
1364
			return;
1365
		}
1366
1367
		FrmAppHelper::localize_script( 'front' );
1368
		FrmStylesController::enqueue_css( 'register' );
1369
	}
1370
1371
	public static function maybe_load_css( $form, $this_load, $global_load ) {
1372
		$load_css = FrmForm::is_form_loaded( $form, $this_load, $global_load );
1373
1374
		if ( $load_css ) {
1375
			global $frm_vars;
1376
			self::footer_js( 'header' );
1377
			$frm_vars['css_loaded'] = true;
1378
		}
1379
	}
1380
1381
	public static function defer_script_loading( $tag, $handle ) {
1382
	    if ( 'recaptcha-api' == $handle && ! strpos( $tag, 'defer' ) ) {
1383
	        $tag = str_replace( ' src', ' defer="defer" async="async" src', $tag );
1384
		}
1385
	    return $tag;
1386
	}
1387
1388
	public static function footer_js( $location = 'footer' ) {
1389
		global $frm_vars;
1390
1391
		FrmStylesController::enqueue_css();
1392
1393
		if ( ! FrmAppHelper::is_admin() && $location != 'header' && ! empty( $frm_vars['forms_loaded'] ) ) {
1394
			//load formidable js
1395
			wp_enqueue_script( 'formidable' );
1396
		}
1397
	}
1398
1399
	/**
1400
	 * @since 2.0.8
1401
	 */
1402
	private static function maybe_minimize_form( $atts, &$content ) {
1403
		// check if minimizing is turned on
1404
		if ( self::is_minification_on( $atts ) ) {
1405
			$content = str_replace( array( "\r\n", "\r", "\n", "\t", '    ' ), '', $content );
1406
		}
1407
	}
1408
1409
	/**
1410
	 * @since 2.0.8
1411
	 * @return boolean
1412
	 */
1413
	private static function is_minification_on( $atts ) {
1414
		return isset( $atts['minimize'] ) && ! empty( $atts['minimize'] );
1415
	}
1416
}
1417