| 1 | Index: author-template.php |
|---|
| 2 | =================================================================== |
|---|
| 3 | --- author-template.php (revision 15608) |
|---|
| 4 | +++ author-template.php (working copy) |
|---|
| 5 | @@ -259,6 +259,13 @@ |
|---|
| 6 | * or as a string.</li> |
|---|
| 7 | * <li>html (bool) (true): Whether to list the items in html for or plaintext. |
|---|
| 8 | * </li> |
|---|
| 9 | + * <li>orderby (string) ('name'): The sort order of the list.</li> |
|---|
| 10 | + * <li>order (string) ('ASC'): Sort order for authors (either ascending or |
|---|
| 11 | + * descending).</li> |
|---|
| 12 | + * <li>number (int) (NULL): Sets the number of authors to display. This |
|---|
| 13 | + * causes the SQL LIMIT value to be defined. Default to no LIMIT.</li> |
|---|
| 14 | + * <li>min_count (int) (NULL): Sets a minimum post count threshold. Defaults to |
|---|
| 15 | + * no minimum.</li> |
|---|
| 16 | * </ul> |
|---|
| 17 | * |
|---|
| 18 | * @link http://codex.wordpress.org/Template_Tags/wp_list_authors |
|---|
| 19 | @@ -267,46 +274,129 @@ |
|---|
| 20 | * @return null|string The output, if echo is set to false. |
|---|
| 21 | */ |
|---|
| 22 | function wp_list_authors($args = '') { |
|---|
| 23 | - global $wpdb; |
|---|
| 24 | + global $wpdb, $blog_id; |
|---|
| 25 | |
|---|
| 26 | $defaults = array( |
|---|
| 27 | 'optioncount' => false, 'exclude_admin' => true, |
|---|
| 28 | 'show_fullname' => false, 'hide_empty' => true, |
|---|
| 29 | 'feed' => '', 'feed_image' => '', 'feed_type' => '', 'echo' => true, |
|---|
| 30 | - 'style' => 'list', 'html' => true |
|---|
| 31 | + 'style' => 'list', 'html' => true, 'orderby' => 'name', |
|---|
| 32 | + 'order' => 'ASC', 'number' => NULL, 'min_count' => NULL |
|---|
| 33 | ); |
|---|
| 34 | |
|---|
| 35 | $r = wp_parse_args( $args, $defaults ); |
|---|
| 36 | extract($r, EXTR_SKIP); |
|---|
| 37 | $return = ''; |
|---|
| 38 | |
|---|
| 39 | - /** @todo Move select to get_authors(). */ |
|---|
| 40 | - $users = get_users(); |
|---|
| 41 | - $author_ids = array(); |
|---|
| 42 | - foreach ( (array) $users as $user ) |
|---|
| 43 | - $author_ids[] = $user->user_id; |
|---|
| 44 | - if ( count($author_ids) > 0 ) { |
|---|
| 45 | - $author_ids = implode(',', $author_ids ); |
|---|
| 46 | - $authors = $wpdb->get_results( "SELECT ID, user_nicename from $wpdb->users WHERE ID IN($author_ids) " . ($exclude_admin ? "AND user_login <> 'admin' " : '') . "ORDER BY display_name" ); |
|---|
| 47 | + if ( empty($id) ) |
|---|
| 48 | + $id = (int) $blog_id; |
|---|
| 49 | + $blog_prefix = $wpdb->get_blog_prefix($id); |
|---|
| 50 | + |
|---|
| 51 | + // Order ASC or DESC based on the order and orderby arguments |
|---|
| 52 | + if ( $show_fullname ) { |
|---|
| 53 | + // Full names will be looked up via user meta-data and sorted later |
|---|
| 54 | + // order by NULL avoids the implicit sorting of the GROUP BY |
|---|
| 55 | + $author_sort = 'ORDER BY NULL'; |
|---|
| 56 | + } else { |
|---|
| 57 | + if ( $orderby == 'count' ) { |
|---|
| 58 | + $author_sort = "ORDER BY post_count $order, author_name"; |
|---|
| 59 | + } else { |
|---|
| 60 | + $author_sort = "ORDER BY author_name $order"; |
|---|
| 61 | + } |
|---|
| 62 | + } |
|---|
| 63 | + |
|---|
| 64 | + // Limit the results based on the min_count argument |
|---|
| 65 | + $min_count = absint( $min_count ); |
|---|
| 66 | + if ( $hide_empty ) |
|---|
| 67 | + $min_count = max(1, $min_count); |
|---|
| 68 | + if ( $min_count ) { |
|---|
| 69 | + $author_having = "HAVING post_count >= $min_count"; |
|---|
| 70 | } else { |
|---|
| 71 | - $authors = array(); |
|---|
| 72 | + $author_having = ''; |
|---|
| 73 | } |
|---|
| 74 | |
|---|
| 75 | - $author_count = array(); |
|---|
| 76 | - foreach ( (array) $wpdb->get_results("SELECT DISTINCT post_author, COUNT(ID) AS count FROM $wpdb->posts WHERE post_type = 'post' AND " . get_private_posts_cap_sql( 'post' ) . " GROUP BY post_author") as $row ) |
|---|
| 77 | - $author_count[$row->post_author] = $row->count; |
|---|
| 78 | + // Limit the results based on the number argument |
|---|
| 79 | + $number = intval( $number ); |
|---|
| 80 | + if ( $number > 0 ) { |
|---|
| 81 | + $author_limit = "LIMIT $number"; |
|---|
| 82 | + } else { |
|---|
| 83 | + $author_limit = ''; |
|---|
| 84 | + } |
|---|
| 85 | |
|---|
| 86 | + // Join to exclude authors from other blogs. Only needed if hide_empty is |
|---|
| 87 | + // false or and no min_count is set (the INNER JOIN to $wpdb->posts would |
|---|
| 88 | + // already exclude non-authors in both cases) |
|---|
| 89 | + if ( !$hide_empty && !$min_count ) { |
|---|
| 90 | + $author_cap_join = "JOIN $wpdb->usermeta AS meta_capabilities ON meta_capabilities.user_id = $wpdb->users.ID AND meta_capabilities.meta_key = '{$blog_prefix}capabilities'"; |
|---|
| 91 | + } else { |
|---|
| 92 | + $author_cap_join = ''; |
|---|
| 93 | + } |
|---|
| 94 | + |
|---|
| 95 | + $author_select = "$wpdb->users.ID, $wpdb->users.user_nicename, $wpdb->users.display_name AS author_name"; |
|---|
| 96 | + |
|---|
| 97 | + // join on posts only when necessary |
|---|
| 98 | + if ( $hide_empty || $min_count || $optioncount || $orderby == 'count' ) { |
|---|
| 99 | + $author_select .= ", COUNT($wpdb->posts.ID) as post_count"; |
|---|
| 100 | + $author_posts_join = "JOIN $wpdb->posts ON $wpdb->posts.post_author = $wpdb->users.ID"; |
|---|
| 101 | + $author_where = "($wpdb->posts.post_type = 'post' OR $wpdb->posts.post_type IS NULL) AND ($wpdb->posts.post_status = 'publish' OR $wpdb->posts.post_status IS NULL)"; |
|---|
| 102 | + $author_group_by = "GROUP BY $wpdb->users.ID"; |
|---|
| 103 | + // a left join is needed if we're only interested in fetching numbers of posts |
|---|
| 104 | + if ( !$min_count && !$hide_empty ) |
|---|
| 105 | + $author_posts_join = 'LEFT ' . $author_posts_join; |
|---|
| 106 | + } else { |
|---|
| 107 | + $author_posts_join = ''; |
|---|
| 108 | + $author_where = ''; |
|---|
| 109 | + $author_group_by = ''; |
|---|
| 110 | + } |
|---|
| 111 | + |
|---|
| 112 | + if ( $exclude_admin ) |
|---|
| 113 | + $author_where .= ( $author_where ? ' AND ' : '' ) . "$wpdb->users.user_login <> 'admin'"; |
|---|
| 114 | + |
|---|
| 115 | + $author_where = $author_where ? ( 'WHERE ' . $author_where ) : ''; |
|---|
| 116 | + |
|---|
| 117 | + // Query the list of users |
|---|
| 118 | + $sql = " |
|---|
| 119 | + SELECT $author_select |
|---|
| 120 | + FROM $wpdb->users |
|---|
| 121 | + $author_posts_join |
|---|
| 122 | + $author_name_join |
|---|
| 123 | + $author_cap_join |
|---|
| 124 | + $author_where |
|---|
| 125 | + $author_group_by |
|---|
| 126 | + $author_having |
|---|
| 127 | + $author_sort |
|---|
| 128 | + $author_limit |
|---|
| 129 | + "; |
|---|
| 130 | + |
|---|
| 131 | + $authors = $wpdb->get_results($sql); |
|---|
| 132 | + |
|---|
| 133 | + if ( $show_fullname ) { |
|---|
| 134 | + // Lookup first and last name via cached user meta-data |
|---|
| 135 | + foreach ( (array) $authors as $author ) { |
|---|
| 136 | + $userdata = get_userdata( $author->ID ); |
|---|
| 137 | + if ( !empty( $userdata->first_name ) || !empty( $userdata->last_name ) ) { |
|---|
| 138 | + $author->author_name = $userdata->first_name . ' ' . $userdata->last_name; |
|---|
| 139 | + trim ( $author->author_name ); |
|---|
| 140 | + } |
|---|
| 141 | + } |
|---|
| 142 | + // Sort the objects |
|---|
| 143 | + if ( $orderby == 'name' ) { |
|---|
| 144 | + usort( $authors, '_wp_list_authors_usort_callback_name' ); |
|---|
| 145 | + } else { |
|---|
| 146 | + usort( $authors, '_wp_list_authors_usort_callback_count' ); |
|---|
| 147 | + } |
|---|
| 148 | + if ( $order == 'DESC' ) { |
|---|
| 149 | + $authors = array_reverse( $authors ); |
|---|
| 150 | + } |
|---|
| 151 | + } |
|---|
| 152 | + |
|---|
| 153 | foreach ( (array) $authors as $author ) { |
|---|
| 154 | |
|---|
| 155 | $link = ''; |
|---|
| 156 | |
|---|
| 157 | - $author = get_userdata( $author->ID ); |
|---|
| 158 | - $posts = (isset($author_count[$author->ID])) ? $author_count[$author->ID] : 0; |
|---|
| 159 | - $name = $author->display_name; |
|---|
| 160 | + $posts = $author->post_count; |
|---|
| 161 | + $name = $author->author_name; |
|---|
| 162 | |
|---|
| 163 | - if ( $show_fullname && ($author->first_name != '' && $author->last_name != '') ) |
|---|
| 164 | - $name = "$author->first_name $author->last_name"; |
|---|
| 165 | - |
|---|
| 166 | if( !$html ) { |
|---|
| 167 | if ( $posts == 0 ) { |
|---|
| 168 | if ( ! $hide_empty ) |
|---|
| 169 | @@ -320,11 +410,11 @@ |
|---|
| 170 | |
|---|
| 171 | if ( !($posts == 0 && $hide_empty) && 'list' == $style ) |
|---|
| 172 | $return .= '<li>'; |
|---|
| 173 | - if ( $posts == 0 ) { |
|---|
| 174 | + if ( $optioncount && $posts == 0 ) { |
|---|
| 175 | if ( ! $hide_empty ) |
|---|
| 176 | $link = $name; |
|---|
| 177 | } else { |
|---|
| 178 | - $link = '<a href="' . get_author_posts_url($author->ID, $author->user_nicename) . '" title="' . esc_attr( sprintf(__("Posts by %s"), $author->display_name) ) . '">' . $name . '</a>'; |
|---|
| 179 | + $link = '<a href="' . get_author_posts_url($author->ID, $author->user_nicename) . '" title="' . esc_attr( sprintf(__("Posts by %s"), $author->author_name) ) . '">' . $name . '</a>'; |
|---|
| 180 | |
|---|
| 181 | if ( (! empty($feed_image)) || (! empty($feed)) ) { |
|---|
| 182 | $link .= ' '; |
|---|
| 183 | @@ -368,4 +458,11 @@ |
|---|
| 184 | echo $return; |
|---|
| 185 | } |
|---|
| 186 | |
|---|
| 187 | +function _wp_list_authors_usort_callback_name($a, $b) { |
|---|
| 188 | + return strcasecmp( $a->author_name, $b->author_name ); |
|---|
| 189 | +} |
|---|
| 190 | + |
|---|
| 191 | +function _wp_list_authors_usort_callback_count($a, $b) { |
|---|
| 192 | + return $a->post_count - $b->post_count; |
|---|
| 193 | +} |
|---|
| 194 | ?> |
|---|