<?php
class Auliettalite_Widget_Popular_Posts extends WP_Widget
{
	function __construct()
    {
		$widget_ops = array(
            'classname'   => 'auliettalite-popular-posts',
            'description' => esc_html__('A widget that displays your popular posts from all categories or a certain', 'auliettalite')
        );
		parent::__construct( 'auliettalite_popular_posts_widget', esc_html__('Auliettalite: Popular Posts', 'auliettalite'), $widget_ops );
	}

	function widget( $args, $instance )
    {
		extract( $args );
		$title        = apply_filters('widget_title', $instance['title'] );
		$categories   = $instance['categories'];
		$number       = $instance['number'];
        
		$query = array(
            'cat'                   => $categories,
            'showposts'             => $number,
            'nopaging'              => 0,
            'post_status'           => 'publish',
            'ignore_sticky_posts'   => 1,
            'meta_key'              => 'auliettalite_post_viewed',
            'orderby'               => 'meta_value_num',
            'order'                 => 'DESC'
        );	
		
        $loop = new WP_Query($query);
        
		if ( $loop->have_posts() ) :
    		echo wp_kses_post( $before_widget );
    		if ( $title ) {
    		    echo wp_kses_post( $before_title . $title . $after_title );
    		} ?>
			<div class="list-popular-post">
			<?php
                while ( $loop->have_posts() ) : 
                $loop->the_post();
            ?>
                <article <?php post_class(); ?>>
                    <?php if ( wp_get_attachment_url(get_post_thumbnail_id()) ) {
                        $featured_image = auliettalite_resize_image( get_post_thumbnail_id(), null, 768, 575, true, false );
                        ?>
                    <div class="post-image">
                        <a href="<?php the_permalink(); ?>">
                            <img src="<?php echo esc_url( $featured_image ); ?>" alt="<?php the_title_attribute(); ?>">
                        </a>
                    </div>
                    <?php } ?>
					<div class="post-content">
                        <h4 class="post-title"><a href="<?php the_permalink() ?>" rel="bookmark"><?php the_title() ?></a></h4>
                        <div class="post-view"><?php echo auliettalite_get_post_view(); ?></div>
                    </div>
                </article>
			<?php endwhile; ?>
            </div><?php
            wp_reset_postdata();
            echo wp_kses_post( $after_widget );
        endif;
	}

	function update( $new_instance, $old_instance )
    {
		$instance = $old_instance;
		$instance['title']        = strip_tags( $new_instance['title'] );
		$instance['categories']   = $new_instance['categories'];
		$instance['number']       = strip_tags( $new_instance['number'] );
		return $instance;
	}

	function form( $instance )
    {
		$defaults = array(
            'title'         => esc_html__('Most Popular Posts', 'auliettalite'),
            'number'        => 2,
            'categories'    => ''
        );
		$instance = wp_parse_args( (array) $instance, $defaults ); ?>
		<p>
			<label for="<?php echo esc_attr( $this->get_field_id( 'title' ) ); ?>"><?php esc_html_e('Title:', 'auliettalite'); ?></label>
			<input  type="text" 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('categories') ); ?>"><?php echo esc_html__('Filter by Category:', 'auliettalite');?></label> 
    		<select id="<?php echo esc_attr( $this->get_field_id('categories') ); ?>" name="<?php echo esc_attr( $this->get_field_name('categories') ); ?>" class="widefat categories" style="width:100%;">
    			<option value='all' <?php if ('all' == $instance['categories']) echo 'selected="selected"'; ?>><?php echo esc_html__('All categories', 'auliettalite');?></option>
    			<?php $categories = get_categories('hide_empty=0&depth=1&type=post'); ?>
    			<?php foreach($categories as $category) { ?>
    			<option value='<?php echo esc_attr( $category->term_id ); ?>' <?php if ($category->term_id == $instance['categories']) echo 'selected="selected"'; ?>><?php echo wp_kses_post( $category->cat_name ); ?></option>
    			<?php } ?>
    		</select>
		</p>
		<p>
			<label for="<?php echo esc_attr( $this->get_field_id( 'number' ) ); ?>"><?php esc_html_e('Number of posts to show:', 'auliettalite'); ?></label>
			<input min="1" type="number" class="widefat" id="<?php echo esc_attr( $this->get_field_id( 'number' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'number' ) ); ?>" value="<?php echo esc_attr( $instance['number'] ); ?>" size="3" />
		</p>
	<?php
	}
}

function auliettalite_popular_posts_init() {
	register_widget( 'Auliettalite_Widget_Popular_Posts' );
}
add_action( 'widgets_init', 'auliettalite_popular_posts_init' );
