<?php

if (! defined('ABSPATH')) {
    exit;
} trait PufferGo_Hierarchical_Taxonomy_Nav
{
    private function get_direct_child_terms($taxonomy, $parent_id)
    {
        $terms = get_terms(array('taxonomy' => $taxonomy, 'parent' => $parent_id, 'orderby' => 'name', 'order' => 'ASC', 'hide_empty' => false, ));

        if (is_wp_error($terms) || empty($terms)) {
            return array();
        } $result = array();

        foreach ($terms as $term) {
            if (! $this->term_has_content($taxonomy, $term->term_id, (int) $term->count)) {
                continue;
            } $link = get_term_link($term, $taxonomy);
            $result[] = array('id' => (int) $term->term_id, 'name' => (string) $term->name, 'slug' => (string) $term->slug, 'url' => is_wp_error($link) ? '' : $link, );
        }

return $result;
    }

    private function term_has_content($taxonomy, $term_id, $direct_count = 0)
    {
        if ($direct_count > 0) {
            return true;
        } $descendant_ids = get_term_children($term_id, $taxonomy);

        if (is_wp_error($descendant_ids) || empty($descendant_ids)) {
            return false;
        }

foreach ($descendant_ids as $descendant_id) {
            $descendant = get_term($descendant_id, $taxonomy);

            if ($descendant && ! is_wp_error($descendant) && (int) $descendant->count > 0) {
                return true;
            }
        }

return false;
    }

    private function get_term_ancestor_breadcrumb_items($taxonomy, $term_id)
    {
        $items = array();
        $ancestor_ids = array_reverse(array_map('intval', get_ancestors($term_id, $taxonomy, 'taxonomy')));

        foreach ($ancestor_ids as $ancestor_id) {
            $ancestor = get_term($ancestor_id, $taxonomy);

            if (! $ancestor || is_wp_error($ancestor)) {
                continue;
            } $link = get_term_link($ancestor, $taxonomy);
            $items[] = array('url' => is_wp_error($link) ? null : $link, 'label' => (string) $ancestor->name, );
        }

return $items;
    }
}
