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

function super_slider_widget() {register_widget( 'super_slider_t1t' );}

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

	function __construct() {
		parent::__construct(
				'super_slider_widget_t1t', 	//Widget ID
				'Super Slider', // Name
				array( 'description' => 'Super Slider', ) // Args
		);}
		
		/* Front-end display of widget. */
		
		public function widget( $args, $instance ) {
			
			$defaults = array( 'title' => 'Super Slider', 'number' => 7, 'widget_size' => 'fullwidth-super-slider', 'tags' => 0, 'author' => 'on', 'date' => 'on');
			$instance = wp_parse_args( (array) $instance, $defaults );

			$number = $instance['number'];
			$tags = $instance['tags'];
			$widget_size = $instance['widget_size'];
			$author = $instance['author'];
			$date = $instance['date'];
		
			$args['before_widget'] = str_replace('class="home-widget', 'class="home-widget '. $widget_size , $args['before_widget']);				
			echo $args['before_widget'];
			?>

<div class="super-slider <?php echo get_option('t1t_slider_picker');?> loading">
	<ul class="slides">
			<?php  $count=0; $t1t_posts = new WP_Query(array( 'tag' => $tags, 'posts_per_page' =>  $number , )); while($t1t_posts->have_posts()) : $t1t_posts->the_post(); $count++; if ($count == 5){$count = 1;}?>
			<?php if ($count == 1 || $count == 2 || $count == 4){?>
			<li>
			<div class="super-slider-part">
				<?php } ?>
				<div class="super-slider-post">
					<?php if (  (function_exists('has_post_thumbnail')) && (has_post_thumbnail())  ) { ?>
					<a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>">
					<?php if ($count == 2 || $count == 3){the_post_thumbnail('super-slider-short');}elseif($count == 1 || $count == 4){the_post_thumbnail('super-slider-high');} ?>
					</a>
					<?php } ?>
					<div class="super-slider-text-box">
						<div class="super-slider-category">
							<?php $category = get_the_category(); if($category[0]){echo '<a href="'.get_category_link($category[0]->term_id ).'">'.$category[0]->cat_name.'</a>';} ?>
						</div>
						<!--super-slider-category-->
						<div class="super-slider-title">
							<h2>
								<a href="<?php the_permalink() ?>">
								<?php the_title(); ?>
								</a>
							</h2>
						</div>
						<!--super-slide-title-->
						<?php if($author || $date ) { ?>
						<div class="author-date">
							<?php if($author) { ?>
							<div class="author">
								<?php the_author_posts_link(); ?>
							</div>
							<!--author-->
							<?php } ?>
							<?php if($date) { ?>
							<div class="date">
								<?php echo get_the_date(); ?>
							</div>
							<!--date-->
							<?php } ?>
						</div>
						<!--author-date-->
						<?php } ?>
					</div>
					<!--super-slider-text-box-->				
				</div>
				<!--super-slider-post-->				
				<?php if ($count == 1 || $count == 3 || $count == 4){?>
			</div>
			<!--super-slider-part-->
			</li>
			<?php }?>
			
			
			<?php endwhile;	?>
	</ul>
</div>
<!--super-slider-->

<?php	
	/* After widget. */	
				
		echo $args['after_widget'];
	}
	
	/* Widget settings. */
	
	function update( $new_instance, $old_instance ) {
		$instance = $old_instance;	
		
		/* Strip tags. */
					
		$instance['number'] = $new_instance['number'];
		$instance['tags'] = $new_instance['tags'];
		$instance['widget_size'] = $new_instance['widget_size'];
		$instance['author'] = $new_instance['author'];
		$instance['date'] = $new_instance['date'];			
		return $instance;
	}		
	
		/* Default widget settings. */
		
	function form( $instance ) {	
		$defaults = array( 'title' => 'Super Slider', 'number' => 7, 'widget_size' => 'fullwidth-super-slider', 'tags' => 0, 'author' => 'on', 'date' => 'on');
		$instance = wp_parse_args( (array) $instance, $defaults ); ?>

<!-- Width -->
<p>
	<label for="<?php echo $this->get_field_id('widget_size'); ?>">Widget width:</label>
	<select name="<?php echo $this->get_field_name('widget_size'); ?>" id="<?php echo $this->get_field_id('widget_size'); ?>" class="widefat" >
		<?php $options = array('body-super-slider', 'fullwidth-super-slider');
		foreach ($options as $option) {?>
		<option value='<?php echo $option; ?>' <?php if ($option == $instance['widget_size']) echo 'selected="selected"'; ?>><?php echo $option; ?></option>
		<?php } ?>
	</select>
</p>

<!-- Number of posts -->
<p>
	<label for="<?php echo $this->get_field_id( 'number' ); ?>"> Number of slides: </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'); ?>"> Tag: </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>
<!-- Date -->
<p>
	<label for="<?php echo $this->get_field_id( 'date' ); ?>">
		Show Date:
	</label>
	<input type="checkbox" id="<?php echo $this->get_field_id( 'date' ); ?>" name="<?php echo $this->get_field_name( 'date' ); ?>" <?php checked( (bool) $instance['date'], true ); ?> />
</p>
<?php }} ?>