<?php

if ( ! function_exists( 'gp_recent_posts' ) ) {
	function gp_recent_posts() {
		register_widget( 'GP_Recent_Posts' );
	}
}
add_action( 'widgets_init', 'gp_recent_posts' );

class GP_Recent_Posts extends WP_Widget {
	
	function GP_Recent_Posts() {
		$widget_ops = array( 'classname' => 'gp-recent-posts', 'description' => __( 'Your site\'s most recent Posts. with thumbnails.', 'gp_lang' ) );
		$this->WP_Widget( 'gp-recent-posts-widget', __( 'GP Recent Posts', 'gp_lang' ), $widget_ops );
	}

	function widget( $args, $instance ) {
		
		global $date_range;
		extract( $args );
		$title = apply_filters( 'widget_title', empty( $instance['title'] ) ? __( 'Recent Posts', 'gp_lang' ) : $instance['title'] );
        $posts = empty( $instance['posts'] ) ? '5' : $instance['posts'];
        $show_date = $instance['show_date'] ? '1' : '0';
        $show_views = $instance['show_views'] ? '1' : '0';
        
		global $gp;
	
		echo html_entity_decode( $before_widget );
			
		?>

		<?php if ( $title ) { echo html_entity_decode( $before_title . $title . $after_title ); } ?>
	
		<?php 

		$args = array( 
		'post_type'      => 'post',
		'posts_per_page' => $posts,
		);

		$featured_query = new wp_query( $args ); ?>
					
		<div class="blog-wrapper blog-standard blog-small-size">
		
			<?php if ( $featured_query->have_posts() ) : ?>
			
				<div class="inner-loop">
			
					<?php while ( $featured_query->have_posts() ) : $featured_query->the_post(); ?>
	
						<article <?php post_class( 'post-item' ); ?>>

							<div class="post-thumbnail featured-content image-align-left">
							
								<a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>">

									<?php if ( has_post_thumbnail() ) { ?>
								
										<?php $image = aq_resize( wp_get_attachment_url( get_post_thumbnail_id( get_the_ID() ) ), 55, 55, true, false, true ); ?>
										<?php if ( $gp['retina'] == 'gp-retina' ) {
											$retina = aq_resize( wp_get_attachment_url( get_post_thumbnail_id( get_the_ID() ) ), 55 * 2, 55 * 2, true, true, true );
										} else {
											$retina = '';
										} ?>
									
										<img src="<?php echo esc_url( $image[0] ); ?>" data-rel="<?php echo esc_url( $retina ); ?>" width="<?php echo absint( $image[1] ); ?>" height="<?php echo absint( $image[2] ); ?>" alt="<?php if ( get_post_meta( get_post_thumbnail_id(), '_wp_attachment_image_alt', true ) ) { echo get_post_meta( get_post_thumbnail_id(), '_wp_attachment_image_alt', true ); } else { the_title_attribute(); } ?>" class="wp-post-image" />
									
									<?php } else { ?>
								
										<?php if ( get_post_format() == 'video' ) { 
											$post_format_class = 'fa-video-camera';
										} elseif ( get_post_format() == 'audio' ) {
											$post_format_class = 'fa-music';
										} elseif ( get_post_format() == 'gallery' ) {
											$post_format_class = 'fa-picture-o';
										} elseif ( get_post_format() == 'quote' ) {
											$post_format_class = 'fa-quote-left';
										} else {
											$post_format_class = 'fa-file-o';
										} ?>
									
										<i class="no-thumbnail fa fa-lg <?php echo sanitize_html_class( $post_format_class ); ?>"></i>	
								
									<?php } ?>
																									
								</a>
							
							</div><!-- END FEATURED IMAGE -->

							<div class="post-item-content image-align-left">
							
								<div class="loop-header">
									<h2 class="entry-title loop-title"><a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>"><?php the_title(); ?></a></h2>
								</div><!-- END ENTRY HEADER -->
						
								<?php if ( $show_date OR $show_views ) { ?>
									<div class="entry-meta">
										<?php if ( $show_date ) { ?><span class="meta-date"><?php the_time( get_option( 'date_format' ) ); ?></span>	<?php } ?>
										<?php if ( function_exists( 'pvc_get_post_views' ) && $show_views ) { ?><span class="meta-views"><?php echo pvc_get_post_views(); ?> <?php _e( 'views', 'gp_lang' ); ?></span><?php } ?>		
									</div>
								<?php } ?>
							
							</div>
													
						</article><!-- END POST -->

					<?php endwhile; ?>
					
				</div>
		
			<?php else : ?>

				<strong><?php _e( 'No items found.', 'gp_lang' ); ?></strong>

			<?php endif; wp_reset_postdata(); ?>
							
		</div><!-- END BLOG WRAPPER -->		

		<?php echo html_entity_decode( $after_widget ); 

	}

	function update( $new_instance, $old_instance ) {
		$instance = $old_instance;
		$instance['title'] = strip_tags( $new_instance['title'] );
		$instance['posts'] = $new_instance['posts'];
		$instance['show_date'] = $new_instance['show_date'];
		$instance['show_views'] = $new_instance['show_views'];			
		return $instance;
	}

	function form( $instance ) {
		
		$defaults = array( 
			'title'     => 'Recent Posts',
			'posts'     => 5,
			'show_date' => 1,
			'show_views' => 0,
		 ); $instance = wp_parse_args( ( array ) $instance, $defaults ); ?>

		<p>
			<label for="<?php echo esc_attr( $this->get_field_id( 'title' ) ); ?>"><?php _e( 'Title:', 'gp_lang' ); ?></label>
			<br/><input type="text" 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( 'posts' ) ); ?>"><?php _e( 'Number of posts to show:', 'gp_lang' ); ?></label> <input type="text" id="<?php echo esc_attr( $this->get_field_id( 'posts' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'posts' ) ) ?>" value="<?php echo esc_attr( $instance['posts'] ); ?>" size="3" />
		</p>	

		<p>
			<input type="checkbox" id="<?php echo esc_attr( $this->get_field_id( 'show_date' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'show_date' ) ); ?>" value="1" <?php checked( $instance['show_date'], 1 ); ?> /><label for="<?php echo esc_attr( $this->get_field_id( 'show_date' ) ); ?>"><?php _e( 'Display post date?', 'gp_lang' ); ?></label>
		</p>

		<p>
			<input type="checkbox" id="<?php echo esc_attr( $this->get_field_id( 'show_views' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'show_views' ) ); ?>" value="1" <?php checked( $instance['show_views'], 1 ); ?> /><label for="<?php echo esc_attr( $this->get_field_id( 'show_views' ) ); ?>"><?php _e( 'Display post views?', 'gp_lang' ); ?></label>
		</p>
									
		<input type="hidden" name="widget-options" id="widget-options" value="1" />

		<?php

	}
}

?>