PluginProbe ʕ •ᴥ•ʔ
Secure Custom Fields / 6.4.0-beta2
Secure Custom Fields v6.4.0-beta2
6.9.5 6.9.4 6.9.3 6.9.2 6.9.1 6.9.0 6.8.9 6.8.7 6.8.8 6.8.6 6.8.4 6.8.5 trunk 6.4.0-beta1 6.4.0-beta2 6.4.1 6.4.1-beta3 6.4.1-beta4 6.4.1-beta5 6.4.1-beta6 6.4.1-beta7 6.4.2 6.5.0 6.5.1 6.5.2 6.5.3 6.5.4 6.5.5 6.5.6 6.5.7 6.6.0 6.7.0 6.7.1 6.8.0 6.8.1 6.8.2 6.8.3
secure-custom-fields / includes / fields / class-acf-field-url.php
secure-custom-fields / includes / fields Last commit date
class-acf-field-accordion.php 1 year ago class-acf-field-button-group.php 1 year ago class-acf-field-checkbox.php 1 year ago class-acf-field-color_picker.php 1 year ago class-acf-field-date_picker.php 1 year ago class-acf-field-date_time_picker.php 1 year ago class-acf-field-email.php 1 year ago class-acf-field-file.php 1 year ago class-acf-field-google-map.php 1 year ago class-acf-field-group.php 1 year ago class-acf-field-icon_picker.php 1 year ago class-acf-field-image.php 1 year ago class-acf-field-link.php 1 year ago class-acf-field-message.php 1 year ago class-acf-field-number.php 1 year ago class-acf-field-oembed.php 1 year ago class-acf-field-output.php 1 year ago class-acf-field-page_link.php 1 year ago class-acf-field-password.php 1 year ago class-acf-field-post_object.php 1 year ago class-acf-field-radio.php 1 year ago class-acf-field-range.php 1 year ago class-acf-field-relationship.php 1 year ago class-acf-field-select.php 1 year ago class-acf-field-separator.php 1 year ago class-acf-field-tab.php 1 year ago class-acf-field-taxonomy.php 1 year ago class-acf-field-text.php 1 year ago class-acf-field-textarea.php 1 year ago class-acf-field-time_picker.php 1 year ago class-acf-field-true_false.php 1 year ago class-acf-field-url.php 1 year ago class-acf-field-user.php 1 year ago class-acf-field-wysiwyg.php 1 year ago class-acf-field.php 1 year ago index.php 1 year ago
class-acf-field-url.php
181 lines
1 <?php
2
3 if ( ! class_exists( 'acf_field_url' ) ) :
4
5 /**
6 * The URL field class.
7 */
8 class acf_field_url extends acf_field {
9
10
11 /**
12 * This function will setup the field type data
13 *
14 * @since 5.0.0
15 */
16 public function initialize() {
17 // vars
18 $this->name = 'url';
19 $this->label = __( 'URL', 'acf' );
20 $this->description = __( 'A text input specifically designed for storing web addresses.', 'acf' );
21 $this->preview_image = acf_get_url() . '/assets/images/field-type-previews/field-preview-url.png';
22 $this->doc_url = 'https://www.advancedcustomfields.com/resources/url/';
23 $this->defaults = array(
24 'default_value' => '',
25 'placeholder' => '',
26 );
27 $this->supports = array(
28 'escaping_html' => true,
29 );
30 }
31
32
33 /**
34 * Create the HTML interface for your field
35 *
36 * @since 3.6
37 *
38 * @param array $field An array holding all the field's data.
39 */
40 public function render_field( $field ) {
41 $atts = array();
42 $keys = array( 'type', 'id', 'class', 'name', 'value', 'placeholder', 'pattern' );
43 $keys2 = array( 'readonly', 'disabled', 'required' );
44
45 // atts (value="123")
46 foreach ( $keys as $k ) {
47 if ( isset( $field[ $k ] ) ) {
48 $atts[ $k ] = $field[ $k ];
49 }
50 }
51
52 // atts2 (disabled="disabled")
53 foreach ( $keys2 as $k ) {
54 if ( ! empty( $field[ $k ] ) ) {
55 $atts[ $k ] = $k;
56 }
57 }
58
59 // remove empty atts
60 $atts = acf_clean_atts( $atts );
61
62 // render
63 $html = '<div class="acf-input-wrap acf-url">';
64 $html .= '<i class="acf-icon -globe -small"></i>' . acf_get_text_input( $atts );
65 $html .= '</div>';
66
67 // return
68 echo $html; //phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- safe HTML, escaped by acf_get_text_input.
69 }
70
71
72 /**
73 * Create extra options for your field. This is rendered when editing a field.
74 * The value of $field['name'] can be used (like bellow) to save extra data to the $field
75 *
76 * @since 3.6
77 *
78 * @param array $field An array holding all the field's data.
79 */
80 public function render_field_settings( $field ) {
81 acf_render_field_setting(
82 $field,
83 array(
84 'label' => __( 'Default Value', 'acf' ),
85 'instructions' => __( 'Appears when creating a new post', 'acf' ),
86 'type' => 'text',
87 'name' => 'default_value',
88 )
89 );
90 }
91
92 /**
93 * Renders the field settings used in the "Presentation" tab.
94 *
95 * @since 6.0
96 *
97 * @param array $field The field settings array.
98 * @return void
99 */
100 public function render_field_presentation_settings( $field ) {
101 acf_render_field_setting(
102 $field,
103 array(
104 'label' => __( 'Placeholder Text', 'acf' ),
105 'instructions' => __( 'Appears within the input', 'acf' ),
106 'type' => 'text',
107 'name' => 'placeholder',
108 )
109 );
110 }
111
112
113 /**
114 * Validate the fields value is correctly formatted as a URL
115 *
116 * @since 5.0.0
117 *
118 * @param mixed $valid The current validity of the field value. Boolean true if valid, a validation error message string if not.
119 * @param string $value The value of the field.
120 * @param array $field Field object array.
121 * @param string $input The form input name for this field.
122 * @return mixed Boolean true if valid, a validation error message string if not.
123 */
124 public function validate_value( $valid, $value, $field, $input ) {
125
126 // bail early if empty
127 if ( empty( $value ) ) {
128 return $valid;
129 }
130
131 if ( strpos( $value, '://' ) !== false ) {
132
133 // url
134 } elseif ( strpos( $value, '//' ) === 0 ) {
135
136 // protocol relative url
137 } else {
138 $valid = __( 'Value must be a valid URL', 'acf' );
139 }
140
141 // return
142 return $valid;
143 }
144
145 /**
146 * This filter is applied to the $value after it is loaded from the db, and before it is returned to the template
147 *
148 * @since 6.2.6
149 *
150 * @param mixed $value The value which was loaded from the database.
151 * @param mixed $post_id The $post_id from which the value was loaded.
152 * @param array $field The field array holding all the field options.
153 * @param boolean $escape_html Should the field return a HTML safe formatted value.
154 * @return mixed $value The modified value
155 */
156 public function format_value( $value, $post_id, $field, $escape_html ) {
157 if ( $escape_html ) {
158 return esc_url( $value );
159 }
160 return $value;
161 }
162
163 /**
164 * Return the schema array for the REST API.
165 *
166 * @param array $field The field object.
167 * @return array
168 */
169 public function get_rest_schema( array $field ) {
170 $schema = parent::get_rest_schema( $field );
171 $schema['format'] = 'uri';
172
173 return $schema;
174 }
175 }
176
177
178 // initialize
179 acf_register_field_type( 'acf_field_url' );
180 endif; // class_exists check
181