field-advanced-link.php
1 month ago
field-button.php
5 months ago
field-checkbox.php
1 month ago
field-clone.php
2 years ago
field-code-editor.php
1 month ago
field-column.php
3 years ago
field-dynamic-render.php
3 years ago
field-file.php
1 month ago
field-flexible-content-actions-title.php
1 month ago
field-flexible-content-actions-toggle.php
1 month ago
field-flexible-content-actions.php
1 month ago
field-flexible-content-async.php
2 months ago
field-flexible-content-compatibility.php
1 month ago
field-flexible-content-controls.php
7 months ago
field-flexible-content-hide.php
1 month ago
field-flexible-content-hooks.php
7 months ago
field-flexible-content-modal-edit.php
7 months ago
field-flexible-content-modal-select.php
1 month ago
field-flexible-content-modal-settings.php
1 month ago
field-flexible-content-popup.php
1 month ago
field-flexible-content-preview.php
1 month ago
field-flexible-content-state.php
7 months ago
field-flexible-content-thumbnail.php
7 months ago
field-flexible-content-wysiwyg.php
7 months ago
field-flexible-content.php
1 month ago
field-forms.php
1 month ago
field-gallery.php
1 month ago
field-google-map.php
1 month ago
field-group.php
1 month ago
field-hidden.php
3 years ago
field-image.php
1 month ago
field-post-object.php
1 month ago
field-post-statuses.php
1 month ago
field-post-types.php
1 month ago
field-radio.php
1 month ago
field-recaptcha.php
1 month ago
field-relationship.php
1 month ago
field-repeater.php
1 month ago
field-select.php
1 month ago
field-slug.php
10 months ago
field-taxonomies.php
1 month ago
field-taxonomy-terms.php
1 month ago
field-taxonomy.php
1 month ago
field-textarea.php
1 month ago
field-user-roles.php
1 month ago
field-user.php
1 month ago
field-wysiwyg.php
1 month ago
field-select.php
342 lines
| 1 | <?php |
| 2 | |
| 3 | if(!defined('ABSPATH')){ |
| 4 | exit; |
| 5 | } |
| 6 | |
| 7 | if(!class_exists('acfe_field_select')): |
| 8 | |
| 9 | class acfe_field_select extends acfe_field_extend{ |
| 10 | |
| 11 | /** |
| 12 | * initialize |
| 13 | */ |
| 14 | function initialize(){ |
| 15 | |
| 16 | $this->name = 'select'; |
| 17 | $this->defaults = array( |
| 18 | 'allow_custom' => 0, |
| 19 | 'placeholder' => '', |
| 20 | 'search_placeholder' => '', |
| 21 | ); |
| 22 | |
| 23 | } |
| 24 | |
| 25 | |
| 26 | /** |
| 27 | * render_field_settings |
| 28 | * |
| 29 | * @param $field |
| 30 | */ |
| 31 | function render_field_settings($field){ |
| 32 | |
| 33 | // allow custom |
| 34 | acf_render_field_setting($field, array( |
| 35 | 'label' => __('Allow Custom','acf'), |
| 36 | 'instructions' => __("Allow 'custom' values to be added", 'acf'), |
| 37 | 'name' => 'allow_custom', |
| 38 | 'type' => 'true_false', |
| 39 | 'ui' => 1, |
| 40 | 'conditional_logic' => array( |
| 41 | array( |
| 42 | array( |
| 43 | 'field' => 'ui', |
| 44 | 'operator' => '==', |
| 45 | 'value' => '1', |
| 46 | ), |
| 47 | ), |
| 48 | ) |
| 49 | )); |
| 50 | |
| 51 | // Placeholder |
| 52 | acf_render_field_setting($field, array( |
| 53 | 'label' => __('Placeholder','acf'), |
| 54 | 'instructions' => __('Appears within the input','acf'), |
| 55 | 'type' => 'text', |
| 56 | 'name' => 'placeholder', |
| 57 | 'placeholder' => _x('Select', 'verb', 'acf'), |
| 58 | 'conditional_logic' => array( |
| 59 | array( |
| 60 | array( |
| 61 | 'field' => 'ui', |
| 62 | 'operator' => '==', |
| 63 | 'value' => '0', |
| 64 | ), |
| 65 | array( |
| 66 | 'field' => 'allow_null', |
| 67 | 'operator' => '==', |
| 68 | 'value' => '1', |
| 69 | ), |
| 70 | array( |
| 71 | 'field' => 'multiple', |
| 72 | 'operator' => '==', |
| 73 | 'value' => '0', |
| 74 | ), |
| 75 | ), |
| 76 | array( |
| 77 | array( |
| 78 | 'field' => 'ui', |
| 79 | 'operator' => '==', |
| 80 | 'value' => '1', |
| 81 | ), |
| 82 | array( |
| 83 | 'field' => 'allow_null', |
| 84 | 'operator' => '==', |
| 85 | 'value' => '1', |
| 86 | ), |
| 87 | ), |
| 88 | array( |
| 89 | array( |
| 90 | 'field' => 'ui', |
| 91 | 'operator' => '==', |
| 92 | 'value' => '1', |
| 93 | ), |
| 94 | array( |
| 95 | 'field' => 'multiple', |
| 96 | 'operator' => '==', |
| 97 | 'value' => '1', |
| 98 | ), |
| 99 | ), |
| 100 | ) |
| 101 | )); |
| 102 | |
| 103 | // Search Placeholder |
| 104 | acf_render_field_setting($field, array( |
| 105 | 'label' => __('Search Input Placeholder','acf'), |
| 106 | 'instructions' => __('Appears within the search input','acf'), |
| 107 | 'type' => 'text', |
| 108 | 'name' => 'search_placeholder', |
| 109 | 'placeholder' => '', |
| 110 | 'conditional_logic' => array( |
| 111 | array( |
| 112 | array( |
| 113 | 'field' => 'ui', |
| 114 | 'operator' => '==', |
| 115 | 'value' => '1', |
| 116 | ), |
| 117 | array( |
| 118 | 'field' => 'multiple', |
| 119 | 'operator' => '==', |
| 120 | 'value' => '0', |
| 121 | ), |
| 122 | ), |
| 123 | ) |
| 124 | )); |
| 125 | |
| 126 | } |
| 127 | |
| 128 | |
| 129 | /** |
| 130 | * load_field |
| 131 | * |
| 132 | * @param $field |
| 133 | * |
| 134 | * @return mixed |
| 135 | */ |
| 136 | function load_field($field){ |
| 137 | |
| 138 | // ajax enabled |
| 139 | if(!empty($field['ajax'])){ |
| 140 | $field = acfe_strip_choices_label($field); // strip '## title' grouping for ajax |
| 141 | } |
| 142 | |
| 143 | return $field; |
| 144 | |
| 145 | } |
| 146 | |
| 147 | |
| 148 | /** |
| 149 | * prepare_field |
| 150 | * |
| 151 | * @param $field |
| 152 | * |
| 153 | * @return mixed |
| 154 | */ |
| 155 | function prepare_field($field){ |
| 156 | |
| 157 | // allow custom |
| 158 | if(!empty($field['allow_custom'])){ |
| 159 | |
| 160 | $value = acfe_get($field, 'value'); |
| 161 | $value = acfe_as_array($value); |
| 162 | |
| 163 | foreach($value as $v){ |
| 164 | |
| 165 | // append custom value to choices |
| 166 | if(!isset($field['choices'][ $v ])){ |
| 167 | $field['choices'][ $v ] = $v; |
| 168 | $field['custom_choices'][ $v ] = $v; |
| 169 | } |
| 170 | } |
| 171 | |
| 172 | } |
| 173 | |
| 174 | // group choices using '## title' |
| 175 | if(empty($field['ajax'])){ |
| 176 | $field = acfe_decode_choices_label($field); |
| 177 | } |
| 178 | |
| 179 | // return |
| 180 | return $field; |
| 181 | |
| 182 | } |
| 183 | |
| 184 | |
| 185 | /** |
| 186 | * field_wrapper_attributes |
| 187 | * |
| 188 | * @param $wrapper |
| 189 | * @param $field |
| 190 | * |
| 191 | * @return mixed |
| 192 | */ |
| 193 | function field_wrapper_attributes($wrapper, $field){ |
| 194 | |
| 195 | // search placeholder |
| 196 | if($field['search_placeholder']){ |
| 197 | $wrapper['data-acfe-search-placeholder'] = $field['search_placeholder']; |
| 198 | } |
| 199 | |
| 200 | // allow custom |
| 201 | if($field['allow_custom']){ |
| 202 | $wrapper['data-acfe-allow-custom'] = 1; |
| 203 | } |
| 204 | |
| 205 | // return |
| 206 | return $wrapper; |
| 207 | |
| 208 | } |
| 209 | |
| 210 | |
| 211 | /** |
| 212 | * format_front_value |
| 213 | * |
| 214 | * @param $formatted |
| 215 | * @param $unformatted |
| 216 | * @param $post_id |
| 217 | * @param $field |
| 218 | * @param $form |
| 219 | * |
| 220 | * @return string |
| 221 | */ |
| 222 | function format_front_value($formatted, $unformatted, $post_id, $field, $form){ |
| 223 | |
| 224 | // vars |
| 225 | $value = acfe_as_array($unformatted); |
| 226 | $array = array(); |
| 227 | |
| 228 | // loop values |
| 229 | foreach($value as $v){ |
| 230 | $array[] = acfe_get($field['choices'], $v, $v); |
| 231 | } |
| 232 | |
| 233 | // merge |
| 234 | return implode(', ', $array); |
| 235 | |
| 236 | } |
| 237 | |
| 238 | |
| 239 | /** |
| 240 | * validate_front_value |
| 241 | * |
| 242 | * @param $valid |
| 243 | * @param $value |
| 244 | * @param $field |
| 245 | * @param $input |
| 246 | * @param $form |
| 247 | * |
| 248 | * @return false |
| 249 | */ |
| 250 | function validate_front_value($valid, $value, $field, $input, $form){ |
| 251 | |
| 252 | // bail early |
| 253 | if(!$this->pre_validate_front_value($valid, $value, $field, $form)){ |
| 254 | return $valid; |
| 255 | } |
| 256 | |
| 257 | // custom value allowed |
| 258 | if(!empty($field['create_options']) || !empty($field['allow_custom'])){ |
| 259 | return $valid; |
| 260 | } |
| 261 | |
| 262 | // vars |
| 263 | $value = acfe_as_array($value); |
| 264 | $choices = acfe_as_array($field['choices']); |
| 265 | |
| 266 | // handle ajax choices |
| 267 | if(!empty($field['ajax'])){ |
| 268 | |
| 269 | if(method_exists($this->instance, 'get_ajax_query')){ |
| 270 | |
| 271 | // perform select ajax query |
| 272 | $query = $this->instance->get_ajax_query(array( |
| 273 | 'field_key' => $field['key'], |
| 274 | 'post_id' => $form['post_id'], |
| 275 | )); |
| 276 | |
| 277 | // empty query |
| 278 | if(empty($query)){ |
| 279 | return false; |
| 280 | } |
| 281 | |
| 282 | // get results |
| 283 | // expecting array('results' => array( array('id' => '', 'text' => '') )) |
| 284 | $results = acfe_get($query, 'results'); |
| 285 | $results = acfe_as_array($results); |
| 286 | |
| 287 | // no results |
| 288 | if(empty($results)){ |
| 289 | return false; |
| 290 | } |
| 291 | |
| 292 | // reset choices |
| 293 | $choices = array(); |
| 294 | |
| 295 | // loop results and assign choices |
| 296 | foreach($results as $result){ |
| 297 | if(isset($result['id'], $result['text'])){ |
| 298 | $choices[ $result['id'] ] = $result['text']; |
| 299 | } |
| 300 | } |
| 301 | |
| 302 | } |
| 303 | |
| 304 | } |
| 305 | |
| 306 | // empty choices |
| 307 | if(empty($choices)){ |
| 308 | return false; // value is always invalid as there no choice is allowed |
| 309 | } |
| 310 | |
| 311 | // check values against choices |
| 312 | if(!empty(array_diff($value, array_keys($choices)))){ |
| 313 | return false; |
| 314 | } |
| 315 | |
| 316 | // return |
| 317 | return $valid; |
| 318 | |
| 319 | } |
| 320 | |
| 321 | |
| 322 | /** |
| 323 | * translate_field |
| 324 | * |
| 325 | * @param $field |
| 326 | * |
| 327 | * @return mixed |
| 328 | */ |
| 329 | function translate_field($field){ |
| 330 | |
| 331 | $field['placeholder'] = acf_translate($field['placeholder']); |
| 332 | $field['search_placeholder'] = acf_translate($field['search_placeholder']); |
| 333 | |
| 334 | return $field; |
| 335 | |
| 336 | } |
| 337 | |
| 338 | } |
| 339 | |
| 340 | acf_new_instance('acfe_field_select'); |
| 341 | |
| 342 | endif; |