Completed
Push — master ( 9ef2ea...96155a )
by Stephanie
03:03
created

FrmFormsHelper::setup_new_vars()   B

Complexity

Conditions 10
Paths 135

Size

Total Lines 47

Duplication

Lines 10
Ratio 21.28 %

Importance

Changes 0
Metric Value
cc 10
nc 135
nop 1
dl 10
loc 47
rs 7.0563
c 0
b 0
f 0

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
if ( ! defined( 'ABSPATH' ) ) {
3
	die( 'You are not allowed to call this page directly.' );
4
}
5
6
class FrmFormsHelper {
7
8
	/**
9
	 * @since 2.2.10
10
	 */
11
	public static function form_error_class() {
12
		return apply_filters( 'frm_form_error_class', 'frm_error_style' );
13
	}
14
15
	public static function get_direct_link( $key, $form = false ) {
16
		$target_url = esc_url( admin_url( 'admin-ajax.php?action=frm_forms_preview&form=' . $key ) );
17
		$target_url = apply_filters( 'frm_direct_link', $target_url, $key, $form );
18
19
        return $target_url;
20
    }
21
22
    public static function forms_dropdown( $field_name, $field_value = '', $args = array() ) {
23
        $defaults = array(
24
            'blank'     => true,
25
            'field_id'  => false,
26
            'onchange'  => false,
27
            'exclude'   => false,
28
            'class'     => '',
29
			'inc_children' => 'exclude',
30
        );
31
        $args = wp_parse_args( $args, $defaults );
32
33
        if ( ! $args['field_id'] ) {
34
            $args['field_id'] = $field_name;
35
        }
36
37
		$query = array();
38
        if ( $args['exclude'] ) {
39
			$query['id !'] = $args['exclude'];
40
        }
41
42
		$where = apply_filters( 'frm_forms_dropdown', $query, $field_name );
43
		$forms = FrmForm::get_published_forms( $where, 999, $args['inc_children'] );
44
		$add_html = array();
45
		self::add_html_attr( $args['onchange'], 'onchange', $add_html );
46
		self::add_html_attr( $args['class'], 'class', $add_html );
47
48
        ?>
49
		<select name="<?php echo esc_attr( $field_name ); ?>" id="<?php echo esc_attr( $args['field_id'] ) ?>" <?php echo wp_strip_all_tags( implode( ' ', $add_html ) ); // WPCS: XSS ok. ?>>
50
		<?php if ( $args['blank'] ) { ?>
51
			<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...
52
		<?php } ?>
53
		<?php foreach ( $forms as $form ) { ?>
54
			<option value="<?php echo esc_attr( $form->id ); ?>" <?php selected( $field_value, $form->id ); ?>>
55
				<?php echo esc_html( '' === $form->name ? __( '(no title)', 'formidable' ) : FrmAppHelper::truncate( $form->name, 50 ) . ( $form->parent_form_id ? __( ' (child)', 'formidable' ) : '' ) ); ?>
0 ignored issues
show
introduced by
Expected next thing to be a escaping function, not '('
Loading history...
56
			</option>
57
		<?php } ?>
58
        </select>
59
        <?php
60
    }
61
62
	/**
63
	 * @param string $class
64
	 * @param string $param
65
	 * @param array $add_html
66
	 *
67
	 * @since 2.0.6
68
	 */
69
	public static function add_html_attr( $class, $param, &$add_html ) {
70
		if ( ! empty( $class ) ) {
71
			$add_html[ $param ] = sanitize_title( $param ) . '="' . esc_attr( trim( sanitize_text_field( $class ) ) ) . '"';
72
		}
73
	}
74
75
    public static function form_switcher() {
76
		$where = apply_filters( 'frm_forms_dropdown', array(), '' );
77
		$forms = FrmForm::get_published_forms( $where );
78
79
		$args = array(
80
			'id'   => 0,
81
			'form' => 0,
82
		);
83
		if ( isset( $_GET['id'] ) && ! isset( $_GET['form'] ) ) {
84
			unset( $args['form'] );
85
		} elseif ( isset( $_GET['form'] ) && ! isset( $_GET['id'] ) ) {
86
			unset( $args['id'] );
87
        }
88
89
		$frm_action = FrmAppHelper::simple_get( 'frm_action', 'sanitize_title' );
90
		if ( FrmAppHelper::is_admin_page( 'formidable-entries' ) && in_array( $frm_action, array( 'edit', 'show', 'destroy_all' ) ) ) {
91
            $args['frm_action'] = 'list';
92
            $args['form'] = 0;
93
		} elseif ( FrmAppHelper::is_admin_page( 'formidable' ) && in_array( $frm_action, array( 'new', 'duplicate' ) ) ) {
94
            $args['frm_action'] = 'edit';
95
		} else if ( isset( $_GET['post'] ) ) {
96
            $args['form'] = 0;
97
			$base = admin_url( 'edit.php?post_type=frm_display' );
98
        }
99
100
        ?>
101
		<li id="frm_bs_dropdown" class="dropdown <?php echo esc_attr( is_rtl() ? 'pull-right' : 'pull-left' ) ?>">
102
			<a href="#" id="frm-navbarDrop" class="frm-dropdown-toggle" data-toggle="dropdown"><?php esc_html_e( 'Switch Form', 'formidable' ) ?> <b class="caret"></b></a>
103
		    <ul class="frm-dropdown-menu frm-on-top" role="menu" aria-labelledby="frm-navbarDrop">
104
			<?php
105
			foreach ( $forms as $form ) {
106
				if ( isset( $args['id'] ) ) {
107
			        $args['id'] = $form->id;
108
				}
109
			    if ( isset( $args['form'] ) ) {
110
			        $args['form'] = $form->id;
111
				}
112
                ?>
113
				<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>
114
			<?php
115
				unset( $form );
116
			}
117
			?>
118
			</ul>
119
		</li>
120
        <?php
121
    }
122
123
	/**
124
	 * @since 3.05
125
	 * @param array $values - The form array
126
	 */
127
	public static function builder_submit_button( $values ) {
128
		$page_action = FrmAppHelper::get_param( 'frm_action' );
129
		$label = ( $page_action == 'edit' || $page_action == 'update' ) ? __( 'Update', 'formidable' ) : __( 'Create', 'formidable' );
130
131
		?>
132
		<div class="postbox">
133
			<p class="inside">
134
				<button class="frm_submit_<?php echo esc_attr( ( isset( $values['ajax_load'] ) && $values['ajax_load'] ) ? '' : 'no_' ); ?>ajax button-primary frm_button_submit" type="button">
135
					<?php echo esc_html( $label ); ?>
136
				</button>
137
			</p>
138
		</div>
139
		<?php
140
	}
141
142
	public static function get_sortable_classes( $col, $sort_col, $sort_dir ) {
143
		echo ( $sort_col == $col ) ? 'sorted' : 'sortable';
0 ignored issues
show
introduced by
Expected next thing to be a escaping function, not '('
Loading history...
144
		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...
145
	}
146
147
	/**
148
	 * @since 3.0
149
	 */
150
	public static function get_field_link_name( $field_type ) {
151
		if ( is_array( $field_type ) ) {
152
			$field_label = $field_type['name'];
153
		} else {
154
			$field_label = $field_type;
155
		}
156
		return $field_label;
157
	}
158
159
	/**
160
	 * @since 3.0
161
	 */
162
	public static function get_field_link_icon( $field_type ) {
163
		if ( is_array( $field_type ) && isset( $field_type['icon'] ) ) {
164
			$icon = $field_type['icon'];
165
		} else {
166
			$icon = 'frm_icon_font frm_pencil_icon';
167
		}
168
		return $icon;
169
	}
170
171
	/**
172
	 * Get the invalid form error message
173
	 *
174
	 * @since 2.02.07
175
	 * @param array $args
176
	 * @return string
177
	 */
178
	public static function get_invalid_error_message( $args ) {
179
		$frm_settings = FrmAppHelper::get_settings();
180
		$invalid_msg = do_shortcode( $frm_settings->invalid_msg );
181
		return apply_filters( 'frm_invalid_error_message', $invalid_msg, $args );
182
	}
183
184
	public static function get_success_message( $atts ) {
185
		$message = apply_filters( 'frm_content', $atts['message'], $atts['form'], $atts['entry_id'] );
186
		$message = FrmAppHelper::use_wpautop( do_shortcode( $message ) );
187
		$message = '<div class="' . esc_attr( $atts['class'] ) . '">' . $message . '</div>';
188
		return $message;
189
	}
190
191
    /**
192
     * Used when a form is created
193
     */
194
    public static function setup_new_vars( $values = array() ) {
195
        global $wpdb;
196
197
        if ( ! empty( $values ) ) {
198
            $post_values = $values;
199
        } else {
200
            $values = array();
201
			$post_values = isset( $_POST ) ? $_POST : array();
202
        }
203
204
		$defaults = array(
205
			'name' => '',
206
			'description' => '',
207
		);
208 View Code Duplication
		foreach ( $defaults as $var => $default ) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
209
			if ( ! isset( $values[ $var ] ) ) {
210
				$values[ $var ] = FrmAppHelper::get_param( $var, $default, 'get', 'sanitize_text_field' );
211
            }
212
        }
213
214
		$values['description'] = FrmAppHelper::use_wpautop( $values['description'] );
215
216
		$defaults = array(
217
			'form_id'        => '',
218
			'logged_in'      => '',
219
			'editable'       => '',
220
			'default_template' => 0,
221
			'is_template'    => 0,
222
			'status'         => 'draft',
223
			'parent_form_id' => 0,
224
		);
225 View Code Duplication
		foreach ( $defaults as $var => $default ) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
226
			if ( ! isset( $values[ $var ] ) ) {
227
				$values[ $var ] = FrmAppHelper::get_param( $var, $default, 'get', 'sanitize_text_field' );
228
			}
229
		}
230
		unset( $defaults );
231
232
        if ( ! isset( $values['form_key'] ) ) {
233
			$values['form_key'] = ( $post_values && isset( $post_values['form_key'] ) ) ? $post_values['form_key'] : FrmAppHelper::get_unique_key( '', $wpdb->prefix . 'frm_forms', 'form_key' );
234
        }
235
236
		$values = self::fill_default_opts( $values, false, $post_values );
237
		$values['custom_style'] = FrmAppHelper::custom_style_value( $post_values );
238
239
		return apply_filters( 'frm_setup_new_form_vars', $values );
240
    }
241
242
    /**
243
     * Used when editing a form
244
     */
245
    public static function setup_edit_vars( $values, $record, $post_values = array() ) {
246
		if ( empty( $post_values ) ) {
247
			$post_values = stripslashes_deep( $_POST );
248
		}
249
250
		$values['form_key'] = isset( $post_values['form_key'] ) ? $post_values['form_key'] : $record->form_key;
251
		$values['default_template'] = isset( $post_values['default_template'] ) ? $post_values['default_template'] : $record->default_template;
252
		$values['is_template'] = isset( $post_values['is_template'] ) ? $post_values['is_template'] : $record->is_template;
253
        $values['status'] = $record->status;
254
255
		$values = self::fill_default_opts( $values, $record, $post_values );
256
257
		return apply_filters( 'frm_setup_edit_form_vars', $values );
258
    }
259
260
	public static function fill_default_opts( $values, $record, $post_values ) {
261
262
        $defaults = self::get_default_opts();
263
		foreach ( $defaults as $var => $default ) {
264
			if ( is_array( $default ) ) {
265
                if ( ! isset( $values[ $var ] ) ) {
266
					$values[ $var ] = ( $record && isset( $record->options[ $var ] ) ) ? $record->options[ $var ] : array();
267
                }
268
269
                foreach ( $default as $k => $v ) {
270
					$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 );
271
272
                    if ( is_array( $v ) ) {
273
                        foreach ( $v as $k1 => $v1 ) {
274
							$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 );
275
                            unset( $k1, $v1 );
276
                        }
277
                    }
278
279
					unset( $k, $v );
280
                }
281
            } else {
282
				$values[ $var ] = ( $post_values && isset( $post_values['options'][ $var ] ) ) ? $post_values['options'][ $var ] : ( ( $record && isset( $record->options[ $var ] ) ) ? $record->options[ $var ] : $default );
283
            }
284
285
			unset( $var, $default );
286
        }
287
288
        return $values;
289
    }
290
291
	public static function get_default_opts() {
292
		$frm_settings = FrmAppHelper::get_settings();
293
294
		return array(
295
			'submit_value'   => $frm_settings->submit_value,
296
			'success_action' => 'message',
297
			'success_msg'    => $frm_settings->success_msg,
298
			'show_form'      => 0,
299
			'akismet'        => '',
300
			'no_save'        => 0,
301
			'ajax_load'      => 0,
302
			'js_validate'    => 0,
303
			'form_class'     => '',
304
			'custom_style'   => 1,
305
			'before_html'    => self::get_default_html( 'before' ),
306
			'after_html'     => '',
307
			'submit_html'    => self::get_default_html( 'submit' ),
308
		);
309
	}
310
311
	/**
312
	 * @param array $options
313
	 * @param array $values
314
	 * @since 2.0.6
315
	 */
316
	public static function fill_form_options( &$options, $values ) {
317
		$defaults = self::get_default_opts();
318
		foreach ( $defaults as $var => $default ) {
319
			$options[ $var ] = isset( $values['options'][ $var ] ) ? $values['options'][ $var ] : $default;
320
			unset( $var, $default );
321
		}
322
	}
323
324
    /**
325
     * @param string $loc
326
     */
327
	public static function get_default_html( $loc ) {
328
		if ( $loc == 'submit' ) {
329
            $draft_link = self::get_draft_link();
330
            $default_html = <<<SUBMIT_HTML
331
<div class="frm_submit">
332
[if back_button]<button type="submit" name="frm_prev_page" formnovalidate="formnovalidate" class="frm_prev_page" [back_hook]>[back_label]</button>[/if back_button]
333
<button class="frm_button_submit" type="submit"  [button_action]>[button_label]</button>
334
$draft_link
335
</div>
336
SUBMIT_HTML;
337
		} else if ( $loc == 'before' ) {
338
            $default_html = <<<BEFORE_HTML
339
<legend class="frm_screen_reader">[form_name]</legend>
340
[if form_name]<h3 class="frm_form_title">[form_name]</h3>[/if form_name]
341
[if form_description]<div class="frm_description">[form_description]</div>[/if form_description]
342
BEFORE_HTML;
343
		} else {
344
            $default_html = '';
345
        }
346
347
        return $default_html;
348
    }
349
350
    public static function get_draft_link() {
351
        $link = '[if save_draft]<a href="#" tabindex="0" class="frm_save_draft" [draft_hook]>[draft_label]</a>[/if save_draft]';
352
        return $link;
353
    }
354
355
	public static function get_custom_submit( $html, $form, $submit, $form_action, $values ) {
356
		$button = self::replace_shortcodes( $html, $form, $submit, $form_action, $values );
357
		if ( ! strpos( $button, '[button_action]' ) ) {
358
			echo $button; // WPCS: XSS ok.
359
			return;
360
		}
361
362
		$button_parts = explode( '[button_action]', $button );
363
364
		$classes = apply_filters( 'frm_submit_button_class', array(), $form );
365
		if ( ! empty( $classes ) ) {
366
			$classes = implode( ' ', $classes );
367
			$button_class = ' class="frm_button_submit';
368
			if ( strpos( $button_parts[0], $button_class ) !== false ) {
369
				$button_parts[0] = str_replace( $button_class, $button_class . ' ' . esc_attr( $classes ), $button_parts[0] );
370
			} else {
371
				$button_parts[0] .= ' class="' . esc_attr( $classes ) . '"';
372
			}
373
		}
374
375
		echo $button_parts[0]; // WPCS: XSS ok.
376
		do_action( 'frm_submit_button_action', $form, $form_action );
377
		echo $button_parts[1]; // WPCS: XSS ok.
378
	}
379
380
    /**
381
     * Automatically add end section fields if they don't exist (2.0 migration)
382
	 *
383
     * @since 2.0
384
     *
385
     * @param boolean $reset_fields
386
     */
387
    public static function auto_add_end_section_fields( $form, $fields, &$reset_fields ) {
388
		if ( empty( $fields ) ) {
389
			return;
390
		}
391
392
		$end_section_values = apply_filters( 'frm_before_field_created', FrmFieldsHelper::setup_new_vars( 'end_divider', $form->id ) );
393
		$open = false;
394
		$prev_order = false;
395
		$add_order = 0;
396
		$last_field = false;
397
        foreach ( $fields as $field ) {
398
			if ( $prev_order === $field->field_order ) {
399
				$add_order++;
400
			}
401
402
			if ( $add_order ) {
403
				$reset_fields = true;
404
				$field->field_order = $field->field_order + $add_order;
405
				FrmField::update( $field->id, array( 'field_order' => $field->field_order ) );
406
			}
407
408
            switch ( $field->type ) {
409
                case 'divider':
410
                    // create an end section if open
411
					self::maybe_create_end_section( $open, $reset_fields, $add_order, $end_section_values, $field, 'move' );
412
413
                    // mark it open for the next end section
414
                    $open = true;
415
					break;
416
                case 'break':
417
					self::maybe_create_end_section( $open, $reset_fields, $add_order, $end_section_values, $field, 'move' );
418
					break;
419
                case 'end_divider':
420
                    if ( ! $open ) {
421
                        // the section isn't open, so this is an extra field that needs to be removed
422
                        FrmField::destroy( $field->id );
423
                        $reset_fields = true;
424
                    }
425
426
                    // There is already an end section here, so there is no need to create one
427
                    $open = false;
428
            }
429
			$prev_order = $field->field_order;
430
431
			$last_field = $field;
432
			unset( $field );
433
        }
434
435
		self::maybe_create_end_section( $open, $reset_fields, $add_order, $end_section_values, $last_field );
436
    }
437
438
	/**
439
	 * Create end section field if it doesn't exist. This is for migration from < 2.0
440
	 * Fix any ordering that may be messed up
441
	 */
442
	public static function maybe_create_end_section( &$open, &$reset_fields, &$add_order, $end_section_values, $field, $move = 'no' ) {
443
        if ( ! $open ) {
444
            return;
445
        }
446
447
		$end_section_values['field_order'] = $field->field_order + 1;
448
449
        FrmField::create( $end_section_values );
450
451
		if ( $move == 'move' ) {
452
			// bump the order of current field unless we're at the end of the form
453
			FrmField::update( $field->id, array( 'field_order' => $field->field_order + 2 ) );
454
		}
455
456
		$add_order += 2;
457
        $open = false;
458
        $reset_fields = true;
459
    }
460
461
	public static function replace_shortcodes( $html, $form, $title = false, $description = false, $values = array() ) {
462
		$codes = array(
463
			'form_name' => $title,
464
			'form_description' => $description,
465
			'entry_key' => true,
466
		);
467
		foreach ( $codes as $code => $show ) {
468
			if ( $code == 'form_name' ) {
469
				$replace_with = $form->name;
470
			} elseif ( $code == 'form_description' ) {
471
				$replace_with = FrmAppHelper::use_wpautop( $form->description );
472
			} elseif ( $code == 'entry_key' && isset( $_GET ) && isset( $_GET['entry'] ) ) {
473
				$replace_with = FrmAppHelper::simple_get( 'entry' );
474
			} else {
475
				$replace_with = '';
476
			}
477
478
			FrmShortcodeHelper::remove_inline_conditions( ( FrmAppHelper::is_true( $show ) && $replace_with != '' ), $code, $replace_with, $html );
479
        }
480
481
		//replace [form_key]
482
		$html = str_replace( '[form_key]', $form->form_key, $html );
483
484
		//replace [frmurl]
485
		$html = str_replace( '[frmurl]', FrmFieldsHelper::dynamic_default_values( 'frmurl' ), $html );
486
487
		if ( strpos( $html, '[button_label]' ) ) {
488
			add_filter( 'frm_submit_button', 'FrmFormsHelper::submit_button_label', 1 );
489
			$submit_label = apply_filters( 'frm_submit_button', $title, $form );
490
			$submit_label = esc_attr( do_shortcode( $submit_label ) );
491
			$html = str_replace( '[button_label]', $submit_label, $html );
492
        }
493
494
		$html = apply_filters( 'frm_form_replace_shortcodes', $html, $form, $values );
495
496
		if ( strpos( $html, '[if back_button]' ) ) {
497
			$html = preg_replace( '/(\[if\s+back_button\])(.*?)(\[\/if\s+back_button\])/mis', '', $html );
498
		}
499
500
		if ( strpos( $html, '[if save_draft]' ) ) {
501
			$html = preg_replace( '/(\[if\s+save_draft\])(.*?)(\[\/if\s+save_draft\])/mis', '', $html );
502
		}
503
504
		if ( apply_filters( 'frm_do_html_shortcodes', true ) ) {
505
			$html = do_shortcode( $html );
506
		}
507
508
        return $html;
509
    }
510
511
	public static function submit_button_label( $submit ) {
512
		if ( ! $submit || empty( $submit ) ) {
513
            $frm_settings = FrmAppHelper::get_settings();
514
            $submit = $frm_settings->submit_value;
515
        }
516
517
        return $submit;
518
    }
519
520
	/**
521
	 * If the Formidable styling isn't being loaded,
522
	 * use inline styling to hide the element
523
	 *
524
	 * @since 2.03.05
525
	 */
526
	public static function maybe_hide_inline() {
527
		$frm_settings = FrmAppHelper::get_settings();
528
		if ( $frm_settings->load_style == 'none' ) {
529
			echo ' style="display:none;"';
530
		} elseif ( $frm_settings->load_style == 'dynamic' ) {
531
			FrmStylesController::enqueue_style();
532
		}
533
	}
534
535
	public static function get_form_style_class( $form = false ) {
536
		$style = self::get_form_style( $form );
537
		$class = ' with_frm_style';
538
539
		if ( empty( $style ) ) {
540
			if ( FrmAppHelper::is_admin_page( 'formidable-entries' ) ) {
541
				return $class;
542
			} else {
543
				return;
544
			}
545
		}
546
547
        //If submit button needs to be inline or centered
548
		if ( is_object( $form ) ) {
549
			$form = $form->options;
550
		}
551
552
		$submit_align = isset( $form['submit_align'] ) ? $form['submit_align'] : '';
553
554
		if ( 'inline' === $submit_align ) {
555
			$class .= ' frm_inline_form';
556
			$class .= self::maybe_align_fields_top( $form );
557
		} elseif ( 'center' === $submit_align ) {
558
			$class .= ' frm_center_submit';
559
		}
560
561
		$class = apply_filters( 'frm_add_form_style_class', $class, $style );
562
563
        return $class;
564
    }
565
566
	/**
567
	 * Returns appropriate class if form has top labels
568
	 *
569
	 * @param $form
570
	 *
571
	 * @return string
572
	 */
573
	private static function maybe_align_fields_top( $form ) {
574
		return self::form_has_top_labels( $form ) ? ' frm_inline_top' : '';
575
	}
576
577
	/**
578
	 * Determine if a form has fields with top labels so submit button can be aligned properly
579
	 *
580
	 * @param $form
581
	 *
582
	 * @return bool
583
	 */
584
	private static function form_has_top_labels( $form ) {
585
		if ( ! isset( $form['fields'] ) ) {
586
			return false;
587
		}
588
589
		$fields = $form['fields'];
590
		if ( count( $fields ) <= 0 ) {
591
			return false;
592
		}
593
594
		$fields = array_reverse( $fields ); // start from the fields closest to the submit button
595
		foreach ( $fields as $field ) {
596
			$type = isset( $field['original_type'] ) ? $field['original_type'] : $field['type'];
597
			$has_input = FrmFieldFactory::field_has_property( $type, 'has_input' );
598
			if ( $has_input ) {
599
				return self::field_has_top_label( $field, $form );
600
			}
601
		}
602
603
		return false;
604
	}
605
606
	/**
607
	 * Check if a field's label position is set to "top"
608
	 *
609
	 * @param $field
610
	 * @param $form
611
	 *
612
	 * @return bool
613
	 */
614
	private static function field_has_top_label( $field, $form ) {
615
		$label_position = FrmFieldsHelper::label_position( $field['label'], $field, $form );
616
		return in_array( $label_position, array( 'top', 'inside', 'hidden' ) );
617
	}
618
619
    /**
620
     * @param string|boolean $form
621
     *
622
     * @return string
623
     */
624
    public static function get_form_style( $form ) {
625
		$style = 1;
626
		if ( empty( $form ) || 'default' == 'form' ) {
627
			return $style;
628
		} else if ( is_object( $form ) && $form->parent_form_id ) {
629
			// get the parent form if this is a child
630
			$form = $form->parent_form_id;
631
		} else if ( is_array( $form ) && isset( $form['parent_form_id'] ) && $form['parent_form_id'] ) {
632
			$form = $form['parent_form_id'];
633
		} else if ( is_array( $form ) && isset( $form['custom_style'] ) ) {
634
			$style = $form['custom_style'];
635
		}
636
637
		if ( $form && is_string( $form ) ) {
638
			$form = FrmForm::getOne( $form );
639
		}
640
641
		$style = ( $form && is_object( $form ) && isset( $form->options['custom_style'] ) ) ? $form->options['custom_style'] : $style;
642
643
		return $style;
644
    }
645
646
	/**
647
	 * Display the validation error messages when an entry is submitted
648
	 *
649
	 * @param array $args - includes img, errors
650
	 * @since 2.0.6
651
	 */
652
	public static function show_errors( $args ) {
653
		$invalid_msg = self::get_invalid_error_message( $args );
654
655
		if ( empty( $invalid_msg ) ) {
656
			$show_img = false;
657
		} else {
658
			echo wp_kses_post( $invalid_msg );
659
			$show_img = true;
660
		}
661
662
		self::show_error(
663
			array(
664
				'img'      => $args['img'],
665
				'errors'   => $args['errors'],
666
				'show_img' => $show_img,
667
			)
668
		);
669
	}
670
671
	/**
672
	 * Display the error message in the front-end along with the image if set
673
	 * The image was removed from the styling settings, but it may still be set with a hook
674
	 * If the message in the global settings is empty, show every validation message in the error box
675
	 *
676
	 * @param array $args - includes img, errors, and show_img
677
	 * @since 2.0.6
678
	 */
679
	public static function show_error( $args ) {
680
		// remove any blank messages
681
		$args['errors'] = array_filter( (array) $args['errors'] );
682
683
		$line_break_first = $args['show_img'];
684
		foreach ( $args['errors'] as $error_key => $error ) {
0 ignored issues
show
Bug introduced by
The expression $args['errors'] of type array|boolean is not guaranteed to be traversable. How about adding an additional type check?

There are different options of fixing this problem.

  1. If you want to be on the safe side, you can add an additional type-check:

    $collection = json_decode($data, true);
    if ( ! is_array($collection)) {
        throw new \RuntimeException('$collection must be an array.');
    }
    
    foreach ($collection as $item) { /** ... */ }
    
  2. If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:

    /** @var array $collection */
    $collection = json_decode($data, true);
    
    foreach ($collection as $item) { /** .. */ }
    
  3. Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.

Loading history...
685
			if ( $line_break_first && ! is_numeric( $error_key ) && ( $error_key == 'cptch_number' || strpos( $error_key, 'field' ) === 0 ) ) {
686
				continue;
687
			}
688
689
			if ( $line_break_first ) {
690
				echo '<br/>';
691
			}
692
693
			if ( $args['show_img'] && ! empty( $args['img'] ) ) {
694
				echo '<img src="' . esc_attr( $args['img'] ) . '" alt="" />';
695
			} else {
696
				$args['show_img'] = true;
697
			}
698
699
			echo wp_kses_post( $error );
700
701
			if ( ! $line_break_first ) {
702
				echo '<br/>';
703
			}
704
		}
705
	}
706
707
	public static function maybe_get_scroll_js( $id ) {
708
		$offset = apply_filters( 'frm_scroll_offset', 4, array( 'form_id' => $id ) );
709
		if ( $offset != -1 ) {
710
			self::get_scroll_js( $id );
711
		}
712
	}
713
714
	public static function get_scroll_js( $form_id ) {
715
        echo '<script type="text/javascript">document.addEventListener(\'DOMContentLoaded\',function(){frmFrontForm.scrollMsg(' . (int) $form_id . ');})</script>';
716
    }
717
718
	/**
719
	 * @since 3.0
720
	 */
721
	public static function actions_dropdown( $atts ) {
722
		if ( FrmAppHelper::is_admin_page( 'formidable' ) ) {
723
			$status = $atts['status'];
724
			$form_id = isset( $atts['id'] ) ? $atts['id'] : FrmAppHelper::get_param( 'id', 0, 'get', 'absint' );
725
			$trash_link = self::delete_trash_info( $form_id, $status );
726
			$links = self::get_action_links( $form_id, $status );
727
			include( FrmAppHelper::plugin_path() . '/classes/views/frm-forms/actions-dropdown.php' );
728
		}
729
	}
730
731
	/**
732
	 * @since 3.0
733
	 */
734
	public static function get_action_links( $form_id, $form ) {
735
		if ( ! is_object( $form ) ) {
736
			$form = FrmForm::getOne( $form_id );
737
		}
738
739
		$actions = array();
740
		$trash_links = self::delete_trash_links( $form_id );
741
		if ( 'trash' == $form->status ) {
742
			$actions['restore'] = $trash_links['restore'];
743
744
			if ( current_user_can( 'frm_delete_forms' ) ) {
745
				$actions['trash'] = $trash_links['delete'];
746
			}
747
		} elseif ( current_user_can( 'frm_edit_forms' ) ) {
748
			$duplicate_link = '?page=formidable&frm_action=duplicate&id=' . $form_id;
749
			if ( $form->is_template ) {
750
				$actions['frm_duplicate'] = array(
751
					'url'   => wp_nonce_url( $duplicate_link ),
752
					'label' => __( 'Create Form from Template', 'formidable' ),
753
					'icon'  => 'frm_icon_font frm_duplicate_icon',
754
				);
755
			} else {
756
				$actions['duplicate'] = array(
757
					'url'   => wp_nonce_url( $duplicate_link ),
758
					'label' => __( 'Duplicate Form', 'formidable' ),
759
					'icon'  => 'frm_icon_font frm_duplicate_icon',
760
				);
761
			}
762
763
			$actions['trash'] = self::delete_trash_info( $form_id, $form->status );
764
		}
765
766
		return $actions;
767
	}
768
769
	public static function edit_form_link( $form_id ) {
770
		if ( is_object( $form_id ) ) {
771
            $form = $form_id;
772
            $name = $form->name;
773
            $form_id = $form->id;
774
        } else {
775
			$name = FrmForm::getName( $form_id );
776
        }
777
778
        if ( $form_id ) {
779
			$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>';
780
	    } else {
781
	        $val = '';
782
	    }
783
784
	    return $val;
785
	}
786
787
	public static function delete_trash_link( $id, $status, $length = 'label' ) {
788
		$link_details = self::delete_trash_info( $id, $status );
789
790
		return self::format_link_html( $link_details, $length );
791
	}
792
793
	/**
794
	 * @since 3.0
795
	 */
796
	public static function format_link_html( $link_details, $length = 'label' ) {
797
		$link = '';
798
		if ( ! empty( $link_details ) ) {
799
			$link = '<a href="' . esc_url( $link_details['url'] ) . '"';
800
			if ( isset( $link_details['data'] ) ) {
801
				foreach ( $link_details['data'] as $data => $value ) {
802
					$link .= ' data-' . esc_attr( $data ) . '="' . esc_attr( $value ) . '"';
803
				}
804
			} elseif ( isset( $link_details['confirm'] ) ) {
805
				$link .= ' onclick="return confirm(\'' . esc_attr( $link_details['confirm'] ) . '\')"';
806
			}
807
			$label = ( isset( $link_details[ $length ] ) ? $link_details[ $length ] : $link_details['label'] );
808
			$link .= '>' . $label . '</a>';
809
		}
810
		return $link;
811
	}
812
813
	/**
814
	 * @since 3.0
815
	 */
816
	public static function delete_trash_info( $id, $status ) {
817
		$labels = self::delete_trash_links( $id );
818
819
		if ( 'trash' == $status ) {
820
			$info = $labels['restore'];
821
		} elseif ( current_user_can( 'frm_delete_forms' ) ) {
822
			if ( EMPTY_TRASH_DAYS ) {
823
				$info = $labels['trash'];
824
			} else {
825
				$info = $labels['delete'];
826
			}
827
		} else {
828
			$info = array();
829
		}
830
831
		return $info;
832
	}
833
834
	/**
835
	 * @since 3.0
836
	 */
837
	private static function delete_trash_links( $id ) {
838
		$current_page = FrmAppHelper::get_simple_request( array( 'param' => 'form_type' ) );
839
		$base_url = '?page=formidable&form_type=' . $current_page . '&id=' . $id;
840
841
		return array(
842
			'restore' => array(
843
				'label' => __( 'Restore from Trash', 'formidable' ),
844
				'short' => __( 'Restore', 'formidable' ),
845
				'url'   => wp_nonce_url( $base_url . '&frm_action=untrash', 'untrash_form_' . absint( $id ) ),
846
			),
847
			'trash' => array(
848
				'label' => __( 'Move Form to Trash', 'formidable' ),
849
				'short' => __( 'Trash', 'formidable' ),
850
				'url'   => wp_nonce_url( $base_url . '&frm_action=trash', 'trash_form_' . absint( $id ) ),
851
				'icon'  => 'frm_icon_font frm_delete_icon',
852
				'data'  => array( 'frmverify' => __( 'Are you sure?', 'formidable' ) ),
853
			),
854
			'delete' => array(
855
				'label' => __( 'Delete Permanently', 'formidable' ),
856
				'short' => __( 'Delete', 'formidable' ),
857
				'url'   => wp_nonce_url( $base_url . '&frm_action=destroy', 'destroy_form_' . absint( $id ) ),
858
				'confirm' => __( 'Are you sure you want to delete this form and all its entries?', 'formidable' ),
859
				'icon'  => 'frm_icon_font frm_delete_icon',
860
				'data'  => array( 'frmverify' => __( 'Delete form & entries?', 'formidable' ) ),
861
			),
862
		);
863
	}
864
865
	/**
866
	 * @since 3.0
867
	 */
868
	public static function css_classes() {
869
		$classes = array(
870
			'frm_first'      => array(
871
				'label'      => __( 'First', 'formidable' ),
872
				'title'      => __( 'Add this to the first field in each row along with a width. ie frm_first frm4', 'formidable' ),
873
			),
874
			'frm_alignright' => __( 'Right', 'formidable' ),
875
			'frm_total'      => array(
876
				'label'      => __( 'Total', 'formidable' ),
877
				'title'      => __( 'Add this to a read-only field to display the text in bold without a border or background.', 'formidable' ),
878
			),
879
			'frm_grid_first' => __( 'First Grid Row', 'formidable' ),
880
			'frm_grid'       => __( 'Even Grid Row', 'formidable' ),
881
			'frm_grid_odd'   => __( 'Odd Grid Row', 'formidable' ),
882
			'frm_two_col'    => array(
883
				'label'      => __( '2 Col Options', 'formidable' ),
884
				'title'      => __( 'Put your radio button or checkbox options into two columns.', 'formidable' ),
885
			),
886
			'frm_three_col'  => array(
887
				'label'      => __( '3 Col Options', 'formidable' ),
888
				'title'      => __( 'Put your radio button or checkbox options into three columns.', 'formidable' ),
889
			),
890
			'frm_four_col'   => array(
891
				'label'      => __( '4 Col Options', 'formidable' ),
892
				'title'      => __( 'Put your radio button or checkbox options into four columns.', 'formidable' ),
893
			),
894
			'frm_scroll_box' => array(
895
				'label'      => __( 'Scroll Box', 'formidable' ),
896
				'title'      => __( 'If you have many checkbox or radio button options, you may add this class to allow your user to easily scroll through the options.', 'formidable' ),
897
			),
898
			'frm_capitalize' => array(
899
				'label'      => __( 'Capitalize', 'formidable' ),
900
				'title'      => __( 'Automatically capitalize the first letter in each word.', 'formidable' ),
901
			),
902
		);
903
904
		return apply_filters( 'frm_layout_classes', $classes );
905
	}
906
907
	public static function grid_classes() {
908
		$base = array(
909
			'frm_half'          => '1/2',
910
911
			'frm_third'         => '1/3',
912
			'frm_two_thirds'    => '2/3',
913
914
			'frm_fourth'        => '1/4',
915
			'frm_three_fourths' => '3/4',
916
		);
917
918
		$frm_settings = FrmAppHelper::get_settings();
919
		if ( $frm_settings->old_css ) {
920
			$classes = array(
921
				'frm_fifth'        => '1/5',
922
				'frm_four_fifths'  => '4/5',
923
924
				'frm_two_fifths'   => '2/5',
925
				'frm_three_fifths' => '3/5',
926
927
				'frm_sixth'         => '1/6',
928
929
				'frm_full'          => '100%',
930
			);
931
		} else {
932
			$classes = array(
933
				'frm_sixth'         => '1/6',
934
				'frm10'             => '5/6',
935
936
				'frm11'             => '11/12',
937
				'frm1'              => '1/12',
938
939
				'frm5'              => '5/12',
940
				'frm7'              => '7/12',
941
942
				'frm12'             => '100%',
943
			);
944
		}
945
946
		return array_merge( $base, $classes );
947
	}
948
949
	/**
950
	 * @since 3.0
951
	 */
952
	public static function style_class_label( $style, $class ) {
953
		$label = '';
954
		if ( empty( $style ) ) {
955
			$label = $class;
956
		} elseif ( ! is_array( $style ) ) {
957
			$label = $style;
958
		} else if ( isset( $style['label'] ) ) {
959
			$label = $style['label'];
960
		}
961
		return $label;
962
	}
963
964
	public static function status_nice_name( $status ) {
965
        $nice_names = array(
966
            'draft'     => __( 'Draft', 'formidable' ),
967
            'trash'     => __( 'Trash', 'formidable' ),
968
            'publish'   => __( 'Published', 'formidable' ),
969
        );
970
971
		if ( ! in_array( $status, array_keys( $nice_names ) ) ) {
972
            $status = 'publish';
973
        }
974
975
		$name = $nice_names[ $status ];
976
977
        return $name;
978
    }
979
}
980