PageRenderTime 47ms CodeModel.GetById 16ms RepoModel.GetById 1ms app.codeStats 0ms

/wp-includes/author-template.php

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