FlexibleContent
2 months ago
class-acf-field-accordion.php
2 months ago
class-acf-field-button-group.php
2 months ago
class-acf-field-checkbox.php
3 days ago
class-acf-field-clone.php
2 months ago
class-acf-field-color_picker.php
2 months ago
class-acf-field-date_picker.php
2 months ago
class-acf-field-date_time_picker.php
2 months ago
class-acf-field-email.php
2 months ago
class-acf-field-file.php
2 months ago
class-acf-field-flexible-content.php
1 week ago
class-acf-field-gallery.php
3 weeks ago
class-acf-field-google-map.php
2 months ago
class-acf-field-group.php
2 months ago
class-acf-field-icon_picker.php
7 months ago
class-acf-field-image.php
2 months ago
class-acf-field-link.php
2 months ago
class-acf-field-message.php
1 year ago
class-acf-field-nav-menu.php
1 week ago
class-acf-field-number.php
2 months ago
class-acf-field-oembed.php
3 weeks ago
class-acf-field-output.php
1 year ago
class-acf-field-page_link.php
3 weeks ago
class-acf-field-password.php
2 months ago
class-acf-field-post_object.php
3 weeks ago
class-acf-field-radio.php
3 days ago
class-acf-field-range.php
2 months ago
class-acf-field-relationship.php
3 weeks ago
class-acf-field-repeater.php
3 weeks ago
class-acf-field-select.php
3 days ago
class-acf-field-separator.php
1 year ago
class-acf-field-tab.php
1 year ago
class-acf-field-taxonomy.php
3 weeks ago
class-acf-field-text.php
3 weeks ago
class-acf-field-textarea.php
3 weeks ago
class-acf-field-time_picker.php
2 months ago
class-acf-field-true_false.php
2 months ago
class-acf-field-url.php
3 weeks ago
class-acf-field-user.php
3 weeks ago
class-acf-field-wysiwyg.php
2 months ago
class-acf-field.php
2 months ago
class-acf-repeater-table.php
1 year ago
index.php
1 year ago
class-acf-field-url.php
199 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 ACF 5.0.0 |
| 15 | */ |
| 16 | public function initialize() { |
| 17 | // vars |
| 18 | $this->name = 'url'; |
| 19 | $this->label = __( 'URL', 'secure-custom-fields' ); |
| 20 | $this->description = __( 'A text input specifically designed for storing web addresses.', 'secure-custom-fields' ); |
| 21 | $this->preview_image = acf_get_url() . '/assets/images/field-type-previews/field-preview-url.png'; |
| 22 | $this->doc_url = 'https://developer.wordpress.org/secure-custom-fields/features/fields/url/'; |
| 23 | $this->tutorial_url = 'https://developer.wordpress.org/secure-custom-fields/features/fields/url/url-tutorial/'; |
| 24 | $this->defaults = array( |
| 25 | 'default_value' => '', |
| 26 | 'placeholder' => '', |
| 27 | ); |
| 28 | $this->supports = array( |
| 29 | 'escaping_html' => true, |
| 30 | ); |
| 31 | } |
| 32 | |
| 33 | |
| 34 | /** |
| 35 | * Create the HTML interface for your field |
| 36 | * |
| 37 | * @since ACF 3.6 |
| 38 | * |
| 39 | * @param array $field An array holding all the field's data. |
| 40 | */ |
| 41 | public function render_field( $field ) { |
| 42 | $atts = array(); |
| 43 | $keys = array( 'type', 'id', 'class', 'name', 'value', 'placeholder', 'pattern' ); |
| 44 | $keys2 = array( 'readonly', 'disabled', 'required' ); |
| 45 | |
| 46 | // atts (value="123") |
| 47 | foreach ( $keys as $k ) { |
| 48 | if ( isset( $field[ $k ] ) ) { |
| 49 | $atts[ $k ] = $field[ $k ]; |
| 50 | } |
| 51 | } |
| 52 | |
| 53 | // atts2 (disabled="disabled") |
| 54 | foreach ( $keys2 as $k ) { |
| 55 | if ( ! empty( $field[ $k ] ) ) { |
| 56 | $atts[ $k ] = $k; |
| 57 | } |
| 58 | } |
| 59 | |
| 60 | // remove empty atts |
| 61 | $atts = acf_clean_atts( $atts ); |
| 62 | |
| 63 | // render |
| 64 | $html = '<div class="acf-input-wrap acf-url">'; |
| 65 | $html .= '<i class="acf-icon -globe -small"></i>' . acf_get_text_input( $atts ); |
| 66 | $html .= '</div>'; |
| 67 | |
| 68 | // return |
| 69 | echo $html; //phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- safe HTML, escaped by acf_get_text_input. |
| 70 | } |
| 71 | |
| 72 | |
| 73 | /** |
| 74 | * Create extra options for your field. This is rendered when editing a field. |
| 75 | * The value of $field['name'] can be used (like bellow) to save extra data to the $field |
| 76 | * |
| 77 | * @since ACF 3.6 |
| 78 | * |
| 79 | * @param array $field An array holding all the field's data. |
| 80 | */ |
| 81 | public function render_field_settings( $field ) { |
| 82 | acf_render_field_setting( |
| 83 | $field, |
| 84 | array( |
| 85 | 'label' => __( 'Default Value', 'secure-custom-fields' ), |
| 86 | 'instructions' => __( 'Appears when creating a new post', 'secure-custom-fields' ), |
| 87 | 'type' => 'text', |
| 88 | 'name' => 'default_value', |
| 89 | ) |
| 90 | ); |
| 91 | } |
| 92 | |
| 93 | /** |
| 94 | * Renders the field settings used in the "Presentation" tab. |
| 95 | * |
| 96 | * @since ACF 6.0 |
| 97 | * |
| 98 | * @param array $field The field settings array. |
| 99 | * @return void |
| 100 | */ |
| 101 | public function render_field_presentation_settings( $field ) { |
| 102 | acf_render_field_setting( |
| 103 | $field, |
| 104 | array( |
| 105 | 'label' => __( 'Placeholder Text', 'secure-custom-fields' ), |
| 106 | 'instructions' => __( 'Appears within the input', 'secure-custom-fields' ), |
| 107 | 'type' => 'text', |
| 108 | 'name' => 'placeholder', |
| 109 | ) |
| 110 | ); |
| 111 | } |
| 112 | |
| 113 | |
| 114 | /** |
| 115 | * Validate the fields value is correctly formatted as a URL |
| 116 | * |
| 117 | * @since ACF 5.0.0 |
| 118 | * |
| 119 | * @param mixed $valid The current validity of the field value. Boolean true if valid, a validation error message string if not. |
| 120 | * @param string $value The value of the field. |
| 121 | * @param array $field Field object array. |
| 122 | * @param string $input The form input name for this field. |
| 123 | * @return mixed Boolean true if valid, a validation error message string if not. |
| 124 | */ |
| 125 | public function validate_value( $valid, $value, $field, $input ) { |
| 126 | |
| 127 | // bail early if empty |
| 128 | if ( empty( $value ) ) { |
| 129 | return $valid; |
| 130 | } |
| 131 | |
| 132 | // A non-string value (e.g. an array from a crafted submission) is never a valid URL. |
| 133 | if ( ! is_string( $value ) ) { |
| 134 | return __( 'Value must be a valid URL', 'secure-custom-fields' ); |
| 135 | } |
| 136 | |
| 137 | if ( strpos( $value, '://' ) !== false ) { |
| 138 | |
| 139 | // url |
| 140 | } elseif ( strpos( $value, '//' ) === 0 ) { |
| 141 | |
| 142 | // protocol relative url |
| 143 | } else { |
| 144 | $valid = __( 'Value must be a valid URL', 'secure-custom-fields' ); |
| 145 | } |
| 146 | |
| 147 | // return |
| 148 | return $valid; |
| 149 | } |
| 150 | |
| 151 | /** |
| 152 | * This filter is applied to the $value after it is loaded from the db, and before it is returned to the template |
| 153 | * |
| 154 | * @since ACF 6.2.6 |
| 155 | * |
| 156 | * @param mixed $value The value which was loaded from the database. |
| 157 | * @param mixed $post_id The $post_id from which the value was loaded. |
| 158 | * @param array $field The field array holding all the field options. |
| 159 | * @param boolean $escape_html Should the field return a HTML safe formatted value. |
| 160 | * @return mixed $value The modified value |
| 161 | */ |
| 162 | public function format_value( $value, $post_id, $field, $escape_html ) { |
| 163 | if ( $escape_html ) { |
| 164 | // esc_url() expects a string; treat a non-string value as empty. |
| 165 | return is_string( $value ) ? esc_url( $value ) : ''; |
| 166 | } |
| 167 | return $value; |
| 168 | } |
| 169 | |
| 170 | /** |
| 171 | * Return the schema array for the REST API. |
| 172 | * |
| 173 | * @param array $field The field object. |
| 174 | * @return array |
| 175 | */ |
| 176 | public function get_rest_schema( array $field ) { |
| 177 | $schema = parent::get_rest_schema( $field ); |
| 178 | $schema['format'] = 'uri'; |
| 179 | |
| 180 | return $schema; |
| 181 | } |
| 182 | |
| 183 | /** |
| 184 | * Returns an array of JSON-LD Property output types that are supported by this field type. |
| 185 | * |
| 186 | * @since 6.8 |
| 187 | * |
| 188 | * @return string[] |
| 189 | */ |
| 190 | public function get_jsonld_output_types(): array { |
| 191 | return array( 'URL' ); |
| 192 | } |
| 193 | } |
| 194 | |
| 195 | |
| 196 | // initialize |
| 197 | acf_register_field_type( 'acf_field_url' ); |
| 198 | endif; // class_exists check |
| 199 |