<?php
/* 
Plugin Name: Content Slider 
Description: Content Slider by selecting tags.
Version: 1.0 
Author: Stefan Naumovski 
*/    
add_action( 'widgets_init', 'Slider_widget' );

function Slider_widget() {register_widget( 'Slider' );}

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

	function __construct() {
		parent::__construct(
				'Slider_widget_tn', 	//Widget ID
				'Slider main', // Name
				array( 'description' => 'Main image slider - display your posts on the main slider by selecting tags.', ) // Args
		);}
		
		/* Front-end display of widget. */
		
		public function widget( $args, $instance ) {
			$title = apply_filters( 'widget_title', $instance['title'] );
			$number = $instance['number'];
			$tags = $instance['tags'];
			$author = $instance['author'];			
			echo $args['before_widget'];
			if ( ! empty( $title ) )
				echo $args['before_title'] . $title . $args['after_title'];	
			?>
            <div id="solo-slider-container">
                <div class="flexslider">
                    <ul class="slides">
                        <?php $tn_posts = new WP_Query(array( 'tag' => $tags, 'posts_per_page' => $number  )); while($tn_posts->have_posts()) : $tn_posts->the_post();?>
                        <li>
                            <?php if (  (function_exists('has_post_thumbnail')) && (has_post_thumbnail())  ) { ?>
                            <a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>">
                            <?php the_post_thumbnail('solo-slider-img');?>
                            </a>
                            <?php } ?>
                            <div class="slider-text-box">
                                <span class="slide-date"><?php echo get_the_date(); ?></span>		
                                <div class="slide-title"><h2><a href="<?php the_permalink() ?>">
                                    <?php the_title(); ?>
                                    </a></h2>
                                </div><!--slider-text-box-->
                                <div class="slide-info">
                                    <span class="slide-excerpt">
                                        <?php  $tn_subtitle = get_post_meta(get_the_ID(), 'tn_sub_title', true); if(empty($tn_subtitle)) { echo excerpt(22); }  else { echo $tn_subtitle;}  ?>
                                    </span>
                                                    
                                </div><!--slider-info-->
                            </div><!--slider-text-box-->
                        </li>
                        <?php endwhile; ?>
                    </ul>
                </div><!--flexslider-->
            </div><!--slider-container-->

<?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['tags'] = $new_instance['tags'];
		$instance['author'] = $new_instance['author'];		
		return $instance;
	}		
	
		/* Default widget settings. */
		
	function form( $instance ) {	
		$defaults = array( 'title' => 'Main Image Slider', 'number' => 3, 'author' => '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' ); ?>">Number of slides to show:</label>
		  <input id="<?php echo $this->get_field_id( 'number' ); ?>" name="<?php echo $this->get_field_name( 'number' ); ?>" value="<?php echo $instance['number']; ?>" size="3" />
		</p>
		
		<!--Tags-->
		<p>
		  <label for="<?php echo $this->get_field_id('tags'); ?>">IMPORTANT!!!! if you dont select a tag nothing will show on the slider. Select tag to display in slider:</label>
		  <select id="<?php echo $this->get_field_id('tags'); ?>" name="<?php echo $this->get_field_name('tags'); ?>" style="width:100%;">
			<option value='all' <?php if ('all' == (isset($instance['tags']))) echo 'selected="selected"'; ?>>No Tag Selected</option>
			<?php $tags = get_tags('hide_empty=0'); ?>
			<?php foreach($tags as $tag) { ?>
			<option value='<?php echo $tag->slug; ?>' <?php if(isset($instance['tags'])){ if ($tag->slug == $instance['tags']) echo 'selected="selected"';} ?>><?php echo $tag->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 }} ?>