<?php
/*
Plugin Name: Author Widget
*/


class TF_Author_Widget extends WP_Widget {
	public function __construct() {
		parent::__construct(
			'th_author_widget',
			'Site Authors',
			array( 'description' => __( 'Show Authors from this site in a widget', 'pf-widget' ), ) // Args
		);
	}

	public function form( $instance ) {
		if( isset( $instance[ 'title' ] ) ) {
			$title = $instance[ 'title' ];
		} else {
			$title = __( 'Our Writers' );
		}
		if( isset( $instance[ 'max_authors' ] ) ) {
			$max_authors = $instance[ 'max_authors' ];
		} else {
			$max_authors = 10;
		}
		if( isset( $instance[ 'posted_since' ] ) ) {
			$posted_since = $instance[ 'posted_since' ];
		} else {
			$posted_since = "All";
		}
		?>
		<p>
		<label for="<?php echo $this->get_field_name( 'title' ); ?>"><?php _e( 'Title:' ); ?></label> 
		<input class="widefat" id="<?php echo $this->get_field_id( 'title' ); ?>" name="<?php echo $this->get_field_name( 'title' ); ?>" type="text" value="<?php echo esc_attr( $title ); ?>" />
		<label for="<?php echo $this->get_field_name( 'max_authors' ); ?>"><?php _e( 'Max Authors Displayed:' ); ?></label> 
		<input class="widefat" id="<?php echo $this->get_field_id( 'max_authors' ); ?>" name="<?php echo $this->get_field_name( 'max_authors' ); ?>" type="text" value="<?php echo esc_attr( $max_authors ); ?>" />	
		<label for="<?php echo $this->get_field_name( 'posted_since' ); ?>"><?php _e( 'Posted Since (days):' ); ?></label> 
		<input class="widefat" id="<?php echo $this->get_field_id( 'posted_since' ); ?>" name="<?php echo $this->get_field_name( 'posted_since' ); ?>" type="text" value="<?php echo esc_attr( $posted_since ); ?>" />	
		</p>
		<?php
	}

	public function update( $new_instance, $old_instance ) {
		$instance = array();
		$instance[ 'title' ] = ( ! empty( $new_instance[ 'title' ] ) ) ? strip_tags( $new_instance[ 'title' ] ) : '';
		$instance[ 'max_authors' ] = ( ! empty( $new_instance[ 'max_authors' ] ) ) ? strip_tags( $new_instance[ 'max_authors' ] ) : 10;
		$instance[ 'posted_since' ] = ( ! empty( $new_instance[ 'posted_since' ] ) ) ? strip_tags( $new_instance[ 'posted_since' ] ) : "All";
		return $instance;
	}

	public function widget( $args, $instance ) {
		$title = apply_filters( 'widget_title', $instance[ 'title' ] );
		echo $args[ 'before_widget' ];
		if( ! empty( $title ) ) {
			echo $args[ 'before_title' ] . $title . $args[ 'after_title' ];
		}
		echo $this->tf_get_authors( $instance );
		echo $args['after_widget'];
	}

	public function tf_get_authors( $instance ) {
		$instance = wp_parse_args( $instance, array(
			'title' => 'Our Writers',
			'max_authors' => 10,
			'posted_since' => "All"
			) );
		if( is_numeric( $instance[ 'posted_since' ] ) AND $instance[ 'posted_since' ] != 0 ) {
			$since = " AND post_date > '" . date('Y-m-d', strtotime('-' . $instance[ 'posted_since' ] . ' days')) . "'";
		} else {
			$since = "";
		}
		$output = "";
		global $wpdb; 
		foreach ( (array) $wpdb->get_results( "SELECT DISTINCT post_author FROM $wpdb->posts WHERE post_type = 'post' AND " . get_private_posts_cap_sql( 'post' ) . $since . " GROUP BY post_author LIMIT " . $instance[ 'max_authors' ] ) as $row ) { 
			$firstname		= get_the_author_meta( 'first_name', $row->post_author ); 
			$lastname		= get_the_author_meta( 'last_name', $row->post_author ); 
			$fullname		= $firstname . " " . $lastname; 
			$author_url		= home_url( '/author/' ) . get_the_author_meta( 'nicename', $row->post_author ); 
			$author_avatar  = get_avatar( get_the_author_meta( 'email', $row->post_author ), 32 ); 
			$output .= '<p class="author-single"><a href="'. $author_url .'" title="' .  $fullname . '">' . $author_avatar . "&nbsp;" . $fullname . '</a></p>';
		} 
	    return $output;
	}

}
function register_author_widget() {
	register_widget( 'TF_Author_Widget' );
}
add_action( 'widgets_init', 'register_author_widget' );