<?php
/* 
Plugin Name: Featured category
Description:  Featured cateogry with square images display
Version: 1.0 
Author: Stefan Naumovski 
*/    
add_action( 'widgets_init', 'square_img_widget' );

function square_img_widget() {register_widget( 'square_img_cat' );}

class square_img_cat extends WP_Widget {
	
	/* Register widget with WordPress. */
	
	function __construct() {
		parent::__construct(
			'square_img_category_tn', // Widget ID
			'Featured category square',  //Name
			array( 'description' => 'Featured category square - display a featured category with square images. ', ) // Args
		);}
		
		/* Front-end display of widget. */
		
	public function widget( $args, $instance ) {
		$title = apply_filters( 'widget_title', $instance['title'] );
		$categories = $instance['categories'];
		$number = $instance['number'];
		$review = $instance['review'];
		$one_column = (isset($instance['one_column']));
		
		echo $args['before_widget'];
		if ( ! empty( $title ) )
			echo $args['before_title'] . $title . $args['after_title'];	
			?>
<div id="square-wrapper">
	<ul class="square-category" <?php if($one_column) { ?>id="square-category"<?php } ?>>
    
    
    
		<?php if($review) {$tn_posts = new WP_Query(array(  'tax_query' => array(array('taxonomy' => 'post_format', 'field' => 'slug', 'terms' => array('post-format-aside'))),'posts_per_page' => $number, 'ignore_sticky_posts' => 1 ));}
		else
		{$tn_posts = new WP_Query(array( 'cat' => $categories, 'posts_per_page' => $number, 'ignore_sticky_posts' => 1 ));}			
		while
			 ( $tn_posts->have_posts()) : $tn_posts->the_post(); ?>
		<li>
			<div class="square-image">
				<?php if (  (function_exists('has_post_thumbnail')) && (has_post_thumbnail())  ) { ?>
				<a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>">
				<?php the_post_thumbnail('carousel-thumb'); if ( 'video' == get_post_format() ): echo '<span class="play-icon"></span>'; endif; ?>
				</a>
				<?php } ?>
               <?php if($review) { ?> <div class="square-review-score">
					<?php echo get_post_meta( get_the_ID(), 'tn_review_total', true ); ?>
                </div><!--review-score-->
               <?php } ?>
           	</div><!---square-image--> 
            <div class="square-text">
                <div class="square-title"><a href="<?php the_permalink() ?>">
                <?php $tit = the_title('','',FALSE); echo substr($tit, 0, 40); if (strlen($tit) > 40) echo " ..."; ?>
                </a>
                </div><!--square-title--> 
                <span class="date"><?php echo get_the_date(); ?></span>
            </div><!--square-text-->
		</li>
		<?php endwhile; ?>
	</ul>
</div>
<?php

		/* After widget. */

		echo $args['after_widget'];
	}
	
		/* Widget settings. */
		
	function update( $new_instance, $old_instance ) {
		$instance = $old_instance;
		
		/* Strip tags. */
		
		$instance['title'] = strip_tags( $new_instance['title'] );
		$instance['number'] = strip_tags( $new_instance['number'] );
		$instance['categories'] = $new_instance['categories'];
		$instance['review'] = $new_instance['review'];
		$instance['one_column'] = $new_instance['one_column'];
		
		return $instance;
	}

	function form( $instance ) {
		
		/* Default widget settings. */

		$defaults = array( 'title' => 'Image featured category', 'number' => 2, 'review' => 'on', 'one_column'=> 'on');
		$instance = wp_parse_args( (array) $instance, $defaults ); ?>
		
		<!-- Widget Title-->
		<p>
		  <label for="<?php echo $this->get_field_id( 'title' ); ?>">Title:</label>
		  <input id="<?php echo $this->get_field_id( 'title' ); ?>" name="<?php echo $this->get_field_name( 'title' ); ?>" value="<?php echo $instance['title']; ?>" style="width:90%;" />
		</p>
		
		<!-- Number of posts -->
		<p>
			<label for="<?php echo $this->get_field_id('number'); ?>">Show number of posts:</label>
			<select name="<?php echo $this->get_field_name('number'); ?>" id="<?php echo $this->get_field_id('number'); ?>" class="widefat" >
				<?php $options = array('2','4','6','8');
				foreach ($options as $option) {?>
				<option value='<?php echo $option; ?>' <?php if ($option == $instance['number']) echo 'selected="selected"'; ?>><?php echo $option; ?></option>
						<?php } ?>
			</select>
		</p>
		
		<!-- Category -->
		<p>
			<label for="<?php echo $this->get_field_id('categories'); ?>">(Optional)Select Category:</label>
			<select id="<?php echo $this->get_field_id('categories'); ?>" name="<?php echo $this->get_field_name('categories'); ?>" style="width:100%;">
				<option value='all' <?php if ('all' == (isset($instance['categories']))) echo 'selected="selected"'; ?>>All Categories</option>
				<?php $categories = get_categories('hide_empty=0&depth=1&type=post'); ?>
				<?php foreach($categories as $category) { ?>
				<option value='<?php echo $category->term_id; ?>' <?php if(isset($instance['categories'])){ if ($category->term_id == $instance['categories']) echo 'selected="selected"';}?>><?php echo $category->cat_name; ?></option>
				<?php } ?>
			</select>
		</p>
		
		<!-- review -->
		<p>
		  <label for="<?php echo $this->get_field_id( 'review' ); ?>">Show reviews only:</label>
		  <input type="checkbox" id="<?php echo $this->get_field_id( 'review' ); ?>" name="<?php echo $this->get_field_name( 'review' ); ?>" <?php checked( (bool) $instance['review'], true ); ?> />
		</p>
        
		<!-- one column -->
		<p>
		  <label for="<?php echo $this->get_field_id( 'one_column' ); ?>">Make widget one column:</label>
		  <input type="checkbox" id="<?php echo $this->get_field_id( 'one_column' ); ?>" name="<?php echo $this->get_field_name( 'one_column' ); ?>" <?php checked( (bool) $instance['one_column'], true ); ?> />
		</p>
        
<?php }} ?>