<?php
/* 
Plugin Name: Image featured category
Description:  One column image featured category
Version: 1.0 
Author: Stefan Naumovski 
*/    
add_action( 'widgets_init', 'img_feat_widget' );

function img_feat_widget() {register_widget( 'img_feat_cat' );}

class img_feat_cat extends WP_Widget {
	
	/* Register widget with WordPress. */
	
	function __construct() {
		parent::__construct(
			'img_featured_category_tn', // Widget ID
			'Image featured category',  //Name
			array( 'description' => 'Image featured category - display a featured category in one column with 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'];
		$vertical = $instance['vertical'];
		
		echo $args['before_widget'];
		if ( ! empty( $title ) )
			echo $args['before_title'] . $title . $args['after_title'];	
			?>

<div class="featured-category">
	<ul class="mosaic" <?php if($vertical) { ?>id="mosaic"<?php } ?>>
		<?php $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="mosaic-posts-image">
				<?php if (  (function_exists('has_post_thumbnail')) && (has_post_thumbnail())  ) { ?>
				<a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>">
				<?php the_post_thumbnail('image-featured'); ?>
				</a>
				<?php } ?>
                <div class="mosaic-title">
                    <h2>
                      <a href="<?php the_permalink() ?>" class="big-featured-posts-title">
						<?php the_title(); ?>
                      </a>
                    </h2>
                    <span class="date"><?php echo get_the_date(); ?></span>
                    <div class="mosaic-posts-content">
                      <?php echo excerpt(20); ?>
                	</div><!--mosaic-posts-content-->	
                </div><!--mosaic-title-->
			</div><!--mosaic-posts-image-->
		</li>
		<?php endwhile; ?>
	</ul>
</div><!--featured-category-->

<?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['vertical'] = $new_instance['vertical'];	
		
		return $instance;
	}

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

		$defaults = array( 'title' => 'Image featured category', 'number' => 2, 'vertical' => '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('1','2', '3', '4', '5', '6');
				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>
			
		<!-- vertical -->
		<p>
		  <label for="<?php echo $this->get_field_id( 'vertical' ); ?>">Make widget one column:</label>
		  <input type="checkbox" id="<?php echo $this->get_field_id( 'vertical' ); ?>" name="<?php echo $this->get_field_name( 'vertical' ); ?>" <?php checked( (bool) $instance['vertical'], true ); ?> />
		</p>
<?php }} ?>