field-advanced-link.php
6 years ago
field-button.php
6 years ago
field-clone.php
6 years ago
field-column.php
6 years ago
field-dynamic-message.php
6 years ago
field-file.php
6 years ago
field-flexible-content.php
6 years ago
field-forms.php
6 years ago
field-group.php
6 years ago
field-hidden.php
6 years ago
field-image.php
6 years ago
field-post-statuses.php
6 years ago
field-post-types.php
6 years ago
field-recaptcha.php
6 years ago
field-repeater.php
6 years ago
field-select.php
6 years ago
field-slug.php
6 years ago
field-taxonomies.php
6 years ago
field-taxonomy-terms.php
6 years ago
field-textarea.php
6 years ago
field-user-roles.php
6 years ago
field-advanced-link.php
264 lines
| 1 | <?php |
| 2 | |
| 3 | if(!defined('ABSPATH')) |
| 4 | exit; |
| 5 | |
| 6 | class acfe_field_advanced_link extends acf_field{ |
| 7 | |
| 8 | function __construct(){ |
| 9 | |
| 10 | $this->name = 'acfe_advanced_link'; |
| 11 | $this->label = __('Advanced Link', 'acfe'); |
| 12 | $this->category = 'relational'; |
| 13 | $this->defaults = array( |
| 14 | |
| 15 | ); |
| 16 | |
| 17 | acf_add_local_field(array( |
| 18 | 'key' => 'acfe_advanced_link_post', |
| 19 | 'label' => __('Post', 'acf'), |
| 20 | 'type' => 'post_object', |
| 21 | 'required' => false, |
| 22 | 'post_type' => false, |
| 23 | 'taxonomy' => false, |
| 24 | )); |
| 25 | |
| 26 | parent::__construct(); |
| 27 | |
| 28 | } |
| 29 | |
| 30 | function get_link($value = ''){ |
| 31 | |
| 32 | // vars |
| 33 | $value = wp_parse_args($value, array( |
| 34 | 'acfe_advanced_link_type' => 'url', |
| 35 | 'acfe_advanced_link_url' => '', |
| 36 | 'acfe_advanced_link_post' => '', |
| 37 | 'acfe_advanced_link_title' => '', |
| 38 | 'acfe_advanced_link_target' => false, |
| 39 | )); |
| 40 | |
| 41 | $link = array( |
| 42 | 'type' => 'url', |
| 43 | 'url' => false, |
| 44 | 'post' => '', |
| 45 | 'title' => '', |
| 46 | 'target' => false, |
| 47 | ); |
| 48 | |
| 49 | |
| 50 | $link['type'] = $value['acfe_advanced_link_type']; |
| 51 | $link['title'] = $value['acfe_advanced_link_title']; |
| 52 | if($value['acfe_advanced_link_target']) |
| 53 | $link['target'] = '_blank'; |
| 54 | |
| 55 | // URL |
| 56 | if($value['acfe_advanced_link_type'] === 'url'){ |
| 57 | |
| 58 | $link['url'] = $value['acfe_advanced_link_url']; |
| 59 | |
| 60 | // Post |
| 61 | }elseif($value['acfe_advanced_link_type'] === 'post'){ |
| 62 | |
| 63 | $link['post'] = $value['acfe_advanced_link_post']; |
| 64 | |
| 65 | if(!empty($value['acfe_advanced_link_post'])){ |
| 66 | |
| 67 | $link['url'] = get_permalink($value['acfe_advanced_link_post']); |
| 68 | |
| 69 | } |
| 70 | |
| 71 | } |
| 72 | |
| 73 | // return |
| 74 | return $link; |
| 75 | |
| 76 | } |
| 77 | |
| 78 | function render_field($field){ |
| 79 | |
| 80 | // vars |
| 81 | $div = array( |
| 82 | 'id' => $field['id'], |
| 83 | 'class' => $field['class'] . ' acf-link', |
| 84 | ); |
| 85 | |
| 86 | // get link |
| 87 | $link = $this->get_link($field['value']); |
| 88 | |
| 89 | // classes |
| 90 | if($link['url']) |
| 91 | $div['class'] .= ' -value'; |
| 92 | |
| 93 | if($link['target'] === '_blank') |
| 94 | $div['class'] .= ' -external'; |
| 95 | |
| 96 | $link['url_title'] = ''; |
| 97 | |
| 98 | // URL |
| 99 | if($link['type'] === 'url'){ |
| 100 | |
| 101 | $link['url_title'] = $link['url']; |
| 102 | |
| 103 | // Post |
| 104 | }elseif($link['type'] === 'post'){ |
| 105 | |
| 106 | if(!empty($link['post'])){ |
| 107 | |
| 108 | $link['url_title'] = get_the_title($link['post']); |
| 109 | |
| 110 | } |
| 111 | |
| 112 | } |
| 113 | |
| 114 | $fields = array( |
| 115 | |
| 116 | array( |
| 117 | 'prefix' => $field['name'], |
| 118 | 'name' => 'type', |
| 119 | 'key' => 'acfe_advanced_link_type', |
| 120 | 'label' => __('Type', 'acf'), |
| 121 | 'type' => 'radio', |
| 122 | 'value' => $link['type'], |
| 123 | 'required' => false, |
| 124 | 'class' => 'input-type', |
| 125 | 'choices' => array( |
| 126 | 'url' => __('URL', 'acf'), |
| 127 | 'post' => __('Post', 'acf'), |
| 128 | ), |
| 129 | ), |
| 130 | |
| 131 | array( |
| 132 | 'prefix' => $field['name'], |
| 133 | 'name' => 'url', |
| 134 | 'key' => 'acfe_advanced_link_url', |
| 135 | 'label' => __('URL', 'acf'), |
| 136 | 'type' => 'text', |
| 137 | 'value' => $link['url'], |
| 138 | 'required' => false, |
| 139 | 'class' => 'input-url', |
| 140 | 'conditional_logic' => array( |
| 141 | array( |
| 142 | array( |
| 143 | 'field' => 'acfe_advanced_link_type', |
| 144 | 'operator' => '==', |
| 145 | 'value' => 'url', |
| 146 | ) |
| 147 | ) |
| 148 | ) |
| 149 | |
| 150 | ), |
| 151 | |
| 152 | array( |
| 153 | 'prefix' => $field['name'], |
| 154 | 'name' => 'post', |
| 155 | 'key' => 'acfe_advanced_link_post', |
| 156 | 'label' => __('Post', 'acf'), |
| 157 | 'type' => 'post_object', |
| 158 | 'value' => $link['post'], |
| 159 | 'required' => false, |
| 160 | 'class' => 'input-post', |
| 161 | 'allow_null' => 1, |
| 162 | 'conditional_logic' => array( |
| 163 | array( |
| 164 | array( |
| 165 | 'field' => 'acfe_advanced_link_type', |
| 166 | 'operator' => '==', |
| 167 | 'value' => 'post', |
| 168 | ) |
| 169 | ) |
| 170 | ) |
| 171 | ), |
| 172 | |
| 173 | array( |
| 174 | 'prefix' => $field['name'], |
| 175 | 'name' => 'title', |
| 176 | 'key' => 'acfe_advanced_link_title', |
| 177 | 'label' => __('Link text', 'acf'), |
| 178 | 'type' => 'text', |
| 179 | 'value' => $link['title'], |
| 180 | 'required' => false, |
| 181 | 'class' => 'input-title', |
| 182 | ), |
| 183 | |
| 184 | array( |
| 185 | 'prefix' => $field['name'], |
| 186 | 'name' => 'target', |
| 187 | 'key' => 'acfe_advanced_link_target', |
| 188 | 'label' => __('Target', 'acf'), |
| 189 | 'type' => 'true_false', |
| 190 | 'value' => $link['target'], |
| 191 | 'message' => __('Open in an new window', 'acf'), |
| 192 | 'required' => false, |
| 193 | 'class' => 'input-target', |
| 194 | ), |
| 195 | |
| 196 | ); |
| 197 | |
| 198 | $fields = apply_filters('acfe/fields/advanced_link/fields', $fields, $field, $link); |
| 199 | |
| 200 | ?> |
| 201 | |
| 202 | <div <?php acf_esc_attr_e($div); ?>> |
| 203 | |
| 204 | <div class="acfe-modal" data-modal-title="<?php echo $field['label']; ?>"> |
| 205 | <div class="acfe-modal-wrapper"> |
| 206 | <div class="acfe-modal-content"> |
| 207 | |
| 208 | <div class="acf-fields acf-form-fields -left"> |
| 209 | |
| 210 | <?php acf_render_fields($fields, false, 'div', 'label'); ?> |
| 211 | |
| 212 | </div> |
| 213 | |
| 214 | </div> |
| 215 | </div> |
| 216 | </div> |
| 217 | |
| 218 | <a href="#" class="button" data-name="add" target=""><?php _e('Select Link', 'acf'); ?></a> |
| 219 | |
| 220 | <div class="link-wrap"> |
| 221 | <span class="link-title"><?php echo esc_html($link['title']); ?></span> |
| 222 | <a class="link-url" href="<?php echo esc_url($link['url']); ?>" target="_blank"><?php echo esc_html($link['url_title']); ?></a> |
| 223 | <i class="acf-icon -link-ext acf-js-tooltip" title="<?php _e('Opens in a new window/tab', 'acf'); ?>"></i><?php |
| 224 | ?><a class="acf-icon -pencil -clear acf-js-tooltip" data-name="edit" href="#" title="<?php _e('Edit', 'acf'); ?>"></a><?php |
| 225 | ?><a class="acf-icon -cancel -clear acf-js-tooltip" data-name="remove" href="#" title="<?php _e('Remove', 'acf'); ?>"></a> |
| 226 | </div> |
| 227 | |
| 228 | </div> |
| 229 | <?php |
| 230 | |
| 231 | } |
| 232 | |
| 233 | function format_value($value, $post_id, $field){ |
| 234 | |
| 235 | // bail early if no value |
| 236 | if(empty($value)) |
| 237 | return $value; |
| 238 | |
| 239 | // get link |
| 240 | $link = $this->get_link($value); |
| 241 | |
| 242 | // return link |
| 243 | return $link; |
| 244 | |
| 245 | } |
| 246 | |
| 247 | function validate_value($valid, $value, $field, $input){ |
| 248 | |
| 249 | // bail early if not required |
| 250 | if(!$field['required']) |
| 251 | return $valid; |
| 252 | |
| 253 | // URL is required |
| 254 | if(empty($value) || (empty($value['acfe_advanced_link_url'] && empty($value['acfe_advanced_link_post'])))) |
| 255 | return false; |
| 256 | |
| 257 | // return |
| 258 | return $valid; |
| 259 | |
| 260 | } |
| 261 | |
| 262 | } |
| 263 | |
| 264 | new acfe_field_advanced_link(); |