PluginProbe ʕ •ᴥ•ʔ
Pods – Custom Content Types and Fields / 3.3.8
Pods – Custom Content Types and Fields v3.3.8
trunk 1.14.8 2.7.31.3 2.8.23.3 2.9.19.3 3.0.10.3 3.1.4.1 3.2.0 3.2.1 3.2.1.1 3.2.2 3.2.4 3.2.5 3.2.6 3.2.7 3.2.7.1 3.2.8 3.2.8.1 3.2.8.2 3.3.0 3.3.1 3.3.2 3.3.3 3.3.4 3.3.5 3.3.6 3.3.7 3.3.8 3.3.9
pods / classes / fields / website.php
pods / classes / fields Last commit date
avatar.php 4 months ago boolean.php 4 months ago code.php 4 months ago color.php 4 months ago comment.php 4 months ago currency.php 4 months ago date.php 4 months ago datetime.php 4 months ago email.php 4 months ago file.php 4 months ago heading.php 4 months ago html.php 4 months ago link.php 4 months ago number.php 4 months ago oembed.php 4 months ago paragraph.php 4 months ago password.php 4 months ago phone.php 4 months ago pick.php 4 months ago slug.php 4 months ago taxonomy.php 4 months ago text.php 4 months ago time.php 4 months ago website.php 4 months ago wysiwyg.php 4 months ago
website.php
465 lines
1 <?php
2
3 // Don't load directly.
4 if ( ! defined( 'ABSPATH' ) ) {
5 die( '-1' );
6 }
7
8 /**
9 * @package Pods\Fields
10 */
11 class PodsField_Website extends PodsField {
12
13 /**
14 * {@inheritdoc}
15 */
16 public static $group = 'Text';
17
18 /**
19 * {@inheritdoc}
20 */
21 public static $type = 'website';
22
23 /**
24 * {@inheritdoc}
25 */
26 public static $label = 'Website';
27
28 /**
29 * {@inheritdoc}
30 */
31 public static $prepare = '%s';
32
33 /**
34 * {@inheritdoc}
35 */
36 public function setup() {
37
38 static::$group = __( 'Text', 'pods' );
39 static::$label = __( 'Website', 'pods' );
40
41 }
42
43 /**
44 * {@inheritdoc}
45 */
46 public function options() {
47 $options = [
48 static::$type . '_format' => [
49 'label' => __( 'Format', 'pods' ),
50 'default' => 'normal',
51 'type' => 'pick',
52 'data' => [
53 'normal' => __( 'https://example.com/', 'pods' ),
54 'no-www' => __( 'https://example.com/ (remove www)', 'pods' ),
55 'force-www' => __( 'https://www.example.com/ (force www if no sub-domain provided)', 'pods' ),
56 'no-http' => __( 'example.com', 'pods' ),
57 'no-http-no-www' => __( 'example.com (force removal of www)', 'pods' ),
58 'no-http-force-www' => __( 'www.example.com (force www if no sub-domain provided)', 'pods' ),
59 'none' => __( 'No format', 'pods' ),
60 ],
61 'pick_format_single' => 'dropdown',
62 'pick_show_select_text' => 0,
63 'dependency' => true,
64 ],
65 static::$type . '_allow_port' => [
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' => [
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' => [
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' => [ static::$type . '_clickable' => true ],
82 ],
83 static::$type . '_nofollow' => [
84 'label' => __( 'Make link "nofollow" to exclude from search engines', 'pods' ),
85 'default' => apply_filters( 'pods_form_ui_field_website_nofollow', 0, static::$type ),
86 'type' => 'boolean',
87 'depends-on' => [ static::$type . '_clickable' => true ],
88 ],
89 static::$type . '_max_length' => [
90 'label' => __( 'Maximum Length', 'pods' ),
91 'default' => 255,
92 'type' => 'number',
93 'help' => __( 'Set to -1 for no limit', 'pods' ),
94 ],
95 static::$type . '_html5' => [
96 'label' => __( 'Enable HTML5 Input Field', 'pods' ),
97 'default' => apply_filters( 'pods_form_ui_field_html5', 0, static::$type ),
98 'type' => 'boolean',
99 'excludes-on' => [ static::$type . '_format' => [ 'no-http', 'no-http-no-www', 'no-http-force-www' ] ],
100 ],
101 static::$type . '_placeholder' => [
102 'label' => __( 'HTML Placeholder', 'pods' ),
103 'default' => '',
104 'type' => 'text',
105 'help' => [
106 __( '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' ),
107 'https://www.w3.org/WAI/tutorials/forms/instructions/#placeholder-text',
108 ],
109 ],
110 ];
111
112 return $options;
113 }
114
115 /**
116 * {@inheritdoc}
117 */
118 public function schema( $options = null ) {
119 $length = (int) pods_v( static::$type . '_max_length', $options, 255 );
120
121 $schema = 'VARCHAR(' . $length . ')';
122
123 if ( 255 < $length || $length < 1 ) {
124 $schema = 'LONGTEXT';
125 }
126
127 return $schema;
128 }
129
130 /**
131 * {@inheritdoc}
132 */
133 public function display( $value = null, $name = null, $options = null, $pod = null, $id = null ) {
134 // Ensure proper format
135 $value = $this->pre_save( $value, $id, $name, $options, null, $pod );
136
137 if ( 1 === (int) pods_v( static::$type . '_clickable', $options ) && 0 < strlen( (string) $value ) ) {
138 $link = '<a href="%s"%s>%s</a>';
139
140 $atts = '';
141 $rel = [];
142
143 if ( 1 === (int) pods_v( static::$type . '_nofollow', $options ) ) {
144 $rel[] = 'nofollow';
145 }
146
147 if ( 1 === (int) pods_v( static::$type . '_new_window', $options ) ) {
148 $rel[] = 'noopener';
149 $rel[] = 'noreferrer';
150
151 $atts .= ' target="_blank"';
152 }
153
154 if ( ! empty( $rel ) ) {
155 $atts .= ' rel="' . esc_attr( implode( ' ', $rel ) ) . '"';
156 }
157
158 $value = sprintf( $link, esc_url( $value ), $atts, esc_html( $value ) );
159 }
160
161 return $value;
162 }
163
164 /**
165 * {@inheritdoc}
166 */
167 public function input( $name, $value = null, $options = null, $pod = null, $id = null ) {
168 $options = ( is_array( $options ) || is_object( $options ) ) ? $options : (array) $options;
169 $form_field_type = PodsForm::$field_type;
170
171 $value = $this->normalize_value_for_input( $value, $options );
172
173 // Ensure proper format
174 if ( is_array( $value ) ) {
175 foreach ( $value as $k => $repeatable_value ) {
176 $value[ $k ] = $this->pre_save( $repeatable_value, $id, $name, $options, null, $pod );
177 }
178 } else {
179 $value = $this->pre_save( $value, $id, $name, $options, null, $pod );
180 }
181
182 $field_type = 'website';
183
184 if ( isset( $options['name'] ) && ! pods_permission( $options ) ) {
185 if ( pods_v_bool( 'read_only_restricted', $options ) ) {
186 $options['readonly'] = true;
187
188 $field_type = 'text';
189 } else {
190 return;
191 }
192 } elseif ( ! pods_has_permissions( $options ) ) {
193 if ( pods_v_bool( 'read_only', $options ) ) {
194 $options['readonly'] = true;
195
196 $field_type = 'text';
197 }
198 }
199
200 if ( ! empty( $options['disable_dfv'] ) ) {
201 return pods_view( PODS_DIR . 'ui/fields/' . $field_type . '.php', compact( array_keys( get_defined_vars() ) ) );
202 }
203
204 $type = pods_v( 'type', $options, static::$type );
205
206 $args = compact( array_keys( get_defined_vars() ) );
207 $args = (object) $args;
208
209 $this->render_input_script( $args );
210 }
211
212 /**
213 * {@inheritdoc}
214 */
215 public function validate( $value, $name = null, $options = null, $fields = null, $pod = null, $id = null, $params = null ) {
216 $validate = parent::validate( $value, $name, $options, $fields, $pod, $id, $params );
217
218 $errors = [];
219
220 if ( is_array( $validate ) ) {
221 $errors = $validate;
222 }
223
224 $label = wp_strip_all_tags( pods_v( 'label', $options, ucwords( str_replace( '_', ' ', $name ) ) ) );
225
226 $check = $this->pre_save( $value, $id, $name, $options, $fields, $pod, $params );
227
228 if ( is_array( $check ) ) {
229 $errors = $check;
230 } else {
231 if ( 0 < strlen( (string) $value ) && '' === $check ) {
232 if ( $this->is_required( $options ) ) {
233 // translators: %s is the field label.
234 $errors[] = sprintf( __( 'The %s field is required.', 'pods' ), $label );
235 } else {
236 // translators: %s is the field label.
237 $errors[] = sprintf( __( 'Invalid website provided for the field %s.', 'pods' ), $label );
238 }
239 }
240 }
241
242 if ( ! empty( $errors ) ) {
243 return $errors;
244 }
245
246 return $validate;
247 }
248
249 /**
250 * {@inheritdoc}
251 */
252 public function pre_save( $value, $id = null, $name = null, $options = null, $fields = null, $pod = null, $params = null ) {
253 $options = ( is_array( $options ) || is_object( $options ) ) ? $options : (array) $options;
254
255 // Update from an array input field (like link) if the field updates
256 if ( is_array( $value ) ) {
257 if ( isset( $value['url'] ) ) {
258 $value = $value['url'];
259 } else {
260 $value = $this->normalize_value_for_input( $value, $options );
261
262 // @todo Eventually rework this further.
263 }
264 }
265
266 $value = $this->validate_url( $value, $options );
267
268 $length = (int) pods_v( static::$type . '_max_length', $options, 255 );
269
270 if ( 0 < $length && $length < pods_mb_strlen( $value ) ) {
271 $value = pods_mb_substr( $value, 0, $length );
272 }
273
274 return $value;
275 }
276
277 /**
278 * Validate an URL with the options
279 *
280 * @param string|array $value Field value.
281 * @param array|null $options Field options.
282 *
283 * @return string
284 *
285 * @since 2.7.0
286 */
287 public function validate_url( $value, $options = null ) {
288 if ( empty( $value ) ) {
289 return $value;
290 }
291
292 if ( 'none' === pods_v( static::$type . '_format', $options ) ) {
293 $value = $this->strip_html( $value, $options );
294 $value = $this->strip_shortcodes( $value, $options );
295 $value = $this->trim_whitespace( $value, $options );
296
297 return $value;
298 }
299
300 if ( is_array( $value ) ) {
301 if ( isset( $value['scheme'] ) ) {
302 $value = $this->build_url( $value, $options );
303 } else {
304 $value = @implode( '', $value );
305 }
306 }
307
308 if ( false === strpos( (string) $value, '://' ) && 0 !== strpos( (string) $value, '//' ) ) {
309 $value = 'http://' . $value;
310 }
311
312 $url = wp_parse_url( $value );
313
314 if ( empty( $url ) || count( $url ) < 2 ) {
315 $value = '';
316 } else {
317 $defaults = [
318 'scheme' => 'http',
319 'host' => '',
320 'port' => '',
321 'path' => '/',
322 'query' => '',
323 'fragment' => '',
324 ];
325
326 $url = array_merge( $defaults, $url );
327
328 if ( 'normal' === pods_v( static::$type . '_format', $options ) ) {
329 $value = $this->build_url( $url, $options );
330 } elseif ( 'no-www' === pods_v( static::$type . '_format', $options ) ) {
331 if ( 0 === strpos( (string) $url['host'], 'www.' ) ) {
332 $url['host'] = substr( (string) $url['host'], 4 );
333 }
334
335 $value = $this->build_url( $url, $options );
336 } elseif ( 'force-www' === pods_v( static::$type . '_format', $options ) ) {
337 if ( false !== strpos( (string) $url['host'], '.' ) && false === strpos( (string) $url['host'], 'www', 0 ) ) {
338 $url['host'] = 'www.' . $url['host'];
339 }
340
341 $value = $this->build_url( $url, $options );
342 } elseif ( 'no-http' === pods_v( static::$type . '_format', $options ) ) {
343 $value = $this->build_url( $url, $options );
344 $value = str_replace( trim( $url['scheme'] . '://', ':' ), '', $value );
345
346 if ( '/' === $url['path'] ) {
347 $value = trim( $value, '/' );
348 }
349 } elseif ( 'no-http-no-www' === pods_v( static::$type . '_format', $options ) ) {
350 if ( 0 === strpos( (string) $url['host'], 'www.' ) ) {
351 $url['host'] = substr( (string) $url['host'], 4 );
352 }
353
354 $value = $this->build_url( $url, $options );
355 $value = str_replace( trim( $url['scheme'] . '://', ':' ), '', $value );
356
357 if ( '/' === $url['path'] ) {
358 $value = trim( $value, '/' );
359 }
360 } elseif ( 'no-http-force-www' === pods_v( static::$type . '_format', $options ) ) {
361 if ( false !== strpos( (string) $url['host'], '.' ) && false === strpos( (string) $url['host'], 'www', 0 ) ) {
362 $url['host'] = 'www.' . $url['host'];
363 }
364
365 $value = $this->build_url( $url, $options );
366 $value = str_replace( trim( $url['scheme'] . '://', ':' ), '', $value );
367
368 if ( '/' === $url['path'] ) {
369 $value = trim( $value, '/' );
370 }
371 }//end if
372 }//end if
373
374 return $value;
375 }
376
377 /**
378 * Validate an target attribute with the options
379 *
380 * @param string $value Field value.
381 *
382 * @return string
383 *
384 * @since 2.7.0
385 */
386 public function validate_target( $value ) {
387 if ( ! empty( $value ) && '_blank' === $value ) {
388 $value = '_blank';
389 } else {
390 $value = '';
391 }
392
393 return $value;
394 }
395
396 /**
397 * Build a url from url parts
398 *
399 * @param array|string $url URL value.
400 * @param array $options Field options.
401 *
402 * @return string
403 */
404 public function build_url( $url, $options = [] ) {
405
406 $url = (array) $url;
407
408 $allow_port = (int) pods_v( static::$type . '_allow_port', $options, 0 );
409
410 // If port is not allowed, always set to empty
411 if ( 0 === $allow_port ) {
412 $url['port'] = '';
413 }
414
415 if ( function_exists( 'http_build_url' ) ) {
416 return http_build_url( $url );
417 }
418
419 $defaults = [
420 'scheme' => 'http',
421 'host' => '',
422 'port' => '',
423 'path' => '/',
424 'query' => '',
425 'fragment' => '',
426 ];
427
428 $url = array_merge( $defaults, $url );
429
430 $new_url = [];
431
432 $new_url[] = trim( $url['scheme'] . '://', ':' );
433 $new_url[] = $url['host'];
434
435 if ( ! empty( $url['port'] ) ) {
436 $new_url[] = ':' . $url['port'];
437 }
438
439 $new_url[] = '/' . ltrim( $url['path'], '/' );
440
441 if ( isset( $url['query'] ) ) {
442 $query = ltrim( $url['query'], '?' );
443
444 if ( ! empty( $query ) ) {
445 $new_url[] = '?' . $query;
446 }
447 }
448
449 if ( isset( $url['fragment'] ) ) {
450 $fragment = ltrim( $url['fragment'], '#' );
451
452 if ( ! empty( $fragment ) ) {
453 $new_url[] = '#' . $fragment;
454 }
455 }
456
457 // Pull all of the parts back together
458 $new_url = implode( '', $new_url );
459
460 return $new_url;
461
462 }
463
464 }
465