PluginProbe ʕ •ᴥ•ʔ
Pods – Custom Content Types and Fields / 2.7.31.4
Pods – Custom Content Types and Fields v2.7.31.4
2.7.31.4 2.8.23.5 2.9.19.5 3.0.10.5 3.1.4.3 3.2.8.4 3.3.9.2 2.8.23.4 2.9.19.4 3.0.10.4 3.1.4.2 3.2.8.3 3.3.9.1 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 2 days ago boolean.php 2 days ago code.php 2 days ago color.php 2 days ago comment.php 2 days ago currency.php 2 days ago date.php 2 days ago datetime.php 2 days ago email.php 2 days ago file.php 2 days ago html.php 2 days ago link.php 2 days ago number.php 2 days ago oembed.php 2 days ago paragraph.php 2 days ago password.php 2 days ago phone.php 2 days ago pick.php 2 days ago slug.php 2 days ago taxonomy.php 2 days ago text.php 2 days ago time.php 2 days ago website.php 2 days ago wysiwyg.php 2 days ago
website.php
410 lines
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" rel="noopener noreferrer"';
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 $validate = parent::validate( $value, $name, $options, $fields, $pod, $id, $params );
179
180 $errors = array();
181
182 if ( is_array( $validate ) ) {
183 $errors = $validate;
184 }
185
186 $label = strip_tags( pods_v( 'label', $options, ucwords( str_replace( '_', ' ', $name ) ) ) );
187
188 $check = $this->pre_save( $value, $id, $name, $options, $fields, $pod, $params );
189
190 if ( is_array( $check ) ) {
191 $errors = $check;
192 } else {
193 if ( 0 < strlen( $value ) && '' === $check ) {
194 if ( $this->is_required( $options ) ) {
195 $errors[] = sprintf( __( 'The %s field is required.', 'pods' ), $label );
196 } else {
197 $errors[] = sprintf( __( 'Invalid website provided for the field %s.', 'pods' ), $label );
198 }
199 }
200 }
201
202 if ( ! empty( $errors ) ) {
203 return $errors;
204 }
205
206 return $validate;
207 }
208
209 /**
210 * {@inheritdoc}
211 */
212 public function pre_save( $value, $id = null, $name = null, $options = null, $fields = null, $pod = null, $params = null ) {
213 $options = (array) $options;
214
215 // Update from a array input field (like link) if the field updates
216 if ( is_array( $value ) ) {
217 if ( isset( $value['url'] ) ) {
218 $value = $value['url'];
219 } else {
220 $value = implode( ' ', $value );
221 }
222 }
223
224 $value = $this->validate_url( $value, $options );
225
226 $length = (int) pods_v( static::$type . '_max_length', $options, 255 );
227
228 if ( 0 < $length && $length < pods_mb_strlen( $value ) ) {
229 $value = pods_mb_substr( $value, 0, $length );
230 }
231
232 return $value;
233 }
234
235 /**
236 * Validate an URL with the options
237 *
238 * @param string|array $value Field value.
239 * @param array|null $options Field options.
240 *
241 * @return string
242 *
243 * @since 2.7.0
244 */
245 public function validate_url( $value, $options = null ) {
246 if ( empty( $value ) ) {
247 return $value;
248 }
249
250 if ( 'none' === pods_v( static::$type . '_format', $options ) ) {
251 return $this->strip_html( $value, $options );
252 }
253
254 if ( is_array( $value ) ) {
255 if ( isset( $value['scheme'] ) ) {
256 $value = $this->build_url( $value, $options );
257 } else {
258 $value = @implode( '', $value );
259 }
260 }
261
262 if ( false === strpos( $value, '://' ) && 0 !== strpos( $value, '//' ) ) {
263 $value = 'http://' . $value;
264 }
265
266 $url = wp_parse_url( $value );
267
268 if ( empty( $url ) || count( $url ) < 2 ) {
269 $value = '';
270 } else {
271 $defaults = array(
272 'scheme' => 'http',
273 'host' => '',
274 'port' => '',
275 'path' => '/',
276 'query' => '',
277 'fragment' => '',
278 );
279
280 $url = array_merge( $defaults, $url );
281
282 if ( 'normal' === pods_v( static::$type . '_format', $options ) ) {
283 $value = $this->build_url( $url, $options );
284 } elseif ( 'no-www' === pods_v( static::$type . '_format', $options ) ) {
285 if ( 0 === strpos( $url['host'], 'www.' ) ) {
286 $url['host'] = substr( $url['host'], 4 );
287 }
288
289 $value = $this->build_url( $url, $options );
290 } elseif ( 'force-www' === pods_v( static::$type . '_format', $options ) ) {
291 if ( false !== strpos( $url['host'], '.' ) && false === strpos( $url['host'], 'www', 0 ) ) {
292 $url['host'] = 'www.' . $url['host'];
293 }
294
295 $value = $this->build_url( $url, $options );
296 } elseif ( 'no-http' === pods_v( static::$type . '_format', $options ) ) {
297 $value = $this->build_url( $url, $options );
298 $value = str_replace( trim( $url['scheme'] . '://', ':' ), '', $value );
299
300 if ( '/' === $url['path'] ) {
301 $value = trim( $value, '/' );
302 }
303 } elseif ( 'no-http-no-www' === pods_v( static::$type . '_format', $options ) ) {
304 if ( 0 === strpos( $url['host'], 'www.' ) ) {
305 $url['host'] = substr( $url['host'], 4 );
306 }
307
308 $value = $this->build_url( $url, $options );
309 $value = str_replace( trim( $url['scheme'] . '://', ':' ), '', $value );
310
311 if ( '/' === $url['path'] ) {
312 $value = trim( $value, '/' );
313 }
314 } elseif ( 'no-http-force-www' === pods_v( static::$type . '_format', $options ) ) {
315 if ( false !== strpos( $url['host'], '.' ) && false === strpos( $url['host'], 'www', 0 ) ) {
316 $url['host'] = 'www.' . $url['host'];
317 }
318
319 $value = $this->build_url( $url, $options );
320 $value = str_replace( trim( $url['scheme'] . '://', ':' ), '', $value );
321
322 if ( '/' === $url['path'] ) {
323 $value = trim( $value, '/' );
324 }
325 }//end if
326 }//end if
327
328 return $value;
329 }
330
331 /**
332 * Validate an target attribute with the options
333 *
334 * @param string $value Field value.
335 *
336 * @return string
337 *
338 * @since 2.7.0
339 */
340 public function validate_target( $value ) {
341 if ( ! empty( $value ) && '_blank' === $value ) {
342 $value = '_blank';
343 } else {
344 $value = '';
345 }
346 return $value;
347 }
348
349 /**
350 * Build a url from url parts
351 *
352 * @param array|string $url URL value.
353 * @param array $options Field options.
354 *
355 * @return string
356 */
357 public function build_url( $url, $options = array() ) {
358
359 $url = (array) $url;
360
361 $allow_port = (int) pods_v( static::$type . '_allow_port', $options, 0 );
362
363 // If port is not allowed, always set to empty
364 if ( 0 === $allow_port ) {
365 $url['port'] = '';
366 }
367
368 if ( function_exists( 'http_build_url' ) ) {
369 return http_build_url( $url );
370 }
371
372 $defaults = array(
373 'scheme' => 'http',
374 'host' => '',
375 'port' => '',
376 'path' => '/',
377 'query' => '',
378 'fragment' => '',
379 );
380
381 $url = array_merge( $defaults, $url );
382
383 $new_url = array();
384
385 $new_url[] = trim( $url['scheme'] . '://', ':' );
386 $new_url[] = $url['host'];
387
388 if ( ! empty( $url['port'] ) ) {
389 $new_url[] = ':' . $url['port'];
390 }
391
392 $new_url[] = '/' . ltrim( $url['path'], '/' );
393
394 if ( ! empty( $url['query'] ) ) {
395 $new_url[] = '?' . ltrim( $url['query'], '?' );
396 }
397
398 if ( ! empty( $url['fragment'] ) ) {
399 $new_url[] = '#' . ltrim( $url['fragment'], '#' );
400 }
401
402 // Pull all of the parts back together
403 $new_url = implode( '', $new_url );
404
405 return $new_url;
406
407 }
408
409 }
410