Example #1
0
function hocwp_post_get_top_parent_terms($post, $taxonomy = 'any')
{
    $result = array();
    $taxonomies = array($taxonomy);
    if ('any' === $taxonomy) {
        $taxonomies = hocwp_post_get_taxonomies($post);
    }
    foreach ($taxonomies as $tax) {
        $terms = wp_get_post_terms($post->ID, $tax);
        foreach ($terms as $term) {
            if ($term->parent != 0) {
                $term = hocwp_term_get_top_most_parent($term);
            }
            $result[] = $term;
        }
    }
    return $result;
}
 public function widget($args, $instance)
 {
     $this->instance = $instance;
     $before_widget = hocwp_get_value_by_key($args, 'before_widget');
     $taxonomy = $this->get_taxonomy_from_instance($instance);
     $taxonomies = array();
     $widget_class = '';
     foreach ($taxonomy as $tax) {
         $tax = hocwp_get_value_by_key($tax, 'value');
         if (!empty($tax)) {
             $taxonomies[] = $tax;
             hocwp_add_string_with_space_before($widget_class, hocwp_sanitize_html_class($tax));
         }
     }
     $before_widget = hocwp_add_class_to_string('', $before_widget, $widget_class);
     $args['before_widget'] = $before_widget;
     $in_current_post = (bool) hocwp_get_value_by_key($instance, 'in_current_post', hocwp_get_value_by_key($this->args, 'in_current_post'));
     if ($in_current_post && (!is_singular() || is_page())) {
         return;
     }
     if ($in_current_post) {
         $post_id = get_the_ID();
         $current_post = get_post($post_id);
         $obj_taxs = get_object_taxonomies($current_post->post_type);
         $has_tax = false;
         foreach ($taxonomies as $tax) {
             foreach ($obj_taxs as $tax_name) {
                 if ($tax == $tax_name) {
                     $has_tax = true;
                     break;
                 }
             }
         }
         if (!$has_tax) {
             return;
         }
         $before_widget = hocwp_add_class_to_string('', $before_widget, 'in-current-post');
         $args['before_widget'] = $before_widget;
     }
     $number = hocwp_get_value_by_key($instance, 'number', hocwp_get_value_by_key($this->args, 'number'));
     if (0 > $number) {
         $number = 0;
     }
     $thumbnail_size = hocwp_get_value_by_key($instance, 'thumbnail_size', hocwp_get_value_by_key($this->args, 'thumbnail_size'));
     $thumbnail_size = hocwp_sanitize_size($thumbnail_size);
     $full_width_item = hocwp_get_value_by_key($instance, 'full_width_item', hocwp_get_value_by_key($this->args, 'full_width_item'));
     $show_count = hocwp_get_value_by_key($instance, 'show_count', hocwp_get_value_by_key($this->args, 'show_count'));
     $hide_thumbnail = hocwp_get_value_by_key($instance, 'hide_thumbnail', hocwp_get_value_by_key($this->args, 'hide_thumbnail'));
     $only_thumbnail = hocwp_get_value_by_key($instance, 'only_thumbnail', hocwp_get_value_by_key($this->args, 'only_thumbnail'));
     $order = hocwp_get_value_by_key($instance, 'order', hocwp_get_value_by_key($this->args, 'order'));
     $orderby = hocwp_get_value_by_key($instance, 'orderby', hocwp_get_value_by_key($this->args, 'orderby'));
     $count_format = hocwp_get_value_by_key($instance, 'count_format', hocwp_get_value_by_key($this->args, 'count_format'));
     $different_name = (bool) hocwp_get_value_by_key($instance, 'different_name', hocwp_get_value_by_key($this->args, 'different_name'));
     $hide_empty = hocwp_get_value_by_key($instance, 'hide_empty', hocwp_get_value_by_key($this->args, 'hide_empty'));
     $only_parent = hocwp_get_value_by_key($instance, 'only_parent', hocwp_get_value_by_key($this->args, 'only_parent'));
     $child_of_current = hocwp_get_value_by_key($instance, 'child_of_current', hocwp_get_value_by_key($this->args, 'child_of_current'));
     $child_of_parent = hocwp_get_value_by_key($instance, 'child_of_parent', hocwp_get_value_by_key($this->args, 'child_of_parent'));
     $parent_as_title = hocwp_get_value_by_key($instance, 'parent_as_title', hocwp_get_value_by_key($this->args, 'parent_as_title'));
     if ($hide_thumbnail) {
         $only_thumbnail = false;
     }
     $defaults = array('order' => $order, 'orderby' => $orderby, 'number' => absint($number));
     if (0 == $hide_empty || !(bool) $hide_empty) {
         $defaults['hide_empty'] = 0;
     } else {
         $defaults['hide_empty'] = 1;
     }
     if ($only_parent) {
         $defaults['parent'] = 0;
     }
     if ($in_current_post && is_singular()) {
         $terms = wp_get_post_terms(get_the_ID(), $taxonomies);
     } elseif (($child_of_current || $child_of_parent) && is_tax()) {
         $current_term = hocwp_term_get_current();
         $tax_args = array('child_of' => $current_term->term_id);
         $tax_args = wp_parse_args($tax_args, $defaults);
         unset($tax_args['parent']);
         $terms = hocwp_get_terms($current_term->taxonomy, $tax_args);
         if ($child_of_parent) {
             if (!hocwp_array_has_value($terms) && hocwp_id_number_valid($current_term->parent)) {
                 $parent = hocwp_term_get_top_most_parent($current_term);
                 $tax_args['child_of'] = $parent->term_id;
                 $terms = hocwp_get_terms($parent->taxonomy, $tax_args);
             }
         }
     } else {
         $terms = hocwp_get_terms($taxonomies, $defaults);
     }
     if (($in_current_post || $child_of_current) && !hocwp_array_has_value($terms)) {
         return;
     }
     if ($parent_as_title && is_tax()) {
         $current_term = hocwp_term_get_current();
         if (hocwp_id_number_valid($current_term->parent)) {
             $parent = hocwp_term_get_top_most_parent($current_term);
             $instance['title'] = $parent->name;
         } elseif ($child_of_current) {
             $instance['title'] = $current_term->name;
         }
     }
     hocwp_widget_before($args, $instance);
     ob_start();
     if (hocwp_array_has_value($terms)) {
         $count_terms = count($terms);
         $html = '<ul class="list-unstyled list-terms">';
         $count = 0;
         foreach ($terms as $term) {
             $item_class = 'term-item';
             hocwp_add_string_with_space_before($item_class, hocwp_sanitize_html_class('tax-' . $term->taxonomy));
             if (!(bool) $hide_thumbnail) {
                 hocwp_add_string_with_space_before($item_class, 'show-thumbnail');
             }
             if ((bool) $only_thumbnail) {
                 hocwp_add_string_with_space_before($item_class, 'only-thumbnail');
             }
             $full_width = hocwp_widget_item_full_width_result($full_width_item, $count_terms, $count);
             if ($full_width) {
                 hocwp_add_string_with_space_before($item_class, 'full-width');
             }
             if ((bool) $show_count) {
                 hocwp_add_string_with_space_before($item_class, 'show-count');
             } else {
                 hocwp_add_string_with_space_before($item_class, 'no-count');
             }
             $html .= '<li class="' . $item_class . '">';
             if (!(bool) $hide_thumbnail) {
                 $html .= hocwp_term_get_thumbnail_html(array('term' => $term, 'width' => $thumbnail_size[0], $thumbnail_size[1], 'bfi_thumb' => false));
             }
             if (!(bool) $only_thumbnail) {
                 $term_name = $term->name;
                 if ($different_name) {
                     $term_name = hocwp_term_get_name($term);
                 }
                 $html .= '<a class="term-name" href="' . get_term_link($term) . '">' . $term_name . '</a>';
                 if ((bool) $show_count && !empty($count_format)) {
                     $html .= ' <span class="count">' . str_replace('%TERM_COUNT%', $term->count, $count_format) . '</span>';
                 }
             }
             $html .= '</li>';
             $count++;
         }
         $html .= '</ul>';
         echo $html;
     } else {
         _e('Sorry, nothing found.', 'hocwp-theme');
     }
     $widget_html = ob_get_clean();
     $widget_html = apply_filters('hocwp_widget_term_html', $widget_html, $args, $instance, $this);
     echo $widget_html;
     hocwp_widget_after($args, $instance);
 }