0
|
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 http://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 |
|
17 |
* @uses $authordata The current author's DB object. |
|
18 |
* @uses apply_filters() Calls 'the_author' hook on the author display name. |
|
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 http://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 |
|
72 |
* @uses $post The current post's DB object. |
|
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. |
|
77 |
*/ |
|
78 |
function get_the_modified_author() { |
|
79 |
if ( $last_id = get_post_meta( get_post()->ID, '_edit_last', true) ) { |
|
80 |
$last_user = get_userdata($last_id); |
|
81 |
|
|
82 |
/** |
|
83 |
* Filter the display name of the author who last edited the current post. |
|
84 |
* |
|
85 |
* @since 2.8.0 |
|
86 |
* |
|
87 |
* @param string $last_user->display_name The author's display name. |
|
88 |
*/ |
|
89 |
return apply_filters('the_modified_author', $last_user->display_name); |
|
90 |
} |
|
91 |
} |
|
92 |
|
|
93 |
/** |
|
94 |
* Display the name of the author who last edited the current post. |
|
95 |
* |
|
96 |
* @since 2.8 |
|
97 |
* @see get_the_author() |
|
98 |
* @return string The author's display name, from get_the_modified_author(). |
|
99 |
*/ |
|
100 |
function the_modified_author() { |
|
101 |
echo get_the_modified_author(); |
|
102 |
} |
|
103 |
|
|
104 |
/** |
|
105 |
* Retrieve the requested data of the author of the current post. |
|
106 |
* @link http://codex.wordpress.org/Template_Tags/the_author_meta |
|
107 |
* @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. |
|
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 |
if ( ! $user_id ) { |
|
115 |
global $authordata; |
|
116 |
$user_id = isset( $authordata->ID ) ? $authordata->ID : 0; |
|
117 |
} else { |
|
118 |
$authordata = get_userdata( $user_id ); |
|
119 |
} |
|
120 |
|
|
121 |
if ( in_array( $field, array( 'login', 'pass', 'nicename', 'email', 'url', 'registered', 'activation_key', 'status' ) ) ) |
|
122 |
$field = 'user_' . $field; |
|
123 |
|
|
124 |
$value = isset( $authordata->$field ) ? $authordata->$field : ''; |
|
125 |
|
|
126 |
/** |
|
127 |
* Filter the value of the requested user metadata. |
|
128 |
* |
|
129 |
* The filter name is dynamic and depends on the $field parameter of the function. |
|
130 |
* |
|
131 |
* @since 2.8.0 |
|
132 |
* |
|
133 |
* @param string $value The value of the metadata. |
|
134 |
* @param int $user_id The user ID. |
|
135 |
*/ |
|
136 |
return apply_filters( 'get_the_author_' . $field, $value, $user_id ); |
|
137 |
} |
|
138 |
|
|
139 |
/** |
|
140 |
* Retrieve the requested data of the author of the current post. |
|
141 |
* @link http://codex.wordpress.org/Template_Tags/the_author_meta |
|
142 |
* @since 2.8.0 |
|
143 |
* @param string $field selects the field of the users record. |
|
144 |
* @param int $user_id Optional. User ID. |
|
145 |
* @echo string The author's field from the current author's DB object. |
|
146 |
*/ |
|
147 |
function the_author_meta( $field = '', $user_id = false ) { |
|
148 |
$author_meta = get_the_author_meta( $field, $user_id ); |
|
149 |
|
|
150 |
/** |
|
151 |
* The value of the requested user metadata. |
|
152 |
* |
|
153 |
* The filter name is dynamic and depends on the $field parameter of the function. |
|
154 |
* |
|
155 |
* @since 2.8.0 |
|
156 |
* |
|
157 |
* @param string $author_meta The value of the metadata. |
|
158 |
* @param int $user_id The user ID. |
|
159 |
*/ |
|
160 |
echo apply_filters( 'the_author_' . $field, $author_meta, $user_id ); |
|
161 |
} |
|
162 |
|
|
163 |
/** |
|
164 |
* Retrieve either author's link or author's name. |
|
165 |
* |
|
166 |
* If the author has a home page set, return an HTML link, otherwise just return the |
|
167 |
* author's name. |
|
168 |
* |
|
169 |
* @uses get_the_author_meta() |
|
170 |
* @uses get_the_author() |
|
171 |
*/ |
|
172 |
function get_the_author_link() { |
|
173 |
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>'; |
|
175 |
} else { |
|
176 |
return get_the_author(); |
|
177 |
} |
|
178 |
} |
|
179 |
|
|
180 |
/** |
|
181 |
* Display either author's link or author's name. |
|
182 |
* |
|
183 |
* If the author has a home page set, echo an HTML link, otherwise just echo the |
|
184 |
* author's name. |
|
185 |
* |
|
186 |
* @link http://codex.wordpress.org/Template_Tags/the_author_link |
|
187 |
* @since 2.1 |
|
188 |
* @uses get_the_author_link() |
|
189 |
*/ |
|
190 |
function the_author_link() { |
|
191 |
echo get_the_author_link(); |
|
192 |
} |
|
193 |
|
|
194 |
/** |
|
195 |
* Retrieve the number of posts by the author of the current post. |
|
196 |
* |
|
197 |
* @since 1.5 |
|
198 |
* @uses $post The current post in the Loop's DB object. |
|
199 |
* @uses count_user_posts() |
|
200 |
* @return int The number of posts by the author. |
|
201 |
*/ |
|
202 |
function get_the_author_posts() { |
|
203 |
return count_user_posts( get_post()->post_author ); |
|
204 |
} |
|
205 |
|
|
206 |
/** |
|
207 |
* Display the number of posts by the author of the current post. |
|
208 |
* |
|
209 |
* @link http://codex.wordpress.org/Template_Tags/the_author_posts |
|
210 |
* @since 0.71 |
|
211 |
* @uses get_the_author_posts() Echoes returned value from function. |
|
212 |
*/ |
|
213 |
function the_author_posts() { |
|
214 |
echo get_the_author_posts(); |
|
215 |
} |
|
216 |
|
|
217 |
/** |
|
218 |
* Display an HTML link to the author page of the author of the current post. |
|
219 |
* |
|
220 |
* 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 |
|
222 |
* link to the author's posts. |
|
223 |
* |
|
224 |
* @link http://codex.wordpress.org/Template_Tags/the_author_posts_link |
|
225 |
* @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. |
|
230 |
*/ |
|
231 |
function the_author_posts_link($deprecated = '') { |
|
232 |
if ( !empty( $deprecated ) ) |
|
233 |
_deprecated_argument( __FUNCTION__, '2.1' ); |
|
234 |
|
|
235 |
global $authordata; |
|
236 |
if ( !is_object( $authordata ) ) |
|
237 |
return false; |
|
238 |
$link = sprintf( |
|
239 |
'<a href="%1$s" title="%2$s" rel="author">%3$s</a>', |
|
240 |
esc_url( get_author_posts_url( $authordata->ID, $authordata->user_nicename ) ), |
|
241 |
esc_attr( sprintf( __( 'Posts by %s' ), get_the_author() ) ), |
|
242 |
get_the_author() |
|
243 |
); |
|
244 |
|
|
245 |
/** |
|
246 |
* Filter the link to the author page of the author of the current post. |
|
247 |
* |
|
248 |
* @since 2.9.0 |
|
249 |
* |
|
250 |
* @param string $link HTML link. |
|
251 |
*/ |
|
252 |
echo apply_filters( 'the_author_posts_link', $link ); |
|
253 |
} |
|
254 |
|
|
255 |
/** |
|
256 |
* Retrieve the URL to the author page for the user with the ID provided. |
|
257 |
* |
|
258 |
* @since 2.1.0 |
|
259 |
* @uses $wp_rewrite WP_Rewrite |
|
260 |
* @return string The URL to the author's page. |
|
261 |
*/ |
|
262 |
function get_author_posts_url($author_id, $author_nicename = '') { |
|
263 |
global $wp_rewrite; |
|
264 |
$auth_ID = (int) $author_id; |
|
265 |
$link = $wp_rewrite->get_author_permastruct(); |
|
266 |
|
|
267 |
if ( empty($link) ) { |
|
268 |
$file = home_url( '/' ); |
|
269 |
$link = $file . '?author=' . $auth_ID; |
|
270 |
} else { |
|
271 |
if ( '' == $author_nicename ) { |
|
272 |
$user = get_userdata($author_id); |
|
273 |
if ( !empty($user->user_nicename) ) |
|
274 |
$author_nicename = $user->user_nicename; |
|
275 |
} |
|
276 |
$link = str_replace('%author%', $author_nicename, $link); |
|
277 |
$link = home_url( user_trailingslashit( $link ) ); |
|
278 |
} |
|
279 |
|
|
280 |
/** |
|
281 |
* Filter the URL to the author's page. |
|
282 |
* |
|
283 |
* @since 2.1.0 |
|
284 |
* |
|
285 |
* @param string $link The URL to the author's page. |
|
286 |
* @param int $author_id The author's id. |
|
287 |
* @param string $author_nicename The author's nice name. |
|
288 |
*/ |
|
289 |
$link = apply_filters( 'author_link', $link, $author_id, $author_nicename ); |
|
290 |
|
|
291 |
return $link; |
|
292 |
} |
|
293 |
|
|
294 |
/** |
|
295 |
* List all the authors of the blog, with several options available. |
|
296 |
* |
|
297 |
* <ul> |
|
298 |
* <li>optioncount (boolean) (false): Show the count in parenthesis next to the |
|
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 |
|
317 |
* @param array $args The argument array. |
|
318 |
* @return null|string The output, if echo is set to false. |
|
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 |
|
329 |
); |
|
330 |
|
|
331 |
$args = wp_parse_args( $args, $defaults ); |
|
332 |
extract( $args, EXTR_SKIP ); |
|
333 |
|
|
334 |
$return = ''; |
|
335 |
|
|
336 |
$query_args = wp_array_slice_assoc( $args, array( 'orderby', 'order', 'number' ) ); |
|
337 |
$query_args['fields'] = 'ids'; |
|
338 |
$authors = get_users( $query_args ); |
|
339 |
|
|
340 |
$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 ) |
|
342 |
$author_count[$row->post_author] = $row->count; |
|
343 |
|
|
344 |
foreach ( $authors as $author_id ) { |
|
345 |
$author = get_userdata( $author_id ); |
|
346 |
|
|
347 |
if ( $exclude_admin && 'admin' == $author->display_name ) |
|
348 |
continue; |
|
349 |
|
|
350 |
$posts = isset( $author_count[$author->ID] ) ? $author_count[$author->ID] : 0; |
|
351 |
|
|
352 |
if ( !$posts && $hide_empty ) |
|
353 |
continue; |
|
354 |
|
|
355 |
$link = ''; |
|
356 |
|
|
357 |
if ( $show_fullname && $author->first_name && $author->last_name ) |
|
358 |
$name = "$author->first_name $author->last_name"; |
|
359 |
else |
|
360 |
$name = $author->display_name; |
|
361 |
|
|
362 |
if ( !$html ) { |
|
363 |
$return .= $name . ', '; |
|
364 |
|
|
365 |
continue; // No need to go further to process HTML. |
|
366 |
} |
|
367 |
|
|
368 |
if ( 'list' == $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( $feed_image ) || !empty( $feed ) ) { |
|
375 |
$link .= ' '; |
|
376 |
if ( empty( $feed_image ) ) { |
|
377 |
$link .= '('; |
|
378 |
} |
|
379 |
|
|
380 |
$link .= '<a href="' . get_author_feed_link( $author->ID ) . '"'; |
|
381 |
|
|
382 |
$alt = $title = ''; |
|
383 |
if ( !empty( $feed ) ) { |
|
384 |
$title = ' title="' . esc_attr( $feed ) . '"'; |
|
385 |
$alt = ' alt="' . esc_attr( $feed ) . '"'; |
|
386 |
$name = $feed; |
|
387 |
$link .= $title; |
|
388 |
} |
|
389 |
|
|
390 |
$link .= '>'; |
|
391 |
|
|
392 |
if ( !empty( $feed_image ) ) |
|
393 |
$link .= '<img src="' . esc_url( $feed_image ) . '" style="border: none;"' . $alt . $title . ' />'; |
|
394 |
else |
|
395 |
$link .= $name; |
|
396 |
|
|
397 |
$link .= '</a>'; |
|
398 |
|
|
399 |
if ( empty( $feed_image ) ) |
|
400 |
$link .= ')'; |
|
401 |
} |
|
402 |
|
|
403 |
if ( $optioncount ) |
|
404 |
$link .= ' ('. $posts . ')'; |
|
405 |
|
|
406 |
$return .= $link; |
|
407 |
$return .= ( 'list' == $style ) ? '</li>' : ', '; |
|
408 |
} |
|
409 |
|
|
410 |
$return = rtrim($return, ', '); |
|
411 |
|
|
412 |
if ( !$echo ) |
|
413 |
return $return; |
|
414 |
|
|
415 |
echo $return; |
|
416 |
} |
|
417 |
|
|
418 |
/** |
|
419 |
* Does this site have more than one author |
|
420 |
* |
|
421 |
* Checks to see if more than one author has published posts. |
|
422 |
* |
|
423 |
* @since 3.2.0 |
|
424 |
* @return bool Whether or not we have more than one author |
|
425 |
*/ |
|
426 |
function is_multi_author() { |
|
427 |
global $wpdb; |
|
428 |
|
|
429 |
if ( false === ( $is_multi_author = get_transient( 'is_multi_author' ) ) ) { |
|
430 |
$rows = (array) $wpdb->get_col("SELECT DISTINCT post_author FROM $wpdb->posts WHERE post_type = 'post' AND post_status = 'publish' LIMIT 2"); |
|
431 |
$is_multi_author = 1 < count( $rows ) ? 1 : 0; |
|
432 |
set_transient( 'is_multi_author', $is_multi_author ); |
|
433 |
} |
|
434 |
|
|
435 |
/** |
|
436 |
* Filter whether the site has more than one author with published posts. |
|
437 |
* |
|
438 |
* @since 3.2.0 |
|
439 |
* |
|
440 |
* @param bool $is_multi_author Whether $is_multi_author should evaluate as true. |
|
441 |
*/ |
|
442 |
return apply_filters( 'is_multi_author', (bool) $is_multi_author ); |
|
443 |
} |
|
444 |
|
|
445 |
/** |
|
446 |
* Helper function to clear the cache for number of authors. |
|
447 |
* |
|
448 |
* @private |
|
449 |
*/ |
|
450 |
function __clear_multi_author_cache() { |
|
451 |
delete_transient( 'is_multi_author' ); |
|
452 |
} |
|
453 |
add_action('transition_post_status', '__clear_multi_author_cache'); |