Make WordPress Core

Ticket #4420: author-template.php

File author-template.php, 5.2 KB (added by J2000_ca, 18 years ago)
Line 
1Index: author-template.php
2===================================================================
3--- author-template.php (revision 6360)
4+++ author-template.php (working copy)
5@@ -364,7 +364,8 @@
6        global $wpdb;
7 
8        $defaults = array(
9-               'optioncount' => false, 'exclude_admin' => true,
10+               'order' => 'ASC', 'limit' => '0', 'list' => true,
11+               'link' => true, 'optioncount' => false, 'exclude_admin' => true,
12                'show_fullname' => false, 'hide_empty' => true,
13                'feed' => '', 'feed_image' => '', 'echo' => true
14        );
15@@ -372,69 +373,104 @@
16        $r = wp_parse_args( $args, $defaults );
17        extract($r, EXTR_SKIP);
18 
19+       $exclude_admin = $exclude_admin ? "WHERE user_login <> 'admin'" : '';
20+
21+       $sql_limit = $limit > 0 ? "LIMIT 0,$limit" : '';
22+       
23        $return = '';
24 
25-       // TODO:  Move select to get_authors().
26-       $authors = $wpdb->get_results("SELECT ID, user_nicename from $wpdb->users " . ($exclude_admin ? "WHERE user_login <> 'admin' " : '') . "ORDER BY display_name");
27+       $authors = (array) $wpdb->get_results("
28+               SELECT $wpdb->users.ID, user_nicename, display_name, COUNT($wpdb->posts.ID) AS posts
29+               FROM $wpdb->users " . (!$hide_empty ? 'LEFT' : '') . " JOIN $wpdb->posts
30+                       ON  $wpdb->posts.post_author = $wpdb->users.ID AND post_type = 'post' AND " . get_private_posts_cap_sql( 'post' ) . "
31+               $exclude_admin
32+               GROUP BY post_author
33+               ORDER BY display_name $order $sql_limit");
34 
35-       $author_count = array();
36-       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) {
37-               $author_count[$row->post_author] = $row->count;
38+       if ( empty($authors) ) return false;
39+
40+       if ( $show_fullname ) {
41+               $user_ids = $wpdb->get_col(null);
42+
43+               $meta = (array) $wpdb->get_results("
44+                       SELECT user_id, meta_key, meta_value
45+                       FROM $wpdb->usermeta
46+                       WHERE user_id IN (" . join(',', $user_ids) . ") AND (meta_key = 'first_name' OR meta_key = 'last_name')
47+                       ORDER BY meta_key, meta_value, user_id");
48+
49+               $user_meta = array();
50+               foreach ($meta as $v)
51+                       $user_meta[$v->user_id][$v->meta_key] = $v->meta_value;
52        }
53-
54-       foreach ( (array) $authors as $author ) {
55-               $author = get_userdata( $author->ID );
56-               $posts = (isset($author_count[$author->ID])) ? $author_count[$author->ID] : 0;
57+       
58+       $return .= $before;
59+       $i = 0;
60+       if($list)
61+               $return .= '<ul>';
62+       foreach ($authors as $key => $author) {
63                $name = $author->display_name;
64+               if ( $show_fullname && !empty($user_meta[$author->ID]['first_name']) && !empty($user_meta[$author->ID]['last_name']) )
65+                       $name = trim($user_meta[$author->ID]['first_name'] . ' ' . $user_meta[$author->ID]['last_name']);
66 
67-               if ( $show_fullname && ($author->first_name != '' && $author->last_name != '') )
68-                       $name = "$author->first_name $author->last_name";
69-
70-               if ( !($posts == 0 && $hide_empty) )
71+               if ( $hide_empty && 0 == $author->posts ) {
72+                       if($list)
73+                               $return .= '<li>' . $name . '</li>';
74+                       else
75+                               $return .= $name;
76+                       $return .= $between;
77+                       continue;
78+               }
79+               if($list)
80                        $return .= '<li>';
81-               if ( $posts == 0 ) {
82-                       if ( !$hide_empty )
83-                               $link = $name;
84-               } else {
85-                       $link = '<a href="' . get_author_posts_url($author->ID, $author->user_nicename) . '" title="' . sprintf(__("Posts by %s"), attribute_escape($author->display_name)) . '">' . $name . '</a>';
86+               if($link)
87+                       $text = '<a href="' . get_author_posts_url($author->ID, $author->user_nicename) . '" title="' . sprintf(__("Posts by %s"), attribute_escape($author->display_name)) . '">' . $name . '</a>';
88+               else
89+                       $text = $name;
90+                       
91+               if ( ((! empty($feed_image)) || (! empty($feed))) && $link) {
92+                       $text .= ' ';
93+                       if (empty($feed_image))
94+                               $text .= '(';
95+                       $text .= '<a href="' . get_author_rss_link(0, $author->ID, $author->user_nicename) . '"';
96 
97-                       if ( (! empty($feed_image)) || (! empty($feed)) ) {
98-                               $link .= ' ';
99-                               if (empty($feed_image))
100-                                       $link .= '(';
101-                               $link .= '<a href="' . get_author_rss_link(0, $author->ID, $author->user_nicename) . '"';
102+                       if ( !empty($feed) ) {
103+                               $title = ' title="' . $feed . '"';
104+                               $alt = ' alt="' . $feed . '"';
105+                               $name = $feed;
106+                               $text .= $title;
107+                       }
108 
109-                               if ( !empty($feed) ) {
110-                                       $title = ' title="' . $feed . '"';
111-                                       $alt = ' alt="' . $feed . '"';
112-                                       $name = $feed;
113-                                       $link .= $title;
114-                               }
115+                       $text .= '>';
116 
117-                               $link .= '>';
118+                       if ( !empty($feed_image) )
119+                               $text .= "<img src=\"$feed_image\" border=\"0\"$alt$title" . ' />';
120+                       else
121+                               $text .= $name;
122 
123-                               if ( !empty($feed_image) )
124-                                       $link .= "<img src=\"$feed_image\" border=\"0\"$alt$title" . ' />';
125-                               else
126-                                       $link .= $name;
127+                       $text .= '</a>';
128 
129-                               $link .= '</a>';
130+                       if ( empty($feed_image) )
131+                               $text .= ')';
132+               }
133 
134-                               if ( empty($feed_image) )
135-                                       $link .= ')';
136-                       }
137+               if ( $optioncount )
138+                       $text .= ' ('. $author->posts . ')';
139 
140-                       if ( $optioncount )
141-                               $link .= ' ('. $posts . ')';
142-
143-               }
144-
145-               if ( !($posts == 0 && $hide_empty) )
146-                       $return .= $link . '</li>';
147+               $return .= $text;
148+               if($list)
149+                       $return .= '</li>';
150+               $i++;
151+               if($i < $limit)
152+                       $return .= $between."\n";
153+               
154        }
155+       if($list)
156+               $return .= '</ul>';     
157+       $return .= $after;
158+       
159        if ( !$echo )
160                return $return;
161        echo $return;
162 }
163 
164-?>
165\ No newline at end of file
166+?>