| 1 |
<?php |
| 2 |
if ( ! defined( 'ABSPATH' ) ) { |
| 3 |
die( 'You are not allowed to call this page directly.' ); |
| 4 |
} |
| 5 |
|
| 6 |
/** |
| 7 |
* @since 3.0 |
| 8 |
*/ |
| 9 |
class FrmFieldUrl extends FrmFieldType { |
| 10 |
|
| 11 |
/** |
| 12 |
* @var string |
| 13 |
* @since 3.0 |
| 14 |
*/ |
| 15 |
protected $type = 'url'; |
| 16 |
|
| 17 |
/** |
| 18 |
* @var bool |
| 19 |
*/ |
| 20 |
protected $array_allowed = false; |
| 21 |
|
| 22 |
/** |
| 23 |
* @return bool[] |
| 24 |
*/ |
| 25 |
protected function field_settings_for_type() { |
| 26 |
return array( |
| 27 |
'size' => true, |
| 28 |
'clear_on_focus' => true, |
| 29 |
'invalid' => true, |
| 30 |
'show_image' => true, |
| 31 |
); |
| 32 |
} |
| 33 |
|
| 34 |
/** |
| 35 |
* @return array |
| 36 |
*/ |
| 37 |
protected function extra_field_opts() { |
| 38 |
return array( |
| 39 |
'show_image' => 0, |
| 40 |
); |
| 41 |
} |
| 42 |
|
| 43 |
/** |
| 44 |
* @return string |
| 45 |
*/ |
| 46 |
protected function get_field_name() { |
| 47 |
return __( 'Website', 'formidable' ); |
| 48 |
} |
| 49 |
|
| 50 |
/** |
| 51 |
* @return void |
| 52 |
*/ |
| 53 |
protected function fill_default_atts( &$atts ) { |
| 54 |
$defaults = array( |
| 55 |
'sep' => ', ', |
| 56 |
'html' => false, |
| 57 |
); |
| 58 |
$atts = wp_parse_args( $atts, $defaults ); |
| 59 |
|
| 60 |
if ( $atts['html'] ) { |
| 61 |
$atts['sep'] = ' '; |
| 62 |
} |
| 63 |
} |
| 64 |
|
| 65 |
public function validate( $args ) { |
| 66 |
$value = $args['value']; |
| 67 |
if ( trim( $value ) === 'http://' || empty( $value ) ) { |
| 68 |
$value = ''; |
| 69 |
} else { |
| 70 |
$value = esc_url_raw( $value ); |
| 71 |
$value = preg_match( '/^(https?|ftps?|mailto|news|feed|telnet):/is', $value ) ? $value : 'http://' . $value; |
| 72 |
} |
| 73 |
|
| 74 |
FrmEntriesHelper::set_posted_value( $this->field, $value, $args ); |
| 75 |
|
| 76 |
$errors = array(); |
| 77 |
|
| 78 |
// validate the url format |
| 79 |
if ( ! empty( $value ) && ! preg_match( '/^http(s)?:\/\/(?:localhost|(?:[\da-z\.-]+\.[\da-z\.-]+))/i', $value ) ) { |
| 80 |
$errors[ 'field' . $args['id'] ] = FrmFieldsHelper::get_error_msg( $this->field, 'invalid' ); |
| 81 |
} elseif ( $this->field->required == '1' && empty( $value ) ) { |
| 82 |
$errors[ 'field' . $args['id'] ] = FrmFieldsHelper::get_error_msg( $this->field, 'blank' ); |
| 83 |
} |
| 84 |
|
| 85 |
return $errors; |
| 86 |
} |
| 87 |
|
| 88 |
protected function prepare_display_value( $value, $atts ) { |
| 89 |
if ( $atts['html'] ) { |
| 90 |
$images = ''; |
| 91 |
foreach ( (array) $value as $url ) { |
| 92 |
$image_regex = '/(\.(?i)(jpg|jpeg|png|gif))$/'; |
| 93 |
$is_image = preg_match( $image_regex, $url ); |
| 94 |
if ( $is_image ) { |
| 95 |
$images .= '<img src="' . esc_url( $url ) . '" class="frm_image_from_url" alt="" /> '; |
| 96 |
} else { |
| 97 |
$images .= strip_tags( $url ); |
| 98 |
} |
| 99 |
} |
| 100 |
$value = $images; |
| 101 |
} |
| 102 |
|
| 103 |
return $value; |
| 104 |
} |
| 105 |
|
| 106 |
/** |
| 107 |
* @since 4.0.04 |
| 108 |
* |
| 109 |
* @return void |
| 110 |
*/ |
| 111 |
public function sanitize_value( &$value ) { |
| 112 |
FrmAppHelper::sanitize_value( 'esc_url_raw', $value ); |
| 113 |
} |
| 114 |
} |
| 115 |
|