]> scripts.mit.edu Git - autoinstalls/wordpress.git/blob - wp-includes/author-template.php
Wordpress 3.1-scripts
[autoinstalls/wordpress.git] / wp-includes / author-template.php
1 <?php
2 /**
3  * Author Template functions for use in themes.
4  *
5  * These functions must be used within the WordPress Loop.
6  *
7  * @link http://codex.wordpress.org/Author_Templates
8  *
9  * @package WordPress
10  * @subpackage Template
11  */
12
13 /**
14  * Retrieve the author of the current post.
15  *
16  * @since 1.5
17  * @uses $authordata The current author's DB object.
18  * @uses apply_filters() Calls 'the_author' hook on the author display name.
19  *
20  * @param string $deprecated Deprecated.
21  * @return string The author's display name.
22  */
23 function get_the_author($deprecated = '') {
24         global $authordata;
25
26         if ( !empty( $deprecated ) )
27                 _deprecated_argument( __FUNCTION__, '2.1' );
28
29         return apply_filters('the_author', is_object($authordata) ? $authordata->display_name : null);
30 }
31
32 /**
33  * Display the name of the author of the current post.
34  *
35  * The behavior of this function is based off of old functionality predating
36  * get_the_author(). This function is not deprecated, but is designed to echo
37  * the value from get_the_author() and as an result of any old theme that might
38  * still use the old behavior will also pass the value from get_the_author().
39  *
40  * The normal, expected behavior of this function is to echo the author and not
41  * return it. However, backwards compatiability has to be maintained.
42  *
43  * @since 0.71
44  * @see get_the_author()
45  * @link http://codex.wordpress.org/Template_Tags/the_author
46  *
47  * @param string $deprecated Deprecated.
48  * @param string $deprecated_echo Deprecated. Use get_the_author(). Echo the string or return it.
49  * @return string The author's display name, from get_the_author().
50  */
51 function the_author( $deprecated = '', $deprecated_echo = true ) {
52         if ( !empty( $deprecated ) )
53                 _deprecated_argument( __FUNCTION__, '2.1' );
54         if ( $deprecated_echo !== true )
55                 _deprecated_argument( __FUNCTION__, '1.5', __('Use <code>get_the_author()</code> instead if you do not want the value echoed.') );
56         if ( $deprecated_echo )
57                 echo get_the_author();
58         return get_the_author();
59 }
60
61 /**
62  * Retrieve the author who last edited the current post.
63  *
64  * @since 2.8
65  * @uses $post The current post's DB object.
66  * @uses get_post_meta() Retrieves the ID of the author who last edited the current post.
67  * @uses get_userdata() Retrieves the author's DB object.
68  * @uses apply_filters() Calls 'the_modified_author' hook on the author display name.
69  * @return string The author's display name.
70  */
71 function get_the_modified_author() {
72         global $post;
73         if ( $last_id = get_post_meta($post->ID, '_edit_last', true) ) {
74                 $last_user = get_userdata($last_id);
75                 return apply_filters('the_modified_author', $last_user->display_name);
76         }
77 }
78
79 /**
80  * Display the name of the author who last edited the current post.
81  *
82  * @since 2.8
83  * @see get_the_author()
84  * @return string The author's display name, from get_the_modified_author().
85  */
86 function the_modified_author() {
87         echo get_the_modified_author();
88 }
89
90 /**
91  * Retrieve the requested data of the author of the current post.
92  * @link http://codex.wordpress.org/Template_Tags/the_author_meta
93  * @since 2.8.0
94  * @uses $authordata The current author's DB object (if $user_id not specified).
95  * @param string $field selects the field of the users record.
96  * @param int $user_id Optional. User ID.
97  * @return string The author's field from the current author's DB object.
98  */
99 function get_the_author_meta($field = '', $user_id = false) {
100         if ( ! $user_id )
101                 global $authordata;
102         else
103                 $authordata = get_userdata( $user_id );
104
105         // Keys used as object vars cannot have dashes.
106         $field = str_replace('-', '', $field);
107         $field = strtolower($field);
108         $user_field = "user_$field";
109
110         if ( 'id' == $field )
111                 $value = isset($authordata->ID) ? (int)$authordata->ID : 0;
112         elseif ( isset($authordata->$user_field) )
113                 $value = $authordata->$user_field;
114         else
115                 $value = isset($authordata->$field) ? $authordata->$field : '';
116
117         return apply_filters('get_the_author_' . $field, $value, $user_id);
118 }
119
120 /**
121  * Retrieve the requested data of the author of the current post.
122  * @link http://codex.wordpress.org/Template_Tags/the_author_meta
123  * @since 2.8.0
124  * @param string $field selects the field of the users record.
125  * @param int $user_id Optional. User ID.
126  * @echo string The author's field from the current author's DB object.
127  */
128 function the_author_meta($field = '', $user_id = false) {
129         echo apply_filters('the_author_' . $field, get_the_author_meta($field, $user_id), $user_id);
130 }
131
132 /**
133  * Retrieve either author's link or author's name.
134  *
135  * If the author has a home page set, return an HTML link, otherwise just return the
136  * author's name.
137  *
138  * @uses get_the_author_meta()
139  * @uses get_the_author()
140  */
141 function get_the_author_link() {
142         if ( get_the_author_meta('url') ) {
143                 return '<a href="' . get_the_author_meta('url') . '" title="' . esc_attr( sprintf(__("Visit %s&#8217;s website"), get_the_author()) ) . '" rel="external">' . get_the_author() . '</a>';
144         } else {
145                 return get_the_author();
146         }
147 }
148
149 /**
150  * Display either author's link or author's name.
151  *
152  * If the author has a home page set, echo an HTML link, otherwise just echo the
153  * author's name.
154  *
155  * @link http://codex.wordpress.org/Template_Tags/the_author_link
156  * @since 2.1
157  * @uses get_the_author_link()
158  */
159 function the_author_link() {
160         echo get_the_author_link();
161 }
162
163 /**
164  * Retrieve the number of posts by the author of the current post.
165  *
166  * @since 1.5
167  * @uses $post The current post in the Loop's DB object.
168  * @uses count_user_posts()
169  * @return int The number of posts by the author.
170  */
171 function get_the_author_posts() {
172         global $post;
173         return count_user_posts($post->post_author);
174 }
175
176 /**
177  * Display the number of posts by the author of the current post.
178  *
179  * @link http://codex.wordpress.org/Template_Tags/the_author_posts
180  * @since 0.71
181  * @uses get_the_author_posts() Echoes returned value from function.
182  */
183 function the_author_posts() {
184         echo get_the_author_posts();
185 }
186
187 /**
188  * Display an HTML link to the author page of the author of the current post.
189  *
190  * Does just echo get_author_posts_url() function, like the others do. The
191  * reason for this, is that another function is used to help in printing the
192  * link to the author's posts.
193  *
194  * @link http://codex.wordpress.org/Template_Tags/the_author_posts_link
195  * @since 1.2.0
196  * @uses $authordata The current author's DB object.
197  * @uses get_author_posts_url()
198  * @uses get_the_author()
199  * @param string $deprecated Deprecated.
200  */
201 function the_author_posts_link($deprecated = '') {
202         if ( !empty( $deprecated ) )
203                 _deprecated_argument( __FUNCTION__, '2.1' );
204
205         global $authordata;
206         $link = sprintf(
207                 '<a href="%1$s" title="%2$s">%3$s</a>',
208                 get_author_posts_url( $authordata->ID, $authordata->user_nicename ),
209                 esc_attr( sprintf( __( 'Posts by %s' ), get_the_author() ) ),
210                 get_the_author()
211         );
212         echo apply_filters( 'the_author_posts_link', $link );
213 }
214
215 /**
216  * Retrieve the URL to the author page for the user with the ID provided.
217  *
218  * @since 2.1.0
219  * @uses $wp_rewrite WP_Rewrite
220  * @return string The URL to the author's page.
221  */
222 function get_author_posts_url($author_id, $author_nicename = '') {
223         global $wp_rewrite;
224         $auth_ID = (int) $author_id;
225         $link = $wp_rewrite->get_author_permastruct();
226
227         if ( empty($link) ) {
228                 $file = home_url( '/' );
229                 $link = $file . '?author=' . $auth_ID;
230         } else {
231                 if ( '' == $author_nicename ) {
232                         $user = get_userdata($author_id);
233                         if ( !empty($user->user_nicename) )
234                                 $author_nicename = $user->user_nicename;
235                 }
236                 $link = str_replace('%author%', $author_nicename, $link);
237                 $link = home_url( user_trailingslashit( $link ) );
238         }
239
240         $link = apply_filters('author_link', $link, $author_id, $author_nicename);
241
242         return $link;
243 }
244
245 /**
246  * List all the authors of the blog, with several options available.
247  *
248  * <ul>
249  * <li>optioncount (boolean) (false): Show the count in parenthesis next to the
250  * author's name.</li>
251  * <li>exclude_admin (boolean) (true): Exclude the 'admin' user that is
252  * installed bydefault.</li>
253  * <li>show_fullname (boolean) (false): Show their full names.</li>
254  * <li>hide_empty (boolean) (true): Don't show authors without any posts.</li>
255  * <li>feed (string) (''): If isn't empty, show links to author's feeds.</li>
256  * <li>feed_image (string) (''): If isn't empty, use this image to link to
257  * feeds.</li>
258  * <li>echo (boolean) (true): Set to false to return the output, instead of
259  * echoing.</li>
260  * <li>style (string) ('list'): Whether to display list of authors in list form
261  * or as a string.</li>
262  * <li>html (bool) (true): Whether to list the items in html form or plaintext.
263  * </li>
264  * </ul>
265  *
266  * @link http://codex.wordpress.org/Template_Tags/wp_list_authors
267  * @since 1.2.0
268  * @param array $args The argument array.
269  * @return null|string The output, if echo is set to false.
270  */
271 function wp_list_authors($args = '') {
272         global $wpdb;
273
274         $defaults = array(
275                 'orderby' => 'name', 'order' => 'ASC', 'number' => '',
276                 'optioncount' => false, 'exclude_admin' => true,
277                 'show_fullname' => false, 'hide_empty' => true,
278                 'feed' => '', 'feed_image' => '', 'feed_type' => '', 'echo' => true,
279                 'style' => 'list', 'html' => true
280         );
281
282         $args = wp_parse_args( $args, $defaults );
283         extract( $args, EXTR_SKIP );
284
285         $return = '';
286
287         $query_args = wp_array_slice_assoc( $args, array( 'orderby', 'order', 'number' ) );
288         $query_args['fields'] = 'ids';
289         $authors = get_users( $query_args );
290
291         $author_count = array();
292         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 )
293                 $author_count[$row->post_author] = $row->count;
294
295         foreach ( $authors as $author_id ) {
296                 $author = get_userdata( $author_id );
297
298                 if ( $exclude_admin && 'admin' == $author->display_name )
299                         continue;
300
301                 $posts = isset( $author_count[$author->ID] ) ? $author_count[$author->ID] : 0;
302
303                 if ( !$posts && $hide_empty )
304                         continue;
305
306                 $link = '';
307
308                 if ( $show_fullname && $author->first_name && $author->last_name )
309                         $name = "$author->first_name $author->last_name";
310                 else
311                         $name = $author->display_name;
312
313                 if ( !$html ) {
314                         $return .= $name . ', ';
315
316                         continue; // No need to go further to process HTML.
317                 }
318
319                 if ( 'list' == $style ) {
320                         $return .= '<li>';
321                 }
322
323                 $link = '<a href="' . get_author_posts_url( $author->ID, $author->user_nicename ) . '" title="' . esc_attr( sprintf(__("Posts by %s"), $author->display_name) ) . '">' . $name . '</a>';
324
325                 if ( !empty( $feed_image ) || !empty( $feed ) ) {
326                         $link .= ' ';
327                         if ( empty( $feed_image ) ) {
328                                 $link .= '(';
329                         }
330
331                         $link .= '<a href="' . get_author_feed_link( $author->ID ) . '"';
332
333                         $alt = $title = '';
334                         if ( !empty( $feed ) ) {
335                                 $title = ' title="' . esc_attr( $feed ) . '"';
336                                 $alt = ' alt="' . esc_attr( $feed ) . '"';
337                                 $name = $feed;
338                                 $link .= $title;
339                         }
340
341                         $link .= '>';
342
343                         if ( !empty( $feed_image ) )
344                                 $link .= '<img src="' . esc_url( $feed_image ) . '" style="border: none;"' . $alt . $title . ' />';
345                         else
346                                 $link .= $name;
347
348                         $link .= '</a>';
349
350                         if ( empty( $feed_image ) )
351                                 $link .= ')';
352                 }
353
354                 if ( $optioncount )
355                         $link .= ' ('. $posts . ')';
356
357                 $return .= $link;
358                 $return .= ( 'list' == $style ) ? '</li>' : ', ';
359         }
360
361         $return = rtrim($return, ', ');
362
363         if ( !$echo )
364                 return $return;
365
366         echo $return;
367 }
368
369 ?>