PageRenderTime 61ms CodeModel.GetById 33ms RepoModel.GetById 0ms app.codeStats 0ms

/wp-includes/author-template.php

https://bitbucket.org/Thane2376/death-edge.ru
PHP | 471 lines | 181 code | 53 blank | 237 comment | 41 complexity | 1bb8ef07fd019f4b6dc8beb48bdcd8d8 MD5 | raw file
Possible License(s): LGPL-2.1, GPL-2.0, LGPL-3.0, AGPL-1.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.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. $post = get_post();
  194. if ( ! $post ) {
  195. return 0;
  196. }
  197. return count_user_posts( $post->post_author );
  198. }
  199. /**
  200. * Display the number of posts by the author of the current post.
  201. *
  202. * @link http://codex.wordpress.org/Template_Tags/the_author_posts
  203. * @since 0.71
  204. * @uses get_the_author_posts() Echoes returned value from function.
  205. */
  206. function the_author_posts() {
  207. echo get_the_author_posts();
  208. }
  209. /**
  210. * Display an HTML link to the author page of the author of the current post.
  211. *
  212. * Does just echo get_author_posts_url() function, like the others do. The
  213. * reason for this, is that another function is used to help in printing the
  214. * link to the author's posts.
  215. *
  216. * @link http://codex.wordpress.org/Template_Tags/the_author_posts_link
  217. * @since 1.2.0
  218. * @uses $authordata The current author's DB object.
  219. * @uses get_author_posts_url()
  220. * @uses get_the_author()
  221. * @param string $deprecated Deprecated.
  222. */
  223. function the_author_posts_link($deprecated = '') {
  224. if ( !empty( $deprecated ) )
  225. _deprecated_argument( __FUNCTION__, '2.1' );
  226. global $authordata;
  227. if ( !is_object( $authordata ) )
  228. return false;
  229. $link = sprintf(
  230. '<a href="%1$s" title="%2$s" rel="author">%3$s</a>',
  231. esc_url( get_author_posts_url( $authordata->ID, $authordata->user_nicename ) ),
  232. esc_attr( sprintf( __( 'Posts by %s' ), get_the_author() ) ),
  233. get_the_author()
  234. );
  235. /**
  236. * Filter the link to the author page of the author of the current post.
  237. *
  238. * @since 2.9.0
  239. *
  240. * @param string $link HTML link.
  241. */
  242. echo apply_filters( 'the_author_posts_link', $link );
  243. }
  244. /**
  245. * Retrieve the URL to the author page for the user with the ID provided.
  246. *
  247. * @since 2.1.0
  248. * @uses $wp_rewrite WP_Rewrite
  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 http://codex.wordpress.org/Template_Tags/wp_list_authors
  283. *
  284. * @since 1.2.0
  285. *
  286. * @param string|array $args {
  287. * Optional. Array or string of default arguments.
  288. *
  289. * @type string $orderby How to sort the authors. Accepts 'nicename', 'email', 'url', 'registered',
  290. * 'user_nicename', 'user_email', 'user_url', 'user_registered', 'name',
  291. * 'display_name', 'post_count', 'ID', 'meta_value', 'user_login'. Default 'name'.
  292. * @type string $order Sorting direction for $orderby. Accepts 'ASC', 'DESC'. Default 'ASC'.
  293. * @type int $number Maximum authors to return or display. Default empty (all authors).
  294. * @type bool $optioncount Show the count in parenthesis next to the author's name. Default false.
  295. * @type bool $exclude_admin Whether to exclude the 'admin' account, if it exists. Default false.
  296. * @type bool $show_fullname Whether to show the author's full name. Default false.
  297. * @type bool $hide_empty Whether to hide any authors with no posts. Default true.
  298. * @type string $feed If not empty, show a link to the author's feed and use this text as the alt
  299. * parameter of the link. Default empty.
  300. * @type string $feed_image If not empty, show a link to the author's feed and use this image URL as
  301. * clickable anchor. Default empty.
  302. * @type string $feed_type The feed type to link to, such as 'rss2'. Defaults to default feed type.
  303. * @type bool $echo Whether to output the result or instead return it. Default true.
  304. * @type string $style If 'list', each author is wrapped in an `<li>` element, otherwise the authors
  305. * will be separated by commas.
  306. * @type bool $html Whether to list the items in HTML form or plaintext. Default true.
  307. * @type string $exclude An array, comma-, or space-separated list of author IDs to exclude. Default empty.
  308. * @type string $exclude An array, comma-, or space-separated list of author IDs to include. Default empty.
  309. * }
  310. * @return null|string The output, if echo is set to false. Otherwise null.
  311. */
  312. function wp_list_authors( $args = '' ) {
  313. global $wpdb;
  314. $defaults = array(
  315. 'orderby' => 'name', 'order' => 'ASC', 'number' => '',
  316. 'optioncount' => false, 'exclude_admin' => true,
  317. 'show_fullname' => false, 'hide_empty' => true,
  318. 'feed' => '', 'feed_image' => '', 'feed_type' => '', 'echo' => true,
  319. 'style' => 'list', 'html' => true, 'exclude' => '', 'include' => ''
  320. );
  321. $args = wp_parse_args( $args, $defaults );
  322. $return = '';
  323. $query_args = wp_array_slice_assoc( $args, array( 'orderby', 'order', 'number', 'exclude', 'include' ) );
  324. $query_args['fields'] = 'ids';
  325. $authors = get_users( $query_args );
  326. $author_count = array();
  327. 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 ) {
  328. $author_count[$row->post_author] = $row->count;
  329. }
  330. foreach ( $authors as $author_id ) {
  331. $author = get_userdata( $author_id );
  332. if ( $args['exclude_admin'] && 'admin' == $author->display_name ) {
  333. continue;
  334. }
  335. $posts = isset( $author_count[$author->ID] ) ? $author_count[$author->ID] : 0;
  336. if ( ! $posts && $args['hide_empty'] ) {
  337. continue;
  338. }
  339. if ( $args['show_fullname'] && $author->first_name && $author->last_name ) {
  340. $name = "$author->first_name $author->last_name";
  341. } else {
  342. $name = $author->display_name;
  343. }
  344. if ( ! $args['html'] ) {
  345. $return .= $name . ', ';
  346. continue; // No need to go further to process HTML.
  347. }
  348. if ( 'list' == $args['style'] ) {
  349. $return .= '<li>';
  350. }
  351. $link = '<a href="' . get_author_posts_url( $author->ID, $author->user_nicename ) . '" title="' . esc_attr( sprintf(__("Posts by %s"), $author->display_name) ) . '">' . $name . '</a>';
  352. if ( ! empty( $args['feed_image'] ) || ! empty( $args['feed'] ) ) {
  353. $link .= ' ';
  354. if ( empty( $args['feed_image'] ) ) {
  355. $link .= '(';
  356. }
  357. $link .= '<a href="' . get_author_feed_link( $author->ID, $args['feed_type'] ) . '"';
  358. $alt = '';
  359. if ( ! empty( $args['feed'] ) ) {
  360. $alt = ' alt="' . esc_attr( $args['feed'] ) . '"';
  361. $name = $args['feed'];
  362. }
  363. $link .= '>';
  364. if ( ! empty( $args['feed_image'] ) ) {
  365. $link .= '<img src="' . esc_url( $args['feed_image'] ) . '" style="border: none;"' . $alt . ' />';
  366. } else {
  367. $link .= $name;
  368. }
  369. $link .= '</a>';
  370. if ( empty( $args['feed_image'] ) ) {
  371. $link .= ')';
  372. }
  373. }
  374. if ( $args['optioncount'] ) {
  375. $link .= ' ('. $posts . ')';
  376. }
  377. $return .= $link;
  378. $return .= ( 'list' == $args['style'] ) ? '</li>' : ', ';
  379. }
  380. $return = rtrim( $return, ', ' );
  381. if ( ! $args['echo'] ) {
  382. return $return;
  383. }
  384. echo $return;
  385. }
  386. /**
  387. * Does this site have more than one author
  388. *
  389. * Checks to see if more than one author has published posts.
  390. *
  391. * @since 3.2.0
  392. * @return bool Whether or not we have more than one author
  393. */
  394. function is_multi_author() {
  395. global $wpdb;
  396. if ( false === ( $is_multi_author = get_transient( 'is_multi_author' ) ) ) {
  397. $rows = (array) $wpdb->get_col("SELECT DISTINCT post_author FROM $wpdb->posts WHERE post_type = 'post' AND post_status = 'publish' LIMIT 2");
  398. $is_multi_author = 1 < count( $rows ) ? 1 : 0;
  399. set_transient( 'is_multi_author', $is_multi_author );
  400. }
  401. /**
  402. * Filter whether the site has more than one author with published posts.
  403. *
  404. * @since 3.2.0
  405. *
  406. * @param bool $is_multi_author Whether $is_multi_author should evaluate as true.
  407. */
  408. return apply_filters( 'is_multi_author', (bool) $is_multi_author );
  409. }
  410. /**
  411. * Helper function to clear the cache for number of authors.
  412. *
  413. * @private
  414. */
  415. function __clear_multi_author_cache() {
  416. delete_transient( 'is_multi_author' );
  417. }
  418. add_action('transition_post_status', '__clear_multi_author_cache');