File "api-term.php"

Full path: /home/kosmetik/public_html/wp-content/plugins/advanced-custom-fields/includes/api/api-term.php
File size: 6.16 B
MIME-type: text/x-php
Charset: utf-8

Download   Open   Edit   Advanced Editor   Back

<?php

function acf_get_taxonomies($args = array())
{
    $taxonomies = array();
    $objects = get_taxonomies($args, 'objects');
    foreach ($objects as $i => $object) {
        if ($object->_builtin && !$object->public) {
            continue;
        }
        $taxonomies[] = $i;
    }
    if (isset($args['post_type'])) {
        $taxonomies = acf_get_taxonomies_for_post_type($args['post_type']);
    }
    $taxonomies = apply_filters('acf/get_taxonomies', $taxonomies, $args);
    return $taxonomies;
}
function acf_get_taxonomies_for_post_type($post_types = 'post')
{
    $taxonomies = array();
    foreach ((array) $post_types as $post_type) {
        $object_taxonomies = get_object_taxonomies($post_type);
        foreach ((array) $object_taxonomies as $taxonomy) {
            $taxonomies[] = $taxonomy;
        }
    }
    $taxonomies = array_unique($taxonomies);
    return $taxonomies;
}
function acf_get_taxonomy_labels($taxonomies = array())
{
    if (empty($taxonomies)) {
        $taxonomies = acf_get_taxonomies();
    }
    $ref = array();
    $data = array();
    foreach ($taxonomies as $taxonomy) {
        $object = get_taxonomy($taxonomy);
        $label = $object->labels->singular_name;
        $data[$taxonomy] = $label;
        if (!isset($ref[$label])) {
            $ref[$label] = 0;
        }
        $ref[$label]++;
    }
    foreach ($data as $taxonomy => $label) {
        if ($ref[$label] > 1) {
            $data[$taxonomy] .= ' (' . $taxonomy . ')';
        }
    }
    return $data;
}
function acf_get_term_title($term)
{
    $title = $term->name;
    if ($title === '') {
        $title = __('(no title)', 'acf');
    }
    if (is_taxonomy_hierarchical($term->taxonomy)) {
        $ancestors = get_ancestors($term->term_id, $term->taxonomy);
        $title = str_repeat('- ', count($ancestors)) . $title;
    }
    return $title;
}
function acf_get_grouped_terms($args)
{
    $data = array();
    $args = wp_parse_args($args, array('taxonomy' => null, 'hide_empty' => false, 'update_term_meta_cache' => false));
    $taxonomies = acf_get_taxonomy_labels(acf_get_array($args['taxonomy']));
    $is_single = count($taxonomies) == 1;
    $args['taxonomy'] = array_keys($taxonomies);
    if (!$is_single) {
        add_filter('terms_clauses', '_acf_terms_clauses', 10, 3);
    }
    $terms = get_terms($args);
    if (!$is_single) {
        remove_filter('terms_clauses', '_acf_terms_clauses', 10, 3);
    }
    foreach ($taxonomies as $taxonomy => $label) {
        $this_terms = array();
        foreach ($terms as $term) {
            if ($term->taxonomy == $taxonomy) {
                $this_terms[] = $term;
            }
        }
        if (empty($this_terms)) {
            continue;
        }
        if (is_taxonomy_hierarchical($taxonomy) && empty($args['s'])) {
            $all_terms = get_terms(array_merge($args, array('number' => 0, 'offset' => 0, 'taxonomy' => $taxonomy)));
            $length = count($this_terms);
            $offset = 0;
            foreach ($all_terms as $i => $term) {
                if ($term->term_id == $this_terms[0]->term_id) {
                    $offset = $i;
                    break;
                }
            }
            $parent = acf_maybe_get($args, 'parent', 0);
            $parent = acf_maybe_get($args, 'child_of', $parent);
            $ordered_terms = _get_term_children($parent, $all_terms, $taxonomy);
            if (count($ordered_terms) == count($all_terms)) {
                $this_terms = array_slice($ordered_terms, $offset, $length);
            }
        }
        $data[$label] = array();
        foreach ($this_terms as $term) {
            $data[$label][$term->term_id] = $term;
        }
    }
    return $data;
}
function _acf_terms_clauses($pieces, $taxonomies, $args)
{
    if (is_array($taxonomies)) {
        $sql = "FIELD(tt.taxonomy,'" . implode("', '", array_map('esc_sql', $taxonomies)) . "')";
        $pieces['orderby'] = str_replace('ORDER BY', "ORDER BY {$sql},", $pieces['orderby']);
    }
    return $pieces;
}
function acf_get_pretty_taxonomies($taxonomies = array())
{
    return acf_get_taxonomy_labels($taxonomies);
}
function acf_get_term($term_id, $taxonomy = '')
{
    if (is_string($term_id) && strpos($term_id, ':')) {
        list($taxonomy, $term_id) = explode(':', $term_id);
        $term = get_term_by('slug', $term_id, $taxonomy);
        if ($term) {
            return $term;
        }
    }
    return get_term($term_id, $taxonomy);
}
function acf_encode_term($term)
{
    return "{$term->taxonomy}:{$term->slug}";
}
function acf_decode_term($string)
{
    if (is_string($string) && strpos($string, ':')) {
        list($taxonomy, $slug) = explode(':', $string);
        return compact('taxonomy', 'slug');
    }
    return false;
}
function acf_get_encoded_terms($values)
{
    $terms = array();
    foreach ((array) $values as $value) {
        $term = acf_get_term($value);
        if ($term instanceof WP_Term) {
            $terms[] = $term;
        }
    }
    return $terms;
}
function acf_get_choices_from_terms($terms, $format = 'term_id')
{
    $groups = array();
    $labels = acf_get_taxonomy_labels();
    $term = reset($terms);
    if (!$term instanceof WP_Term) {
        $terms = acf_get_encoded_terms($terms);
    }
    foreach ($terms as $term) {
        $group = $labels[$term->taxonomy];
        $choice = acf_get_choice_from_term($term, $format);
        $groups[$group][$choice['id']] = $choice['text'];
    }
    return $groups;
}
function acf_get_choices_from_grouped_terms($value, $format = 'term_id')
{
    $groups = array();
    foreach ($value as $group => $terms) {
        $groups[$group] = array();
        foreach ($terms as $term_id => $term) {
            $choice = acf_get_choice_from_term($term, $format);
            $groups[$group][$choice['id']] = $choice['text'];
        }
    }
    return $groups;
}
function acf_get_choice_from_term($term, $format = 'term_id')
{
    $id = $term->term_id;
    $text = acf_get_term_title($term);
    if ($format == 'slug') {
        $id = acf_encode_term($term);
    }
    return array('id' => $id, 'text' => $text);
}
function acf_get_term_post_id($taxonomy, $term_id)
{
    _deprecated_function(__FUNCTION__, '5.9.2', 'string format term_%d');
    return 'term_' . $term_id;
}