<?php
/*	
	
	WIDGET FILTERS:

    widget_title			- widget title
	wlogin-box_args	 		- arguments for get_posts for login-box
	wlogin-box_post_title		- login-box post title
	wlogin-box_post_content	- login-box post content
	
*/

// WIDGET CLASS
class Bizz_Widget_Login_Box extends WP_Widget {

	/**
	 * Set up the widget's unique name, ID, class, description, and other options.
	 * @since 0.6
	 */
	function Bizz_Widget_Login_Box() {
		$widget_ops = array( 'classname' => 'login-box', 'description' => __( 'Login box with phone number.', 'bizzthemes' ) );
		$control_ops = array( 'id_base' => "widgets-reloaded-bizz-login-box" );
		$this->WP_Widget( "widgets-reloaded-bizz-login-box", __( 'Login Box', 'bizzthemes' ), $widget_ops, $control_ops );
	}

	/**
	 * Outputs the widget based on the arguments input through the widget controls.
	 * @since 0.6
	 */
	function widget( $args, $instance ) {
		extract( $args );

		$args = array();

		$args['phone_number'] = $instance['phone_number'];
		$args['phone_number_link'] = $instance['phone_number_link'];
		$args['button_text'] = $instance['button_text'];
		$args['button_link'] = $instance['button_link'];
		
		$widget_id = preg_replace("/[^0-9\.]/", '', $widget_id);
		$widget_id = 'login_box_'.$widget_id;

		echo $before_widget;
				
		if ( isset($args['phone_number_link']) && $args['phone_number_link'] != 'http://' )
			echo '<span class="phone_number"><a href="'.$args['phone_number_link'].'">'.$args['phone_number'].'</a></span>';
		else
			echo '<span class="phone_number">'.$args['phone_number'].'</span>';
			
		echo '<a href="'.$args['button_link'].'" class="btn login_button"><span><span>'.$args['button_text'].'</span></span></a>';
		
		echo $after_widget;
	}

	/**
	 * Updates the widget control options for the particular instance of the widget.
	 * @since 0.6
	 */
	function update( $new_instance, $old_instance ) {
		$instance = $old_instance;
		$instance = $new_instance;
		
		$instance['phone_number'] = strip_tags( $new_instance['phone_number'] );
		$instance['phone_number_link'] = strip_tags( $new_instance['phone_number_link'] );
		$instance['button_text'] = $new_instance['button_text'];
		$instance['button_link'] = $new_instance['button_link'];
		
		return $instance;
	}

	/**
	 * Displays the widget control options in the Widgets admin screen.
	 * @since 0.6
	 */
	function form( $instance ) {

		//Defaults
		$defaults = array(
			'phone_number' => '+1 800 513 8899',
			'phone_number_link' => 'http://',
			'button_text' => 'Clients login',
			'button_link' => 'http://bizzthemes.com'
		);
		$instance = wp_parse_args( (array) $instance, $defaults );
?>

		<p>
			<label for="<?php echo $this->get_field_id( 'phone_number' ); ?>">Phone number</label>
			<input type="text" class="widefat" id="<?php echo $this->get_field_id( 'phone_number' ); ?>" name="<?php echo $this->get_field_name( 'phone_number' ); ?>" value="<?php echo $instance['phone_number']; ?>" />
		</p>
		<p>
			<label for="<?php echo $this->get_field_id( 'phone_number_link' ); ?>">Phone number link</label>
			<input type="text" class="widefat" id="<?php echo $this->get_field_id( 'phone_number_link' ); ?>" name="<?php echo $this->get_field_name( 'phone_number_link' ); ?>" value="<?php echo $instance['phone_number_link']; ?>" />
		</p>
		<p>
			<label for="<?php echo $this->get_field_id( 'button_text' ); ?>">Button text</label>
			<input type="text" class="widefat" id="<?php echo $this->get_field_id( 'button_text' ); ?>" name="<?php echo $this->get_field_name( 'button_text' ); ?>" value="<?php echo $instance['button_text']; ?>" />
		</p>
		<p>
			<label for="<?php echo $this->get_field_id( 'button_link' ); ?>">Button link</label>
			<input type="text" class="widefat" id="<?php echo $this->get_field_id( 'button_link' ); ?>" name="<?php echo $this->get_field_name( 'button_link' ); ?>" value="<?php echo $instance['button_link']; ?>" />
		</p>
		<div style="clear:both;">&nbsp;</div>
	<?php
	}
}

// INITIATE WIDGET
register_widget( 'Bizz_Widget_Login_Box' );