Completed
Push — master ( cbbb72...0e27d3 )
by Stephanie
03:08
created

FrmFormsHelper::maybe_hide_inline()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 8
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 6
nc 3
nop 0
dl 0
loc 8
rs 9.4285
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
            $draft_link = self::get_draft_link();
269
            $default_html = <<<SUBMIT_HTML
270
<div class="frm_submit">
271
[if back_button]<button type="submit" name="frm_prev_page" formnovalidate="formnovalidate" class="frm_prev_page" [back_hook]>[back_label]</button>[/if back_button]
272
<button class="frm_button_submit" type="submit"  [button_action]>[button_label]</button>
273
$draft_link
274
</div>
275
SUBMIT_HTML;
276
		} else if ( $loc == 'before' ) {
277
            $default_html = <<<BEFORE_HTML
278
<legend class="frm_hidden">[form_name]</legend>
279
[if form_name]<h3 class="frm_form_title">[form_name]</h3>[/if form_name]
280
[if form_description]<div class="frm_description">[form_description]</div>[/if form_description]
281
BEFORE_HTML;
282
		} else {
283
            $default_html = '';
284
        }
285
286
        return $default_html;
287
    }
288
289
    public static function get_draft_link() {
290
        $link = '[if save_draft]<a href="#" class="frm_save_draft" [draft_hook]>[draft_label]</a>[/if save_draft]';
291
        return $link;
292
    }
293
294
	public static function get_custom_submit( $html, $form, $submit, $form_action, $values ) {
295
		$button = self::replace_shortcodes( $html, $form, $submit, $form_action, $values );
296
		if ( ! strpos( $button, '[button_action]' ) ) {
297
			echo $button;
0 ignored issues
show
introduced by
Expected next thing to be a escaping function, not '$button'
Loading history...
298
			return;
299
		}
300
301
		$button_parts = explode( '[button_action]', $button );
302
303
		$classes = apply_filters( 'frm_submit_button_class', array(), $form );
304
		if ( ! empty( $classes ) ) {
305
			$classes = implode( ' ', $classes );
306
			$button_class = ' class="frm_button_submit';
307
			if ( strpos( $button_parts[0], $button_class ) !== false ) {
308
				$button_parts[0] = str_replace( $button_class, $button_class . ' ' . esc_attr( $classes ), $button_parts[0] );
309
			} else {
310
				$button_parts[0] .= ' class="' . esc_attr( $classes ) . '"';
311
			}
312
		}
313
314
		echo $button_parts[0];
0 ignored issues
show
introduced by
Expected next thing to be a escaping function, not '$button_parts'
Loading history...
315
		do_action( 'frm_submit_button_action', $form, $form_action );
316
		echo $button_parts[1];
0 ignored issues
show
introduced by
Expected next thing to be a escaping function, not '$button_parts'
Loading history...
317
	}
318
319
    /**
320
     * Automatically add end section fields if they don't exist (2.0 migration)
321
     * @since 2.0
322
     *
323
     * @param boolean $reset_fields
324
     */
325
    public static function auto_add_end_section_fields( $form, $fields, &$reset_fields ) {
326
		if ( empty( $fields ) ) {
327
			return;
328
		}
329
330
		$end_section_values = apply_filters( 'frm_before_field_created', FrmFieldsHelper::setup_new_vars( 'end_divider', $form->id ) );
331
		$open = false;
332
		$prev_order = false;
333
		$add_order = 0;
334
		$last_field = false;
335
        foreach ( $fields as $field ) {
336
			if ( $prev_order === $field->field_order ) {
337
				$add_order++;
338
			}
339
340
			if ( $add_order ) {
341
				$reset_fields = true;
342
				$field->field_order = $field->field_order + $add_order;
343
				FrmField::update( $field->id, array( 'field_order' => $field->field_order ) );
344
			}
345
346
            switch ( $field->type ) {
347
                case 'divider':
348
                    // create an end section if open
349
					self::maybe_create_end_section( $open, $reset_fields, $add_order, $end_section_values, $field, 'move' );
350
351
                    // mark it open for the next end section
352
                    $open = true;
353
                break;
354
                case 'break';
355
					self::maybe_create_end_section( $open, $reset_fields, $add_order, $end_section_values, $field, 'move' );
356
                break;
357
                case 'end_divider':
358
                    if ( ! $open ) {
359
                        // the section isn't open, so this is an extra field that needs to be removed
360
                        FrmField::destroy( $field->id );
361
                        $reset_fields = true;
362
                    }
363
364
                    // There is already an end section here, so there is no need to create one
365
                    $open = false;
366
            }
367
			$prev_order = $field->field_order;
368
369
			$last_field = $field;
370
			unset( $field );
371
        }
372
373
		self::maybe_create_end_section( $open, $reset_fields, $add_order, $end_section_values, $last_field );
374
    }
375
376
	/**
377
	 * Create end section field if it doesn't exist. This is for migration from < 2.0
378
	 * Fix any ordering that may be messed up
379
	 */
380
	public static function maybe_create_end_section( &$open, &$reset_fields, &$add_order, $end_section_values, $field, $move = 'no' ) {
381
        if ( ! $open ) {
382
            return;
383
        }
384
385
		$end_section_values['field_order'] = $field->field_order + 1;
386
387
        FrmField::create( $end_section_values );
388
389
		if ( $move == 'move' ) {
390
			// bump the order of current field unless we're at the end of the form
391
			FrmField::update( $field->id, array( 'field_order' => $field->field_order + 2 ) );
392
		}
393
394
		$add_order += 2;
395
        $open = false;
396
        $reset_fields = true;
397
    }
398
399
    public static function replace_shortcodes( $html, $form, $title = false, $description = false, $values = array() ) {
400
		foreach ( array( 'form_name' => $title, 'form_description' => $description, 'entry_key' => true ) as $code => $show ) {
401
            if ( $code == 'form_name' ) {
402
                $replace_with = $form->name;
403
            } else if ( $code == 'form_description' ) {
404
                $replace_with = FrmAppHelper::use_wpautop($form->description);
405
            } else if ( $code == 'entry_key' && isset($_GET) && isset($_GET['entry']) ) {
406
                $replace_with = FrmAppHelper::simple_get( 'entry' );
407
            } else {
408
                $replace_with = '';
409
            }
410
411
            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...
412
        }
413
414
        //replace [form_key]
415
        $html = str_replace('[form_key]', $form->form_key, $html);
416
417
        //replace [frmurl]
418
        $html = str_replace('[frmurl]', FrmFieldsHelper::dynamic_default_values( 'frmurl' ), $html);
419
420
		if ( strpos( $html, '[button_label]' ) ) {
421
			add_filter( 'frm_submit_button', 'FrmFormsHelper::submit_button_label', 1 );
422
			$submit_label = apply_filters( 'frm_submit_button', $title, $form );
423
			$submit_label = esc_attr( do_shortcode( $submit_label ) );
424
			$html = str_replace( '[button_label]', $submit_label, $html );
425
        }
426
427
        $html = apply_filters('frm_form_replace_shortcodes', $html, $form, $values);
428
429
		if ( strpos( $html, '[if back_button]' ) ) {
430
			$html = preg_replace( '/(\[if\s+back_button\])(.*?)(\[\/if\s+back_button\])/mis', '', $html );
431
		}
432
433
		if ( strpos( $html, '[if save_draft]' ) ) {
434
			$html = preg_replace( '/(\[if\s+save_draft\])(.*?)(\[\/if\s+save_draft\])/mis', '', $html );
435
		}
436
437
		if ( apply_filters( 'frm_do_html_shortcodes', true ) ) {
438
			$html = do_shortcode( $html );
439
		}
440
441
        return $html;
442
    }
443
444
	public static function submit_button_label( $submit ) {
445
        if ( ! $submit || empty($submit) ) {
446
            $frm_settings = FrmAppHelper::get_settings();
447
            $submit = $frm_settings->submit_value;
448
        }
449
450
        return $submit;
451
    }
452
453
	/**
454
	 * If the Formidable styling isn't being loaded,
455
	 * use inline styling to hide the element
456
	 * @since 2.03.05
457
	 */
458
	public static function maybe_hide_inline() {
459
		$frm_settings = FrmAppHelper::get_settings();
460
		if ( $frm_settings->load_style == 'none' ) {
461
			echo ' style="display:none;"';
462
		} elseif ( $frm_settings->load_style == 'dynamic' ) {
463
			FrmStylesController::enqueue_style();
464
		}
465
	}
466
467
	public static function get_form_style_class( $form = false ) {
468
        $style = self::get_form_style($form);
469
        $class = ' with_frm_style';
470
471
        if ( empty($style) ) {
472
            if ( FrmAppHelper::is_admin_page('formidable-entries') ) {
473
                return $class;
474
            } else {
475
                return;
476
            }
477
        }
478
479
        //If submit button needs to be inline or centered
480
        if ( is_object($form) ) {
481
			$form = $form->options;
482
		}
483
484
		$submit_align = isset( $form['submit_align'] ) ? $form['submit_align'] : '';
485
486
		if ( $submit_align == 'inline' ) {
487
			$class .= ' frm_inline_form';
488
		} else if ( $submit_align == 'center' ) {
489
			$class .= ' frm_center_submit';
490
		}
491
492
        $class = apply_filters('frm_add_form_style_class', $class, $style);
493
494
        return $class;
495
    }
496
497
    /**
498
     * @param string|boolean $form
499
     *
500
     * @return string
501
     */
502
    public static function get_form_style( $form ) {
503
		$style = 1;
504
		if ( empty( $form ) || 'default' == 'form' ) {
505
			return $style;
506
		} else if ( is_object( $form ) && $form->parent_form_id ) {
507
			// get the parent form if this is a child
508
			$form = $form->parent_form_id;
509
		} else if ( is_array( $form ) && isset( $form['parent_form_id'] ) && $form['parent_form_id'] ) {
510
			$form = $form['parent_form_id'];
511
		} else if ( is_array( $form ) && isset( $form['custom_style'] ) ) {
512
			$style = $form['custom_style'];
513
		}
514
515
		if ( $form && is_string( $form ) ) {
516
			$form = FrmForm::getOne( $form );
517
		}
518
519
		$style = ( $form && is_object( $form ) && isset( $form->options['custom_style'] ) ) ? $form->options['custom_style'] : $style;
520
521
		return $style;
522
    }
523
524
	/**
525
	 * Display the validation error messages when an entry is submitted
526
	 *
527
	 * @param array $args - includes img, errors
528
	 * @since 2.0.6
529
	 */
530
	public static function show_errors( $args ) {
531
		$invalid_msg = self::get_invalid_error_message( $args );
532
533
		if ( empty( $invalid_msg ) ) {
534
			$show_img = false;
535
		} else {
536
			echo wp_kses_post( $invalid_msg );
537
			$show_img = true;
538
		}
539
540
		self::show_error( array( 'img' => $args['img'], 'errors' => $args['errors'], 'show_img' => $show_img ) );
541
	}
542
543
	/**
544
	 * Display the error message in the front-end along with the image if set
545
	 * The image was removed from the styling settings, but it may still be set with a hook
546
	 * If the message in the global settings is empty, show every validation message in the error box
547
	 *
548
	 * @param array $args - includes img, errors, and show_img
549
	 * @since 2.0.6
550
	 */
551
	public static function show_error( $args ) {
552
		$line_break_first = $args['show_img'];
553
		foreach ( $args['errors'] as $error_key => $error ) {
554
			if ( $line_break_first && ! is_numeric( $error_key ) && ( $error_key == 'cptch_number' || strpos( $error_key, 'field' ) === 0 ) ) {
555
				continue;
556
			}
557
558
			if ( $line_break_first ) {
559
				echo '<br/>';
560
			}
561
562
			if ( $args['show_img'] && ! empty( $args['img'] ) ) {
563
				?><img src="<?php echo esc_attr( $args['img'] ) ?>" alt="" /><?php
564
			} else {
565
				$args['show_img'] = true;
566
			}
567
568
			echo wp_kses_post( $error );
569
570
			if ( ! $line_break_first ) {
571
				echo '<br/>';
572
			}
573
		}
574
	}
575
576
	public static function maybe_get_scroll_js( $id ) {
577
		$offset = apply_filters( 'frm_scroll_offset', 4, array( 'form_id' => $id ) );
578
		if ( $offset != -1 ) {
579
			self::get_scroll_js( $id );
580
		}
581
	}
582
583
	public static function get_scroll_js( $form_id ) {
584
        ?><script type="text/javascript">document.addEventListener('DOMContentLoaded',function(){frmFrontForm.scrollMsg(<?php echo (int) $form_id ?>);})</script><?php
585
    }
586
587
	public static function edit_form_link( $form_id ) {
588
        if ( is_object($form_id) ) {
589
            $form = $form_id;
590
            $name = $form->name;
591
            $form_id = $form->id;
592
        } else {
593
            $name = FrmForm::getName($form_id);
594
        }
595
596
        if ( $form_id ) {
597
			$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>';
598
	    } else {
599
	        $val = '';
600
	    }
601
602
	    return $val;
603
	}
604
605
	public static function delete_trash_link( $id, $status, $length = 'long' ) {
606
        $link = '';
607
        $labels = array(
608
            'restore' => array(
609
                'long'  => __( 'Restore from Trash', 'formidable' ),
610
                'short' => __( 'Restore', 'formidable' ),
611
            ),
612
            'trash' => array(
613
                'long'  => __( 'Move to Trash', 'formidable' ),
614
                'short' => __( 'Trash', 'formidable' ),
615
            ),
616
            'delete' => array(
617
                'long'  => __( 'Delete Permanently', 'formidable' ),
618
                'short' => __( 'Delete', 'formidable' ),
619
            ),
620
        );
621
622
        $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...
623
		$base_url = '?page=formidable&form_type=' . $current_page . '&id=' . $id;
624
        if ( 'trash' == $status ) {
625
			$link = '<a href="' . esc_url( wp_nonce_url( $base_url . '&frm_action=untrash', 'untrash_form_' . $id ) ) . '" class="submitdelete deletion">' . $labels['restore'][ $length ] . '</a>';
626
        } else if ( current_user_can('frm_delete_forms') ) {
627
            if ( EMPTY_TRASH_DAYS ) {
628
				$link = '<a href="' . esc_url( wp_nonce_url( $base_url . '&frm_action=trash', 'trash_form_' . $id ) ) . '" class="submitdelete deletion">' . $labels['trash'][ $length ] . '</a>';
629
            } else {
630
				$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...
631
            }
632
        }
633
634
        return $link;
635
    }
636
637
	public static function status_nice_name( $status ) {
638
        $nice_names = array(
639
            'draft'     => __( 'Draft', 'formidable' ),
640
            'trash'     => __( 'Trash', 'formidable' ),
641
            'publish'   => __( 'Published', 'formidable' ),
642
        );
643
644
        if ( ! in_array($status, array_keys($nice_names)) ) {
645
            $status = 'publish';
646
        }
647
648
		$name = $nice_names[ $status ];
649
650
        return $name;
651
    }
652
653
	public static function get_params() {
654
		_deprecated_function( __FUNCTION__, '2.0.9', 'FrmForm::list_page_params' );
655
		return FrmForm::list_page_params();
656
	}
657
658
	public static function form_loaded( $form, $this_load, $global_load ) {
659
		_deprecated_function( __FUNCTION__, '2.0.9', 'FrmFormsController::maybe_load_css' );
660
		FrmFormsController::maybe_load_css( $form, $this_load, $global_load );
661
	}
662
}
663