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