<?php
/*
Plugin Name: List Authors By Role
Plugin URI: http://wordpress.org/support/topic/68840
Description: Replacement for list_authors/wp_list_authors allows for restricting authors by role.
Version: R1.0.1
Author: Kaf Oseo
Author URI: http://szub.net

    Copyright (c) 2006 Kaf Oseo (http://szub.net)
    List Authors By Role is released under the GNU General Public
    License (GPL) http://www.gnu.org/licenses/gpl.txt

    This is a WordPress plugin (http://wordpress.org) and is based
    on elements of the WordPress code base.

Usage Details:

list_authors_by_role()
    See: http://codex.wordpress.org/Template_Tags/list_authors

    New Parameter (appended to list_authors() argument list):
        role
        (string) Name of role assigned to author. Delimit 2 or more
        roles with comma ('author,contributor').

szub_list_authors_by_role()
    See: http://codex.wordpress.org/Template_Tags/wp_list_authors

    New Parameter:
        role
        (string) Name of role assigned to author. Delimit 2 or
        more roles with comma ('role=author,contributor').

~Changelog:
R1.0.1 (03-Nov-2006)
Bug fix! Darn that WordPress table prefix...
*/

function szub_list_authors_by_role($args '') {
    
parse_str($args$r);

    if ( !isset(
$r['optioncount']) )
        
$r['optioncount'] = false;
    if ( !isset(
$r['exclude_admin']) )
        
$r['exclude_admin'] = true;
    if ( !isset(
$r['show_fullname']) )
        
$r['show_fullname'] = false;
    if ( !isset(
$r['hide_empty']) )
        
$r['hide_empty'] = true;
    if ( !isset(
$r['feed']) )
        
$r['feed'] = '';
    if ( !isset(
$r['feed_image']) )
        
$r['feed_image'] = '';
    if ( !isset(
$r['role']) )
        
$r['role'] = 'all';

    
list_authors_by_role($r['optioncount'], $r['exclude_admin'], $r['show_fullname'], $r['hide_empty'], $r['feed'], $r['feed_image'], $r['role']);
}


function 
list_authors_by_role($optioncount false$exclude_admin true$show_fullname false$hide_empty true$feed ''$feed_image ''$role 'all') {
    global 
$wpdb$table_prefix;
    if ( 
$role && ('all' != $role) ) {
        
$role explode(','$role);
        
$numrole count($role) - 1;

        foreach(
$role as $rq) {
            
$meta_like .= 'meta_value LIKE \'%' trim($rq) . '%\'';
            if ( 
$role[$numrole] != $rq )
                
$meta_like .= ' OR ';
        }

        
$query "SELECT ID, user_nicename from $wpdb->users LEFT JOIN $wpdb->usermeta ON ID = user_id WHERE meta_key = '{$table_prefix}capabilities' AND $meta_like " . ($exclude_admin "AND user_login <> 'admin' " '') . "ORDER BY display_name";
    } else {
        
$query "SELECT ID, user_nicename from $wpdb->users " . ($exclude_admin "WHERE user_login <> 'admin' " '') . "ORDER BY display_name";
    }

    
$authors $wpdb->get_results($query);

    foreach ( 
$authors as $author ) {
        
$author get_userdata$author->ID );
        
$posts get_usernumposts($author->ID);
        
$name $author->nickname;

        if ( 
$show_fullname && ($author->first_name != '' && $author->last_name != '') )
            
$name "$author->first_name $author->last_name";

        if ( !(
$posts == && $hide_empty) )
            echo 
"<li>";
        if ( 
$posts == ) {
            if ( !
$hide_empty )
                
$link $name;
        } else {
            
$link '<a href="' get_author_link(0$author->ID$author->user_nicename) . '" title="' sprintf(__("Posts by %s"), wp_specialchars($author->display_name)) . '">' $name '</a>';

            if ( (! empty(
$feed_image)) || (! empty($feed)) ) {
                
$link .= ' ';
                if (empty(
$feed_image))
                    
$link .= '(';
                
$link .= '<a href="' get_author_rss_link(0$author->ID$author->user_nicename) . '"';

                if ( !empty(
$feed) ) {
                    
$title ' title="' $feed '"';
                    
$alt ' alt="' $feed '"';
                    
$name $feed;
                    
$link .= $title;
                }

                
$link .= '>';

                if ( !empty(
$feed_image) )
                    
$link .= "<img src=\"$feed_image\" border=\"0\"$alt$title" ' />';
                else
                    
$link .= $name;

                
$link .= '</a>';

                if ( empty(
$feed_image) )
                    
$link .= ')';
            }

            if ( 
$optioncount )
                
$link .= ' ('$posts ')';

        }

        if ( !(
$posts == && $hide_empty) )
            echo 
"$link</li>";
    }
}
?>