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
2 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
2 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
2 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-link.php
341 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 ACF 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', 'secure-custom-fields' ); |
| 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.', 'secure-custom-fields' ); |
| 25 | $this->preview_image = acf_get_url() . '/assets/images/field-type-previews/field-preview-link.png'; |
| 26 | $this->doc_url = 'https://developer.wordpress.org/secure-custom-fields/features/fields/link/'; |
| 27 | $this->tutorial_url = 'https://developer.wordpress.org/secure-custom-fields/features/fields/link/link-tutorial/'; |
| 28 | $this->defaults = array( |
| 29 | 'return_format' => 'array', |
| 30 | ); |
| 31 | } |
| 32 | |
| 33 | |
| 34 | /** |
| 35 | * description |
| 36 | * |
| 37 | * @type function |
| 38 | * @date 16/5/17 |
| 39 | * @since ACF 5.5.13 |
| 40 | * |
| 41 | * @param $post_id (int) |
| 42 | * @return $post_id (int) |
| 43 | */ |
| 44 | function get_link( $value = '' ) { |
| 45 | |
| 46 | // vars |
| 47 | $link = array( |
| 48 | 'title' => '', |
| 49 | 'url' => '', |
| 50 | 'target' => '', |
| 51 | ); |
| 52 | |
| 53 | // array (ACF 5.6.0) |
| 54 | if ( is_array( $value ) ) { |
| 55 | $link = array_merge( $link, $value ); |
| 56 | |
| 57 | // post id (ACF < 5.6.0) |
| 58 | } elseif ( is_numeric( $value ) ) { |
| 59 | $link['title'] = get_the_title( $value ); |
| 60 | $link['url'] = get_permalink( $value ); |
| 61 | |
| 62 | // string (ACF < 5.6.0) |
| 63 | } elseif ( is_string( $value ) ) { |
| 64 | $link['url'] = $value; |
| 65 | } |
| 66 | |
| 67 | // return |
| 68 | return $link; |
| 69 | } |
| 70 | |
| 71 | |
| 72 | /** |
| 73 | * Create the HTML interface for your field |
| 74 | * |
| 75 | * @param $field - an array holding all the field's data |
| 76 | * |
| 77 | * @type action |
| 78 | * @since ACF 3.6 |
| 79 | */ |
| 80 | public function render_field( $field ) { |
| 81 | |
| 82 | // vars |
| 83 | $div = array( |
| 84 | 'id' => $field['id'], |
| 85 | 'class' => $field['class'] . ' acf-link', |
| 86 | ); |
| 87 | |
| 88 | // render scripts/styles |
| 89 | acf_enqueue_uploader(); |
| 90 | |
| 91 | // get link |
| 92 | $link = $this->get_link( $field['value'] ); |
| 93 | |
| 94 | // classes |
| 95 | if ( $link['url'] ) { |
| 96 | $div['class'] .= ' -value'; |
| 97 | } |
| 98 | |
| 99 | if ( $link['target'] === '_blank' ) { |
| 100 | $div['class'] .= ' -external'; |
| 101 | } |
| 102 | ?> |
| 103 | <div <?php echo acf_esc_attrs( $div ); ?>> |
| 104 | |
| 105 | <div class="acf-hidden"> |
| 106 | <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> |
| 107 | <?php foreach ( $link as $k => $v ) : ?> |
| 108 | <?php |
| 109 | acf_hidden_input( |
| 110 | array( |
| 111 | 'class' => "input-$k", |
| 112 | 'name' => $field['name'] . "[$k]", |
| 113 | 'value' => $v, |
| 114 | ) |
| 115 | ); |
| 116 | ?> |
| 117 | <?php endforeach; ?> |
| 118 | </div> |
| 119 | |
| 120 | <a href="#" class="button" data-name="add" target=""><?php esc_html_e( 'Select Link', 'secure-custom-fields' ); ?></a> |
| 121 | |
| 122 | <div class="link-wrap"> |
| 123 | <span class="link-title"><?php echo esc_html( $link['title'] ); ?></span> |
| 124 | <a class="link-url" href="<?php echo esc_url( $link['url'] ); ?>" target="_blank"><?php echo esc_html( $link['url'] ); ?></a> |
| 125 | <i class="acf-icon -link-ext acf-js-tooltip" title="<?php esc_attr_e( 'Opens in a new window/tab', 'secure-custom-fields' ); ?>"></i><a class="acf-icon -pencil -clear acf-js-tooltip" data-name="edit" href="#" title="<?php esc_attr_e( 'Edit', 'secure-custom-fields' ); ?>"></a><a class="acf-icon -cancel -clear acf-js-tooltip" data-name="remove" href="#" title="<?php esc_attr_e( 'Remove', 'secure-custom-fields' ); ?>"></a> |
| 126 | </div> |
| 127 | |
| 128 | </div> |
| 129 | <?php |
| 130 | } |
| 131 | |
| 132 | |
| 133 | /** |
| 134 | * Create extra options for your field. This is rendered when editing a field. |
| 135 | * The value of $field['name'] can be used (like bellow) to save extra data to the $field |
| 136 | * |
| 137 | * @type action |
| 138 | * @since ACF 3.6 |
| 139 | * @date 23/01/13 |
| 140 | * |
| 141 | * @param $field - an array holding all the field's data |
| 142 | */ |
| 143 | function render_field_settings( $field ) { |
| 144 | acf_render_field_setting( |
| 145 | $field, |
| 146 | array( |
| 147 | 'label' => __( 'Return Value', 'secure-custom-fields' ), |
| 148 | 'instructions' => __( 'Specify the returned value on front end', 'secure-custom-fields' ), |
| 149 | 'type' => 'radio', |
| 150 | 'name' => 'return_format', |
| 151 | 'layout' => 'horizontal', |
| 152 | 'choices' => array( |
| 153 | 'array' => __( 'Link Array', 'secure-custom-fields' ), |
| 154 | 'url' => __( 'Link URL', 'secure-custom-fields' ), |
| 155 | ), |
| 156 | ) |
| 157 | ); |
| 158 | } |
| 159 | |
| 160 | /** |
| 161 | * This filter is applied to the $value after it is loaded from the db and before it is returned to the template |
| 162 | * |
| 163 | * @type filter |
| 164 | * @since ACF 3.6 |
| 165 | * @date 23/01/13 |
| 166 | * |
| 167 | * @param $value (mixed) the value which was loaded from the database |
| 168 | * @param $post_id (mixed) the post_id from which the value was loaded |
| 169 | * @param $field (array) the field array holding all the field options |
| 170 | * |
| 171 | * @return $value (mixed) the modified value |
| 172 | */ |
| 173 | function format_value( $value, $post_id, $field ) { |
| 174 | |
| 175 | // bail early if no value |
| 176 | if ( empty( $value ) ) { |
| 177 | return $value; |
| 178 | } |
| 179 | |
| 180 | // get link |
| 181 | $link = $this->get_link( $value ); |
| 182 | |
| 183 | // format value |
| 184 | if ( $field['return_format'] == 'url' ) { |
| 185 | return $link['url']; |
| 186 | } |
| 187 | |
| 188 | // return link |
| 189 | return $link; |
| 190 | } |
| 191 | |
| 192 | |
| 193 | /** |
| 194 | * description |
| 195 | * |
| 196 | * @type function |
| 197 | * @date 11/02/2014 |
| 198 | * @since ACF 5.0.0 |
| 199 | * |
| 200 | * @param $post_id (int) |
| 201 | * @return $post_id (int) |
| 202 | */ |
| 203 | function validate_value( $valid, $value, $field, $input ) { |
| 204 | |
| 205 | // bail early if not required |
| 206 | if ( ! $field['required'] ) { |
| 207 | return $valid; |
| 208 | } |
| 209 | |
| 210 | // URL is required |
| 211 | if ( empty( $value ) || empty( $value['url'] ) ) { |
| 212 | return false; |
| 213 | } |
| 214 | |
| 215 | // return |
| 216 | return $valid; |
| 217 | } |
| 218 | |
| 219 | |
| 220 | /** |
| 221 | * This filter is applied to the $value before it is updated in the db |
| 222 | * |
| 223 | * @type filter |
| 224 | * @since ACF 3.6 |
| 225 | * @date 23/01/13 |
| 226 | * |
| 227 | * @param $value - the value which will be saved in the database |
| 228 | * @param $post_id - the post_id of which the value will be saved |
| 229 | * @param $field - the field array holding all the field options |
| 230 | * |
| 231 | * @return $value - the modified value |
| 232 | */ |
| 233 | function update_value( $value, $post_id, $field ) { |
| 234 | |
| 235 | // Check if value is an empty array and convert to empty string. |
| 236 | if ( empty( $value ) || empty( $value['url'] ) ) { |
| 237 | $value = ''; |
| 238 | } |
| 239 | |
| 240 | // return |
| 241 | return $value; |
| 242 | } |
| 243 | |
| 244 | /** |
| 245 | * Return the schema array for the REST API. |
| 246 | * |
| 247 | * @param array $field |
| 248 | * @return array |
| 249 | */ |
| 250 | public function get_rest_schema( array $field ) { |
| 251 | return array( |
| 252 | 'type' => array( 'object', 'null' ), |
| 253 | 'required' => ! empty( $field['required'] ), |
| 254 | 'properties' => array( |
| 255 | 'title' => array( |
| 256 | 'type' => 'string', |
| 257 | ), |
| 258 | 'url' => array( |
| 259 | 'type' => 'string', |
| 260 | 'required' => true, |
| 261 | 'format' => 'uri', |
| 262 | ), |
| 263 | 'target' => array( |
| 264 | 'type' => 'string', |
| 265 | ), |
| 266 | ), |
| 267 | ); |
| 268 | } |
| 269 | |
| 270 | /** |
| 271 | * Returns an array of JSON-LD Property output types that are supported by this field type. |
| 272 | * |
| 273 | * @since 6.8 |
| 274 | * |
| 275 | * @return string[] |
| 276 | */ |
| 277 | public function get_jsonld_output_types(): array { |
| 278 | return array( 'URL', 'WebPage' ); |
| 279 | } |
| 280 | |
| 281 | /** |
| 282 | * Formats the field value for JSON-LD output. |
| 283 | * |
| 284 | * @since 6.8.0 |
| 285 | * |
| 286 | * @param mixed $value The value of the field. |
| 287 | * @param integer|string $post_id The ID of the post. |
| 288 | * @param array $field The field array. |
| 289 | * @return mixed |
| 290 | */ |
| 291 | public function format_value_for_jsonld( $value, $post_id, $field ) { |
| 292 | if ( empty( $value ) ) { |
| 293 | return null; |
| 294 | } |
| 295 | |
| 296 | // Get link data. |
| 297 | $link = $this->get_link( $value ); |
| 298 | |
| 299 | if ( empty( $link['url'] ) ) { |
| 300 | return null; |
| 301 | } |
| 302 | |
| 303 | // Get output format with fallback. |
| 304 | $output_format = $field['schema_output_format'] ?? ''; |
| 305 | if ( empty( $output_format ) ) { |
| 306 | $property = $field['schema_property'] ?? ''; |
| 307 | $output_format = \SCF\AI\GEO\Schema::get_default_output_format( $this->name, $property ); |
| 308 | } |
| 309 | |
| 310 | // Default to URL if no format determined. |
| 311 | if ( empty( $output_format ) ) { |
| 312 | $output_format = 'URL'; |
| 313 | } |
| 314 | |
| 315 | // URL format - just return the URL string. |
| 316 | if ( 'URL' === $output_format ) { |
| 317 | return $link['url']; |
| 318 | } |
| 319 | |
| 320 | // WebPage format - return structured object. |
| 321 | $webpage = array( |
| 322 | '@type' => 'WebPage', |
| 323 | 'url' => $link['url'], |
| 324 | ); |
| 325 | |
| 326 | // Add title as name if available. |
| 327 | if ( ! empty( $link['title'] ) ) { |
| 328 | $webpage['name'] = $link['title']; |
| 329 | } |
| 330 | |
| 331 | return $webpage; |
| 332 | } |
| 333 | } |
| 334 | |
| 335 | |
| 336 | // initialize |
| 337 | acf_register_field_type( 'acf_field_link' ); |
| 338 | endif; // class_exists check |
| 339 | |
| 340 | ?> |
| 341 |