Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.
 
 
 
 
 

510 wiersze
15 KiB

  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 https://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. * @global object $authordata The current author's DB object.
  18. *
  19. * @param string $deprecated Deprecated.
  20. * @return string|null 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.0' );
  26. /**
  27. * Filters 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, backward compatibility has to be maintained.
  45. *
  46. * @since 0.71
  47. * @see get_the_author()
  48. * @link https://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|null 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.0' );
  57. }
  58. if ( true !== $deprecated_echo ) {
  59. _deprecated_argument( __FUNCTION__, '1.5.0',
  60. /* translators: %s: get_the_author() */
  61. sprintf( __( 'Use %s instead if you do not want the value echoed.' ),
  62. '<code>get_the_author()</code>'
  63. )
  64. );
  65. }
  66. if ( $deprecated_echo ) {
  67. echo get_the_author();
  68. }
  69. return get_the_author();
  70. }
  71. /**
  72. * Retrieve the author who last edited the current post.
  73. *
  74. * @since 2.8.0
  75. *
  76. * @return string|void The author's display name.
  77. */
  78. function get_the_modified_author() {
  79. if ( $last_id = get_post_meta( get_post()->ID, '_edit_last', true) ) {
  80. $last_user = get_userdata($last_id);
  81. /**
  82. * Filters the display name of the author who last edited the current post.
  83. *
  84. * @since 2.8.0
  85. *
  86. * @param string $last_user->display_name The author's display name.
  87. */
  88. return apply_filters('the_modified_author', $last_user->display_name);
  89. }
  90. }
  91. /**
  92. * Display the name of the author who last edited the current post,
  93. * if the author's ID is available.
  94. *
  95. * @since 2.8.0
  96. *
  97. * @see get_the_author()
  98. */
  99. function the_modified_author() {
  100. echo get_the_modified_author();
  101. }
  102. /**
  103. * Retrieve the requested data of the author of the current post.
  104. * @link https://codex.wordpress.org/Template_Tags/the_author_meta
  105. * @since 2.8.0
  106. *
  107. * @global object $authordata The current author's DB object.
  108. *
  109. * @param string $field selects the field of the users record.
  110. * @param int $user_id Optional. User ID.
  111. * @return string The author's field from the current author's DB object.
  112. */
  113. function get_the_author_meta( $field = '', $user_id = false ) {
  114. $original_user_id = $user_id;
  115. if ( ! $user_id ) {
  116. global $authordata;
  117. $user_id = isset( $authordata->ID ) ? $authordata->ID : 0;
  118. } else {
  119. $authordata = get_userdata( $user_id );
  120. }
  121. if ( in_array( $field, array( 'login', 'pass', 'nicename', 'email', 'url', 'registered', 'activation_key', 'status' ) ) )
  122. $field = 'user_' . $field;
  123. $value = isset( $authordata->$field ) ? $authordata->$field : '';
  124. /**
  125. * Filters the value of the requested user metadata.
  126. *
  127. * The filter name is dynamic and depends on the $field parameter of the function.
  128. *
  129. * @since 2.8.0
  130. * @since 4.3.0 The `$original_user_id` parameter was added.
  131. *
  132. * @param string $value The value of the metadata.
  133. * @param int $user_id The user ID for the value.
  134. * @param int|bool $original_user_id The original user ID, as passed to the function.
  135. */
  136. return apply_filters( 'get_the_author_' . $field, $value, $user_id, $original_user_id );
  137. }
  138. /**
  139. * Outputs the field from the user's DB object. Defaults to current post's author.
  140. *
  141. * @link https://codex.wordpress.org/Template_Tags/the_author_meta
  142. *
  143. * @since 2.8.0
  144. *
  145. * @param string $field selects the field of the users record.
  146. * @param int $user_id Optional. User ID.
  147. */
  148. function the_author_meta( $field = '', $user_id = false ) {
  149. $author_meta = get_the_author_meta( $field, $user_id );
  150. /**
  151. * The value of the requested user metadata.
  152. *
  153. * The filter name is dynamic and depends on the $field parameter of the function.
  154. *
  155. * @since 2.8.0
  156. *
  157. * @param string $author_meta The value of the metadata.
  158. * @param int $user_id The user ID.
  159. */
  160. echo apply_filters( 'the_author_' . $field, $author_meta, $user_id );
  161. }
  162. /**
  163. * Retrieve either author's link or author's name.
  164. *
  165. * If the author has a home page set, return an HTML link, otherwise just return the
  166. * author's name.
  167. *
  168. * @return string|null An HTML link if the author's url exist in user meta,
  169. * else the result of get_the_author().
  170. */
  171. function get_the_author_link() {
  172. if ( get_the_author_meta('url') ) {
  173. return sprintf( '<a href="%1$s" title="%2$s" rel="author external">%3$s</a>',
  174. esc_url( get_the_author_meta('url') ),
  175. /* translators: %s: author's display name */
  176. esc_attr( sprintf( __( 'Visit %s&#8217;s website' ), get_the_author() ) ),
  177. get_the_author()
  178. );
  179. } else {
  180. return get_the_author();
  181. }
  182. }
  183. /**
  184. * Display either author's link or author's name.
  185. *
  186. * If the author has a home page set, echo an HTML link, otherwise just echo the
  187. * author's name.
  188. *
  189. * @link https://codex.wordpress.org/Template_Tags/the_author_link
  190. *
  191. * @since 2.1.0
  192. */
  193. function the_author_link() {
  194. echo get_the_author_link();
  195. }
  196. /**
  197. * Retrieve the number of posts by the author of the current post.
  198. *
  199. * @since 1.5.0
  200. *
  201. * @return int The number of posts by the author.
  202. */
  203. function get_the_author_posts() {
  204. $post = get_post();
  205. if ( ! $post ) {
  206. return 0;
  207. }
  208. return count_user_posts( $post->post_author, $post->post_type );
  209. }
  210. /**
  211. * Display the number of posts by the author of the current post.
  212. *
  213. * @link https://codex.wordpress.org/Template_Tags/the_author_posts
  214. * @since 0.71
  215. */
  216. function the_author_posts() {
  217. echo get_the_author_posts();
  218. }
  219. /**
  220. * Retrieves an HTML link to the author page of the current post's author.
  221. *
  222. * Returns an HTML-formatted link using get_author_posts_url().
  223. *
  224. * @since 4.4.0
  225. *
  226. * @global object $authordata The current author's DB object.
  227. *
  228. * @return string An HTML link to the author page.
  229. */
  230. function get_the_author_posts_link() {
  231. global $authordata;
  232. if ( ! is_object( $authordata ) ) {
  233. return;
  234. }
  235. $link = sprintf( '<a href="%1$s" title="%2$s" rel="author">%3$s</a>',
  236. esc_url( get_author_posts_url( $authordata->ID, $authordata->user_nicename ) ),
  237. /* translators: %s: author's display name */
  238. esc_attr( sprintf( __( 'Posts by %s' ), get_the_author() ) ),
  239. get_the_author()
  240. );
  241. /**
  242. * Filters the link to the author page of the author of the current post.
  243. *
  244. * @since 2.9.0
  245. *
  246. * @param string $link HTML link.
  247. */
  248. return apply_filters( 'the_author_posts_link', $link );
  249. }
  250. /**
  251. * Displays an HTML link to the author page of the current post's author.
  252. *
  253. * @since 1.2.0
  254. * @since 4.4.0 Converted into a wrapper for get_the_author_posts_link()
  255. *
  256. * @param string $deprecated Unused.
  257. */
  258. function the_author_posts_link( $deprecated = '' ) {
  259. if ( ! empty( $deprecated ) ) {
  260. _deprecated_argument( __FUNCTION__, '2.1.0' );
  261. }
  262. echo get_the_author_posts_link();
  263. }
  264. /**
  265. * Retrieve the URL to the author page for the user with the ID provided.
  266. *
  267. * @since 2.1.0
  268. *
  269. * @global WP_Rewrite $wp_rewrite
  270. *
  271. * @param int $author_id Author ID.
  272. * @param string $author_nicename Optional. The author's nicename (slug). Default empty.
  273. * @return string The URL to the author's page.
  274. */
  275. function get_author_posts_url( $author_id, $author_nicename = '' ) {
  276. global $wp_rewrite;
  277. $auth_ID = (int) $author_id;
  278. $link = $wp_rewrite->get_author_permastruct();
  279. if ( empty($link) ) {
  280. $file = home_url( '/' );
  281. $link = $file . '?author=' . $auth_ID;
  282. } else {
  283. if ( '' == $author_nicename ) {
  284. $user = get_userdata($author_id);
  285. if ( !empty($user->user_nicename) )
  286. $author_nicename = $user->user_nicename;
  287. }
  288. $link = str_replace('%author%', $author_nicename, $link);
  289. $link = home_url( user_trailingslashit( $link ) );
  290. }
  291. /**
  292. * Filters the URL to the author's page.
  293. *
  294. * @since 2.1.0
  295. *
  296. * @param string $link The URL to the author's page.
  297. * @param int $author_id The author's id.
  298. * @param string $author_nicename The author's nice name.
  299. */
  300. $link = apply_filters( 'author_link', $link, $author_id, $author_nicename );
  301. return $link;
  302. }
  303. /**
  304. * List all the authors of the site, with several options available.
  305. *
  306. * @link https://codex.wordpress.org/Template_Tags/wp_list_authors
  307. *
  308. * @since 1.2.0
  309. *
  310. * @global wpdb $wpdb WordPress database abstraction object.
  311. *
  312. * @param string|array $args {
  313. * Optional. Array or string of default arguments.
  314. *
  315. * @type string $orderby How to sort the authors. Accepts 'nicename', 'email', 'url', 'registered',
  316. * 'user_nicename', 'user_email', 'user_url', 'user_registered', 'name',
  317. * 'display_name', 'post_count', 'ID', 'meta_value', 'user_login'. Default 'name'.
  318. * @type string $order Sorting direction for $orderby. Accepts 'ASC', 'DESC'. Default 'ASC'.
  319. * @type int $number Maximum authors to return or display. Default empty (all authors).
  320. * @type bool $optioncount Show the count in parenthesis next to the author's name. Default false.
  321. * @type bool $exclude_admin Whether to exclude the 'admin' account, if it exists. Default false.
  322. * @type bool $show_fullname Whether to show the author's full name. Default false.
  323. * @type bool $hide_empty Whether to hide any authors with no posts. Default true.
  324. * @type string $feed If not empty, show a link to the author's feed and use this text as the alt
  325. * parameter of the link. Default empty.
  326. * @type string $feed_image If not empty, show a link to the author's feed and use this image URL as
  327. * clickable anchor. Default empty.
  328. * @type string $feed_type The feed type to link to, such as 'rss2'. Defaults to default feed type.
  329. * @type bool $echo Whether to output the result or instead return it. Default true.
  330. * @type string $style If 'list', each author is wrapped in an `<li>` element, otherwise the authors
  331. * will be separated by commas.
  332. * @type bool $html Whether to list the items in HTML form or plaintext. Default true.
  333. * @type array|string $exclude Array or comma/space-separated list of author IDs to exclude. Default empty.
  334. * @type array|string $include Array or comma/space-separated list of author IDs to include. Default empty.
  335. * }
  336. * @return string|void The output, if echo is set to false.
  337. */
  338. function wp_list_authors( $args = '' ) {
  339. global $wpdb;
  340. $defaults = array(
  341. 'orderby' => 'name', 'order' => 'ASC', 'number' => '',
  342. 'optioncount' => false, 'exclude_admin' => true,
  343. 'show_fullname' => false, 'hide_empty' => true,
  344. 'feed' => '', 'feed_image' => '', 'feed_type' => '', 'echo' => true,
  345. 'style' => 'list', 'html' => true, 'exclude' => '', 'include' => ''
  346. );
  347. $args = wp_parse_args( $args, $defaults );
  348. $return = '';
  349. $query_args = wp_array_slice_assoc( $args, array( 'orderby', 'order', 'number', 'exclude', 'include' ) );
  350. $query_args['fields'] = 'ids';
  351. $authors = get_users( $query_args );
  352. $author_count = array();
  353. foreach ( (array) $wpdb->get_results( "SELECT DISTINCT post_author, COUNT(ID) AS count FROM $wpdb->posts WHERE " . get_private_posts_cap_sql( 'post' ) . " GROUP BY post_author" ) as $row ) {
  354. $author_count[$row->post_author] = $row->count;
  355. }
  356. foreach ( $authors as $author_id ) {
  357. $author = get_userdata( $author_id );
  358. if ( $args['exclude_admin'] && 'admin' == $author->display_name ) {
  359. continue;
  360. }
  361. $posts = isset( $author_count[$author->ID] ) ? $author_count[$author->ID] : 0;
  362. if ( ! $posts && $args['hide_empty'] ) {
  363. continue;
  364. }
  365. if ( $args['show_fullname'] && $author->first_name && $author->last_name ) {
  366. $name = "$author->first_name $author->last_name";
  367. } else {
  368. $name = $author->display_name;
  369. }
  370. if ( ! $args['html'] ) {
  371. $return .= $name . ', ';
  372. continue; // No need to go further to process HTML.
  373. }
  374. if ( 'list' == $args['style'] ) {
  375. $return .= '<li>';
  376. }
  377. $link = sprintf( '<a href="%1$s" title="%2$s">%3$s</a>',
  378. get_author_posts_url( $author->ID, $author->user_nicename ),
  379. /* translators: %s: author's display name */
  380. esc_attr( sprintf( __( 'Posts by %s' ), $author->display_name ) ),
  381. $name
  382. );
  383. if ( ! empty( $args['feed_image'] ) || ! empty( $args['feed'] ) ) {
  384. $link .= ' ';
  385. if ( empty( $args['feed_image'] ) ) {
  386. $link .= '(';
  387. }
  388. $link .= '<a href="' . get_author_feed_link( $author->ID, $args['feed_type'] ) . '"';
  389. $alt = '';
  390. if ( ! empty( $args['feed'] ) ) {
  391. $alt = ' alt="' . esc_attr( $args['feed'] ) . '"';
  392. $name = $args['feed'];
  393. }
  394. $link .= '>';
  395. if ( ! empty( $args['feed_image'] ) ) {
  396. $link .= '<img src="' . esc_url( $args['feed_image'] ) . '" style="border: none;"' . $alt . ' />';
  397. } else {
  398. $link .= $name;
  399. }
  400. $link .= '</a>';
  401. if ( empty( $args['feed_image'] ) ) {
  402. $link .= ')';
  403. }
  404. }
  405. if ( $args['optioncount'] ) {
  406. $link .= ' ('. $posts . ')';
  407. }
  408. $return .= $link;
  409. $return .= ( 'list' == $args['style'] ) ? '</li>' : ', ';
  410. }
  411. $return = rtrim( $return, ', ' );
  412. if ( ! $args['echo'] ) {
  413. return $return;
  414. }
  415. echo $return;
  416. }
  417. /**
  418. * Does this site have more than one author
  419. *
  420. * Checks to see if more than one author has published posts.
  421. *
  422. * @since 3.2.0
  423. *
  424. * @global wpdb $wpdb WordPress database abstraction object.
  425. *
  426. * @return bool Whether or not we have more than one author
  427. */
  428. function is_multi_author() {
  429. global $wpdb;
  430. if ( false === ( $is_multi_author = get_transient( 'is_multi_author' ) ) ) {
  431. $rows = (array) $wpdb->get_col("SELECT DISTINCT post_author FROM $wpdb->posts WHERE post_type = 'post' AND post_status = 'publish' LIMIT 2");
  432. $is_multi_author = 1 < count( $rows ) ? 1 : 0;
  433. set_transient( 'is_multi_author', $is_multi_author );
  434. }
  435. /**
  436. * Filters whether the site has more than one author with published posts.
  437. *
  438. * @since 3.2.0
  439. *
  440. * @param bool $is_multi_author Whether $is_multi_author should evaluate as true.
  441. */
  442. return apply_filters( 'is_multi_author', (bool) $is_multi_author );
  443. }
  444. /**
  445. * Helper function to clear the cache for number of authors.
  446. *
  447. * @private
  448. */
  449. function __clear_multi_author_cache() {
  450. delete_transient( 'is_multi_author' );
  451. }