|
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
|
|
|
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
|
|
|
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
|
|
|
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
|
|
|
* Validate an URL with the options |
|
231
|
|
|
* |
|
232
|
|
|
* @param string|array $value Field value. |
|
233
|
|
|
* @param array|null $options Field options. |
|
234
|
|
|
* |
|
235
|
|
|
* @return string |
|
236
|
|
|
* |
|
237
|
|
|
* @since 2.7 |
|
238
|
|
|
*/ |
|
239
|
|
|
public function validate_url( $value, $options = null ) { |
|
240
|
|
|
if ( empty( $value ) ) { |
|
241
|
|
|
return $value; |
|
|
|
|
|
|
242
|
|
|
} |
|
243
|
|
|
|
|
244
|
|
|
if ( 'none' === pods_v( static::$type . '_format', $options ) ) { |
|
245
|
|
|
return $this->strip_html( $value, $options ); |
|
246
|
|
|
} |
|
247
|
|
|
|
|
248
|
|
|
if ( is_array( $value ) ) { |
|
249
|
|
|
if ( isset( $value['scheme'] ) ) { |
|
250
|
|
|
$value = $this->build_url( $value, $options ); |
|
251
|
|
|
} else { |
|
252
|
|
|
$value = @implode( '', $value ); |
|
|
|
|
|
|
253
|
|
|
} |
|
254
|
|
|
} |
|
255
|
|
|
|
|
256
|
|
|
if ( false === strpos( $value, '://' ) && 0 !== strpos( $value, '//' ) ) { |
|
257
|
|
|
$value = 'http://' . $value; |
|
258
|
|
|
} |
|
259
|
|
|
|
|
260
|
|
|
$url = wp_parse_url( $value ); |
|
261
|
|
|
|
|
262
|
|
|
if ( empty( $url ) || count( $url ) < 2 ) { |
|
263
|
|
|
$value = ''; |
|
264
|
|
|
} else { |
|
265
|
|
|
$defaults = array( |
|
266
|
|
|
'scheme' => 'http', |
|
267
|
|
|
'host' => '', |
|
268
|
|
|
'port' => '', |
|
269
|
|
|
'path' => '/', |
|
270
|
|
|
'query' => '', |
|
271
|
|
|
'fragment' => '', |
|
272
|
|
|
); |
|
273
|
|
|
|
|
274
|
|
|
$url = array_merge( $defaults, $url ); |
|
275
|
|
|
|
|
276
|
|
|
if ( 'normal' === pods_v( static::$type . '_format', $options ) ) { |
|
277
|
|
|
$value = $this->build_url( $url, $options ); |
|
278
|
|
|
} elseif ( 'no-www' === pods_v( static::$type . '_format', $options ) ) { |
|
279
|
|
|
if ( 0 === strpos( $url['host'], 'www.' ) ) { |
|
280
|
|
|
$url['host'] = substr( $url['host'], 4 ); |
|
281
|
|
|
} |
|
282
|
|
|
|
|
283
|
|
|
$value = $this->build_url( $url, $options ); |
|
284
|
|
|
} elseif ( 'force-www' === pods_v( static::$type . '_format', $options ) ) { |
|
285
|
|
|
if ( false !== strpos( $url['host'], '.' ) && false === strpos( $url['host'], '.', 1 ) ) { |
|
286
|
|
|
$url['host'] = 'www.' . $url['host']; |
|
287
|
|
|
} |
|
288
|
|
|
|
|
289
|
|
|
$value = $this->build_url( $url, $options ); |
|
290
|
|
|
} elseif ( 'no-http' === pods_v( static::$type . '_format', $options ) ) { |
|
291
|
|
|
$value = $this->build_url( $url, $options ); |
|
292
|
|
|
$value = str_replace( trim( $url['scheme'] . '://', ':' ), '', $value ); |
|
293
|
|
|
|
|
294
|
|
|
if ( '/' === $url['path'] ) { |
|
295
|
|
|
$value = trim( $value, '/' ); |
|
296
|
|
|
} |
|
297
|
|
|
} elseif ( 'no-http-no-www' === pods_v( static::$type . '_format', $options ) ) { |
|
298
|
|
|
if ( 0 === strpos( $url['host'], 'www.' ) ) { |
|
299
|
|
|
$url['host'] = substr( $url['host'], 4 ); |
|
300
|
|
|
} |
|
301
|
|
|
|
|
302
|
|
|
$value = $this->build_url( $url, $options ); |
|
303
|
|
|
$value = str_replace( trim( $url['scheme'] . '://', ':' ), '', $value ); |
|
304
|
|
|
|
|
305
|
|
|
if ( '/' === $url['path'] ) { |
|
306
|
|
|
$value = trim( $value, '/' ); |
|
307
|
|
|
} |
|
308
|
|
|
} elseif ( 'no-http-force-www' === pods_v( static::$type . '_format', $options ) ) { |
|
309
|
|
|
if ( false !== strpos( $url['host'], '.' ) && false === strpos( $url['host'], '.', 1 ) ) { |
|
310
|
|
|
$url['host'] = 'www.' . $url['host']; |
|
311
|
|
|
} |
|
312
|
|
|
|
|
313
|
|
|
$value = $this->build_url( $url, $options ); |
|
314
|
|
|
$value = str_replace( trim( $url['scheme'] . '://', ':' ), '', $value ); |
|
315
|
|
|
|
|
316
|
|
|
if ( '/' === $url['path'] ) { |
|
317
|
|
|
$value = trim( $value, '/' ); |
|
318
|
|
|
} |
|
319
|
|
|
}//end if |
|
320
|
|
|
}//end if |
|
321
|
|
|
|
|
322
|
|
|
return $value; |
|
323
|
|
|
} |
|
324
|
|
|
|
|
325
|
|
|
/** |
|
326
|
|
|
* Validate an target attribute with the options |
|
327
|
|
|
* |
|
328
|
|
|
* @param string $value Field value. |
|
329
|
|
|
* |
|
330
|
|
|
* @return string |
|
331
|
|
|
* |
|
332
|
|
|
* @since 2.7 |
|
333
|
|
|
*/ |
|
334
|
|
|
public function validate_target( $value ) { |
|
335
|
|
|
if ( ! empty( $value ) && '_blank' === $value ) { |
|
336
|
|
|
$value = '_blank'; |
|
337
|
|
|
} else { |
|
338
|
|
|
$value = ''; |
|
339
|
|
|
} |
|
340
|
|
|
return $value; |
|
341
|
|
|
} |
|
342
|
|
|
|
|
343
|
|
|
/** |
|
344
|
|
|
* Build a url from url parts |
|
345
|
|
|
* |
|
346
|
|
|
* @param array|string $url URL value. |
|
347
|
|
|
* @param array $options Field options. |
|
348
|
|
|
* |
|
349
|
|
|
* @return string |
|
350
|
|
|
*/ |
|
351
|
|
|
public function build_url( $url, $options = array() ) { |
|
352
|
|
|
|
|
353
|
|
|
$url = (array) $url; |
|
354
|
|
|
|
|
355
|
|
|
$allow_port = (int) pods_v( static::$type . '_allow_port', $options, 0 ); |
|
356
|
|
|
|
|
357
|
|
|
// If port is not allowed, always set to empty |
|
358
|
|
|
if ( 0 === $allow_port ) { |
|
359
|
|
|
$url['port'] = ''; |
|
360
|
|
|
} |
|
361
|
|
|
|
|
362
|
|
|
if ( function_exists( 'http_build_url' ) ) { |
|
363
|
|
|
return http_build_url( $url ); |
|
364
|
|
|
} |
|
365
|
|
|
|
|
366
|
|
|
$defaults = array( |
|
367
|
|
|
'scheme' => 'http', |
|
368
|
|
|
'host' => '', |
|
369
|
|
|
'port' => '', |
|
370
|
|
|
'path' => '/', |
|
371
|
|
|
'query' => '', |
|
372
|
|
|
'fragment' => '', |
|
373
|
|
|
); |
|
374
|
|
|
|
|
375
|
|
|
$url = array_merge( $defaults, $url ); |
|
376
|
|
|
|
|
377
|
|
|
$new_url = array(); |
|
378
|
|
|
|
|
379
|
|
|
$new_url[] = trim( $url['scheme'] . '://', ':' ); |
|
380
|
|
|
$new_url[] = $url['host']; |
|
381
|
|
|
|
|
382
|
|
|
if ( ! empty( $url['port'] ) ) { |
|
383
|
|
|
$new_url[] = ':' . $url['port']; |
|
384
|
|
|
} |
|
385
|
|
|
|
|
386
|
|
|
$new_url[] = '/' . ltrim( $url['path'], '/' ); |
|
387
|
|
|
|
|
388
|
|
|
if ( ! empty( $url['query'] ) ) { |
|
389
|
|
|
$new_url[] = '?' . ltrim( $url['query'], '?' ); |
|
390
|
|
|
} |
|
391
|
|
|
|
|
392
|
|
|
if ( ! empty( $url['fragment'] ) ) { |
|
393
|
|
|
$new_url[] = '#' . ltrim( $url['fragment'], '#' ); |
|
394
|
|
|
} |
|
395
|
|
|
|
|
396
|
|
|
// Pull all of the parts back together |
|
397
|
|
|
$new_url = implode( '', $new_url ); |
|
398
|
|
|
|
|
399
|
|
|
return $new_url; |
|
400
|
|
|
|
|
401
|
|
|
} |
|
402
|
|
|
|
|
403
|
|
|
} |
|
404
|
|
|
|
If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.
Let’s take a look at an example:
Our function
my_functionexpects aPostobject, and outputs the author of the post. The base classPostreturns a simple string and outputting a simple string will work just fine. However, the child classBlogPostwhich is a sub-type ofPostinstead decided to return anobject, and is therefore violating the SOLID principles. If aBlogPostwere passed tomy_function, PHP would not complain, but ultimately fail when executing thestrtouppercall in its body.