<?php
if ( ! class_exists( 'Sarahlist_Latest_Posts_Widget' ) ) {
    class Sarahlist_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', 'loza') );
    		parent::__construct( 'widget_latest_posts', __('Sarahlist: Latest Posts', 'loza'), $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">
                                    <?php
                                        if ( ! get_theme_mod('loza_post_hide_date_time') ) {
                                            $archive_year  = get_the_time('Y'); 
                                            $archive_month = get_the_time('m'); 
                                            $archive_day   = get_the_time('d'); ?>
                                            <a href="<?php echo esc_url( get_day_link( $archive_year, $archive_month, $archive_day) ); ?>"><?php the_date(); ?></a><?php
                                        }
                                    ?>
                                </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', 'loza'), '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:', 'loza'); ?></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:', 'loza'); ?></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', 'loza'); ?></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:', 'loza'); ?></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( 'loza_latest_posts_widget_init' ) ) {
        function loza_latest_posts_widget_init() {
        	register_widget( 'Sarahlist_Latest_Posts_Widget' );
        }
        add_action( 'widgets_init', 'loza_latest_posts_widget_init' );
    }
}
