PageRenderTime 42ms CodeModel.GetById 13ms RepoModel.GetById 1ms app.codeStats 0ms

/wp-includes/author-template.php

https://bitbucket.org/matyhaty/senses-avj-printclub
PHP | 394 lines | 174 code | 53 blank | 167 comment | 38 complexity | 279e83571b83017dbcdd3b7bb7522b83 MD5 | raw file
Possible License(s): AGPL-1.0, 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 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. $user_id = isset( $authordata->ID ) ? $authordata->ID : 0;
  96. } else {
  97. $authordata = get_userdata( $user_id );
  98. }
  99. if ( in_array( $field, array( 'login', 'pass', 'nicename', 'email', 'url', 'registered', 'activation_key', 'status' ) ) )
  100. $field = 'user_' . $field;
  101. $value = isset( $authordata->$field ) ? $authordata->$field : '';
  102. return apply_filters( 'get_the_author_' . $field, $value, $user_id );
  103. }
  104. /**
  105. * Retrieve the requested data of the author of the current post.
  106. * @link http://codex.wordpress.org/Template_Tags/the_author_meta
  107. * @since 2.8.0
  108. * @param string $field selects the field of the users record.
  109. * @param int $user_id Optional. User ID.
  110. * @echo string The author's field from the current author's DB object.
  111. */
  112. function the_author_meta($field = '', $user_id = false) {
  113. echo apply_filters('the_author_' . $field, get_the_author_meta($field, $user_id), $user_id);
  114. }
  115. /**
  116. * Retrieve either author's link or author's name.
  117. *
  118. * If the author has a home page set, return an HTML link, otherwise just return the
  119. * author's name.
  120. *
  121. * @uses get_the_author_meta()
  122. * @uses get_the_author()
  123. */
  124. function get_the_author_link() {
  125. if ( get_the_author_meta('url') ) {
  126. return '<a href="' . get_the_author_meta('url') . '" title="' . esc_attr( sprintf(__("Visit %s&#8217;s website"), get_the_author()) ) . '" rel="author external">' . get_the_author() . '</a>';
  127. } else {
  128. return get_the_author();
  129. }
  130. }
  131. /**
  132. * Display either author's link or author's name.
  133. *
  134. * If the author has a home page set, echo an HTML link, otherwise just echo the
  135. * author's name.
  136. *
  137. * @link http://codex.wordpress.org/Template_Tags/the_author_link
  138. * @since 2.1
  139. * @uses get_the_author_link()
  140. */
  141. function the_author_link() {
  142. echo get_the_author_link();
  143. }
  144. /**
  145. * Retrieve the number of posts by the author of the current post.
  146. *
  147. * @since 1.5
  148. * @uses $post The current post in the Loop's DB object.
  149. * @uses count_user_posts()
  150. * @return int The number of posts by the author.
  151. */
  152. function get_the_author_posts() {
  153. global $post;
  154. return count_user_posts($post->post_author);
  155. }
  156. /**
  157. * Display the number of posts by the author of the current post.
  158. *
  159. * @link http://codex.wordpress.org/Template_Tags/the_author_posts
  160. * @since 0.71
  161. * @uses get_the_author_posts() Echoes returned value from function.
  162. */
  163. function the_author_posts() {
  164. echo get_the_author_posts();
  165. }
  166. /**
  167. * Display an HTML link to the author page of the author of the current post.
  168. *
  169. * Does just echo get_author_posts_url() function, like the others do. The
  170. * reason for this, is that another function is used to help in printing the
  171. * link to the author's posts.
  172. *
  173. * @link http://codex.wordpress.org/Template_Tags/the_author_posts_link
  174. * @since 1.2.0
  175. * @uses $authordata The current author's DB object.
  176. * @uses get_author_posts_url()
  177. * @uses get_the_author()
  178. * @param string $deprecated Deprecated.
  179. */
  180. function the_author_posts_link($deprecated = '') {
  181. if ( !empty( $deprecated ) )
  182. _deprecated_argument( __FUNCTION__, '2.1' );
  183. global $authordata;
  184. if ( !is_object( $authordata ) )
  185. return false;
  186. $link = sprintf(
  187. '<a href="%1$s" title="%2$s" rel="author">%3$s</a>',
  188. get_author_posts_url( $authordata->ID, $authordata->user_nicename ),
  189. esc_attr( sprintf( __( 'Posts by %s' ), get_the_author() ) ),
  190. get_the_author()
  191. );
  192. echo apply_filters( 'the_author_posts_link', $link );
  193. }
  194. /**
  195. * Retrieve the URL to the author page for the user with the ID provided.
  196. *
  197. * @since 2.1.0
  198. * @uses $wp_rewrite WP_Rewrite
  199. * @return string The URL to the author's page.
  200. */
  201. function get_author_posts_url($author_id, $author_nicename = '') {
  202. global $wp_rewrite;
  203. $auth_ID = (int) $author_id;
  204. $link = $wp_rewrite->get_author_permastruct();
  205. if ( empty($link) ) {
  206. $file = home_url( '/' );
  207. $link = $file . '?author=' . $auth_ID;
  208. } else {
  209. if ( '' == $author_nicename ) {
  210. $user = get_userdata($author_id);
  211. if ( !empty($user->user_nicename) )
  212. $author_nicename = $user->user_nicename;
  213. }
  214. $link = str_replace('%author%', $author_nicename, $link);
  215. $link = home_url( user_trailingslashit( $link ) );
  216. }
  217. $link = apply_filters('author_link', $link, $author_id, $author_nicename);
  218. return $link;
  219. }
  220. /**
  221. * List all the authors of the blog, with several options available.
  222. *
  223. * <ul>
  224. * <li>optioncount (boolean) (false): Show the count in parenthesis next to the
  225. * author's name.</li>
  226. * <li>exclude_admin (boolean) (true): Exclude the 'admin' user that is
  227. * installed bydefault.</li>
  228. * <li>show_fullname (boolean) (false): Show their full names.</li>
  229. * <li>hide_empty (boolean) (true): Don't show authors without any posts.</li>
  230. * <li>feed (string) (''): If isn't empty, show links to author's feeds.</li>
  231. * <li>feed_image (string) (''): If isn't empty, use this image to link to
  232. * feeds.</li>
  233. * <li>echo (boolean) (true): Set to false to return the output, instead of
  234. * echoing.</li>
  235. * <li>style (string) ('list'): Whether to display list of authors in list form
  236. * or as a string.</li>
  237. * <li>html (bool) (true): Whether to list the items in html form or plaintext.
  238. * </li>
  239. * </ul>
  240. *
  241. * @link http://codex.wordpress.org/Template_Tags/wp_list_authors
  242. * @since 1.2.0
  243. * @param array $args The argument array.
  244. * @return null|string The output, if echo is set to false.
  245. */
  246. function wp_list_authors($args = '') {
  247. global $wpdb;
  248. $defaults = array(
  249. 'orderby' => 'name', 'order' => 'ASC', 'number' => '',
  250. 'optioncount' => false, 'exclude_admin' => true,
  251. 'show_fullname' => false, 'hide_empty' => true,
  252. 'feed' => '', 'feed_image' => '', 'feed_type' => '', 'echo' => true,
  253. 'style' => 'list', 'html' => true
  254. );
  255. $args = wp_parse_args( $args, $defaults );
  256. extract( $args, EXTR_SKIP );
  257. $return = '';
  258. $query_args = wp_array_slice_assoc( $args, array( 'orderby', 'order', 'number' ) );
  259. $query_args['fields'] = 'ids';
  260. $authors = get_users( $query_args );
  261. $author_count = array();
  262. 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 )
  263. $author_count[$row->post_author] = $row->count;
  264. foreach ( $authors as $author_id ) {
  265. $author = get_userdata( $author_id );
  266. if ( $exclude_admin && 'admin' == $author->display_name )
  267. continue;
  268. $posts = isset( $author_count[$author->ID] ) ? $author_count[$author->ID] : 0;
  269. if ( !$posts && $hide_empty )
  270. continue;
  271. $link = '';
  272. if ( $show_fullname && $author->first_name && $author->last_name )
  273. $name = "$author->first_name $author->last_name";
  274. else
  275. $name = $author->display_name;
  276. if ( !$html ) {
  277. $return .= $name . ', ';
  278. continue; // No need to go further to process HTML.
  279. }
  280. if ( 'list' == $style ) {
  281. $return .= '<li>';
  282. }
  283. $link = '<a href="' . get_author_posts_url( $author->ID, $author->user_nicename ) . '" title="' . esc_attr( sprintf(__("Posts by %s"), $author->display_name) ) . '">' . $name . '</a>';
  284. if ( !empty( $feed_image ) || !empty( $feed ) ) {
  285. $link .= ' ';
  286. if ( empty( $feed_image ) ) {
  287. $link .= '(';
  288. }
  289. $link .= '<a href="' . get_author_feed_link( $author->ID ) . '"';
  290. $alt = $title = '';
  291. if ( !empty( $feed ) ) {
  292. $title = ' title="' . esc_attr( $feed ) . '"';
  293. $alt = ' alt="' . esc_attr( $feed ) . '"';
  294. $name = $feed;
  295. $link .= $title;
  296. }
  297. $link .= '>';
  298. if ( !empty( $feed_image ) )
  299. $link .= '<img src="' . esc_url( $feed_image ) . '" style="border: none;"' . $alt . $title . ' />';
  300. else
  301. $link .= $name;
  302. $link .= '</a>';
  303. if ( empty( $feed_image ) )
  304. $link .= ')';
  305. }
  306. if ( $optioncount )
  307. $link .= ' ('. $posts . ')';
  308. $return .= $link;
  309. $return .= ( 'list' == $style ) ? '</li>' : ', ';
  310. }
  311. $return = rtrim($return, ', ');
  312. if ( !$echo )
  313. return $return;
  314. echo $return;
  315. }
  316. /**
  317. * Does this site have more than one author
  318. *
  319. * Checks to see if more than one author has published posts.
  320. *
  321. * @since 3.2.0
  322. * @return bool Whether or not we have more than one author
  323. */
  324. function is_multi_author() {
  325. global $wpdb;
  326. if ( false === ( $is_multi_author = wp_cache_get('is_multi_author', 'posts') ) ) {
  327. $rows = (array) $wpdb->get_col("SELECT DISTINCT post_author FROM $wpdb->posts WHERE post_type = 'post' AND post_status = 'publish' LIMIT 2");
  328. $is_multi_author = 1 < count( $rows ) ? 1 : 0;
  329. wp_cache_set('is_multi_author', $is_multi_author, 'posts');
  330. }
  331. return apply_filters( 'is_multi_author', (bool) $is_multi_author );
  332. }
  333. /**
  334. * Helper function to clear the cache for number of authors.
  335. *
  336. * @private
  337. */
  338. function __clear_multi_author_cache() {
  339. wp_cache_delete('is_multi_author', 'posts');
  340. }
  341. add_action('transition_post_status', '__clear_multi_author_cache');