<?php   

/* 
Plugin Name: Video
Description: Video widget 
Version: 1.0 
Author: Stefan Naumovski 
*/    

add_action( 'widgets_init', 'embed_widget' );

function embed_widget() {register_widget( 'embed_vid' );}

class embed_vid extends WP_Widget {
	
	/* Register widget with WordPress. */
	
	function __construct() {
		parent::__construct(
			'embed_vid_widget_tn', // Widget ID
			'Video', // Name
			array( 'description' => 'Video embed - embed your video into a widget.', ) // Args
			);}
		
		/* Front-end display of widget. */
		
		public function widget( $args, $instance ) {
			$title = apply_filters( 'widget_title', $instance['title'] );
			$link_to_vid = $instance['link_to_vid'];
			
			echo $args['before_widget'];
			if ( ! empty( $title ) )
				echo $args['before_title'] . $title . $args['after_title'];
			?>
			
			<div class="embed-widget">
				<?php
        		$video_embed = wp_oembed_get($link_to_vid, array('width'=>305) );    	
				echo '<div class="embed-wrapper">' .$video_embed. '</div>';
				?>
			</div><!--embed-widget-->
		
			<?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['link_to_vid'] = strip_tags( $new_instance['link_to_vid'] );
			
			return $instance;
		}
				
		function form( $instance ) {
			
			/* Default widget settings. */
			
			$defaults = array( 'title' =>'Video');
			$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>
			
			<!-- Link to Video-->
			<p>
				<label for="<?php echo $this->get_field_id( 'link_to_vid' ); ?>">Link your video</label>
				<input id="<?php echo $this->get_field_id( 'link_to_vid' ); ?>" name="<?php echo $this->get_field_name( 'link_to_vid' ); ?>" value="<?php if(isset($instance['link_to_vid'])){ echo $instance['link_to_vid'];} ?>" style="width:90%;" />
			</p>
<?php }} ?>