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-taxonomy.php
153 lines
| 1 | <?php |
| 2 | |
| 3 | if(!defined('ABSPATH')){ |
| 4 | exit; |
| 5 | } |
| 6 | |
| 7 | if(!class_exists('acfe_field_taxonomy')): |
| 8 | |
| 9 | class acfe_field_taxonomy extends acfe_field_extend{ |
| 10 | |
| 11 | /** |
| 12 | * initialize |
| 13 | */ |
| 14 | function initialize(){ |
| 15 | |
| 16 | $this->name = 'taxonomy'; |
| 17 | $this->replace = array( |
| 18 | 'load_value', |
| 19 | 'update_value', |
| 20 | ); |
| 21 | |
| 22 | } |
| 23 | |
| 24 | |
| 25 | /** |
| 26 | * load_value |
| 27 | * |
| 28 | * @param $value |
| 29 | * @param $post_id |
| 30 | * @param $field |
| 31 | * |
| 32 | * @return mixed |
| 33 | */ |
| 34 | function load_value($value, $post_id, $field){ |
| 35 | |
| 36 | // disable load terms for local meta & acfe_form |
| 37 | if(acfe_is_local_post_id($post_id) || acfe_starts_with($post_id, 'acfe_form')){ |
| 38 | $field['load_terms'] = false; |
| 39 | } |
| 40 | |
| 41 | // return |
| 42 | return $this->instance->load_value($value, $post_id, $field); |
| 43 | |
| 44 | } |
| 45 | |
| 46 | |
| 47 | /** |
| 48 | * update_value |
| 49 | * |
| 50 | * @param $value |
| 51 | * @param $post_id |
| 52 | * @param $field |
| 53 | * |
| 54 | * @return array|mixed |
| 55 | */ |
| 56 | function update_value($value, $post_id, $field){ |
| 57 | |
| 58 | // disable save terms for local meta & acfe_form |
| 59 | if(acfe_is_local_post_id($post_id) || acfe_starts_with($post_id, 'acfe_form')){ |
| 60 | $field['save_terms'] = false; |
| 61 | } |
| 62 | |
| 63 | // return |
| 64 | return $this->instance->update_value($value, $post_id, $field); |
| 65 | |
| 66 | } |
| 67 | |
| 68 | |
| 69 | /** |
| 70 | * format_front_value |
| 71 | * |
| 72 | * @param $formatted |
| 73 | * @param $unformatted |
| 74 | * @param $post_id |
| 75 | * @param $field |
| 76 | * @param $form |
| 77 | * |
| 78 | * @return string |
| 79 | */ |
| 80 | function format_front_value($formatted, $unformatted, $post_id, $field, $form){ |
| 81 | |
| 82 | // vars |
| 83 | $value = acfe_as_array($unformatted); |
| 84 | $array = array(); |
| 85 | |
| 86 | // loop values |
| 87 | foreach($value as $term_id){ |
| 88 | |
| 89 | // get term |
| 90 | $term = get_term($term_id); |
| 91 | |
| 92 | // validate |
| 93 | if($term && !is_wp_error($term)){ |
| 94 | $array[] = $term->name; |
| 95 | } |
| 96 | |
| 97 | } |
| 98 | |
| 99 | // merge |
| 100 | return implode(', ', $array); |
| 101 | |
| 102 | } |
| 103 | |
| 104 | |
| 105 | /** |
| 106 | * validate_front_value |
| 107 | * |
| 108 | * @param $valid |
| 109 | * @param $value |
| 110 | * @param $field |
| 111 | * @param $input |
| 112 | * @param $form |
| 113 | * |
| 114 | * @return false |
| 115 | */ |
| 116 | function validate_front_value($valid, $value, $field, $input, $form){ |
| 117 | |
| 118 | // bail early |
| 119 | if(!$this->pre_validate_front_value($valid, $value, $field, $form)){ |
| 120 | return $valid; |
| 121 | } |
| 122 | |
| 123 | // cast array |
| 124 | $value = acfe_as_array($value); |
| 125 | |
| 126 | // loop values |
| 127 | foreach($value as $v){ |
| 128 | |
| 129 | // get post |
| 130 | $term = get_term($v); |
| 131 | |
| 132 | // check term exists |
| 133 | if(!$term || is_wp_error($term)){ |
| 134 | return false; |
| 135 | } |
| 136 | |
| 137 | // check if term is part of $field['taxonomy'] |
| 138 | if(!in_array($term->taxonomy, (array) $field['taxonomy'], true)){ |
| 139 | return false; |
| 140 | } |
| 141 | |
| 142 | } |
| 143 | |
| 144 | // return |
| 145 | return $valid; |
| 146 | |
| 147 | } |
| 148 | |
| 149 | } |
| 150 | |
| 151 | acf_new_instance('acfe_field_taxonomy'); |
| 152 | |
| 153 | endif; |