<?php   
/* 
Plugin Name: Featured Category
Description: Featured category widget
Version: 1.0 
Author: Stefan Naumovski 
*/    

add_action( 'widgets_init', 'featured_posts_widget' );

function featured_posts_widget() {register_widget( 'featured_posts' );}

class featured_posts extends WP_Widget {
	
	/* Register widget with WordPress. */

	function __construct() {
		parent::__construct(
			'featured_posts_tn', // Widget ID
			'Featured category',// Name
			array( 'description' => 'Featured category - Latest posts from a selected category.')// Args
		);}
		
		/* Front-end display of widget. */
		
		public function widget( $args, $instance ) {
			$title = apply_filters( 'widget_title', $instance['title'] );
			$category_one = $instance['category_one'];
			$category_two = $instance['category_two'];
			$category_three = $instance['category_three'];
			$author = $instance['author'];
			$more_categories = $instance['more_categories'];
							
			echo $args['before_widget'];
			if (! empty( $title ))
				echo '<h3><div class="widget-title">'. $title . '</div>';?>
              
				  <?php if($more_categories) { ?>           
                  <div class="feat-cat-categories">
                      <ul>
                          <li>
                            <a href="#">
							  <?php echo get_cat_name($category_one); ?> 
                            </a>
                          </li>
                          
                          <li>  
                            <a href="#">    
							  <?php echo get_cat_name($category_two); ?> 
                            </a>
                          </li>
                          
                          <li>   
                            <a href="#">   
							  <?php echo get_cat_name($category_three); ?> 
                            </a>
                          </li>                        
                      </ul>
                  </div><!--feat-cat-categories-->
					<?php } ?>

				<?php echo '</h3>';?>



<?php if($more_categories) { ?>
<div class="cat-slider">
	<ul class="slides">
		<li>
 <?php } ?>
            <ul class="featured-posts-category-big">
                <?php $tn_posts = new WP_Query(array( 'cat' => $category_one, 'posts_per_page' => 1, 'ignore_sticky_posts' => 1 )); while ( $tn_posts->have_posts()) : $tn_posts->the_post(); ?>
                <li>
                    <div class="big-featured-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('feat-cat-thumb'); ?></a>
                        <?php } ?>
                        <div class="big-featured-posts-title-box">
                            <div class="big-featured-posts-title">
                                <h2><a href="<?php the_permalink() ?>" class="big-featured-posts-title"><?php the_title(); ?></a></h2>
                            </div><!--big-featured-posts-title-->
                        </div><!--big-featured-posts-title-box-->
                    </div><!--big-featured-posts-image-->
                        <div class="big-featured-posts-content">
                            <?php echo excerpt(20); ?>
                        </div><!--big-featured-posts-content-->	
                        <div class="post-date">
                            <?php if($author) { ?><span class="bypostauthor"><?php the_author_posts_link(); ?></span><?php } ?>
                            <span class="date"><?php the_date(); ?></span>
                        </div><!--post-date-->				
                </li>
                <?php endwhile; ?>
            </ul>
                        
            
            <ul class="featured-posts-category-small">
                <?php $tn_posts = new WP_Query(array( 'cat' => $category_one, 'posts_per_page' => 3, 'offset' => 1, 'ignore_sticky_posts' => 1 )); while ( $tn_posts->have_posts()) : $tn_posts->the_post(); ?>
                    <li>
                        <div class="small-featured-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('small-thumb'); ?>
                            </a>
                            <?php } ?>
                        </div><!--small-featured-posts-image-->
                        <div class="small-featured-posts-text">
                            <h2><a href="<?php the_permalink() ?>" class="small-featured-posts-title">
                                <?php $tit = the_title('','',FALSE); echo substr($tit, 0, 45); if (strlen($tit) > 45) echo " ..."; ?>
                                </a></h2>
                            <div class="small-featured-posts-content">
                                <?php echo excerpt(10); ?>
                            </div><!--small-featured-posts-content-->	
                            <div class="small-featured-posts-date-posted">
                                <div class="post-date">
                                    <span class="date">Posted <?php echo human_time_diff( get_the_time('U'), current_time('timestamp') ) . ' ago'; ?></span>
                                </div><!--post-date-->
                            </div><!--small-featured-posts-date-posted--> 
                        </div><!--small-featured-posts-text--> 
                    </li>
                <?php endwhile; ?>
            </ul>
		</li>

<?php if($more_categories) { ?>

		<li>
                      
            <ul class="featured-posts-category-big">
                <?php $tn_posts = new WP_Query(array( 'cat' => $category_two, 'posts_per_page' => 1, 'ignore_sticky_posts' => 1 )); while ( $tn_posts->have_posts()) : $tn_posts->the_post(); ?>
                <li>
                    <div class="big-featured-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('feat-cat-thumb'); ?></a>
                        <?php } ?>
                        <div class="big-featured-posts-title-box">
                            <div class="big-featured-posts-title">
                                <h2><a href="<?php the_permalink() ?>" class="big-featured-posts-title"><?php the_title(); ?></a></h2>
                            </div><!--big-featured-posts-title-->
                        </div><!--big-featured-posts-title-box-->
                    </div><!--big-featured-posts-image-->
                        <div class="big-featured-posts-content">
                            <?php echo excerpt(20); ?>
                        </div><!--big-featured-posts-content-->					
                        <div class="post-date">
                            <?php if($author) { ?><span class="bypostauthor"><?php the_author_posts_link(); ?></span><?php } ?>
                            <span class="date"><?php the_date(); ?></span>
                        </div><!--post-date-->
                </li>
                <?php endwhile; ?>
            </ul>
                        
            
            <ul class="featured-posts-category-small">
                <?php $tn_posts = new WP_Query(array( 'cat' => $category_two, 'posts_per_page' => 3, 'offset' => 1, 'ignore_sticky_posts' => 1 )); while ( $tn_posts->have_posts()) : $tn_posts->the_post(); ?>
                    <li>
                        <div class="small-featured-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('small-thumb'); ?>
                            </a>
                            <?php } ?>
                        </div><!--small-featured-posts-image-->
                        <div class="small-featured-posts-text">
                            <h2><a href="<?php the_permalink() ?>" class="small-featured-posts-title">
                                <?php $tit = the_title('','',FALSE); echo substr($tit, 0, 45); if (strlen($tit) > 45) echo " ..."; ?>
                                </a></h2>
                            <div class="small-featured-posts-content">
                                <?php echo excerpt(10); ?>
                            </div><!--small-featured-posts-content-->                                
                            <div class="small-featured-posts-date-posted">
                                <div class="post-date">
                                    <span class="date">Posted <?php echo human_time_diff( get_the_time('U'), current_time('timestamp') ) . ' ago'; ?></span>
                                </div><!--post-date-->
                            </div><!--small-featured-posts-date-posted--> 
                        </div><!--small-featured-posts-text--> 
                    </li>
                <?php endwhile; ?>
            </ul>

		</li>



		<li>

            <ul class="featured-posts-category-big">
                <?php $tn_posts = new WP_Query(array( 'cat' => $category_three, 'posts_per_page' => 1, 'ignore_sticky_posts' => 1 )); while ( $tn_posts->have_posts()) : $tn_posts->the_post(); ?>
                <li>
                    <div class="big-featured-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('feat-cat-thumb'); ?></a>
                        <?php } ?>
                        <div class="big-featured-posts-title-box">
                            <div class="big-featured-posts-title">
                                <h2><a href="<?php the_permalink() ?>" class="big-featured-posts-title"><?php the_title(); ?></a></h2>
                            </div><!--big-featured-posts-title-->
                        </div><!--big-featured-posts-title-box-->
                    </div><!--big-featured-posts-image-->
                    
                    
                        <div class="big-featured-posts-content">
                            <?php echo excerpt(20); ?>
                        </div><!--big-featured-posts-content-->					
                        <div class="post-date">
                            <?php if($author) { ?><span class="bypostauthor"><?php the_author_posts_link(); ?></span><?php } ?>
                            <span class="date"><?php the_date(); ?></span>
                        </div><!--post-date-->
                </li>
                <?php endwhile; ?>
            </ul>
                        
            
            <ul class="featured-posts-category-small">
                <?php $tn_posts = new WP_Query(array( 'cat' => $category_three, 'posts_per_page' => 3, 'offset' => 1, 'ignore_sticky_posts' => 1 )); while ( $tn_posts->have_posts()) : $tn_posts->the_post(); ?>
                    <li>
                        <div class="small-featured-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('small-thumb'); ?>
                            </a>
                            <?php } ?>
                        </div><!--small-featured-posts-image-->
                        <div class="small-featured-posts-text">
                            <h2><a href="<?php the_permalink() ?>" class="small-featured-posts-title">
                                <?php $tit = the_title('','',FALSE); echo substr($tit, 0, 45); if (strlen($tit) > 45) echo " ..."; ?>
                                </a></h2>
                            <div class="small-featured-posts-content">
                                <?php echo excerpt(10); ?>
                            </div><!--small-featured-posts-content-->                                
                            <div class="small-featured-posts-date-posted">
                                <div class="post-date">
                                    <span class="date">Posted <?php echo human_time_diff( get_the_time('U'), current_time('timestamp') ) . ' ago'; ?></span>
                                </div><!--post-date-->
                            </div><!--small-featured-posts-date-posted--> 
                        </div><!--small-featured-posts-text--> 
                    </li>
                <?php endwhile; ?>
            </ul>

		</li>
	</ul>
</div><!--cat-slider-->
<?php } ?>

<?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['category_one'] = $new_instance['category_one'];
		$instance['category_two'] = $new_instance['category_two'];
		$instance['category_three'] = $new_instance['category_three'];
		$instance['author'] = $new_instance['author'];
		$instance['more_categories'] = $new_instance['more_categories'];		
		return $instance;
	}
	
	function form( $instance ) {
		
		/* Default widget settings. */

		$defaults = array( 'title' => 'Featured category', 'author' => 'on', 'more_categories' => '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:100%;" />
		</p>
		
		<!-- Category -->
		<p>
			<label for="<?php echo $this->get_field_id('category_one'); ?>">Select Category:</label>
			<select id="<?php echo $this->get_field_id('category_one'); ?>" name="<?php echo $this->get_field_name('category_one'); ?>" style="width:100%;">
				<option value='all' <?php if ('all' == (isset($instance['category_one']))) 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['category_one'])){ if ($category->term_id == $instance['category_one']) echo 'selected="selected"';}?>><?php echo $category->cat_name; ?></option>
				<?php } ?>
			</select>
		</p>
        
        <!-- More categories -->
		<p>
			<label for="<?php echo $this->get_field_id( 'more_categories' ); ?>">Show more categories:</label>
			<input type="checkbox" id="<?php echo $this->get_field_id( 'more_categories' ); ?>" name="<?php echo $this->get_field_name( 'more_categories' ); ?>" <?php checked( (bool) $instance['more_categories'], true ); ?> />
		</p>        
  
		<!-- Category two -->
		<p>
			<label for="<?php echo $this->get_field_id('category_two'); ?>">(Optional)Select Category:</label>
			<select id="<?php echo $this->get_field_id('category_two'); ?>" name="<?php echo $this->get_field_name('category_two'); ?>" style="width:100%;">
				<option value='all' <?php if ('all' == (isset($instance['category_two']))) 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['category_two'])){ if ($category->term_id == $instance['category_two']) echo 'selected="selected"';}?>><?php echo $category->cat_name; ?></option>
				<?php } ?>
			</select>
		</p>
        
		<!-- Category three -->
		<p>
			<label for="<?php echo $this->get_field_id('category_three'); ?>">(Optional)Select Category:</label>
			<select id="<?php echo $this->get_field_id('category_three'); ?>" name="<?php echo $this->get_field_name('category_three'); ?>" style="width:100%;">
				<option value='all' <?php if ('all' == (isset($instance['category_three']))) 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['category_three'])){ if ($category->term_id == $instance['category_three']) echo 'selected="selected"';}?>><?php echo $category->cat_name; ?></option>
				<?php } ?>
			</select>
		</p>

		<!-- Author -->
		<p>
			<label for="<?php echo $this->get_field_id( 'author' ); ?>">Show Author:</label>
			<input type="checkbox" id="<?php echo $this->get_field_id( 'author' ); ?>" name="<?php echo $this->get_field_name( 'author' ); ?>" <?php checked( (bool) $instance['author'], true ); ?> />
		</p>

<?php }}?>