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