PageRenderTime 50ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

/wp-includes/author-template.php

https://bitbucket.org/aqge/deptandashboard
PHP | 401 lines | 179 code | 54 blank | 168 comment | 38 complexity | 684e42660fd6bf25d80d0e09108cad50 MD5 | raw file
Possible License(s): AGPL-1.0, GPL-2.0, LGPL-2.1
  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
  16. * @uses $authordata The current author's DB object.
  17. * @uses apply_filters() Calls 'the_author' hook on the author display name.
  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. return apply_filters('the_author', is_object($authordata) ? $authordata->display_name : null);
  27. }
  28. /**
  29. * Display the name of the author of the current post.
  30. *
  31. * The behavior of this function is based off of old functionality predating
  32. * get_the_author(). This function is not deprecated, but is designed to echo
  33. * the value from get_the_author() and as an result of any old theme that might
  34. * still use the old behavior will also pass the value from get_the_author().
  35. *
  36. * The normal, expected behavior of this function is to echo the author and not
  37. * return it. However, backwards compatibility has to be maintained.
  38. *
  39. * @since 0.71
  40. * @see get_the_author()
  41. * @link http://codex.wordpress.org/Template_Tags/the_author
  42. *
  43. * @param string $deprecated Deprecated.
  44. * @param string $deprecated_echo Deprecated. Use get_the_author(). Echo the string or return it.
  45. * @return string The author's display name, from get_the_author().
  46. */
  47. function the_author( $deprecated = '', $deprecated_echo = true ) {
  48. if ( !empty( $deprecated ) )
  49. _deprecated_argument( __FUNCTION__, '2.1' );
  50. if ( $deprecated_echo !== true )
  51. _deprecated_argument( __FUNCTION__, '1.5', __('Use <code>get_the_author()</code> instead if you do not want the value echoed.') );
  52. if ( $deprecated_echo )
  53. echo get_the_author();
  54. return get_the_author();
  55. }
  56. /**
  57. * Retrieve the author who last edited the current post.
  58. *
  59. * @since 2.8
  60. * @uses $post The current post's DB object.
  61. * @uses get_post_meta() Retrieves the ID of the author who last edited the current post.
  62. * @uses get_userdata() Retrieves the author's DB object.
  63. * @uses apply_filters() Calls 'the_modified_author' hook on the author display name.
  64. * @return string The author's display name.
  65. */
  66. function get_the_modified_author() {
  67. global $post;
  68. if ( $last_id = get_post_meta($post->ID, '_edit_last', true) ) {
  69. $last_user = get_userdata($last_id);
  70. return apply_filters('the_modified_author', $last_user->display_name);
  71. }
  72. }
  73. /**
  74. * Display the name of the author who last edited the current post.
  75. *
  76. * @since 2.8
  77. * @see get_the_author()
  78. * @return string The author's display name, from get_the_modified_author().
  79. */
  80. function the_modified_author() {
  81. echo get_the_modified_author();
  82. }
  83. /**
  84. * Retrieve the requested data of the author of the current post.
  85. * @link http://codex.wordpress.org/Template_Tags/the_author_meta
  86. * @since 2.8.0
  87. * @uses $authordata The current author's DB object (if $user_id not specified).
  88. * @param string $field selects the field of the users record.
  89. * @param int $user_id Optional. User ID.
  90. * @return string The author's field from the current author's DB object.
  91. */
  92. function get_the_author_meta($field = '', $user_id = false) {
  93. if ( ! $user_id )
  94. global $authordata;
  95. else
  96. $authordata = get_userdata( $user_id );
  97. // Keys used as object vars cannot have dashes.
  98. $field = str_replace('-', '', $field);
  99. $field = strtolower($field);
  100. $user_field = "user_$field";
  101. if ( 'id' == $field )
  102. $value = isset($authordata->ID) ? (int)$authordata->ID : 0;
  103. elseif ( isset($authordata->$user_field) )
  104. $value = $authordata->$user_field;
  105. else
  106. $value = isset($authordata->$field) ? $authordata->$field : '';
  107. return apply_filters('get_the_author_' . $field, $value, $user_id);
  108. }
  109. /**
  110. * Retrieve the requested data of the author of the current post.
  111. * @link http://codex.wordpress.org/Template_Tags/the_author_meta
  112. * @since 2.8.0
  113. * @param string $field selects the field of the users record.
  114. * @param int $user_id Optional. User ID.
  115. * @echo string The author's field from the current author's DB object.
  116. */
  117. function the_author_meta($field = '', $user_id = false) {
  118. echo apply_filters('the_author_' . $field, get_the_author_meta($field, $user_id), $user_id);
  119. }
  120. /**
  121. * Retrieve either author's link or author's name.
  122. *
  123. * If the author has a home page set, return an HTML link, otherwise just return the
  124. * author's name.
  125. *
  126. * @uses get_the_author_meta()
  127. * @uses get_the_author()
  128. */
  129. function get_the_author_link() {
  130. if ( get_the_author_meta('url') ) {
  131. return '<a href="' . get_the_author_meta('url') . '" title="' . esc_attr( sprintf(__("Visit %s&#8217;s website"), get_the_author()) ) . '" rel="external">' . get_the_author() . '</a>';
  132. } else {
  133. return get_the_author();
  134. }
  135. }
  136. /**
  137. * Display either author's link or author's name.
  138. *
  139. * If the author has a home page set, echo an HTML link, otherwise just echo the
  140. * author's name.
  141. *
  142. * @link http://codex.wordpress.org/Template_Tags/the_author_link
  143. * @since 2.1
  144. * @uses get_the_author_link()
  145. */
  146. function the_author_link() {
  147. echo get_the_author_link();
  148. }
  149. /**
  150. * Retrieve the number of posts by the author of the current post.
  151. *
  152. * @since 1.5
  153. * @uses $post The current post in the Loop's DB object.
  154. * @uses count_user_posts()
  155. * @return int The number of posts by the author.
  156. */
  157. function get_the_author_posts() {
  158. global $post;
  159. return count_user_posts($post->post_author);
  160. }
  161. /**
  162. * Display the number of posts by the author of the current post.
  163. *
  164. * @link http://codex.wordpress.org/Template_Tags/the_author_posts
  165. * @since 0.71
  166. * @uses get_the_author_posts() Echoes returned value from function.
  167. */
  168. function the_author_posts() {
  169. echo get_the_author_posts();
  170. }
  171. /**
  172. * Display an HTML link to the author page of the author of the current post.
  173. *
  174. * Does just echo get_author_posts_url() function, like the others do. The
  175. * reason for this, is that another function is used to help in printing the
  176. * link to the author's posts.
  177. *
  178. * @link http://codex.wordpress.org/Template_Tags/the_author_posts_link
  179. * @since 1.2.0
  180. * @uses $authordata The current author's DB object.
  181. * @uses get_author_posts_url()
  182. * @uses get_the_author()
  183. * @param string $deprecated Deprecated.
  184. */
  185. function the_author_posts_link($deprecated = '') {
  186. if ( !empty( $deprecated ) )
  187. _deprecated_argument( __FUNCTION__, '2.1' );
  188. global $authordata;
  189. if ( !is_object( $authordata ) )
  190. return false;
  191. $link = sprintf(
  192. '<a href="%1$s" title="%2$s" rel="author">%3$s</a>',
  193. get_author_posts_url( $authordata->ID, $authordata->user_nicename ),
  194. esc_attr( sprintf( __( 'Posts by %s' ), get_the_author() ) ),
  195. get_the_author()
  196. );
  197. echo apply_filters( 'the_author_posts_link', $link );
  198. }
  199. /**
  200. * Retrieve the URL to the author page for the user with the ID provided.
  201. *
  202. * @since 2.1.0
  203. * @uses $wp_rewrite WP_Rewrite
  204. * @return string The URL to the author's page.
  205. */
  206. function get_author_posts_url($author_id, $author_nicename = '') {
  207. global $wp_rewrite;
  208. $auth_ID = (int) $author_id;
  209. $link = $wp_rewrite->get_author_permastruct();
  210. if ( empty($link) ) {
  211. $file = home_url( '/' );
  212. $link = $file . '?author=' . $auth_ID;
  213. } else {
  214. if ( '' == $author_nicename ) {
  215. $user = get_userdata($author_id);
  216. if ( !empty($user->user_nicename) )
  217. $author_nicename = $user->user_nicename;
  218. }
  219. $link = str_replace('%author%', $author_nicename, $link);
  220. $link = home_url( user_trailingslashit( $link ) );
  221. }
  222. $link = apply_filters('author_link', $link, $author_id, $author_nicename);
  223. return $link;
  224. }
  225. /**
  226. * List all the authors of the blog, with several options available.
  227. *
  228. * <ul>
  229. * <li>optioncount (boolean) (false): Show the count in parenthesis next to the
  230. * author's name.</li>
  231. * <li>exclude_admin (boolean) (true): Exclude the 'admin' user that is
  232. * installed bydefault.</li>
  233. * <li>show_fullname (boolean) (false): Show their full names.</li>
  234. * <li>hide_empty (boolean) (true): Don't show authors without any posts.</li>
  235. * <li>feed (string) (''): If isn't empty, show links to author's feeds.</li>
  236. * <li>feed_image (string) (''): If isn't empty, use this image to link to
  237. * feeds.</li>
  238. * <li>echo (boolean) (true): Set to false to return the output, instead of
  239. * echoing.</li>
  240. * <li>style (string) ('list'): Whether to display list of authors in list form
  241. * or as a string.</li>
  242. * <li>html (bool) (true): Whether to list the items in html form or plaintext.
  243. * </li>
  244. * </ul>
  245. *
  246. * @link http://codex.wordpress.org/Template_Tags/wp_list_authors
  247. * @since 1.2.0
  248. * @param array $args The argument array.
  249. * @return null|string The output, if echo is set to false.
  250. */
  251. function wp_list_authors($args = '') {
  252. global $wpdb;
  253. $defaults = array(
  254. 'orderby' => 'name', 'order' => 'ASC', 'number' => '',
  255. 'optioncount' => false, 'exclude_admin' => true,
  256. 'show_fullname' => false, 'hide_empty' => true,
  257. 'feed' => '', 'feed_image' => '', 'feed_type' => '', 'echo' => true,
  258. 'style' => 'list', 'html' => true
  259. );
  260. $args = wp_parse_args( $args, $defaults );
  261. extract( $args, EXTR_SKIP );
  262. $return = '';
  263. $query_args = wp_array_slice_assoc( $args, array( 'orderby', 'order', 'number' ) );
  264. $query_args['fields'] = 'ids';
  265. $authors = get_users( $query_args );
  266. $author_count = array();
  267. 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 )
  268. $author_count[$row->post_author] = $row->count;
  269. foreach ( $authors as $author_id ) {
  270. $author = get_userdata( $author_id );
  271. if ( $exclude_admin && 'admin' == $author->display_name )
  272. continue;
  273. $posts = isset( $author_count[$author->ID] ) ? $author_count[$author->ID] : 0;
  274. if ( !$posts && $hide_empty )
  275. continue;
  276. $link = '';
  277. if ( $show_fullname && $author->first_name && $author->last_name )
  278. $name = "$author->first_name $author->last_name";
  279. else
  280. $name = $author->display_name;
  281. if ( !$html ) {
  282. $return .= $name . ', ';
  283. continue; // No need to go further to process HTML.
  284. }
  285. if ( 'list' == $style ) {
  286. $return .= '<li>';
  287. }
  288. $link = '<a href="' . get_author_posts_url( $author->ID, $author->user_nicename ) . '" title="' . esc_attr( sprintf(__("Posts by %s"), $author->display_name) ) . '">' . $name . '</a>';
  289. if ( !empty( $feed_image ) || !empty( $feed ) ) {
  290. $link .= ' ';
  291. if ( empty( $feed_image ) ) {
  292. $link .= '(';
  293. }
  294. $link .= '<a href="' . get_author_feed_link( $author->ID ) . '"';
  295. $alt = $title = '';
  296. if ( !empty( $feed ) ) {
  297. $title = ' title="' . esc_attr( $feed ) . '"';
  298. $alt = ' alt="' . esc_attr( $feed ) . '"';
  299. $name = $feed;
  300. $link .= $title;
  301. }
  302. $link .= '>';
  303. if ( !empty( $feed_image ) )
  304. $link .= '<img src="' . esc_url( $feed_image ) . '" style="border: none;"' . $alt . $title . ' />';
  305. else
  306. $link .= $name;
  307. $link .= '</a>';
  308. if ( empty( $feed_image ) )
  309. $link .= ')';
  310. }
  311. if ( $optioncount )
  312. $link .= ' ('. $posts . ')';
  313. $return .= $link;
  314. $return .= ( 'list' == $style ) ? '</li>' : ', ';
  315. }
  316. $return = rtrim($return, ', ');
  317. if ( !$echo )
  318. return $return;
  319. echo $return;
  320. }
  321. /**
  322. * Does this site have more than one author
  323. *
  324. * Checks to see if more than one author has published posts.
  325. *
  326. * @since 3.2.0
  327. * @return bool Whether or not we have more than one author
  328. */
  329. function is_multi_author() {
  330. global $wpdb;
  331. if ( false === ( $is_multi_author = wp_cache_get('is_multi_author', 'posts') ) ) {
  332. $rows = (array) $wpdb->get_col("SELECT DISTINCT post_author FROM $wpdb->posts WHERE post_type = 'post' AND post_status = 'publish' LIMIT 2");
  333. $is_multi_author = 1 < count( $rows ) ? 1 : 0;
  334. wp_cache_set('is_multi_author', $is_multi_author, 'posts');
  335. }
  336. return apply_filters( 'is_multi_author', (bool) $is_multi_author );
  337. }
  338. /**
  339. * Helper function to clear the cache for number of authors.
  340. *
  341. * @private
  342. */
  343. function __clear_multi_author_cache() {
  344. wp_cache_delete('is_multi_author', 'posts');
  345. }
  346. add_action('transition_post_status', '__clear_multi_author_cache');
  347. ?>