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