PageRenderTime 108ms CodeModel.GetById 34ms RepoModel.GetById 7ms app.codeStats 0ms

/wp-includes/author-template.php

https://github.com/schr/wordpress
PHP | 593 lines | 297 code | 54 blank | 242 comment | 43 complexity | d0f44148ea1fd3a46bf40b9093e2def7 MD5 | raw file
  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. * Retrieve the author of the current post.
  14. *
  15. * @since 1.5
  16. * @uses $authordata The current author's DB object.
  17. * @uses apply_filters() Calls 'the_author' hook on the author display name.
  18. *
  19. * @param string $deprecated Deprecated.
  20. * @return string The author's display name.
  21. */
  22. function get_the_author($deprecated = '') {
  23. global $authordata;
  24. return apply_filters('the_author', $authordata->display_name);
  25. }
  26. /**
  27. * Display the name of the author of the current post.
  28. *
  29. * The behavior of this function is based off of old functionality predating
  30. * get_the_author(). This function is not deprecated, but is designed to echo
  31. * the value from get_the_author() and as an result of any old theme that might
  32. * still use the old behavior will also pass the value from get_the_author().
  33. *
  34. * The normal, expected behavior of this function is to echo the author and not
  35. * return it. However, backwards compatiability has to be maintained.
  36. *
  37. * @since 0.71
  38. * @see get_the_author()
  39. * @link http://codex.wordpress.org/Template_Tags/the_author
  40. *
  41. * @param string $deprecated Deprecated.
  42. * @param string $deprecated_echo Echo the string or return it.
  43. * @return string The author's display name, from get_the_author().
  44. */
  45. function the_author($deprecated = '', $deprecated_echo = true) {
  46. if ( $deprecated_echo )
  47. echo get_the_author();
  48. return get_the_author();
  49. }
  50. /**
  51. * Retrieve the author who last edited the current post.
  52. *
  53. * @since 2.8
  54. * @uses $post The current post's DB object.
  55. * @uses get_post_meta() Retrieves the ID of the author who last edited the current post.
  56. * @uses get_userdata() Retrieves the author's DB object.
  57. * @uses apply_filters() Calls 'the_modified_author' hook on the author display name.
  58. * @return string The author's display name.
  59. */
  60. function get_the_modified_author() {
  61. global $post;
  62. if ( $last_id = get_post_meta($post->ID, '_edit_last', true) ) {
  63. $last_user = get_userdata($last_id);
  64. return apply_filters('the_modified_author', $last_user->display_name);
  65. }
  66. }
  67. /**
  68. * Display the name of the author who last edited the current post.
  69. *
  70. * @since 2.8
  71. * @see get_the_author()
  72. * @return string The author's display name, from get_the_modified_author().
  73. */
  74. function the_modified_author() {
  75. echo get_the_modified_author();
  76. }
  77. /**
  78. * Retrieve the description of the author of the current post.
  79. *
  80. * @since 1.5
  81. * @uses $authordata The current author's DB object.
  82. * @return string The author's description.
  83. */
  84. function get_the_author_description() {
  85. global $authordata;
  86. return $authordata->description;
  87. }
  88. /**
  89. * Display the description of the author of the current post.
  90. *
  91. * @link http://codex.wordpress.org/Template_Tags/the_author_description
  92. * @since 1.0.0
  93. * @see get_the_author_description()
  94. */
  95. function the_author_description() {
  96. echo get_the_author_description();
  97. }
  98. /**
  99. * Retrieve the login name of the author of the current post.
  100. *
  101. * @since 1.5
  102. * @uses $authordata The current author's DB object.
  103. * @return string The author's login name (username).
  104. */
  105. function get_the_author_login() {
  106. global $authordata;
  107. return $authordata->user_login;
  108. }
  109. /**
  110. * Display the login name of the author of the current post.
  111. *
  112. * @link http://codex.wordpress.org/Template_Tags/the_author_login
  113. * @since 0.71
  114. * @see get_the_author_login()
  115. */
  116. function the_author_login() {
  117. echo get_the_author_login();
  118. }
  119. /**
  120. * Retrieve the first name of the author of the current post.
  121. *
  122. * @since 1.5
  123. * @uses $authordata The current author's DB object.
  124. * @return string The author's first name.
  125. */
  126. function get_the_author_firstname() {
  127. global $authordata;
  128. return $authordata->first_name;
  129. }
  130. /**
  131. * Display the first name of the author of the current post.
  132. *
  133. * @link http://codex.wordpress.org/Template_Tags/the_author_firstname
  134. * @since 0.71
  135. * @uses get_the_author_firstname()
  136. */
  137. function the_author_firstname() {
  138. echo get_the_author_firstname();
  139. }
  140. /**
  141. * Retrieve the last name of the author of the current post.
  142. *
  143. * @since 1.5
  144. * @uses $authordata The current author's DB object.
  145. * @return string The author's last name.
  146. */
  147. function get_the_author_lastname() {
  148. global $authordata;
  149. return $authordata->last_name;
  150. }
  151. /**
  152. * Display the last name of the author of the current post.
  153. *
  154. * @link http://codex.wordpress.org/Template_Tags/the_author_lastname
  155. * @since 0.71
  156. * @uses get_the_author_lastname()
  157. */
  158. function the_author_lastname() {
  159. echo get_the_author_lastname();
  160. }
  161. /**
  162. * Retrieve the nickname of the author of the current post.
  163. *
  164. * @since 1.5
  165. * @uses $authordata The current author's DB object.
  166. * @return string The author's nickname.
  167. */
  168. function get_the_author_nickname() {
  169. global $authordata;
  170. return $authordata->nickname;
  171. }
  172. /**
  173. * Display the nickname of the author of the current post.
  174. *
  175. * @link http://codex.wordpress.org/Template_Tags/the_author_nickname
  176. * @since 0.71
  177. * @uses get_the_author_nickname()
  178. */
  179. function the_author_nickname() {
  180. echo get_the_author_nickname();
  181. }
  182. /**
  183. * Retrieve the ID of the author of the current post.
  184. *
  185. * @since 1.5
  186. * @uses $authordata The current author's DB object.
  187. * @return int The author's ID.
  188. */
  189. function get_the_author_ID() {
  190. global $authordata;
  191. return (int) $authordata->ID;
  192. }
  193. /**
  194. * Display the ID of the author of the current post.
  195. *
  196. * @link http://codex.wordpress.org/Template_Tags/the_author_ID
  197. * @since 0.71
  198. * @uses get_the_author_ID()
  199. */
  200. function the_author_ID() {
  201. echo get_the_author_id();
  202. }
  203. /**
  204. * Retrieve the email of the author of the current post.
  205. *
  206. * @since 1.5
  207. * @uses $authordata The current author's DB object.
  208. * @return string The author's username.
  209. */
  210. function get_the_author_email() {
  211. global $authordata;
  212. return $authordata->user_email;
  213. }
  214. /**
  215. * Display the email of the author of the current post.
  216. *
  217. * @link http://codex.wordpress.org/Template_Tags/the_author_email
  218. * @since 0.71
  219. * @uses get_the_author_email()
  220. */
  221. function the_author_email() {
  222. echo apply_filters('the_author_email', get_the_author_email() );
  223. }
  224. /**
  225. * Retrieve the URL to the home page of the author of the current post.
  226. *
  227. * @since 1.5
  228. * @uses $authordata The current author's DB object.
  229. * @return string The URL to the author's page.
  230. */
  231. function get_the_author_url() {
  232. global $authordata;
  233. if ( 'http://' == $authordata->user_url )
  234. return '';
  235. return $authordata->user_url;
  236. }
  237. /**
  238. * Display the URL to the home page of the author of the current post.
  239. *
  240. * @link http://codex.wordpress.org/Template_Tags/the_author_url
  241. * @since 0.71
  242. * @uses get_the_author_url()
  243. */
  244. function the_author_url() {
  245. echo get_the_author_url();
  246. }
  247. /**
  248. * Display either author's link or author's name.
  249. *
  250. * If the author has a home page set, echo an HTML link, otherwise just echo the
  251. * author's name.
  252. *
  253. * @link http://codex.wordpress.org/Template_Tags/the_author_link
  254. * @since 2.1
  255. * @uses get_the_author_url()
  256. * @uses the_author()
  257. */
  258. function the_author_link() {
  259. if (get_the_author_url()) {
  260. echo '<a href="' . get_the_author_url() . '" title="' . sprintf(__("Visit %s's website"), get_the_author()) . '" rel="external">' . get_the_author() . '</a>';
  261. } else {
  262. the_author();
  263. }
  264. }
  265. /**
  266. * Retrieve the ICQ number of the author of the current post.
  267. *
  268. * @since 1.5
  269. * @uses $authordata The current author's DB object.
  270. * @return string The author's ICQ number.
  271. */
  272. function get_the_author_icq() {
  273. global $authordata;
  274. return $authordata->icq;
  275. }
  276. /**
  277. * Display the ICQ number of the author of the current post.
  278. *
  279. * @link http://codex.wordpress.org/Template_Tags/the_author_icq
  280. * @since 0.71
  281. * @see get_the_author_icq()
  282. */
  283. function the_author_icq() {
  284. echo get_the_author_icq();
  285. }
  286. /**
  287. * Retrieve the AIM name of the author of the current post.
  288. *
  289. * @since 1.5
  290. * @uses $authordata The current author's DB object.
  291. * @return string The author's AIM name.
  292. */
  293. function get_the_author_aim() {
  294. global $authordata;
  295. return str_replace(' ', '+', $authordata->aim);
  296. }
  297. /**
  298. * Display the AIM name of the author of the current post.
  299. *
  300. * @link http://codex.wordpress.org/Template_Tags/the_author_aim
  301. * @since 0.71
  302. * @see get_the_author_aim()
  303. */
  304. function the_author_aim() {
  305. echo get_the_author_aim();
  306. }
  307. /**
  308. * Retrieve the Yahoo! IM name of the author of the current post.
  309. *
  310. * @since 1.5
  311. * @uses $authordata The current author's DB object.
  312. * @return string The author's Yahoo! IM name.
  313. */
  314. function get_the_author_yim() {
  315. global $authordata;
  316. return $authordata->yim;
  317. }
  318. /**
  319. * Display the Yahoo! IM name of the author of the current post.
  320. *
  321. * @link http://codex.wordpress.org/Template_Tags/the_author_yim
  322. * @since 0.71
  323. * @see get_the_author_yim()
  324. */
  325. function the_author_yim() {
  326. echo get_the_author_yim();
  327. }
  328. /**
  329. * Retrieve the MSN address of the author of the current post.
  330. *
  331. * @since 1.5
  332. * @uses $authordata The current author's DB object.
  333. * @return string The author's MSN address.
  334. */
  335. function get_the_author_msn() {
  336. global $authordata;
  337. return $authordata->msn;
  338. }
  339. /**
  340. * Display the MSN address of the author of the current post.
  341. *
  342. * @link http://codex.wordpress.org/Template_Tags/the_author_msn
  343. * @since 0.71
  344. * @see get_the_author_msn()
  345. */
  346. function the_author_msn() {
  347. echo get_the_author_msn();
  348. }
  349. /**
  350. * Retrieve the number of posts by the author of the current post.
  351. *
  352. * @since 1.5
  353. * @uses $post The current post in the Loop's DB object.
  354. * @uses get_usernumposts()
  355. * @return int The number of posts by the author.
  356. */
  357. function get_the_author_posts() {
  358. global $post;
  359. return get_usernumposts($post->post_author);
  360. }
  361. /**
  362. * Display the number of posts by the author of the current post.
  363. *
  364. * @link http://codex.wordpress.org/Template_Tags/the_author_posts
  365. * @since 0.71
  366. * @uses get_the_author_posts() Echos returned value from function.
  367. */
  368. function the_author_posts() {
  369. echo get_the_author_posts();
  370. }
  371. /**
  372. * Display an HTML link to the author page of the author of the current post.
  373. *
  374. * Does just echo get_author_posts_url() function, like the others do. The
  375. * reason for this, is that another function is used to help in printing the
  376. * link to the author's posts.
  377. *
  378. * @link http://codex.wordpress.org/Template_Tags/the_author_posts_link
  379. * @since 1.2.0
  380. * @uses $authordata The current author's DB object.
  381. * @uses get_author_posts_url()
  382. * @uses get_the_author()
  383. * @param string $deprecated Deprecated.
  384. */
  385. function the_author_posts_link($deprecated = '') {
  386. global $authordata;
  387. printf(
  388. '<a href="%1$s" title="%2$s">%3$s</a>',
  389. get_author_posts_url( $authordata->ID, $authordata->user_nicename ),
  390. sprintf( __( 'Posts by %s' ), attribute_escape( get_the_author() ) ),
  391. get_the_author()
  392. );
  393. }
  394. /**
  395. * Retrieve the URL to the author page of the author of the current post.
  396. *
  397. * @since 2.1.0
  398. * @uses $wp_rewrite WP_Rewrite
  399. * @return string The URL to the author's page.
  400. */
  401. function get_author_posts_url($author_id, $author_nicename = '') {
  402. global $wp_rewrite;
  403. $auth_ID = (int) $author_id;
  404. $link = $wp_rewrite->get_author_permastruct();
  405. if ( empty($link) ) {
  406. $file = get_option('home') . '/';
  407. $link = $file . '?author=' . $auth_ID;
  408. } else {
  409. if ( '' == $author_nicename ) {
  410. $user = get_userdata($author_id);
  411. if ( !empty($user->user_nicename) )
  412. $author_nicename = $user->user_nicename;
  413. }
  414. $link = str_replace('%author%', $author_nicename, $link);
  415. $link = get_option('home') . trailingslashit($link);
  416. }
  417. $link = apply_filters('author_link', $link, $author_id, $author_nicename);
  418. return $link;
  419. }
  420. /**
  421. * Retrieve the specified author's preferred display name.
  422. *
  423. * @since 1.0.0
  424. * @param int $auth_id The ID of the author.
  425. * @return string The author's display name.
  426. */
  427. function get_author_name( $auth_id ) {
  428. $authordata = get_userdata( $auth_id );
  429. return $authordata->display_name;
  430. }
  431. /**
  432. * List all the authors of the blog, with several options available.
  433. *
  434. * <ul>
  435. * <li>optioncount (boolean) (false): Show the count in parenthesis next to the
  436. * author's name.</li>
  437. * <li>exclude_admin (boolean) (true): Exclude the 'admin' user that is
  438. * installed bydefault.</li>
  439. * <li>show_fullname (boolean) (false): Show their full names.</li>
  440. * <li>hide_empty (boolean) (true): Don't show authors without any posts.</li>
  441. * <li>feed (string) (''): If isn't empty, show links to author's feeds.</li>
  442. * <li>feed_image (string) (''): If isn't empty, use this image to link to
  443. * feeds.</li>
  444. * <li>echo (boolean) (true): Set to false to return the output, instead of
  445. * echoing.</li>
  446. * <li>style (string) ('list'): Whether to display list of authors in list form
  447. * or as a string.</li>
  448. * <li>html (bool) (true): Whether to list the items in html for or plaintext.
  449. * </li>
  450. * </ul>
  451. *
  452. * @link http://codex.wordpress.org/Template_Tags/wp_list_authors
  453. * @since 1.2.0
  454. * @param array $args The argument array.
  455. * @return null|string The output, if echo is set to false.
  456. */
  457. function wp_list_authors($args = '') {
  458. global $wpdb;
  459. $defaults = array(
  460. 'optioncount' => false, 'exclude_admin' => true,
  461. 'show_fullname' => false, 'hide_empty' => true,
  462. 'feed' => '', 'feed_image' => '', 'feed_type' => '', 'echo' => true,
  463. 'style' => 'list', 'html' => true
  464. );
  465. $r = wp_parse_args( $args, $defaults );
  466. extract($r, EXTR_SKIP);
  467. $return = '';
  468. /** @todo Move select to get_authors(). */
  469. $authors = $wpdb->get_results("SELECT ID, user_nicename from $wpdb->users " . ($exclude_admin ? "WHERE user_login <> 'admin' " : '') . "ORDER BY display_name");
  470. $author_count = array();
  471. 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) {
  472. $author_count[$row->post_author] = $row->count;
  473. }
  474. foreach ( (array) $authors as $author ) {
  475. $link = '';
  476. $author = get_userdata( $author->ID );
  477. $posts = (isset($author_count[$author->ID])) ? $author_count[$author->ID] : 0;
  478. $name = $author->display_name;
  479. if ( $show_fullname && ($author->first_name != '' && $author->last_name != '') )
  480. $name = "$author->first_name $author->last_name";
  481. if( !$html ) {
  482. if ( $posts == 0 ) {
  483. if ( ! $hide_empty )
  484. $return .= $name . ', ';
  485. } else
  486. $return .= $name . ', ';
  487. // No need to go further to process HTML.
  488. continue;
  489. }
  490. if ( !($posts == 0 && $hide_empty) && 'list' == $style )
  491. $return .= '<li>';
  492. if ( $posts == 0 ) {
  493. if ( ! $hide_empty )
  494. $link = $name;
  495. } else {
  496. $link = '<a href="' . get_author_posts_url($author->ID, $author->user_nicename) . '" title="' . sprintf(__("Posts by %s"), attribute_escape($author->display_name)) . '">' . $name . '</a>';
  497. if ( (! empty($feed_image)) || (! empty($feed)) ) {
  498. $link .= ' ';
  499. if (empty($feed_image))
  500. $link .= '(';
  501. $link .= '<a href="' . get_author_feed_link($author->ID) . '"';
  502. if ( !empty($feed) ) {
  503. $title = ' title="' . $feed . '"';
  504. $alt = ' alt="' . $feed . '"';
  505. $name = $feed;
  506. $link .= $title;
  507. }
  508. $link .= '>';
  509. if ( !empty($feed_image) )
  510. $link .= "<img src=\"$feed_image\" style=\"border: none;\"$alt$title" . ' />';
  511. else
  512. $link .= $name;
  513. $link .= '</a>';
  514. if ( empty($feed_image) )
  515. $link .= ')';
  516. }
  517. if ( $optioncount )
  518. $link .= ' ('. $posts . ')';
  519. }
  520. if ( !($posts == 0 && $hide_empty) && 'list' == $style )
  521. $return .= $link . '</li>';
  522. else if ( ! $hide_empty )
  523. $return .= $link . ', ';
  524. }
  525. $return = trim($return, ', ');
  526. if ( ! $echo )
  527. return $return;
  528. echo $return;
  529. }
  530. ?>