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