<?php
add_action('widgets_init', 'reedwan_latest_reviews_load_widgets');

function reedwan_latest_reviews_load_widgets()
{
	register_widget('Reedwan_Latest_Reviews_Widget');
}

class Reedwan_Latest_Reviews_Widget extends WP_Widget {
	
	function Reedwan_Latest_Reviews_Widget()
	{
		$widget_ops = array('classname' => 'reedwan_latest_reviews_widget', 'description' => 'Latest reviews widget');

		$control_ops = array('id_base' => 'reedwan_latest_reviews_widget');

		$this->WP_Widget('reedwan_latest_reviews_widget', 'Backstreet : Latest Reviews', $widget_ops, $control_ops);
	}
	
	function widget($args, $instance)
	{
		extract($args);
		$title = $instance['title'];
		$post_type = 'all';
		$categories = $instance['categories'];
		$posts = $instance['posts'];
		$images = true;
		echo $before_widget;	
		?>
		<!-- BEGIN WIDGET -->
		<?php
		if($title) {
			echo $before_title.$title.$after_title;
		}
		?>
		<?php
		$post_types = get_post_types();
		unset($post_types['page'], $post_types['post'], $post_types['page'], $post_types['attachment'], $post_types['revision'], $post_types['nav_menu_item']);
		
		if($post_type == 'all') {
			$post_type_array = $post_types;
		} else {
			$post_type_array = $post_type;
		}
		?>
			<div class="tab_content">
				<?php if($instance['categories']):?>
					<?php $recent_posts1 = new WP_Query(array('showposts' => $posts,'post_type' => $post_type_array,'review_category' => $categories,)); ?>
					<?php while($recent_posts1->have_posts()): $recent_posts1->the_post(); ?>
					<div class="block-tabs">
						<?php if($images && has_post_thumbnail()): ?>
							<?php $image = wp_get_attachment_image_src(get_post_thumbnail_id(), 'small-post-thumb'); ?>
							<div class="tabs-image"><a class="reviews-icon" href='<?php the_permalink(); ?>' title='<?php the_title(); ?>'><img class="fadeover" src="<?php echo $image[0]; ?>" alt="<?php the_title(); ?>"  width='75' height='60' /></a>
							</div>
						<?php else: ?>
							<div class="tabs-image"><a class="reviews-icon" href='<?php the_permalink(); ?>' title='<?php the_title(); ?>'><img class="fadeover" src="<?php echo get_template_directory_uri(); ?>/timthumb.php?src=<?php echo get_template_directory_uri(); ?>/images/thumbnail.png&w=75&h=60" alt="<?php the_title(); ?>"  width='75' height='60' /></a>
							</div>
						<?php endif; ?>
						<div class="description">
							<h3><a href='<?php the_permalink(); ?>' title='<?php the_title(); ?>'><?php the_title(); ?></a></h3>
							<div class="tabs-meta">
								<?php the_time(get_option('date_format')); ?> 
							</div>
							<?php if( get_post_meta(get_the_ID(), 'reedwan_overall_score', true)): ?>
							<div class="review">
								<span><img src="<?php echo get_template_directory_uri(); ?>/images/stars2/<?php echo get_post_meta(get_the_ID(), 'reedwan_overall_score', true); ?>.png" alt="" /></span>
							</div>
							<?php endif; ?>
						</div>	
					</div>
					<?php endwhile; ?>
				<?php endif; ?>
			</div>
		<!-- END WIDGET -->
		<div class="clear"></div>
		<?php
		echo $after_widget;
	}
	
	function update($new_instance, $old_instance)
	{
		$instance = $old_instance;
		$instance['title'] = $new_instance['title'];
		$instance['post_type'] = 'all';
		$instance['categories'] = $new_instance['categories'];
		$instance['posts'] = $new_instance['posts'];

		return $instance;
	}

	function form($instance)
	{
		$defaults = array('title'=>'Latest Reviews', 'posts' => 3, 'categories'=>'' );
		$instance = wp_parse_args((array) $instance, $defaults); ?>
		<p>
			<label for="<?php echo $this->get_field_id('title'); ?>">Title:</label>
			<input class="widefat" style="width: 216px;" id="<?php echo $this->get_field_id('title'); ?>" name="<?php echo $this->get_field_name('title'); ?>" value="<?php echo $instance['title']; ?>" />
		</p>
		<p>
			<label for="<?php echo $this->get_field_id('categories'); ?>">Select Category:</label> 
			<?php $tax_terms = get_terms('review_category'); ?>
			<select id="<?php echo $this->get_field_id( 'categories' ); ?>" name="<?php echo $this->get_field_name( 'categories' ); ?>" class="widefat categories" style="width:100%;">
				<option selected="selected"><?php echo $instance['categories']; ?></option>
			<?php foreach ($tax_terms as $tax_term) { ?>
				<option><?php echo $tax_term->name; ?></option>
			<?php } ?>
			</select>

		</p>
		<p>
			<label for="<?php echo $this->get_field_id('posts'); ?>">Number of reviews:</label>
			<input class="widefat" style="width: 30px;" id="<?php echo $this->get_field_id('posts'); ?>" name="<?php echo $this->get_field_name('posts'); ?>" value="<?php echo $instance['posts']; ?>" />
		</p>
	<?php }
}
?>