<?php
/*
 * Plugin Name: Advertisement Block
 * Plugin URI: http://www.goldfishsoftware.com
 * Description: A widget that allows advertisement banner
 * Version: 1.0
 * Author: Sanjeev Chugh
 * Author URI: http://www.goldfishsoftware.com
 */

/*
 * Add function to widgets_init that'll load our widget.
 */
add_action( 'widgets_init', 'advertisement' );

/*
 * Register widget.
 */
function advertisement() {
	register_widget('Advertisement_Widget' );
}

/*
 * Widget class.
 */
class advertisement_widget extends WP_Widget {

	/* ---------------------------- */
	/* -------- Widget setup -------- */
	/* ---------------------------- */
	
	function Advertisement_Widget() {
	
		/* Widget settings */
		$widget_ops = array( 'classname' => 'tz_ad300_widget', 'description' => __('This widget show advertisement block in sidebar', 'framework') );

		/* Widget control settings 
		$control_ops = array( 'width' => 300, 'height' => 250, 'id_base' => 'tz_ad300_widget' );
*/
		/* Create the widget */
		$this->WP_Widget( 'advertisement', __('Advertisement Block', 'framework'));
	}

	/* ---------------------------- */
	/* ------- Display Widget -------- */
	/* ---------------------------- */
	
	function widget( $args, $instance ) {
		extract( $args );
echo $before_widget;
		/* Our variables from the widget settings. */
		$title = apply_filters('widget_title', $instance['title'] );
		$ad = $instance['ad'];
		$link = $instance['link'];
if($title) echo $before_title.$title.$after_title;

		/* Before widget (defined by themes). */
		
		
		/* Display the widget title if one was input (before and after defined by themes). */
		//echo $before_title.$title.$after_title;
		?>
		<?php		
		if ( $link ) {
				echo '<a href="'.$link.'" target="_blank"><img src="'.$ad.'" alt="Banner"/></a>';
			} elseif ( $ad ) {
				echo '<img src="'.$ad.'" alt="Banner"/>';
			}?>
			
			<?php
		/* After widget (defined by themes). */
		echo $after_widget;
	}

	/* ---------------------------- */
	/* ------- Update Widget -------- */
	/* ---------------------------- */
	
	function update( $new_instance, $old_instance ) {
		$instance = $old_instance;

		/* Strip tags for title and name to remove HTML (important for text inputs). */
		$instance['title'] = strip_tags( $new_instance['title'] );

		/* No need to strip tags */
		$instance['ad'] = $new_instance['ad'];
		$instance['link'] = $new_instance['link'];

		return $instance;
	}
	
	/* ---------------------------- */
	/* ------- Widget Settings ------- */
	/* ---------------------------- */
	
	/**
	 * Displays the widget settings controls on the widget panel.
	 * Make use of the get_field_id() and get_field_name() function
	 * when creating your form elements. This handles the confusing stuff.
	 */
	
	function form( $instance ) {
	
		/* Set up some default widget settings. */
		$defaults = array(
		'title' => '',
		'ad' => get_template_directory_uri()."/images/no-banner.jpg",
		'link' => 'http://www.goldfishsoftware.in',
		);
		$instance = wp_parse_args( (array) $instance, $defaults ); ?>

		<!-- Widget Title: Text Input -->
		<p>
			<label for="<?php echo $this->get_field_id( 'title' ); ?>"><?php _e('Title:', THEME_NAME) ?></label>
			<input class="widefat" id="<?php echo $this->get_field_id( 'title' ); ?>" name="<?php echo $this->get_field_name( 'title' ); ?>" value="<?php echo $instance['title']; ?>" />
		</p>

		<!-- Ad image url: Text Input -->
		<p>
			<label for="<?php echo $this->get_field_id( 'ad' ); ?>"><?php _e('Ad image url:', THEME_NAME) ?></label>
			<input class="widefat" id="<?php echo $this->get_field_id( 'ad' ); ?>" name="<?php echo $this->get_field_name( 'ad' ); ?>" value="<?php echo $instance['ad']; ?>" />
		</p>
		
		<!-- Ad link url: Text Input -->
		<p>
			<label for="<?php echo $this->get_field_id( 'link' ); ?>"><?php _e('Ad link url:', THEME_NAME) ?></label>
			<input class="widefat" id="<?php echo $this->get_field_id( 'link' ); ?>" name="<?php echo $this->get_field_name( 'link' ); ?>" value="<?php echo $instance['link']; ?>" />
		</p>
		
	<?php
	}
}
?>