Created
December 28, 2011 18:10
-
-
Save benbalter/1528950 to your computer and use it in GitHub Desktop.
Top Help Users Widget
This file contains 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 | |
/* | |
Plugin Name: Top Help Users | |
Description: Widget to display top users of help.hackshackers.com by reputation | |
Author: Benjamin J. Balter | |
Version: 1.0 | |
Author URI: http://ben.balter.com | |
*/ | |
class Top_Help_Users_Widget extends WP_Widget { | |
//default settings | |
private $defaults = array( | |
'number' => 5, | |
'title' => 'Top Users', | |
); | |
/** | |
* Init widget and register | |
*/ | |
function __construct() { | |
parent::WP_Widget( 'Top_Help_Users_Widget', $name = 'Top Help Users' ); | |
add_action( 'widgets_init', create_function( '', 'return register_widget("Top_Help_Users_Widget");' ) ); | |
} | |
/** | |
* Callback to display widget contents | |
*/ | |
function widget( $args, $instance ) { | |
extract( $args ); | |
$args = array( | |
'number' => $instance['number'], | |
'meta_key' => '_qa_rep', | |
'meta_value' => 0, | |
'meta_compare' => '>', | |
'orderby' => 'wp_usermeta.meta_value', | |
'order' => 'DESC', | |
); | |
add_filter( 'pre_user_query', array( &$this, 'user_query_filter' ) ); | |
$users = get_users( $args ); | |
remove_filter( 'pre_user_query', array( &$this, 'user_query_filter' ) ); | |
echo $before_widget; | |
echo $before_title . apply_filters( 'widget_title', $instance['title'] ) . $after_title; | |
echo "<ol>"; | |
foreach ( $users as $user ) { | |
$rep = get_user_meta( $user->ID, '_qa_rep', true ); | |
echo '<li><a href="'. site_url( 'questions/user/' . $user->user_nicename . '/' ) .'" title="View ' . $user->display_name . '\'s profile">' . $user->display_name . "</a> ($rep)</li>"; | |
} | |
echo "</ol>"; | |
echo $after_widget; | |
} | |
/** | |
* Callback to display widget options form | |
*/ | |
function form( $instance ) { | |
foreach ( $this->defaults as $key => $value ) | |
if ( !isset( $instance[ $key ] ) ) | |
$instance[ $key ] = $value; | |
?> | |
<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 $instance['title']; ?>" /> | |
</p> | |
<p> | |
<label for="<?php echo $this->get_field_id('number'); ?>">Number of Users</label><br /> | |
<input class="small-text" id="<?php echo $this->get_field_id('number'); ?>" name="<?php echo $this->get_field_name('number'); ?>" type="text" value="<?php echo $instance['number']; ?>" /> | |
</p> | |
<?php | |
} | |
/** | |
* Sanitizes options and saves | |
*/ | |
function update( $new_instance, $old_instance ) { | |
$instance = $old_instance; | |
$instance['title'] = strip_tags( $new_instance['title'] ); | |
$instance['number'] = (int) $new_instance['number']; | |
return $instance; | |
} | |
/** | |
* Sorts get_users by meta_key | |
*/ | |
function user_query_filter( $query ) { | |
//note: "+0" forces the value into an int so mysql sorts properly | |
$query->query_orderby = 'ORDER BY (wp_usermeta.meta_value + 0) DESC'; | |
return $query; | |
} | |
} | |
new Top_Help_Users_Widget; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment