PageRenderTime 131ms CodeModel.GetById 26ms RepoModel.GetById 0ms app.codeStats 0ms

/APP/wp-includes/author-template.php

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