Instantly share code, notes, and snippets.
Created
October 3, 2010 11:21
-
Star
0
(0)
You must be signed in to star a gist -
Fork
0
(0)
You must be signed in to fork a gist
-
Save sorich87/608511 to your computer and use it in GitHub Desktop.
So's WordPress Authors Widget: displays a list of blog authors ordered by number of posts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Description: So's WordPress Authors Widget: displays a list of blog authors ordered by number of posts | |
* Author: Ulrich SOSSOU | |
* Author URI: http://ulrichsossou.com/ | |
* Version: 1.0 | |
* Licence: GPLv2 | |
* Compatibility: WordPress 3.0+ | |
* | |
*/ | |
/* | |
Copyright (C) 2010 Ulrich SOSSOU | |
This program is free software; you can redistribute it and/or | |
modify it under the terms of the GNU General Public License | |
as published by the Free Software Foundation; either version 2 | |
of the License, or (at your option) any later version. | |
This program is distributed in the hope that it will be useful, | |
but WITHOUT ANY WARRANTY; without even the implied warranty of | |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
GNU General Public License for more details. | |
You should have received a copy of the GNU General Public License | |
along with this program; if not, write to the Free Software | |
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. | |
*/ | |
class SO_Widget_Authors extends WP_Widget { | |
function SO_Widget_Authors() { | |
$widget_ops = array( 'classname' => 'so_widget_authors', 'description' => __( 'The authors ordered by number of posts', 'so' ) ); | |
$this->WP_Widget( 'so_authors', __( 'Authors', 'so' ), $widget_ops); | |
$this->alt_option_name = 'so_widget_authors'; | |
add_action( 'save_post', array(&$this, 'flush_widget_cache' ) ); | |
add_action( 'transition_post_status', array(&$this, 'flush_widget_cache' ) ); | |
} | |
function flush_widget_cache() { | |
wp_cache_delete( 'so_widget_authors', 'widget' ); | |
} | |
function widget( $args, $instance ) { | |
global $comments, $comment, $wpdb; | |
$cache = wp_cache_get( 'so_widget_authors', 'widget' ); | |
if ( ! is_array( $cache ) ) | |
$cache = array(); | |
if ( isset( $cache[$args['widget_id']] ) ) { | |
echo $cache[$args['widget_id']]; | |
return; | |
} | |
extract($args, EXTR_SKIP); | |
$output = ''; | |
$title = apply_filters( 'widget_title', empty($instance['title']) ? __( 'Authors', 'so' ) : $instance['title']); | |
if ( ! $number = (int) $instance['number'] ) | |
$number = 5; | |
else if ( $number < 1 ) | |
$number = 1; | |
require_once( ABSPATH . '/wp-admin/includes/user.php' ); | |
$wp_user_search = new WP_User_Search(); | |
$wp_user_search->query_from .= " LEFT JOIN $wpdb->posts ON ($wpdb->users.ID = $wpdb->posts.post_author)"; | |
$wp_user_search->query_where .= " AND (post_type = 'post' OR post_type IS NULL)"; | |
$wp_user_search->query_orderby = " GROUP BY $wpdb->users.ID ORDER BY COUNT($wpdb->posts.ID) DESC, user_login ASC"; | |
$wp_user_search->query_limit = " LIMIT 0, $number"; | |
$wp_user_search->users_per_page = $number; | |
$users_id = $wp_user_search->get_results(); | |
$output .= $before_widget; | |
if ( $title ) | |
$output .= $before_title . $title . $after_title; | |
$output .= '<ul class="so-authors">'; | |
if ( $users_id ) { | |
foreach ( (array) $users_id as $user_id) { | |
$count = count_user_posts( $user_id ); | |
$count_text = ( $count > 1 ) ? $count . ' posts' : $count . ' post'; | |
if( $count ) { | |
$user_post = get_posts( 'showposts=1&author=' . $user_id ); | |
$date = mysql2date( get_option('date_format'), $user_post[0]->post_date ); | |
$output .= '<li>' . get_avatar( $user_id, 40 ) . '<span>' . get_the_author_meta( 'display_name', $user_id ) . '</span> <span>' . $count_text . '</span> <span>latest post - ' . mysql2date( get_option('date_format'), $user_post[0]->post_date ) . '</span></li>'; | |
} | |
} | |
} | |
$output .= '</ul>'; | |
$output .= $after_widget; | |
echo $output; | |
$cache[$args['widget_id']] = $output; | |
wp_cache_set( 'so_widget_authors', $cache, 'widget' ); | |
} | |
function update( $new_instance, $old_instance ) { | |
$instance = $old_instance; | |
$instance['title'] = strip_tags($new_instance['title']); | |
$instance['number'] = (int) $new_instance['number']; | |
$this->flush_widget_cache(); | |
$alloptions = wp_cache_get( 'alloptions', 'options' ); | |
if ( isset($alloptions['so_widget_authors']) ) | |
delete_option( 'so_widget_authors' ); | |
return $instance; | |
} | |
function form( $instance ) { | |
$title = isset($instance['title']) ? esc_attr($instance['title']) : ''; | |
$number = isset($instance['number']) ? absint($instance['number']) : 5; | |
?> | |
<p><label for="<?php echo $this->get_field_id( '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 $title; ?>" /></p> | |
<p><label for="<?php echo $this->get_field_id( 'number' ); ?>"><?php _e( 'Number of authors to show:' ); ?></label> | |
<input id="<?php echo $this->get_field_id( 'number' ); ?>" name="<?php echo $this->get_field_name( 'number' ); ?>" type="text" value="<?php echo $number; ?>" size="3" /></p> | |
<?php | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment