<?php

/* Add our function to the widgets_init hook. */
add_action( 'widgets_init', 'AboutInit' );

/* Function that registers our widget. */
function AboutInit() {
	register_widget( 'About_Widget' );
}

class About_Widget extends WP_Widget {

	function About_Widget() {
		/* Widget settings. */
		$widget_ops = array( 'classname' => 'about', 'description' => 'Display your gravatar and a short bio blurb.' );

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

		/* Create the widget. */
		$this->WP_Widget( 'about', 'About (by @ThemeJam)', $widget_ops, $control_ops );
	}
	
	function widget( $args, $instance ) {
		extract( $args );

		/* User-selected settings. */
		$title = apply_filters('widget_title', $instance['title'] );
		$imageurl = $instance['imageurl'];
		$biotext = $instance['biotext'];
		$morelinkurl = isset($instance['morelinkurl']) ? $instance['morelinkurl'] : '';

		/* Before widget (defined by theme). */
		echo $before_widget;

		/* Title of widget (before and after defined by theme). */
		if ( $title )
			echo $before_title . $title . $after_title;
		
		echo '<p>';
		
		/* Display image */
		if ( $imageurl )
			echo '<img src="' . $imageurl . '" class="alignleft" alt="'. $title . '" />';

		/* Display text */
		if ( $biotext )
			echo $biotext;
		
		echo '</p>';
		
		/* Display Learn More link */
		if ( $morelinkurl )
			echo '<p class="action-link"><a href="' . $morelinkurl . '">Learn More</a></p>';

		/* After widget (defined by theme). */
		echo $after_widget;
	}
	
	function update( $new_instance, $old_instance ) {
		$instance = $old_instance;

		/* Strip tags (if needed) and update the widget settings. */
		$instance['title'] = strip_tags( $new_instance['title'] );
		$instance['imageurl'] = strip_tags( $new_instance['imageurl'] );
		$instance['biotext'] = $new_instance['biotext'];
		$instance['morelinkurl'] = $new_instance['morelinkurl'];

		return $instance;
	}
	
	function form( $instance ) {

		/* Set up some default widget settings. */
		$defaults = array( 
			'title' => 'Example',
			'imageurl' => '',
			'biotext' => '',
			'show_sex' => true 
			);
		$instance = wp_parse_args( (array) $instance, $defaults ); ?>
		
		<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:100%;" />
		</p>

		<p>
			<label for="<?php echo $this->get_field_id( 'imageurl' ); ?>">Image Path:</label>
			<input id="<?php echo $this->get_field_id( 'imageurl' ); ?>" name="<?php echo $this->get_field_name( 'imageurl' ); ?>" value="<?php echo $instance['imageurl']; ?>" style="width:100%;" />
		</p>
		
		<p>
			<label for="<?php echo $this->get_field_id( 'biotext' ); ?>">About Text:</label>
			<textarea id="<?php echo $this->get_field_id( 'biotext' ); ?>" name="<?php echo $this->get_field_name( 'biotext' ); ?>"  style="width:100%;"><?php echo $instance['biotext']; ?></textarea>
		</p>
		
		<p>
			<label for="<?php echo $this->get_field_id( 'morelinkurl' ); ?>">More Link URL:</label>
			<input id="<?php echo $this->get_field_id( 'morelinkurl' ); ?>" name="<?php echo $this->get_field_name( 'morelinkurl' ); ?>" value="<?php echo isset($instance['morelinkurl']) ? $instance['morelinkurl'] : ''; ?>" style="width:100%;" />
		</p>
		
		
		<?php
	}
}

?>