<?php
class rst_recent_post_widget extends WP_Widget {
	
	function rst_recent_post_widget() {
		/* Widget settings. */
		$widget_ops = array( 'classname' => 'widget_recent_post', 'description' => 'A widget that show recent posts' );

		/* Create the widget. */
		parent::__construct( 'rst-recent-posts-widget', 'Laza - Recent Posts', $widget_ops);	
	}
	 
	function widget($args, $instance) {
		$html = '';
		$html .= $args['before_widget'];
		if( !empty($instance['title']) ) {
			$html .= $args['before_title'];
				$html .= $instance['title'];
			$html .= $args['after_title'];
		}
		$html .= '<div>';
		
		if( absint($instance['number_posts']) > 0 ) {
			$lastposts = get_posts( array( 'posts_per_page' => absint($instance['number_posts']) ) );
			if( sizeof($lastposts) > 0 ) {
				foreach( $lastposts as $post  ) {
				setup_postdata( $post  );
				
				$html .= '<div class="media">';	
					if( get_post_thumbnail_id($post->ID) ) {
						$html .= '<div class="media-left">';
							$html .= '<a href="'. get_the_permalink($post->ID) .'"><img class="media-object" src="'. bfi_thumb(rst_get_attachment_image_src(get_post_thumbnail_id($post->ID),'small'),array('width' => 80,'height'=> 80)) .'" alt=""></a>';
						$html .= '</div>';
					}
					elseif( get_post_format( $post->ID ) == 'gallery' && !get_post_thumbnail_id($post->ID) ){
						$html .= '<div class="media-left">';
							foreach(rs::getField('rst_gallery', $post -> ID) as $thumbnail_id){
								$html .= '<a href="'. get_the_permalink($post->ID) .'"><img class="media-object" src="'. bfi_thumb(rst_get_attachment_image_src($thumbnail_id,'small'),array('width' => 80,'height'=> 80)) .'" alt=""></a>';
								break;
							}
						$html .= '</div>';
					}
					
					$html .= '<div class="media-body">';
						$html .= '<h4 class="media-heading"><a href="'. get_the_permalink($post->ID) .'">'. $post->post_title .'</a></h4>';
						
						$html .= '<span class="meta-date">'. mysql2date('d F Y', $post->post_date) .'</span>';
					$html .= '</div>';
				$html .= '</div>';
				}
				wp_reset_postdata();
			}
				
		}
			
		$html .= '</div>';
		$html .= $args['after_widget'];
		echo force_balance_tags($html);
	}
 
	function update($new_instance, $old_instance) {
		return $new_instance;		
	}
 
	function form($instance) {
		?><br/>
		<label>Title:
		<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 isset($instance['title']) ? esc_attr($instance['title']) : ''; ?>" style="width:100%" /></label>
		<br /><br />
		<label>Number of posts to show:
		<input type="text" id="<?php echo esc_attr($this->get_field_id( 'number_posts' )); ?>" name="<?php echo esc_attr($this->get_field_name( 'number_posts' )); ?>" value="<?php echo isset($instance['number_posts']) ? esc_attr($instance['number_posts']) : '3'; ?>" size="3" /></label>
		
		<?php	
	}
	
}

add_action( 'widgets_init', 'create_recent_posts_widget' );

function create_recent_posts_widget(){
	return register_widget("rst_recent_post_widget");
}