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-link.php
276 lines
| 1 | <?php |
| 2 | |
| 3 | if ( ! class_exists( 'acf_field_link' ) ) : |
| 4 | |
| 5 | class acf_field_link extends acf_field { |
| 6 | |
| 7 | |
| 8 | /** |
| 9 | * This function will setup the field type data |
| 10 | * |
| 11 | * @type function |
| 12 | * @date 5/03/2014 |
| 13 | * @since 5.0.0 |
| 14 | * |
| 15 | * @param n/a |
| 16 | * @return n/a |
| 17 | */ |
| 18 | function initialize() { |
| 19 | |
| 20 | // vars |
| 21 | $this->name = 'link'; |
| 22 | $this->label = __( 'Link', 'acf' ); |
| 23 | $this->category = 'relational'; |
| 24 | $this->description = __( 'Allows you to specify a link and its properties such as title and target using the WordPress native link picker.', 'acf' ); |
| 25 | $this->preview_image = acf_get_url() . '/assets/images/field-type-previews/field-preview-link.png'; |
| 26 | $this->doc_url = 'https://www.advancedcustomfields.com/resources/link/'; |
| 27 | $this->defaults = array( |
| 28 | 'return_format' => 'array', |
| 29 | ); |
| 30 | } |
| 31 | |
| 32 | |
| 33 | /** |
| 34 | * description |
| 35 | * |
| 36 | * @type function |
| 37 | * @date 16/5/17 |
| 38 | * @since 5.5.13 |
| 39 | * |
| 40 | * @param $post_id (int) |
| 41 | * @return $post_id (int) |
| 42 | */ |
| 43 | function get_link( $value = '' ) { |
| 44 | |
| 45 | // vars |
| 46 | $link = array( |
| 47 | 'title' => '', |
| 48 | 'url' => '', |
| 49 | 'target' => '', |
| 50 | ); |
| 51 | |
| 52 | // array (ACF 5.6.0) |
| 53 | if ( is_array( $value ) ) { |
| 54 | $link = array_merge( $link, $value ); |
| 55 | |
| 56 | // post id (ACF < 5.6.0) |
| 57 | } elseif ( is_numeric( $value ) ) { |
| 58 | $link['title'] = get_the_title( $value ); |
| 59 | $link['url'] = get_permalink( $value ); |
| 60 | |
| 61 | // string (ACF < 5.6.0) |
| 62 | } elseif ( is_string( $value ) ) { |
| 63 | $link['url'] = $value; |
| 64 | } |
| 65 | |
| 66 | // return |
| 67 | return $link; |
| 68 | } |
| 69 | |
| 70 | |
| 71 | /** |
| 72 | * Create the HTML interface for your field |
| 73 | * |
| 74 | * @param $field - an array holding all the field's data |
| 75 | * |
| 76 | * @type action |
| 77 | * @since 3.6 |
| 78 | */ |
| 79 | public function render_field( $field ) { |
| 80 | |
| 81 | // vars |
| 82 | $div = array( |
| 83 | 'id' => $field['id'], |
| 84 | 'class' => $field['class'] . ' acf-link', |
| 85 | ); |
| 86 | |
| 87 | // render scripts/styles |
| 88 | acf_enqueue_uploader(); |
| 89 | |
| 90 | // get link |
| 91 | $link = $this->get_link( $field['value'] ); |
| 92 | |
| 93 | // classes |
| 94 | if ( $link['url'] ) { |
| 95 | $div['class'] .= ' -value'; |
| 96 | } |
| 97 | |
| 98 | if ( $link['target'] === '_blank' ) { |
| 99 | $div['class'] .= ' -external'; |
| 100 | } |
| 101 | ?> |
| 102 | <div <?php echo acf_esc_attrs( $div ); ?>> |
| 103 | |
| 104 | <div class="acf-hidden"> |
| 105 | <a class="link-node" href="<?php echo esc_url( $link['url'] ); ?>" target="<?php echo esc_attr( $link['target'] ); ?>"><?php echo esc_html( $link['title'] ); ?></a> |
| 106 | <?php foreach ( $link as $k => $v ) : ?> |
| 107 | <?php |
| 108 | acf_hidden_input( |
| 109 | array( |
| 110 | 'class' => "input-$k", |
| 111 | 'name' => $field['name'] . "[$k]", |
| 112 | 'value' => $v, |
| 113 | ) |
| 114 | ); |
| 115 | ?> |
| 116 | <?php endforeach; ?> |
| 117 | </div> |
| 118 | |
| 119 | <a href="#" class="button" data-name="add" target=""><?php esc_html_e( 'Select Link', 'acf' ); ?></a> |
| 120 | |
| 121 | <div class="link-wrap"> |
| 122 | <span class="link-title"><?php echo esc_html( $link['title'] ); ?></span> |
| 123 | <a class="link-url" href="<?php echo esc_url( $link['url'] ); ?>" target="_blank"><?php echo esc_html( $link['url'] ); ?></a> |
| 124 | <i class="acf-icon -link-ext acf-js-tooltip" title="<?php esc_attr_e( 'Opens in a new window/tab', 'acf' ); ?>"></i><a class="acf-icon -pencil -clear acf-js-tooltip" data-name="edit" href="#" title="<?php esc_attr_e( 'Edit', 'acf' ); ?>"></a><a class="acf-icon -cancel -clear acf-js-tooltip" data-name="remove" href="#" title="<?php esc_attr_e( 'Remove', 'acf' ); ?>"></a> |
| 125 | </div> |
| 126 | |
| 127 | </div> |
| 128 | <?php |
| 129 | } |
| 130 | |
| 131 | |
| 132 | /** |
| 133 | * Create extra options for your field. This is rendered when editing a field. |
| 134 | * The value of $field['name'] can be used (like bellow) to save extra data to the $field |
| 135 | * |
| 136 | * @type action |
| 137 | * @since 3.6 |
| 138 | * @date 23/01/13 |
| 139 | * |
| 140 | * @param $field - an array holding all the field's data |
| 141 | */ |
| 142 | function render_field_settings( $field ) { |
| 143 | acf_render_field_setting( |
| 144 | $field, |
| 145 | array( |
| 146 | 'label' => __( 'Return Value', 'acf' ), |
| 147 | 'instructions' => __( 'Specify the returned value on front end', 'acf' ), |
| 148 | 'type' => 'radio', |
| 149 | 'name' => 'return_format', |
| 150 | 'layout' => 'horizontal', |
| 151 | 'choices' => array( |
| 152 | 'array' => __( 'Link Array', 'acf' ), |
| 153 | 'url' => __( 'Link URL', 'acf' ), |
| 154 | ), |
| 155 | ) |
| 156 | ); |
| 157 | } |
| 158 | |
| 159 | /** |
| 160 | * This filter is appied to the $value after it is loaded from the db and before it is returned to the template |
| 161 | * |
| 162 | * @type filter |
| 163 | * @since 3.6 |
| 164 | * @date 23/01/13 |
| 165 | * |
| 166 | * @param $value (mixed) the value which was loaded from the database |
| 167 | * @param $post_id (mixed) the post_id from which the value was loaded |
| 168 | * @param $field (array) the field array holding all the field options |
| 169 | * |
| 170 | * @return $value (mixed) the modified value |
| 171 | */ |
| 172 | function format_value( $value, $post_id, $field ) { |
| 173 | |
| 174 | // bail early if no value |
| 175 | if ( empty( $value ) ) { |
| 176 | return $value; |
| 177 | } |
| 178 | |
| 179 | // get link |
| 180 | $link = $this->get_link( $value ); |
| 181 | |
| 182 | // format value |
| 183 | if ( $field['return_format'] == 'url' ) { |
| 184 | return $link['url']; |
| 185 | } |
| 186 | |
| 187 | // return link |
| 188 | return $link; |
| 189 | } |
| 190 | |
| 191 | |
| 192 | /** |
| 193 | * description |
| 194 | * |
| 195 | * @type function |
| 196 | * @date 11/02/2014 |
| 197 | * @since 5.0.0 |
| 198 | * |
| 199 | * @param $post_id (int) |
| 200 | * @return $post_id (int) |
| 201 | */ |
| 202 | function validate_value( $valid, $value, $field, $input ) { |
| 203 | |
| 204 | // bail early if not required |
| 205 | if ( ! $field['required'] ) { |
| 206 | return $valid; |
| 207 | } |
| 208 | |
| 209 | // URL is required |
| 210 | if ( empty( $value ) || empty( $value['url'] ) ) { |
| 211 | return false; |
| 212 | } |
| 213 | |
| 214 | // return |
| 215 | return $valid; |
| 216 | } |
| 217 | |
| 218 | |
| 219 | /** |
| 220 | * This filter is appied to the $value before it is updated in the db |
| 221 | * |
| 222 | * @type filter |
| 223 | * @since 3.6 |
| 224 | * @date 23/01/13 |
| 225 | * |
| 226 | * @param $value - the value which will be saved in the database |
| 227 | * @param $post_id - the post_id of which the value will be saved |
| 228 | * @param $field - the field array holding all the field options |
| 229 | * |
| 230 | * @return $value - the modified value |
| 231 | */ |
| 232 | function update_value( $value, $post_id, $field ) { |
| 233 | |
| 234 | // Check if value is an empty array and convert to empty string. |
| 235 | if ( empty( $value ) || empty( $value['url'] ) ) { |
| 236 | $value = ''; |
| 237 | } |
| 238 | |
| 239 | // return |
| 240 | return $value; |
| 241 | } |
| 242 | |
| 243 | /** |
| 244 | * Return the schema array for the REST API. |
| 245 | * |
| 246 | * @param array $field |
| 247 | * @return array |
| 248 | */ |
| 249 | public function get_rest_schema( array $field ) { |
| 250 | return array( |
| 251 | 'type' => array( 'object', 'null' ), |
| 252 | 'required' => ! empty( $field['required'] ), |
| 253 | 'properties' => array( |
| 254 | 'title' => array( |
| 255 | 'type' => 'string', |
| 256 | ), |
| 257 | 'url' => array( |
| 258 | 'type' => 'string', |
| 259 | 'required' => true, |
| 260 | 'format' => 'uri', |
| 261 | ), |
| 262 | 'target' => array( |
| 263 | 'type' => 'string', |
| 264 | ), |
| 265 | ), |
| 266 | ); |
| 267 | } |
| 268 | } |
| 269 | |
| 270 | |
| 271 | // initialize |
| 272 | acf_register_field_type( 'acf_field_link' ); |
| 273 | endif; // class_exists check |
| 274 | |
| 275 | ?> |
| 276 |