<?php 
/* 
Plugin Name: Ticker Widget 
Description: Ticker posts widget.
Version: 1.0 
Author: Stefan Naumovski 
*/    

add_action( 'widgets_init', 'ticker_posts_widget' );

function ticker_posts_widget() {register_widget( 'ticker_widget_t1t' );}

class ticker_widget_t1t extends WP_Widget {
	
	/* Register widget with WordPress. */
	
	function __construct() {
		parent::__construct(
			'ticker_widget_t1t', // Widget ID
			'Ticker', // Name
			array( 'description' => 'Ticker Description.', ) // Args
			);}
	
		/* Front-end display of widget. */
		
		public function widget( $args, $instance ) {
		/* Default widget settings. */
		
			$defaults = array( 'title' => 'Breaking News', 'number' => 10, 'widget_size' => 'body-width-ticker', 'tags' => 0, 'sign_before_link' => '+');
			$instance = wp_parse_args( (array) $instance, $defaults );
			
			$title = $instance['title'];
			$number = $instance['number'];
			$tags = $instance['tags'];
			$widget_size = $instance['widget_size'];
			$sign_before_link = $instance['sign_before_link'];
			
			
		$args['before_widget'] = str_replace('class="home-widget', 'class="home-widget '. $widget_size , $args['before_widget']);	
		echo $args ['before_widget'];?>

<div class="ticker-box">
	<div id="ticker">
		<div class="ticker-heading">
			<?php echo  $title; ?>
		</div>
		<!--ticker-heading-->
		<div id="ticker-list-box">
			<ul class="ticker-list">
				<?php $t1t_posts = new WP_Query(array( 'tag' => $tags, 'posts_per_page' => $number)); while($t1t_posts->have_posts()) : $t1t_posts->the_post();?>
				<li>
					<a href="<?php the_permalink() ?>">
					<span class="ticker-sign">
					<?php echo $sign_before_link; ?>
					</span>
					<?php the_title(); ?>
					</a>
				</li>
				<?php endwhile; ?>
			</ul>
			<div class="ticker-arrows">
				<a class="ticker-left">
				</a>
				<a class="ticker-right">
				</a>
			</div>
		</div>
		<!--ticker-list-box-->
	</div>
	<!--ticker-->
</div>
<!--ticker-box-->
<?php

	/* After widget. */
		
		echo $args['after_widget'];
		}

	/* Widget settings. */
	
	
	function update( $new_instance, $old_instance ) {
		$instance = $old_instance;

		/* Strip tags. */
		
		$instance['title'] = $new_instance['title'];
		$instance['number'] =$new_instance['number'];
		$instance['tags'] = $new_instance['tags'];
		$instance['widget_size'] = $new_instance['widget_size'];
		$instance['sign_before_link'] = $new_instance['sign_before_link'];

		return $instance;
	}


	function form( $instance ) {

		/* Default widget settings. */
		
		$defaults = array( 'title' => 'Breaking News', 'number' => 10, 'tags' => 0, 'widget_size' => 'body-width-ticker', 'sign_before_link' => '+');
		$instance = wp_parse_args( (array) $instance, $defaults ); ?>

<!-- Widget Title-->
<p>
	<label for="<?php echo $this->get_field_id( 'title' ); ?>"> Ticker 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>

<!-- 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-width-ticker', 'fullwidth-ticker');
		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>

<!-- Maximum number of posts -->
<p>
	<label for="<?php echo $this->get_field_id( 'number' ); ?>"> Number of posts: </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>

<!-- sign before -->
<p>
	<label for="<?php echo $this->get_field_id( 'sign_before_link' ); ?>"> Sign before post: </label>
	<input id="<?php echo $this->get_field_id( 'sign_before_link' ); ?>" name="<?php echo $this->get_field_name( 'sign_before_link' ); ?>" value="<?php echo $instance['sign_before_link']; ?>" size="3" />
</p>

<!-- Pick a tag to display -->
<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>
<?php }}?>