<?php

/*******************************************************
*
*	Popular Posts Widget
*	By: Andre Gagnon
*	http://www.themewich.com
*
*******************************************************/

// Initialize widget
add_action( 'widgets_init', 'ag_popular_widgets' );

// Register widget
function ag_popular_widgets() {
	register_widget( 'ag_popular_widget' );
}

// Widget class
class ag_popular_widget extends WP_Widget {

/*----------------------------------------------------------*/
/*	Set up the Widget
/*----------------------------------------------------------*/
	
	function __construct() {
	
		/* General widget settings */
		$widget_ops = array( 'classname' => 'ag_popular_widget', 'description' => __('A widget that displays popular posts.', 'themewich') );

		/* Widget control settings */
		$control_ops = array( 'width' => 400, 'height' => 350, 'id_base' => 'ag_popular_widget' );

		/* Create widget */
		parent::__construct( 'ag_popular_widget', __('Themewich Popular Posts Widget', 'themewich'), $widget_ops, $control_ops );
	}

/*----------------------------------------------------------*/
/*	Display The Widget 
/*----------------------------------------------------------*/
	
	function widget( $args, $instance ) {
		extract( $args );
		
		$title = apply_filters('widget_title', $instance['title'] );

		/* Variables from settings. */
		$numposts = $instance['numposts'] ;

		/* Before widget (defined in functions.php). */
		echo wp_kses_post( $before_widget );

		/* Display The Widget */
		?>

 <?php 
/* Display the widget title & subtitle if one was input (before and after defined by themes). */
echo wp_kses_post( $before_title . $title . $after_title ); ?>

<?php $twocol = 'twocol';  ?>
		
<div class="tabswrap">

<ul class="tabs ajax-tabs">
	<li><a class="active" href="#tab2" data-query="weekly"><?php _e('Week', 'themewich')?></a></li>
	<li><a href="#tab2" data-query="monthly"><?php _e('Month', 'themewich')?></a></li>
	<li><a href="#tab2" data-query="all_time"><?php _e('All Time', 'themewich')?></a></li>
</ul>	
<div class="clear"></div>

<!-- Preloading Image -->
<div class="loading"></div>

<ul class="tabs-content" data-posts="<?php echo (int) $numposts; ?>">

	<!-- First Tab 
	================================================-->
	<li class="active">
		
<?php
if (function_exists('wp_most_popular_get_popular')) :
	$posts = wp_most_popular_get_popular( array( 'limit' => $numposts, 'post_type' => 'post', 'range' => 'weekly' ) );
	global $post;
	if ( count( $posts ) > 0 ): foreach ( $posts as $post ):
		setup_postdata( $post );
		?>
		<div class="tabpost">
		
		<?php 
        if ( has_post_thumbnail() ) { // check if the post has a Post Thumbnail assigned to it.?>
	        <div class="featuredimagewidget thumbnailarea">
	            <a class="thumblink" href="<?php the_permalink(); ?>">
	                <?php the_post_thumbnail('tinyfeatured'); ?>
	            </a>
	        </div>
		<?php } else { ?>
		<div class="featurednoimagewidget">
			<i class="fa fa-align-left"></i>
		</div>
		<?php } ?>

		<p class="tab-title"><a href="<?php the_permalink(); ?>" title="<?php echo esc_attr(get_the_title() ? get_the_title() : get_the_ID()); ?>"><?php if ( get_the_title() ) the_title(); else the_ID(); ?></a></p>
		<p class="views"><i class="fa fa-eye"></i> <?php echo themewich_get_post_views(get_the_ID(), 'weekly'); ?></p>
		<div class="clear"></div>
	</div>

		<?php
	endforeach; endif;
endif;
?>

	</li>

	<!-- Second Tab 
	================================================-->
	<li>

	<?php
		query_posts('ignore_sticky_posts=1&posts_per_page='. $numposts);
		if (have_posts()) : while (have_posts()) : the_post(); 
	?>

	<div class="tabpost">
		
        <div class="featuredimagewidget thumbnailarea">
            <?php 
            if ( has_post_thumbnail() ) { // check if the post has a Post Thumbnail assigned to it.?>
                <a class="thumblink" href="<?php the_permalink(); ?>">
                    <?php the_post_thumbnail('tinyfeatured'); ?>
                </a>
            <?php } ?>
        </div>

		<p class="tab-title"><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></p>
		<p class="views"><?php the_time(get_option('date_format')); ?></p>
		<div class="clear"></div>
	</div>
	<?php endwhile; endif; wp_reset_query(); ?>
	
	</li>
</ul>
	<div class="clear"></div>
</div>


<?php
	/* After widget (defined by themes). */
		echo wp_kses_post( $after_widget );
	}

/*----------------------------------------------------------*/
/*	Update the Widget
/*----------------------------------------------------------*/
	
	function update( $new_instance, $old_instance ) {
		
		$instance = $old_instance;
		
		/* Remove HTML: */
		$instance['title'] = strip_tags( $new_instance['title'] );
		$instance['numposts'] = strip_tags( $new_instance['numposts'] );
		$instance['facebook'] = strip_tags( $new_instance['facebook'] );
	
		return $instance;
	}
	

/*----------------------------------------------------------*/
/*	Widget Settings
/*----------------------------------------------------------*/
	 
	function form( $instance ) {

		/* Default widget settings */
		$defaults = array(
		'title' => '',
		'numposts' => '6',
		'facebook' => '',		
		);

		$instance = wp_parse_args( (array) $instance, $defaults ); ?>
<p>
  <label for="<?php echo esc_attr( $this->get_field_id( 'title' ) ); ?>">
        <?php _e('Tabs Title (Optional):', 'themewich') ?>
    </label>
    <input class="widefat" id="<?php echo esc_attr( $this->get_field_id( 'title' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'title' ) ); ?>" value="<?php echo esc_attr( $instance['title'] ); ?>" />
</p>

<p>
  <label for="<?php echo esc_attr( $this->get_field_id( 'numposts' ) ); ?>">
        <?php _e('Number of Posts', 'themewich') ?>
    </label>
    <input class="widefat" id="<?php echo esc_attr( $this->get_field_id( 'numposts' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'numposts' ) ); ?>" value="<?php echo esc_attr( $instance['numposts'] ); ?>" />
</p>

<?php
	}
}
?>