|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* @package Pods\Fields |
|
5
|
|
|
*/ |
|
6
|
|
|
class PodsField_Text extends PodsField { |
|
7
|
|
|
|
|
8
|
|
|
/** |
|
9
|
|
|
* {@inheritdoc} |
|
10
|
|
|
*/ |
|
11
|
|
|
public static $group = 'Text'; |
|
12
|
|
|
|
|
13
|
|
|
/** |
|
14
|
|
|
* {@inheritdoc} |
|
15
|
|
|
*/ |
|
16
|
|
|
public static $type = 'text'; |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* {@inheritdoc} |
|
20
|
|
|
*/ |
|
21
|
|
|
public static $label = 'Plain Text'; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* {@inheritdoc} |
|
25
|
|
|
*/ |
|
26
|
|
|
public static $prepare = '%s'; |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* {@inheritdoc} |
|
30
|
|
|
*/ |
|
31
|
|
|
public function __construct() { |
|
32
|
|
|
|
|
33
|
|
|
self::$label = __( 'Plain Text', 'pods' ); |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* {@inheritdoc} |
|
38
|
|
|
*/ |
|
39
|
|
|
public function options() { |
|
40
|
|
|
|
|
41
|
|
|
$options = array( |
|
42
|
|
|
static::$type . '_repeatable' => array( |
|
43
|
|
|
'label' => __( 'Repeatable Field', 'pods' ), |
|
44
|
|
|
'default' => 0, |
|
45
|
|
|
'type' => 'boolean', |
|
46
|
|
|
'help' => __( 'Making a field repeatable will add controls next to the field which allows users to Add/Remove/Reorder additional values. These values are saved in the database as an array, so searching and filtering by them may require further adjustments".', 'pods' ), |
|
47
|
|
|
'boolean_yes_label' => '', |
|
48
|
|
|
'dependency' => true, |
|
49
|
|
|
'developer_mode' => true, |
|
50
|
|
|
), |
|
51
|
|
|
'output_options' => array( |
|
52
|
|
|
'label' => __( 'Output Options', 'pods' ), |
|
53
|
|
|
'group' => array( |
|
54
|
|
|
static::$type . '_allow_shortcode' => array( |
|
55
|
|
|
'label' => __( 'Allow Shortcodes?', 'pods' ), |
|
56
|
|
|
'default' => 0, |
|
57
|
|
|
'type' => 'boolean', |
|
58
|
|
|
'dependency' => true, |
|
59
|
|
|
), |
|
60
|
|
|
static::$type . '_allow_html' => array( |
|
61
|
|
|
'label' => __( 'Allow HTML?', 'pods' ), |
|
62
|
|
|
'default' => 0, |
|
63
|
|
|
'type' => 'boolean', |
|
64
|
|
|
'dependency' => true, |
|
65
|
|
|
), |
|
66
|
|
|
), |
|
67
|
|
|
), |
|
68
|
|
|
static::$type . '_allowed_html_tags' => array( |
|
69
|
|
|
'label' => __( 'Allowed HTML Tags', 'pods' ), |
|
70
|
|
|
'depends-on' => array( static::$type . '_allow_html' => true ), |
|
71
|
|
|
'default' => 'strong em a ul ol li b i', |
|
72
|
|
|
'type' => 'text', |
|
73
|
|
|
), |
|
74
|
|
|
static::$type . '_max_length' => array( |
|
75
|
|
|
'label' => __( 'Maximum Length', 'pods' ), |
|
76
|
|
|
'default' => 255, |
|
77
|
|
|
'type' => 'number', |
|
78
|
|
|
'help' => __( 'Set to -1 for no limit', 'pods' ), |
|
79
|
|
|
), |
|
80
|
|
|
static::$type . '_placeholder' => array( |
|
81
|
|
|
'label' => __( 'HTML Placeholder', 'pods' ), |
|
82
|
|
|
'default' => '', |
|
83
|
|
|
'type' => 'text', |
|
84
|
|
|
'help' => array( |
|
85
|
|
|
__( 'Placeholders can provide instructions or an example of the required data format for a field. Please note: It is not a replacement for labels or description text, and it is less accessible for people using screen readers.', 'pods' ), |
|
86
|
|
|
'https://www.w3.org/WAI/tutorials/forms/instructions/#placeholder-text', |
|
87
|
|
|
), |
|
88
|
|
|
), |
|
89
|
|
|
); |
|
90
|
|
|
|
|
91
|
|
|
return $options; |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
/** |
|
95
|
|
|
* {@inheritdoc} |
|
96
|
|
|
*/ |
|
97
|
|
View Code Duplication |
public function schema( $options = null ) { |
|
|
|
|
|
|
98
|
|
|
|
|
99
|
|
|
$length = (int) pods_v( static::$type . '_max_length', $options, 255 ); |
|
100
|
|
|
|
|
101
|
|
|
$schema = 'VARCHAR(' . $length . ')'; |
|
102
|
|
|
|
|
103
|
|
|
if ( 255 < $length || $length < 1 ) { |
|
104
|
|
|
$schema = 'LONGTEXT'; |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
return $schema; |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
/** |
|
111
|
|
|
* {@inheritdoc} |
|
112
|
|
|
*/ |
|
113
|
|
View Code Duplication |
public function display( $value = null, $name = null, $options = null, $pod = null, $id = null ) { |
|
|
|
|
|
|
114
|
|
|
|
|
115
|
|
|
$value = $this->strip_html( $value, $options ); |
|
116
|
|
|
|
|
117
|
|
|
if ( 1 === (int) pods_v( static::$type . '_allow_shortcode', $options ) ) { |
|
118
|
|
|
$value = do_shortcode( $value ); |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
|
|
return $value; |
|
122
|
|
|
} |
|
123
|
|
|
|
|
124
|
|
|
/** |
|
125
|
|
|
* {@inheritdoc} |
|
126
|
|
|
*/ |
|
127
|
|
View Code Duplication |
public function input( $name, $value = null, $options = null, $pod = null, $id = null ) { |
|
|
|
|
|
|
128
|
|
|
|
|
129
|
|
|
$options = (array) $options; |
|
130
|
|
|
$form_field_type = PodsForm::$field_type; |
|
131
|
|
|
|
|
132
|
|
|
if ( is_array( $value ) ) { |
|
133
|
|
|
$value = implode( ' ', $value ); |
|
134
|
|
|
} |
|
135
|
|
|
|
|
136
|
|
|
if ( isset( $options['name'] ) && false === PodsForm::permission( static::$type, $options['name'], $options, null, $pod, $id ) ) { |
|
137
|
|
|
if ( pods_v( 'read_only', $options, false ) ) { |
|
138
|
|
|
$options['readonly'] = true; |
|
139
|
|
|
} else { |
|
140
|
|
|
return; |
|
141
|
|
|
} |
|
142
|
|
|
} elseif ( ! pods_has_permissions( $options ) && pods_v( 'read_only', $options, false ) ) { |
|
143
|
|
|
$options['readonly'] = true; |
|
144
|
|
|
} |
|
145
|
|
|
|
|
146
|
|
|
pods_view( PODS_DIR . 'ui/fields/text.php', compact( array_keys( get_defined_vars() ) ) ); |
|
147
|
|
|
} |
|
148
|
|
|
|
|
149
|
|
|
/** |
|
150
|
|
|
* {@inheritdoc} |
|
151
|
|
|
*/ |
|
152
|
|
View Code Duplication |
public function validate( $value, $name = null, $options = null, $fields = null, $pod = null, $id = null, $params = null ) { |
|
|
|
|
|
|
153
|
|
|
|
|
154
|
|
|
$errors = array(); |
|
155
|
|
|
|
|
156
|
|
|
$check = $this->pre_save( $value, $id, $name, $options, $fields, $pod, $params ); |
|
157
|
|
|
|
|
158
|
|
|
if ( is_array( $check ) ) { |
|
159
|
|
|
$errors = $check; |
|
160
|
|
|
} else { |
|
161
|
|
|
if ( 0 < strlen( $value ) && '' === $check ) { |
|
162
|
|
|
if ( 1 === (int) pods_v( 'required', $options ) ) { |
|
163
|
|
|
$errors[] = __( 'This field is required.', 'pods' ); |
|
164
|
|
|
} |
|
165
|
|
|
} |
|
166
|
|
|
} |
|
167
|
|
|
|
|
168
|
|
|
if ( ! empty( $errors ) ) { |
|
169
|
|
|
return $errors; |
|
170
|
|
|
} |
|
171
|
|
|
|
|
172
|
|
|
return true; |
|
173
|
|
|
} |
|
174
|
|
|
|
|
175
|
|
|
/** |
|
176
|
|
|
* {@inheritdoc} |
|
177
|
|
|
*/ |
|
178
|
|
View Code Duplication |
public function pre_save( $value, $id = null, $name = null, $options = null, $fields = null, $pod = null, $params = null ) { |
|
|
|
|
|
|
179
|
|
|
|
|
180
|
|
|
$value = $this->strip_html( $value, $options ); |
|
181
|
|
|
|
|
182
|
|
|
$length = (int) pods_v( static::$type . '_max_length', $options, 255 ); |
|
183
|
|
|
|
|
184
|
|
|
if ( 0 < $length && $length < pods_mb_strlen( $value ) ) { |
|
185
|
|
|
$value = pods_mb_substr( $value, 0, $length ); |
|
186
|
|
|
} |
|
187
|
|
|
|
|
188
|
|
|
return $value; |
|
189
|
|
|
} |
|
190
|
|
|
|
|
191
|
|
|
/** |
|
192
|
|
|
* {@inheritdoc} |
|
193
|
|
|
*/ |
|
194
|
|
View Code Duplication |
public function ui( $id, $value, $name = null, $options = null, $fields = null, $pod = null ) { |
|
|
|
|
|
|
195
|
|
|
|
|
196
|
|
|
$value = $this->strip_html( $value, $options ); |
|
197
|
|
|
|
|
198
|
|
|
if ( 0 === (int) pods_v( static::$type . '_allow_html', $options, 0, true ) ) { |
|
199
|
|
|
$value = wp_trim_words( $value ); |
|
200
|
|
|
} |
|
201
|
|
|
|
|
202
|
|
|
return $value; |
|
203
|
|
|
} |
|
204
|
|
|
} |
|
205
|
|
|
|
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.