Completed
Push — master ( 3860cc...10e837 )
by Stephanie
9s
created

FrmFormsHelper::form_loaded()   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 3
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
if ( ! defined('ABSPATH') ) {
3
	die( 'You are not allowed to call this page directly.' );
4
}
5
6
class FrmFormsHelper {
7
8
	public static function maybe_get_form( &$form ) {
9
		_deprecated_function( __FUNCTION__, '2.0.9', 'FrmForm::maybe_get_form' );
10
		FrmForm::maybe_get_form( $form );
11
	}
12
13
	/**
14
	 * @since 2.2.10
15
	 */
16
	public static function form_error_class() {
17
		return apply_filters( 'frm_form_error_class', 'frm_error_style' );
18
	}
19
20
	public static function get_direct_link( $key, $form = false ) {
21
		$target_url = esc_url( admin_url( 'admin-ajax.php?action=frm_forms_preview&form=' . $key ) );
22
        $target_url = apply_filters('frm_direct_link', $target_url, $key, $form);
23
24
        return $target_url;
25
    }
26
27
    public static function forms_dropdown( $field_name, $field_value = '', $args = array() ) {
28
        $defaults = array(
29
            'blank'     => true,
30
            'field_id'  => false,
31
            'onchange'  => false,
32
            'exclude'   => false,
33
            'class'     => '',
34
			'inc_children' => 'exclude',
35
        );
36
        $args = wp_parse_args( $args, $defaults );
37
38
        if ( ! $args['field_id'] ) {
39
            $args['field_id'] = $field_name;
40
        }
41
42
		$query = array();
43
        if ( $args['exclude'] ) {
44
			$query['id !'] = $args['exclude'];
45
        }
46
47
        $where = apply_filters('frm_forms_dropdown', $query, $field_name);
48
		$forms = FrmForm::get_published_forms( $where, 999, $args['inc_children'] );
49
		$add_html = array();
50
		self::add_html_attr( $args['onchange'], 'onchange', $add_html );
51
		self::add_html_attr( $args['class'], 'class', $add_html );
52
53
        ?>
54
		<select name="<?php echo esc_attr( $field_name ); ?>" id="<?php echo esc_attr( $args['field_id'] ) ?>" <?php echo implode( ' ', $add_html ); ?>>
0 ignored issues
show
introduced by
Expected a sanitizing function (see Codex for 'Data Validation'), but instead saw 'implode'
Loading history...
55
		<?php if ( $args['blank'] ) { ?>
56
			<option value=""><?php echo ( $args['blank'] == 1 ) ? ' ' : '- ' . esc_attr( $args['blank'] ) . ' -'; ?></option>
0 ignored issues
show
introduced by
Expected next thing to be a escaping function, not '('
Loading history...
57
		<?php } ?>
58
		<?php foreach ( $forms as $form ) { ?>
59
			<option value="<?php echo esc_attr( $form->id ); ?>" <?php selected( $field_value, $form->id ); ?>><?php
60
				echo ( '' == $form->name ) ? esc_html__( '(no title)', 'formidable' ) : esc_html( FrmAppHelper::truncate( $form->name, 50 ) ) . ( $form->parent_form_id ? esc_html__( ' (child)', 'formidable' ) : '' ) ;
0 ignored issues
show
introduced by
Expected next thing to be a escaping function, not '('
Loading history...
61
			?></option>
62
		<?php } ?>
63
        </select>
64
        <?php
65
    }
66
67
	/**
68
	 * @param string $class
69
	 * @param string $param
70
	 * @param array $add_html
71
	 *
72
	 * @since 2.0.6
73
	 */
74
	public static function add_html_attr( $class, $param, &$add_html ) {
75
		if ( ! empty( $class ) ) {
76
			$add_html[ $param ] = sanitize_title( $param ) . '="' . esc_attr( trim( sanitize_text_field( $class ) ) ) . '"';
77
		}
78
	}
79
80
    public static function form_switcher() {
81
		$where = apply_filters( 'frm_forms_dropdown', array(), '' );
82
		$forms = FrmForm::get_published_forms( $where );
83
84
		$args = array( 'id' => 0, 'form' => 0 );
85
		if ( isset( $_GET['id'] ) && ! isset( $_GET['form'] ) ) {
0 ignored issues
show
introduced by
Detected access of super global var $_GET, probably need manual inspection.
Loading history...
86
			unset( $args['form'] );
87
		} else if ( isset( $_GET['form']) && ! isset( $_GET['id'] ) ) {
0 ignored issues
show
introduced by
Detected access of super global var $_GET, probably need manual inspection.
Loading history...
88
			unset( $args['id'] );
89
        }
90
91
		$frm_action = FrmAppHelper::simple_get( 'frm_action', 'sanitize_title' );
92
		if ( FrmAppHelper::is_admin_page( 'formidable-entries' ) && in_array( $frm_action, array( 'edit', 'show', 'destroy_all' ) ) ) {
93
            $args['frm_action'] = 'list';
94
            $args['form'] = 0;
95
		} else if ( FrmAppHelper::is_admin_page('formidable' ) && in_array( $frm_action, array( 'new', 'duplicate' ) ) ) {
96
            $args['frm_action'] = 'edit';
97
		} else if ( isset( $_GET['post'] ) ) {
98
            $args['form'] = 0;
99
            $base = admin_url('edit.php?post_type=frm_display');
100
        }
101
102
        ?>
103
		<li class="dropdown last" id="frm_bs_dropdown">
104
			<a href="#" id="frm-navbarDrop" class="frm-dropdown-toggle" data-toggle="dropdown"><?php _e( 'Switch Form', 'formidable' ) ?> <b class="caret"></b></a>
105
		    <ul class="frm-dropdown-menu frm-on-top" role="menu" aria-labelledby="frm-navbarDrop">
106
			<?php
107
			foreach ( $forms as $form ) {
108
				if ( isset( $args['id'] ) ) {
109
			        $args['id'] = $form->id;
110
				}
111
			    if ( isset( $args['form'] ) ) {
112
			        $args['form'] = $form->id;
113
				}
114
                ?>
115
				<li><a href="<?php echo esc_url( isset( $base ) ? add_query_arg( $args, $base ) : add_query_arg( $args ) ); ?>" tabindex="-1"><?php echo esc_html( empty( $form->name ) ? __( '(no title)') : FrmAppHelper::truncate( $form->name, 60 ) ); ?></a></li>
116
			<?php
117
				unset( $form );
118
			} ?>
119
			</ul>
120
		</li>
121
        <?php
122
    }
123
124
	public static function get_sortable_classes( $col, $sort_col, $sort_dir ) {
125
        echo ($sort_col == $col) ? 'sorted' : 'sortable';
0 ignored issues
show
introduced by
Expected next thing to be a escaping function, not '('
Loading history...
126
        echo ($sort_col == $col && $sort_dir == 'desc') ? ' asc' : ' desc';
0 ignored issues
show
introduced by
Expected next thing to be a escaping function, not '('
Loading history...
127
    }
128
129
	/**
130
	 * Get the invalid form error message
131
	 *
132
	 * @since 2.02.07
133
	 * @param array $args
134
	 * @return string
135
	 */
136
	public static function get_invalid_error_message( $args ) {
137
		$frm_settings = FrmAppHelper::get_settings();
138
139
		$invalid_msg = apply_filters( 'frm_invalid_error_message', $frm_settings->invalid_msg, $args );
140
141
		return $invalid_msg;
142
	}
143
144
	public static function get_success_message( $atts ) {
145
		$message = apply_filters( 'frm_content', $atts['message'], $atts['form'], $atts['entry_id'] );
146
		$message = FrmAppHelper::use_wpautop( do_shortcode( $message ) );
147
		$message = '<div class="' . esc_attr( $atts['class'] ) . '">' . $message . '</div>';
148
		return $message;
149
	}
150
151
    /**
152
     * Used when a form is created
153
     */
154
    public static function setup_new_vars( $values = array() ) {
155
        global $wpdb;
156
157
        if ( ! empty( $values ) ) {
158
            $post_values = $values;
159
        } else {
160
            $values = array();
161
            $post_values = isset($_POST) ? $_POST : array();
0 ignored issues
show
introduced by
Detected access of super global var $_POST, probably need manual inspection.
Loading history...
introduced by
Detected usage of a non-sanitized input variable: $_POST
Loading history...
162
        }
163
164
		foreach ( array( 'name' => '', 'description' => '' ) as $var => $default ) {
165
			if ( ! isset( $values[ $var ] ) ) {
166
				$values[ $var ] = FrmAppHelper::get_param( $var, $default );
167
            }
168
        }
169
170
        $values['description'] = FrmAppHelper::use_wpautop($values['description']);
171
172
		foreach ( array( 'form_id' => '', 'logged_in' => '', 'editable' => '', 'default_template' => 0, 'is_template' => 0, 'status' => 'draft', 'parent_form_id' => 0 ) as $var => $default ) {
173
            if ( ! isset( $values[ $var ] ) ) {
174
				$values[ $var ] = FrmAppHelper::get_param( $var, $default );
175
            }
176
        }
177
178
        if ( ! isset( $values['form_key'] ) ) {
179
			$values['form_key'] = ( $post_values && isset( $post_values['form_key'] ) ) ? $post_values['form_key'] : FrmAppHelper::get_unique_key( '', $wpdb->prefix . 'frm_forms', 'form_key' );
180
        }
181
182
		$values = self::fill_default_opts( $values, false, $post_values );
183
		$values['custom_style'] = FrmAppHelper::custom_style_value( $post_values );
184
185
        return apply_filters('frm_setup_new_form_vars', $values);
186
    }
187
188
    /**
189
     * Used when editing a form
190
     */
191
    public static function setup_edit_vars( $values, $record, $post_values = array() ) {
192
		if ( empty( $post_values ) ) {
193
			$post_values = stripslashes_deep( $_POST );
0 ignored issues
show
introduced by
Detected access of super global var $_POST, probably need manual inspection.
Loading history...
194
		}
195
196
        $values['form_key'] = isset($post_values['form_key']) ? $post_values['form_key'] : $record->form_key;
197
        $values['default_template'] = isset($post_values['default_template']) ? $post_values['default_template'] : $record->default_template;
198
        $values['is_template'] = isset($post_values['is_template']) ? $post_values['is_template'] : $record->is_template;
199
        $values['status'] = $record->status;
200
201
        $values = self::fill_default_opts($values, $record, $post_values);
202
203
        return apply_filters('frm_setup_edit_form_vars', $values);
204
    }
205
206
	public static function fill_default_opts( $values, $record, $post_values ) {
207
208
        $defaults = self::get_default_opts();
209
		foreach ( $defaults as $var => $default ) {
210
            if ( is_array($default) ) {
211
                if ( ! isset( $values[ $var ] ) ) {
212
					$values[ $var ] = ( $record && isset( $record->options[ $var ] ) ) ? $record->options[ $var ] : array();
213
                }
214
215
                foreach ( $default as $k => $v ) {
216
					$values[ $var ][ $k ] = ( $post_values && isset( $post_values[ $var ][ $k ] ) ) ? $post_values[ $var ][ $k ] : ( ( $record && isset( $record->options[ $var ] ) && isset( $record->options[ $var ][ $k ] ) ) ? $record->options[ $var ][ $k ] : $v);
217
218
                    if ( is_array( $v ) ) {
219
                        foreach ( $v as $k1 => $v1 ) {
220
							$values[ $var ][ $k ][ $k1 ] = ( $post_values && isset( $post_values[ $var ][ $k ][ $k1 ] ) ) ? $post_values[ $var ][ $k ][ $k1 ] : ( ( $record && isset( $record->options[ $var ] ) && isset( $record->options[ $var ][ $k ] ) && isset( $record->options[ $var ][ $k ][ $k1 ] ) ) ? $record->options[ $var ][ $k ][ $k1 ] : $v1 );
221
                            unset( $k1, $v1 );
222
                        }
223
                    }
224
225
                    unset($k, $v);
226
                }
227
            } else {
228
				$values[ $var ] = ( $post_values && isset( $post_values['options'][ $var ] ) ) ? $post_values['options'][ $var ] : ( ( $record && isset( $record->options[ $var ] ) ) ? $record->options[ $var ] : $default );
229
            }
230
231
            unset($var, $default);
232
        }
233
234
        return $values;
235
    }
236
237
    public static function get_default_opts() {
238
        $frm_settings = FrmAppHelper::get_settings();
239
240
        return array(
241
            'submit_value' => $frm_settings->submit_value, 'success_action' => 'message',
242
            'success_msg' => $frm_settings->success_msg, 'show_form' => 0, 'akismet' => '',
243
            'no_save' => 0, 'ajax_load' => 0, 'form_class' => '', 'custom_style' => 1,
244
            'before_html' => self::get_default_html('before'),
245
            'after_html' => '',
246
            'submit_html' => self::get_default_html('submit'),
247
        );
248
    }
249
250
	/**
251
	 * @param array $options
252
	 * @param array $values
253
	 * @since 2.0.6
254
	 */
255
	public static function fill_form_options( &$options, $values ) {
256
		$defaults = self::get_default_opts();
257
		foreach ( $defaults as $var => $default ) {
258
			$options[ $var ] = isset( $values['options'][ $var ] ) ? $values['options'][ $var ] : $default;
259
			unset( $var, $default );
260
		}
261
	}
262
263
    /**
264
     * @param string $loc
265
     */
266
	public static function get_default_html( $loc ) {
267
		if ( $loc == 'submit' ) {
268
            $sending = __( 'Sending', 'formidable' );
0 ignored issues
show
Unused Code introduced by
$sending is not used, you could remove the assignment.

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

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

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

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

Loading history...
269
            $draft_link = self::get_draft_link();
270
            $default_html = <<<SUBMIT_HTML
271
<div class="frm_submit">
272
[if back_button]<button type="submit" name="frm_prev_page" formnovalidate="formnovalidate" class="frm_prev_page" [back_hook]>[back_label]</button>[/if back_button]
273
<button class="frm_button_submit" type="submit"  [button_action]>[button_label]</button>
274
$draft_link
275
</div>
276
SUBMIT_HTML;
277
		} else if ( $loc == 'before' ) {
278
            $default_html = <<<BEFORE_HTML
279
<legend class="frm_hidden">[form_name]</legend>
280
[if form_name]<h3 class="frm_form_title">[form_name]</h3>[/if form_name]
281
[if form_description]<div class="frm_description">[form_description]</div>[/if form_description]
282
BEFORE_HTML;
283
		} else {
284
            $default_html = '';
285
        }
286
287
        return $default_html;
288
    }
289
290
    public static function get_draft_link() {
291
        $link = '[if save_draft]<a href="#" class="frm_save_draft" [draft_hook]>[draft_label]</a>[/if save_draft]';
292
        return $link;
293
    }
294
295
	public static function get_custom_submit( $html, $form, $submit, $form_action, $values ) {
296
		$button = self::replace_shortcodes( $html, $form, $submit, $form_action, $values );
297
		if ( ! strpos( $button, '[button_action]' ) ) {
298
			echo $button;
0 ignored issues
show
introduced by
Expected next thing to be a escaping function, not '$button'
Loading history...
299
			return;
300
		}
301
302
		$button_parts = explode( '[button_action]', $button );
303
304
		$classes = apply_filters( 'frm_submit_button_class', array(), $form );
305
		if ( ! empty( $classes ) ) {
306
			$classes = implode( ' ', $classes );
307
			$button_class = ' class="frm_button_submit';
308
			if ( strpos( $button_parts[0], $button_class ) !== false ) {
309
				$button_parts[0] = str_replace( $button_class, $button_class . ' ' . esc_attr( $classes ), $button_parts[0] );
310
			} else {
311
				$button_parts[0] .= ' class="' . esc_attr( $classes ) . '"';
312
			}
313
		}
314
315
		echo $button_parts[0];
0 ignored issues
show
introduced by
Expected next thing to be a escaping function, not '$button_parts'
Loading history...
316
		do_action( 'frm_submit_button_action', $form, $form_action );
317
		echo $button_parts[1];
0 ignored issues
show
introduced by
Expected next thing to be a escaping function, not '$button_parts'
Loading history...
318
	}
319
320
    /**
321
     * Automatically add end section fields if they don't exist (2.0 migration)
322
     * @since 2.0
323
     *
324
     * @param boolean $reset_fields
325
     */
326
    public static function auto_add_end_section_fields( $form, $fields, &$reset_fields ) {
327
		if ( empty( $fields ) ) {
328
			return;
329
		}
330
331
		$end_section_values = apply_filters( 'frm_before_field_created', FrmFieldsHelper::setup_new_vars( 'end_divider', $form->id ) );
332
		$open = false;
333
		$prev_order = false;
334
		$add_order = 0;
335
		$last_field = false;
336
        foreach ( $fields as $field ) {
337
			if ( $prev_order === $field->field_order ) {
338
				$add_order++;
339
			}
340
341
			if ( $add_order ) {
342
				$reset_fields = true;
343
				$field->field_order = $field->field_order + $add_order;
344
				FrmField::update( $field->id, array( 'field_order' => $field->field_order ) );
345
			}
346
347
            switch ( $field->type ) {
348
                case 'divider':
349
                    // create an end section if open
350
					self::maybe_create_end_section( $open, $reset_fields, $add_order, $end_section_values, $field, 'move' );
351
352
                    // mark it open for the next end section
353
                    $open = true;
354
                break;
355
                case 'break';
356
					self::maybe_create_end_section( $open, $reset_fields, $add_order, $end_section_values, $field, 'move' );
357
                break;
358
                case 'end_divider':
359
                    if ( ! $open ) {
360
                        // the section isn't open, so this is an extra field that needs to be removed
361
                        FrmField::destroy( $field->id );
362
                        $reset_fields = true;
363
                    }
364
365
                    // There is already an end section here, so there is no need to create one
366
                    $open = false;
367
            }
368
			$prev_order = $field->field_order;
369
370
			$last_field = $field;
371
			unset( $field );
372
        }
373
374
		self::maybe_create_end_section( $open, $reset_fields, $add_order, $end_section_values, $last_field );
375
    }
376
377
	/**
378
	 * Create end section field if it doesn't exist. This is for migration from < 2.0
379
	 * Fix any ordering that may be messed up
380
	 */
381
	public static function maybe_create_end_section( &$open, &$reset_fields, &$add_order, $end_section_values, $field, $move = 'no' ) {
382
        if ( ! $open ) {
383
            return;
384
        }
385
386
		$end_section_values['field_order'] = $field->field_order + 1;
387
388
        FrmField::create( $end_section_values );
389
390
		if ( $move == 'move' ) {
391
			// bump the order of current field unless we're at the end of the form
392
			FrmField::update( $field->id, array( 'field_order' => $field->field_order + 2 ) );
393
		}
394
395
		$add_order += 2;
396
        $open = false;
397
        $reset_fields = true;
398
    }
399
400
    public static function replace_shortcodes( $html, $form, $title = false, $description = false, $values = array() ) {
401
		foreach ( array( 'form_name' => $title, 'form_description' => $description, 'entry_key' => true ) as $code => $show ) {
402
            if ( $code == 'form_name' ) {
403
                $replace_with = $form->name;
404
            } else if ( $code == 'form_description' ) {
405
                $replace_with = FrmAppHelper::use_wpautop($form->description);
406
            } else if ( $code == 'entry_key' && isset($_GET) && isset($_GET['entry']) ) {
407
                $replace_with = FrmAppHelper::simple_get( 'entry' );
408
            } else {
409
                $replace_with = '';
410
            }
411
412
            FrmFieldsHelper::remove_inline_conditions( ( FrmAppHelper::is_true($show) && $replace_with != '' ), $code, $replace_with, $html );
0 ignored issues
show
Documentation introduced by
$show is of type boolean, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
413
        }
414
415
        //replace [form_key]
416
        $html = str_replace('[form_key]', $form->form_key, $html);
417
418
        //replace [frmurl]
419
        $html = str_replace('[frmurl]', FrmFieldsHelper::dynamic_default_values( 'frmurl' ), $html);
420
421
		if ( strpos( $html, '[button_label]' ) ) {
422
			add_filter( 'frm_submit_button', 'FrmFormsHelper::submit_button_label', 1 );
423
			$submit_label = apply_filters( 'frm_submit_button', $title, $form );
424
			$submit_label = esc_attr( do_shortcode( $submit_label ) );
425
			$html = str_replace( '[button_label]', $submit_label, $html );
426
        }
427
428
        $html = apply_filters('frm_form_replace_shortcodes', $html, $form, $values);
429
430
		if ( strpos( $html, '[if back_button]' ) ) {
431
			$html = preg_replace( '/(\[if\s+back_button\])(.*?)(\[\/if\s+back_button\])/mis', '', $html );
432
		}
433
434
		if ( strpos( $html, '[if save_draft]' ) ) {
435
			$html = preg_replace( '/(\[if\s+save_draft\])(.*?)(\[\/if\s+save_draft\])/mis', '', $html );
436
		}
437
438
		if ( apply_filters( 'frm_do_html_shortcodes', true ) ) {
439
			$html = do_shortcode( $html );
440
		}
441
442
        return $html;
443
    }
444
445
	public static function submit_button_label( $submit ) {
446
        if ( ! $submit || empty($submit) ) {
447
            $frm_settings = FrmAppHelper::get_settings();
448
            $submit = $frm_settings->submit_value;
449
        }
450
451
        return $submit;
452
    }
453
454
	public static function get_form_style_class( $form = false ) {
455
        $style = self::get_form_style($form);
456
        $class = ' with_frm_style';
457
458
        if ( empty($style) ) {
459
            if ( FrmAppHelper::is_admin_page('formidable-entries') ) {
460
                return $class;
461
            } else {
462
                return;
463
            }
464
        }
465
466
        //If submit button needs to be inline or centered
467
        if ( is_object($form) ) {
468
			$form = $form->options;
469
		}
470
471
		$submit_align = isset( $form['submit_align'] ) ? $form['submit_align'] : '';
472
473
		if ( $submit_align == 'inline' ) {
474
			$class .= ' frm_inline_form';
475
		} else if ( $submit_align == 'center' ) {
476
			$class .= ' frm_center_submit';
477
		}
478
479
        $class = apply_filters('frm_add_form_style_class', $class, $style);
480
481
        return $class;
482
    }
483
484
    /**
485
     * @param string|boolean $form
486
     *
487
     * @return string
488
     */
489
    public static function get_form_style( $form ) {
490
		$style = 1;
491
		if ( empty( $form ) || 'default' == 'form' ) {
492
			return $style;
493
		} else if ( is_object( $form ) && $form->parent_form_id ) {
494
			// get the parent form if this is a child
495
			$form = $form->parent_form_id;
496
		} else if ( is_array( $form ) && isset( $form['parent_form_id'] ) && $form['parent_form_id'] ) {
497
			$form = $form['parent_form_id'];
498
		} else if ( is_array( $form ) && isset( $form['custom_style'] ) ) {
499
			$style = $form['custom_style'];
500
		}
501
502
		if ( $form && is_string( $form ) ) {
503
			$form = FrmForm::getOne( $form );
504
		}
505
506
		$style = ( $form && is_object( $form ) && isset( $form->options['custom_style'] ) ) ? $form->options['custom_style'] : $style;
507
508
		return $style;
509
    }
510
511
	/**
512
	 * Display the validation error messages when an entry is submitted
513
	 *
514
	 * @param array $args - includes img, errors
515
	 * @since 2.0.6
516
	 */
517
	public static function show_errors( $args ) {
518
		$invalid_msg = self::get_invalid_error_message( $args );
519
520
		if ( empty( $invalid_msg ) ) {
521
			$show_img = false;
522
		} else {
523
			echo wp_kses_post( $invalid_msg );
524
			$show_img = true;
525
		}
526
527
		self::show_error( array( 'img' => $args['img'], 'errors' => $args['errors'], 'show_img' => $show_img ) );
528
	}
529
530
	/**
531
	 * Display the error message in the front-end along with the image if set
532
	 * The image was removed from the styling settings, but it may still be set with a hook
533
	 * If the message in the global settings is empty, show every validation message in the error box
534
	 *
535
	 * @param array $args - includes img, errors, and show_img
536
	 * @since 2.0.6
537
	 */
538
	public static function show_error( $args ) {
539
		$line_break_first = $args['show_img'];
540
		foreach ( $args['errors'] as $error_key => $error ) {
541
			if ( $line_break_first && ! is_numeric( $error_key ) && ( $error_key == 'cptch_number' || strpos( $error_key, 'field' ) === 0 ) ) {
542
				continue;
543
			}
544
545
			if ( $line_break_first ) {
546
				echo '<br/>';
547
			}
548
549
			if ( $args['show_img'] && ! empty( $args['img'] ) ) {
550
				?><img src="<?php echo esc_attr( $args['img'] ) ?>" alt="" /><?php
551
			} else {
552
				$args['show_img'] = true;
553
			}
554
555
			echo wp_kses_post( $error );
556
557
			if ( ! $line_break_first ) {
558
				echo '<br/>';
559
			}
560
		}
561
	}
562
563
	public static function maybe_get_scroll_js( $id ) {
564
		$offset = apply_filters( 'frm_scroll_offset', 4, array( 'form_id' => $id ) );
565
		if ( $offset != -1 ) {
566
			self::get_scroll_js( $id );
567
		}
568
	}
569
570
	public static function get_scroll_js( $form_id ) {
571
        ?><script type="text/javascript">document.addEventListener('DOMContentLoaded',function(){frmFrontForm.scrollMsg(<?php echo (int) $form_id ?>);})</script><?php
572
    }
573
574
	public static function edit_form_link( $form_id ) {
575
        if ( is_object($form_id) ) {
576
            $form = $form_id;
577
            $name = $form->name;
578
            $form_id = $form->id;
579
        } else {
580
            $name = FrmForm::getName($form_id);
581
        }
582
583
        if ( $form_id ) {
584
			$val = '<a href="' . esc_url( admin_url( 'admin.php?page=formidable&frm_action=edit&id=' . $form_id ) ) . '">' . ( '' == $name ? __( '(no title)' ) : FrmAppHelper::truncate( $name, 40 ) ) . '</a>';
585
	    } else {
586
	        $val = '';
587
	    }
588
589
	    return $val;
590
	}
591
592
	public static function delete_trash_link( $id, $status, $length = 'long' ) {
593
        $link = '';
594
        $labels = array(
595
            'restore' => array(
596
                'long'  => __( 'Restore from Trash', 'formidable' ),
597
                'short' => __( 'Restore', 'formidable' ),
598
            ),
599
            'trash' => array(
600
                'long'  => __( 'Move to Trash', 'formidable' ),
601
                'short' => __( 'Trash', 'formidable' ),
602
            ),
603
            'delete' => array(
604
                'long'  => __( 'Delete Permanently', 'formidable' ),
605
                'short' => __( 'Delete', 'formidable' ),
606
            ),
607
        );
608
609
        $current_page = isset( $_REQUEST['form_type'] ) ? $_REQUEST['form_type'] : '';
0 ignored issues
show
introduced by
Detected access of super global var $_REQUEST, probably need manual inspection.
Loading history...
introduced by
Detected usage of a non-sanitized input variable: $_REQUEST
Loading history...
610
		$base_url = '?page=formidable&form_type=' . $current_page . '&id=' . $id;
611
        if ( 'trash' == $status ) {
612
			$link = '<a href="' . esc_url( wp_nonce_url( $base_url . '&frm_action=untrash', 'untrash_form_' . $id ) ) . '" class="submitdelete deletion">' . $labels['restore'][ $length ] . '</a>';
613
        } else if ( current_user_can('frm_delete_forms') ) {
614
            if ( EMPTY_TRASH_DAYS ) {
615
				$link = '<a href="' . esc_url( wp_nonce_url( $base_url . '&frm_action=trash', 'trash_form_' . $id ) ) . '" class="submitdelete deletion">' . $labels['trash'][ $length ] . '</a>';
616
            } else {
617
				$link = '<a href="' . esc_url( wp_nonce_url( $base_url . '&frm_action=destroy', 'destroy_form_' . $id ) ) . '" class="submitdelete deletion" onclick="return confirm(\'' . esc_attr( __( 'Are you sure you want to delete this form and all its entries?', 'formidable' ) ) . '\')">' . $labels['delete'][ $length ] . '</a>';
0 ignored issues
show
introduced by
Expected next thing to be a escaping function, not '$labels'
Loading history...
618
            }
619
        }
620
621
        return $link;
622
    }
623
624
	public static function status_nice_name( $status ) {
625
        $nice_names = array(
626
            'draft'     => __( 'Draft', 'formidable' ),
627
            'trash'     => __( 'Trash', 'formidable' ),
628
            'publish'   => __( 'Published', 'formidable' ),
629
        );
630
631
        if ( ! in_array($status, array_keys($nice_names)) ) {
632
            $status = 'publish';
633
        }
634
635
		$name = $nice_names[ $status ];
636
637
        return $name;
638
    }
639
640
	public static function get_params() {
641
		_deprecated_function( __FUNCTION__, '2.0.9', 'FrmForm::list_page_params' );
642
		return FrmForm::list_page_params();
643
	}
644
645
	public static function form_loaded( $form, $this_load, $global_load ) {
646
		_deprecated_function( __FUNCTION__, '2.0.9', 'FrmFormsController::maybe_load_css' );
647
		FrmFormsController::maybe_load_css( $form, $this_load, $global_load );
648
	}
649
}
650