|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* @package Pods\Fields |
|
4
|
|
|
*/ |
|
5
|
|
|
class PodsField_Website extends PodsField { |
|
6
|
|
|
|
|
7
|
|
|
/** |
|
8
|
|
|
* {@inheritdoc} |
|
9
|
|
|
*/ |
|
10
|
|
|
public static $group = 'Text'; |
|
11
|
|
|
|
|
12
|
|
|
/** |
|
13
|
|
|
* {@inheritdoc} |
|
14
|
|
|
*/ |
|
15
|
|
|
public static $type = 'website'; |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* {@inheritdoc} |
|
19
|
|
|
*/ |
|
20
|
|
|
public static $label = 'Website'; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* {@inheritdoc} |
|
24
|
|
|
*/ |
|
25
|
|
|
public static $prepare = '%s'; |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* {@inheritdoc} |
|
29
|
|
|
*/ |
|
30
|
|
|
public function setup() { |
|
31
|
|
|
|
|
32
|
|
|
self::$label = __( 'Website', 'pods' ); |
|
33
|
|
|
|
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* {@inheritdoc} |
|
38
|
|
|
*/ |
|
39
|
|
|
public function options() { |
|
40
|
|
|
$options = array( |
|
41
|
|
|
static::$type . '_repeatable' => array( |
|
42
|
|
|
'label' => __( 'Repeatable Field', 'pods' ), |
|
43
|
|
|
'default' => 0, |
|
44
|
|
|
'type' => 'boolean', |
|
45
|
|
|
'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' ), |
|
46
|
|
|
'boolean_yes_label' => '', |
|
47
|
|
|
'dependency' => true, |
|
48
|
|
|
'developer_mode' => true, |
|
49
|
|
|
), |
|
50
|
|
|
static::$type . '_format' => array( |
|
51
|
|
|
'label' => __( 'Format', 'pods' ), |
|
52
|
|
|
'default' => 'normal', |
|
53
|
|
|
'type' => 'pick', |
|
54
|
|
|
'data' => array( |
|
55
|
|
|
'normal' => __( 'http://example.com/', 'pods' ), |
|
56
|
|
|
'no-www' => __( 'http://example.com/ (remove www)', 'pods' ), |
|
57
|
|
|
'force-www' => __( 'http://www.example.com/ (force www if no sub-domain provided)', 'pods' ), |
|
58
|
|
|
'no-http' => __( 'example.com', 'pods' ), |
|
59
|
|
|
'no-http-no-www' => __( 'example.com (force removal of www)', 'pods' ), |
|
60
|
|
|
'no-http-force-www' => __( 'www.example.com (force www if no sub-domain provided)', 'pods' ), |
|
61
|
|
|
'none' => __( 'No format', 'pods' ), |
|
62
|
|
|
), |
|
63
|
|
|
'dependency' => true, |
|
64
|
|
|
), |
|
65
|
|
|
static::$type . '_allow_port' => array( |
|
66
|
|
|
'label' => __( 'Allow port in URL?', 'pods' ), |
|
67
|
|
|
'default' => apply_filters( 'pods_form_ui_field_website_port', 0, static::$type ), |
|
68
|
|
|
'type' => 'boolean', |
|
69
|
|
|
'dependency' => true, |
|
70
|
|
|
), |
|
71
|
|
|
static::$type . '_clickable' => array( |
|
72
|
|
|
'label' => __( 'Output as a link?', 'pods' ), |
|
73
|
|
|
'default' => apply_filters( 'pods_form_ui_field_website_clickable', 0, static::$type ), |
|
74
|
|
|
'type' => 'boolean', |
|
75
|
|
|
'dependency' => true, |
|
76
|
|
|
), |
|
77
|
|
|
static::$type . '_new_window' => array( |
|
78
|
|
|
'label' => __( 'Open link in new window?', 'pods' ), |
|
79
|
|
|
'default' => apply_filters( 'pods_form_ui_field_website_new_window', 0, static::$type ), |
|
80
|
|
|
'type' => 'boolean', |
|
81
|
|
|
'depends-on' => array( static::$type . '_clickable' => true ), |
|
82
|
|
|
), |
|
83
|
|
|
static::$type . '_max_length' => array( |
|
84
|
|
|
'label' => __( 'Maximum Length', 'pods' ), |
|
85
|
|
|
'default' => 255, |
|
86
|
|
|
'type' => 'number', |
|
87
|
|
|
'help' => __( 'Set to -1 for no limit', 'pods' ), |
|
88
|
|
|
), |
|
89
|
|
|
static::$type . '_html5' => array( |
|
90
|
|
|
'label' => __( 'Enable HTML5 Input Field?', 'pods' ), |
|
91
|
|
|
'default' => apply_filters( 'pods_form_ui_field_html5', 0, static::$type ), |
|
92
|
|
|
'type' => 'boolean', |
|
93
|
|
|
'excludes-on' => array( static::$type . '_format' => array( 'no-http', 'no-http-no-www', 'no-http-force-www' ) ), |
|
94
|
|
|
), |
|
95
|
|
|
static::$type . '_placeholder' => array( |
|
96
|
|
|
'label' => __( 'HTML Placeholder', 'pods' ), |
|
97
|
|
|
'default' => '', |
|
98
|
|
|
'type' => 'text', |
|
99
|
|
|
'help' => array( |
|
100
|
|
|
__( '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' ), |
|
101
|
|
|
'https://www.w3.org/WAI/tutorials/forms/instructions/#placeholder-text', |
|
102
|
|
|
), |
|
103
|
|
|
), |
|
104
|
|
|
); |
|
105
|
|
|
return $options; |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
/** |
|
109
|
|
|
* {@inheritdoc} |
|
110
|
|
|
*/ |
|
111
|
|
View Code Duplication |
public function schema( $options = null ) { |
|
|
|
|
|
|
112
|
|
|
$length = (int) pods_v( static::$type . '_max_length', $options, 255 ); |
|
113
|
|
|
|
|
114
|
|
|
$schema = 'VARCHAR(' . $length . ')'; |
|
115
|
|
|
|
|
116
|
|
|
if ( 255 < $length || $length < 1 ) { |
|
117
|
|
|
$schema = 'LONGTEXT'; |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
|
|
return $schema; |
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
|
|
/** |
|
124
|
|
|
* {@inheritdoc} |
|
125
|
|
|
*/ |
|
126
|
|
|
public function display( $value = null, $name = null, $options = null, $pod = null, $id = null ) { |
|
127
|
|
|
// Ensure proper format |
|
128
|
|
|
$value = $this->pre_save( $value, $id, $name, $options, null, $pod ); |
|
129
|
|
|
|
|
130
|
|
|
if ( 1 === (int) pods_v( static::$type . '_clickable', $options ) && 0 < strlen( $value ) ) { |
|
131
|
|
|
$link = '<a href="%s"%s>%s</a>'; |
|
132
|
|
|
|
|
133
|
|
|
$atts = ''; |
|
134
|
|
|
|
|
135
|
|
|
if ( 1 === (int) pods_v( static::$type . '_new_window', $options ) ) { |
|
136
|
|
|
$atts .= ' target="_blank"'; |
|
137
|
|
|
} |
|
138
|
|
|
|
|
139
|
|
|
$value = sprintf( $link, esc_url( $value ), $atts, esc_html( $value ) ); |
|
140
|
|
|
} |
|
141
|
|
|
|
|
142
|
|
|
return $value; |
|
143
|
|
|
} |
|
144
|
|
|
|
|
145
|
|
|
/** |
|
146
|
|
|
* {@inheritdoc} |
|
147
|
|
|
*/ |
|
148
|
|
|
public function input( $name, $value = null, $options = null, $pod = null, $id = null ) { |
|
149
|
|
|
$options = (array) $options; |
|
150
|
|
|
$form_field_type = PodsForm::$field_type; |
|
151
|
|
|
|
|
152
|
|
|
// Ensure proper format |
|
153
|
|
|
$value = $this->pre_save( $value, $id, $name, $options, null, $pod ); |
|
154
|
|
|
|
|
155
|
|
|
$field_type = 'website'; |
|
156
|
|
|
|
|
157
|
|
|
if ( isset( $options['name'] ) && false === PodsForm::permission( static::$type, $options['name'], $options, null, $pod, $id ) ) { |
|
158
|
|
|
if ( pods_v( 'read_only', $options, false ) ) { |
|
159
|
|
|
$options['readonly'] = true; |
|
160
|
|
|
|
|
161
|
|
|
$field_type = 'text'; |
|
162
|
|
|
} else { |
|
163
|
|
|
return; |
|
164
|
|
|
} |
|
165
|
|
|
} elseif ( ! pods_has_permissions( $options ) && pods_v( 'read_only', $options, false ) ) { |
|
166
|
|
|
$options['readonly'] = true; |
|
167
|
|
|
|
|
168
|
|
|
$field_type = 'text'; |
|
169
|
|
|
} |
|
170
|
|
|
|
|
171
|
|
|
pods_view( PODS_DIR . 'ui/fields/' . $field_type . '.php', compact( array_keys( get_defined_vars() ) ) ); |
|
172
|
|
|
} |
|
173
|
|
|
|
|
174
|
|
|
/** |
|
175
|
|
|
* {@inheritdoc} |
|
176
|
|
|
*/ |
|
177
|
|
View Code Duplication |
public function validate( $value, $name = null, $options = null, $fields = null, $pod = null, $id = null, $params = null ) { |
|
|
|
|
|
|
178
|
|
|
$errors = array(); |
|
179
|
|
|
|
|
180
|
|
|
$label = strip_tags( pods_v( 'label', $options, ucwords( str_replace( '_', ' ', $name ) ) ) ); |
|
181
|
|
|
|
|
182
|
|
|
$check = $this->pre_save( $value, $id, $name, $options, $fields, $pod, $params ); |
|
183
|
|
|
|
|
184
|
|
|
if ( is_array( $check ) ) { |
|
185
|
|
|
$errors = $check; |
|
186
|
|
|
} else { |
|
187
|
|
|
if ( 0 < strlen( $value ) && '' === $check ) { |
|
188
|
|
|
if ( 1 === (int) pods_v( 'required', $options ) ) { |
|
189
|
|
|
$errors[] = sprintf( __( 'The %s field is required.', 'pods' ), $label ); |
|
190
|
|
|
} else { |
|
191
|
|
|
$errors[] = sprintf( __( 'Invalid website provided for the field %s.', 'pods' ), $label ); |
|
192
|
|
|
} |
|
193
|
|
|
} |
|
194
|
|
|
} |
|
195
|
|
|
|
|
196
|
|
|
if ( ! empty( $errors ) ) { |
|
197
|
|
|
return $errors; |
|
198
|
|
|
} |
|
199
|
|
|
|
|
200
|
|
|
return true; |
|
201
|
|
|
} |
|
202
|
|
|
|
|
203
|
|
|
/** |
|
204
|
|
|
* {@inheritdoc} |
|
205
|
|
|
*/ |
|
206
|
|
|
public function pre_save( $value, $id = null, $name = null, $options = null, $fields = null, $pod = null, $params = null ) { |
|
207
|
|
|
$options = (array) $options; |
|
208
|
|
|
|
|
209
|
|
|
// Update from a array input field (like link) if the field updates |
|
210
|
|
View Code Duplication |
if ( is_array( $value ) ) { |
|
211
|
|
|
if ( isset( $value['url'] ) ) { |
|
212
|
|
|
$value = $value['url']; |
|
213
|
|
|
} else { |
|
214
|
|
|
$value = implode( ' ', $value ); |
|
215
|
|
|
} |
|
216
|
|
|
} |
|
217
|
|
|
|
|
218
|
|
|
$value = $this->validate_url( $value, $options ); |
|
219
|
|
|
|
|
220
|
|
|
$length = (int) pods_v( static::$type . '_max_length', $options, 255 ); |
|
221
|
|
|
|
|
222
|
|
|
if ( 0 < $length && $length < pods_mb_strlen( $value ) ) { |
|
223
|
|
|
$value = pods_mb_substr( $value, 0, $length ); |
|
224
|
|
|
} |
|
225
|
|
|
|
|
226
|
|
|
return $value; |
|
227
|
|
|
} |
|
228
|
|
|
|
|
229
|
|
|
/** |
|
230
|
|
|
* {@inheritdoc} |
|
231
|
|
|
*/ |
|
232
|
|
|
public function ui( $id, $value, $name = null, $options = null, $fields = null, $pod = null ) { |
|
233
|
|
|
$value = $this->display( $value, $name, $options, $pod, $id ); |
|
234
|
|
|
|
|
235
|
|
|
return $value; |
|
236
|
|
|
} |
|
237
|
|
|
|
|
238
|
|
|
/** |
|
239
|
|
|
* Validate an URL with the options |
|
240
|
|
|
* |
|
241
|
|
|
* @param string|array $value Field value. |
|
242
|
|
|
* @param array|null $options Field options. |
|
243
|
|
|
* |
|
244
|
|
|
* @return string |
|
245
|
|
|
* |
|
246
|
|
|
* @since 2.7 |
|
247
|
|
|
*/ |
|
248
|
|
|
public function validate_url( $value, $options = null ) { |
|
249
|
|
|
if ( empty( $value ) ) { |
|
250
|
|
|
return $value; |
|
251
|
|
|
} |
|
252
|
|
|
|
|
253
|
|
|
if ( 'none' === pods_v( static::$type . '_format', $options ) ) { |
|
254
|
|
|
return $this->strip_html( $value, $options ); |
|
255
|
|
|
} |
|
256
|
|
|
|
|
257
|
|
View Code Duplication |
if ( is_array( $value ) ) { |
|
258
|
|
|
if ( isset( $value['scheme'] ) ) { |
|
259
|
|
|
$value = $this->build_url( $value, $options ); |
|
260
|
|
|
} else { |
|
261
|
|
|
$value = @implode( '', $value ); |
|
262
|
|
|
} |
|
263
|
|
|
} |
|
264
|
|
|
|
|
265
|
|
|
if ( false === strpos( $value, '://' ) && 0 !== strpos( $value, '//' ) ) { |
|
266
|
|
|
$value = 'http://' . $value; |
|
267
|
|
|
} |
|
268
|
|
|
|
|
269
|
|
|
$url = wp_parse_url( $value ); |
|
270
|
|
|
|
|
271
|
|
|
if ( empty( $url ) || count( $url ) < 2 ) { |
|
272
|
|
|
$value = ''; |
|
273
|
|
|
} else { |
|
274
|
|
|
$defaults = array( |
|
275
|
|
|
'scheme' => 'http', |
|
276
|
|
|
'host' => '', |
|
277
|
|
|
'port' => '', |
|
278
|
|
|
'path' => '/', |
|
279
|
|
|
'query' => '', |
|
280
|
|
|
'fragment' => '', |
|
281
|
|
|
); |
|
282
|
|
|
|
|
283
|
|
|
$url = array_merge( $defaults, $url ); |
|
284
|
|
|
|
|
285
|
|
|
if ( 'normal' === pods_v( static::$type . '_format', $options ) ) { |
|
286
|
|
|
$value = $this->build_url( $url, $options ); |
|
287
|
|
|
} elseif ( 'no-www' === pods_v( static::$type . '_format', $options ) ) { |
|
288
|
|
View Code Duplication |
if ( 0 === strpos( $url['host'], 'www.' ) ) { |
|
289
|
|
|
$url['host'] = substr( $url['host'], 4 ); |
|
290
|
|
|
} |
|
291
|
|
|
|
|
292
|
|
|
$value = $this->build_url( $url, $options ); |
|
293
|
|
|
} elseif ( 'force-www' === pods_v( static::$type . '_format', $options ) ) { |
|
294
|
|
View Code Duplication |
if ( false !== strpos( $url['host'], '.' ) && false === strpos( $url['host'], '.', 1 ) ) { |
|
295
|
|
|
$url['host'] = 'www.' . $url['host']; |
|
296
|
|
|
} |
|
297
|
|
|
|
|
298
|
|
|
$value = $this->build_url( $url, $options ); |
|
299
|
|
|
} elseif ( 'no-http' === pods_v( static::$type . '_format', $options ) ) { |
|
300
|
|
|
$value = $this->build_url( $url, $options ); |
|
301
|
|
|
$value = str_replace( trim( $url['scheme'] . '://', ':' ), '', $value ); |
|
302
|
|
|
|
|
303
|
|
|
if ( '/' === $url['path'] ) { |
|
304
|
|
|
$value = trim( $value, '/' ); |
|
305
|
|
|
} |
|
306
|
|
|
} elseif ( 'no-http-no-www' === pods_v( static::$type . '_format', $options ) ) { |
|
307
|
|
View Code Duplication |
if ( 0 === strpos( $url['host'], 'www.' ) ) { |
|
308
|
|
|
$url['host'] = substr( $url['host'], 4 ); |
|
309
|
|
|
} |
|
310
|
|
|
|
|
311
|
|
|
$value = $this->build_url( $url, $options ); |
|
312
|
|
|
$value = str_replace( trim( $url['scheme'] . '://', ':' ), '', $value ); |
|
313
|
|
|
|
|
314
|
|
|
if ( '/' === $url['path'] ) { |
|
315
|
|
|
$value = trim( $value, '/' ); |
|
316
|
|
|
} |
|
317
|
|
|
} elseif ( 'no-http-force-www' === pods_v( static::$type . '_format', $options ) ) { |
|
318
|
|
View Code Duplication |
if ( false !== strpos( $url['host'], '.' ) && false === strpos( $url['host'], '.', 1 ) ) { |
|
319
|
|
|
$url['host'] = 'www.' . $url['host']; |
|
320
|
|
|
} |
|
321
|
|
|
|
|
322
|
|
|
$value = $this->build_url( $url, $options ); |
|
323
|
|
|
$value = str_replace( trim( $url['scheme'] . '://', ':' ), '', $value ); |
|
324
|
|
|
|
|
325
|
|
|
if ( '/' === $url['path'] ) { |
|
326
|
|
|
$value = trim( $value, '/' ); |
|
327
|
|
|
} |
|
328
|
|
|
}//end if |
|
329
|
|
|
}//end if |
|
330
|
|
|
|
|
331
|
|
|
return $value; |
|
332
|
|
|
} |
|
333
|
|
|
|
|
334
|
|
|
/** |
|
335
|
|
|
* Validate an target attribute with the options |
|
336
|
|
|
* |
|
337
|
|
|
* @param string $value Field value. |
|
338
|
|
|
* |
|
339
|
|
|
* @return string |
|
340
|
|
|
* |
|
341
|
|
|
* @since 2.7 |
|
342
|
|
|
*/ |
|
343
|
|
|
public function validate_target( $value ) { |
|
344
|
|
|
if ( ! empty( $value ) && '_blank' === $value ) { |
|
345
|
|
|
$value = '_blank'; |
|
346
|
|
|
} else { |
|
347
|
|
|
$value = ''; |
|
348
|
|
|
} |
|
349
|
|
|
return $value; |
|
350
|
|
|
} |
|
351
|
|
|
|
|
352
|
|
|
/** |
|
353
|
|
|
* Build a url from url parts |
|
354
|
|
|
* |
|
355
|
|
|
* @param array|string $url URL value. |
|
356
|
|
|
* @param array $options Field options. |
|
357
|
|
|
* |
|
358
|
|
|
* @return string |
|
359
|
|
|
*/ |
|
360
|
|
|
public function build_url( $url, $options = array() ) { |
|
361
|
|
|
|
|
362
|
|
|
$url = (array) $url; |
|
363
|
|
|
|
|
364
|
|
|
$allow_port = (int) pods_v( static::$type . '_allow_port', $options, 0 ); |
|
365
|
|
|
|
|
366
|
|
|
// If port is not allowed, always set to empty |
|
367
|
|
|
if ( 0 === $allow_port ) { |
|
368
|
|
|
$url['port'] = ''; |
|
369
|
|
|
} |
|
370
|
|
|
|
|
371
|
|
|
if ( function_exists( 'http_build_url' ) ) { |
|
372
|
|
|
return http_build_url( $url ); |
|
373
|
|
|
} |
|
374
|
|
|
|
|
375
|
|
|
$defaults = array( |
|
376
|
|
|
'scheme' => 'http', |
|
377
|
|
|
'host' => '', |
|
378
|
|
|
'port' => '', |
|
379
|
|
|
'path' => '/', |
|
380
|
|
|
'query' => '', |
|
381
|
|
|
'fragment' => '', |
|
382
|
|
|
); |
|
383
|
|
|
|
|
384
|
|
|
$url = array_merge( $defaults, $url ); |
|
385
|
|
|
|
|
386
|
|
|
$new_url = array(); |
|
387
|
|
|
|
|
388
|
|
|
$new_url[] = trim( $url['scheme'] . '://', ':' ); |
|
389
|
|
|
$new_url[] = $url['host']; |
|
390
|
|
|
|
|
391
|
|
|
if ( ! empty( $url['port'] ) ) { |
|
392
|
|
|
$new_url[] = ':' . $url['port']; |
|
393
|
|
|
} |
|
394
|
|
|
|
|
395
|
|
|
$new_url[] = '/' . ltrim( $url['path'], '/' ); |
|
396
|
|
|
|
|
397
|
|
|
if ( ! empty( $url['query'] ) ) { |
|
398
|
|
|
$new_url[] = '?' . ltrim( $url['query'], '?' ); |
|
399
|
|
|
} |
|
400
|
|
|
|
|
401
|
|
|
if ( ! empty( $url['fragment'] ) ) { |
|
402
|
|
|
$new_url[] = '#' . ltrim( $url['fragment'], '#' ); |
|
403
|
|
|
} |
|
404
|
|
|
|
|
405
|
|
|
// Pull all of the parts back together |
|
406
|
|
|
$new_url = implode( '', $new_url ); |
|
407
|
|
|
|
|
408
|
|
|
return $new_url; |
|
409
|
|
|
|
|
410
|
|
|
} |
|
411
|
|
|
|
|
412
|
|
|
} |
|
413
|
|
|
|
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.