Create New Item
×
Item Type
File
Folder
Item Name
×
Search file in folder and subfolders...
File Manager
/
wp-content
/
plugins
/
advanced-custom-fields
/
includes
/
fields
Advanced Search
Upload
New Item
Settings
Back
Back Up
Advanced Editor
Save
<?php if (!class_exists('acf_field_taxonomy')) { class acf_field_taxonomy extends acf_field { var $save_post_terms = array(); function initialize() { $this->name = 'taxonomy'; $this->label = __('Taxonomy', 'acf'); $this->category = 'relational'; $this->defaults = array('taxonomy' => 'category', 'field_type' => 'checkbox', 'multiple' => 0, 'allow_null' => 0, 'return_format' => 'id', 'add_term' => 1, 'load_terms' => 0, 'save_terms' => 0); acf_add_filter_variations('acf/fields/taxonomy/query', array('name', 'key'), 1); acf_add_filter_variations('acf/fields/taxonomy/result', array('name', 'key'), 2); add_action('wp_ajax_acf/fields/taxonomy/query', array($this, 'ajax_query')); add_action('wp_ajax_nopriv_acf/fields/taxonomy/query', array($this, 'ajax_query')); add_action('wp_ajax_acf/fields/taxonomy/add_term', array($this, 'ajax_add_term')); add_action('acf/save_post', array($this, 'save_post'), 15, 1); } function ajax_query() { if (!acf_verify_ajax()) { die; } $response = $this->get_ajax_query($_POST); acf_send_ajax_results($response); } function get_ajax_query($options = array()) { $options = acf_parse_args($options, array('post_id' => 0, 's' => '', 'field_key' => '', 'paged' => 0)); $field = acf_get_field($options['field_key']); if (!$field) { return false; } if (!taxonomy_exists($field['taxonomy'])) { return false; } $results = array(); $is_hierarchical = is_taxonomy_hierarchical($field['taxonomy']); $is_pagination = $options['paged'] > 0; $is_search = false; $limit = 20; $offset = 20 * ($options['paged'] - 1); $args = array('taxonomy' => $field['taxonomy'], 'hide_empty' => false); if ($is_pagination && !$is_hierarchical) { $args['number'] = $limit; $args['offset'] = $offset; } if ($options['s'] !== '') { $s = wp_unslash(strval($options['s'])); $args['search'] = $s; $is_search = true; } $args = apply_filters('acf/fields/taxonomy/query', $args, $field, $options['post_id']); $terms = acf_get_terms($args); if ($is_hierarchical) { $limit = acf_maybe_get($args, 'number', $limit); $offset = acf_maybe_get($args, 'offset', $offset); $parent = acf_maybe_get($args, 'parent', 0); $parent = acf_maybe_get($args, 'child_of', $parent); if (!$is_search) { $ordered_terms = _get_term_children($parent, $terms, $field['taxonomy']); if (!empty($ordered_terms)) { $terms = $ordered_terms; } } if ($is_pagination) { $terms = array_slice($terms, $offset, $limit); } } foreach ($terms as $term) { $results[] = array('id' => $term->term_id, 'text' => $this->get_term_title($term, $field, $options['post_id'])); } $response = array('results' => $results, 'limit' => $limit); return $response; } function get_term_title($term, $field, $post_id = 0) { $title = acf_get_term_title($term); $post_id = $post_id ? $post_id : acf_get_form_data('post_id'); return apply_filters('acf/fields/taxonomy/result', $title, $term, $field, $post_id); } function get_terms($value, $taxonomy = 'category') { if (count($value) > 1) { $terms = acf_get_terms(array('taxonomy' => $taxonomy, 'include' => $value, 'hide_empty' => false)); } foreach (array_keys($value) as $i) { $value[$i] = get_term($value[$i], $taxonomy); } $value = array_filter($value); return $value; } function load_value($value, $post_id, $field) { $value = acf_get_valid_terms($value, $field['taxonomy']); if ($field['load_terms']) { extract(acf_decode_post_id($post_id)); if ($type === 'block') { } $term_ids = wp_get_object_terms($id, $field['taxonomy'], array('fields' => 'ids', 'orderby' => 'none')); if (empty($term_ids) || is_wp_error($term_ids)) { return false; } if (!empty($value)) { $order = array(); foreach ($term_ids as $i => $v) { $order[$i] = array_search($v, $value); } array_multisort($order, $term_ids); } $value = $term_ids; } if ($field['field_type'] == 'select' || $field['field_type'] == 'radio') { $value = array_shift($value); } return $value; } function update_value($value, $post_id, $field) { if (is_array($value)) { $value = array_filter($value); } if ($field['save_terms']) { $taxonomy = $field['taxonomy']; $term_ids = acf_get_array($value); $term_ids = array_map('intval', $term_ids); $old_term_ids = isset($this->save_post_terms[$taxonomy]) ? $this->save_post_terms[$taxonomy] : array(); $this->save_post_terms[$taxonomy] = array_merge($old_term_ids, $term_ids); if (!did_action('acf/save_post')) { $this->save_post($post_id); return $value; } } return $value; } function save_post($post_id) { if (!empty($this->save_post_terms)) { extract(acf_decode_post_id($post_id)); if ($type === 'block') { } foreach ($this->save_post_terms as $taxonomy => $term_ids) { wp_set_object_terms($id, $term_ids, $taxonomy, false); } $this->save_post_terms = array(); } } function format_value($value, $post_id, $field) { if (empty($value)) { return false; } $value = acf_get_array($value); if ($field['return_format'] == 'object') { $value = $this->get_terms($value, $field['taxonomy']); } if ($field['field_type'] == 'select' || $field['field_type'] == 'radio') { $value = array_shift($value); } return $value; } function render_field($field) { $field['value'] = acf_get_array($field['value']); $div = array('class' => 'acf-taxonomy-field', 'data-save' => $field['save_terms'], 'data-ftype' => $field['field_type'], 'data-taxonomy' => $field['taxonomy'], 'data-allow_null' => $field['allow_null']); $taxonomy = get_taxonomy($field['taxonomy']); if (!$taxonomy) { return; } ?> <div <?php acf_esc_attr_e($div); ?>> <?php if ($field['add_term'] && current_user_can($taxonomy->cap->manage_terms)) { ?> <div class="acf-actions -hover"> <a href="#" class="acf-icon -plus acf-js-tooltip small" data-name="add" title="<?php echo esc_attr($taxonomy->labels->add_new_item); ?>"></a> </div> <?php } if ($field['field_type'] == 'select') { $field['multiple'] = 0; $this->render_field_select($field); } elseif ($field['field_type'] == 'multi_select') { $field['multiple'] = 1; $this->render_field_select($field); } elseif ($field['field_type'] == 'radio') { $this->render_field_checkbox($field); } elseif ($field['field_type'] == 'checkbox') { $this->render_field_checkbox($field); } ?> </div> <?php } function render_field_select($field) { $field['type'] = 'select'; $field['ui'] = 1; $field['ajax'] = 1; $field['choices'] = array(); if (!empty($field['value'])) { $terms = $this->get_terms($field['value'], $field['taxonomy']); if (!empty($terms)) { foreach (array_keys($terms) as $i) { $term = acf_extract_var($terms, $i); $field['choices'][$term->term_id] = $this->get_term_title($term, $field); } } } acf_render_field($field); } function render_field_checkbox($field) { acf_hidden_input(array('type' => 'hidden', 'name' => $field['name'])); if ($field['field_type'] == 'checkbox') { $field['name'] .= '[]'; } $taxonomy_obj = get_taxonomy($field['taxonomy']); acf_include('includes/walkers/class-acf-walker-taxonomy-field.php'); $args = array('taxonomy' => $field['taxonomy'], 'show_option_none' => sprintf(_x('No %s', 'No terms', 'acf'), strtolower($taxonomy_obj->labels->name)), 'hide_empty' => false, 'style' => 'none', 'walker' => new ACF_Taxonomy_Field_Walker($field)); $args = apply_filters('acf/fields/taxonomy/wp_list_categories', $args, $field); $args = apply_filters('acf/fields/taxonomy/wp_list_categories/name=' . $field['_name'], $args, $field); $args = apply_filters('acf/fields/taxonomy/wp_list_categories/key=' . $field['key'], $args, $field); ?> <div class="categorychecklist-holder"> <ul class="acf-checkbox-list acf-bl"> <?php wp_list_categories($args); ?> </ul> </div> <?php } function render_field_settings($field) { acf_render_field_setting($field, array('label' => __('Taxonomy', 'acf'), 'instructions' => __('Select the taxonomy to be displayed', 'acf'), 'type' => 'select', 'name' => 'taxonomy', 'choices' => acf_get_taxonomy_labels())); acf_render_field_setting($field, array('label' => __('Appearance', 'acf'), 'instructions' => __('Select the appearance of this field', 'acf'), 'type' => 'select', 'name' => 'field_type', 'optgroup' => true, 'choices' => array(__('Multiple Values', 'acf') => array('checkbox' => __('Checkbox', 'acf'), 'multi_select' => __('Multi Select', 'acf')), __('Single Value', 'acf') => array('radio' => __('Radio Buttons', 'acf'), 'select' => _x('Select', 'noun', 'acf'))))); acf_render_field_setting($field, array('label' => __('Allow Null?', 'acf'), 'instructions' => '', 'name' => 'allow_null', 'type' => 'true_false', 'ui' => 1, 'conditions' => array('field' => 'field_type', 'operator' => '!=', 'value' => 'checkbox'))); acf_render_field_setting($field, array('label' => __('Create Terms', 'acf'), 'instructions' => __('Allow new terms to be created whilst editing', 'acf'), 'name' => 'add_term', 'type' => 'true_false', 'ui' => 1)); acf_render_field_setting($field, array('label' => __('Save Terms', 'acf'), 'instructions' => __('Connect selected terms to the post', 'acf'), 'name' => 'save_terms', 'type' => 'true_false', 'ui' => 1)); acf_render_field_setting($field, array('label' => __('Load Terms', 'acf'), 'instructions' => __('Load value from posts terms', 'acf'), 'name' => 'load_terms', 'type' => 'true_false', 'ui' => 1)); acf_render_field_setting($field, array('label' => __('Return Value', 'acf'), 'instructions' => '', 'type' => 'radio', 'name' => 'return_format', 'choices' => array('object' => __('Term Object', 'acf'), 'id' => __('Term ID', 'acf')), 'layout' => 'horizontal')); } function ajax_add_term() { $args = wp_parse_args($_POST, array('nonce' => '', 'field_key' => '', 'term_name' => '', 'term_parent' => '')); if (!acf_verify_ajax()) { die; } $field = acf_get_field($args['field_key']); if (!$field) { die; } $taxonomy_obj = get_taxonomy($field['taxonomy']); $taxonomy_label = $taxonomy_obj->labels->singular_name; if (!current_user_can($taxonomy_obj->cap->manage_terms)) { wp_send_json_error(array('error' => sprintf(__('User unable to add new %s', 'acf'), $taxonomy_label))); } if ($args['term_name']) { if (term_exists($args['term_name'], $field['taxonomy'], $args['term_parent'])) { wp_send_json_error(array('error' => sprintf(__('%s already exists', 'acf'), $taxonomy_label))); } $extra = array(); if ($args['term_parent']) { $extra['parent'] = (int) $args['term_parent']; } $data = wp_insert_term($args['term_name'], $field['taxonomy'], $extra); if (is_wp_error($data)) { wp_send_json_error(array('error' => $data->get_error_message())); } $term = get_term($data['term_id']); $prefix = ''; $ancestors = get_ancestors($term->term_id, $term->taxonomy); if (!empty($ancestors)) { $prefix = str_repeat('- ', count($ancestors)); } wp_send_json_success(array('message' => sprintf(__('%s added', 'acf'), $taxonomy_label), 'term_id' => $term->term_id, 'term_name' => $term->name, 'term_label' => $prefix . $term->name, 'term_parent' => $term->parent)); } ?> <form method="post"> <?php acf_render_field_wrap(array('label' => __('Name', 'acf'), 'name' => 'term_name', 'type' => 'text')); if (is_taxonomy_hierarchical($field['taxonomy'])) { $choices = array(); $response = $this->get_ajax_query($args); if ($response) { foreach ($response['results'] as $v) { $choices[$v['id']] = $v['text']; } } acf_render_field_wrap(array('label' => __('Parent', 'acf'), 'name' => 'term_parent', 'type' => 'select', 'allow_null' => 1, 'ui' => 0, 'choices' => $choices)); } ?> <p class="acf-submit"> <button class="acf-submit-button button button-primary" type="submit"><?php _e('Add', 'acf'); ?></button> </p> </form><?php die; } } acf_register_field_type('acf_field_taxonomy'); }