<?php
/**
 * Plugin Name: Trending image grid Widget
 */

add_action( 'widgets_init', 'rehub_postimagetrend_load_widget' );

function rehub_postimagetrend_load_widget() {
	register_widget( 'rehub_postimagetrend_widget' );
}

class rehub_postimagetrend_widget extends WP_Widget {

    function __construct() {
		$widget_ops = array( 'classname' => 'postimagetrend', 'description' => __('Widget that displays image grid posts. Use only in sidebar!', 'rehub_framework') );
		$control_ops = array( 'width' => 250, 'height' => 350, 'id_base' => 'rehub_postimagetrend' );
        parent::__construct('rehub_postimagetrend', __('ReHub: Image grid posts', 'rehub_framework'), $widget_ops, $control_ops );
    }

/**
 * How to display the widget on the screen.
 */
function widget( $args, $instance ) {
	extract( $args );

	/* Our variables from the widget settings. */
	$title = apply_filters('widget_title', $instance['title'] );
	$tags = $instance['tags'];
	$number = $instance['number'];
	if( !empty($instance['nohead']) ) $nohead = ' nohead';
	else $nohead = '';	
	if( !empty($instance['two']) ) $two = ' two_column';
	else $two = '';	
	global $post;
	
	if(!empty($tags)) :
		$query = array('showposts' => $number, 'nopaging' => 0, 'post_status' => 'publish', 'ignore_sticky_posts' => 1, 'tag' => $tags);
	else :
		$query = array('showposts' => $number, 'nopaging' => 0, 'post_status' => 'publish', 'ignore_sticky_posts' => 1);
	endif;	
	$loop = new WP_Query($query);
	
	/* Before widget (defined by themes). */
	echo $before_widget;

	if ($loop->have_posts()) :

	/* Display the widget title if one was input (before and after defined by themes). */
	if ( $title )
		echo $before_title . $title . $after_title;
	?>
		<div class="postimagetrend<?php echo $nohead; echo $two;?>">		
		<?php  while ($loop->have_posts()) : $loop->the_post(); ?>	
		<div class="wrap">
			<a href="<?php the_permalink();?>" class="view-link">
                <?php $img = get_post_thumb(); ?>
                <?php if(!empty($instance['two'])) : ?>
                    <?php $params = array( 'width' => 166,  'crop' => true );?>
                <?php else : ?>
                    <?php $params = array( 'width' => 336,'crop' => true);?>
                <?php endif ; ?>
                <?php if(!empty($img)) : ?>
                  	<img src="<?php echo bfi_thumb( $img, $params ); ?>" alt="<?php the_title_attribute(); ?>" />
                <?php else :?>
                	<img src="<?php echo get_template_directory_uri() . '/images/default/noimage_336_220.png' ?>" alt="<?php the_title_attribute(); ?>" />
                <?php endif ; ?>
				<h4><?php the_title();?></h4>
			</a>	
		</div>	
		<?php endwhile; ?>
		</div>
		<?php wp_reset_query(); ?>
		<?php endif; ?>
			
	<?php

	/* After widget (defined by themes). */
	echo $after_widget;
}


	/**
	 * Update the widget settings.
	 */
	function update( $new_instance, $old_instance ) {
		$instance = $old_instance;

		/* Strip tags for title and name to remove HTML (important for text inputs). */
		$instance['title'] = strip_tags( $new_instance['title'] );
		$instance['tags'] = strip_tags($new_instance['tags']);
		$instance['number'] = strip_tags( $new_instance['number'] );
		$instance['nohead'] =  $new_instance['nohead'];
		$instance['two'] =  $new_instance['two'];
		return $instance;
	}


	function form( $instance ) {

		/* Set up some default widget settings. */
		$defaults = array( 'title' => __('Trending', 'rehub_framework'), 'number' => 6, 'tags' => '', 'nohead' => true, 'two' => false);
		$instance = wp_parse_args( (array) $instance, $defaults ); ?>
		

		<p>
			<label for="<?php echo $this->get_field_id( 'title' ); ?>"><?php _e('Title of widget:', 'rehub_framework'); ?></label>
			<input  type="text" class="widefat" 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( 'number' ); ?>"><?php _e('Number of posts to show:', 'rehub_framework'); ?></label>
			<input  type="text" class="widefat" id="<?php echo $this->get_field_id( 'number' ); ?>" name="<?php echo $this->get_field_name( 'number' ); ?>" value="<?php echo $instance['number']; ?>" size="3" />
		</p>
		<p>
			<label for="<?php echo $this->get_field_id( 'tags' ); ?>"><?php _e('Enter tag slug:', 'rehub_framework'); ?></label>
			<input  type="text" class="widefat" id="<?php echo $this->get_field_id( 'tags' ); ?>" name="<?php echo $this->get_field_name( 'tags' ); ?>" value="<?php echo $instance['tags']; ?>"  />
		</p>
		<p>
			<label for="<?php echo $this->get_field_id( 'nohead' ); ?>"><?php _e('Disable headings  ?', 'rehub_framework'); ?></label>
			<input id="<?php echo $this->get_field_id( 'nohead' ); ?>" name="<?php echo $this->get_field_name( 'nohead' ); ?>" value="true" <?php if( $instance['nohead'] ) echo 'checked="checked"'; ?> type="checkbox" />
		</p>
		<p>
			<label for="<?php echo $this->get_field_id( 'two' ); ?>"><?php _e('Two column?', 'rehub_framework'); ?></label>
			<input id="<?php echo $this->get_field_id( 'two' ); ?>" name="<?php echo $this->get_field_name( 'two' ); ?>" value="false" <?php if( $instance['two'] ) echo 'checked="checked"'; ?> type="checkbox" />
		</p>				

	<?php
	}
}

?>