field-advanced-link.php
3 months ago
field-button-group.php
1 week ago
field-button.php
7 months ago
field-checkbox.php
3 months ago
field-clone.php
2 years ago
field-code-editor.php
3 months ago
field-column.php
3 years ago
field-dynamic-render.php
3 years ago
field-file.php
3 months ago
field-flexible-content-actions-title.php
3 months ago
field-flexible-content-actions-toggle.php
3 months ago
field-flexible-content-actions.php
3 months ago
field-flexible-content-async.php
4 months ago
field-flexible-content-compatibility.php
3 months ago
field-flexible-content-controls.php
9 months ago
field-flexible-content-hide.php
3 months ago
field-flexible-content-hooks.php
9 months ago
field-flexible-content-modal-edit.php
9 months ago
field-flexible-content-modal-select.php
3 months ago
field-flexible-content-modal-settings.php
3 months ago
field-flexible-content-popup.php
3 months ago
field-flexible-content-preview.php
3 months ago
field-flexible-content-state.php
9 months ago
field-flexible-content-thumbnail.php
9 months ago
field-flexible-content-wysiwyg.php
9 months ago
field-flexible-content.php
3 months ago
field-forms.php
3 months ago
field-gallery.php
3 months ago
field-google-map.php
3 months ago
field-group.php
3 months ago
field-hidden.php
3 years ago
field-image.php
3 months ago
field-post-object.php
3 months ago
field-post-statuses.php
3 months ago
field-post-types.php
3 months ago
field-radio.php
3 months ago
field-recaptcha.php
3 months ago
field-relationship.php
3 months ago
field-repeater.php
3 months ago
field-select.php
3 months ago
field-slug.php
1 year ago
field-taxonomies.php
3 months ago
field-taxonomy-terms.php
3 months ago
field-taxonomy.php
3 months ago
field-textarea.php
3 months ago
field-user-roles.php
3 months ago
field-user.php
3 months ago
field-wysiwyg.php
3 months ago
field-taxonomies.php
549 lines
| 1 | <?php |
| 2 | |
| 3 | if(!defined('ABSPATH')){ |
| 4 | exit; |
| 5 | } |
| 6 | |
| 7 | if(!class_exists('acfe_field_taxonomies')): |
| 8 | |
| 9 | class acfe_field_taxonomies extends acfe_field{ |
| 10 | |
| 11 | /** |
| 12 | * initialize |
| 13 | */ |
| 14 | function initialize(){ |
| 15 | |
| 16 | $this->name = 'acfe_taxonomies'; |
| 17 | $this->label = __('Taxonomies', 'acfe'); |
| 18 | $this->category = 'WordPress'; |
| 19 | $this->defaults = array( |
| 20 | 'taxonomy' => array(), |
| 21 | 'field_type' => 'checkbox', |
| 22 | 'multiple' => 0, |
| 23 | 'allow_null' => 0, |
| 24 | 'choices' => array(), |
| 25 | 'default_value' => '', |
| 26 | 'ui' => 0, |
| 27 | 'ajax' => 0, |
| 28 | 'placeholder' => '', |
| 29 | 'search_placeholder' => '', |
| 30 | 'layout' => '', |
| 31 | 'toggle' => 0, |
| 32 | 'allow_custom' => 0, |
| 33 | 'other_choice' => 0, |
| 34 | 'return_format' => 'object', |
| 35 | ); |
| 36 | |
| 37 | } |
| 38 | |
| 39 | |
| 40 | /** |
| 41 | * render_field_settings |
| 42 | * |
| 43 | * @param $field |
| 44 | */ |
| 45 | function render_field_settings($field){ |
| 46 | |
| 47 | if(isset($field['default_value'])){ |
| 48 | $field['default_value'] = acf_encode_choices($field['default_value'], false); |
| 49 | } |
| 50 | |
| 51 | // Allow Taxonomy |
| 52 | acf_render_field_setting($field, array( |
| 53 | 'label' => __('Allow Taxonomy','acf'), |
| 54 | 'instructions' => '', |
| 55 | 'type' => 'select', |
| 56 | 'name' => 'taxonomy', |
| 57 | 'choices' => acf_get_taxonomy_labels(), |
| 58 | 'multiple' => 1, |
| 59 | 'ui' => 1, |
| 60 | 'allow_null' => 1, |
| 61 | 'placeholder' => __("All taxonomies",'acf'), |
| 62 | )); |
| 63 | |
| 64 | // field_type |
| 65 | acf_render_field_setting($field, array( |
| 66 | 'label' => __('Appearance','acf'), |
| 67 | 'instructions' => __('Select the appearance of this field', 'acf'), |
| 68 | 'type' => 'select', |
| 69 | 'name' => 'field_type', |
| 70 | 'optgroup' => true, |
| 71 | 'choices' => array( |
| 72 | 'checkbox' => __('Checkbox', 'acf'), |
| 73 | 'radio' => __('Radio Buttons', 'acf'), |
| 74 | 'select' => _x('Select', 'noun', 'acf') |
| 75 | ) |
| 76 | )); |
| 77 | |
| 78 | // default_value |
| 79 | acf_render_field_setting($field, array( |
| 80 | 'label' => __('Default Value','acf'), |
| 81 | 'instructions' => __('Enter each default value on a new line','acf'), |
| 82 | 'name' => 'default_value', |
| 83 | 'type' => 'textarea', |
| 84 | )); |
| 85 | |
| 86 | // return_format |
| 87 | acf_render_field_setting($field, array( |
| 88 | 'label' => __('Return Value', 'acf'), |
| 89 | 'instructions' => '', |
| 90 | 'type' => 'radio', |
| 91 | 'name' => 'return_format', |
| 92 | 'choices' => array( |
| 93 | 'object' => __('Taxonomy object', 'acfe'), |
| 94 | 'name' => __('Taxonomy name', 'acfe') |
| 95 | ), |
| 96 | 'layout' => 'horizontal', |
| 97 | )); |
| 98 | |
| 99 | // Select + Radio: allow_null |
| 100 | acf_render_field_setting($field, array( |
| 101 | 'label' => __('Allow Null?','acf'), |
| 102 | 'instructions' => '', |
| 103 | 'name' => 'allow_null', |
| 104 | 'type' => 'true_false', |
| 105 | 'ui' => 1, |
| 106 | 'conditions' => array( |
| 107 | array( |
| 108 | array( |
| 109 | 'field' => 'field_type', |
| 110 | 'operator' => '==', |
| 111 | 'value' => 'select', |
| 112 | ), |
| 113 | ), |
| 114 | array( |
| 115 | array( |
| 116 | 'field' => 'field_type', |
| 117 | 'operator' => '==', |
| 118 | 'value' => 'radio', |
| 119 | ), |
| 120 | ), |
| 121 | ) |
| 122 | )); |
| 123 | |
| 124 | // Select: multiple |
| 125 | acf_render_field_setting($field, array( |
| 126 | 'label' => __('Select multiple values?','acf'), |
| 127 | 'instructions' => '', |
| 128 | 'name' => 'multiple', |
| 129 | 'type' => 'true_false', |
| 130 | 'ui' => 1, |
| 131 | 'conditions' => array( |
| 132 | array( |
| 133 | array( |
| 134 | 'field' => 'field_type', |
| 135 | 'operator' => '==', |
| 136 | 'value' => 'select', |
| 137 | ), |
| 138 | ), |
| 139 | ) |
| 140 | )); |
| 141 | |
| 142 | // Select: ui |
| 143 | acf_render_field_setting($field, array( |
| 144 | 'label' => __('Stylised UI','acf'), |
| 145 | 'instructions' => '', |
| 146 | 'name' => 'ui', |
| 147 | 'type' => 'true_false', |
| 148 | 'ui' => 1, |
| 149 | 'conditions' => array( |
| 150 | array( |
| 151 | array( |
| 152 | 'field' => 'field_type', |
| 153 | 'operator' => '==', |
| 154 | 'value' => 'select', |
| 155 | ), |
| 156 | ), |
| 157 | ) |
| 158 | )); |
| 159 | |
| 160 | |
| 161 | // Select: ajax |
| 162 | acf_render_field_setting($field, array( |
| 163 | 'label' => __('Use AJAX to lazy load choices?','acf'), |
| 164 | 'instructions' => '', |
| 165 | 'name' => 'ajax', |
| 166 | 'type' => 'true_false', |
| 167 | 'ui' => 1, |
| 168 | 'conditions' => array( |
| 169 | array( |
| 170 | array( |
| 171 | 'field' => 'field_type', |
| 172 | 'operator' => '==', |
| 173 | 'value' => 'select', |
| 174 | ), |
| 175 | array( |
| 176 | 'field' => 'ui', |
| 177 | 'operator' => '==', |
| 178 | 'value' => 1, |
| 179 | ), |
| 180 | ), |
| 181 | ) |
| 182 | )); |
| 183 | |
| 184 | // Select: Placeholder |
| 185 | acf_render_field_setting($field, array( |
| 186 | 'label' => __('Placeholder','acf'), |
| 187 | 'instructions' => __('Appears within the input','acf'), |
| 188 | 'type' => 'text', |
| 189 | 'name' => 'placeholder', |
| 190 | 'placeholder' => _x('Select', 'verb', 'acf'), |
| 191 | 'conditional_logic' => array( |
| 192 | array( |
| 193 | array( |
| 194 | 'field' => 'field_type', |
| 195 | 'operator' => '==', |
| 196 | 'value' => 'select', |
| 197 | ), |
| 198 | array( |
| 199 | 'field' => 'ui', |
| 200 | 'operator' => '==', |
| 201 | 'value' => '0', |
| 202 | ), |
| 203 | array( |
| 204 | 'field' => 'allow_null', |
| 205 | 'operator' => '==', |
| 206 | 'value' => '1', |
| 207 | ), |
| 208 | array( |
| 209 | 'field' => 'multiple', |
| 210 | 'operator' => '==', |
| 211 | 'value' => '0', |
| 212 | ), |
| 213 | ), |
| 214 | array( |
| 215 | array( |
| 216 | 'field' => 'field_type', |
| 217 | 'operator' => '==', |
| 218 | 'value' => 'select', |
| 219 | ), |
| 220 | array( |
| 221 | 'field' => 'ui', |
| 222 | 'operator' => '==', |
| 223 | 'value' => '1', |
| 224 | ), |
| 225 | array( |
| 226 | 'field' => 'allow_null', |
| 227 | 'operator' => '==', |
| 228 | 'value' => '1', |
| 229 | ), |
| 230 | ), |
| 231 | array( |
| 232 | array( |
| 233 | 'field' => 'field_type', |
| 234 | 'operator' => '==', |
| 235 | 'value' => 'select', |
| 236 | ), |
| 237 | array( |
| 238 | 'field' => 'ui', |
| 239 | 'operator' => '==', |
| 240 | 'value' => '1', |
| 241 | ), |
| 242 | array( |
| 243 | 'field' => 'multiple', |
| 244 | 'operator' => '==', |
| 245 | 'value' => '1', |
| 246 | ), |
| 247 | ), |
| 248 | ) |
| 249 | )); |
| 250 | |
| 251 | // Select: Search Placeholder |
| 252 | acf_render_field_setting($field, array( |
| 253 | 'label' => __('Search Input Placeholder','acf'), |
| 254 | 'instructions' => __('Appears within the search input','acf'), |
| 255 | 'type' => 'text', |
| 256 | 'name' => 'search_placeholder', |
| 257 | 'placeholder' => '', |
| 258 | 'conditional_logic' => array( |
| 259 | array( |
| 260 | array( |
| 261 | 'field' => 'field_type', |
| 262 | 'operator' => '==', |
| 263 | 'value' => 'select', |
| 264 | ), |
| 265 | array( |
| 266 | 'field' => 'ui', |
| 267 | 'operator' => '==', |
| 268 | 'value' => '1', |
| 269 | ), |
| 270 | array( |
| 271 | 'field' => 'multiple', |
| 272 | 'operator' => '==', |
| 273 | 'value' => '0', |
| 274 | ), |
| 275 | ), |
| 276 | ) |
| 277 | )); |
| 278 | |
| 279 | // Radio: other_choice |
| 280 | acf_render_field_setting($field, array( |
| 281 | 'label' => __('Other','acf'), |
| 282 | 'instructions' => '', |
| 283 | 'name' => 'other_choice', |
| 284 | 'type' => 'true_false', |
| 285 | 'ui' => 1, |
| 286 | 'message' => __("Add 'other' choice to allow for custom values", 'acf'), |
| 287 | 'conditions' => array( |
| 288 | array( |
| 289 | array( |
| 290 | 'field' => 'field_type', |
| 291 | 'operator' => '==', |
| 292 | 'value' => 'radio', |
| 293 | ), |
| 294 | ), |
| 295 | ) |
| 296 | )); |
| 297 | |
| 298 | // Checkbox: layout |
| 299 | acf_render_field_setting($field, array( |
| 300 | 'label' => __('Layout','acf'), |
| 301 | 'instructions' => '', |
| 302 | 'type' => 'radio', |
| 303 | 'name' => 'layout', |
| 304 | 'layout' => 'horizontal', |
| 305 | 'choices' => array( |
| 306 | 'vertical' => __("Vertical",'acf'), |
| 307 | 'horizontal' => __("Horizontal",'acf') |
| 308 | ), |
| 309 | 'conditions' => array( |
| 310 | array( |
| 311 | array( |
| 312 | 'field' => 'field_type', |
| 313 | 'operator' => '==', |
| 314 | 'value' => 'checkbox', |
| 315 | ), |
| 316 | ), |
| 317 | array( |
| 318 | array( |
| 319 | 'field' => 'field_type', |
| 320 | 'operator' => '==', |
| 321 | 'value' => 'radio', |
| 322 | ), |
| 323 | ), |
| 324 | ) |
| 325 | )); |
| 326 | |
| 327 | // Checkbox: toggle |
| 328 | acf_render_field_setting($field, array( |
| 329 | 'label' => __('Toggle','acf'), |
| 330 | 'instructions' => __('Prepend an extra checkbox to toggle all choices','acf'), |
| 331 | 'name' => 'toggle', |
| 332 | 'type' => 'true_false', |
| 333 | 'ui' => 1, |
| 334 | 'conditions' => array( |
| 335 | array( |
| 336 | array( |
| 337 | 'field' => 'field_type', |
| 338 | 'operator' => '==', |
| 339 | 'value' => 'checkbox', |
| 340 | ), |
| 341 | ), |
| 342 | ) |
| 343 | )); |
| 344 | |
| 345 | // Checkbox: other_choice |
| 346 | acf_render_field_setting($field, array( |
| 347 | 'label' => __('Allow Custom','acf'), |
| 348 | 'instructions' => __("Allow 'custom' values to be added", 'acf'), |
| 349 | 'name' => 'allow_custom', |
| 350 | 'type' => 'true_false', |
| 351 | 'ui' => 1, |
| 352 | 'conditions' => array( |
| 353 | array( |
| 354 | array( |
| 355 | 'field' => 'field_type', |
| 356 | 'operator' => '==', |
| 357 | 'value' => 'checkbox', |
| 358 | ), |
| 359 | ), |
| 360 | array( |
| 361 | array( |
| 362 | 'field' => 'field_type', |
| 363 | 'operator' => '==', |
| 364 | 'value' => 'select', |
| 365 | ), |
| 366 | array( |
| 367 | 'field' => 'ui', |
| 368 | 'operator' => '==', |
| 369 | 'value' => '1', |
| 370 | ), |
| 371 | ) |
| 372 | ) |
| 373 | )); |
| 374 | |
| 375 | } |
| 376 | |
| 377 | |
| 378 | /** |
| 379 | * update_field |
| 380 | * |
| 381 | * @param $field |
| 382 | * |
| 383 | * @return mixed |
| 384 | */ |
| 385 | function update_field($field){ |
| 386 | |
| 387 | $field['default_value'] = acf_decode_choices($field['default_value'], true); |
| 388 | |
| 389 | if($field['field_type'] === 'radio'){ |
| 390 | $field['default_value'] = acfe_unarray($field['default_value']); |
| 391 | } |
| 392 | |
| 393 | return $field; |
| 394 | |
| 395 | } |
| 396 | |
| 397 | |
| 398 | /** |
| 399 | * prepare_field |
| 400 | * |
| 401 | * @param $field |
| 402 | * |
| 403 | * @return mixed |
| 404 | */ |
| 405 | function prepare_field($field){ |
| 406 | |
| 407 | // field type |
| 408 | $type = $field['type']; |
| 409 | $field_type = $field['field_type']; |
| 410 | |
| 411 | $field['type'] = $field_type; |
| 412 | $field['wrapper']['data-ftype'] = $type; |
| 413 | |
| 414 | // choices |
| 415 | $field['choices'] = acf_get_taxonomy_labels($field['taxonomy']); |
| 416 | |
| 417 | // allow custom |
| 418 | if($field['allow_custom']){ |
| 419 | |
| 420 | $value = acfe_get($field, 'value'); |
| 421 | $value = acfe_as_array($value); |
| 422 | |
| 423 | foreach($value as $v){ |
| 424 | |
| 425 | // append custom value to choices |
| 426 | if(!isset($field['choices'][ $v ])){ |
| 427 | $field['choices'][ $v ] = $v; |
| 428 | $field['custom_choices'][ $v ] = $v; |
| 429 | } |
| 430 | } |
| 431 | |
| 432 | } |
| 433 | |
| 434 | // return |
| 435 | return $field; |
| 436 | |
| 437 | } |
| 438 | |
| 439 | |
| 440 | /** |
| 441 | * format_value |
| 442 | * |
| 443 | * @param $value |
| 444 | * @param $post_id |
| 445 | * @param $field |
| 446 | * |
| 447 | * @return array|false|mixed|string[] |
| 448 | */ |
| 449 | function format_value($value, $post_id, $field){ |
| 450 | |
| 451 | // bail early |
| 452 | if(empty($value)){ |
| 453 | return $value; |
| 454 | } |
| 455 | |
| 456 | // vars |
| 457 | $is_array = is_array($value); |
| 458 | $value = acfe_as_array($value); |
| 459 | |
| 460 | // moop |
| 461 | foreach($value as &$v){ |
| 462 | |
| 463 | // get object |
| 464 | $object = get_taxonomy($v); |
| 465 | |
| 466 | if(!$object || is_wp_error($object)) continue; |
| 467 | |
| 468 | // return: object |
| 469 | if($field['return_format'] === 'object'){ |
| 470 | $v = $object; |
| 471 | } |
| 472 | |
| 473 | } |
| 474 | |
| 475 | // check array |
| 476 | if(!$is_array){ |
| 477 | $value = acfe_unarray($value); |
| 478 | } |
| 479 | |
| 480 | // return |
| 481 | return $value; |
| 482 | |
| 483 | } |
| 484 | |
| 485 | |
| 486 | /** |
| 487 | * validate_front_value |
| 488 | * |
| 489 | * @param $valid |
| 490 | * @param $value |
| 491 | * @param $field |
| 492 | * @param $input |
| 493 | * @param $form |
| 494 | * |
| 495 | * @return false |
| 496 | */ |
| 497 | function validate_front_value($valid, $value, $field, $input, $form){ |
| 498 | |
| 499 | // bail early |
| 500 | if(!$this->pre_validate_front_value($valid, $value, $field, $form)){ |
| 501 | return $valid; |
| 502 | } |
| 503 | |
| 504 | // custom value allowed |
| 505 | if(!empty($field['allow_custom']) || !empty($field['other_choice'])){ |
| 506 | return $valid; |
| 507 | } |
| 508 | |
| 509 | $value = acfe_as_array($value); |
| 510 | $choices = acfe_as_array($field['taxonomy']); |
| 511 | |
| 512 | // empty choices |
| 513 | if(empty($choices)){ |
| 514 | return $valid; |
| 515 | } |
| 516 | |
| 517 | // check values against choices |
| 518 | if(!empty(array_diff($value, $choices))){ |
| 519 | return false; |
| 520 | } |
| 521 | |
| 522 | // return |
| 523 | return $valid; |
| 524 | |
| 525 | } |
| 526 | |
| 527 | |
| 528 | /** |
| 529 | * translate_field |
| 530 | * |
| 531 | * @param $field |
| 532 | * |
| 533 | * @return mixed |
| 534 | */ |
| 535 | function translate_field($field){ |
| 536 | |
| 537 | $field['placeholder'] = acf_translate($field['placeholder']); |
| 538 | $field['search_placeholder'] = acf_translate($field['search_placeholder']); |
| 539 | |
| 540 | return $field; |
| 541 | |
| 542 | } |
| 543 | |
| 544 | } |
| 545 | |
| 546 | // initialize |
| 547 | acf_register_field_type('acfe_field_taxonomies'); |
| 548 | |
| 549 | endif; |