<?php   
/* 
Plugin Name: Tv-widget
Description: Tv-widget-display latest news from video.
Version: 1.0 
Author: Stefan Naumovski 
*/    

add_action( 'widgets_init', 'tv_widget_latest' );

function tv_widget_latest() {register_widget( 'tv_widget' );}

class tv_widget extends WP_Widget {
	
	/* Register widget with WordPress. */
	
	function __construct() {
		parent::__construct(
			'tv_widget_tn', // Widget ID
			'TV-Widget', // Name
			array( 'description' =>'Tv-widget - your latest videos.', ) // Args
			);}
		
		/* Front-end display of widget. */
		
		public function widget( $args, $instance ) {
			$title = apply_filters( 'widget_title', $instance['title'] );
			$number = $instance['number'];
			
			echo $args['before_widget'];
			?>
			
<div class="tv-featured">

		<?php if ( ! empty( $title ) )
				echo '<div class="tv-featured-title"><a href="' . get_post_format_link( 'video' ) . '">' . $title . '</a></div>'; ?>
                
	<ul class="tv-featured-posts">
	<?php $tn_posts = new WP_Query(array( 'tax_query' => array(array('taxonomy' => 'post_format', 'field' => 'slug', 'terms' => array( 'post-format-video' ))), 'posts_per_page' => $number, 'ignore_sticky_posts' => 1 )); while ( $tn_posts->have_posts()) : $tn_posts->the_post(); ?>
		<li>
			<div class="tv-featured-image">
				<?php if (  (function_exists('has_post_thumbnail')) && (has_post_thumbnail())  ) { ?>
				<a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>">
				<?php the_post_thumbnail('tv-widget-image'); ?>
				</a>
				<?php } ?>
            </div><!---tv-featured-image--> 
					<div class="tv-featured-text">
                    <span class="category-icon"><?php $category = get_the_category(); if($category[0]){echo '<a href="'.get_category_link($category[0]->term_id ).'">'.$category[0]->cat_name.'</a>';} ?></span>
						<div class="tv-featured-post-title"><a href="<?php the_permalink() ?>">
						<?php $tit = the_title('','',FALSE); echo substr($tit, 0, 60); if (strlen($tit) > 60) echo " ..."; ?>
						</a>
                        </div><!--tv-featured-title--> 
                        <span class="date"><?php echo get_the_date(); ?></span>
					</div><!--tv-featured-text--> 
		</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'] );
		
		return $instance;
	}
	
	
	function form( $instance ) {
		
	/* Default widget settings. */
		
		$defaults = array( 'title' => 'New on Theme TV', 'number' => 2);
		$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 posts 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>
    
<?php }} ?>