PageRenderTime 1428ms CodeModel.GetById 18ms RepoModel.GetById 23ms app.codeStats 1ms

/wp-content/plugins/bbpress/includes/replies/template-tags.php

https://github.com/bfay/maniacal-kitten
PHP | 2173 lines | 778 code | 275 blank | 1120 comment | 156 complexity | 39326c306e8c93ee94f079543aa7de47 MD5 | raw file
Possible License(s): GPL-2.0, GPL-3.0, AGPL-1.0, LGPL-3.0, LGPL-2.1

Large files files are truncated, but you can click here to view the full file

  1. <?php
  2. /**
  3. * bbPress Reply Template Tags
  4. *
  5. * @package bbPress
  6. * @subpackage TemplateTags
  7. */
  8. // Exit if accessed directly
  9. if ( !defined( 'ABSPATH' ) ) exit;
  10. /** Post Type *****************************************************************/
  11. /**
  12. * Return the unique id of the custom post type for replies
  13. *
  14. * @since bbPress (r2857)
  15. *
  16. * @uses bbp_get_reply_post_type() To get the reply post type
  17. */
  18. function bbp_reply_post_type() {
  19. echo bbp_get_reply_post_type();
  20. }
  21. /**
  22. * Return the unique id of the custom post type for replies
  23. *
  24. * @since bbPress (r2857)
  25. *
  26. * @uses apply_filters() Calls 'bbp_get_forum_post_type' with the forum
  27. * post type id
  28. * @return string The unique reply post type id
  29. */
  30. function bbp_get_reply_post_type() {
  31. return apply_filters( 'bbp_get_reply_post_type', bbpress()->reply_post_type );
  32. }
  33. /** Reply Loop Functions ******************************************************/
  34. /**
  35. * The main reply loop. WordPress makes this easy for us
  36. *
  37. * @since bbPress (r2553)
  38. *
  39. * @param mixed $args All the arguments supported by {@link WP_Query}
  40. * @uses bbp_show_lead_topic() Are we showing the topic as a lead?
  41. * @uses bbp_get_topic_id() To get the topic id
  42. * @uses bbp_get_reply_post_type() To get the reply post type
  43. * @uses bbp_get_topic_post_type() To get the topic post type
  44. * @uses get_option() To get the replies per page option
  45. * @uses bbp_get_paged() To get the current page value
  46. * @uses current_user_can() To check if the current user is capable of editing
  47. * others' replies
  48. * @uses WP_Query To make query and get the replies
  49. * @uses WP_Rewrite::using_permalinks() To check if the blog is using permalinks
  50. * @uses get_permalink() To get the permalink
  51. * @uses add_query_arg() To add custom args to the url
  52. * @uses apply_filters() Calls 'bbp_replies_pagination' with the pagination args
  53. * @uses paginate_links() To paginate the links
  54. * @uses apply_filters() Calls 'bbp_has_replies' with
  55. * bbPres::reply_query::have_posts()
  56. * and bbPres::reply_query
  57. * @return object Multidimensional array of reply information
  58. */
  59. function bbp_has_replies( $args = '' ) {
  60. global $wp_rewrite;
  61. /** Defaults **************************************************************/
  62. // Other defaults
  63. $default_reply_search = !empty( $_REQUEST['rs'] ) ? $_REQUEST['rs'] : false;
  64. $default_post_parent = ( bbp_is_single_topic() ) ? bbp_get_topic_id() : 'any';
  65. $default_post_type = ( bbp_is_single_topic() && bbp_show_lead_topic() ) ? bbp_get_reply_post_type() : array( bbp_get_topic_post_type(), bbp_get_reply_post_type() );
  66. // Default query args
  67. $default = array(
  68. 'post_type' => $default_post_type, // Only replies
  69. 'post_parent' => $default_post_parent, // Of this topic
  70. 'posts_per_page' => bbp_get_replies_per_page(), // This many
  71. 'paged' => bbp_get_paged(), // On this page
  72. 'orderby' => 'date', // Sorted by date
  73. 'order' => 'ASC', // Oldest to newest
  74. 's' => $default_reply_search, // Maybe search
  75. );
  76. // What are the default allowed statuses (based on user caps)
  77. if ( bbp_get_view_all() ) {
  78. // Default view=all statuses
  79. $post_statuses = array(
  80. bbp_get_public_status_id(),
  81. bbp_get_closed_status_id(),
  82. bbp_get_spam_status_id(),
  83. bbp_get_trash_status_id()
  84. );
  85. // Add support for private status
  86. if ( current_user_can( 'read_private_replies' ) ) {
  87. $post_statuses[] = bbp_get_private_status_id();
  88. }
  89. // Join post statuses together
  90. $default['post_status'] = join( ',', $post_statuses );
  91. // Lean on the 'perm' query var value of 'readable' to provide statuses
  92. } else {
  93. $default['perm'] = 'readable';
  94. }
  95. /** Setup *****************************************************************/
  96. // Parse arguments against default values
  97. $r = bbp_parse_args( $args, $default, 'has_replies' );
  98. // Get bbPress
  99. $bbp = bbpress();
  100. // Call the query
  101. $bbp->reply_query = new WP_Query( $r );
  102. // Add pagination values to query object
  103. $bbp->reply_query->posts_per_page = $r['posts_per_page'];
  104. $bbp->reply_query->paged = $r['paged'];
  105. // Never home, regardless of what parse_query says
  106. $bbp->reply_query->is_home = false;
  107. // Reset is_single if single topic
  108. if ( bbp_is_single_topic() ) {
  109. $bbp->reply_query->is_single = true;
  110. }
  111. // Only add pagination if query returned results
  112. if ( (int) $bbp->reply_query->found_posts && (int) $bbp->reply_query->posts_per_page ) {
  113. // If pretty permalinks are enabled, make our pagination pretty
  114. if ( $wp_rewrite->using_permalinks() ) {
  115. // User's replies
  116. if ( bbp_is_single_user_replies() ) {
  117. $base = bbp_get_user_replies_created_url( bbp_get_displayed_user_id() );
  118. // Root profile page
  119. } elseif ( bbp_is_single_user() ) {
  120. $base = bbp_get_user_profile_url( bbp_get_displayed_user_id() );
  121. // Page or single post
  122. } elseif ( is_page() || is_single() ) {
  123. $base = get_permalink();
  124. // Single topic
  125. } else {
  126. $base = get_permalink( bbp_get_topic_id() );
  127. }
  128. $base = trailingslashit( $base ) . user_trailingslashit( $wp_rewrite->pagination_base . '/%#%/' );
  129. // Unpretty permalinks
  130. } else {
  131. $base = add_query_arg( 'paged', '%#%' );
  132. }
  133. // Add pagination to query object
  134. $bbp->reply_query->pagination_links = paginate_links(
  135. apply_filters( 'bbp_replies_pagination', array(
  136. 'base' => $base,
  137. 'format' => '',
  138. 'total' => ceil( (int) $bbp->reply_query->found_posts / (int) $r['posts_per_page'] ),
  139. 'current' => (int) $bbp->reply_query->paged,
  140. 'prev_text' => is_rtl() ? '&rarr;' : '&larr;',
  141. 'next_text' => is_rtl() ? '&larr;' : '&rarr;',
  142. 'mid_size' => 1,
  143. 'add_args' => ( bbp_get_view_all() ) ? array( 'view' => 'all' ) : false
  144. ) )
  145. );
  146. // Remove first page from pagination
  147. if ( $wp_rewrite->using_permalinks() ) {
  148. $bbp->reply_query->pagination_links = str_replace( $wp_rewrite->pagination_base . '/1/', '', $bbp->reply_query->pagination_links );
  149. } else {
  150. $bbp->reply_query->pagination_links = str_replace( '&#038;paged=1', '', $bbp->reply_query->pagination_links );
  151. }
  152. }
  153. // Return object
  154. return apply_filters( 'bbp_has_replies', $bbp->reply_query->have_posts(), $bbp->reply_query );
  155. }
  156. /**
  157. * Whether there are more replies available in the loop
  158. *
  159. * @since bbPress (r2553)
  160. *
  161. * @uses WP_Query bbPress::reply_query::have_posts() To check if there are more
  162. * replies available
  163. * @return object Replies information
  164. */
  165. function bbp_replies() {
  166. // Put into variable to check against next
  167. $have_posts = bbpress()->reply_query->have_posts();
  168. // Reset the post data when finished
  169. if ( empty( $have_posts ) )
  170. wp_reset_postdata();
  171. return $have_posts;
  172. }
  173. /**
  174. * Loads up the current reply in the loop
  175. *
  176. * @since bbPress (r2553)
  177. *
  178. * @uses WP_Query bbPress::reply_query::the_post() To get the current reply
  179. * @return object Reply information
  180. */
  181. function bbp_the_reply() {
  182. return bbpress()->reply_query->the_post();
  183. }
  184. /**
  185. * Output reply id
  186. *
  187. * @since bbPress (r2553)
  188. *
  189. * @param $reply_id Optional. Used to check emptiness
  190. * @uses bbp_get_reply_id() To get the reply id
  191. */
  192. function bbp_reply_id( $reply_id = 0 ) {
  193. echo bbp_get_reply_id( $reply_id );
  194. }
  195. /**
  196. * Return the id of the reply in a replies loop
  197. *
  198. * @since bbPress (r2553)
  199. *
  200. * @param $reply_id Optional. Used to check emptiness
  201. * @uses bbPress::reply_query::post::ID To get the reply id
  202. * @uses bbp_is_reply() To check if the search result is a reply
  203. * @uses bbp_is_single_reply() To check if it's a reply page
  204. * @uses bbp_is_reply_edit() To check if it's a reply edit page
  205. * @uses get_post_field() To get the post's post type
  206. * @uses WP_Query::post::ID To get the reply id
  207. * @uses bbp_get_reply_post_type() To get the reply post type
  208. * @uses apply_filters() Calls 'bbp_get_reply_id' with the reply id and
  209. * supplied reply id
  210. * @return int The reply id
  211. */
  212. function bbp_get_reply_id( $reply_id = 0 ) {
  213. global $wp_query;
  214. $bbp = bbpress();
  215. // Easy empty checking
  216. if ( !empty( $reply_id ) && is_numeric( $reply_id ) ) {
  217. $bbp_reply_id = $reply_id;
  218. // Currently inside a replies loop
  219. } elseif ( !empty( $bbp->reply_query->in_the_loop ) && isset( $bbp->reply_query->post->ID ) ) {
  220. $bbp_reply_id = $bbp->reply_query->post->ID;
  221. // Currently inside a search loop
  222. } elseif ( !empty( $bbp->search_query->in_the_loop ) && isset( $bbp->search_query->post->ID ) && bbp_is_reply( $bbp->search_query->post->ID ) ) {
  223. $bbp_reply_id = $bbp->search_query->post->ID;
  224. // Currently viewing a forum
  225. } elseif ( ( bbp_is_single_reply() || bbp_is_reply_edit() ) && !empty( $bbp->current_reply_id ) ) {
  226. $bbp_reply_id = $bbp->current_reply_id;
  227. // Currently viewing a reply
  228. } elseif ( ( bbp_is_single_reply() || bbp_is_reply_edit() ) && isset( $wp_query->post->ID ) ) {
  229. $bbp_reply_id = $wp_query->post->ID;
  230. // Fallback
  231. } else {
  232. $bbp_reply_id = 0;
  233. }
  234. return (int) apply_filters( 'bbp_get_reply_id', $bbp_reply_id, $reply_id );
  235. }
  236. /**
  237. * Gets a reply
  238. *
  239. * @since bbPress (r2787)
  240. *
  241. * @param int|object $reply reply id or reply object
  242. * @param string $output Optional. OBJECT, ARRAY_A, or ARRAY_N. Default = OBJECT
  243. * @param string $filter Optional Sanitation filter. See {@link sanitize_post()}
  244. * @uses get_post() To get the reply
  245. * @uses bbp_get_reply_post_type() To get the reply post type
  246. * @uses apply_filters() Calls 'bbp_get_reply' with the reply, output type and
  247. * sanitation filter
  248. * @return mixed Null if error or reply (in specified form) if success
  249. */
  250. function bbp_get_reply( $reply, $output = OBJECT, $filter = 'raw' ) {
  251. if ( empty( $reply ) || is_numeric( $reply ) )
  252. $reply = bbp_get_reply_id( $reply );
  253. $reply = get_post( $reply, OBJECT, $filter );
  254. if ( empty( $reply ) )
  255. return $reply;
  256. if ( $reply->post_type !== bbp_get_reply_post_type() )
  257. return null;
  258. if ( $output == OBJECT ) {
  259. return $reply;
  260. } elseif ( $output == ARRAY_A ) {
  261. $_reply = get_object_vars( $reply );
  262. return $_reply;
  263. } elseif ( $output == ARRAY_N ) {
  264. $_reply = array_values( get_object_vars( $reply ) );
  265. return $_reply;
  266. }
  267. return apply_filters( 'bbp_get_reply', $reply, $output, $filter );
  268. }
  269. /**
  270. * Output the link to the reply in the reply loop
  271. *
  272. * @since bbPress (r2553)
  273. *
  274. * @param int $reply_id Optional. Reply id
  275. * @uses bbp_get_reply_permalink() To get the reply permalink
  276. */
  277. function bbp_reply_permalink( $reply_id = 0 ) {
  278. echo bbp_get_reply_permalink( $reply_id );
  279. }
  280. /**
  281. * Return the link to the reply
  282. *
  283. * @since bbPress (r2553)
  284. *
  285. * @param int $reply_id Optional. Reply id
  286. * @uses bbp_get_reply_id() To get the reply id
  287. * @uses get_permalink() To get the permalink of the reply
  288. * @uses apply_filters() Calls 'bbp_get_reply_permalink' with the link
  289. * and reply id
  290. * @return string Permanent link to reply
  291. */
  292. function bbp_get_reply_permalink( $reply_id = 0 ) {
  293. $reply_id = bbp_get_reply_id( $reply_id );
  294. return apply_filters( 'bbp_get_reply_permalink', get_permalink( $reply_id ), $reply_id );
  295. }
  296. /**
  297. * Output the paginated url to the reply in the reply loop
  298. *
  299. * @since bbPress (r2679)
  300. *
  301. * @param int $reply_id Optional. Reply id
  302. * @uses bbp_get_reply_url() To get the reply url
  303. */
  304. function bbp_reply_url( $reply_id = 0 ) {
  305. echo bbp_get_reply_url( $reply_id );
  306. }
  307. /**
  308. * Return the paginated url to the reply in the reply loop
  309. *
  310. * @since bbPress (r2679)
  311. *
  312. * @param int $reply_id Optional. Reply id
  313. * @param $string $redirect_to Optional. Pass a redirect value for use with
  314. * shortcodes and other fun things.
  315. * @uses bbp_get_reply_id() To get the reply id
  316. * @uses bbp_get_reply_topic_id() To get the reply topic id
  317. * @uses bbp_get_topic_permalink() To get the topic permalink
  318. * @uses bbp_get_reply_position() To get the reply position
  319. * @uses get_option() To get the replies per page option
  320. * @uses WP_Rewrite::using_permalinks() To check if the blog uses
  321. * permalinks
  322. * @uses add_query_arg() To add custom args to the url
  323. * @uses apply_filters() Calls 'bbp_get_reply_url' with the reply url,
  324. * reply id and bool count hidden
  325. * @return string Link to reply relative to paginated topic
  326. */
  327. function bbp_get_reply_url( $reply_id = 0, $redirect_to = '' ) {
  328. // Set needed variables
  329. $reply_id = bbp_get_reply_id ( $reply_id );
  330. $topic_id = bbp_get_reply_topic_id( $reply_id );
  331. $reply_page = ceil( (int) bbp_get_reply_position( $reply_id, $topic_id ) / (int) bbp_get_replies_per_page() );
  332. $reply_hash = '#post-' . $reply_id;
  333. $topic_link = bbp_get_topic_permalink( $topic_id, $redirect_to );
  334. $topic_url = remove_query_arg( 'view', $topic_link );
  335. // Don't include pagination if on first page
  336. if ( 1 >= $reply_page ) {
  337. $url = trailingslashit( $topic_url ) . $reply_hash;
  338. // Include pagination
  339. } else {
  340. global $wp_rewrite;
  341. // Pretty permalinks
  342. if ( $wp_rewrite->using_permalinks() ) {
  343. $url = trailingslashit( $topic_url ) . trailingslashit( $wp_rewrite->pagination_base ) . trailingslashit( $reply_page ) . $reply_hash;
  344. // Yucky links
  345. } else {
  346. $url = add_query_arg( 'paged', $reply_page, $topic_url ) . $reply_hash;
  347. }
  348. }
  349. // Add topic view query arg back to end if it is set
  350. if ( bbp_get_view_all() )
  351. $url = bbp_add_view_all( $url );
  352. return apply_filters( 'bbp_get_reply_url', $url, $reply_id, $redirect_to );
  353. }
  354. /**
  355. * Output the title of the reply
  356. *
  357. * @since bbPress (r2553)
  358. *
  359. * @param int $reply_id Optional. Reply id
  360. * @uses bbp_get_reply_title() To get the reply title
  361. */
  362. function bbp_reply_title( $reply_id = 0 ) {
  363. echo bbp_get_reply_title( $reply_id );
  364. }
  365. /**
  366. * Return the title of the reply
  367. *
  368. * @since bbPress (r2553)
  369. *
  370. * @param int $reply_id Optional. Reply id
  371. * @uses bbp_get_reply_id() To get the reply id
  372. * @uses get_the_title() To get the reply title
  373. * @uses apply_filters() Calls 'bbp_get_reply_title' with the title and
  374. * reply id
  375. * @return string Title of reply
  376. */
  377. function bbp_get_reply_title( $reply_id = 0 ) {
  378. $reply_id = bbp_get_reply_id( $reply_id );
  379. return apply_filters( 'bbp_get_reply_title', get_the_title( $reply_id ), $reply_id );
  380. }
  381. /**
  382. * Output the content of the reply
  383. *
  384. * @since bbPress (r2553)
  385. *
  386. * @param int $reply_id Optional. reply id
  387. * @uses bbp_get_reply_content() To get the reply content
  388. */
  389. function bbp_reply_content( $reply_id = 0 ) {
  390. echo bbp_get_reply_content( $reply_id );
  391. }
  392. /**
  393. * Return the content of the reply
  394. *
  395. * @since bbPress (r2780)
  396. *
  397. * @param int $reply_id Optional. reply id
  398. * @uses bbp_get_reply_id() To get the reply id
  399. * @uses post_password_required() To check if the reply requires pass
  400. * @uses get_the_password_form() To get the password form
  401. * @uses get_post_field() To get the content post field
  402. * @uses apply_filters() Calls 'bbp_get_reply_content' with the content
  403. * and reply id
  404. * @return string Content of the reply
  405. */
  406. function bbp_get_reply_content( $reply_id = 0 ) {
  407. $reply_id = bbp_get_reply_id( $reply_id );
  408. // Check if password is required
  409. if ( post_password_required( $reply_id ) )
  410. return get_the_password_form();
  411. $content = get_post_field( 'post_content', $reply_id );
  412. return apply_filters( 'bbp_get_reply_content', $content, $reply_id );
  413. }
  414. /**
  415. * Output the excerpt of the reply
  416. *
  417. * @since bbPress (r2751)
  418. *
  419. * @param int $reply_id Optional. Reply id
  420. * @param int $length Optional. Length of the excerpt. Defaults to 100 letters
  421. * @uses bbp_get_reply_excerpt() To get the reply excerpt
  422. */
  423. function bbp_reply_excerpt( $reply_id = 0, $length = 100 ) {
  424. echo bbp_get_reply_excerpt( $reply_id, $length );
  425. }
  426. /**
  427. * Return the excerpt of the reply
  428. *
  429. * @since bbPress (r2751)
  430. *
  431. * @param int $reply_id Optional. Reply id
  432. * @param int $length Optional. Length of the excerpt. Defaults to 100
  433. * letters
  434. * @uses bbp_get_reply_id() To get the reply id
  435. * @uses get_post_field() To get the excerpt
  436. * @uses bbp_get_reply_content() To get the reply content
  437. * @uses apply_filters() Calls 'bbp_get_reply_excerpt' with the excerpt,
  438. * reply id and length
  439. * @return string Reply Excerpt
  440. */
  441. function bbp_get_reply_excerpt( $reply_id = 0, $length = 100 ) {
  442. $reply_id = bbp_get_reply_id( $reply_id );
  443. $length = (int) $length;
  444. $excerpt = get_post_field( 'post_excerpt', $reply_id );
  445. if ( empty( $excerpt ) ) {
  446. $excerpt = bbp_get_reply_content( $reply_id );
  447. }
  448. $excerpt = trim ( strip_tags( $excerpt ) );
  449. // Multibyte support
  450. if ( function_exists( 'mb_strlen' ) ) {
  451. $excerpt_length = mb_strlen( $excerpt );
  452. } else {
  453. $excerpt_length = strlen( $excerpt );
  454. }
  455. if ( !empty( $length ) && ( $excerpt_length > $length ) ) {
  456. $excerpt = substr( $excerpt, 0, $length - 1 );
  457. $excerpt .= '&hellip;';
  458. }
  459. return apply_filters( 'bbp_get_reply_excerpt', $excerpt, $reply_id, $length );
  460. }
  461. /**
  462. * Output the post date and time of a reply
  463. *
  464. * @since bbPress (r4155)
  465. *
  466. * @param int $reply_id Optional. Reply id.
  467. * @param bool $humanize Optional. Humanize output using time_since
  468. * @param bool $gmt Optional. Use GMT
  469. * @uses bbp_get_reply_post_date() to get the output
  470. */
  471. function bbp_reply_post_date( $reply_id = 0, $humanize = false, $gmt = false ) {
  472. echo bbp_get_reply_post_date( $reply_id, $humanize, $gmt );
  473. }
  474. /**
  475. * Return the post date and time of a reply
  476. *
  477. * @since bbPress (r4155)
  478. *
  479. * @param int $reply_id Optional. Reply id.
  480. * @param bool $humanize Optional. Humanize output using time_since
  481. * @param bool $gmt Optional. Use GMT
  482. * @uses bbp_get_reply_id() To get the reply id
  483. * @uses get_post_time() to get the reply post time
  484. * @uses bbp_get_time_since() to maybe humanize the reply post time
  485. * @return string
  486. */
  487. function bbp_get_reply_post_date( $reply_id = 0, $humanize = false, $gmt = false ) {
  488. $reply_id = bbp_get_reply_id( $reply_id );
  489. // 4 days, 4 hours ago
  490. if ( !empty( $humanize ) ) {
  491. $gmt_s = !empty( $gmt ) ? 'G' : 'U';
  492. $date = get_post_time( $gmt_s, $gmt, $reply_id );
  493. $time = false; // For filter below
  494. $result = bbp_get_time_since( $date );
  495. // August 4, 2012 at 2:37 pm
  496. } else {
  497. $date = get_post_time( get_option( 'date_format' ), $gmt, $reply_id, true );
  498. $time = get_post_time( get_option( 'time_format' ), $gmt, $reply_id, true );
  499. $result = sprintf( _x( '%1$s at %2$s', 'date at time', 'bbpress' ), $date, $time );
  500. }
  501. return apply_filters( 'bbp_get_reply_post_date', $result, $reply_id, $humanize, $gmt, $date, $time );
  502. }
  503. /**
  504. * Append revisions to the reply content
  505. *
  506. * @since bbPress (r2782)
  507. *
  508. * @param string $content Optional. Content to which we need to append the revisions to
  509. * @param int $reply_id Optional. Reply id
  510. * @uses bbp_get_reply_revision_log() To get the reply revision log
  511. * @uses apply_filters() Calls 'bbp_reply_append_revisions' with the processed
  512. * content, original content and reply id
  513. * @return string Content with the revisions appended
  514. */
  515. function bbp_reply_content_append_revisions( $content = '', $reply_id = 0 ) {
  516. // Bail if in admin or feed
  517. if ( is_admin() || is_feed() )
  518. return $content;
  519. // Validate the ID
  520. $reply_id = bbp_get_reply_id( $reply_id );
  521. return apply_filters( 'bbp_reply_append_revisions', $content . bbp_get_reply_revision_log( $reply_id ), $content, $reply_id );
  522. }
  523. /**
  524. * Output the revision log of the reply
  525. *
  526. * @since bbPress (r2782)
  527. *
  528. * @param int $reply_id Optional. Reply id
  529. * @uses bbp_get_reply_revision_log() To get the reply revision log
  530. */
  531. function bbp_reply_revision_log( $reply_id = 0 ) {
  532. echo bbp_get_reply_revision_log( $reply_id );
  533. }
  534. /**
  535. * Return the formatted revision log of the reply
  536. *
  537. * @since bbPress (r2782)
  538. *
  539. * @param int $reply_id Optional. Reply id
  540. * @uses bbp_get_reply_id() To get the reply id
  541. * @uses bbp_get_reply_revisions() To get the reply revisions
  542. * @uses bbp_get_reply_raw_revision_log() To get the raw revision log
  543. * @uses bbp_get_reply_author_display_name() To get the reply author
  544. * @uses bbp_get_reply_author_link() To get the reply author link
  545. * @uses bbp_convert_date() To convert the date
  546. * @uses bbp_get_time_since() To get the time in since format
  547. * @uses apply_filters() Calls 'bbp_get_reply_revision_log' with the
  548. * log and reply id
  549. * @return string Revision log of the reply
  550. */
  551. function bbp_get_reply_revision_log( $reply_id = 0 ) {
  552. // Create necessary variables
  553. $reply_id = bbp_get_reply_id( $reply_id );
  554. // Show the topic reply log if this is a topic in a reply loop
  555. if ( bbp_is_topic( $reply_id ) ) {
  556. return bbp_get_topic_revision_log( $reply_id );
  557. }
  558. // Get the reply revision log (out of post meta
  559. $revision_log = bbp_get_reply_raw_revision_log( $reply_id );
  560. // Check reply and revision log exist
  561. if ( empty( $reply_id ) || empty( $revision_log ) || !is_array( $revision_log ) )
  562. return false;
  563. // Get the actual revisions
  564. $revisions = bbp_get_reply_revisions( $reply_id );
  565. if ( empty( $revisions ) )
  566. return false;
  567. $r = "\n\n" . '<ul id="bbp-reply-revision-log-' . $reply_id . '" class="bbp-reply-revision-log">' . "\n\n";
  568. // Loop through revisions
  569. foreach ( (array) $revisions as $revision ) {
  570. if ( empty( $revision_log[$revision->ID] ) ) {
  571. $author_id = $revision->post_author;
  572. $reason = '';
  573. } else {
  574. $author_id = $revision_log[$revision->ID]['author'];
  575. $reason = $revision_log[$revision->ID]['reason'];
  576. }
  577. $author = bbp_get_author_link( array( 'size' => 14, 'link_text' => bbp_get_reply_author_display_name( $revision->ID ), 'post_id' => $revision->ID ) );
  578. $since = bbp_get_time_since( bbp_convert_date( $revision->post_modified ) );
  579. $r .= "\t" . '<li id="bbp-reply-revision-log-' . $reply_id . '-item-' . $revision->ID . '" class="bbp-reply-revision-log-item">' . "\n";
  580. if ( !empty( $reason ) ) {
  581. $r .= "\t\t" . sprintf( __( 'This reply was modified %1$s by %2$s. Reason: %3$s', 'bbpress' ), $since, $author, $reason ) . "\n";
  582. } else {
  583. $r .= "\t\t" . sprintf( __( 'This reply was modified %1$s by %2$s.', 'bbpress' ), $since, $author ) . "\n";
  584. }
  585. $r .= "\t" . '</li>' . "\n";
  586. }
  587. $r .= "\n" . '</ul>' . "\n\n";
  588. return apply_filters( 'bbp_get_reply_revision_log', $r, $reply_id );
  589. }
  590. /**
  591. * Return the raw revision log of the reply
  592. *
  593. * @since bbPress (r2782)
  594. *
  595. * @param int $reply_id Optional. Reply id
  596. * @uses bbp_get_reply_id() To get the reply id
  597. * @uses get_post_meta() To get the revision log meta
  598. * @uses apply_filters() Calls 'bbp_get_reply_raw_revision_log'
  599. * with the log and reply id
  600. * @return string Raw revision log of the reply
  601. */
  602. function bbp_get_reply_raw_revision_log( $reply_id = 0 ) {
  603. $reply_id = bbp_get_reply_id( $reply_id );
  604. $revision_log = get_post_meta( $reply_id, '_bbp_revision_log', true );
  605. $revision_log = empty( $revision_log ) ? array() : $revision_log;
  606. return apply_filters( 'bbp_get_reply_raw_revision_log', $revision_log, $reply_id );
  607. }
  608. /**
  609. * Return the revisions of the reply
  610. *
  611. * @since bbPress (r2782)
  612. *
  613. * @param int $reply_id Optional. Reply id
  614. * @uses bbp_get_reply_id() To get the reply id
  615. * @uses wp_get_post_revisions() To get the reply revisions
  616. * @uses apply_filters() Calls 'bbp_get_reply_revisions'
  617. * with the revisions and reply id
  618. * @return string reply revisions
  619. */
  620. function bbp_get_reply_revisions( $reply_id = 0 ) {
  621. $reply_id = bbp_get_reply_id( $reply_id );
  622. $revisions = wp_get_post_revisions( $reply_id, array( 'order' => 'ASC' ) );
  623. return apply_filters( 'bbp_get_reply_revisions', $revisions, $reply_id );
  624. }
  625. /**
  626. * Return the revision count of the reply
  627. *
  628. * @since bbPress (r2782)
  629. *
  630. * @param int $reply_id Optional. Reply id
  631. * @param boolean $integer Optional. Whether or not to format the result
  632. * @uses bbp_get_reply_revisions() To get the reply revisions
  633. * @uses apply_filters() Calls 'bbp_get_reply_revision_count'
  634. * with the revision count and reply id
  635. * @return string reply revision count
  636. */
  637. function bbp_get_reply_revision_count( $reply_id = 0, $integer = false ) {
  638. $count = (int) count( bbp_get_reply_revisions( $reply_id ) );
  639. $filter = ( true === $integer ) ? 'bbp_get_reply_revision_count_int' : 'bbp_get_reply_revision_count';
  640. return apply_filters( $filter, $count, $reply_id );
  641. }
  642. /**
  643. * Output the status of the reply
  644. *
  645. * @since bbPress (r2667)
  646. *
  647. * @param int $reply_id Optional. Reply id
  648. * @uses bbp_get_reply_status() To get the reply status
  649. */
  650. function bbp_reply_status( $reply_id = 0 ) {
  651. echo bbp_get_reply_status( $reply_id );
  652. }
  653. /**
  654. * Return the status of the reply
  655. *
  656. * @since bbPress (r2667)
  657. *
  658. * @param int $reply_id Optional. Reply id
  659. * @uses bbp_get_reply_id() To get the reply id
  660. * @uses get_post_status() To get the reply status
  661. * @uses apply_filters() Calls 'bbp_get_reply_status' with the reply id
  662. * @return string Status of reply
  663. */
  664. function bbp_get_reply_status( $reply_id = 0 ) {
  665. $reply_id = bbp_get_reply_id( $reply_id );
  666. return apply_filters( 'bbp_get_reply_status', get_post_status( $reply_id ), $reply_id );
  667. }
  668. /**
  669. * Is the reply not spam or deleted?
  670. *
  671. * @since bbPress (r3496)
  672. *
  673. * @param int $reply_id Optional. Topic id
  674. * @uses bbp_get_reply_id() To get the reply id
  675. * @uses bbp_get_reply_status() To get the reply status
  676. * @return bool True if published, false if not.
  677. */
  678. function bbp_is_reply_published( $reply_id = 0 ) {
  679. $reply_status = bbp_get_reply_status( bbp_get_reply_id( $reply_id ) ) == bbp_get_public_status_id();
  680. return (bool) apply_filters( 'bbp_is_reply_published', (bool) $reply_status, $reply_id );
  681. }
  682. /**
  683. * Is the reply marked as spam?
  684. *
  685. * @since bbPress (r2740)
  686. *
  687. * @param int $reply_id Optional. Reply id
  688. * @uses bbp_get_reply_id() To get the reply id
  689. * @uses bbp_get_reply_status() To get the reply status
  690. * @return bool True if spam, false if not.
  691. */
  692. function bbp_is_reply_spam( $reply_id = 0 ) {
  693. $reply_status = bbp_get_reply_status( bbp_get_reply_id( $reply_id ) ) == bbp_get_spam_status_id();
  694. return (bool) apply_filters( 'bbp_is_reply_spam', (bool) $reply_status, $reply_id );
  695. }
  696. /**
  697. * Is the reply trashed?
  698. *
  699. * @since bbPress (r2884)
  700. *
  701. * @param int $reply_id Optional. Topic id
  702. * @uses bbp_get_reply_id() To get the reply id
  703. * @uses bbp_get_reply_status() To get the reply status
  704. * @return bool True if spam, false if not.
  705. */
  706. function bbp_is_reply_trash( $reply_id = 0 ) {
  707. $reply_status = bbp_get_reply_status( bbp_get_reply_id( $reply_id ) ) == bbp_get_trash_status_id();
  708. return (bool) apply_filters( 'bbp_is_reply_trash', (bool) $reply_status, $reply_id );
  709. }
  710. /**
  711. * Is the reply by an anonymous user?
  712. *
  713. * @since bbPress (r2753)
  714. *
  715. * @param int $reply_id Optional. Reply id
  716. * @uses bbp_get_reply_id() To get the reply id
  717. * @uses bbp_get_reply_author_id() To get the reply author id
  718. * @uses get_post_meta() To get the anonymous name and email metas
  719. * @return bool True if the post is by an anonymous user, false if not.
  720. */
  721. function bbp_is_reply_anonymous( $reply_id = 0 ) {
  722. $reply_id = bbp_get_reply_id( $reply_id );
  723. $retval = false;
  724. if ( !bbp_get_reply_author_id( $reply_id ) )
  725. $retval = true;
  726. elseif ( get_post_meta( $reply_id, '_bbp_anonymous_name', true ) )
  727. $retval = true;
  728. elseif ( get_post_meta( $reply_id, '_bbp_anonymous_email', true ) )
  729. $retval = true;
  730. return (bool) apply_filters( 'bbp_is_reply_anonymous', $retval, $reply_id );
  731. }
  732. /**
  733. * Output the author of the reply
  734. *
  735. * @since bbPress (r2667)
  736. *
  737. * @param int $reply_id Optional. Reply id
  738. * @uses bbp_get_reply_author() To get the reply author
  739. */
  740. function bbp_reply_author( $reply_id = 0 ) {
  741. echo bbp_get_reply_author( $reply_id );
  742. }
  743. /**
  744. * Return the author of the reply
  745. *
  746. * @since bbPress (r2667)
  747. *
  748. * @param int $reply_id Optional. Reply id
  749. * @uses bbp_get_reply_id() To get the reply id
  750. * @uses bbp_is_reply_anonymous() To check if the reply is by an
  751. * anonymous user
  752. * @uses get_the_author_meta() To get the reply author display name
  753. * @uses get_post_meta() To get the anonymous poster name
  754. * @uses apply_filters() Calls 'bbp_get_reply_author' with the reply
  755. * author and reply id
  756. * @return string Author of reply
  757. */
  758. function bbp_get_reply_author( $reply_id = 0 ) {
  759. $reply_id = bbp_get_reply_id( $reply_id );
  760. if ( !bbp_is_reply_anonymous( $reply_id ) ) {
  761. $author = get_the_author_meta( 'display_name', bbp_get_reply_author_id( $reply_id ) );
  762. } else {
  763. $author = get_post_meta( $reply_id, '_bbp_anonymous_name', true );
  764. }
  765. return apply_filters( 'bbp_get_reply_author', $author, $reply_id );
  766. }
  767. /**
  768. * Output the author ID of the reply
  769. *
  770. * @since bbPress (r2667)
  771. *
  772. * @param int $reply_id Optional. Reply id
  773. * @uses bbp_get_reply_author_id() To get the reply author id
  774. */
  775. function bbp_reply_author_id( $reply_id = 0 ) {
  776. echo bbp_get_reply_author_id( $reply_id );
  777. }
  778. /**
  779. * Return the author ID of the reply
  780. *
  781. * @since bbPress (r2667)
  782. *
  783. * @param int $reply_id Optional. Reply id
  784. * @uses bbp_get_reply_id() To get the reply id
  785. * @uses get_post_field() To get the reply author id
  786. * @uses apply_filters() Calls 'bbp_get_reply_author_id' with the author
  787. * id and reply id
  788. * @return string Author id of reply
  789. */
  790. function bbp_get_reply_author_id( $reply_id = 0 ) {
  791. $reply_id = bbp_get_reply_id( $reply_id );
  792. $author_id = get_post_field( 'post_author', $reply_id );
  793. return (int) apply_filters( 'bbp_get_reply_author_id', $author_id, $reply_id );
  794. }
  795. /**
  796. * Output the author display_name of the reply
  797. *
  798. * @since bbPress (r2667)
  799. *
  800. * @param int $reply_id Optional. Reply id
  801. * @uses bbp_get_reply_author_display_name()
  802. */
  803. function bbp_reply_author_display_name( $reply_id = 0 ) {
  804. echo bbp_get_reply_author_display_name( $reply_id );
  805. }
  806. /**
  807. * Return the author display_name of the reply
  808. *
  809. * @since bbPress (r2667)
  810. *
  811. * @param int $reply_id Optional. Reply id
  812. * @uses bbp_get_reply_id() To get the reply id
  813. * @uses bbp_is_reply_anonymous() To check if the reply is by an
  814. * anonymous user
  815. * @uses bbp_get_reply_author_id() To get the reply author id
  816. * @uses get_the_author_meta() To get the reply author's display name
  817. * @uses get_post_meta() To get the anonymous poster's name
  818. * @uses apply_filters() Calls 'bbp_get_reply_author_display_name' with
  819. * the author display name and reply id
  820. * @return string Reply's author's display name
  821. */
  822. function bbp_get_reply_author_display_name( $reply_id = 0 ) {
  823. $reply_id = bbp_get_reply_id( $reply_id );
  824. // User is not a guest
  825. if ( !bbp_is_reply_anonymous( $reply_id ) ) {
  826. // Get the author ID
  827. $author_id = bbp_get_reply_author_id( $reply_id );
  828. // Try to get a display name
  829. $author_name = get_the_author_meta( 'display_name', $author_id );
  830. // Fall back to user login
  831. if ( empty( $author_name ) ) {
  832. $author_name = get_the_author_meta( 'user_login', $author_id );
  833. }
  834. // User does not have an account
  835. } else {
  836. $author_name = get_post_meta( $reply_id, '_bbp_anonymous_name', true );
  837. }
  838. // If nothing could be found anywhere, use Anonymous
  839. if ( empty( $author_name ) )
  840. $author_name = __( 'Anonymous', 'bbpress' );
  841. // Encode possible UTF8 display names
  842. if ( seems_utf8( $author_name ) === false )
  843. $author_name = utf8_encode( $author_name );
  844. return apply_filters( 'bbp_get_reply_author_display_name', $author_name, $reply_id );
  845. }
  846. /**
  847. * Output the author avatar of the reply
  848. *
  849. * @since bbPress (r2667)
  850. *
  851. * @param int $reply_id Optional. Reply id
  852. * @param int $size Optional. Size of the avatar. Defaults to 40
  853. * @uses bbp_get_reply_author_avatar() To get the reply author id
  854. */
  855. function bbp_reply_author_avatar( $reply_id = 0, $size = 40 ) {
  856. echo bbp_get_reply_author_avatar( $reply_id, $size );
  857. }
  858. /**
  859. * Return the author avatar of the reply
  860. *
  861. * @since bbPress (r2667)
  862. *
  863. * @param int $reply_id Optional. Reply id
  864. * @param int $size Optional. Size of the avatar. Defaults to 40
  865. * @uses bbp_get_reply_id() To get the reply id
  866. * @uses bbp_is_reply_anonymous() To check if the reply is by an
  867. * anonymous user
  868. * @uses bbp_get_reply_author_id() To get the reply author id
  869. * @uses get_post_meta() To get the anonymous poster's email id
  870. * @uses get_avatar() To get the avatar
  871. * @uses apply_filters() Calls 'bbp_get_reply_author_avatar' with the
  872. * author avatar, reply id and size
  873. * @return string Avatar of author of the reply
  874. */
  875. function bbp_get_reply_author_avatar( $reply_id = 0, $size = 40 ) {
  876. $reply_id = bbp_get_reply_id( $reply_id );
  877. if ( !empty( $reply_id ) ) {
  878. // Check for anonymous user
  879. if ( !bbp_is_reply_anonymous( $reply_id ) ) {
  880. $author_avatar = get_avatar( bbp_get_reply_author_id( $reply_id ), $size );
  881. } else {
  882. $author_avatar = get_avatar( get_post_meta( $reply_id, '_bbp_anonymous_email', true ), $size );
  883. }
  884. } else {
  885. $author_avatar = '';
  886. }
  887. return apply_filters( 'bbp_get_reply_author_avatar', $author_avatar, $reply_id, $size );
  888. }
  889. /**
  890. * Output the author link of the reply
  891. *
  892. * @since bbPress (r2717)
  893. *
  894. * @param mixed $args Optional. If it is an integer, it is used as reply id.
  895. * @uses bbp_get_reply_author_link() To get the reply author link
  896. */
  897. function bbp_reply_author_link( $args = '' ) {
  898. echo bbp_get_reply_author_link( $args );
  899. }
  900. /**
  901. * Return the author link of the reply
  902. *
  903. * @since bbPress (r2717)
  904. *
  905. * @param mixed $args Optional. If an integer, it is used as reply id.
  906. * @uses bbp_get_reply_id() To get the reply id
  907. * @uses bbp_is_reply_anonymous() To check if the reply is by an
  908. * anonymous user
  909. * @uses bbp_get_reply_author() To get the reply author name
  910. * @uses bbp_get_reply_author_url() To get the reply author url
  911. * @uses bbp_get_reply_author_avatar() To get the reply author avatar
  912. * @uses bbp_get_reply_author_display_name() To get the reply author display
  913. * name
  914. * @uses bbp_get_user_display_role() To get the reply author display role
  915. * @uses bbp_get_reply_author_id() To get the reply author id
  916. * @uses apply_filters() Calls 'bbp_get_reply_author_link' with the
  917. * author link and args
  918. * @return string Author link of reply
  919. */
  920. function bbp_get_reply_author_link( $args = '' ) {
  921. // Parse arguments against default values
  922. $r = bbp_parse_args( $args, array(
  923. 'post_id' => 0,
  924. 'link_title' => '',
  925. 'type' => 'both',
  926. 'size' => 80,
  927. 'sep' => '&nbsp;',
  928. 'show_role' => false
  929. ), 'get_reply_author_link' );
  930. // Used as reply_id
  931. if ( is_numeric( $args ) ) {
  932. $reply_id = bbp_get_reply_id( $args );
  933. } else {
  934. $reply_id = bbp_get_reply_id( $r['post_id'] );
  935. }
  936. // Reply ID is good
  937. if ( !empty( $reply_id ) ) {
  938. // Get some useful reply information
  939. $author_url = bbp_get_reply_author_url( $reply_id );
  940. $anonymous = bbp_is_reply_anonymous( $reply_id );
  941. // Tweak link title if empty
  942. if ( empty( $$r['link_title'] ) ) {
  943. $link_title = sprintf( empty( $anonymous ) ? __( 'View %s\'s profile', 'bbpress' ) : __( 'Visit %s\'s website', 'bbpress' ), bbp_get_reply_author_display_name( $reply_id ) );
  944. // Use what was passed if not
  945. } else {
  946. $link_title = $r['link_title'];
  947. }
  948. // Setup title and author_links array
  949. $link_title = !empty( $link_title ) ? ' title="' . $link_title . '"' : '';
  950. $author_links = array();
  951. // Get avatar
  952. if ( 'avatar' == $r['type'] || 'both' == $r['type'] ) {
  953. $author_links['avatar'] = bbp_get_reply_author_avatar( $reply_id, $r['size'] );
  954. }
  955. // Get display name
  956. if ( 'name' == $r['type'] || 'both' == $r['type'] ) {
  957. $author_links['name'] = bbp_get_reply_author_display_name( $reply_id );
  958. }
  959. // Link class
  960. $link_class = ' class="bbp-author-' . $r['type'] . '"';
  961. // Add links if not anonymous
  962. if ( empty( $anonymous ) && bbp_user_has_profile( bbp_get_reply_author_id( $reply_id ) ) ) {
  963. // Assemble the links
  964. foreach ( $author_links as $link => $link_text ) {
  965. $link_class = ' class="bbp-author-' . $link . '"';
  966. $author_link[] = sprintf( '<a href="%1$s"%2$s%3$s>%4$s</a>', $author_url, $link_title, $link_class, $link_text );
  967. }
  968. if ( true === $r['show_role'] ) {
  969. $author_link[] = bbp_get_reply_author_role( array( 'reply_id' => $reply_id ) );
  970. }
  971. $author_link = join( $r['sep'], $author_link );
  972. // No links if anonymous
  973. } else {
  974. $author_link = join( $r['sep'], $author_links );
  975. }
  976. // No replies so link is empty
  977. } else {
  978. $author_link = '';
  979. }
  980. return apply_filters( 'bbp_get_reply_author_link', $author_link, $r );
  981. }
  982. /**
  983. * Output the author url of the reply
  984. *
  985. * @since bbPress (r2667)
  986. *
  987. * @param int $reply_id Optional. Reply id
  988. * @uses bbp_get_reply_author_url() To get the reply author url
  989. */
  990. function bbp_reply_author_url( $reply_id = 0 ) {
  991. echo bbp_get_reply_author_url( $reply_id );
  992. }
  993. /**
  994. * Return the author url of the reply
  995. *
  996. * @since bbPress (r2667)
  997. *
  998. * @param int $reply_id Optional. Reply id
  999. * @uses bbp_get_reply_id() To get the reply id
  1000. * @uses bbp_is_reply_anonymous() To check if the reply is by an anonymous
  1001. * user
  1002. * @uses bbp_get_reply_author_id() To get the reply author id
  1003. * @uses bbp_get_user_profile_url() To get the user profile url
  1004. * @uses get_post_meta() To get the anonymous poster's website url
  1005. * @uses apply_filters() Calls bbp_get_reply_author_url with the author
  1006. * url & reply id
  1007. * @return string Author URL of the reply
  1008. */
  1009. function bbp_get_reply_author_url( $reply_id = 0 ) {
  1010. $reply_id = bbp_get_reply_id( $reply_id );
  1011. // Check for anonymous user
  1012. if ( !bbp_is_reply_anonymous( $reply_id ) ) {
  1013. $author_url = bbp_get_user_profile_url( bbp_get_reply_author_id( $reply_id ) );
  1014. } else {
  1015. $author_url = get_post_meta( $reply_id, '_bbp_anonymous_website', true );
  1016. if ( empty( $author_url ) ) {
  1017. $author_url = '';
  1018. }
  1019. }
  1020. return apply_filters( 'bbp_get_reply_author_url', $author_url, $reply_id );
  1021. }
  1022. /**
  1023. * Output the reply author email address
  1024. *
  1025. * @since bbPress (r3445)
  1026. *
  1027. * @param int $reply_id Optional. Reply id
  1028. * @uses bbp_get_reply_author_email() To get the reply author email
  1029. */
  1030. function bbp_reply_author_email( $reply_id = 0 ) {
  1031. echo bbp_get_reply_author_email( $reply_id );
  1032. }
  1033. /**
  1034. * Return the reply author email address
  1035. *
  1036. * @since bbPress (r3445)
  1037. *
  1038. * @param int $reply_id Optional. Reply id
  1039. * @uses bbp_get_reply_id() To get the reply id
  1040. * @uses bbp_is_reply_anonymous() To check if the reply is by an anonymous
  1041. * user
  1042. * @uses bbp_get_reply_author_id() To get the reply author id
  1043. * @uses get_userdata() To get the user data
  1044. * @uses get_post_meta() To get the anonymous poster's website email
  1045. * @uses apply_filters() Calls bbp_get_reply_author_email with the author
  1046. * email & reply id
  1047. * @return string Reply author email address
  1048. */
  1049. function bbp_get_reply_author_email( $reply_id = 0 ) {
  1050. $reply_id = bbp_get_reply_id( $reply_id );
  1051. // Not anonymous
  1052. if ( !bbp_is_reply_anonymous( $reply_id ) ) {
  1053. // Use reply author email address
  1054. $user_id = bbp_get_reply_author_id( $reply_id );
  1055. $user = get_userdata( $user_id );
  1056. $author_email = !empty( $user->user_email ) ? $user->user_email : '';
  1057. // Anonymous
  1058. } else {
  1059. // Get email from post meta
  1060. $author_email = get_post_meta( $reply_id, '_bbp_anonymous_email', true );
  1061. // Sanity check for missing email address
  1062. if ( empty( $author_email ) ) {
  1063. $author_email = '';
  1064. }
  1065. }
  1066. return apply_filters( 'bbp_get_reply_author_email', $author_email, $reply_id );
  1067. }
  1068. /**
  1069. * Output the reply author role
  1070. *
  1071. * @since bbPress (r3860)
  1072. *
  1073. * @param array $args Optional.
  1074. * @uses bbp_get_reply_author_role() To get the reply author role
  1075. */
  1076. function bbp_reply_author_role( $args = array() ) {
  1077. echo bbp_get_reply_author_role( $args );
  1078. }
  1079. /**
  1080. * Return the reply author role
  1081. *
  1082. * @since bbPress (r3860)
  1083. *
  1084. * @param array $args Optional.
  1085. * @uses bbp_get_reply_id() To get the reply id
  1086. * @uses bbp_get_user_display_role() To get the user display role
  1087. * @uses bbp_get_reply_author_id() To get the reply author id
  1088. * @uses apply_filters() Calls bbp_get_reply_author_role with the author
  1089. * role & args
  1090. * @return string Reply author role
  1091. */
  1092. function bbp_get_reply_author_role( $args = array() ) {
  1093. // Parse arguments against default values
  1094. $r = bbp_parse_args( $args, array(
  1095. 'reply_id' => 0,
  1096. 'class' => 'bbp-author-role',
  1097. 'before' => '',
  1098. 'after' => ''
  1099. ), 'get_reply_author_role' );
  1100. $reply_id = bbp_get_reply_id( $r['reply_id'] );
  1101. $role = bbp_get_user_display_role( bbp_get_reply_author_id( $reply_id ) );
  1102. $author_role = sprintf( '%1$s<div class="%2$s">%3$s</div>%4$s', $r['before'], $r['class'], $role, $r['after'] );
  1103. return apply_filters( 'bbp_get_reply_author_role', $author_role, $r );
  1104. }
  1105. /**
  1106. * Output the topic title a reply belongs to
  1107. *
  1108. * @since bbPress (r2553)
  1109. *
  1110. * @param int $reply_id Optional. Reply id
  1111. * @uses bbp_get_reply_topic_title() To get the reply topic title
  1112. */
  1113. function bbp_reply_topic_title( $reply_id = 0 ) {
  1114. echo bbp_get_reply_topic_title( $reply_id );
  1115. }
  1116. /**
  1117. * Return the topic title a reply belongs to
  1118. *
  1119. * @since bbPress (r2553)
  1120. *
  1121. * @param int $reply_id Optional. Reply id
  1122. * @uses bbp_get_reply_id() To get the reply id
  1123. * @uses bbp_get_reply_topic_id() To get the reply topic id
  1124. * @uses bbp_get_topic_title() To get the reply topic title
  1125. * @uses apply_filters() Calls 'bbp_get_reply_topic_title' with the
  1126. * topic title and reply id
  1127. * @return string Reply's topic's title
  1128. */
  1129. function bbp_get_reply_topic_title( $reply_id = 0 ) {
  1130. $reply_id = bbp_get_reply_id( $reply_id );
  1131. $topic_id = bbp_get_reply_topic_id( $reply_id );
  1132. return apply_filters( 'bbp_get_reply_topic_title', bbp_get_topic_title( $topic_id ), $reply_id );
  1133. }
  1134. /**
  1135. * Output the topic id a reply belongs to
  1136. *
  1137. * @since bbPress (r2553)
  1138. *
  1139. * @param int $reply_id Optional. Reply id
  1140. * @uses bbp_get_reply_topic_id() To get the reply topic id
  1141. */
  1142. function bbp_reply_topic_id( $reply_id = 0 ) {
  1143. echo bbp_get_reply_topic_id( $reply_id );
  1144. }
  1145. /**
  1146. * Return the topic id a reply belongs to
  1147. *
  1148. * @since bbPress (r2553)
  1149. *
  1150. * @param int $reply_id Optional. Reply id
  1151. * @uses bbp_get_reply_id() To get the reply id
  1152. * @uses get_post_meta() To get the reply topic id from meta
  1153. * @uses bbp_get_topic_id() To get the topic id
  1154. * @uses apply_filters() Calls 'bbp_get_reply_topic_id' with the topic
  1155. * id and reply id
  1156. * @return int Reply's topic id
  1157. */
  1158. function bbp_get_reply_topic_id( $reply_id = 0 ) {
  1159. // Assume there is no topic id
  1160. $topic_id = 0;
  1161. // Check that reply_id is valid
  1162. if ( $reply_id = bbp_get_reply_id( $reply_id ) )
  1163. // Get topic_id from reply
  1164. if ( $topic_id = get_post_meta( $reply_id, '_bbp_topic_id', true ) )
  1165. // Validate the topic_id
  1166. $topic_id = bbp_get_topic_id( $topic_id );
  1167. return (int) apply_filters( 'bbp_get_reply_topic_id', $topic_id, $reply_id );
  1168. }
  1169. /**
  1170. * Output the forum id a reply belongs to
  1171. *
  1172. * @since bbPress (r2679)
  1173. *
  1174. * @param int $reply_id Optional. Reply id
  1175. * @uses bbp_get_reply_forum_id() To get the reply forum id
  1176. */
  1177. function bbp_reply_forum_id( $reply_id = 0 ) {
  1178. echo bbp_get_reply_forum_id( $reply_id );
  1179. }
  1180. /**
  1181. * Return the forum id a reply belongs to
  1182. *
  1183. * @since bbPress (r2679)
  1184. *
  1185. * @param int $reply_id Optional. Reply id
  1186. * @uses bbp_get_reply_id() To get the reply id
  1187. * @uses get_post_meta() To get the reply forum id
  1188. * @uses apply_filters() Calls 'bbp_get_reply_forum_id' with the forum
  1189. * id and reply id
  1190. * @return int Reply's forum id
  1191. */
  1192. function bbp_get_reply_forum_id( $reply_id = 0 ) {
  1193. // Assume there is no forum
  1194. $forum_id = 0;
  1195. // Check that reply_id is valid
  1196. if ( $reply_id = bbp_get_reply_id( $reply_id ) )
  1197. // Get forum_id from reply
  1198. if ( $forum_id = get_post_meta( $reply_id, '_bbp_forum_id', true ) )
  1199. // Validate the forum_id
  1200. $forum_id = bbp_get_forum_id( $forum_id );
  1201. return (int) apply_filters( 'bbp_get_reply_forum_id', $forum_id, $reply_id );
  1202. }
  1203. /**
  1204. * Output the numeric position of a reply within a topic
  1205. *
  1206. * @since bbPress (r2984)
  1207. *
  1208. * @param int $reply_id Optional. Reply id
  1209. * @param int $topic_id Optional. Topic id
  1210. * @uses bbp_get_reply_position() To get the reply position
  1211. */
  1212. function bbp_reply_position( $reply_id = 0, $topic_id = 0 ) {
  1213. echo bbp_get_reply_position( $reply_id, $topic_id );
  1214. }
  1215. /**
  1216. * Return the numeric position of a reply within a topic
  1217. *
  1218. * @since bbPress (r2984)
  1219. *
  1220. * @param int $reply_id Optional. Reply id
  1221. * @param int $topic_id Optional. Topic id
  1222. * @uses bbp_get_reply_id() To get the reply id
  1223. * @uses bbp_get_reply_topic_id() Get the topic id of the reply id
  1224. * @uses bbp_get_topic_reply_count() To get the topic reply count
  1225. * @uses bbp_get_reply_post_type() To get the reply post type
  1226. * @uses bbp_get_reply_position_raw() To get calculate the reply position
  1227. * @uses bbp_update_reply_position() To update the reply position
  1228. * @uses bbp_show_lead_topic() Bump the count if lead topic is included
  1229. * @uses apply_filters() Calls 'bbp_get_reply_position' with the reply
  1230. * position, reply id and topic id
  1231. * @return int Reply position
  1232. */
  1233. function bbp_get_reply_position( $reply_id = 0, $topic_id = 0 ) {
  1234. // Get required data
  1235. $reply_id = bbp_get_reply_id( $reply_id );
  1236. $reply_position = get_post_field( 'menu_order', $reply_id );
  1237. // Reply doesn't have a position so get the raw value
  1238. if ( empty( $reply_position ) ) {
  1239. $topic_id = !empty( $topic_id ) ? bbp_get_topic_id( $topic_id ) : bbp_get_reply_topic_id( $reply_id );
  1240. // Post is not the topic
  1241. if ( $reply_id != $topic_id ) {
  1242. $reply_position = bbp_get_reply_position_raw( $reply_id, $topic_id );
  1243. // Update the reply position in the posts table so we'll never have
  1244. // to hit the DB again.
  1245. if ( !empty( $reply_position ) ) {
  1246. bbp_update_reply_position( $reply_id, $reply_position );
  1247. }
  1248. // Topic's position is always 0
  1249. } else {
  1250. $reply_position = 0;
  1251. }
  1252. }
  1253. // Bump the position by one if the lead topic is in the replies loop
  1254. if ( ! bbp_show_lead_topic() )
  1255. $reply_position++;
  1256. return (int) apply_filters( 'bbp_get_reply_position', $reply_position, $reply_id, $topic_id );
  1257. }
  1258. /** Reply Admin Links *********************************************************/
  1259. /**
  1260. * Output admin links for reply
  1261. *
  1262. * @since bbPress (r2667)
  1263. *
  1264. * @param mixed $args See {@link bbp_get_reply_admin_links()}
  1265. * @uses bbp_get_reply_admin_links() To get the reply admin links
  1266. */
  1267. function bbp_reply_admin_links( $args = '' ) {
  1268. echo bbp_get_reply_admin_links( $args );
  1269. }
  1270. /**
  1271. * Return admin links for reply
  1272. *
  1273. * @since bbPress (r2667)
  1274. *
  1275. * @param mixed $args This function supports these arguments:
  1276. * - id: Optional. Reply id
  1277. * - before: HTML before the links. Defaults to
  1278. * '<span class="bbp-admin-links">'
  1279. * - after: HTML after the links. Defaults to '</span>'
  1280. * - sep: Separator. Defaults to ' | '
  1281. * - links: Array of the links to display. By default, edit, trash,
  1282. * spam, reply move, and topic split links are displayed
  1283. * @uses bbp_is_topic() To check if it's the topic page
  1284. * @uses bbp_is_reply() To check if it's the reply page
  1285. * @uses bbp_get_reply_id() To get the reply id
  1286. * @uses bbp_get_reply_edit_link() To get the reply edit link
  1287. * @uses bbp_get_reply_trash_link() To get the reply trash link
  1288. * @uses bbp_get_reply_spam_link() To get the reply spam link
  1289. * @uses bbp_get_reply_move_link() To get the reply move link
  1290. * @uses bbp_get_topic_split_link() To get the topic split link
  1291. * @uses current_user_can() To check if the current user can edit or
  1292. * delete the reply
  1293. * @uses apply_filters() Calls 'bbp_get_reply_admin_links' with the
  1294. * reply admin links and args
  1295. * @return string Reply admin links
  1296. */
  1297. function bbp_get_reply_admin_links( $args = '' ) {
  1298. // Parse arguments against default values
  1299. $r = bbp_parse_args( $args, array(
  1300. 'id' => 0,
  1301. 'before' => '<span class="bbp-admin-links">',
  1302. 'after' => '</span>',
  1303. 'sep' => ' | ',
  1304. 'links' => array()
  1305. ), 'get_reply_admin_links' );
  1306. $r['id'] = bbp_get_reply_id( (int) $r['id'] );
  1307. // If post is a topic, return the topic admin links instead
  1308. if ( bbp_is_topic( $r['id'] ) )
  1309. return bbp_get_topic_admin_links( $args );
  1310. // If post is not a reply, return
  1311. if ( !bbp_is_reply( $r['id'] ) )
  1312. return;
  1313. // Make sure user can edit this reply
  1314. if ( !current_user_can( 'edit_reply', $r['id'] ) )
  1315. return;
  1316. // If topic is trashed, do not show admin links
  1317. if ( bbp_is_topic_trash( bbp_get_reply_topic_id( $r['id'] ) ) )
  1318. return;
  1319. // If no links were passed, default to the standard
  1320. if ( empty( $r['links'] ) ) {
  1321. $r['links'] = apply_filters( 'bbp_reply_admin_links', array(
  1322. 'edit' => bbp_get_reply_edit_link ( $r ),
  1323. 'move' => bbp_get_reply_move_link ( $r ),
  1324. 'split' => bbp_get_topic_split_link( $r ),
  1325. 'trash' => bbp_get_reply_t

Large files files are truncated, but you can click here to view the full file