Completed
Push — master ( b5f6e0...77c454 )
by Stephanie
02:40
created

FrmFormsController::admin_bar_configure()   B

Complexity

Conditions 5
Paths 7

Size

Total Lines 21
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

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