| 1 | <?php |
|---|
| 2 | /* |
|---|
| 3 | Plugin Name: Author Widget |
|---|
| 4 | */ |
|---|
| 5 | |
|---|
| 6 | |
|---|
| 7 | class TF_Author_Widget extends WP_Widget { |
|---|
| 8 | public function __construct() { |
|---|
| 9 | parent::__construct( |
|---|
| 10 | 'th_author_widget', |
|---|
| 11 | 'Site Authors', |
|---|
| 12 | array( 'description' => __( 'Show Authors from this site in a widget', 'pf-widget' ), ) // Args |
|---|
| 13 | ); |
|---|
| 14 | } |
|---|
| 15 | |
|---|
| 16 | public function form( $instance ) { |
|---|
| 17 | if( isset( $instance[ 'title' ] ) ) { |
|---|
| 18 | $title = $instance[ 'title' ]; |
|---|
| 19 | } else { |
|---|
| 20 | $title = __( 'Our Writers' ); |
|---|
| 21 | } |
|---|
| 22 | if( isset( $instance[ 'max_authors' ] ) ) { |
|---|
| 23 | $max_authors = $instance[ 'max_authors' ]; |
|---|
| 24 | } else { |
|---|
| 25 | $max_authors = 10; |
|---|
| 26 | } |
|---|
| 27 | if( isset( $instance[ 'posted_since' ] ) ) { |
|---|
| 28 | $posted_since = $instance[ 'posted_since' ]; |
|---|
| 29 | } else { |
|---|
| 30 | $posted_since = "All"; |
|---|
| 31 | } |
|---|
| 32 | ?> |
|---|
| 33 | <p> |
|---|
| 34 | <label for="<?php echo $this->get_field_name( 'title' ); ?>"><?php _e( 'Title:' ); ?></label> |
|---|
| 35 | <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 ); ?>" /> |
|---|
| 36 | <label for="<?php echo $this->get_field_name( 'max_authors' ); ?>"><?php _e( 'Max Authors Displayed:' ); ?></label> |
|---|
| 37 | <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 ); ?>" /> |
|---|
| 38 | <label for="<?php echo $this->get_field_name( 'posted_since' ); ?>"><?php _e( 'Posted Since (days):' ); ?></label> |
|---|
| 39 | <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 ); ?>" /> |
|---|
| 40 | </p> |
|---|
| 41 | <?php |
|---|
| 42 | } |
|---|
| 43 | |
|---|
| 44 | public function update( $new_instance, $old_instance ) { |
|---|
| 45 | $instance = array(); |
|---|
| 46 | $instance[ 'title' ] = ( ! empty( $new_instance[ 'title' ] ) ) ? strip_tags( $new_instance[ 'title' ] ) : ''; |
|---|
| 47 | $instance[ 'max_authors' ] = ( ! empty( $new_instance[ 'max_authors' ] ) ) ? strip_tags( $new_instance[ 'max_authors' ] ) : 10; |
|---|
| 48 | $instance[ 'posted_since' ] = ( ! empty( $new_instance[ 'posted_since' ] ) ) ? strip_tags( $new_instance[ 'posted_since' ] ) : "All"; |
|---|
| 49 | return $instance; |
|---|
| 50 | } |
|---|
| 51 | |
|---|
| 52 | public function widget( $args, $instance ) { |
|---|
| 53 | $title = apply_filters( 'widget_title', $instance[ 'title' ] ); |
|---|
| 54 | echo $args[ 'before_widget' ]; |
|---|
| 55 | if( ! empty( $title ) ) { |
|---|
| 56 | echo $args[ 'before_title' ] . $title . $args[ 'after_title' ]; |
|---|
| 57 | } |
|---|
| 58 | echo $this->tf_get_authors( $instance ); |
|---|
| 59 | echo $args['after_widget']; |
|---|
| 60 | } |
|---|
| 61 | |
|---|
| 62 | public function tf_get_authors( $instance ) { |
|---|
| 63 | $instance = wp_parse_args( $instance, array( |
|---|
| 64 | 'title' => 'Our Writers', |
|---|
| 65 | 'max_authors' => 10, |
|---|
| 66 | 'posted_since' => "All" |
|---|
| 67 | ) ); |
|---|
| 68 | if( is_numeric( $instance[ 'posted_since' ] ) AND $instance[ 'posted_since' ] != 0 ) { |
|---|
| 69 | $since = " AND post_date > '" . date('Y-m-d', strtotime('-' . $instance[ 'posted_since' ] . ' days')) . "'"; |
|---|
| 70 | } else { |
|---|
| 71 | $since = ""; |
|---|
| 72 | } |
|---|
| 73 | $output = ""; |
|---|
| 74 | global $wpdb; |
|---|
| 75 | 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 ) { |
|---|
| 76 | $firstname = get_the_author_meta( 'first_name', $row->post_author ); |
|---|
| 77 | $lastname = get_the_author_meta( 'last_name', $row->post_author ); |
|---|
| 78 | $fullname = $firstname . " " . $lastname; |
|---|
| 79 | $author_url = home_url( '/author/' ) . get_the_author_meta( 'nicename', $row->post_author ); |
|---|
| 80 | $author_avatar = get_avatar( get_the_author_meta( 'email', $row->post_author ), 32 ); |
|---|
| 81 | $output .= '<p class="author-single"><a href="'. $author_url .'" title="' . $fullname . '">' . $author_avatar . " " . $fullname . '</a></p>'; |
|---|
| 82 | } |
|---|
| 83 | return $output; |
|---|
| 84 | } |
|---|
| 85 | |
|---|
| 86 | } |
|---|
| 87 | function register_author_widget() { |
|---|
| 88 | register_widget( 'TF_Author_Widget' ); |
|---|
| 89 | } |
|---|
| 90 | add_action( 'widgets_init', 'register_author_widget' ); |
|---|