avatar.php
3 years ago
boolean.php
3 years ago
code.php
2 years ago
color.php
1 year ago
comment.php
4 years ago
currency.php
9 months ago
date.php
2 years ago
datetime.php
1 year ago
email.php
1 year ago
file.php
11 months ago
heading.php
1 year ago
html.php
2 years ago
link.php
1 year ago
number.php
9 months ago
oembed.php
1 year ago
paragraph.php
2 years ago
password.php
1 year ago
phone.php
1 year ago
pick.php
11 months ago
slug.php
3 years ago
taxonomy.php
4 years ago
text.php
2 years ago
time.php
2 years ago
website.php
11 months ago
wysiwyg.php
1 year ago
website.php
453 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 . '_format' => array( |
| 43 | 'label' => __( 'Format', 'pods' ), |
| 44 | 'default' => 'normal', |
| 45 | 'type' => 'pick', |
| 46 | 'data' => array( |
| 47 | 'normal' => __( 'https://example.com/', 'pods' ), |
| 48 | 'no-www' => __( 'https://example.com/ (remove www)', 'pods' ), |
| 49 | 'force-www' => __( 'https://www.example.com/ (force www if no sub-domain provided)', 'pods' ), |
| 50 | 'no-http' => __( 'example.com', 'pods' ), |
| 51 | 'no-http-no-www' => __( 'example.com (force removal of www)', 'pods' ), |
| 52 | 'no-http-force-www' => __( 'www.example.com (force www if no sub-domain provided)', 'pods' ), |
| 53 | 'none' => __( 'No format', 'pods' ), |
| 54 | ), |
| 55 | 'pick_format_single' => 'dropdown', |
| 56 | 'pick_show_select_text' => 0, |
| 57 | 'dependency' => true, |
| 58 | ), |
| 59 | static::$type . '_allow_port' => array( |
| 60 | 'label' => __( 'Allow port in URL', 'pods' ), |
| 61 | 'default' => apply_filters( 'pods_form_ui_field_website_port', 0, static::$type ), |
| 62 | 'type' => 'boolean', |
| 63 | 'dependency' => true, |
| 64 | ), |
| 65 | static::$type . '_clickable' => array( |
| 66 | 'label' => __( 'Output as a link', 'pods' ), |
| 67 | 'default' => apply_filters( 'pods_form_ui_field_website_clickable', 0, static::$type ), |
| 68 | 'type' => 'boolean', |
| 69 | 'dependency' => true, |
| 70 | ), |
| 71 | static::$type . '_new_window' => array( |
| 72 | 'label' => __( 'Open link in new window', 'pods' ), |
| 73 | 'default' => apply_filters( 'pods_form_ui_field_website_new_window', 0, static::$type ), |
| 74 | 'type' => 'boolean', |
| 75 | 'depends-on' => array( static::$type . '_clickable' => true ), |
| 76 | ), |
| 77 | static::$type . '_nofollow' => array( |
| 78 | 'label' => __( 'Make link "nofollow" to exclude from search engines', 'pods' ), |
| 79 | 'default' => apply_filters( 'pods_form_ui_field_website_nofollow', 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( (string) $value ) ) { |
| 131 | $link = '<a href="%s"%s>%s</a>'; |
| 132 | |
| 133 | $atts = ''; |
| 134 | $rel = []; |
| 135 | |
| 136 | if ( 1 === (int) pods_v( static::$type . '_nofollow', $options ) ) { |
| 137 | $rel[] = 'nofollow'; |
| 138 | } |
| 139 | |
| 140 | if ( 1 === (int) pods_v( static::$type . '_new_window', $options ) ) { |
| 141 | $rel[] = 'noopener'; |
| 142 | $rel[] = 'noreferrer'; |
| 143 | |
| 144 | $atts .= ' target="_blank"'; |
| 145 | } |
| 146 | |
| 147 | if ( ! empty( $rel ) ) { |
| 148 | $atts .= ' rel="' . esc_attr( implode( ' ', $rel ) ) . '"'; |
| 149 | } |
| 150 | |
| 151 | $value = sprintf( $link, esc_url( $value ), $atts, esc_html( $value ) ); |
| 152 | } |
| 153 | |
| 154 | return $value; |
| 155 | } |
| 156 | |
| 157 | /** |
| 158 | * {@inheritdoc} |
| 159 | */ |
| 160 | public function input( $name, $value = null, $options = null, $pod = null, $id = null ) { |
| 161 | $options = ( is_array( $options ) || is_object( $options ) ) ? $options : (array) $options; |
| 162 | $form_field_type = PodsForm::$field_type; |
| 163 | |
| 164 | $value = $this->normalize_value_for_input( $value, $options ); |
| 165 | |
| 166 | // Ensure proper format |
| 167 | if ( is_array( $value ) ) { |
| 168 | foreach ( $value as $k => $repeatable_value ) { |
| 169 | $value[ $k ] = $this->pre_save( $repeatable_value, $id, $name, $options, null, $pod ); |
| 170 | } |
| 171 | } else { |
| 172 | $value = $this->pre_save( $value, $id, $name, $options, null, $pod ); |
| 173 | } |
| 174 | |
| 175 | $field_type = 'website'; |
| 176 | |
| 177 | if ( isset( $options['name'] ) && ! pods_permission( $options ) ) { |
| 178 | if ( pods_v( 'read_only', $options, false ) ) { |
| 179 | $options['readonly'] = true; |
| 180 | |
| 181 | $field_type = 'text'; |
| 182 | } else { |
| 183 | return; |
| 184 | } |
| 185 | } elseif ( ! pods_has_permissions( $options ) && pods_v( 'read_only', $options, false ) ) { |
| 186 | $options['readonly'] = true; |
| 187 | |
| 188 | $field_type = 'text'; |
| 189 | } |
| 190 | |
| 191 | if ( ! empty( $options['disable_dfv'] ) ) { |
| 192 | return pods_view( PODS_DIR . 'ui/fields/' . $field_type . '.php', compact( array_keys( get_defined_vars() ) ) ); |
| 193 | } |
| 194 | |
| 195 | $type = pods_v( 'type', $options, static::$type ); |
| 196 | |
| 197 | $args = compact( array_keys( get_defined_vars() ) ); |
| 198 | $args = (object) $args; |
| 199 | |
| 200 | $this->render_input_script( $args ); |
| 201 | } |
| 202 | |
| 203 | /** |
| 204 | * {@inheritdoc} |
| 205 | */ |
| 206 | public function validate( $value, $name = null, $options = null, $fields = null, $pod = null, $id = null, $params = null ) { |
| 207 | $validate = parent::validate( $value, $name, $options, $fields, $pod, $id, $params ); |
| 208 | |
| 209 | $errors = array(); |
| 210 | |
| 211 | if ( is_array( $validate ) ) { |
| 212 | $errors = $validate; |
| 213 | } |
| 214 | |
| 215 | $label = strip_tags( pods_v( 'label', $options, ucwords( str_replace( '_', ' ', $name ) ) ) ); |
| 216 | |
| 217 | $check = $this->pre_save( $value, $id, $name, $options, $fields, $pod, $params ); |
| 218 | |
| 219 | if ( is_array( $check ) ) { |
| 220 | $errors = $check; |
| 221 | } else { |
| 222 | if ( 0 < strlen( (string) $value ) && '' === $check ) { |
| 223 | if ( $this->is_required( $options ) ) { |
| 224 | $errors[] = sprintf( __( 'The %s field is required.', 'pods' ), $label ); |
| 225 | } else { |
| 226 | $errors[] = sprintf( __( 'Invalid website provided for the field %s.', 'pods' ), $label ); |
| 227 | } |
| 228 | } |
| 229 | } |
| 230 | |
| 231 | if ( ! empty( $errors ) ) { |
| 232 | return $errors; |
| 233 | } |
| 234 | |
| 235 | return $validate; |
| 236 | } |
| 237 | |
| 238 | /** |
| 239 | * {@inheritdoc} |
| 240 | */ |
| 241 | public function pre_save( $value, $id = null, $name = null, $options = null, $fields = null, $pod = null, $params = null ) { |
| 242 | $options = ( is_array( $options ) || is_object( $options ) ) ? $options : (array) $options; |
| 243 | |
| 244 | // Update from an array input field (like link) if the field updates |
| 245 | if ( is_array( $value ) ) { |
| 246 | if ( isset( $value['url'] ) ) { |
| 247 | $value = $value['url']; |
| 248 | } else { |
| 249 | $value = $this->normalize_value_for_input( $value, $options ); |
| 250 | |
| 251 | // @todo Eventually rework this further. |
| 252 | } |
| 253 | } |
| 254 | |
| 255 | $value = $this->validate_url( $value, $options ); |
| 256 | |
| 257 | $length = (int) pods_v( static::$type . '_max_length', $options, 255 ); |
| 258 | |
| 259 | if ( 0 < $length && $length < pods_mb_strlen( $value ) ) { |
| 260 | $value = pods_mb_substr( $value, 0, $length ); |
| 261 | } |
| 262 | |
| 263 | return $value; |
| 264 | } |
| 265 | |
| 266 | /** |
| 267 | * Validate an URL with the options |
| 268 | * |
| 269 | * @param string|array $value Field value. |
| 270 | * @param array|null $options Field options. |
| 271 | * |
| 272 | * @return string |
| 273 | * |
| 274 | * @since 2.7.0 |
| 275 | */ |
| 276 | public function validate_url( $value, $options = null ) { |
| 277 | if ( empty( $value ) ) { |
| 278 | return $value; |
| 279 | } |
| 280 | |
| 281 | if ( 'none' === pods_v( static::$type . '_format', $options ) ) { |
| 282 | $value = $this->strip_html( $value, $options ); |
| 283 | $value = $this->strip_shortcodes( $value, $options ); |
| 284 | $value = $this->trim_whitespace( $value, $options ); |
| 285 | |
| 286 | return $value; |
| 287 | } |
| 288 | |
| 289 | if ( is_array( $value ) ) { |
| 290 | if ( isset( $value['scheme'] ) ) { |
| 291 | $value = $this->build_url( $value, $options ); |
| 292 | } else { |
| 293 | $value = @implode( '', $value ); |
| 294 | } |
| 295 | } |
| 296 | |
| 297 | if ( false === strpos( $value, '://' ) && 0 !== strpos( $value, '//' ) ) { |
| 298 | $value = 'http://' . $value; |
| 299 | } |
| 300 | |
| 301 | $url = wp_parse_url( $value ); |
| 302 | |
| 303 | if ( empty( $url ) || count( $url ) < 2 ) { |
| 304 | $value = ''; |
| 305 | } else { |
| 306 | $defaults = array( |
| 307 | 'scheme' => 'http', |
| 308 | 'host' => '', |
| 309 | 'port' => '', |
| 310 | 'path' => '/', |
| 311 | 'query' => '', |
| 312 | 'fragment' => '', |
| 313 | ); |
| 314 | |
| 315 | $url = array_merge( $defaults, $url ); |
| 316 | |
| 317 | if ( 'normal' === pods_v( static::$type . '_format', $options ) ) { |
| 318 | $value = $this->build_url( $url, $options ); |
| 319 | } elseif ( 'no-www' === pods_v( static::$type . '_format', $options ) ) { |
| 320 | if ( 0 === strpos( $url['host'], 'www.' ) ) { |
| 321 | $url['host'] = substr( $url['host'], 4 ); |
| 322 | } |
| 323 | |
| 324 | $value = $this->build_url( $url, $options ); |
| 325 | } elseif ( 'force-www' === pods_v( static::$type . '_format', $options ) ) { |
| 326 | if ( false !== strpos( $url['host'], '.' ) && false === strpos( $url['host'], 'www', 0 ) ) { |
| 327 | $url['host'] = 'www.' . $url['host']; |
| 328 | } |
| 329 | |
| 330 | $value = $this->build_url( $url, $options ); |
| 331 | } elseif ( 'no-http' === pods_v( static::$type . '_format', $options ) ) { |
| 332 | $value = $this->build_url( $url, $options ); |
| 333 | $value = str_replace( trim( $url['scheme'] . '://', ':' ), '', $value ); |
| 334 | |
| 335 | if ( '/' === $url['path'] ) { |
| 336 | $value = trim( $value, '/' ); |
| 337 | } |
| 338 | } elseif ( 'no-http-no-www' === pods_v( static::$type . '_format', $options ) ) { |
| 339 | if ( 0 === strpos( $url['host'], 'www.' ) ) { |
| 340 | $url['host'] = substr( $url['host'], 4 ); |
| 341 | } |
| 342 | |
| 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-force-www' === pods_v( static::$type . '_format', $options ) ) { |
| 350 | if ( false !== strpos( $url['host'], '.' ) && false === strpos( $url['host'], 'www', 0 ) ) { |
| 351 | $url['host'] = 'www.' . $url['host']; |
| 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 | }//end if |
| 361 | }//end if |
| 362 | |
| 363 | return $value; |
| 364 | } |
| 365 | |
| 366 | /** |
| 367 | * Validate an target attribute with the options |
| 368 | * |
| 369 | * @param string $value Field value. |
| 370 | * |
| 371 | * @return string |
| 372 | * |
| 373 | * @since 2.7.0 |
| 374 | */ |
| 375 | public function validate_target( $value ) { |
| 376 | if ( ! empty( $value ) && '_blank' === $value ) { |
| 377 | $value = '_blank'; |
| 378 | } else { |
| 379 | $value = ''; |
| 380 | } |
| 381 | return $value; |
| 382 | } |
| 383 | |
| 384 | /** |
| 385 | * Build a url from url parts |
| 386 | * |
| 387 | * @param array|string $url URL value. |
| 388 | * @param array $options Field options. |
| 389 | * |
| 390 | * @return string |
| 391 | */ |
| 392 | public function build_url( $url, $options = array() ) { |
| 393 | |
| 394 | $url = (array) $url; |
| 395 | |
| 396 | $allow_port = (int) pods_v( static::$type . '_allow_port', $options, 0 ); |
| 397 | |
| 398 | // If port is not allowed, always set to empty |
| 399 | if ( 0 === $allow_port ) { |
| 400 | $url['port'] = ''; |
| 401 | } |
| 402 | |
| 403 | if ( function_exists( 'http_build_url' ) ) { |
| 404 | return http_build_url( $url ); |
| 405 | } |
| 406 | |
| 407 | $defaults = array( |
| 408 | 'scheme' => 'http', |
| 409 | 'host' => '', |
| 410 | 'port' => '', |
| 411 | 'path' => '/', |
| 412 | 'query' => '', |
| 413 | 'fragment' => '', |
| 414 | ); |
| 415 | |
| 416 | $url = array_merge( $defaults, $url ); |
| 417 | |
| 418 | $new_url = array(); |
| 419 | |
| 420 | $new_url[] = trim( $url['scheme'] . '://', ':' ); |
| 421 | $new_url[] = $url['host']; |
| 422 | |
| 423 | if ( ! empty( $url['port'] ) ) { |
| 424 | $new_url[] = ':' . $url['port']; |
| 425 | } |
| 426 | |
| 427 | $new_url[] = '/' . ltrim( $url['path'], '/' ); |
| 428 | |
| 429 | if ( isset( $url['query'] ) ) { |
| 430 | $query = ltrim( $url['query'], '?' ); |
| 431 | |
| 432 | if ( ! empty( $query ) ) { |
| 433 | $new_url[] = '?' . $query; |
| 434 | } |
| 435 | } |
| 436 | |
| 437 | if ( isset( $url['fragment'] ) ) { |
| 438 | $fragment = ltrim( $url['fragment'], '#' ); |
| 439 | |
| 440 | if ( ! empty( $fragment ) ) { |
| 441 | $new_url[] = '#' . $fragment; |
| 442 | } |
| 443 | } |
| 444 | |
| 445 | // Pull all of the parts back together |
| 446 | $new_url = implode( '', $new_url ); |
| 447 | |
| 448 | return $new_url; |
| 449 | |
| 450 | } |
| 451 | |
| 452 | } |
| 453 |