changeset 5 | 5e2f62d02dcd |
parent 0 | d970ebf37754 |
child 7 | cf61fcea0001 |
4:346c88efed21 | 5:5e2f62d02dcd |
---|---|
2 /** |
2 /** |
3 * Author Template functions for use in themes. |
3 * Author Template functions for use in themes. |
4 * |
4 * |
5 * These functions must be used within the WordPress Loop. |
5 * These functions must be used within the WordPress Loop. |
6 * |
6 * |
7 * @link http://codex.wordpress.org/Author_Templates |
7 * @link https://codex.wordpress.org/Author_Templates |
8 * |
8 * |
9 * @package WordPress |
9 * @package WordPress |
10 * @subpackage Template |
10 * @subpackage Template |
11 */ |
11 */ |
12 |
12 |
13 /** |
13 /** |
14 * Retrieve the author of the current post. |
14 * Retrieve the author of the current post. |
15 * |
15 * |
16 * @since 1.5 |
16 * @since 1.5.0 |
17 * |
|
17 * @uses $authordata The current author's DB object. |
18 * @uses $authordata The current author's DB object. |
18 * @uses apply_filters() Calls 'the_author' hook on the author display name. |
|
19 * |
19 * |
20 * @param string $deprecated Deprecated. |
20 * @param string $deprecated Deprecated. |
21 * @return string The author's display name. |
21 * @return string The author's display name. |
22 */ |
22 */ |
23 function get_the_author($deprecated = '') { |
23 function get_the_author($deprecated = '') { |
47 * The normal, expected behavior of this function is to echo the author and not |
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. |
48 * return it. However, backwards compatibility has to be maintained. |
49 * |
49 * |
50 * @since 0.71 |
50 * @since 0.71 |
51 * @see get_the_author() |
51 * @see get_the_author() |
52 * @link http://codex.wordpress.org/Template_Tags/the_author |
52 * @link https://codex.wordpress.org/Template_Tags/the_author |
53 * |
53 * |
54 * @param string $deprecated Deprecated. |
54 * @param string $deprecated Deprecated. |
55 * @param string $deprecated_echo Deprecated. Use get_the_author(). Echo the string or return it. |
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(). |
56 * @return string The author's display name, from get_the_author(). |
57 */ |
57 */ |
66 } |
66 } |
67 |
67 |
68 /** |
68 /** |
69 * Retrieve the author who last edited the current post. |
69 * Retrieve the author who last edited the current post. |
70 * |
70 * |
71 * @since 2.8 |
71 * @since 2.8.0 |
72 * @uses $post The current post's DB object. |
72 * |
73 * @uses get_post_meta() Retrieves the ID of the author who last edited the current post. |
|
74 * @uses get_userdata() Retrieves the author's DB object. |
|
75 * @uses apply_filters() Calls 'the_modified_author' hook on the author display name. |
|
76 * @return string The author's display name. |
73 * @return string The author's display name. |
77 */ |
74 */ |
78 function get_the_modified_author() { |
75 function get_the_modified_author() { |
79 if ( $last_id = get_post_meta( get_post()->ID, '_edit_last', true) ) { |
76 if ( $last_id = get_post_meta( get_post()->ID, '_edit_last', true) ) { |
80 $last_user = get_userdata($last_id); |
77 $last_user = get_userdata($last_id); |
91 } |
88 } |
92 |
89 |
93 /** |
90 /** |
94 * Display the name of the author who last edited the current post. |
91 * Display the name of the author who last edited the current post. |
95 * |
92 * |
96 * @since 2.8 |
93 * @since 2.8.0 |
94 * |
|
97 * @see get_the_author() |
95 * @see get_the_author() |
98 * @return string The author's display name, from get_the_modified_author(). |
96 * @return string The author's display name, from get_the_modified_author(). |
99 */ |
97 */ |
100 function the_modified_author() { |
98 function the_modified_author() { |
101 echo get_the_modified_author(); |
99 echo get_the_modified_author(); |
102 } |
100 } |
103 |
101 |
104 /** |
102 /** |
105 * Retrieve the requested data of the author of the current post. |
103 * Retrieve the requested data of the author of the current post. |
106 * @link http://codex.wordpress.org/Template_Tags/the_author_meta |
104 * @link https://codex.wordpress.org/Template_Tags/the_author_meta |
107 * @since 2.8.0 |
105 * @since 2.8.0 |
108 * @uses $authordata The current author's DB object (if $user_id not specified). |
|
109 * @param string $field selects the field of the users record. |
106 * @param string $field selects the field of the users record. |
110 * @param int $user_id Optional. User ID. |
107 * @param int $user_id Optional. User ID. |
111 * @return string The author's field from the current author's DB object. |
108 * @return string The author's field from the current author's DB object. |
112 */ |
109 */ |
113 function get_the_author_meta( $field = '', $user_id = false ) { |
110 function get_the_author_meta( $field = '', $user_id = false ) { |
136 return apply_filters( 'get_the_author_' . $field, $value, $user_id ); |
133 return apply_filters( 'get_the_author_' . $field, $value, $user_id ); |
137 } |
134 } |
138 |
135 |
139 /** |
136 /** |
140 * Retrieve the requested data of the author of the current post. |
137 * Retrieve the requested data of the author of the current post. |
141 * @link http://codex.wordpress.org/Template_Tags/the_author_meta |
138 * @link https://codex.wordpress.org/Template_Tags/the_author_meta |
142 * @since 2.8.0 |
139 * @since 2.8.0 |
143 * @param string $field selects the field of the users record. |
140 * @param string $field selects the field of the users record. |
144 * @param int $user_id Optional. User ID. |
141 * @param int $user_id Optional. User ID. |
145 * @echo string The author's field from the current author's DB object. |
142 * @echo string The author's field from the current author's DB object. |
146 */ |
143 */ |
163 /** |
160 /** |
164 * Retrieve either author's link or author's name. |
161 * Retrieve either author's link or author's name. |
165 * |
162 * |
166 * If the author has a home page set, return an HTML link, otherwise just return the |
163 * If the author has a home page set, return an HTML link, otherwise just return the |
167 * author's name. |
164 * author's name. |
168 * |
|
169 * @uses get_the_author_meta() |
|
170 * @uses get_the_author() |
|
171 */ |
165 */ |
172 function get_the_author_link() { |
166 function get_the_author_link() { |
173 if ( get_the_author_meta('url') ) { |
167 if ( get_the_author_meta('url') ) { |
174 return '<a href="' . esc_url( get_the_author_meta('url') ) . '" title="' . esc_attr( sprintf(__("Visit %s’s website"), get_the_author()) ) . '" rel="author external">' . get_the_author() . '</a>'; |
168 return '<a href="' . esc_url( get_the_author_meta('url') ) . '" title="' . esc_attr( sprintf(__("Visit %s’s website"), get_the_author()) ) . '" rel="author external">' . get_the_author() . '</a>'; |
175 } else { |
169 } else { |
181 * Display either author's link or author's name. |
175 * Display either author's link or author's name. |
182 * |
176 * |
183 * If the author has a home page set, echo an HTML link, otherwise just echo the |
177 * If the author has a home page set, echo an HTML link, otherwise just echo the |
184 * author's name. |
178 * author's name. |
185 * |
179 * |
186 * @link http://codex.wordpress.org/Template_Tags/the_author_link |
180 * @link https://codex.wordpress.org/Template_Tags/the_author_link |
187 * @since 2.1 |
181 * |
188 * @uses get_the_author_link() |
182 * @since 2.1.0 |
189 */ |
183 */ |
190 function the_author_link() { |
184 function the_author_link() { |
191 echo get_the_author_link(); |
185 echo get_the_author_link(); |
192 } |
186 } |
193 |
187 |
194 /** |
188 /** |
195 * Retrieve the number of posts by the author of the current post. |
189 * Retrieve the number of posts by the author of the current post. |
196 * |
190 * |
197 * @since 1.5 |
191 * @since 1.5.0 |
198 * @uses $post The current post in the Loop's DB object. |
192 * |
199 * @uses count_user_posts() |
|
200 * @return int The number of posts by the author. |
193 * @return int The number of posts by the author. |
201 */ |
194 */ |
202 function get_the_author_posts() { |
195 function get_the_author_posts() { |
203 return count_user_posts( get_post()->post_author ); |
196 $post = get_post(); |
197 if ( ! $post ) { |
|
198 return 0; |
|
199 } |
|
200 return count_user_posts( $post->post_author, $post->post_type ); |
|
204 } |
201 } |
205 |
202 |
206 /** |
203 /** |
207 * Display the number of posts by the author of the current post. |
204 * Display the number of posts by the author of the current post. |
208 * |
205 * |
209 * @link http://codex.wordpress.org/Template_Tags/the_author_posts |
206 * @link https://codex.wordpress.org/Template_Tags/the_author_posts |
210 * @since 0.71 |
207 * @since 0.71 |
211 * @uses get_the_author_posts() Echoes returned value from function. |
|
212 */ |
208 */ |
213 function the_author_posts() { |
209 function the_author_posts() { |
214 echo get_the_author_posts(); |
210 echo get_the_author_posts(); |
215 } |
211 } |
216 |
212 |
219 * |
215 * |
220 * Does just echo get_author_posts_url() function, like the others do. The |
216 * Does just echo get_author_posts_url() function, like the others do. The |
221 * reason for this, is that another function is used to help in printing the |
217 * reason for this, is that another function is used to help in printing the |
222 * link to the author's posts. |
218 * link to the author's posts. |
223 * |
219 * |
224 * @link http://codex.wordpress.org/Template_Tags/the_author_posts_link |
220 * @link https://codex.wordpress.org/Template_Tags/the_author_posts_link |
225 * @since 1.2.0 |
221 * @since 1.2.0 |
226 * @uses $authordata The current author's DB object. |
|
227 * @uses get_author_posts_url() |
|
228 * @uses get_the_author() |
|
229 * @param string $deprecated Deprecated. |
222 * @param string $deprecated Deprecated. |
230 */ |
223 */ |
231 function the_author_posts_link($deprecated = '') { |
224 function the_author_posts_link($deprecated = '') { |
232 if ( !empty( $deprecated ) ) |
225 if ( !empty( $deprecated ) ) |
233 _deprecated_argument( __FUNCTION__, '2.1' ); |
226 _deprecated_argument( __FUNCTION__, '2.1' ); |
292 } |
285 } |
293 |
286 |
294 /** |
287 /** |
295 * List all the authors of the blog, with several options available. |
288 * List all the authors of the blog, with several options available. |
296 * |
289 * |
297 * <ul> |
290 * @link https://codex.wordpress.org/Template_Tags/wp_list_authors |
298 * <li>optioncount (boolean) (false): Show the count in parenthesis next to the |
291 * |
299 * author's name.</li> |
|
300 * <li>exclude_admin (boolean) (true): Exclude the 'admin' user that is |
|
301 * installed by default.</li> |
|
302 * <li>show_fullname (boolean) (false): Show their full names.</li> |
|
303 * <li>hide_empty (boolean) (true): Don't show authors without any posts.</li> |
|
304 * <li>feed (string) (''): If isn't empty, show links to author's feeds.</li> |
|
305 * <li>feed_image (string) (''): If isn't empty, use this image to link to |
|
306 * feeds.</li> |
|
307 * <li>echo (boolean) (true): Set to false to return the output, instead of |
|
308 * echoing.</li> |
|
309 * <li>style (string) ('list'): Whether to display list of authors in list form |
|
310 * or as a string.</li> |
|
311 * <li>html (bool) (true): Whether to list the items in html form or plaintext. |
|
312 * </li> |
|
313 * </ul> |
|
314 * |
|
315 * @link http://codex.wordpress.org/Template_Tags/wp_list_authors |
|
316 * @since 1.2.0 |
292 * @since 1.2.0 |
317 * @param array $args The argument array. |
293 * |
318 * @return null|string The output, if echo is set to false. |
294 * @param string|array $args { |
319 */ |
295 * Optional. Array or string of default arguments. |
320 function wp_list_authors($args = '') { |
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; |
321 global $wpdb; |
322 |
322 |
323 $defaults = array( |
323 $defaults = array( |
324 'orderby' => 'name', 'order' => 'ASC', 'number' => '', |
324 'orderby' => 'name', 'order' => 'ASC', 'number' => '', |
325 'optioncount' => false, 'exclude_admin' => true, |
325 'optioncount' => false, 'exclude_admin' => true, |
326 'show_fullname' => false, 'hide_empty' => true, |
326 'show_fullname' => false, 'hide_empty' => true, |
327 'feed' => '', 'feed_image' => '', 'feed_type' => '', 'echo' => true, |
327 'feed' => '', 'feed_image' => '', 'feed_type' => '', 'echo' => true, |
328 'style' => 'list', 'html' => true |
328 'style' => 'list', 'html' => true, 'exclude' => '', 'include' => '' |
329 ); |
329 ); |
330 |
330 |
331 $args = wp_parse_args( $args, $defaults ); |
331 $args = wp_parse_args( $args, $defaults ); |
332 extract( $args, EXTR_SKIP ); |
|
333 |
332 |
334 $return = ''; |
333 $return = ''; |
335 |
334 |
336 $query_args = wp_array_slice_assoc( $args, array( 'orderby', 'order', 'number' ) ); |
335 $query_args = wp_array_slice_assoc( $args, array( 'orderby', 'order', 'number', 'exclude', 'include' ) ); |
337 $query_args['fields'] = 'ids'; |
336 $query_args['fields'] = 'ids'; |
338 $authors = get_users( $query_args ); |
337 $authors = get_users( $query_args ); |
339 |
338 |
340 $author_count = array(); |
339 $author_count = array(); |
341 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 ) |
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 ) { |
342 $author_count[$row->post_author] = $row->count; |
341 $author_count[$row->post_author] = $row->count; |
343 |
342 } |
344 foreach ( $authors as $author_id ) { |
343 foreach ( $authors as $author_id ) { |
345 $author = get_userdata( $author_id ); |
344 $author = get_userdata( $author_id ); |
346 |
345 |
347 if ( $exclude_admin && 'admin' == $author->display_name ) |
346 if ( $args['exclude_admin'] && 'admin' == $author->display_name ) { |
348 continue; |
347 continue; |
348 } |
|
349 |
349 |
350 $posts = isset( $author_count[$author->ID] ) ? $author_count[$author->ID] : 0; |
350 $posts = isset( $author_count[$author->ID] ) ? $author_count[$author->ID] : 0; |
351 |
351 |
352 if ( !$posts && $hide_empty ) |
352 if ( ! $posts && $args['hide_empty'] ) { |
353 continue; |
353 continue; |
354 |
354 } |
355 $link = ''; |
355 |
356 |
356 if ( $args['show_fullname'] && $author->first_name && $author->last_name ) { |
357 if ( $show_fullname && $author->first_name && $author->last_name ) |
|
358 $name = "$author->first_name $author->last_name"; |
357 $name = "$author->first_name $author->last_name"; |
359 else |
358 } else { |
360 $name = $author->display_name; |
359 $name = $author->display_name; |
361 |
360 } |
362 if ( !$html ) { |
361 |
362 if ( ! $args['html'] ) { |
|
363 $return .= $name . ', '; |
363 $return .= $name . ', '; |
364 |
364 |
365 continue; // No need to go further to process HTML. |
365 continue; // No need to go further to process HTML. |
366 } |
366 } |
367 |
367 |
368 if ( 'list' == $style ) { |
368 if ( 'list' == $args['style'] ) { |
369 $return .= '<li>'; |
369 $return .= '<li>'; |
370 } |
370 } |
371 |
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>'; |
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 |
373 |
374 if ( !empty( $feed_image ) || !empty( $feed ) ) { |
374 if ( ! empty( $args['feed_image'] ) || ! empty( $args['feed'] ) ) { |
375 $link .= ' '; |
375 $link .= ' '; |
376 if ( empty( $feed_image ) ) { |
376 if ( empty( $args['feed_image'] ) ) { |
377 $link .= '('; |
377 $link .= '('; |
378 } |
378 } |
379 |
379 |
380 $link .= '<a href="' . get_author_feed_link( $author->ID ) . '"'; |
380 $link .= '<a href="' . get_author_feed_link( $author->ID, $args['feed_type'] ) . '"'; |
381 |
381 |
382 $alt = $title = ''; |
382 $alt = ''; |
383 if ( !empty( $feed ) ) { |
383 if ( ! empty( $args['feed'] ) ) { |
384 $title = ' title="' . esc_attr( $feed ) . '"'; |
384 $alt = ' alt="' . esc_attr( $args['feed'] ) . '"'; |
385 $alt = ' alt="' . esc_attr( $feed ) . '"'; |
385 $name = $args['feed']; |
386 $name = $feed; |
|
387 $link .= $title; |
|
388 } |
386 } |
389 |
387 |
390 $link .= '>'; |
388 $link .= '>'; |
391 |
389 |
392 if ( !empty( $feed_image ) ) |
390 if ( ! empty( $args['feed_image'] ) ) { |
393 $link .= '<img src="' . esc_url( $feed_image ) . '" style="border: none;"' . $alt . $title . ' />'; |
391 $link .= '<img src="' . esc_url( $args['feed_image'] ) . '" style="border: none;"' . $alt . ' />'; |
394 else |
392 } else { |
395 $link .= $name; |
393 $link .= $name; |
394 } |
|
396 |
395 |
397 $link .= '</a>'; |
396 $link .= '</a>'; |
398 |
397 |
399 if ( empty( $feed_image ) ) |
398 if ( empty( $args['feed_image'] ) ) { |
400 $link .= ')'; |
399 $link .= ')'; |
401 } |
400 } |
402 |
401 } |
403 if ( $optioncount ) |
402 |
403 if ( $args['optioncount'] ) { |
|
404 $link .= ' ('. $posts . ')'; |
404 $link .= ' ('. $posts . ')'; |
405 } |
|
405 |
406 |
406 $return .= $link; |
407 $return .= $link; |
407 $return .= ( 'list' == $style ) ? '</li>' : ', '; |
408 $return .= ( 'list' == $args['style'] ) ? '</li>' : ', '; |
408 } |
409 } |
409 |
410 |
410 $return = rtrim($return, ', '); |
411 $return = rtrim( $return, ', ' ); |
411 |
412 |
412 if ( !$echo ) |
413 if ( ! $args['echo'] ) { |
413 return $return; |
414 return $return; |
414 |
415 } |
415 echo $return; |
416 echo $return; |
416 } |
417 } |
417 |
418 |
418 /** |
419 /** |
419 * Does this site have more than one author |
420 * Does this site have more than one author |
448 * @private |
449 * @private |
449 */ |
450 */ |
450 function __clear_multi_author_cache() { |
451 function __clear_multi_author_cache() { |
451 delete_transient( 'is_multi_author' ); |
452 delete_transient( 'is_multi_author' ); |
452 } |
453 } |
453 add_action('transition_post_status', '__clear_multi_author_cache'); |