author-template.php 18 KB

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