<?php
/* 
Plugin Name: Tabs (Tabs-widget) 
Description:  Tabs widget that shows Lastest, Popular and most commented posts.
Version: 1.0 
Author: Stefan Naumovski 
*/    
add_action( 'widgets_init', 'Tabs_widget' );

function Tabs_widget() {register_widget( 'Tabs' );}

class Tabs extends WP_Widget {
	
	/* Register widget with WordPress. */
	
	function __construct() {
		parent::__construct(
			'Tabs_tn', 	//Widget ID
			'Tabs Widget', // Name
			array( 'description' => 'Tabs widget that shows Lastest, Popular and most commented posts', ) // Args
		);}
		
		/* Front-end display of widget. */
		
	function widget( $args, $instance ) {
		extract( $args );
		$latest_title = $instance['latest_title'];
		$popular_title = $instance['popular_title'];
		$comments_title = $instance['comments_title'];
		$latest_number = $instance['latest_number'];
		$categories = $instance['categories'];
		$popular_number = $instance['popular_number'];
		$comment_number = $instance['comment_number'];
		echo $args['before_widget'];
		?>

<div class="tabber-container">
	<ul class="tabs">
		<li>
			<h4><a href="#tab1"><?php echo $latest_title; ?></a></h4>
		</li>
		<li>
			<h4><a href="#tab2"><?php echo $popular_title; ?></a></h4>
		</li>
		<li>
			<h4><a href="#tab3"><?php echo $comments_title; ?></a></h4>
		</li>
	</ul>
	<div id="tab1" class="tabber-content">
		<ul class="featured-posts-category-small">
			<?php $tn_posts = new WP_Query(array('cat' => $categories, 'posts_per_page' => $latest_number, 'ignore_sticky_posts' => 1 )); while($tn_posts->have_posts()) : $tn_posts->the_post();?>
                    <li>
                        <div class="small-featured-posts-image">
                            <?php if (  (function_exists('has_post_thumbnail')) && (has_post_thumbnail())  ) { ?>
                            <a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>">
                            <?php the_post_thumbnail('small-thumb'); ?>
                            </a>
                            <?php } ?>
                        </div><!--small-featured-posts-image-->
                        <div class="small-featured-posts-text">
                            <h2><a href="<?php the_permalink() ?>" class="small-featured-posts-title">
                                <?php $tit = the_title('','',FALSE); echo substr($tit, 0, 45); if (strlen($tit) > 45) echo " ..."; ?>
                                </a></h2>
                            <div class="small-featured-posts-content">
                                <?php echo excerpt(10); ?>
                            </div><!--small-featured-posts-content-->	
                            <div class="small-featured-posts-date-posted">
                                <div class="post-date">
                                    <span class="date">Posted <?php echo human_time_diff( get_the_time('U'), current_time('timestamp') ) . ' ago'; ?></span>
                                </div><!--post-date-->
                            </div><!--small-featured-posts-date-posted--> 
                        </div><!--small-featured-posts-text--> 
                    </li>
			<?php endwhile; ?>
		</ul>
	</div><!--tab1-->
	<div id="tab2" class="tabber-content">
		<?php $today = getdate();
			$tn_pop = array(
				'posts_per_page'=> $popular_number,
				'year'     => $today["year"],
				'monthnum' => $today["mon"],
				'meta_key' => 'post_views_count',
				'orderby' => 'meta_value_num',
				'order'    => 'DESC'
				);
					$popular_posts = new WP_Query($tn_pop); if($popular_posts->have_posts()): ?>
		<ul class="featured-posts-category-small">
			<?php while($popular_posts->have_posts()): $popular_posts->the_post(); ?>
                    <li>
                        <div class="small-featured-posts-image">
                            <?php if (  (function_exists('has_post_thumbnail')) && (has_post_thumbnail())  ) { ?>
                            <a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>">
                            <?php the_post_thumbnail('small-thumb');  ?>
                            </a>
                            <?php } ?>
                        </div><!--small-featured-posts-image-->
                        <div class="small-featured-posts-text">
                            <h2><a href="<?php the_permalink() ?>" class="small-featured-posts-title">
                                <?php $tit = the_title('','',FALSE); echo substr($tit, 0, 45); if (strlen($tit) > 45) echo " ..."; ?>
                                </a></h2>
                            <div class="small-featured-posts-content">
                                <?php echo excerpt(10); ?>
                            </div><!--small-featured-posts-content-->	
                            <div class="small-featured-posts-date-posted">
                                <div class="post-date">
                                    <span class="date">Posted <?php echo human_time_diff( get_the_time('U'), current_time('timestamp') ) . ' ago'; ?></span>
                                </div><!--post-date-->
                            </div><!--small-featured-posts-date-posted--> 
                        </div><!--small-featured-posts-text--> 
                    </li>
			<?php endwhile; ?>
		</ul>
		<?php endif; ?>
	</div><!--tab2-->
	
	
	
	<div id="tab3" class="tabber-content">
		<ul class="featured-posts-category-small">
				<?php
			
				$comments = get_comments('status=approve&number='.$comment_number.'');
				
				foreach($comments as $comment) :?>
			
				<?php 
					$comment_post_id = $comment->comment_post_ID ;
					$post_id_comms = get_post($comment_post_id);
					$long = 70;
					$short_comment = $comment->comment_content;
							
					if ( strlen($short_comment) > $long ) {
						$short_comment = substr($short_comment,0,$long);
						$short_comment = $short_comment .' ...';
					}
							 ?> 
				<li>
					<div class="gravatar-comment">
								<?php echo get_avatar( $comment, '60' ); ?>
					</div>
				
					<div class="comment-author-name">
						<?php echo($comment->comment_author);?>
					</div>
					<div class="comment-date-time">
						<?php echo($comment->comment_date);?>
					</div>
					<a href="<?php echo get_permalink($comment_post_id) ?>#comment-<?php echo $comment->comment_post_ID?>" title="on <?php echo $short_comment; ?>"><?php echo $short_comment; ?></a>
				</li>
		<?php endforeach;?>
		</ul>
	</div><!--tab3-->
	
	
</div><!--tabber-container-->
<?php

	/* After widget. */	
	
		echo $after_widget;
	}
	
	/* Widget settings. */
	
	function update( $new_instance, $old_instance ) {
		$instance = $old_instance;
		
		/* Strip tags. */
		
		$instance['latest_title'] = strip_tags( $new_instance['latest_title'] );
		$instance['popular_title'] = strip_tags( $new_instance['popular_title'] );
		$instance['comments_title'] = strip_tags( $new_instance['comments_title'] );
		$instance['latest_number'] = strip_tags( $new_instance['latest_number'] );
		$instance['categories'] = $new_instance['categories'];
		$instance['popular_number'] = strip_tags( $new_instance['popular_number'] );
		$instance['comment_number'] = strip_tags( $new_instance['comment_number'] );
		return $instance;
	}
	
		/* Default widget settings. */
		
	function form( $instance ) {
		$defaults = array( 'latest_title' => 'Latest','popular_title' => 'Popular','comments_title' => 'Comments', 'latest_number' => 3, 'popular_number' => 3, 'comment_number' => 3);
		$instance = wp_parse_args( (array) $instance, $defaults ); ?>
	
		<!--Latest-posts-title-->
		<p>
		  <label for="<?php echo $this->get_field_id( 'latest_title' ); ?>">Latest Title:</label>
		  <input id="<?php echo $this->get_field_id( 'latest_title' ); ?>" name="<?php echo $this->get_field_name( 'latest_title' ); ?>" value="<?php echo $instance['latest_title']; ?>" style="width:90%;" />
		</p>
		
		<!--Latest posts number of posts-->
		<p>
		  <label for="<?php echo $this->get_field_id( 'latest_number' ); ?>">Number of "Latest" posts to show:</label>
		  <input id="<?php echo $this->get_field_id( 'latest_number' ); ?>" name="<?php echo $this->get_field_name( 'latest_number' ); ?>" value="<?php echo $instance['latest_number']; ?>" size="3" />
		</p>
		
		<!--Latest posts category-->
		<p>
			<label for="<?php echo $this->get_field_id('categories'); ?>">(Optional)Select Category:</label>
			<select id="<?php echo $this->get_field_id('categories'); ?>" name="<?php echo $this->get_field_name('categories'); ?>" style="width:100%;">
				<option value='all' <?php if ('all' == (isset($instance['categories']))) echo 'selected="selected"'; ?>>All Categories</option>
				<?php $categories = get_categories('hide_empty=0&depth=1&type=post'); ?>
				<?php foreach($categories as $category) { ?>
				<option value='<?php echo $category->term_id; ?>' <?php if(isset($instance['categories'])){ if ($category->term_id == $instance['categories']) echo 'selected="selected"';}?>><?php echo $category->cat_name; ?></option>
				<?php } ?>
			</select>
		</p>
		
        <!--Popular-posts-title-->
		<p>
		  <label for="<?php echo $this->get_field_id( 'popular_title' ); ?>">Popular Title:</label>
		  <input id="<?php echo $this->get_field_id( 'popular_title' ); ?>" name="<?php echo $this->get_field_name( 'popular_title' ); ?>" value="<?php echo $instance['popular_title']; ?>" style="width:90%;" />
		</p>
        
		<!--Popular posts number of posts-->
		<p>
		  <label for="<?php echo $this->get_field_id( 'popular_number' ); ?>">Number of "Popular" posts to show:</label>
		  <input id="<?php echo $this->get_field_id( 'popular_number' ); ?>" name="<?php echo $this->get_field_name( 'popular_number' ); ?>" value="<?php echo $instance['popular_number']; ?>" size="3" />
		</p>
		
		<!--Comments-posts-title-->
		<p>
		  <label for="<?php echo $this->get_field_id( 'comments_title' ); ?>">Comments Title:</label>
		  <input id="<?php echo $this->get_field_id( 'comments_title' ); ?>" name="<?php echo $this->get_field_name( 'comments_title' ); ?>" value="<?php echo $instance['comments_title']; ?>" style="width:90%;" />
		</p>
        
		<!--Comment number-->
		<p>
		  <label for="<?php echo $this->get_field_id( 'comment_number' ); ?>">Number of "Comments" to show:</label>
		  <input id="<?php echo $this->get_field_id( 'comment_number' ); ?>" name="<?php echo $this->get_field_name( 'comment_number' ); ?>" value="<?php echo $instance['comment_number']; ?>" size="3" />
		</p>
<?php }} ?>