<?php

/*-----------------------------------------------------------------------------------

	Plugin Name: MyThemeShop 125x125 Ad Widget
	Description: A widget for 125 x 125 ad unit (maximum 6 ads)
	Version: 1.0

-----------------------------------------------------------------------------------*/


// load widget
add_action( 'widgets_init', 'sociallyviral_ads_widgets' );

// Register widget
function sociallyviral_ads_widgets() {
	register_widget( 'sociallyviral_Ad_Widget' );
}

// Widget class
class sociallyviral_ad_widget extends WP_Widget {


/*-----------------------------------------------------------------------------------*/
/*	Widget Setup
/*-----------------------------------------------------------------------------------*/
	
function __construct() {

	// Widget settings
	$widget_ops = array (
		'classname' => 'sociallyviral_ad_widget',
		'description' => __('A widget for 125 x 125 ad unit (max 6 ads)', 'sociallyviral' )
	);

	// Widget control settings
	$control_ops = array (
		'width' => 300,
		'height' => 350,
		'id_base' => 'sociallyviral_ad_widget'
	);

	// Create the widget
	parent::__construct( 'sociallyviral_ad_widget', __('MTS 125x125 Ads', 'sociallyviral' ), $widget_ops, $control_ops );
	
}

/*-----------------------------------------------------------------------------------*/
/*	Display Widget
/*-----------------------------------------------------------------------------------*/
	
function widget( $args, $instance ) {
	extract( $args );

	// variables from the widget settings
	$title = apply_filters('widget_title', $instance['title'] );
	$ad1 = $instance['ad1'];
	$ad2 = $instance['ad2'];
	$ad3 = $instance['ad3'];
	$ad4 = $instance['ad4'];
	$ad5 = $instance['ad5'];
	$ad6 = $instance['ad6'];
	$link1 = $instance['link1'];
	$link2 = $instance['link2'];
	$link3 = $instance['link3'];
	$link4 = $instance['link4'];
	$link5 = $instance['link5'];
	$link6 = $instance['link6'];
	$randomize = $instance['random'];

	// Before widget (defined by theme functions file)
	echo $before_widget;

	// Display the widget title if one was input
	if ( $title )
		echo $before_title . $title . $after_title;

	// Randomize ads order in a new array
	$ads = array();
		
	// Display a containing div
	echo '<div class="ad-125">';
	
	echo '<ul>';

	// Display Ad 1
	if ( $link1 )
		$ads[] = '<li class="oddad"><a href="' . esc_url( $link1 ) . '"><img src="' . esc_attr( $ad1 ) . '" width="125" height="125" alt="" /></a></li>';
		
	elseif ( $ad1 )
	 	$ads[] = '<li class="oddad"><img src="' . esc_attr( $ad1 ) . '" width="125" height="125" alt="" /></li>';
	
	// Display Ad 2
	if ( $link2 )
		$ads[] = '<li class="evenad"><a href="' . esc_url( $link2 ) . '"><img src="' . esc_attr( $ad2 ) . '" width="125" height="125" alt="" /></a></li>';
		
	elseif ( $ad2 )
	 	$ads[] = '<li class="evenad"><img src="' . esc_attr( $ad2 ) . '" width="125" height="125" alt="" /></li>';
		
	// Display Ad 3
	if ( $link3 )
		$ads[] = '<li class="oddad"><a href="' . esc_url( $link3 ) . '"><img src="' . esc_attr( $ad3 ) . '" width="125" height="125" alt="" /></a></li>';
		
	elseif ( $ad3 )
	 	$ads[] = '<li class="oddad"><img src="' . esc_attr( $ad3 ) . '" width="125" height="125" alt="" /></li>';
		
	// Display Ad 4
	if ( $link4 )
		$ads[] = '<li class="evenad"><a href="' . esc_url( $link4 ) . '"><img src="' . esc_attr( $ad4 ) . '" width="125" height="125" alt="" /></a></li>';
		
	elseif ( $ad4 )
	 	$ads[] = '<li class="evenad"><img src="' . esc_attr( $ad4 ) . '" width="125" height="125" alt="" /></li>';
		
	// Display Ad 5
	if ( $link5 )
		$ads[] = '<li class="oddad"><a href="' . esc_url( $link5 ) . '"><img src="' . esc_attr( $ad5 ) . '" width="125" height="125" alt="" /></a></li>';
		
	elseif ( $ad5 )
	 	$ads[] = '<li class="oddad"><img src="' . esc_attr( $ad5 ) . '" width="125" height="125" alt="" /></li>';
		
	// Display Ad 6
	if ( $link6 )
		$ads[] = '<li class="evenad"><a href="' . esc_url( $link6 ) . '"><img src="' . esc_attr( $ad6 ) . '" width="125" height="125" alt="" /></a></li>';
		
	elseif ( $ad6 )
	 	$ads[] = '<li class="evenad"><img src="' . esc_attr( $ad6 ) . '" width="125" height="125" alt="" /></li>';
	
	// Randomize order if selected
	if ($randomize){
		shuffle($ads);
	}
	
	//Display ads
	foreach($ads as $ad){
		echo $ad;
	}
	
	echo '</ul>';
		
	echo '</div>';

	// After widget (defined by theme functions file)
	echo $after_widget;
}

/*-----------------------------------------------------------------------------------*/
/*	Update Widget
/*-----------------------------------------------------------------------------------*/
	
function update( $new_instance, $old_instance ) {
	$instance = $old_instance;

	// Strip tags to remove HTML (important for text inputs)
	$instance['title'] = strip_tags( $new_instance['title'] );

	// No need to strip tags
	$instance['ad1'] = esc_url_raw($new_instance['ad1']);
	$instance['ad2'] = esc_url_raw($new_instance['ad2']);
	$instance['ad3'] = esc_url_raw($new_instance['ad3']);
	$instance['ad4'] = esc_url_raw($new_instance['ad4']);
	$instance['ad5'] = esc_url_raw($new_instance['ad5']);
	$instance['ad6'] = esc_url_raw($new_instance['ad6']);
	$instance['link1'] = esc_url_raw($new_instance['link1']);
	$instance['link2'] = esc_url_raw($new_instance['link2']);
	$instance['link3'] = esc_url_raw($new_instance['link3']);
	$instance['link4'] = esc_url_raw($new_instance['link4']);
	$instance['link5'] = esc_url_raw($new_instance['link5']);
	$instance['link6'] = esc_url_raw($new_instance['link6']);
	$instance['random'] = (bool) $new_instance['random'];
	
	return $instance;
}

/*-----------------------------------------------------------------------------------*/
/*	Widget Settings (Displays the widget settings controls on the widget panel)
/*-----------------------------------------------------------------------------------*/
	
function form( $instance ) {

	// Set up some default widget settings
	$defaults = array(
		'title' => 'Our Sponsors',
		'ad1' => get_template_directory_uri()."/images/125x125.png",
		'link1' => 'http://mythemeshop.com/',
		'ad2' => get_template_directory_uri()."/images/125x125.png",
		'link2' => 'http://mythemeshop.com/',
		'ad3' => '',
		'link3' => '',
		'ad4' => '',
		'link4' => '',
		'ad5' => '',
		'link5' => '',
		'ad6' => '',
		'link6' => '',
		'random' => false
	);
		
	$instance = wp_parse_args( (array) $instance, $defaults ); ?>

	<!-- Widget Title: Text Input -->
	<p>
		<label for="<?php echo $this->get_field_id( 'title' ); ?>"><?php _e('Title:', 'sociallyviral' ) ?></label>
		<input type="text" class="widefat" id="<?php echo $this->get_field_id( 'title' ); ?>" name="<?php echo $this->get_field_name( 'title' ); ?>" value="<?php echo esc_attr( $instance['title'] ); ?>" />
	</p>

	<!-- Ad 1 image url: Text Input -->
	<p>
		<label for="<?php echo $this->get_field_id( 'ad1' ); ?>"><?php _e('Ad 1 image url:', 'sociallyviral' ) ?></label>
		<input type="text" class="widefat" id="<?php echo $this->get_field_id( 'ad1' ); ?>" name="<?php echo $this->get_field_name( 'ad1' ); ?>" value="<?php echo esc_attr( $instance['ad1'] ); ?>" />
	</p>
	
	<!-- Ad 1 link url: Text Input -->
	<p>
		<label for="<?php echo $this->get_field_id( 'link1' ); ?>"><?php _e('Ad 1 link url:', 'sociallyviral' ) ?></label>
		<input type="text" class="widefat" id="<?php echo $this->get_field_id( 'link1' ); ?>" name="<?php echo $this->get_field_name( 'link1' ); ?>" value="<?php echo esc_attr( $instance['link1'] ); ?>" />
	</p>
	
	<!-- Ad 2 image url: Text Input -->
	<p>
		<label for="<?php echo $this->get_field_id( 'ad2' ); ?>"><?php _e('Ad 2 image url:', 'sociallyviral' ) ?></label>
		<input type="text" class="widefat" id="<?php echo $this->get_field_id( 'ad2' ); ?>" name="<?php echo $this->get_field_name( 'ad2' ); ?>" value="<?php echo esc_attr( $instance['ad2'] ); ?>" />
	</p>
	
	<!-- Ad 2 link url: Text Input -->
	<p>
		<label for="<?php echo $this->get_field_id( 'link2' ); ?>"><?php _e('Ad 2 link url:', 'sociallyviral' ) ?></label>
		<input type="text" class="widefat" id="<?php echo $this->get_field_id( 'link2' ); ?>" name="<?php echo $this->get_field_name( 'link2' ); ?>" value="<?php echo esc_attr( $instance['link2'] ); ?>" />
	</p>
	
	<!-- Ad 3 image url: Text Input -->
	<p>
		<label for="<?php echo $this->get_field_id( 'ad3' ); ?>"><?php _e('Ad 3 image url:', 'sociallyviral' ) ?></label>
		<input type="text" class="widefat" id="<?php echo $this->get_field_id( 'ad3' ); ?>" name="<?php echo $this->get_field_name( 'ad3' ); ?>" value="<?php echo esc_attr( $instance['ad3'] ); ?>" />
	</p>
	
	<!-- Ad 3 link url: Text Input -->
	<p>
		<label for="<?php echo $this->get_field_id( 'link3' ); ?>"><?php _e('Ad 3 link url:', 'sociallyviral' ) ?></label>
		<input type="text" class="widefat" id="<?php echo $this->get_field_id( 'link3' ); ?>" name="<?php echo $this->get_field_name( 'link3' ); ?>" value="<?php echo esc_attr( $instance['link3'] ); ?>" />
	</p>
	
	<!-- Ad 4 image url: Text Input -->
	<p>
		<label for="<?php echo $this->get_field_id( 'ad4' ); ?>"><?php _e('Ad 4 image url:', 'sociallyviral' ) ?></label>
		<input type="text" class="widefat" id="<?php echo $this->get_field_id( 'ad4' ); ?>" name="<?php echo $this->get_field_name( 'ad4' ); ?>" value="<?php echo esc_attr( $instance['ad4'] ); ?>" />
	</p>
	
	<!-- Ad 4 link url: Text Input -->
	<p>
		<label for="<?php echo $this->get_field_id( 'link4' ); ?>"><?php _e('Ad 4 link url:', 'sociallyviral' ) ?></label>
		<input type="text" class="widefat" id="<?php echo $this->get_field_id( 'link4' ); ?>" name="<?php echo $this->get_field_name( 'link4' ); ?>" value="<?php echo esc_attr( $instance['link4'] ); ?>" />
	</p>
	
	<!-- Ad 5 image url: Text Input -->
	<p>
		<label for="<?php echo $this->get_field_id( 'ad5' ); ?>"><?php _e('Ad 5 image url:', 'sociallyviral' ) ?></label>
		<input type="text" class="widefat" id="<?php echo $this->get_field_id( 'ad5' ); ?>" name="<?php echo $this->get_field_name( 'ad5' ); ?>" value="<?php echo esc_attr( $instance['ad5'] ); ?>" />
	</p>
	
	<!-- Ad 5 link url: Text Input -->
	<p>
		<label for="<?php echo $this->get_field_id( 'link5' ); ?>"><?php _e('Ad 5 link url:', 'sociallyviral' ) ?></label>
		<input type="text" class="widefat" id="<?php echo $this->get_field_id( 'link5' ); ?>" name="<?php echo $this->get_field_name( 'link5' ); ?>" value="<?php echo esc_attr( $instance['link5'] ); ?>" />
	</p>
	
	<!-- Ad 6 image url: Text Input -->
	<p>
		<label for="<?php echo $this->get_field_id( 'ad6' ); ?>"><?php _e('Ad 6 image url:', 'sociallyviral' ) ?></label>
		<input type="text" class="widefat" id="<?php echo $this->get_field_id( 'ad6' ); ?>" name="<?php echo $this->get_field_name( 'ad6' ); ?>" value="<?php echo esc_attr( $instance['ad6'] ); ?>" />
	</p>
	
	<!-- Ad 6 link url: Text Input -->
	<p>
		<label for="<?php echo $this->get_field_id( 'link6' ); ?>"><?php _e('Ad 6 link url:', 'sociallyviral' ) ?></label>
		<input type="text" class="widefat" id="<?php echo $this->get_field_id( 'link6' ); ?>" name="<?php echo $this->get_field_name( 'link6' ); ?>" value="<?php echo esc_attr( $instance['link6'] ); ?>" />
	</p>
	
	<!-- Randomize? -->
	<p>
		<label for="<?php echo $this->get_field_id( 'random' ); ?>"><?php _e('Randomize ads order?', 'sociallyviral' ) ?></label>
		<?php if ($instance['random']){ ?>
			<input type="checkbox" id="<?php echo $this->get_field_id( 'random' ); ?>" name="<?php echo $this->get_field_name( 'random' ); ?>" checked="checked" />
		<?php } else { ?>
			<input type="checkbox" id="<?php echo $this->get_field_id( 'random' ); ?>" name="<?php echo $this->get_field_name( 'random' ); ?>"  />
		<?php } ?>
	</p>
	
	<?php
	}
}
?>