|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* @since 3.0 |
|
5
|
|
|
*/ |
|
6
|
|
|
class FrmFieldUrl extends FrmFieldType { |
|
7
|
|
|
|
|
8
|
|
|
/** |
|
9
|
|
|
* @var string |
|
10
|
|
|
* @since 3.0 |
|
11
|
|
|
*/ |
|
12
|
|
|
protected $type = 'url'; |
|
13
|
|
|
protected $display_type = 'text'; |
|
14
|
|
|
|
|
15
|
|
View Code Duplication |
protected function field_settings_for_type() { |
|
|
|
|
|
|
16
|
|
|
return array( |
|
17
|
|
|
'size' => true, |
|
18
|
|
|
'clear_on_focus' => true, |
|
19
|
|
|
'invalid' => true, |
|
20
|
|
|
'show_image' => true, |
|
21
|
|
|
); |
|
22
|
|
|
} |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* @return array |
|
26
|
|
|
*/ |
|
27
|
|
|
protected function extra_field_opts() { |
|
28
|
|
|
return array( |
|
29
|
|
|
'show_image' => 0, |
|
30
|
|
|
); |
|
31
|
|
|
} |
|
32
|
|
|
|
|
33
|
|
|
protected function get_field_name() { |
|
34
|
|
|
return __( 'Website', 'formidable' ); |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
protected function fill_default_atts( &$atts ) { |
|
38
|
|
|
$defaults = array( |
|
39
|
|
|
'sep' => ', ', |
|
40
|
|
|
'html' => false, |
|
41
|
|
|
); |
|
42
|
|
|
$atts = wp_parse_args( $atts, $defaults ); |
|
43
|
|
|
|
|
44
|
|
|
if ( $atts['html'] ) { |
|
45
|
|
|
$atts['sep'] = ' '; |
|
46
|
|
|
} |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
public function validate( $args ) { |
|
50
|
|
|
$value = $args['value']; |
|
51
|
|
|
if ( trim( $value ) == 'http://' || empty( $value ) ) { |
|
52
|
|
|
$value = ''; |
|
53
|
|
|
} else { |
|
54
|
|
|
$value = esc_url_raw( $value ); |
|
55
|
|
|
$value = preg_match( '/^(https?|ftps?|mailto|news|feed|telnet):/is', $value ) ? $value : 'http://' . $value; |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
FrmEntriesHelper::set_posted_value( $this->field, $value, $args ); |
|
59
|
|
|
|
|
60
|
|
|
$errors = array(); |
|
61
|
|
|
|
|
62
|
|
|
// validate the url format |
|
63
|
|
|
if ( ! empty( $value ) && ! preg_match( '/^http(s)?:\/\/(?:localhost|(?:[\da-z\.-]+\.[\da-z\.-]+))/i', $value ) ) { |
|
64
|
|
|
$errors[ 'field' . $args['id'] ] = FrmFieldsHelper::get_error_msg( $this->field, 'invalid' ); |
|
65
|
|
|
} elseif ( $this->field->required == '1' && empty( $value ) ) { |
|
66
|
|
|
$errors[ 'field' . $args['id'] ] = FrmFieldsHelper::get_error_msg( $this->field, 'blank' ); |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
return $errors; |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
protected function prepare_display_value( $value, $atts ) { |
|
73
|
|
|
if ( $atts['html'] ) { |
|
74
|
|
|
$images = ''; |
|
75
|
|
|
foreach ( (array) $value as $url ) { |
|
76
|
|
|
$image_regex = '/(\.(?i)(jpg|jpeg|png|gif))$/'; |
|
77
|
|
|
$is_image = preg_match( $image_regex, $url ); |
|
78
|
|
|
if ( $is_image ) { |
|
79
|
|
|
$images .= '<img src="' . esc_attr( $url ) . '" class="frm_image_from_url" alt="" /> '; |
|
80
|
|
|
} else { |
|
81
|
|
|
$images .= strip_tags( $url ); |
|
82
|
|
|
} |
|
83
|
|
|
} |
|
84
|
|
|
$value = $images; |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
return $value; |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
/** |
|
91
|
|
|
* @since 4.0 |
|
92
|
|
|
*/ |
|
93
|
|
|
public function sanitize_value( &$value ) { |
|
94
|
|
|
FrmAppHelper::sanitize_value( 'esc_url_raw', $value ); |
|
95
|
|
|
} |
|
96
|
|
|
} |
|
97
|
|
|
|
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.