<?php
add_action('widgets_init', 'pyre_latest_reviews_load_widgets');

function pyre_latest_reviews_load_widgets()
{
	register_widget('Pyre_Latest_Reviews_Widget');
}

class Pyre_Latest_Reviews_Widget extends WP_Widget {
	
	function Pyre_Latest_Reviews_Widget()
	{
		$widget_ops = array('classname' => 'pyre_latest_reviews', 'description' => '');

		$control_ops = array('id_base' => 'pyre_latest_reviews-widget');

		$this->WP_Widget('pyre_latest_reviews-widget', 'Broadway: Latest Reviews', $widget_ops, $control_ops);
	}
	
	function widget($args, $instance)
	{
		global $post;

		extract($args);
		
		$title = $instance['title'];
		$posts = $instance['posts'];
		
		echo $before_widget;
		?>
		<!-- BEGIN WIDGET -->
		<?php
		if($title) {
			echo $before_title.$title.$after_title;
		}
		?>
		
		<?php
		$recent_posts = new WP_Query(array(
			'showposts' => $posts,
			'meta_query' => array(
				array(
					'key' => 'pyre_overall_score',
					'value' => '0.5',
					'compare' => '>='
				)
			),
		));
		?>

		<div class='block-item-small-container'>
		<?php while($recent_posts->have_posts()): $recent_posts->the_post(); ?>
		<div class="block-item-small clearfix">
			<?php if(has_post_thumbnail()): ?>
			<div class="post-thumb-container">
				<?php
				if(has_post_format('video') || has_post_format('audio') || has_post_format('gallery')) {
					$icon = '<span class="post-format ' . get_post_format($post->ID) . '"></span>';
				} else {
					$icon = '';
				}
				echo $icon;
				?>
				<a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>" class="post-thumbnail">
					<?php the_post_thumbnail('one-column-small', array('alt' => get_the_title(), 'title' => get_the_title())); ?>
				</a>
			</div>
			<?php endif; ?>
			<div class="post-desc">
				<h4><a href='<?php the_permalink(); ?>' title='<?php the_title(); ?>'><?php the_title(); ?></a></h4>
				<p class="post-meta"><?php the_time('F jS, Y'); ?>, by <?php the_author_posts_link(); ?></p>
				<?php if(get_post_meta($post->ID, 'pyre_overall_score', true)): ?>
				<div class="normal-post-score"><img src="<?php bloginfo('template_directory'); ?>/images/stars/<?php echo get_post_meta($post->ID, 'pyre_overall_score', true); ?>.png" alt="" /></div>
				<?php endif; ?>
			</div>
		</div>
		<?php endwhile; ?>
		</div>
		<!-- END WIDGET -->
		<?php
		echo $after_widget;
	}
	
	function update($new_instance, $old_instance)
	{
		$instance = $old_instance;
		
		$instance['title'] = $new_instance['title'];
		$instance['posts'] = $new_instance['posts'];
		
		return $instance;
	}

	function form($instance)
	{
		$defaults = array('title' => 'Latest Reviews', 'posts' => 3);
		$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('posts'); ?>">Number of posts:</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 }
}
?>