PageRenderTime 52ms CodeModel.GetById 22ms RepoModel.GetById 1ms app.codeStats 0ms

/wp-includes/author-template.php

https://gitlab.com/geyson/geyson
PHP | 477 lines | 182 code | 55 blank | 240 comment | 41 complexity | 016d84e5998fe4e0795e9a4f66d4c1d8 MD5 | raw file
Possible License(s): LGPL-2.1, GPL-2.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 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' );
  26. /**
  27. * Filter 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, backwards 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' );
  57. if ( $deprecated_echo !== true )
  58. _deprecated_argument( __FUNCTION__, '1.5', __('Use <code>get_the_author()</code> instead if you do not want the value echoed.') );
  59. if ( $deprecated_echo )
  60. echo get_the_author();
  61. return get_the_author();
  62. }
  63. /**
  64. * Retrieve the author who last edited the current post.
  65. *
  66. * @since 2.8.0
  67. *
  68. * @return string|void The author's display name.
  69. */
  70. function get_the_modified_author() {
  71. if ( $last_id = get_post_meta( get_post()->ID, '_edit_last', true) ) {
  72. $last_user = get_userdata($last_id);
  73. /**
  74. * Filter the display name of the author who last edited the current post.
  75. *
  76. * @since 2.8.0
  77. *
  78. * @param string $last_user->display_name The author's display name.
  79. */
  80. return apply_filters('the_modified_author', $last_user->display_name);
  81. }
  82. }
  83. /**
  84. * Display the name of the author who last edited the current post,
  85. * if the author's ID is available.
  86. *
  87. * @since 2.8.0
  88. *
  89. * @see get_the_author()
  90. */
  91. function the_modified_author() {
  92. echo get_the_modified_author();
  93. }
  94. /**
  95. * Retrieve the requested data of the author of the current post.
  96. * @link https://codex.wordpress.org/Template_Tags/the_author_meta
  97. * @since 2.8.0
  98. *
  99. * @global object $authordata The current author's DB object.
  100. *
  101. * @param string $field selects the field of the users record.
  102. * @param int $user_id Optional. User ID.
  103. * @return string The author's field from the current author's DB object.
  104. */
  105. function get_the_author_meta( $field = '', $user_id = false ) {
  106. $original_user_id = $user_id;
  107. if ( ! $user_id ) {
  108. global $authordata;
  109. $user_id = isset( $authordata->ID ) ? $authordata->ID : 0;
  110. } else {
  111. $authordata = get_userdata( $user_id );
  112. }
  113. if ( in_array( $field, array( 'login', 'pass', 'nicename', 'email', 'url', 'registered', 'activation_key', 'status' ) ) )
  114. $field = 'user_' . $field;
  115. $value = isset( $authordata->$field ) ? $authordata->$field : '';
  116. /**
  117. * Filter the value of the requested user metadata.
  118. *
  119. * The filter name is dynamic and depends on the $field parameter of the function.
  120. *
  121. * @since 2.8.0
  122. * @since 4.3.0 The `$original_user_id` parameter was added.
  123. *
  124. * @param string $value The value of the metadata.
  125. * @param int $user_id The user ID for the value.
  126. * @param int|bool $original_user_id The original user ID, as passed to the function.
  127. */
  128. return apply_filters( 'get_the_author_' . $field, $value, $user_id, $original_user_id );
  129. }
  130. /**
  131. * Outputs the field from the user's DB object. Defaults to current post's author.
  132. *
  133. * @link https://codex.wordpress.org/Template_Tags/the_author_meta
  134. *
  135. * @since 2.8.0
  136. *
  137. * @param string $field selects the field of the users record.
  138. * @param int $user_id Optional. User ID.
  139. */
  140. function the_author_meta( $field = '', $user_id = false ) {
  141. $author_meta = get_the_author_meta( $field, $user_id );
  142. /**
  143. * The value of the requested user metadata.
  144. *
  145. * The filter name is dynamic and depends on the $field parameter of the function.
  146. *
  147. * @since 2.8.0
  148. *
  149. * @param string $author_meta The value of the metadata.
  150. * @param int $user_id The user ID.
  151. */
  152. echo apply_filters( 'the_author_' . $field, $author_meta, $user_id );
  153. }
  154. /**
  155. * Retrieve either author's link or author's name.
  156. *
  157. * If the author has a home page set, return an HTML link, otherwise just return the
  158. * author's name.
  159. *
  160. * @return string|null An HTML link if the author's url exist in user meta,
  161. * else the result of get_the_author().
  162. */
  163. function get_the_author_link() {
  164. if ( get_the_author_meta('url') ) {
  165. return '<a href="' . esc_url( get_the_author_meta('url') ) . '" title="' . esc_attr( sprintf(__("Visit %s&#8217;s website"), get_the_author()) ) . '" rel="author external">' . get_the_author() . '</a>';
  166. } else {
  167. return get_the_author();
  168. }
  169. }
  170. /**
  171. * Display either author's link or author's name.
  172. *
  173. * If the author has a home page set, echo an HTML link, otherwise just echo the
  174. * author's name.
  175. *
  176. * @link https://codex.wordpress.org/Template_Tags/the_author_link
  177. *
  178. * @since 2.1.0
  179. */
  180. function the_author_link() {
  181. echo get_the_author_link();
  182. }
  183. /**
  184. * Retrieve the number of posts by the author of the current post.
  185. *
  186. * @since 1.5.0
  187. *
  188. * @return int The number of posts by the author.
  189. */
  190. function get_the_author_posts() {
  191. $post = get_post();
  192. if ( ! $post ) {
  193. return 0;
  194. }
  195. return count_user_posts( $post->post_author, $post->post_type );
  196. }
  197. /**
  198. * Display the number of posts by the author of the current post.
  199. *
  200. * @link https://codex.wordpress.org/Template_Tags/the_author_posts
  201. * @since 0.71
  202. */
  203. function the_author_posts() {
  204. echo get_the_author_posts();
  205. }
  206. /**
  207. * Display an HTML link to the author page of the author of the current post.
  208. *
  209. * Does just echo get_author_posts_url() function, like the others do. The
  210. * reason for this, is that another function is used to help in printing the
  211. * link to the author's posts.
  212. *
  213. * @link https://codex.wordpress.org/Template_Tags/the_author_posts_link
  214. * @since 1.2.0
  215. *
  216. * @global object $authordata The current author's DB object.
  217. *
  218. * @param string $deprecated Deprecated.
  219. */
  220. function the_author_posts_link($deprecated = '') {
  221. if ( !empty( $deprecated ) )
  222. _deprecated_argument( __FUNCTION__, '2.1' );
  223. global $authordata;
  224. if ( ! is_object( $authordata ) ) {
  225. return;
  226. }
  227. $link = sprintf(
  228. '<a href="%1$s" title="%2$s" rel="author">%3$s</a>',
  229. esc_url( get_author_posts_url( $authordata->ID, $authordata->user_nicename ) ),
  230. esc_attr( sprintf( __( 'Posts by %s' ), get_the_author() ) ),
  231. get_the_author()
  232. );
  233. /**
  234. * Filter the link to the author page of the author of the current post.
  235. *
  236. * @since 2.9.0
  237. *
  238. * @param string $link HTML link.
  239. */
  240. echo apply_filters( 'the_author_posts_link', $link );
  241. }
  242. /**
  243. * Retrieve the URL to the author page for the user with the ID provided.
  244. *
  245. * @since 2.1.0
  246. *
  247. * @global WP_Rewrite $wp_rewrite
  248. *
  249. * @return string The URL to the author's page.
  250. */
  251. function get_author_posts_url($author_id, $author_nicename = '') {
  252. global $wp_rewrite;
  253. $auth_ID = (int) $author_id;
  254. $link = $wp_rewrite->get_author_permastruct();
  255. if ( empty($link) ) {
  256. $file = home_url( '/' );
  257. $link = $file . '?author=' . $auth_ID;
  258. } else {
  259. if ( '' == $author_nicename ) {
  260. $user = get_userdata($author_id);
  261. if ( !empty($user->user_nicename) )
  262. $author_nicename = $user->user_nicename;
  263. }
  264. $link = str_replace('%author%', $author_nicename, $link);
  265. $link = home_url( user_trailingslashit( $link ) );
  266. }
  267. /**
  268. * Filter the URL to the author's page.
  269. *
  270. * @since 2.1.0
  271. *
  272. * @param string $link The URL to the author's page.
  273. * @param int $author_id The author's id.
  274. * @param string $author_nicename The author's nice name.
  275. */
  276. $link = apply_filters( 'author_link', $link, $author_id, $author_nicename );
  277. return $link;
  278. }
  279. /**
  280. * List all the authors of the blog, with several options available.
  281. *
  282. * @link https://codex.wordpress.org/Template_Tags/wp_list_authors
  283. *
  284. * @since 1.2.0
  285. *
  286. * @global wpdb $wpdb
  287. *
  288. * @param string|array $args {
  289. * Optional. Array or string of default arguments.
  290. *
  291. * @type string $orderby How to sort the authors. Accepts 'nicename', 'email', 'url', 'registered',
  292. * 'user_nicename', 'user_email', 'user_url', 'user_registered', 'name',
  293. * 'display_name', 'post_count', 'ID', 'meta_value', 'user_login'. Default 'name'.
  294. * @type string $order Sorting direction for $orderby. Accepts 'ASC', 'DESC'. Default 'ASC'.
  295. * @type int $number Maximum authors to return or display. Default empty (all authors).
  296. * @type bool $optioncount Show the count in parenthesis next to the author's name. Default false.
  297. * @type bool $exclude_admin Whether to exclude the 'admin' account, if it exists. Default false.
  298. * @type bool $show_fullname Whether to show the author's full name. Default false.
  299. * @type bool $hide_empty Whether to hide any authors with no posts. Default true.
  300. * @type string $feed If not empty, show a link to the author's feed and use this text as the alt
  301. * parameter of the link. Default empty.
  302. * @type string $feed_image If not empty, show a link to the author's feed and use this image URL as
  303. * clickable anchor. Default empty.
  304. * @type string $feed_type The feed type to link to, such as 'rss2'. Defaults to default feed type.
  305. * @type bool $echo Whether to output the result or instead return it. Default true.
  306. * @type string $style If 'list', each author is wrapped in an `<li>` element, otherwise the authors
  307. * will be separated by commas.
  308. * @type bool $html Whether to list the items in HTML form or plaintext. Default true.
  309. * @type string $exclude An array, comma-, or space-separated list of author IDs to exclude. Default empty.
  310. * @type string $exclude An array, comma-, or space-separated list of author IDs to include. Default empty.
  311. * }
  312. * @return string|void The output, if echo is set to false.
  313. */
  314. function wp_list_authors( $args = '' ) {
  315. global $wpdb;
  316. $defaults = array(
  317. 'orderby' => 'name', 'order' => 'ASC', 'number' => '',
  318. 'optioncount' => false, 'exclude_admin' => true,
  319. 'show_fullname' => false, 'hide_empty' => true,
  320. 'feed' => '', 'feed_image' => '', 'feed_type' => '', 'echo' => true,
  321. 'style' => 'list', 'html' => true, 'exclude' => '', 'include' => ''
  322. );
  323. $args = wp_parse_args( $args, $defaults );
  324. $return = '';
  325. $query_args = wp_array_slice_assoc( $args, array( 'orderby', 'order', 'number', 'exclude', 'include' ) );
  326. $query_args['fields'] = 'ids';
  327. $authors = get_users( $query_args );
  328. $author_count = array();
  329. 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 ) {
  330. $author_count[$row->post_author] = $row->count;
  331. }
  332. foreach ( $authors as $author_id ) {
  333. $author = get_userdata( $author_id );
  334. if ( $args['exclude_admin'] && 'admin' == $author->display_name ) {
  335. continue;
  336. }
  337. $posts = isset( $author_count[$author->ID] ) ? $author_count[$author->ID] : 0;
  338. if ( ! $posts && $args['hide_empty'] ) {
  339. continue;
  340. }
  341. if ( $args['show_fullname'] && $author->first_name && $author->last_name ) {
  342. $name = "$author->first_name $author->last_name";
  343. } else {
  344. $name = $author->display_name;
  345. }
  346. if ( ! $args['html'] ) {
  347. $return .= $name . ', ';
  348. continue; // No need to go further to process HTML.
  349. }
  350. if ( 'list' == $args['style'] ) {
  351. $return .= '<li>';
  352. }
  353. $link = '<a href="' . get_author_posts_url( $author->ID, $author->user_nicename ) . '" title="' . esc_attr( sprintf(__("Posts by %s"), $author->display_name) ) . '">' . $name . '</a>';
  354. if ( ! empty( $args['feed_image'] ) || ! empty( $args['feed'] ) ) {
  355. $link .= ' ';
  356. if ( empty( $args['feed_image'] ) ) {
  357. $link .= '(';
  358. }
  359. $link .= '<a href="' . get_author_feed_link( $author->ID, $args['feed_type'] ) . '"';
  360. $alt = '';
  361. if ( ! empty( $args['feed'] ) ) {
  362. $alt = ' alt="' . esc_attr( $args['feed'] ) . '"';
  363. $name = $args['feed'];
  364. }
  365. $link .= '>';
  366. if ( ! empty( $args['feed_image'] ) ) {
  367. $link .= '<img src="' . esc_url( $args['feed_image'] ) . '" style="border: none;"' . $alt . ' />';
  368. } else {
  369. $link .= $name;
  370. }
  371. $link .= '</a>';
  372. if ( empty( $args['feed_image'] ) ) {
  373. $link .= ')';
  374. }
  375. }
  376. if ( $args['optioncount'] ) {
  377. $link .= ' ('. $posts . ')';
  378. }
  379. $return .= $link;
  380. $return .= ( 'list' == $args['style'] ) ? '</li>' : ', ';
  381. }
  382. $return = rtrim( $return, ', ' );
  383. if ( ! $args['echo'] ) {
  384. return $return;
  385. }
  386. echo $return;
  387. }
  388. /**
  389. * Does this site have more than one author
  390. *
  391. * Checks to see if more than one author has published posts.
  392. *
  393. * @since 3.2.0
  394. *
  395. * @global wpdb $wpdb
  396. *
  397. * @return bool Whether or not we have more than one author
  398. */
  399. function is_multi_author() {
  400. global $wpdb;
  401. if ( false === ( $is_multi_author = get_transient( 'is_multi_author' ) ) ) {
  402. $rows = (array) $wpdb->get_col("SELECT DISTINCT post_author FROM $wpdb->posts WHERE post_type = 'post' AND post_status = 'publish' LIMIT 2");
  403. $is_multi_author = 1 < count( $rows ) ? 1 : 0;
  404. set_transient( 'is_multi_author', $is_multi_author );
  405. }
  406. /**
  407. * Filter whether the site has more than one author with published posts.
  408. *
  409. * @since 3.2.0
  410. *
  411. * @param bool $is_multi_author Whether $is_multi_author should evaluate as true.
  412. */
  413. return apply_filters( 'is_multi_author', (bool) $is_multi_author );
  414. }
  415. /**
  416. * Helper function to clear the cache for number of authors.
  417. *
  418. * @private
  419. */
  420. function __clear_multi_author_cache() {
  421. delete_transient( 'is_multi_author' );
  422. }