/**
     * Outputs the content of the widget.
     *
     * @param array args The array of form elements
     * @param array instance The current instance of the widget
     */
    public function widget($args, $instance)
    {
        extract($args, EXTR_SKIP);
        $instance = wp_parse_args($instance, $this->defaults);
        //$title = $instance['title'];
        $title = isset($instance['title']) ? esc_attr($instance['title']) : '';
        $view_count_meta_key = '';
        $valid_sort_orders = array('date', 'title', 'comment_count', 'rand', 'modified', 'popular', 'helpful');
        if (in_array($instance['sort_by'], $valid_sort_orders)) {
            $sort_by = $instance['sort_by'];
            $sort_order = (bool) $instance['asc_sort_order'] ? 'ASC' : 'DESC';
        } else {
            // by default, display latest first
            $sort_by = 'date';
            $sort_order = 'DESC';
        }
        if ($instance['sort_by'] == 'popular') {
            $sort_by = 'meta_value_num';
            $view_count_meta_key = '_ht_kb_post_views_count';
        }
        if ($instance['sort_by'] == 'helpful') {
            $sort_by = 'meta_value_num';
            $view_count_meta_key = HT_USEFULNESS_KEY;
        }
        // Setup time/date
        $post_date = the_date('Y-m-d', '', '', false);
        $month_ago = date("Y-m-d", mktime(0, 0, 0, date("m") - 1, date("d"), date("Y")));
        if ($post_date > $month_ago) {
            $post_date = sprintf(__('%1$s ago', 'example'), human_time_diff(get_the_time('U'), current_time('timestamp')));
        } else {
            $post_date = get_the_date();
        }
        $args = array('post_type' => 'ht_kb', 'orderby' => $sort_by, 'order' => $sort_order, 'meta_key' => $view_count_meta_key, 'posts_per_page' => $instance["num"], 'ignore_sticky_posts' => 1);
        $category = $instance['category'];
        if ($category == '' || $category == 'all') {
            //do nothing - no tax query required
        } else {
            //tax query
            $args['tax_query'] = array(array('taxonomy' => 'ht_kb_category', 'field' => 'term_id', 'terms' => $category, 'operator' => 'IN'));
        }
        echo $before_widget;
        if ($title) {
            echo $before_title . $title . $after_title;
        }
        $wp_query = new WP_Query($args);
        if ($wp_query->have_posts()) {
            ?>

        <ul>

        <?php 
            while ($wp_query->have_posts()) {
                $wp_query->the_post();
                ?>

            <?php 
                $post_format = get_post_format();
                $pf_class = 'hkb-widget-article__format-';
                $pf_class .= !empty($post_format) ? $post_format : 'standard';
                ?>

            <li class="<?php 
                echo $pf_class;
                ?>
"> 

                <a class="hkb-widget__entry-title" href="<?php 
                the_permalink();
                ?>
"><?php 
                the_title();
                ?>
</a>

                <?php 
                if ($instance['comment_num'] || $instance['comment_num']) {
                    ?>
                <ul class="hkb-meta">

                    <?php 
                    if ($instance['rating']) {
                        ?>
      
                        <?php 
                        $article_usefulness = ht_usefulness(get_the_ID());
                        $helpful_article = $article_usefulness >= 0 ? true : false;
                        $helpful_article_class = $helpful_article ? 'hkb-meta__usefulness--good' : 'hkb-meta__usefulness--bad';
                        ?>
                        <li class="hkb-meta__usefulness <?php 
                        echo $helpful_article_class;
                        ?>
">
                          <?php 
                        echo $article_usefulness;
                        ?>
                        </li>
                    <?php 
                    }
                    //rating
                    ?>

                    <?php 
                    if ($instance['comment_num']) {
                        ?>
                        <?php 
                        $number = get_comments_number(get_the_ID());
                        if ($number != 0) {
                            ?>
                            <li class="hkb-meta__comments">
                                <?php 
                            comments_number(__('0', 'ht-knowledge-base'), __('1', 'ht-knowledge-base'), __('%', 'ht-knowledge-base'));
                            ?>
                            </li>
                        <?php 
                        }
                        //number
                        ?>
                    <?php 
                    }
                    //instance
                    ?>

                </ul>
                <?php 
                }
                ?>

            </li>
            <?php 
            }
            ?>

        </ul>

        <?php 
        }
        echo $after_widget;
    }
<?php

if (function_exists('ht_usefulness')) {
    ?>
    <?php 
    $article_usefulness = ht_usefulness(get_the_ID());
    $helpful_article = $article_usefulness >= 0 ? true : false;
    $helpful_article_class = $helpful_article ? 'hkb-meta__usefulness--good' : 'hkb-meta__usefulness--bad';
    ?>
    <li class="hkb-meta__usefulness <?php 
    echo $helpful_article_class;
    ?>
"><?php 
    echo $article_usefulness;
    ?>
</li>
<?php 
}
//end function exists