]> scripts.mit.edu Git - autoinstalls/wordpress.git/blob - wp-includes/author-template.php
WordPress 3.4.1
[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 compatibility 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                 $user_id = isset( $authordata->ID ) ? $authordata->ID : 0;
103         } else {
104                 $authordata = get_userdata( $user_id );
105         }
106
107         if ( in_array( $field, array( 'login', 'pass', 'nicename', 'email', 'url', 'registered', 'activation_key', 'status' ) ) )
108                 $field = 'user_' . $field;
109
110         $value = isset( $authordata->$field ) ? $authordata->$field : '';
111
112         return apply_filters( 'get_the_author_' . $field, $value, $user_id );
113 }
114
115 /**
116  * Retrieve the requested data of the author of the current post.
117  * @link http://codex.wordpress.org/Template_Tags/the_author_meta
118  * @since 2.8.0
119  * @param string $field selects the field of the users record.
120  * @param int $user_id Optional. User ID.
121  * @echo string The author's field from the current author's DB object.
122  */
123 function the_author_meta($field = '', $user_id = false) {
124         echo apply_filters('the_author_' . $field, get_the_author_meta($field, $user_id), $user_id);
125 }
126
127 /**
128  * Retrieve either author's link or author's name.
129  *
130  * If the author has a home page set, return an HTML link, otherwise just return the
131  * author's name.
132  *
133  * @uses get_the_author_meta()
134  * @uses get_the_author()
135  */
136 function get_the_author_link() {
137         if ( get_the_author_meta('url') ) {
138                 return '<a href="' . get_the_author_meta('url') . '" title="' . esc_attr( sprintf(__("Visit %s&#8217;s website"), get_the_author()) ) . '" rel="author external">' . get_the_author() . '</a>';
139         } else {
140                 return get_the_author();
141         }
142 }
143
144 /**
145  * Display either author's link or author's name.
146  *
147  * If the author has a home page set, echo an HTML link, otherwise just echo the
148  * author's name.
149  *
150  * @link http://codex.wordpress.org/Template_Tags/the_author_link
151  * @since 2.1
152  * @uses get_the_author_link()
153  */
154 function the_author_link() {
155         echo get_the_author_link();
156 }
157
158 /**
159  * Retrieve the number of posts by the author of the current post.
160  *
161  * @since 1.5
162  * @uses $post The current post in the Loop's DB object.
163  * @uses count_user_posts()
164  * @return int The number of posts by the author.
165  */
166 function get_the_author_posts() {
167         global $post;
168         return count_user_posts($post->post_author);
169 }
170
171 /**
172  * Display the number of posts by the author of the current post.
173  *
174  * @link http://codex.wordpress.org/Template_Tags/the_author_posts
175  * @since 0.71
176  * @uses get_the_author_posts() Echoes returned value from function.
177  */
178 function the_author_posts() {
179         echo get_the_author_posts();
180 }
181
182 /**
183  * Display an HTML link to the author page of the author of the current post.
184  *
185  * Does just echo get_author_posts_url() function, like the others do. The
186  * reason for this, is that another function is used to help in printing the
187  * link to the author's posts.
188  *
189  * @link http://codex.wordpress.org/Template_Tags/the_author_posts_link
190  * @since 1.2.0
191  * @uses $authordata The current author's DB object.
192  * @uses get_author_posts_url()
193  * @uses get_the_author()
194  * @param string $deprecated Deprecated.
195  */
196 function the_author_posts_link($deprecated = '') {
197         if ( !empty( $deprecated ) )
198                 _deprecated_argument( __FUNCTION__, '2.1' );
199
200         global $authordata;
201         if ( !is_object( $authordata ) )
202                 return false;
203         $link = sprintf(
204                 '<a href="%1$s" title="%2$s" rel="author">%3$s</a>',
205                 get_author_posts_url( $authordata->ID, $authordata->user_nicename ),
206                 esc_attr( sprintf( __( 'Posts by %s' ), get_the_author() ) ),
207                 get_the_author()
208         );
209         echo apply_filters( 'the_author_posts_link', $link );
210 }
211
212 /**
213  * Retrieve the URL to the author page for the user with the ID provided.
214  *
215  * @since 2.1.0
216  * @uses $wp_rewrite WP_Rewrite
217  * @return string The URL to the author's page.
218  */
219 function get_author_posts_url($author_id, $author_nicename = '') {
220         global $wp_rewrite;
221         $auth_ID = (int) $author_id;
222         $link = $wp_rewrite->get_author_permastruct();
223
224         if ( empty($link) ) {
225                 $file = home_url( '/' );
226                 $link = $file . '?author=' . $auth_ID;
227         } else {
228                 if ( '' == $author_nicename ) {
229                         $user = get_userdata($author_id);
230                         if ( !empty($user->user_nicename) )
231                                 $author_nicename = $user->user_nicename;
232                 }
233                 $link = str_replace('%author%', $author_nicename, $link);
234                 $link = home_url( user_trailingslashit( $link ) );
235         }
236
237         $link = apply_filters('author_link', $link, $author_id, $author_nicename);
238
239         return $link;
240 }
241
242 /**
243  * List all the authors of the blog, with several options available.
244  *
245  * <ul>
246  * <li>optioncount (boolean) (false): Show the count in parenthesis next to the
247  * author's name.</li>
248  * <li>exclude_admin (boolean) (true): Exclude the 'admin' user that is
249  * installed bydefault.</li>
250  * <li>show_fullname (boolean) (false): Show their full names.</li>
251  * <li>hide_empty (boolean) (true): Don't show authors without any posts.</li>
252  * <li>feed (string) (''): If isn't empty, show links to author's feeds.</li>
253  * <li>feed_image (string) (''): If isn't empty, use this image to link to
254  * feeds.</li>
255  * <li>echo (boolean) (true): Set to false to return the output, instead of
256  * echoing.</li>
257  * <li>style (string) ('list'): Whether to display list of authors in list form
258  * or as a string.</li>
259  * <li>html (bool) (true): Whether to list the items in html form or plaintext.
260  * </li>
261  * </ul>
262  *
263  * @link http://codex.wordpress.org/Template_Tags/wp_list_authors
264  * @since 1.2.0
265  * @param array $args The argument array.
266  * @return null|string The output, if echo is set to false.
267  */
268 function wp_list_authors($args = '') {
269         global $wpdb;
270
271         $defaults = array(
272                 'orderby' => 'name', 'order' => 'ASC', 'number' => '',
273                 'optioncount' => false, 'exclude_admin' => true,
274                 'show_fullname' => false, 'hide_empty' => true,
275                 'feed' => '', 'feed_image' => '', 'feed_type' => '', 'echo' => true,
276                 'style' => 'list', 'html' => true
277         );
278
279         $args = wp_parse_args( $args, $defaults );
280         extract( $args, EXTR_SKIP );
281
282         $return = '';
283
284         $query_args = wp_array_slice_assoc( $args, array( 'orderby', 'order', 'number' ) );
285         $query_args['fields'] = 'ids';
286         $authors = get_users( $query_args );
287
288         $author_count = array();
289         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 )
290                 $author_count[$row->post_author] = $row->count;
291
292         foreach ( $authors as $author_id ) {
293                 $author = get_userdata( $author_id );
294
295                 if ( $exclude_admin && 'admin' == $author->display_name )
296                         continue;
297
298                 $posts = isset( $author_count[$author->ID] ) ? $author_count[$author->ID] : 0;
299
300                 if ( !$posts && $hide_empty )
301                         continue;
302
303                 $link = '';
304
305                 if ( $show_fullname && $author->first_name && $author->last_name )
306                         $name = "$author->first_name $author->last_name";
307                 else
308                         $name = $author->display_name;
309
310                 if ( !$html ) {
311                         $return .= $name . ', ';
312
313                         continue; // No need to go further to process HTML.
314                 }
315
316                 if ( 'list' == $style ) {
317                         $return .= '<li>';
318                 }
319
320                 $link = '<a href="' . get_author_posts_url( $author->ID, $author->user_nicename ) . '" title="' . esc_attr( sprintf(__("Posts by %s"), $author->display_name) ) . '">' . $name . '</a>';
321
322                 if ( !empty( $feed_image ) || !empty( $feed ) ) {
323                         $link .= ' ';
324                         if ( empty( $feed_image ) ) {
325                                 $link .= '(';
326                         }
327
328                         $link .= '<a href="' . get_author_feed_link( $author->ID ) . '"';
329
330                         $alt = $title = '';
331                         if ( !empty( $feed ) ) {
332                                 $title = ' title="' . esc_attr( $feed ) . '"';
333                                 $alt = ' alt="' . esc_attr( $feed ) . '"';
334                                 $name = $feed;
335                                 $link .= $title;
336                         }
337
338                         $link .= '>';
339
340                         if ( !empty( $feed_image ) )
341                                 $link .= '<img src="' . esc_url( $feed_image ) . '" style="border: none;"' . $alt . $title . ' />';
342                         else
343                                 $link .= $name;
344
345                         $link .= '</a>';
346
347                         if ( empty( $feed_image ) )
348                                 $link .= ')';
349                 }
350
351                 if ( $optioncount )
352                         $link .= ' ('. $posts . ')';
353
354                 $return .= $link;
355                 $return .= ( 'list' == $style ) ? '</li>' : ', ';
356         }
357
358         $return = rtrim($return, ', ');
359
360         if ( !$echo )
361                 return $return;
362
363         echo $return;
364 }
365
366 /**
367  * Does this site have more than one author
368  *
369  * Checks to see if more than one author has published posts.
370  *
371  * @since 3.2.0
372  * @return bool Whether or not we have more than one author
373  */
374 function is_multi_author() {
375         global $wpdb;
376
377         if ( false === ( $is_multi_author = wp_cache_get('is_multi_author', 'posts') ) ) {
378                 $rows = (array) $wpdb->get_col("SELECT DISTINCT post_author FROM $wpdb->posts WHERE post_type = 'post' AND post_status = 'publish' LIMIT 2");
379                 $is_multi_author = 1 < count( $rows ) ? 1 : 0;
380                 wp_cache_set('is_multi_author', $is_multi_author, 'posts');
381         }
382
383         return apply_filters( 'is_multi_author', (bool) $is_multi_author );
384 }
385
386 /**
387  * Helper function to clear the cache for number of authors.
388  *
389  * @private
390  */
391 function __clear_multi_author_cache() {
392         wp_cache_delete('is_multi_author', 'posts');
393 }
394 add_action('transition_post_status', '__clear_multi_author_cache');