<?php   
/* 
Plugin Name: Trending Posts
Description: Trending posts - most popular
Version: 1.0 
Author: Stefan Naumovski 
*/    

add_action( 'widgets_init', 'trending_posts' );

function trending_posts() {register_widget( 'trending_widget_t1t' );}

class trending_widget_t1t extends WP_Widget {
	
	/* Register widget with WordPress. */
	
	function __construct() {
		parent::__construct(
			'trending_widget_t1t', // Widget ID
			'Trending posts', // Name
			array( 'description' =>'Most popular posts.', ) // Args
			);}
		
		/* Front-end display of widget. */
		
		public function widget( $args, $instance ) {
		/* Default widget settings. */
		
			$defaults = array( 'title' => 'Trending posts', 'number' => 4, 'duration' => 'forever');
			$instance = wp_parse_args( (array) $instance, $defaults );			

			$title = $instance['title'];
			$duration = $instance['duration'];
			$number = $instance['number'];
			
			$args['before_widget'] = str_replace('class="home-widget', 'class="home-widget one-part' , $args['before_widget']);	
			echo $args['before_widget'];
			?>

<div class="trending-posts">
	<?php 			
		if ( ! empty( $title ) )
		echo  '<div class="trending-title">'.$title.'</div>';
	?>
	<ul>
<?php 
if( $duration == 'week'){					
	$week = date('W');
		$pop = array(
			'posts_per_page'=> $number,
			'w' => $week,
			'meta_key' => 'post_views_count',
			'orderby' => 'meta_value_num',
			'order'    => 'DESC'
			);
			
} elseif ($duration == 'year'){
	$year = date('Y');
		$pop = array(
			'posts_per_page'=> $number,
			'year'     => $year,
			'meta_key' => 'post_views_count',
			'orderby' => 'meta_value_num',
			'order'    => 'DESC'
			);	
} elseif($duration == 'month'){	
	$month = date('m');
		$pop = array(
			'posts_per_page'=> $number,
			'monthnum'     => $month,
			'meta_key' => 'post_views_count',
			'orderby' => 'meta_value_num',
			'order'    => 'DESC'
			);
}elseif($duration == 'forever'){
		$pop = array(
			'posts_per_page'=> $number,
			'meta_key' => 'post_views_count',
			'orderby' => 'meta_value_num',
			'order'    => 'DESC'
			);	
}




$t1t_posts = new WP_Query($pop); while ( $t1t_posts->have_posts()) : $t1t_posts->the_post();
?>



		<li>
		
			<div class="trending-posts-category">
			<?php $category = get_the_category(); if($category[0]){echo '<a href="'.get_category_link($category[0]->term_id ).'">'.$category[0]->cat_name.'</a>';} ?>
			</div>
			<div class="trending-posts-title">
				<a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>">
				<?php the_title(); ?>
				</a>
			</div>
			<!--trending-posts-title-->
		</li>
		<?php endwhile; ?>
	</ul>
</div>
<!--most-commented-->

<?php

	/* After widget. */
	
	echo $args['after_widget'];
	}


	/* Widget settings. */


	function update( $new_instance, $old_instance ) {
		$instance = $old_instance;
		
	/* Strip tags. */
		
		$instance['title'] =  $new_instance['title'];
		$instance['duration'] =  $new_instance['duration'];
		$instance['number'] = $new_instance['number'];
		
		return $instance;
	}
	
	
	function form( $instance ) {
		
	/* Default widget settings. */
		
		$defaults = array('title' => 'Trending posts', 'number' => 4, 'duration' => 'forever');
		$instance = wp_parse_args( (array) $instance, $defaults ); ?>

<!-- Widget Title-->
<p>
	<label for="<?php echo $this->get_field_id( 'title' ); ?>">
		Title:
	</label>
	<input id="<?php echo $this->get_field_id( 'title' ); ?>" name="<?php echo $this->get_field_name( 'title' ); ?>" value="<?php echo $instance['title']; ?>" style="width:90%;" />
</p>

<!--Duration-->
<p>
	<label for="<?php echo $this->get_field_id('duration'); ?>">Popular time:</label>
	<select name="<?php echo $this->get_field_name('duration'); ?>" id="<?php echo $this->get_field_id('duration'); ?>" class="widefat" >
		<?php $options = array('week', 'month', 'year', 'forever');
		foreach ($options as $option) {?>
		<option value='<?php echo $option; ?>' <?php if ($option == $instance['duration']) echo 'selected="selected"'; ?>><?php echo $option; ?></option>
		<?php } ?>
	</select>
</p>

<!-- Number of posts -->
<p>
	<label for="<?php echo $this->get_field_id( 'number' ); ?>">
		Number of posts to show:
	</label>
	<input id="<?php echo $this->get_field_id( 'number' ); ?>" name="<?php echo $this->get_field_name( 'number' ); ?>" value="<?php echo $instance['number']; ?>" size="3" />
</p>
<?php }} ?>