author-template.php 16 KB

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