Completed
Push — master ( c647d3...c021d3 )
by Stephanie
04:11
created

FrmFieldsHelper::maybe_get_field()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 3

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 4
rs 10
cc 1
eloc 3
nc 1
nop 1
1
<?php
2
if ( ! defined('ABSPATH') ) {
3
	die( 'You are not allowed to call this page directly.' );
4
}
5
6
class FrmFieldsHelper {
7
8
	public static function setup_new_vars( $type = '', $form_id = '' ) {
9
10
        if ( strpos($type, '|') ) {
11
            list($type, $setting) = explode('|', $type);
12
        }
13
14
        $defaults = self::get_default_field_opts($type, $form_id);
15
        $defaults['field_options']['custom_html'] = self::get_default_html($type);
16
17
        $values = array();
18
19
        foreach ( $defaults as $var => $default ) {
20
            if ( $var == 'field_options' ) {
21
                $values['field_options'] = array();
22
                foreach ( $default as $opt_var => $opt_default ) {
23
                    $values['field_options'][ $opt_var ] = $opt_default;
24
                    unset($opt_var, $opt_default);
25
                }
26
            } else {
27
                $values[ $var ] = $default;
28
            }
29
            unset($var, $default);
30
        }
31
32
        if ( isset( $setting ) && ! empty( $setting ) ) {
33
            if ( 'data' == $type ) {
34
                $values['field_options']['data_type'] = $setting;
35
            } else {
36
                $values['field_options'][ $setting ] = 1;
37
            }
38
        }
39
40
        if ( $type == 'radio' || $type == 'checkbox' ) {
41
            $values['options'] = serialize( array(
42
                __( 'Option 1', 'formidable' ),
43
                __( 'Option 2', 'formidable' ),
44
            ) );
45
        } else if ( $type == 'select' ) {
46
            $values['options'] = serialize( array(
47
                '', __( 'Option 1', 'formidable' ),
48
            ) );
49
        } else if ( $type == 'textarea' ) {
50
            $values['field_options']['max'] = '5';
51
        } else if ( $type == 'captcha' ) {
52
            $frm_settings = FrmAppHelper::get_settings();
53
            $values['invalid'] = $frm_settings->re_msg;
54
        } else if ( 'url' == $type ) {
55
            $values['name'] = __( 'Website', 'formidable' );
56
        }
57
58
		$fields = FrmField::field_selection();
59
        $fields = array_merge($fields, FrmField::pro_field_selection());
60
61
        if ( isset( $fields[ $type ] ) ) {
62
            $values['name'] = is_array( $fields[ $type ] ) ? $fields[ $type ]['name'] : $fields[ $type ];
63
        }
64
65
        unset($fields);
66
67
        return $values;
68
    }
69
70
	public static function get_html_id( $field, $plus = '' ) {
71
        return apply_filters('frm_field_html_id', 'field_'. $field['field_key'] . $plus, $field);
72
    }
73
74
    public static function setup_edit_vars( $record, $doing_ajax = false ) {
75
		$values = array( 'id' => $record->id, 'form_id' => $record->form_id );
76
		$defaults = array(
77
			'name'          => $record->name,
78
			'description'   => $record->description,
79
			'field_key'     => $record->field_key,
80
			'type'          => $record->type,
81
			'default_value' => $record->default_value,
82
			'field_order'   => $record->field_order,
83
			'required'      => $record->required,
84
		);
85
86
		if ( $doing_ajax ) {
87
            $values = $values + $defaults;
88
            $values['form_name'] = '';
89
		} else {
90
			foreach ( $defaults as $var => $default ) {
91
                $values[ $var ] = FrmAppHelper::get_param( $var, $default, 'get', 'htmlspecialchars' );
92
                unset($var, $default);
93
            }
94
95
			$values['form_name'] = $record->form_id ? FrmForm::getName( $record->form_id ) : '';
96
        }
97
98
		unset( $defaults );
99
100
        $values['options'] = $record->options;
101
        $values['field_options'] = $record->field_options;
102
103
        $defaults = self::get_default_field_opts($values['type'], $record, true);
104
105
		if ( $values['type'] == 'captcha' ) {
106
            $frm_settings = FrmAppHelper::get_settings();
107
            $defaults['invalid'] = $frm_settings->re_msg;
108
        }
109
110
		foreach ( $defaults as $opt => $default ) {
111
            $values[ $opt ] = isset( $record->field_options[ $opt ] ) ? $record->field_options[ $opt ] : $default;
112
            unset($opt, $default);
113
        }
114
115
        $values['custom_html'] = (isset($record->field_options['custom_html'])) ? $record->field_options['custom_html'] : self::get_default_html($record->type);
116
117
		return apply_filters( 'frm_setup_edit_field_vars', $values, array( 'doing_ajax' => $doing_ajax ) );
118
    }
119
120
    public static function get_default_field_opts( $type, $field, $limit = false ) {
121
        $field_options = array(
122
            'size' => '', 'max' => '', 'label' => '', 'blank' => '',
123
            'required_indicator' => '*', 'invalid' => '', 'separate_value' => 0,
124
            'clear_on_focus' => 0, 'default_blank' => 0, 'classes' => '',
125
			'custom_html' => '', 'captcha_size' => 'default',
126
        );
127
128
		if ( $limit ) {
129
            return $field_options;
130
		}
131
132
        global $wpdb;
133
134
        $form_id = (is_numeric($field)) ? $field : $field->form_id;
135
136
        $key = is_numeric($field) ? FrmAppHelper::get_unique_key('', $wpdb->prefix .'frm_fields', 'field_key') : $field->field_key;
137
138
        $field_count = FrmDb::get_var( 'frm_fields', array( 'form_id' => $form_id ), 'field_order', array( 'order_by' => 'field_order DESC' ) );
139
140
        $frm_settings = FrmAppHelper::get_settings();
141
        return array(
142
            'name' => __( 'Untitled', 'formidable' ), 'description' => '',
143
			'field_key' => $key, 'type' => $type, 'options' => '', 'default_value' => '',
144
			'field_order' => $field_count + 1, 'required' => false,
145
            'blank' => $frm_settings->blank_msg, 'unique_msg' => $frm_settings->unique_msg,
146
            'invalid' => __( 'This field is invalid', 'formidable' ), 'form_id' => $form_id,
147
			'field_options' => $field_options,
148
        );
149
    }
150
151
    public static function fill_field( &$values, $field, $form_id, $new_key = '' ) {
152
        global $wpdb;
153
154
        $values['field_key'] = FrmAppHelper::get_unique_key($new_key, $wpdb->prefix .'frm_fields', 'field_key');
155
        $values['form_id'] = $form_id;
156
        $values['options'] = maybe_serialize($field->options);
157
        $values['default_value'] = maybe_serialize($field->default_value);
158
159
        foreach ( array( 'name', 'description', 'type', 'field_order', 'field_options', 'required' ) as $col ) {
160
            $values[ $col ] = $field->{$col};
161
        }
162
    }
163
164
    /**
165
     * @since 2.0
166
     */
167
	public static function get_error_msg( $field, $error ) {
168
		$frm_settings = FrmAppHelper::get_settings();
169
		$default_settings = $frm_settings->default_options();
170
		$field_name = is_array( $field ) ? $field['name'] : $field->name;
171
172
		$defaults = array(
173
			'unique_msg' => array( 'full' => $default_settings['unique_msg'], 'part' => sprintf( __('%s must be unique', 'formidable' ), $field_name ) ),
174
			'invalid'   => array( 'full' => __( 'This field is invalid', 'formidable' ), 'part' => sprintf( __('%s is invalid', 'formidable' ), $field_name ) ),
175
			'blank'     => array( 'full' => $frm_settings->blank_msg, 'part' => $frm_settings->blank_msg ),
176
		);
177
178
		$msg = FrmField::get_option( $field, $error );
0 ignored issues
show
Bug introduced by
The method get_option() does not seem to exist on object<FrmField>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
179
		$msg = ( $msg == $defaults[ $error ]['full'] || empty( $msg ) ) ? $defaults[ $error ]['part'] : $msg;
180
		return $msg;
181
	}
182
183
    public static function get_form_fields( $form_id, $error = false ) {
184
        $fields = FrmField::get_all_for_form($form_id);
185
        $fields = apply_filters('frm_get_paged_fields', $fields, $form_id, $error);
186
        return $fields;
187
    }
188
189
	public static function get_default_html( $type = 'text' ) {
190
		if ( apply_filters( 'frm_normal_field_type_html', true, $type ) ) {
191
			$input = ( in_array( $type, array( 'radio', 'checkbox', 'data' ) ) ) ? '<div class="frm_opt_container">[input]</div>' : '[input]';
192
            $for = '';
193
			if ( ! in_array( $type, array( 'radio', 'checkbox', 'data', 'scale' ) ) ) {
194
                $for = 'for="field_[key]"';
195
            }
196
197
            $default_html = <<<DEFAULT_HTML
198
<div id="frm_field_[id]_container" class="frm_form_field form-field [required_class][error_class]">
199
    <label $for class="frm_primary_label">[field_name]
200
        <span class="frm_required">[required_label]</span>
201
    </label>
202
    $input
203
    [if description]<div class="frm_description">[description]</div>[/if description]
204
    [if error]<div class="frm_error">[error]</div>[/if error]
205
</div>
206
DEFAULT_HTML;
207
        } else {
208
			$default_html = apply_filters('frm_other_custom_html', '', $type);
209
        }
210
211
        return apply_filters('frm_custom_html', $default_html, $type);
212
    }
213
214
	public static function replace_shortcodes( $html, $field, $errors = array(), $form = false, $args = array() ) {
215
        $html = apply_filters('frm_before_replace_shortcodes', $html, $field, $errors, $form);
216
217
        $defaults = array(
218
            'field_name'  => 'item_meta['. $field['id'] .']',
219
            'field_id'    => $field['id'],
220
            'field_plus_id' => '',
221
            'section_id'    => '',
222
        );
223
        $args = wp_parse_args($args, $defaults);
224
        $field_name = $args['field_name'];
225
        $field_id = $args['field_id'];
226
        $html_id = self::get_html_id($field, $args['field_plus_id']);
227
228
        if ( FrmField::is_multiple_select($field) ) {
229
            $field_name .= '[]';
230
        }
231
232
        //replace [id]
233
        $html = str_replace('[id]', $field_id, $html);
234
235
        // Remove the for attribute for captcha
236
        if ( $field['type'] == 'captcha' ) {
237
            $html = str_replace(' for="field_[key]"', '', $html);
238
        }
239
240
        // set the label for
241
        $html = str_replace('field_[key]', $html_id, $html);
242
243
        //replace [key]
244
        $html = str_replace('[key]', $field['field_key'], $html);
245
246
        //replace [description] and [required_label] and [error]
247
		$required = FrmField::is_required( $field ) ? $field['required_indicator'] : '';
248
        if ( ! is_array( $errors ) ) {
249
            $errors = array();
250
        }
251
		$error = isset( $errors[ 'field' . $field_id ] ) ? $errors[ 'field' . $field_id ] : false;
252
253
        //If field type is section heading, add class so a bottom margin can be added to either the h3 or description
254
        if ( $field['type'] == 'divider' ) {
255
            if ( FrmField::is_option_true( $field, 'description' ) ) {
256
                $html = str_replace( 'frm_description', 'frm_description frm_section_spacing', $html );
257
            } else {
258
                $html = str_replace('[label_position]', '[label_position] frm_section_spacing', $html);
259
            }
260
        }
261
262
		foreach ( array( 'description' => $field['description'], 'required_label' => $required, 'error' => $error ) as $code => $value ) {
263
            self::remove_inline_conditions( ( $value && $value != '' ), $code, $value, $html );
264
        }
265
266
        //replace [required_class]
267
		$required_class = FrmField::is_required( $field ) ? ' frm_required_field' : '';
268
        $html = str_replace('[required_class]', $required_class, $html);
269
270
        //replace [label_position]
271
        $field['label'] = apply_filters('frm_html_label_position', $field['label'], $field, $form);
272
        $field['label'] = ( $field['label'] && $field['label'] != '' ) ? $field['label'] : 'top';
273
		$html = str_replace( '[label_position]', ( ( in_array( $field['type'], array( 'divider', 'end_divider', 'break' ) ) ) ? $field['label'] : ' frm_primary_label' ), $html );
274
275
        //replace [field_name]
276
        $html = str_replace('[field_name]', $field['name'], $html);
277
278
        //replace [error_class]
279
		$error_class = isset( $errors[ 'field' . $field_id ] ) ? ' frm_blank_field' : '';
280
		self::get_more_field_classes( $error_class, $field, $field_id, $html );
281
		if ( $field['type'] == 'html' && strpos( $html, '[error_class]' ) === false ) {
282
			// there is no error_class shortcode to use for addign fields
283
			$html = str_replace( 'class="frm_form_field', 'class="frm_form_field ' . $error_class, $html );
284
		}
285
        $html = str_replace('[error_class]', $error_class, $html);
286
287
        //replace [entry_key]
288
        $entry_key = FrmAppHelper::simple_get( 'entry', 'sanitize_title' );
289
        $html = str_replace('[entry_key]', $entry_key, $html);
290
291
        //replace [input]
292
        preg_match_all("/\[(input|deletelink)\b(.*?)(?:(\/))?\]/s", $html, $shortcodes, PREG_PATTERN_ORDER);
293
        global $frm_vars;
294
        $frm_settings = FrmAppHelper::get_settings();
295
296
        foreach ( $shortcodes[0] as $short_key => $tag ) {
297
            $atts = shortcode_parse_atts( $shortcodes[2][ $short_key ] );
298
			$tag = self::get_shortcode_tag( $shortcodes, $short_key, array( 'conditional' => false, 'conditional_check' => false ) );
299
300
            $replace_with = '';
301
302
            if ( $tag == 'input' ) {
303
                if ( isset($atts['opt']) ) {
304
                    $atts['opt']--;
305
                }
306
307
                $field['input_class'] = isset($atts['class']) ? $atts['class'] : '';
308
                if ( isset($atts['class']) ) {
309
                    unset($atts['class']);
310
                }
311
312
                $field['shortcodes'] = $atts;
313
                ob_start();
314
                include(FrmAppHelper::plugin_path() .'/classes/views/frm-fields/input.php');
315
                $replace_with = ob_get_contents();
316
                ob_end_clean();
317
            } else if ( $tag == 'deletelink' && FrmAppHelper::pro_is_installed() ) {
318
                $replace_with = FrmProEntriesController::entry_delete_link($atts);
319
            }
320
321
            $html = str_replace( $shortcodes[0][ $short_key ], $replace_with, $html );
322
        }
323
324
		if ( $form ) {
325
            $form = (array) $form;
326
327
            //replace [form_key]
328
            $html = str_replace('[form_key]', $form['form_key'], $html);
329
330
            //replace [form_name]
331
            $html = str_replace('[form_name]', $form['name'], $html);
332
        }
333
        $html .= "\n";
334
335
        //Return html if conf_field to prevent loop
336
        if ( isset($field['conf_field']) && $field['conf_field'] == 'stop' ) {
337
            return $html;
338
        }
339
340
        //If field is in repeating section
341
        if ( $args['section_id'] ) {
342
            $html = apply_filters('frm_replace_shortcodes', $html, $field, array( 'errors' => $errors, 'form' => $form, 'field_name' => $field_name, 'field_id' => $field_id, 'field_plus_id' => $args['field_plus_id'], 'section_id' => $args['section_id'] ));
343
        } else {
344
            $html = apply_filters('frm_replace_shortcodes', $html, $field, array( 'errors' => $errors, 'form' => $form ));
345
        }
346
347
		self::remove_collapse_shortcode( $html );
348
349
		if ( apply_filters( 'frm_do_html_shortcodes', true ) ) {
350
			$html = do_shortcode( $html );
351
		}
352
353
        return $html;
354
    }
355
356
	/**
357
	* Add more classes to certain fields (like confirmation fields, other fields, repeating fields, etc.)
358
	*
359
	* @since 2.0
360
	* @param $error_class string, pass by reference
361
	* @param $field array
362
	* @param $field_id int
363
	* @param $html string
364
	*/
365
	private static function get_more_field_classes( &$error_class, $field, $field_id, $html ) {
366
		$error_class .= ' frm_'. $field['label'] .'_container';
367
		if ( $field['id'] != $field_id ) {
368
			// add a class for repeating/embedded fields
369
			$error_class .= ' frm_field_'. $field['id'] .'_container';
370
		}
371
372
		// Add class to embedded form field
373
		if ( $field['type'] == 'form' ) {
374
			$error_class .= ' frm_embed_form_container';
375
		}
376
377
		//Add classes to inline confirmation field (if it doesn't already have classes set)
378
		if ( isset( $field['conf_field'] ) && $field['conf_field'] == 'inline' && ! $field['classes'] ) {
379
			$error_class .= ' frm_first frm_half';
380
		}
381
382
		//Add class if field includes other option
383
		if ( isset( $field['other'] ) && true == $field['other'] ) {
384
			$error_class .= ' frm_other_container';
385
		}
386
387
		// Add class to Dynamic fields
388
		if ( $field['type'] == 'data' ) {
389
			$error_class .= ' frm_dynamic_' . $field['data_type'] . '_container';
390
		}
391
392
		// Add class to inline Scale field
393
		if ( $field['type'] == 'scale' && $field['label'] == 'inline' ) {
394
			$error_class .= ' frm_scale_container';
395
		}
396
397
		// If this is a Section
398
		if ( $field['type'] == 'divider' ) {
399
400
			// If the top margin needs to be removed from a section heading
401
			if ( $field['label'] == 'none' ) {
402
				$error_class .= ' frm_hide_section';
403
			}
404
405
			// If this is a repeating section that should be hidden with exclude_fields or fields shortcode, hide it
406
			if ( $field['repeat'] ) {
407
				global $frm_vars;
408
				if ( isset( $frm_vars['show_fields'] ) && ! empty( $frm_vars['show_fields'] ) && ! in_array( $field['id'], $frm_vars['show_fields'] ) && ! in_array( $field['field_key'], $frm_vars['show_fields'] ) ) {
409
					$error_class .= ' frm_hidden';
410
				}
411
			}
412
		}
413
414
		//insert custom CSS classes
415
		if ( ! empty( $field['classes'] ) ) {
416
			if ( ! strpos( $html, 'frm_form_field ') ) {
417
				$error_class .= ' frm_form_field';
418
			}
419
			$error_class .= ' '. $field['classes'];
420
		}
421
	}
422
423
    public static function remove_inline_conditions( $no_vars, $code, $replace_with, &$html ) {
424
        if ( $no_vars ) {
425
            $html = str_replace( '[if '. $code.']', '', $html );
426
    	    $html = str_replace( '[/if '. $code.']', '', $html );
427
        } else {
428
            $html = preg_replace( '/(\[if\s+'. $code .'\])(.*?)(\[\/if\s+'. $code .'\])/mis', '', $html );
429
        }
430
431
        $html = str_replace( '['. $code .']', $replace_with, $html );
432
    }
433
434
	public static function get_shortcode_tag( $shortcodes, $short_key, $args ) {
435
		$args = wp_parse_args( $args, array( 'conditional' => false, 'conditional_check' => false, 'foreach' => false ) );
436
        if ( ( $args['conditional'] || $args['foreach'] ) && ! $args['conditional_check'] ) {
437
            $args['conditional_check'] = true;
438
        }
439
440
        $prefix = '';
441
        if ( $args['conditional_check'] ) {
442
            if ( $args['conditional'] ) {
443
                $prefix = 'if ';
444
            } else if ( $args['foreach'] ) {
445
                $prefix = 'foreach ';
446
            }
447
        }
448
449
        $with_tags = $args['conditional_check'] ? 3 : 2;
450
        if ( ! empty( $shortcodes[ $with_tags ][ $short_key ] ) ) {
451
            $tag = str_replace( '[' . $prefix, '', $shortcodes[0][ $short_key ] );
452
            $tag = str_replace(']', '', $tag);
453
            $tags = explode(' ', $tag);
454
            if ( is_array($tags) ) {
455
                $tag = $tags[0];
456
            }
457
        } else {
458
            $tag = $shortcodes[ $with_tags - 1 ][ $short_key ];
459
        }
460
461
        return $tag;
462
    }
463
464
	/**
465
	 * Remove [collapse_this] if it's still included after all processing
466
	 * @since 2.0.8
467
	 */
468
	private static function remove_collapse_shortcode( &$html ) {
469
		if ( preg_match( '/\[(collapse_this)\]/s', $html ) ) {
470
			$html = str_replace( '[collapse_this]', '', $html );
471
		}
472
	}
473
474
	public static function display_recaptcha( $field ) {
475
        $frm_settings = FrmAppHelper::get_settings();
476
        $lang = apply_filters('frm_recaptcha_lang', $frm_settings->re_lang, $field);
477
478
        $api_js_url = 'https://www.google.com/recaptcha/api.js';
479
        if ( $lang != 'en' ) {
480
            $api_js_url .= '?hl='. $lang;
481
        }
482
        $api_js_url = apply_filters('frm_recpatcha_js_url', $api_js_url);
483
484
        wp_register_script('recaptcha-api', $api_js_url, '', true);
485
        wp_enqueue_script('recaptcha-api');
486
487
?>
488
<div id="field_<?php echo esc_attr( $field['field_key'] ) ?>" class="g-recaptcha" data-sitekey="<?php echo esc_attr( $frm_settings->pubkey ) ?>" data-size="<?php echo esc_attr( $field['captcha_size'] ) ?>"></div>
489
<?php
490
    }
491
492
	public static function show_single_option( $field ) {
493
        $field_name = $field['name'];
494
        $html_id = self::get_html_id($field);
495
        foreach ( $field['options'] as $opt_key => $opt ) {
496
            $field_val = apply_filters('frm_field_value_saved', $opt, $opt_key, $field);
497
            $opt = apply_filters('frm_field_label_seen', $opt, $opt_key, $field);
498
499
            // If this is an "Other" option, get the HTML for it
500
			if ( self::is_other_opt( $opt_key ) ) {
501
                // Get string for Other text field, if needed
502
				$other_val = self::get_other_val( compact( 'opt_key', 'field' ) );
503
                require(FrmAppHelper::plugin_path() .'/pro/classes/views/frmpro-fields/other-option.php');
504
            } else {
505
                require(FrmAppHelper::plugin_path() .'/classes/views/frm-fields/single-option.php');
506
            }
507
        }
508
    }
509
510
	public static function dropdown_categories( $args ) {
511
		$defaults = array( 'field' => false, 'name' => false, 'show_option_all' => ' ' );
512
        $args = wp_parse_args($args, $defaults);
513
514
        if ( ! $args['field'] ) {
515
            return;
516
        }
517
518
        if ( ! $args['name'] ) {
519
            $args['name'] = 'item_meta['. $args['field']['id'] .']';
520
        }
521
522
        $id = self::get_html_id($args['field']);
523
        $class = $args['field']['type'];
524
525
        $exclude = (is_array($args['field']['exclude_cat'])) ? implode(',', $args['field']['exclude_cat']) : $args['field']['exclude_cat'];
526
        $exclude = apply_filters('frm_exclude_cats', $exclude, $args['field']);
527
528
        if ( is_array($args['field']['value']) ) {
529
            if ( ! empty($exclude) ) {
530
                $args['field']['value'] = array_diff($args['field']['value'], explode(',', $exclude));
531
            }
532
            $selected = reset($args['field']['value']);
533
        } else {
534
            $selected = $args['field']['value'];
535
        }
536
537
        $tax_atts = array(
538
            'show_option_all' => $args['show_option_all'], 'hierarchical' => 1, 'name' => $args['name'],
539
            'id' => $id, 'exclude' => $exclude, 'class' => $class, 'selected' => $selected,
540
            'hide_empty' => false, 'echo' => 0, 'orderby' => 'name',
541
        );
542
543
        $tax_atts = apply_filters('frm_dropdown_cat', $tax_atts, $args['field']);
544
545
        if ( FrmAppHelper::pro_is_installed() ) {
546
            $post_type = FrmProFormsHelper::post_type($args['field']['form_id']);
547
            $tax_atts['taxonomy'] = FrmProAppHelper::get_custom_taxonomy($post_type, $args['field']);
548
            if ( ! $tax_atts['taxonomy'] ) {
549
                return;
550
            }
551
552
            // If field type is dropdown (not Dynamic), exclude children when parent is excluded
553
            if ( $args['field']['type'] != 'data' && is_taxonomy_hierarchical($tax_atts['taxonomy']) ) {
554
                $tax_atts['exclude_tree'] = $exclude;
555
            }
556
        }
557
558
        $dropdown = wp_dropdown_categories($tax_atts);
559
560
        $add_html = FrmFieldsController::input_html($args['field'], false);
561
562
        if ( FrmAppHelper::pro_is_installed() ) {
563
            $add_html .= FrmProFieldsController::input_html($args['field'], false);
564
        }
565
566
		$dropdown = str_replace( "<select name='" . esc_attr( $args['name'] ) ."' id='" . esc_attr( $id ) . "' class='" . esc_attr( $class ) . "'", "<select name='" . esc_attr( $args['name'] ) . "' id='" . esc_attr( $id ) . "' " . $add_html, $dropdown );
567
568
        if ( is_array($args['field']['value']) ) {
569
            $skip = true;
570
            foreach ( $args['field']['value'] as $v ) {
571
				if ( $skip ) {
572
                    $skip = false;
573
                    continue;
574
                }
575
				$dropdown = str_replace(' value="' . esc_attr( $v ) . '"', ' value="' . esc_attr( $v ) . '" selected="selected"', $dropdown );
576
                unset($v);
577
            }
578
        }
579
580
        return $dropdown;
581
    }
582
583
	public static function get_term_link( $tax_id ) {
584
        $tax = get_taxonomy($tax_id);
585
        if ( ! $tax ) {
586
            return;
587
        }
588
589
        $link = sprintf(
590
            __( 'Please add options from the WordPress "%1$s" page', 'formidable' ),
591
            '<a href="'. esc_url( admin_url( 'edit-tags.php?taxonomy='. $tax->name ) ) .'" target="_blank">'. ( empty($tax->labels->name) ? __( 'Categories' ) : $tax->labels->name ) .'</a>'
0 ignored issues
show
introduced by
Expected next thing to be a escaping function, not '('
Loading history...
592
        );
593
        unset($tax);
594
595
        return $link;
596
    }
597
598
	public static function value_meets_condition( $observed_value, $cond, $hide_opt ) {
599
		// Remove white space from hide_opt
600
		if ( ! is_array( $hide_opt ) ) {
601
			$hide_opt = rtrim( $hide_opt );
602
		}
603
604
        if ( is_array($observed_value) ) {
605
            return self::array_value_condition($observed_value, $cond, $hide_opt);
606
        }
607
608
        $m = false;
609
        if ( $cond == '==' ) {
610
            $m = $observed_value == $hide_opt;
611
        } else if ( $cond == '!=' ) {
612
            $m = $observed_value != $hide_opt;
613
        } else if ( $cond == '>' ) {
614
            $m = $observed_value > $hide_opt;
615
        } else if ( $cond == '<' ) {
616
            $m = $observed_value < $hide_opt;
617
        } else if ( $cond == 'LIKE' || $cond == 'not LIKE' ) {
618
            $m = stripos($observed_value, $hide_opt);
619
            if ( $cond == 'not LIKE' ) {
620
                $m = ( $m === false ) ? true : false;
621
            } else {
622
                $m = ( $m === false ) ? false : true;
623
            }
624
        }
625
        return $m;
626
    }
627
628
	public static function array_value_condition( $observed_value, $cond, $hide_opt ) {
629
        $m = false;
630
        if ( $cond == '==' ) {
631
            if ( is_array($hide_opt) ) {
632
                $m = array_intersect($hide_opt, $observed_value);
633
                $m = empty($m) ? false : true;
634
            } else {
635
                $m = in_array($hide_opt, $observed_value);
636
            }
637
        } else if ( $cond == '!=' ) {
638
            $m = ! in_array($hide_opt, $observed_value);
639
        } else if ( $cond == '>' ) {
640
            $min = min($observed_value);
641
            $m = $min > $hide_opt;
642
        } else if ( $cond == '<' ) {
643
            $max = max($observed_value);
644
            $m = $max < $hide_opt;
645
        } else if ( $cond == 'LIKE' || $cond == 'not LIKE' ) {
646
            foreach ( $observed_value as $ob ) {
647
                $m = strpos($ob, $hide_opt);
648
                if ( $m !== false ) {
649
                    $m = true;
650
                    break;
651
                }
652
            }
653
654
            if ( $cond == 'not LIKE' ) {
655
                $m = ( $m === false ) ? true : false;
656
            }
657
        }
658
659
        return $m;
660
    }
661
662
    /**
663
     * Replace a few basic shortcodes and field ids
664
     * @since 2.0
665
     * @return string
666
     */
667
	public static function basic_replace_shortcodes( $value, $form, $entry ) {
668
        if ( strpos($value, '[sitename]') !== false ) {
669
            $new_value = wp_specialchars_decode( FrmAppHelper::site_name(), ENT_QUOTES );
670
            $value = str_replace('[sitename]', $new_value, $value);
671
        }
672
673
        $value = apply_filters('frm_content', $value, $form, $entry);
674
        $value = do_shortcode($value);
675
676
        return $value;
677
    }
678
679
	public static function get_shortcodes( $content, $form_id ) {
680
        if ( FrmAppHelper::pro_is_installed() ) {
681
            return FrmProDisplaysHelper::get_shortcodes($content, $form_id);
682
        }
683
684
        $fields = FrmField::getAll( array( 'fi.form_id' => (int) $form_id, 'fi.type not' => FrmField::no_save_fields() ) );
685
686
        $tagregexp = self::allowed_shortcodes($fields);
687
688
        preg_match_all("/\[(if )?($tagregexp)\b(.*?)(?:(\/))?\](?:(.+?)\[\/\2\])?/s", $content, $matches, PREG_PATTERN_ORDER);
689
690
        return $matches;
691
    }
692
693
	public static function allowed_shortcodes( $fields = array() ) {
694
        $tagregexp = array(
695
            'editlink', 'id', 'key', 'ip',
696
            'siteurl', 'sitename', 'admin_email',
697
            'post[-|_]id', 'created[-|_]at', 'updated[-|_]at', 'updated[-|_]by',
698
			'parent[-|_]id',
699
        );
700
701
        foreach ( $fields as $field ) {
702
            $tagregexp[] = $field->id;
703
            $tagregexp[] = $field->field_key;
704
        }
705
706
        $tagregexp = implode('|', $tagregexp);
707
        return $tagregexp;
708
    }
709
710
	public static function replace_content_shortcodes( $content, $entry, $shortcodes ) {
711
        $shortcode_values = array(
712
           'id'     => $entry->id,
713
           'key'    => $entry->item_key,
714
           'ip'     => $entry->ip,
715
        );
716
717
        foreach ( $shortcodes[0] as $short_key => $tag ) {
718
            $atts = shortcode_parse_atts( $shortcodes[3][ $short_key ] );
719
720
            if ( ! empty( $shortcodes[3][ $short_key ] ) ) {
721
				$tag = str_replace( array( '[', ']' ), '', $shortcodes[0][ $short_key ] );
722
                $tags = explode(' ', $tag);
723
                if ( is_array($tags) ) {
724
                    $tag = $tags[0];
725
                }
726
            } else {
727
                $tag = $shortcodes[2][ $short_key ];
728
            }
729
730
            switch ( $tag ) {
731
                case 'id':
732
                case 'key':
733
                case 'ip':
734
                    $replace_with = $shortcode_values[ $tag ];
735
                break;
736
737
                case 'user_agent':
738
                case 'user-agent':
739
                    $entry->description = maybe_unserialize($entry->description);
740
					$replace_with = FrmEntryFormat::get_browser( $entry->description['browser'] );
741
                break;
742
743
                case 'created_at':
744
                case 'created-at':
745
                case 'updated_at':
746
                case 'updated-at':
747
                    if ( isset($atts['format']) ) {
748
                        $time_format = ' ';
749
                    } else {
750
                        $atts['format'] = get_option('date_format');
751
                        $time_format = '';
752
                    }
753
754
                    $this_tag = str_replace('-', '_', $tag);
755
                    $replace_with = FrmAppHelper::get_formatted_time($entry->{$this_tag}, $atts['format'], $time_format);
756
                    unset($this_tag);
757
                break;
758
759
                case 'created_by':
760
                case 'created-by':
761
                case 'updated_by':
762
                case 'updated-by':
763
                    $this_tag = str_replace('-', '_', $tag);
764
					$replace_with = self::get_display_value( $entry->{$this_tag}, (object) array( 'type' => 'user_id' ), $atts );
765
                    unset($this_tag);
766
                break;
767
768
                case 'admin_email':
769
                case 'siteurl':
770
                case 'frmurl':
771
                case 'sitename':
772
                case 'get':
773
                    $replace_with = self::dynamic_default_values( $tag, $atts );
774
                break;
775
776
                default:
777
                    $field = FrmField::getOne( $tag );
778
                    if ( ! $field ) {
779
                        break;
780
                    }
781
782
                    $sep = isset($atts['sep']) ? $atts['sep'] : ', ';
783
784
                    $replace_with = FrmEntryMeta::get_meta_value( $entry, $field->id );
785
786
                    $atts['entry_id'] = $entry->id;
787
                    $atts['entry_key'] = $entry->item_key;
788
                    //$replace_with = apply_filters('frmpro_fields_replace_shortcodes', $replace_with, $tag, $atts, $field);
789
790
                    if ( is_array($replace_with) ) {
791
                        $replace_with = implode($sep, $replace_with);
792
                    }
793
794
                    if ( isset($atts['show']) && $atts['show'] == 'field_label' ) {
795
                        $replace_with = $field->name;
796
                    } else if ( isset($atts['show']) && $atts['show'] == 'description' ) {
797
                        $replace_with = $field->description;
798
                    } else if ( empty($replace_with) && $replace_with != '0' ) {
799
                        $replace_with = '';
800
                    } else {
801
                        $replace_with = self::get_display_value($replace_with, $field, $atts);
802
                    }
803
804
                    unset($field);
805
                break;
806
            }
807
808
            if ( isset($replace_with) ) {
809
                $content = str_replace( $shortcodes[0][ $short_key ], $replace_with, $content );
810
            }
811
812
            unset($atts, $conditional, $replace_with);
813
		}
814
815
		return $content;
816
    }
817
818
    /**
819
     * Get the value to replace a few standard shortcodes
820
     *
821
     * @since 2.0
822
     * @return string
823
     */
824
    public static function dynamic_default_values( $tag, $atts = array(), $return_array = false ) {
825
        $new_value = '';
826
        switch ( $tag ) {
827
            case 'admin_email':
828
                $new_value = get_option('admin_email');
829
                break;
830
            case 'siteurl':
831
                $new_value = FrmAppHelper::site_url();
832
                break;
833
            case 'frmurl':
834
                $new_value = FrmAppHelper::plugin_url();
835
                break;
836
            case 'sitename':
837
                $new_value = FrmAppHelper::site_name();
838
                break;
839
            case 'get':
840
                $new_value = self::process_get_shortcode( $atts, $return_array );
841
                break;
842
        }
843
844
        return $new_value;
845
    }
846
847
    /**
848
     * Process the [get] shortcode
849
     *
850
     * @since 2.0
851
     * @return string|array
852
     */
853
    public static function process_get_shortcode( $atts, $return_array = false ) {
854
        if ( ! isset($atts['param']) ) {
855
            return '';
856
        }
857
858
        if ( strpos($atts['param'], '&#91;') ) {
859
            $atts['param'] = str_replace('&#91;', '[', $atts['param']);
860
            $atts['param'] = str_replace('&#93;', ']', $atts['param']);
861
        }
862
863
        $new_value = FrmAppHelper::get_param($atts['param'], '');
864
        $new_value = FrmAppHelper::get_query_var( $new_value, $atts['param'] );
865
866
        if ( $new_value == '' ) {
867
            if ( ! isset($atts['prev_val']) ) {
868
                $atts['prev_val'] = '';
869
            }
870
871
            $new_value = isset($atts['default']) ? $atts['default'] : $atts['prev_val'];
872
        }
873
874
        if ( is_array($new_value) && ! $return_array ) {
875
            $new_value = implode(', ', $new_value);
876
        }
877
878
        return $new_value;
879
    }
880
881
	public static function get_display_value( $replace_with, $field, $atts = array() ) {
882
		$sep = isset( $atts['sep'] ) ? $atts['sep'] : ', ';
883
884
		$replace_with = apply_filters( 'frm_get_display_value', $replace_with, $field, $atts );
885
886
        if ( $field->type == 'textarea' || $field->type == 'rte' ) {
887
            $autop = isset($atts['wpautop']) ? $atts['wpautop'] : true;
888
            if ( apply_filters('frm_use_wpautop', $autop) ) {
889
                if ( is_array($replace_with) ) {
890
                    $replace_with = implode("\n", $replace_with);
891
                }
892
                $replace_with = wpautop($replace_with);
893
            }
894
			unset( $autop );
895
		} else if ( is_array( $replace_with ) ) {
896
			$replace_with = implode( $sep, $replace_with );
897
		}
898
899
		return $replace_with;
900
	}
901
902
	public static function get_field_types( $type ) {
903
        $single_input = array(
904
            'text', 'textarea', 'rte', 'number', 'email', 'url',
905
            'image', 'file', 'date', 'phone', 'hidden', 'time',
906
            'user_id', 'tag', 'password',
907
        );
908
		$multiple_input = array( 'radio', 'checkbox', 'select', 'scale' );
909
		$other_type = array( 'divider', 'html', 'break' );
910
911
		$field_selection = array_merge( FrmField::pro_field_selection(), FrmField::field_selection() );
912
913
        $field_types = array();
914
        if ( in_array($type, $single_input) ) {
915
            self::field_types_for_input( $single_input, $field_selection, $field_types );
916
        } else if ( in_array($type, $multiple_input) ) {
917
            self::field_types_for_input( $multiple_input, $field_selection, $field_types );
918
        } else if ( in_array($type, $other_type) ) {
919
            self::field_types_for_input( $other_type, $field_selection, $field_types );
920
		} else if ( isset( $field_selection[ $type ] ) ) {
921
            $field_types[ $type ] = $field_selection[ $type ];
922
        }
923
924
        return $field_types;
925
    }
926
927
    private static function field_types_for_input( $inputs, $fields, &$field_types ) {
928
        foreach ( $inputs as $input ) {
929
            $field_types[ $input ] = $fields[ $input ];
930
            unset($input);
931
        }
932
    }
933
934
    /**
935
    * Check if current field option is an "other" option
936
    *
937
    * @since 2.0.6
938
    *
939
    * @param string $opt_key
940
    * @return boolean Returns true if current field option is an "Other" option
941
    */
942
    public static function is_other_opt( $opt_key ) {
943
        return $opt_key && strpos( $opt_key, 'other' ) !== false;
944
    }
945
946
    /**
947
    * Get value that belongs in "Other" text box
948
    *
949
    * @since 2.0.6
950
    *
951
    * @param array $args
952
    */
953
    public static function get_other_val( $args ) {
954
		$defaults = array(
955
			'opt_key' => 0, 'field' => array(),
956
			'parent' => false, 'pointer' => false,
957
		);
958
		$args = wp_parse_args( $args, $defaults );
959
960
		$opt_key = $args['opt_key'];
961
		$field = $args['field'];
962
		$parent = $args['parent'];
963
		$pointer = $args['pointer'];
964
		$other_val = '';
965
966
		// If option is an "other" option and there is a value set for this field,
967
		// check if the value belongs in the current "Other" option text field
968
		if ( ! FrmFieldsHelper::is_other_opt( $opt_key ) || ! FrmField::is_option_true( $field, 'value' ) ) {
969
			return $other_val;
970
		}
971
972
		// Check posted vals before checking saved values
973
974
		// For fields inside repeating sections - note, don't check if $pointer is true because it will often be zero
975
		if ( $parent && isset( $_POST['item_meta'][ $parent ][ $pointer ]['other'][ $field['id'] ] ) ) {
976
			if ( FrmField::is_field_with_multiple_values( $field ) ) {
977
				$other_val = isset( $_POST['item_meta'][ $parent ][ $pointer ]['other'][ $field['id'] ][ $opt_key ] ) ? sanitize_text_field( $_POST['item_meta'][ $parent ][ $pointer ]['other'][ $field['id'] ][ $opt_key ] ) : '';
0 ignored issues
show
introduced by
Detected access of super global var $_POST, probably need manual inspection.
Loading history...
978
			} else {
979
				$other_val = sanitize_text_field( $_POST['item_meta'][ $parent ][ $pointer ]['other'][ $field['id'] ] );
0 ignored issues
show
introduced by
Detected access of super global var $_POST, probably need manual inspection.
Loading history...
980
			}
981
			return $other_val;
982
983
		} else if ( isset( $field['id'] ) && isset( $_POST['item_meta']['other'][ $field['id'] ] ) ) {
984
			// For normal fields
985
986
			if ( FrmField::is_field_with_multiple_values( $field ) ) {
987
				$other_val = isset( $_POST['item_meta']['other'][ $field['id'] ][ $opt_key ] ) ? sanitize_text_field( $_POST['item_meta']['other'][ $field['id'] ][ $opt_key ] ) : '';
0 ignored issues
show
introduced by
Detected access of super global var $_POST, probably need manual inspection.
Loading history...
988
			} else {
989
				$other_val = sanitize_text_field( $_POST['item_meta']['other'][ $field['id'] ] );
0 ignored issues
show
introduced by
Detected access of super global var $_POST, probably need manual inspection.
Loading history...
990
			}
991
			return $other_val;
992
		}
993
994
		// For checkboxes
995
		if ( $field['type'] == 'checkbox' && is_array( $field['value'] ) ) {
996
			// Check if there is an "other" val in saved value and make sure the
997
			// "other" val is not equal to the Other checkbox option
998
			if ( isset( $field['value'][ $opt_key ] ) && $field['options'][ $opt_key ] != $field['value'][ $opt_key ] ) {
999
				$other_val = $field['value'][ $opt_key ];
1000
			}
1001
		} else {
1002
			/**
1003
			 * For radio buttons and dropdowns
1004
			 * Check if saved value equals any of the options. If not, set it as the other value.
1005
			 */
1006
			foreach ( $field['options'] as $opt_key => $opt_val ) {
1007
				$temp_val = is_array( $opt_val ) ? $opt_val['value'] : $opt_val;
1008
				// Multi-select dropdowns - key is not preserved
1009
				if ( is_array( $field['value'] ) ) {
1010
					$o_key = array_search( $temp_val, $field['value'] );
1011
					if ( isset( $field['value'][ $o_key ] ) ) {
1012
						unset( $field['value'][ $o_key ], $o_key );
1013
					}
1014
				} else if ( $temp_val == $field['value'] ) {
1015
					// For radio and regular dropdowns
1016
					return '';
1017
				} else {
1018
					$other_val = $field['value'];
1019
				}
1020
				unset( $opt_key, $opt_val, $temp_val );
1021
			}
1022
			// For multi-select dropdowns only
1023
			if ( is_array( $field['value'] ) && ! empty( $field['value'] ) ) {
1024
				$other_val = reset( $field['value'] );
1025
			}
1026
		}
1027
1028
		return $other_val;
1029
    }
1030
1031
    /**
1032
    * Check if there is a saved value for the "Other" text field. If so, set it as the $other_val.
1033
    * Intended for front-end use
1034
    *
1035
    * @since 2.0.6
1036
    *
1037
    * @param array $args should include field, opt_key and field name
1038
    * @param boolean $other_opt
1039
    * @param string $checked
1040
    * @return string $other_val
1041
    */
1042
    public static function prepare_other_input( $args, &$other_opt, &$checked ) {
1043
		//Check if this is an "Other" option
1044
		if ( ! self::is_other_opt( $args['opt_key'] ) ) {
1045
			return;
1046
		}
1047
1048
		$other_opt = true;
1049
		$other_args = array();
1050
1051
		self::set_other_name( $args, $other_args );
1052
		self::set_other_value( $args, $other_args );
1053
1054
		if ( $other_args['value'] ) {
1055
			$checked = 'checked="checked" ';
1056
		}
1057
1058
        return $other_args;
1059
    }
1060
1061
	/**
1062
	 * @param array $args
1063
	 * @param array $other_args
1064
	 * @since 2.0.6
1065
	 */
1066
	private static function set_other_name( $args, &$other_args ) {
1067
		//Set up name for other field
1068
		$other_args['name'] = str_replace( '[]', '', $args['field_name'] );
1069
		$other_args['name'] = preg_replace('/\[' . $args['field']['id'] . '\]$/', '', $other_args['name']);
1070
		$other_args['name'] = $other_args['name'] . '[other]' . '[' . $args['field']['id'] . ']';
1071
1072
		//Converts item_meta[field_id] => item_meta[other][field_id] and
1073
		//item_meta[parent][0][field_id] => item_meta[parent][0][other][field_id]
1074
		if ( FrmField::is_field_with_multiple_values( $args['field'] ) ) {
1075
			$other_args['name'] .= '[' . $args['opt_key'] . ']';
1076
		}
1077
	}
1078
1079
	/**
1080
	 * Find the parent and pointer, and get text for "other" text field
1081
	 * @param array $args
1082
	 * @param array $other_args
1083
	 *
1084
	 * @since 2.0.6
1085
	 */
1086
	private static function set_other_value( $args, &$other_args ) {
1087
		$parent = $pointer = '';
1088
1089
		// Check for parent ID and pointer
1090
		$temp_array = explode( '[', $args['field_name'] );
1091
1092
		// Count should only be greater than 3 if inside of a repeating section
1093
		if ( count( $temp_array ) > 3 ) {
1094
			$parent = str_replace( ']', '', $temp_array[1] );
1095
			$pointer = str_replace( ']', '', $temp_array[2]);
1096
		}
1097
1098
		// Get text for "other" text field
1099
		$other_args['value'] = self::get_other_val( array( 'opt_key' => $args['opt_key'], 'field' => $args['field'], 'parent' => $parent, 'pointer' => $pointer ) );
1100
	}
1101
1102
	/**
1103
	 * If this field includes an other option, show it
1104
	 * @param $args array
1105
	 * @since 2.0.6
1106
	 */
1107
	public static function include_other_input( $args ) {
1108
        if ( ! $args['other_opt'] ) {
1109
        	return;
1110
		}
1111
1112
		$classes = array( 'frm_other_input' );
1113
		if ( ! $args['checked'] || trim( $args['checked'] ) == '' ) {
1114
			// hide the field if the other option is not selected
1115
			$classes[] = 'frm_pos_none';
1116
		}
1117
		if ( $args['field']['type'] == 'select' && $args['field']['multiple'] ) {
1118
			$classes[] = 'frm_other_full';
1119
		}
1120
1121
		// Set up HTML ID for Other field
1122
		$other_id = self::get_other_field_html_id( $args['field']['type'], $args['html_id'], $args['opt_key'] );
1123
1124
		?><input type="text" id="<?php echo esc_attr( $other_id ) ?>" class="<?php echo sanitize_text_field( implode( ' ', $classes ) ) ?>" <?php
1125
		echo ( $args['read_only'] ? ' readonly="readonly" disabled="disabled"' : '' );
0 ignored issues
show
introduced by
Expected next thing to be a escaping function, not '('
Loading history...
1126
		?> name="<?php echo esc_attr( $args['name'] ) ?>" value="<?php echo esc_attr( $args['value'] ); ?>" /><?php
1127
	}
1128
1129
	/**
1130
	* Get the HTML id for an "Other" text field
1131
	* Note: This does not affect fields in repeating sections
1132
	*
1133
	* @since 2.0.08
1134
	* @param string $type - field type
1135
	* @param string $html_id
1136
	* @param string|boolean $opt_key
1137
	* @return string $other_id
1138
	*/
1139
	public static function get_other_field_html_id( $type, $html_id, $opt_key = false ) {
1140
		$other_id = $html_id;
1141
1142
		// If hidden radio field, add an opt key of 0
1143
		if ( $type == 'radio' && $opt_key === false ) {
1144
			$opt_key = 0;
1145
		}
1146
1147
		if ( $opt_key !== false ) {
1148
			$other_id .= '-' . $opt_key;
1149
		}
1150
1151
		$other_id .= '-otext';
1152
1153
		return $other_id;
1154
	}
1155
1156
	public static function clear_on_focus_html( $field, $display, $id = '' ) {
1157
		if ( $display['clear_on_focus'] ) {
1158
			?><span id="frm_clear_on_focus_<?php echo esc_attr( $field['id'] . $id ) ?>" class="frm-show-click"><?php
1159
1160
			if ( $display['default_blank'] ) {
0 ignored issues
show
Coding Style introduced by
Line indented incorrectly; expected 0 tabs, found 3
Loading history...
1161
				self::show_default_blank_js( $field['default_blank'] );
1162
			}
1163
1164
			self::show_onfocus_js( $field['clear_on_focus'] );
1165
			?></span><?php
1166
		}
1167
	}
1168
1169 View Code Duplication
	public static function show_onfocus_js( $is_selected ) {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
1170
		$atts = array(
1171
			'icon'        => 'frm_reload_icon',
1172
			'message'     => $is_selected ? __( 'Clear default value when typing', 'formidable' ) : __( 'Do not clear default value when typing', 'formidable' ),
1173
			'is_selected' => $is_selected,
1174
		);
1175
		self::show_icon_link_js( $atts );
1176
	}
1177
1178 View Code Duplication
	public static function show_default_blank_js( $is_selected ) {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
1179
		$atts = array(
1180
			'icon'        => 'frm_error_icon',
1181
			'message'     => $is_selected ? __( 'Default value will NOT pass form validation', 'formidable' ) : __( 'Default value will pass form validation', 'formidable' ),
1182
			'is_selected' => $is_selected,
1183
		);
1184
		self::show_icon_link_js( $atts );
1185
	}
1186
1187
	public static function show_icon_link_js( $atts ) {
1188
		$atts['icon'] .= $atts['is_selected'] ? ' ' : ' frm_inactive_icon ';
1189
		?><a href="javascript:void(0)" class="frm_bstooltip <?php echo esc_attr( $atts['icon'] ); ?>frm_default_val_icons frm_action_icon frm_icon_font" title="<?php echo esc_attr( $atts['message'] ); ?>"></a><?php
1190
	}
1191
1192
	public static function switch_field_ids( $val ) {
1193
        global $frm_duplicate_ids;
1194
        $replace = array();
1195
        $replace_with = array();
1196
        foreach ( (array) $frm_duplicate_ids as $old => $new ) {
1197
            $replace[] = '[if '. $old .']';
1198
            $replace_with[] = '[if '. $new .']';
1199
            $replace[] = '[if '. $old .' ';
1200
            $replace_with[] = '[if '. $new .' ';
1201
            $replace[] = '[/if '. $old .']';
1202
            $replace_with[] = '[/if '. $new .']';
1203
            $replace[] = '[foreach '. $old .']';
1204
            $replace_with[] = '[foreach '. $new .']';
1205
            $replace[] = '[/foreach '. $old .']';
1206
            $replace_with[] = '[/foreach '. $new .']';
1207
            $replace[] = '['. $old .']';
1208
            $replace_with[] = '['. $new .']';
1209
            $replace[] = '['. $old .' ';
1210
            $replace_with[] = '['. $new .' ';
1211
            unset($old, $new);
1212
        }
1213
		if ( is_array( $val ) ) {
1214
			foreach ( $val as $k => $v ) {
1215
                $val[ $k ] = str_replace( $replace, $replace_with, $v );
1216
                unset($k, $v);
1217
            }
1218
        } else {
1219
            $val = str_replace($replace, $replace_with, $val);
1220
        }
1221
1222
        return $val;
1223
    }
1224
1225
    public static function get_us_states() {
1226
        return apply_filters( 'frm_us_states', array(
1227
            'AL' => 'Alabama', 'AK' => 'Alaska', 'AR' => 'Arkansas', 'AZ' => 'Arizona',
1228
            'CA' => 'California', 'CO' => 'Colorado', 'CT' => 'Connecticut', 'DE' => 'Delaware',
1229
            'DC' => 'District of Columbia',
1230
            'FL' => 'Florida', 'GA' => 'Georgia', 'HI' => 'Hawaii', 'ID' => 'Idaho',
1231
            'IL' => 'Illinois', 'IN' => 'Indiana', 'IA' => 'Iowa', 'KS' => 'Kansas',
1232
            'KY' => 'Kentucky', 'LA' => 'Louisiana', 'ME' => 'Maine','MD' => 'Maryland',
1233
            'MA' => 'Massachusetts', 'MI' => 'Michigan', 'MN' => 'Minnesota', 'MS' => 'Mississippi',
1234
            'MO' => 'Missouri', 'MT' => 'Montana', 'NE' => 'Nebraska', 'NV' => 'Nevada',
1235
            'NH' => 'New Hampshire', 'NJ' => 'New Jersey', 'NM' => 'New Mexico', 'NY' => 'New York',
1236
            'NC' => 'North Carolina', 'ND' => 'North Dakota', 'OH' => 'Ohio', 'OK' => 'Oklahoma',
1237
            'OR' => 'Oregon', 'PA' => 'Pennsylvania', 'RI' => 'Rhode Island', 'SC' => 'South Carolina',
1238
            'SD' => 'South Dakota', 'TN' => 'Tennessee', 'TX' => 'Texas', 'UT' => 'Utah',
1239
            'VT' => 'Vermont', 'VA' => 'Virginia', 'WA' => 'Washington', 'WV' => 'West Virginia',
1240
            'WI' => 'Wisconsin', 'WY' => 'Wyoming',
1241
        ) );
1242
    }
1243
1244
    public static function get_countries() {
1245
        return apply_filters( 'frm_countries', array(
1246
            __( 'Afghanistan', 'formidable' ), __( 'Albania', 'formidable' ), __( 'Algeria', 'formidable' ),
1247
            __( 'American Samoa', 'formidable' ), __( 'Andorra', 'formidable' ), __( 'Angola', 'formidable' ),
1248
            __( 'Anguilla', 'formidable' ), __( 'Antarctica', 'formidable' ), __( 'Antigua and Barbuda', 'formidable' ),
1249
            __( 'Argentina', 'formidable' ), __( 'Armenia', 'formidable' ), __( 'Aruba', 'formidable' ),
1250
            __( 'Australia', 'formidable' ), __( 'Austria', 'formidable' ), __( 'Azerbaijan', 'formidable' ),
1251
            __( 'Bahamas', 'formidable' ), __( 'Bahrain', 'formidable' ), __( 'Bangladesh', 'formidable' ),
1252
            __( 'Barbados', 'formidable' ), __( 'Belarus', 'formidable' ), __( 'Belgium', 'formidable' ),
1253
            __( 'Belize', 'formidable' ), __( 'Benin', 'formidable' ), __( 'Bermuda', 'formidable' ),
1254
            __( 'Bhutan', 'formidable' ), __( 'Bolivia', 'formidable' ), __( 'Bosnia and Herzegovina', 'formidable' ),
1255
            __( 'Botswana', 'formidable' ), __( 'Brazil', 'formidable' ), __( 'Brunei', 'formidable' ),
1256
            __( 'Bulgaria', 'formidable' ), __( 'Burkina Faso', 'formidable' ), __( 'Burundi', 'formidable' ),
1257
            __( 'Cambodia', 'formidable' ), __( 'Cameroon', 'formidable' ), __( 'Canada', 'formidable' ),
1258
            __( 'Cape Verde', 'formidable' ), __( 'Cayman Islands', 'formidable' ), __( 'Central African Republic', 'formidable' ),
1259
            __( 'Chad', 'formidable' ), __( 'Chile', 'formidable' ), __( 'China', 'formidable' ),
1260
            __( 'Colombia', 'formidable' ), __( 'Comoros', 'formidable' ), __( 'Congo', 'formidable' ),
1261
            __( 'Costa Rica', 'formidable' ), __( 'C&ocirc;te d\'Ivoire', 'formidable' ), __( 'Croatia', 'formidable' ),
1262
            __( 'Cuba', 'formidable' ), __( 'Cyprus', 'formidable' ), __( 'Czech Republic', 'formidable' ),
1263
            __( 'Denmark', 'formidable' ), __( 'Djibouti', 'formidable' ), __( 'Dominica', 'formidable' ),
1264
            __( 'Dominican Republic', 'formidable' ), __( 'East Timor', 'formidable' ), __( 'Ecuador', 'formidable' ),
1265
            __( 'Egypt', 'formidable' ), __( 'El Salvador', 'formidable' ), __( 'Equatorial Guinea', 'formidable' ),
1266
            __( 'Eritrea', 'formidable' ), __( 'Estonia', 'formidable' ), __( 'Ethiopia', 'formidable' ),
1267
            __( 'Fiji', 'formidable' ), __( 'Finland', 'formidable' ), __( 'France', 'formidable' ),
1268
            __( 'French Guiana', 'formidable' ), __( 'French Polynesia', 'formidable' ), __( 'Gabon', 'formidable' ),
1269
            __( 'Gambia', 'formidable' ), __( 'Georgia', 'formidable' ), __( 'Germany', 'formidable' ),
1270
            __( 'Ghana', 'formidable' ), __( 'Gibraltar', 'formidable' ), __( 'Greece', 'formidable' ),
1271
            __( 'Greenland', 'formidable' ), __( 'Grenada', 'formidable' ), __( 'Guam', 'formidable' ),
1272
            __( 'Guatemala', 'formidable' ), __( 'Guinea', 'formidable' ), __( 'Guinea-Bissau', 'formidable' ),
1273
            __( 'Guyana', 'formidable' ), __( 'Haiti', 'formidable' ), __( 'Honduras', 'formidable' ),
1274
            __( 'Hong Kong', 'formidable' ), __( 'Hungary', 'formidable' ), __( 'Iceland', 'formidable' ),
1275
            __( 'India', 'formidable' ), __( 'Indonesia', 'formidable' ), __( 'Iran', 'formidable' ),
1276
            __( 'Iraq', 'formidable' ), __( 'Ireland', 'formidable' ), __( 'Israel', 'formidable' ),
1277
            __( 'Italy', 'formidable' ), __( 'Jamaica', 'formidable' ), __( 'Japan', 'formidable' ),
1278
            __( 'Jordan', 'formidable' ), __( 'Kazakhstan', 'formidable' ), __( 'Kenya', 'formidable' ),
1279
            __( 'Kiribati', 'formidable' ), __( 'North Korea', 'formidable' ), __( 'South Korea', 'formidable' ),
1280
            __( 'Kuwait', 'formidable' ), __( 'Kyrgyzstan', 'formidable' ), __( 'Laos', 'formidable' ),
1281
            __( 'Latvia', 'formidable' ), __( 'Lebanon', 'formidable' ), __( 'Lesotho', 'formidable' ),
1282
            __( 'Liberia', 'formidable' ), __( 'Libya', 'formidable' ), __( 'Liechtenstein', 'formidable' ),
1283
            __( 'Lithuania', 'formidable' ), __( 'Luxembourg', 'formidable' ), __( 'Macedonia', 'formidable' ),
1284
            __( 'Madagascar', 'formidable' ), __( 'Malawi', 'formidable' ), __( 'Malaysia', 'formidable' ),
1285
            __( 'Maldives', 'formidable' ), __( 'Mali', 'formidable' ), __( 'Malta', 'formidable' ),
1286
            __( 'Marshall Islands', 'formidable' ), __( 'Mauritania', 'formidable' ), __( 'Mauritius', 'formidable' ),
1287
            __( 'Mexico', 'formidable' ), __( 'Micronesia', 'formidable' ), __( 'Moldova', 'formidable' ),
1288
            __( 'Monaco', 'formidable' ), __( 'Mongolia', 'formidable' ), __( 'Montenegro', 'formidable' ),
1289
            __( 'Montserrat', 'formidable' ), __( 'Morocco', 'formidable' ), __( 'Mozambique', 'formidable' ),
1290
            __( 'Myanmar', 'formidable' ), __( 'Namibia', 'formidable' ), __( 'Nauru', 'formidable' ),
1291
            __( 'Nepal', 'formidable' ), __( 'Netherlands', 'formidable' ), __( 'New Zealand', 'formidable' ),
1292
            __( 'Nicaragua', 'formidable' ), __( 'Niger', 'formidable' ), __( 'Nigeria', 'formidable' ),
1293
            __( 'Norway', 'formidable' ), __( 'Northern Mariana Islands', 'formidable' ), __( 'Oman', 'formidable' ),
1294
            __( 'Pakistan', 'formidable' ), __( 'Palau', 'formidable' ), __( 'Palestine', 'formidable' ),
1295
            __( 'Panama', 'formidable' ), __( 'Papua New Guinea', 'formidable' ), __( 'Paraguay', 'formidable' ),
1296
            __( 'Peru', 'formidable' ), __( 'Philippines', 'formidable' ), __( 'Poland', 'formidable' ),
1297
            __( 'Portugal', 'formidable' ), __( 'Puerto Rico', 'formidable' ), __( 'Qatar', 'formidable' ),
1298
            __( 'Romania', 'formidable' ), __( 'Russia', 'formidable' ), __( 'Rwanda', 'formidable' ),
1299
            __( 'Saint Kitts and Nevis', 'formidable' ), __( 'Saint Lucia', 'formidable' ),
1300
            __( 'Saint Vincent and the Grenadines', 'formidable' ), __( 'Samoa', 'formidable' ),
1301
            __( 'San Marino', 'formidable' ), __( 'Sao Tome and Principe', 'formidable' ), __( 'Saudi Arabia', 'formidable' ),
1302
            __( 'Senegal', 'formidable' ), __( 'Serbia and Montenegro', 'formidable' ), __( 'Seychelles', 'formidable' ),
1303
            __( 'Sierra Leone', 'formidable' ), __( 'Singapore', 'formidable' ), __( 'Slovakia', 'formidable' ),
1304
            __( 'Slovenia', 'formidable' ), __( 'Solomon Islands', 'formidable' ), __( 'Somalia', 'formidable' ),
1305
            __( 'South Africa', 'formidable' ), __( 'South Sudan', 'formidable' ),
1306
            __( 'Spain', 'formidable' ), __( 'Sri Lanka', 'formidable' ),
1307
            __( 'Sudan', 'formidable' ), __( 'Suriname', 'formidable' ), __( 'Swaziland', 'formidable' ),
1308
            __( 'Sweden', 'formidable' ), __( 'Switzerland', 'formidable' ), __( 'Syria', 'formidable' ),
1309
            __( 'Taiwan', 'formidable' ), __( 'Tajikistan', 'formidable' ), __( 'Tanzania', 'formidable' ),
1310
            __( 'Thailand', 'formidable' ), __( 'Togo', 'formidable' ), __( 'Tonga', 'formidable' ),
1311
            __( 'Trinidad and Tobago', 'formidable' ), __( 'Tunisia', 'formidable' ), __( 'Turkey', 'formidable' ),
1312
            __( 'Turkmenistan', 'formidable' ), __( 'Tuvalu', 'formidable' ), __( 'Uganda', 'formidable' ),
1313
            __( 'Ukraine', 'formidable' ), __( 'United Arab Emirates', 'formidable' ), __( 'United Kingdom', 'formidable' ),
1314
            __( 'United States', 'formidable' ), __( 'Uruguay', 'formidable' ), __( 'Uzbekistan', 'formidable' ),
1315
            __( 'Vanuatu', 'formidable' ), __( 'Vatican City', 'formidable' ), __( 'Venezuela', 'formidable' ),
1316
            __( 'Vietnam', 'formidable' ), __( 'Virgin Islands, British', 'formidable' ),
1317
            __( 'Virgin Islands, U.S.', 'formidable' ), __( 'Yemen', 'formidable' ), __( 'Zambia', 'formidable' ),
1318
            __( 'Zimbabwe', 'formidable' ),
1319
        ) );
1320
    }
1321
1322
	public static function get_bulk_prefilled_opts( array &$prepop ) {
1323
		$prepop[ __( 'Countries', 'formidable' ) ] = FrmFieldsHelper::get_countries();
1324
1325
        $states = FrmFieldsHelper::get_us_states();
1326
        $state_abv = array_keys($states);
1327
        sort($state_abv);
1328
		$prepop[ __( 'U.S. State Abbreviations', 'formidable' ) ] = $state_abv;
1329
1330
        $states = array_values($states);
1331
        sort($states);
1332
		$prepop[ __( 'U.S. States', 'formidable' ) ] = $states;
1333
        unset($state_abv, $states);
1334
1335
		$prepop[ __( 'Age', 'formidable' ) ] = array(
1336
            __( 'Under 18', 'formidable' ), __( '18-24', 'formidable' ), __( '25-34', 'formidable' ),
1337
            __( '35-44', 'formidable' ), __( '45-54', 'formidable' ), __( '55-64', 'formidable' ),
1338
            __( '65 or Above', 'formidable' ), __( 'Prefer Not to Answer', 'formidable' ),
1339
        );
1340
1341
		$prepop[ __( 'Satisfaction', 'formidable' ) ] = array(
1342
            __( 'Very Satisfied', 'formidable' ), __( 'Satisfied', 'formidable' ), __( 'Neutral', 'formidable' ),
1343
            __( 'Unsatisfied', 'formidable' ), __( 'Very Unsatisfied', 'formidable' ), __( 'N/A', 'formidable' ),
1344
        );
1345
1346
		$prepop[ __( 'Importance', 'formidable' ) ] = array(
1347
            __( 'Very Important', 'formidable' ), __( 'Important', 'formidable' ), __( 'Neutral', 'formidable' ),
1348
            __( 'Somewhat Important', 'formidable' ), __( 'Not at all Important', 'formidable' ), __( 'N/A', 'formidable' ),
1349
        );
1350
1351
		$prepop[ __( 'Agreement', 'formidable' ) ] = array(
1352
            __( 'Strongly Agree', 'formidable' ), __( 'Agree', 'formidable' ), __( 'Neutral', 'formidable' ),
1353
            __( 'Disagree', 'formidable' ), __( 'Strongly Disagree', 'formidable' ), __( 'N/A', 'formidable' ),
1354
        );
1355
1356
		$prepop = apply_filters( 'frm_bulk_field_choices', $prepop );
1357
    }
1358
1359
	public static function field_selection() {
1360
		_deprecated_function( __FUNCTION__, '2.0.9', 'FrmField::field_selection' );
1361
		return FrmField::field_selection();
1362
	}
1363
1364
	public static function pro_field_selection() {
1365
		_deprecated_function( __FUNCTION__, '2.0.9', 'FrmField::pro_field_selection' );
1366
		return FrmField::pro_field_selection();
1367
	}
1368
1369
	public static function is_no_save_field( $type ) {
1370
		_deprecated_function( __FUNCTION__, '2.0.9', 'FrmField::is_no_save_field' );
1371
		return FrmField::is_no_save_field( $type );
1372
	}
1373
1374
	public static function no_save_fields() {
1375
		_deprecated_function( __FUNCTION__, '2.0.9', 'FrmField::no_save_fields' );
1376
		return FrmField::no_save_fields();
1377
	}
1378
1379
	public static function is_multiple_select( $field ) {
1380
		_deprecated_function( __FUNCTION__, '2.0.9', 'FrmField::is_multiple_select' );
1381
		return FrmField::is_multiple_select( $field );
1382
	}
1383
1384
	public static function is_field_with_multiple_values( $field ) {
1385
		_deprecated_function( __FUNCTION__, '2.0.9', 'FrmField::is_field_with_multiple_values' );
1386
		return FrmField::is_field_with_multiple_values( $field );
1387
	}
1388
1389
	public static function is_required_field( $field ) {
1390
		_deprecated_function( __FUNCTION__, '2.0.9', 'FrmField::is_required' );
1391
		return FrmField::is_required( $field );
1392
	}
1393
1394
    public static function maybe_get_field( &$field ) {
1395
		_deprecated_function( __FUNCTION__, '2.0.9', 'FrmField::maybe_get_field' );
1396
		FrmField::maybe_get_field( $field );
1397
    }
1398
}
1399