]> scripts.mit.edu Git - autoinstalls/wordpress.git/blob - wp-includes/author-template.php
WordPress 3.9.2-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.0
17  *
18  * @uses $authordata The current author's DB object.
19  * @uses apply_filters() Calls 'the_author' hook on the author display name.
20  *
21  * @param string $deprecated Deprecated.
22  * @return string The author's display name.
23  */
24 function get_the_author($deprecated = '') {
25         global $authordata;
26
27         if ( !empty( $deprecated ) )
28                 _deprecated_argument( __FUNCTION__, '2.1' );
29
30         /**
31          * Filter the display name of the current post's author.
32          *
33          * @since 2.9.0
34          *
35          * @param string $authordata->display_name The author's display name.
36          */
37         return apply_filters('the_author', is_object($authordata) ? $authordata->display_name : null);
38 }
39
40 /**
41  * Display the name of the author of the current post.
42  *
43  * The behavior of this function is based off of old functionality predating
44  * get_the_author(). This function is not deprecated, but is designed to echo
45  * the value from get_the_author() and as an result of any old theme that might
46  * still use the old behavior will also pass the value from get_the_author().
47  *
48  * The normal, expected behavior of this function is to echo the author and not
49  * return it. However, backwards compatibility has to be maintained.
50  *
51  * @since 0.71
52  * @see get_the_author()
53  * @link http://codex.wordpress.org/Template_Tags/the_author
54  *
55  * @param string $deprecated Deprecated.
56  * @param string $deprecated_echo Deprecated. Use get_the_author(). Echo the string or return it.
57  * @return string The author's display name, from get_the_author().
58  */
59 function the_author( $deprecated = '', $deprecated_echo = true ) {
60         if ( !empty( $deprecated ) )
61                 _deprecated_argument( __FUNCTION__, '2.1' );
62         if ( $deprecated_echo !== true )
63                 _deprecated_argument( __FUNCTION__, '1.5', __('Use <code>get_the_author()</code> instead if you do not want the value echoed.') );
64         if ( $deprecated_echo )
65                 echo get_the_author();
66         return get_the_author();
67 }
68
69 /**
70  * Retrieve the author who last edited the current post.
71  *
72  * @since 2.8.0
73  *
74  * @uses $post The current post's DB object.
75  * @uses get_post_meta() Retrieves the ID of the author who last edited the current post.
76  * @uses get_userdata() Retrieves the author's DB object.
77  * @uses apply_filters() Calls 'the_modified_author' hook on the author display name.
78  * @return string The author's display name.
79  */
80 function get_the_modified_author() {
81         if ( $last_id = get_post_meta( get_post()->ID, '_edit_last', true) ) {
82                 $last_user = get_userdata($last_id);
83
84                 /**
85                  * Filter the display name of the author who last edited the current post.
86                  *
87                  * @since 2.8.0
88                  *
89                  * @param string $last_user->display_name The author's display name.
90                  */
91                 return apply_filters('the_modified_author', $last_user->display_name);
92         }
93 }
94
95 /**
96  * Display the name of the author who last edited the current post.
97  *
98  * @since 2.8.0
99  *
100  * @see get_the_author()
101  * @return string The author's display name, from get_the_modified_author().
102  */
103 function the_modified_author() {
104         echo get_the_modified_author();
105 }
106
107 /**
108  * Retrieve the requested data of the author of the current post.
109  * @link http://codex.wordpress.org/Template_Tags/the_author_meta
110  * @since 2.8.0
111  * @uses $authordata The current author's DB object (if $user_id not specified).
112  * @param string $field selects the field of the users record.
113  * @param int $user_id Optional. User ID.
114  * @return string The author's field from the current author's DB object.
115  */
116 function get_the_author_meta( $field = '', $user_id = false ) {
117         if ( ! $user_id ) {
118                 global $authordata;
119                 $user_id = isset( $authordata->ID ) ? $authordata->ID : 0;
120         } else {
121                 $authordata = get_userdata( $user_id );
122         }
123
124         if ( in_array( $field, array( 'login', 'pass', 'nicename', 'email', 'url', 'registered', 'activation_key', 'status' ) ) )
125                 $field = 'user_' . $field;
126
127         $value = isset( $authordata->$field ) ? $authordata->$field : '';
128
129         /**
130          * Filter the value of the requested user metadata.
131          *
132          * The filter name is dynamic and depends on the $field parameter of the function.
133          *
134          * @since 2.8.0
135          *
136          * @param string $value   The value of the metadata.
137          * @param int    $user_id The user ID.
138          */
139         return apply_filters( 'get_the_author_' . $field, $value, $user_id );
140 }
141
142 /**
143  * Retrieve the requested data of the author of the current post.
144  * @link http://codex.wordpress.org/Template_Tags/the_author_meta
145  * @since 2.8.0
146  * @param string $field selects the field of the users record.
147  * @param int $user_id Optional. User ID.
148  * @echo string The author's field from the current author's DB object.
149  */
150 function the_author_meta( $field = '', $user_id = false ) {
151         $author_meta = get_the_author_meta( $field, $user_id );
152
153         /**
154          * The value of the requested user metadata.
155          *
156          * The filter name is dynamic and depends on the $field parameter of the function.
157          *
158          * @since 2.8.0
159          *
160          * @param string $author_meta The value of the metadata.
161          * @param int    $user_id     The user ID.
162          */
163         echo apply_filters( 'the_author_' . $field, $author_meta, $user_id );
164 }
165
166 /**
167  * Retrieve either author's link or author's name.
168  *
169  * If the author has a home page set, return an HTML link, otherwise just return the
170  * author's name.
171  *
172  * @uses get_the_author_meta()
173  * @uses get_the_author()
174  */
175 function get_the_author_link() {
176         if ( get_the_author_meta('url') ) {
177                 return '<a href="' . esc_url( get_the_author_meta('url') ) . '" title="' . esc_attr( sprintf(__("Visit %s&#8217;s website"), get_the_author()) ) . '" rel="author external">' . get_the_author() . '</a>';
178         } else {
179                 return get_the_author();
180         }
181 }
182
183 /**
184  * Display either author's link or author's name.
185  *
186  * If the author has a home page set, echo an HTML link, otherwise just echo the
187  * author's name.
188  *
189  * @link http://codex.wordpress.org/Template_Tags/the_author_link
190  *
191  * @since 2.1.0
192  *
193  * @uses get_the_author_link()
194  */
195 function the_author_link() {
196         echo get_the_author_link();
197 }
198
199 /**
200  * Retrieve the number of posts by the author of the current post.
201  *
202  * @since 1.5.0
203  *
204  * @uses $post The current post in the Loop's DB object.
205  * @uses count_user_posts()
206  * @return int The number of posts by the author.
207  */
208 function get_the_author_posts() {
209         return count_user_posts( get_post()->post_author );
210 }
211
212 /**
213  * Display the number of posts by the author of the current post.
214  *
215  * @link http://codex.wordpress.org/Template_Tags/the_author_posts
216  * @since 0.71
217  * @uses get_the_author_posts() Echoes returned value from function.
218  */
219 function the_author_posts() {
220         echo get_the_author_posts();
221 }
222
223 /**
224  * Display an HTML link to the author page of the author of the current post.
225  *
226  * Does just echo get_author_posts_url() function, like the others do. The
227  * reason for this, is that another function is used to help in printing the
228  * link to the author's posts.
229  *
230  * @link http://codex.wordpress.org/Template_Tags/the_author_posts_link
231  * @since 1.2.0
232  * @uses $authordata The current author's DB object.
233  * @uses get_author_posts_url()
234  * @uses get_the_author()
235  * @param string $deprecated Deprecated.
236  */
237 function the_author_posts_link($deprecated = '') {
238         if ( !empty( $deprecated ) )
239                 _deprecated_argument( __FUNCTION__, '2.1' );
240
241         global $authordata;
242         if ( !is_object( $authordata ) )
243                 return false;
244         $link = sprintf(
245                 '<a href="%1$s" title="%2$s" rel="author">%3$s</a>',
246                 esc_url( get_author_posts_url( $authordata->ID, $authordata->user_nicename ) ),
247                 esc_attr( sprintf( __( 'Posts by %s' ), get_the_author() ) ),
248                 get_the_author()
249         );
250
251         /**
252          * Filter the link to the author page of the author of the current post.
253          *
254          * @since 2.9.0
255          *
256          * @param string $link HTML link.
257          */
258         echo apply_filters( 'the_author_posts_link', $link );
259 }
260
261 /**
262  * Retrieve the URL to the author page for the user with the ID provided.
263  *
264  * @since 2.1.0
265  * @uses $wp_rewrite WP_Rewrite
266  * @return string The URL to the author's page.
267  */
268 function get_author_posts_url($author_id, $author_nicename = '') {
269         global $wp_rewrite;
270         $auth_ID = (int) $author_id;
271         $link = $wp_rewrite->get_author_permastruct();
272
273         if ( empty($link) ) {
274                 $file = home_url( '/' );
275                 $link = $file . '?author=' . $auth_ID;
276         } else {
277                 if ( '' == $author_nicename ) {
278                         $user = get_userdata($author_id);
279                         if ( !empty($user->user_nicename) )
280                                 $author_nicename = $user->user_nicename;
281                 }
282                 $link = str_replace('%author%', $author_nicename, $link);
283                 $link = home_url( user_trailingslashit( $link ) );
284         }
285
286         /**
287          * Filter the URL to the author's page.
288          *
289          * @since 2.1.0
290          *
291          * @param string $link            The URL to the author's page.
292          * @param int    $author_id       The author's id.
293          * @param string $author_nicename The author's nice name.
294          */
295         $link = apply_filters( 'author_link', $link, $author_id, $author_nicename );
296
297         return $link;
298 }
299
300 /**
301  * List all the authors of the blog, with several options available.
302  *
303  * <ul>
304  * <li>optioncount (boolean) (false): Show the count in parenthesis next to the
305  * author's name.</li>
306  * <li>exclude_admin (boolean) (true): Exclude the 'admin' user that is
307  * installed by default.</li>
308  * <li>show_fullname (boolean) (false): Show their full names.</li>
309  * <li>hide_empty (boolean) (true): Don't show authors without any posts.</li>
310  * <li>feed (string) (''): If isn't empty, show links to author's feeds.</li>
311  * <li>feed_image (string) (''): If isn't empty, use this image to link to
312  * feeds.</li>
313  * <li>echo (boolean) (true): Set to false to return the output, instead of
314  * echoing.</li>
315  * <li>style (string) ('list'): Whether to display list of authors in list form
316  * or as a string.</li>
317  * <li>html (bool) (true): Whether to list the items in html form or plaintext.
318  * </li>
319  * <li>exclude (array): Array of user IDs to explicitly exclude.</li>
320  * <li>include (array): Array of user IDs to explicitly include.</li>
321  * </ul>
322  *
323  * @link http://codex.wordpress.org/Template_Tags/wp_list_authors
324  * @since 1.2.0
325  * @param array $args The argument array.
326  * @return null|string The output, if echo is set to false.
327  */
328 function wp_list_authors($args = '') {
329         global $wpdb;
330
331         $defaults = array(
332                 'orderby' => 'name', 'order' => 'ASC', 'number' => '',
333                 'optioncount' => false, 'exclude_admin' => true,
334                 'show_fullname' => false, 'hide_empty' => true,
335                 'feed' => '', 'feed_image' => '', 'feed_type' => '', 'echo' => true,
336                 'style' => 'list', 'html' => true, 'exclude' => '', 'include' => ''
337         );
338
339         $args = wp_parse_args( $args, $defaults );
340         extract( $args, EXTR_SKIP );
341
342         $return = '';
343
344         $query_args = wp_array_slice_assoc( $args, array( 'orderby', 'order', 'number', 'exclude', 'include' ) );
345         $query_args['fields'] = 'ids';
346         $authors = get_users( $query_args );
347
348         $author_count = array();
349         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 )
350                 $author_count[$row->post_author] = $row->count;
351
352         foreach ( $authors as $author_id ) {
353                 $author = get_userdata( $author_id );
354
355                 if ( $exclude_admin && 'admin' == $author->display_name )
356                         continue;
357
358                 $posts = isset( $author_count[$author->ID] ) ? $author_count[$author->ID] : 0;
359
360                 if ( !$posts && $hide_empty )
361                         continue;
362
363                 $link = '';
364
365                 if ( $show_fullname && $author->first_name && $author->last_name )
366                         $name = "$author->first_name $author->last_name";
367                 else
368                         $name = $author->display_name;
369
370                 if ( !$html ) {
371                         $return .= $name . ', ';
372
373                         continue; // No need to go further to process HTML.
374                 }
375
376                 if ( 'list' == $style ) {
377                         $return .= '<li>';
378                 }
379
380                 $link = '<a href="' . get_author_posts_url( $author->ID, $author->user_nicename ) . '" title="' . esc_attr( sprintf(__("Posts by %s"), $author->display_name) ) . '">' . $name . '</a>';
381
382                 if ( !empty( $feed_image ) || !empty( $feed ) ) {
383                         $link .= ' ';
384                         if ( empty( $feed_image ) ) {
385                                 $link .= '(';
386                         }
387
388                         $link .= '<a href="' . get_author_feed_link( $author->ID, $feed_type ) . '"';
389
390                         $alt = '';
391                         if ( !empty( $feed ) ) {
392                                 $alt = ' alt="' . esc_attr( $feed ) . '"';
393                                 $name = $feed;
394                         }
395
396                         $link .= '>';
397
398                         if ( !empty( $feed_image ) )
399                                 $link .= '<img src="' . esc_url( $feed_image ) . '" style="border: none;"' . $alt . ' />';
400                         else
401                                 $link .= $name;
402
403                         $link .= '</a>';
404
405                         if ( empty( $feed_image ) )
406                                 $link .= ')';
407                 }
408
409                 if ( $optioncount )
410                         $link .= ' ('. $posts . ')';
411
412                 $return .= $link;
413                 $return .= ( 'list' == $style ) ? '</li>' : ', ';
414         }
415
416         $return = rtrim($return, ', ');
417
418         if ( !$echo )
419                 return $return;
420
421         echo $return;
422 }
423
424 /**
425  * Does this site have more than one author
426  *
427  * Checks to see if more than one author has published posts.
428  *
429  * @since 3.2.0
430  * @return bool Whether or not we have more than one author
431  */
432 function is_multi_author() {
433         global $wpdb;
434
435         if ( false === ( $is_multi_author = get_transient( 'is_multi_author' ) ) ) {
436                 $rows = (array) $wpdb->get_col("SELECT DISTINCT post_author FROM $wpdb->posts WHERE post_type = 'post' AND post_status = 'publish' LIMIT 2");
437                 $is_multi_author = 1 < count( $rows ) ? 1 : 0;
438                 set_transient( 'is_multi_author', $is_multi_author );
439         }
440
441         /**
442          * Filter whether the site has more than one author with published posts.
443          *
444          * @since 3.2.0
445          *
446          * @param bool $is_multi_author Whether $is_multi_author should evaluate as true.
447          */
448         return apply_filters( 'is_multi_author', (bool) $is_multi_author );
449 }
450
451 /**
452  * Helper function to clear the cache for number of authors.
453  *
454  * @private
455  */
456 function __clear_multi_author_cache() {
457         delete_transient( 'is_multi_author' );
458 }
459 add_action('transition_post_status', '__clear_multi_author_cache');