<?php
if ( ! class_exists( 'Kayla_Latest_Posts_Widget' ) ) {
    class Kayla_Latest_Posts_Widget extends WP_Widget
    {
    	function __construct()
        {
    		$widget_ops = array( 'classname' => 'widget_latest_posts', 'description' => __('A widget that displays your latest posts from all categories or a certain', 'kayla') );
    		parent::__construct( 'widget_latest_posts', __('Kayla: Latest Posts', 'kayla'), $widget_ops );
    	}
    
    	function widget( $args, $instance )
        {
    		extract( $args );
    		$title        = apply_filters('widget_title', $instance['title'] );
    		$categories   = $instance['categories'];
    		$number       = $instance['number'];
    		$query        = array('showposts' => $number, 'nopaging' => 0, 'post_status' => 'publish', 'ignore_sticky_posts' => 1, 'cat' => $categories);		
    		$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 );
        		} ?>
    			<ul class="latest-posts">
    			<?php  while ( $loop->have_posts() ) : $loop->the_post(); ?>
    				<li>
                        <article <?php post_class(); ?>>
                            <?php if ( $image_url = wp_get_attachment_url( get_post_thumbnail_id() ) ) { ?>
                            <div class="post-format post-standard">
                                <a href="<?php the_permalink(); ?>">
                                    <?php the_post_thumbnail(); ?>
                                </a>
                            </div>
                            <?php } ?>                    
        					<div class="post-content">
        						<?php the_title( '<h4 class="entry-title"><a href="'. esc_url( get_the_permalink() ) .'">', '</a></h4>' ); ?>
        						<div class="entry-meta"><a href="<?php the_permalink(); ?>"><?php echo get_the_date(); ?></a></div>
        					</div>
                        </article>
    				</li>
    			<?php endwhile; ?>
                </ul><?php
                wp_reset_postdata();
                echo wp_kses_post($after_widget);
            endif;
    	}
    
    	function update( $new_instance, $old_instance )
        {
    		$instance = $old_instance;
    		$instance['title']      = sanitize_text_field($new_instance['title']);
    		$instance['categories'] = sanitize_text_field($new_instance['categories']);
    		$instance['number']     = absint($new_instance['number']);
    		return $instance;
    	}
    
    	function form( $instance )
        {
    		$defaults = array( 'title' => __('Latest Posts', 'kayla'), 'number' => 5, '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:', 'kayla'); ?></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 esc_html_e('Filter by Category:', 'kayla'); ?></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 selected($instance['categories'], 'all'); ?>><?php esc_html_e('All categories', 'kayla'); ?></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 selected($instance['categories'], $category->term_id); ?>><?php echo esc_html( $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:', 'kayla'); ?></label>
    			<input 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
    	}
    }

    if ( ! function_exists( 'kayla_latest_posts_widget_init' ) ) {
        function kayla_latest_posts_widget_init() {
        	register_widget( 'Kayla_Latest_Posts_Widget' );
        }
        add_action( 'widgets_init', 'kayla_latest_posts_widget_init' );
    }
}
