]> scripts.mit.edu Git - autoinstalls/wordpress.git/blob - wp-includes/author-template.php
WordPress 4.7-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.0' );
28
29         /**
30          * Filters 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, backward 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.0' );
61         }
62
63         if ( true !== $deprecated_echo ) {
64                 _deprecated_argument( __FUNCTION__, '1.5.0',
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                  * Filters 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          * Filters 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 sprintf( '<a href="%1$s" title="%2$s" rel="author external">%3$s</a>',
192                         esc_url( get_the_author_meta('url') ),
193                         /* translators: %s: author's display name */
194                         esc_attr( sprintf( __( 'Visit %s&#8217;s website' ), get_the_author() ) ),
195                         get_the_author()
196                 );
197         } else {
198                 return get_the_author();
199         }
200 }
201
202 /**
203  * Display either author's link or author's name.
204  *
205  * If the author has a home page set, echo an HTML link, otherwise just echo the
206  * author's name.
207  *
208  * @link https://codex.wordpress.org/Template_Tags/the_author_link
209  *
210  * @since 2.1.0
211  */
212 function the_author_link() {
213         echo get_the_author_link();
214 }
215
216 /**
217  * Retrieve the number of posts by the author of the current post.
218  *
219  * @since 1.5.0
220  *
221  * @return int The number of posts by the author.
222  */
223 function get_the_author_posts() {
224         $post = get_post();
225         if ( ! $post ) {
226                 return 0;
227         }
228         return count_user_posts( $post->post_author, $post->post_type );
229 }
230
231 /**
232  * Display the number of posts by the author of the current post.
233  *
234  * @link https://codex.wordpress.org/Template_Tags/the_author_posts
235  * @since 0.71
236  */
237 function the_author_posts() {
238         echo get_the_author_posts();
239 }
240
241 /**
242  * Retrieves an HTML link to the author page of the current post's author.
243  *
244  * Returns an HTML-formatted link using get_author_posts_url().
245  *
246  * @since 4.4.0
247  *
248  * @global object $authordata The current author's DB object.
249  *
250  * @return string An HTML link to the author page.
251  */
252 function get_the_author_posts_link() {
253         global $authordata;
254         if ( ! is_object( $authordata ) ) {
255                 return;
256         }
257
258         $link = sprintf( '<a href="%1$s" title="%2$s" rel="author">%3$s</a>',
259                 esc_url( get_author_posts_url( $authordata->ID, $authordata->user_nicename ) ),
260                 /* translators: %s: author's display name */
261                 esc_attr( sprintf( __( 'Posts by %s' ), get_the_author() ) ),
262                 get_the_author()
263         );
264
265         /**
266          * Filters the link to the author page of the author of the current post.
267          *
268          * @since 2.9.0
269          *
270          * @param string $link HTML link.
271          */
272         return apply_filters( 'the_author_posts_link', $link );
273 }
274
275 /**
276  * Displays an HTML link to the author page of the current post's author.
277  *
278  * @since 1.2.0
279  * @since 4.4.0 Converted into a wrapper for get_the_author_posts_link()
280  *
281  * @param string $deprecated Unused.
282  */
283 function the_author_posts_link( $deprecated = '' ) {
284         if ( ! empty( $deprecated ) ) {
285                 _deprecated_argument( __FUNCTION__, '2.1.0' );
286         }
287         echo get_the_author_posts_link();
288 }
289
290 /**
291  * Retrieve the URL to the author page for the user with the ID provided.
292  *
293  * @since 2.1.0
294  *
295  * @global WP_Rewrite $wp_rewrite
296  *
297  * @param int    $author_id       Author ID.
298  * @param string $author_nicename Optional. The author's nicename (slug). Default empty.
299  * @return string The URL to the author's page.
300  */
301 function get_author_posts_url( $author_id, $author_nicename = '' ) {
302         global $wp_rewrite;
303         $auth_ID = (int) $author_id;
304         $link = $wp_rewrite->get_author_permastruct();
305
306         if ( empty($link) ) {
307                 $file = home_url( '/' );
308                 $link = $file . '?author=' . $auth_ID;
309         } else {
310                 if ( '' == $author_nicename ) {
311                         $user = get_userdata($author_id);
312                         if ( !empty($user->user_nicename) )
313                                 $author_nicename = $user->user_nicename;
314                 }
315                 $link = str_replace('%author%', $author_nicename, $link);
316                 $link = home_url( user_trailingslashit( $link ) );
317         }
318
319         /**
320          * Filters the URL to the author's page.
321          *
322          * @since 2.1.0
323          *
324          * @param string $link            The URL to the author's page.
325          * @param int    $author_id       The author's id.
326          * @param string $author_nicename The author's nice name.
327          */
328         $link = apply_filters( 'author_link', $link, $author_id, $author_nicename );
329
330         return $link;
331 }
332
333 /**
334  * List all the authors of the site, with several options available.
335  *
336  * @link https://codex.wordpress.org/Template_Tags/wp_list_authors
337  *
338  * @since 1.2.0
339  *
340  * @global wpdb $wpdb WordPress database abstraction object.
341  *
342  * @param string|array $args {
343  *     Optional. Array or string of default arguments.
344  *
345  *     @type string       $orderby       How to sort the authors. Accepts 'nicename', 'email', 'url', 'registered',
346  *                                       'user_nicename', 'user_email', 'user_url', 'user_registered', 'name',
347  *                                       'display_name', 'post_count', 'ID', 'meta_value', 'user_login'. Default 'name'.
348  *     @type string       $order         Sorting direction for $orderby. Accepts 'ASC', 'DESC'. Default 'ASC'.
349  *     @type int          $number        Maximum authors to return or display. Default empty (all authors).
350  *     @type bool         $optioncount   Show the count in parenthesis next to the author's name. Default false.
351  *     @type bool         $exclude_admin Whether to exclude the 'admin' account, if it exists. Default false.
352  *     @type bool         $show_fullname Whether to show the author's full name. Default false.
353  *     @type bool         $hide_empty    Whether to hide any authors with no posts. Default true.
354  *     @type string       $feed          If not empty, show a link to the author's feed and use this text as the alt
355  *                                       parameter of the link. Default empty.
356  *     @type string       $feed_image    If not empty, show a link to the author's feed and use this image URL as
357  *                                       clickable anchor. Default empty.
358  *     @type string       $feed_type     The feed type to link to, such as 'rss2'. Defaults to default feed type.
359  *     @type bool         $echo          Whether to output the result or instead return it. Default true.
360  *     @type string       $style         If 'list', each author is wrapped in an `<li>` element, otherwise the authors
361  *                                       will be separated by commas.
362  *     @type bool         $html          Whether to list the items in HTML form or plaintext. Default true.
363  *     @type array|string $exclude       Array or comma/space-separated list of author IDs to exclude. Default empty.
364  *     @type array|string $include       Array or comma/space-separated list of author IDs to include. Default empty.
365  * }
366  * @return string|void The output, if echo is set to false.
367  */
368 function wp_list_authors( $args = '' ) {
369         global $wpdb;
370
371         $defaults = array(
372                 'orderby' => 'name', 'order' => 'ASC', 'number' => '',
373                 'optioncount' => false, 'exclude_admin' => true,
374                 'show_fullname' => false, 'hide_empty' => true,
375                 'feed' => '', 'feed_image' => '', 'feed_type' => '', 'echo' => true,
376                 'style' => 'list', 'html' => true, 'exclude' => '', 'include' => ''
377         );
378
379         $args = wp_parse_args( $args, $defaults );
380
381         $return = '';
382
383         $query_args = wp_array_slice_assoc( $args, array( 'orderby', 'order', 'number', 'exclude', 'include' ) );
384         $query_args['fields'] = 'ids';
385         $authors = get_users( $query_args );
386
387         $author_count = array();
388         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 ) {
389                 $author_count[$row->post_author] = $row->count;
390         }
391         foreach ( $authors as $author_id ) {
392                 $author = get_userdata( $author_id );
393
394                 if ( $args['exclude_admin'] && 'admin' == $author->display_name ) {
395                         continue;
396                 }
397
398                 $posts = isset( $author_count[$author->ID] ) ? $author_count[$author->ID] : 0;
399
400                 if ( ! $posts && $args['hide_empty'] ) {
401                         continue;
402                 }
403
404                 if ( $args['show_fullname'] && $author->first_name && $author->last_name ) {
405                         $name = "$author->first_name $author->last_name";
406                 } else {
407                         $name = $author->display_name;
408                 }
409
410                 if ( ! $args['html'] ) {
411                         $return .= $name . ', ';
412
413                         continue; // No need to go further to process HTML.
414                 }
415
416                 if ( 'list' == $args['style'] ) {
417                         $return .= '<li>';
418                 }
419
420                 $link = sprintf( '<a href="%1$s" title="%2$s">%3$s</a>',
421                         get_author_posts_url( $author->ID, $author->user_nicename ),
422                         /* translators: %s: author's display name */
423                         esc_attr( sprintf( __( 'Posts by %s' ), $author->display_name ) ),
424                         $name
425                 );
426
427                 if ( ! empty( $args['feed_image'] ) || ! empty( $args['feed'] ) ) {
428                         $link .= ' ';
429                         if ( empty( $args['feed_image'] ) ) {
430                                 $link .= '(';
431                         }
432
433                         $link .= '<a href="' . get_author_feed_link( $author->ID, $args['feed_type'] ) . '"';
434
435                         $alt = '';
436                         if ( ! empty( $args['feed'] ) ) {
437                                 $alt = ' alt="' . esc_attr( $args['feed'] ) . '"';
438                                 $name = $args['feed'];
439                         }
440
441                         $link .= '>';
442
443                         if ( ! empty( $args['feed_image'] ) ) {
444                                 $link .= '<img src="' . esc_url( $args['feed_image'] ) . '" style="border: none;"' . $alt . ' />';
445                         } else {
446                                 $link .= $name;
447                         }
448
449                         $link .= '</a>';
450
451                         if ( empty( $args['feed_image'] ) ) {
452                                 $link .= ')';
453                         }
454                 }
455
456                 if ( $args['optioncount'] ) {
457                         $link .= ' ('. $posts . ')';
458                 }
459
460                 $return .= $link;
461                 $return .= ( 'list' == $args['style'] ) ? '</li>' : ', ';
462         }
463
464         $return = rtrim( $return, ', ' );
465
466         if ( ! $args['echo'] ) {
467                 return $return;
468         }
469         echo $return;
470 }
471
472 /**
473  * Does this site have more than one author
474  *
475  * Checks to see if more than one author has published posts.
476  *
477  * @since 3.2.0
478  *
479  * @global wpdb $wpdb WordPress database abstraction object.
480  *
481  * @return bool Whether or not we have more than one author
482  */
483 function is_multi_author() {
484         global $wpdb;
485
486         if ( false === ( $is_multi_author = get_transient( 'is_multi_author' ) ) ) {
487                 $rows = (array) $wpdb->get_col("SELECT DISTINCT post_author FROM $wpdb->posts WHERE post_type = 'post' AND post_status = 'publish' LIMIT 2");
488                 $is_multi_author = 1 < count( $rows ) ? 1 : 0;
489                 set_transient( 'is_multi_author', $is_multi_author );
490         }
491
492         /**
493          * Filters whether the site has more than one author with published posts.
494          *
495          * @since 3.2.0
496          *
497          * @param bool $is_multi_author Whether $is_multi_author should evaluate as true.
498          */
499         return apply_filters( 'is_multi_author', (bool) $is_multi_author );
500 }
501
502 /**
503  * Helper function to clear the cache for number of authors.
504  *
505  * @private
506  */
507 function __clear_multi_author_cache() {
508         delete_transient( 'is_multi_author' );
509 }