<?php
add_action('widgets_init', 'serpentsoft_tags_widget_init');

function serpentsoft_tags_widget_init() {
    register_widget('serpentsoft_tags_widget');
}

class serpentsoft_tags_widget extends WP_Widget {

    function serpentsoft_tags_widget() {
        $widget_ops = array('classname' => 'w-tags', 'description' => '');
        $control_ops = array('width' => 250, 'height' => 350, 'id_base' => 'id-widget-tags');
        $this->WP_Widget('id-widget-tags', theme_name . ' - Tags/Categories Box', $widget_ops, $control_ops);
    }

    function widget($args, $instance) {
        extract($args);
        $title = apply_filters('widget_title', $instance['title']);
        $taxonomy = $instance['taxonomy'];
        $count = $instance['count'];
        
        if(empty($count) || !is_numeric($count) || $count == 0) { $count = 0; }
        
        echo $before_widget;
        if ($title) :
            echo $before_title;
            echo $title;
            echo $after_title;
        endif;

        $output = '';
        
        if ($taxonomy == 'tags'):
            $output = '<div class="tags-cloud">';
        
            $args = array(
                'number'                    => $count,
                'orderby'                   => 'count', 
                'order'                     => 'DESC',
            );
            $posttags = get_terms( 'post_tag', $args );
              
            foreach($posttags as $tag):
                $output .= sprintf('<a href="%1$s" class="btn-theme">%2$s<span class="badge">%3$s</span></a>', get_tag_link($tag->term_id), $tag->name, $tag->count );
            endforeach;
            
            $output .= '</div>';
        
        elseif( $taxonomy == 'cats' ):
            $output = '<div class="tags-cloud">';
        
            $args = array(
                'number'                    => $count,
                'orderby'                   => 'count', 
                'order'                     => 'DESC',
            );
            $posttags = get_terms( 'category', $args );
              
            foreach($posttags as $tag):
                $output .= sprintf('<a href="%1$s" class="btn-theme">%2$s<span class="badge">%3$s</span></a>', get_category_link($tag->term_id), $tag->name, $tag->count );
            endforeach;
            
            $output .= '</div>';
        endif;
        
        echo $output;
        
        echo $after_widget;
    }

    function update($new_instance, $old_instance) {
        $instance = $old_instance;
        $instance['title'] = strip_tags($new_instance['title']);
        $instance['taxonomy'] = strip_tags($new_instance['taxonomy']);
        $instance['count'] = strip_tags($new_instance['count']);

        return $instance;
    }

    function form($instance) {
        $defaults = array('title' => '', 'taxonomy' => 'tags', 'count' => '45');
        $instance = wp_parse_args((array) $instance, $defaults); ?>
        <p>
            <label for="<?php echo $this->get_field_id('title'); ?>"><?php _e('Title', 'serpentsoft'); ?>: </label>
            <input id="<?php echo $this->get_field_id('title'); ?>" name="<?php echo $this->get_field_name('title'); ?>" value="<?php echo $instance['title']; ?>" class="widefat" type="text" />
        </p>

        <p>
            <label for="<?php echo $this->get_field_id('taxonomy'); ?>"><?php _e('Taxonomy', 'serpentsoft'); ?>: </label>

            <select id="<?php echo $this->get_field_id('taxonomy'); ?>" name="<?php echo $this->get_field_name('taxonomy'); ?>" >
                <option value="tags" <?php if($instance['taxonomy'] == 'tags') echo "selected=\"selected\""; else echo ""; ?>><?php _e('Tags', 'serpentsoft'); ?></option>
                <option value="cats" <?php if($instance['taxonomy'] == 'cats') echo "selected=\"selected\""; else echo ""; ?>><?php _e('Categories', 'serpentsoft'); ?></option>
            </select>
        </p>
        
        <p>
            <label for="<?php echo $this->get_field_id('count'); ?>"><?php _e('Count (Zero = View all)', 'serpentsoft'); ?>: </label>
            <input id="<?php echo $this->get_field_id('count'); ?>" name="<?php echo $this->get_field_name('count'); ?>" value="<?php echo $instance['count']; ?>" class="widefat" type="text" />
        </p>
        
    <?php } } ?>