Нет описания

author-template.php 17KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573
  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 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. * Display 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. * Retrieve the author who last edited the current post.
  78. *
  79. * @since 2.8.0
  80. *
  81. * @return string|void The author's display name.
  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.
  93. */
  94. return apply_filters( 'the_modified_author', $last_user->display_name );
  95. }
  96. }
  97. /**
  98. * Display 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. * 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. * Retrieve 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. * @return string|null An HTML link if the author's url exist in user meta,
  209. * else the result of get_the_author().
  210. */
  211. function get_the_author_link() {
  212. if ( get_the_author_meta( 'url' ) ) {
  213. return sprintf(
  214. '<a href="%1$s" title="%2$s" rel="author external">%3$s</a>',
  215. esc_url( get_the_author_meta( 'url' ) ),
  216. /* translators: %s: Author's display name. */
  217. esc_attr( sprintf( __( 'Visit %s&#8217;s website' ), get_the_author() ) ),
  218. get_the_author()
  219. );
  220. } else {
  221. return get_the_author();
  222. }
  223. }
  224. /**
  225. * Display either author's link or author's name.
  226. *
  227. * If the author has a home page set, echo an HTML link, otherwise just echo the
  228. * author's name.
  229. *
  230. * @link https://developer.wordpress.org/reference/functions/the_author_link/
  231. *
  232. * @since 2.1.0
  233. */
  234. function the_author_link() {
  235. echo get_the_author_link();
  236. }
  237. /**
  238. * Retrieve the number of posts by the author of the current post.
  239. *
  240. * @since 1.5.0
  241. *
  242. * @return int The number of posts by the author.
  243. */
  244. function get_the_author_posts() {
  245. $post = get_post();
  246. if ( ! $post ) {
  247. return 0;
  248. }
  249. return count_user_posts( $post->post_author, $post->post_type );
  250. }
  251. /**
  252. * Display the number of posts by the author of the current post.
  253. *
  254. * @link https://developer.wordpress.org/reference/functions/the_author_posts/
  255. * @since 0.71
  256. */
  257. function the_author_posts() {
  258. echo get_the_author_posts();
  259. }
  260. /**
  261. * Retrieves an HTML link to the author page of the current post's author.
  262. *
  263. * Returns an HTML-formatted link using get_author_posts_url().
  264. *
  265. * @since 4.4.0
  266. *
  267. * @global WP_User $authordata The current author's data.
  268. *
  269. * @return string An HTML link to the author page, or an empty string if $authordata isn't defined.
  270. */
  271. function get_the_author_posts_link() {
  272. global $authordata;
  273. if ( ! is_object( $authordata ) ) {
  274. return '';
  275. }
  276. $link = sprintf(
  277. '<a href="%1$s" title="%2$s" rel="author">%3$s</a>',
  278. esc_url( get_author_posts_url( $authordata->ID, $authordata->user_nicename ) ),
  279. /* translators: %s: Author's display name. */
  280. esc_attr( sprintf( __( 'Posts by %s' ), get_the_author() ) ),
  281. get_the_author()
  282. );
  283. /**
  284. * Filters the link to the author page of the author of the current post.
  285. *
  286. * @since 2.9.0
  287. *
  288. * @param string $link HTML link.
  289. */
  290. return apply_filters( 'the_author_posts_link', $link );
  291. }
  292. /**
  293. * Displays an HTML link to the author page of the current post's author.
  294. *
  295. * @since 1.2.0
  296. * @since 4.4.0 Converted into a wrapper for get_the_author_posts_link()
  297. *
  298. * @param string $deprecated Unused.
  299. */
  300. function the_author_posts_link( $deprecated = '' ) {
  301. if ( ! empty( $deprecated ) ) {
  302. _deprecated_argument( __FUNCTION__, '2.1.0' );
  303. }
  304. echo get_the_author_posts_link();
  305. }
  306. /**
  307. * Retrieve the URL to the author page for the user with the ID provided.
  308. *
  309. * @since 2.1.0
  310. *
  311. * @global WP_Rewrite $wp_rewrite WordPress rewrite component.
  312. *
  313. * @param int $author_id Author ID.
  314. * @param string $author_nicename Optional. The author's nicename (slug). Default empty.
  315. * @return string The URL to the author's page.
  316. */
  317. function get_author_posts_url( $author_id, $author_nicename = '' ) {
  318. global $wp_rewrite;
  319. $auth_ID = (int) $author_id;
  320. $link = $wp_rewrite->get_author_permastruct();
  321. if ( empty( $link ) ) {
  322. $file = home_url( '/' );
  323. $link = $file . '?author=' . $auth_ID;
  324. } else {
  325. if ( '' === $author_nicename ) {
  326. $user = get_userdata( $author_id );
  327. if ( ! empty( $user->user_nicename ) ) {
  328. $author_nicename = $user->user_nicename;
  329. }
  330. }
  331. $link = str_replace( '%author%', $author_nicename, $link );
  332. $link = home_url( user_trailingslashit( $link ) );
  333. }
  334. /**
  335. * Filters the URL to the author's page.
  336. *
  337. * @since 2.1.0
  338. *
  339. * @param string $link The URL to the author's page.
  340. * @param int $author_id The author's ID.
  341. * @param string $author_nicename The author's nice name.
  342. */
  343. $link = apply_filters( 'author_link', $link, $author_id, $author_nicename );
  344. return $link;
  345. }
  346. /**
  347. * List all the authors of the site, with several options available.
  348. *
  349. * @link https://developer.wordpress.org/reference/functions/wp_list_authors/
  350. *
  351. * @since 1.2.0
  352. *
  353. * @global wpdb $wpdb WordPress database abstraction object.
  354. *
  355. * @param string|array $args {
  356. * Optional. Array or string of default arguments.
  357. *
  358. * @type string $orderby How to sort the authors. Accepts 'nicename', 'email', 'url', 'registered',
  359. * 'user_nicename', 'user_email', 'user_url', 'user_registered', 'name',
  360. * 'display_name', 'post_count', 'ID', 'meta_value', 'user_login'. Default 'name'.
  361. * @type string $order Sorting direction for $orderby. Accepts 'ASC', 'DESC'. Default 'ASC'.
  362. * @type int $number Maximum authors to return or display. Default empty (all authors).
  363. * @type bool $optioncount Show the count in parenthesis next to the author's name. Default false.
  364. * @type bool $exclude_admin Whether to exclude the 'admin' account, if it exists. Default true.
  365. * @type bool $show_fullname Whether to show the author's full name. Default false.
  366. * @type bool $hide_empty Whether to hide any authors with no posts. Default true.
  367. * @type string $feed If not empty, show a link to the author's feed and use this text as the alt
  368. * parameter of the link. Default empty.
  369. * @type string $feed_image If not empty, show a link to the author's feed and use this image URL as
  370. * clickable anchor. Default empty.
  371. * @type string $feed_type The feed type to link to. Possible values include 'rss2', 'atom'.
  372. * Default is the value of get_default_feed().
  373. * @type bool $echo Whether to output the result or instead return it. Default true.
  374. * @type string $style If 'list', each author is wrapped in an `<li>` element, otherwise the authors
  375. * will be separated by commas.
  376. * @type bool $html Whether to list the items in HTML form or plaintext. Default true.
  377. * @type int[]|string $exclude Array or comma/space-separated list of author IDs to exclude. Default empty.
  378. * @type int[]|string $include Array or comma/space-separated list of author IDs to include. Default empty.
  379. * }
  380. * @return void|string Void if 'echo' argument is true, list of authors if 'echo' is false.
  381. */
  382. function wp_list_authors( $args = '' ) {
  383. global $wpdb;
  384. $defaults = array(
  385. 'orderby' => 'name',
  386. 'order' => 'ASC',
  387. 'number' => '',
  388. 'optioncount' => false,
  389. 'exclude_admin' => true,
  390. 'show_fullname' => false,
  391. 'hide_empty' => true,
  392. 'feed' => '',
  393. 'feed_image' => '',
  394. 'feed_type' => '',
  395. 'echo' => true,
  396. 'style' => 'list',
  397. 'html' => true,
  398. 'exclude' => '',
  399. 'include' => '',
  400. );
  401. $args = wp_parse_args( $args, $defaults );
  402. $return = '';
  403. $query_args = wp_array_slice_assoc( $args, array( 'orderby', 'order', 'number', 'exclude', 'include' ) );
  404. $query_args['fields'] = 'ids';
  405. $authors = get_users( $query_args );
  406. $author_count = array();
  407. 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 ) {
  408. $author_count[ $row->post_author ] = $row->count;
  409. }
  410. foreach ( $authors as $author_id ) {
  411. $posts = isset( $author_count[ $author_id ] ) ? $author_count[ $author_id ] : 0;
  412. if ( ! $posts && $args['hide_empty'] ) {
  413. continue;
  414. }
  415. $author = get_userdata( $author_id );
  416. if ( $args['exclude_admin'] && 'admin' === $author->display_name ) {
  417. continue;
  418. }
  419. if ( $args['show_fullname'] && $author->first_name && $author->last_name ) {
  420. $name = "$author->first_name $author->last_name";
  421. } else {
  422. $name = $author->display_name;
  423. }
  424. if ( ! $args['html'] ) {
  425. $return .= $name . ', ';
  426. continue; // No need to go further to process HTML.
  427. }
  428. if ( 'list' === $args['style'] ) {
  429. $return .= '<li>';
  430. }
  431. $link = sprintf(
  432. '<a href="%1$s" title="%2$s">%3$s</a>',
  433. esc_url( get_author_posts_url( $author->ID, $author->user_nicename ) ),
  434. /* translators: %s: Author's display name. */
  435. esc_attr( sprintf( __( 'Posts by %s' ), $author->display_name ) ),
  436. $name
  437. );
  438. if ( ! empty( $args['feed_image'] ) || ! empty( $args['feed'] ) ) {
  439. $link .= ' ';
  440. if ( empty( $args['feed_image'] ) ) {
  441. $link .= '(';
  442. }
  443. $link .= '<a href="' . get_author_feed_link( $author->ID, $args['feed_type'] ) . '"';
  444. $alt = '';
  445. if ( ! empty( $args['feed'] ) ) {
  446. $alt = ' alt="' . esc_attr( $args['feed'] ) . '"';
  447. $name = $args['feed'];
  448. }
  449. $link .= '>';
  450. if ( ! empty( $args['feed_image'] ) ) {
  451. $link .= '<img src="' . esc_url( $args['feed_image'] ) . '" style="border: none;"' . $alt . ' />';
  452. } else {
  453. $link .= $name;
  454. }
  455. $link .= '</a>';
  456. if ( empty( $args['feed_image'] ) ) {
  457. $link .= ')';
  458. }
  459. }
  460. if ( $args['optioncount'] ) {
  461. $link .= ' (' . $posts . ')';
  462. }
  463. $return .= $link;
  464. $return .= ( 'list' === $args['style'] ) ? '</li>' : ', ';
  465. }
  466. $return = rtrim( $return, ', ' );
  467. if ( $args['echo'] ) {
  468. echo $return;
  469. } else {
  470. return $return;
  471. }
  472. }
  473. /**
  474. * Determines whether this site has more than one author.
  475. *
  476. * Checks to see if more than one author has published posts.
  477. *
  478. * For more information on this and similar theme functions, check out
  479. * the {@link https://developer.wordpress.org/themes/basics/conditional-tags/
  480. * Conditional Tags} article in the Theme Developer Handbook.
  481. *
  482. * @since 3.2.0
  483. *
  484. * @global wpdb $wpdb WordPress database abstraction object.
  485. *
  486. * @return bool Whether or not we have more than one author
  487. */
  488. function is_multi_author() {
  489. global $wpdb;
  490. $is_multi_author = get_transient( 'is_multi_author' );
  491. if ( false === $is_multi_author ) {
  492. $rows = (array) $wpdb->get_col( "SELECT DISTINCT post_author FROM $wpdb->posts WHERE post_type = 'post' AND post_status = 'publish' LIMIT 2" );
  493. $is_multi_author = 1 < count( $rows ) ? 1 : 0;
  494. set_transient( 'is_multi_author', $is_multi_author );
  495. }
  496. /**
  497. * Filters whether the site has more than one author with published posts.
  498. *
  499. * @since 3.2.0
  500. *
  501. * @param bool $is_multi_author Whether $is_multi_author should evaluate as true.
  502. */
  503. return apply_filters( 'is_multi_author', (bool) $is_multi_author );
  504. }
  505. /**
  506. * Helper function to clear the cache for number of authors.
  507. *
  508. * @since 3.2.0
  509. * @access private
  510. */
  511. function __clear_multi_author_cache() { // phpcs:ignore WordPress.NamingConventions.ValidFunctionName.FunctionDoubleUnderscore,PHPCompatibility.FunctionNameRestrictions.ReservedFunctionNames.FunctionDoubleUnderscore
  512. delete_transient( 'is_multi_author' );
  513. }