PageRenderTime 38ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 1ms

/wp-content/plugins/bbpress/includes/topics/template.php

https://bitbucket.org/Thane2376/death-edge.ru
PHP | 4005 lines | 1469 code | 528 blank | 2008 comment | 245 complexity | e050cd312edaf2a6e40171f4c0b4bf6f MD5 | raw file
Possible License(s): LGPL-2.1, GPL-2.0, LGPL-3.0, AGPL-1.0
  1. <?php
  2. /**
  3. * bbPress Topic 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. * Output the unique id of the custom post type for topics
  13. *
  14. * @since bbPress (r2857)
  15. *
  16. * @uses bbp_get_topic_post_type() To get the topic post type
  17. */
  18. function bbp_topic_post_type() {
  19. echo bbp_get_topic_post_type();
  20. }
  21. /**
  22. * Return the unique id of the custom post type for topics
  23. *
  24. * @since bbPress (r2857)
  25. *
  26. * @uses apply_filters() Calls 'bbp_get_topic_post_type' with the topic
  27. * post type id
  28. * @return string The unique topic post type id
  29. */
  30. function bbp_get_topic_post_type() {
  31. return apply_filters( 'bbp_get_topic_post_type', bbpress()->topic_post_type );
  32. }
  33. /**
  34. * Return array of labels used by the topic post type
  35. *
  36. * @since bbPress (r5129)
  37. *
  38. * @return array
  39. */
  40. function bbp_get_topic_post_type_labels() {
  41. return apply_filters( 'bbp_get_topic_post_type_labels', array(
  42. 'name' => __( 'Topics', 'bbpress' ),
  43. 'menu_name' => __( 'Topics', 'bbpress' ),
  44. 'singular_name' => __( 'Topic', 'bbpress' ),
  45. 'all_items' => __( 'All Topics', 'bbpress' ),
  46. 'add_new' => __( 'New Topic', 'bbpress' ),
  47. 'add_new_item' => __( 'Create New Topic', 'bbpress' ),
  48. 'edit' => __( 'Edit', 'bbpress' ),
  49. 'edit_item' => __( 'Edit Topic', 'bbpress' ),
  50. 'new_item' => __( 'New Topic', 'bbpress' ),
  51. 'view' => __( 'View Topic', 'bbpress' ),
  52. 'view_item' => __( 'View Topic', 'bbpress' ),
  53. 'search_items' => __( 'Search Topics', 'bbpress' ),
  54. 'not_found' => __( 'No topics found', 'bbpress' ),
  55. 'not_found_in_trash' => __( 'No topics found in Trash', 'bbpress' ),
  56. 'parent_item_colon' => __( 'Forum:', 'bbpress' )
  57. ) );
  58. }
  59. /**
  60. * Return array of topic post type rewrite settings
  61. *
  62. * @since bbPress (r5129)
  63. *
  64. * @return array
  65. */
  66. function bbp_get_topic_post_type_rewrite() {
  67. return apply_filters( 'bbp_get_topic_post_type_rewrite', array(
  68. 'slug' => bbp_get_topic_slug(),
  69. 'with_front' => false
  70. ) );
  71. }
  72. /**
  73. * Return array of features the topic post type supports
  74. *
  75. * @since bbPress (r5129)
  76. *
  77. * @return array
  78. */
  79. function bbp_get_topic_post_type_supports() {
  80. return apply_filters( 'bbp_get_topic_post_type_supports', array(
  81. 'title',
  82. 'editor',
  83. 'revisions'
  84. ) );
  85. }
  86. /**
  87. * The plugin version of bbPress comes with two topic display options:
  88. * - Traditional: Topics are included in the reply loop (default)
  89. * - New Style: Topics appear as "lead" posts, ahead of replies
  90. *
  91. * @since bbPress (r2954)
  92. * @param $show_lead Optional. Default false
  93. * @return bool Yes if the topic appears as a lead, otherwise false
  94. */
  95. function bbp_show_lead_topic( $show_lead = false ) {
  96. // Never separate the lead topic in feeds
  97. if ( is_feed() )
  98. return false;
  99. return (bool) apply_filters( 'bbp_show_lead_topic', (bool) $show_lead );
  100. }
  101. /** Topic Loop ****************************************************************/
  102. /**
  103. * The main topic loop. WordPress makes this easy for us
  104. *
  105. * @since bbPress (r2485)
  106. *
  107. * @param mixed $args All the arguments supported by {@link WP_Query}
  108. * @uses current_user_can() To check if the current user can edit other's topics
  109. * @uses bbp_get_topic_post_type() To get the topic post type
  110. * @uses WP_Query To make query and get the topics
  111. * @uses is_page() To check if it's a page
  112. * @uses bbp_is_single_forum() To check if it's a forum
  113. * @uses bbp_get_forum_id() To get the forum id
  114. * @uses bbp_get_paged() To get the current page value
  115. * @uses bbp_get_super_stickies() To get the super stickies
  116. * @uses bbp_get_stickies() To get the forum stickies
  117. * @uses wpdb::get_results() To execute our query and get the results
  118. * @uses WP_Rewrite::using_permalinks() To check if the blog is using permalinks
  119. * @uses get_permalink() To get the permalink
  120. * @uses add_query_arg() To add custom args to the url
  121. * @uses apply_filters() Calls 'bbp_topics_pagination' with the pagination args
  122. * @uses paginate_links() To paginate the links
  123. * @uses apply_filters() Calls 'bbp_has_topics' with
  124. * bbPres::topic_query::have_posts()
  125. * and bbPres::topic_query
  126. * @return object Multidimensional array of topic information
  127. */
  128. function bbp_has_topics( $args = '' ) {
  129. global $wp_rewrite;
  130. /** Defaults **************************************************************/
  131. // Other defaults
  132. $default_topic_search = !empty( $_REQUEST['ts'] ) ? $_REQUEST['ts'] : false;
  133. $default_show_stickies = (bool) ( bbp_is_single_forum() || bbp_is_topic_archive() ) && ( false === $default_topic_search );
  134. $default_post_parent = bbp_is_single_forum() ? bbp_get_forum_id() : 'any';
  135. // Default argument array
  136. $default = array(
  137. 'post_type' => bbp_get_topic_post_type(), // Narrow query down to bbPress topics
  138. 'post_parent' => $default_post_parent, // Forum ID
  139. 'meta_key' => '_bbp_last_active_time', // Make sure topic has some last activity time
  140. 'orderby' => 'meta_value', // 'meta_value', 'author', 'date', 'title', 'modified', 'parent', rand',
  141. 'order' => 'DESC', // 'ASC', 'DESC'
  142. 'posts_per_page' => bbp_get_topics_per_page(), // Topics per page
  143. 'paged' => bbp_get_paged(), // Page Number
  144. 's' => $default_topic_search, // Topic Search
  145. 'show_stickies' => $default_show_stickies, // Ignore sticky topics?
  146. 'max_num_pages' => false, // Maximum number of pages to show
  147. );
  148. // What are the default allowed statuses (based on user caps)
  149. if ( bbp_get_view_all() ) {
  150. // Default view=all statuses
  151. $post_statuses = array(
  152. bbp_get_public_status_id(),
  153. bbp_get_closed_status_id(),
  154. bbp_get_spam_status_id(),
  155. bbp_get_trash_status_id()
  156. );
  157. // Add support for private status
  158. if ( current_user_can( 'read_private_topics' ) ) {
  159. $post_statuses[] = bbp_get_private_status_id();
  160. }
  161. // Join post statuses together
  162. $default['post_status'] = implode( ',', $post_statuses );
  163. // Lean on the 'perm' query var value of 'readable' to provide statuses
  164. } else {
  165. $default['perm'] = 'readable';
  166. }
  167. // Maybe query for topic tags
  168. if ( bbp_is_topic_tag() ) {
  169. $default['term'] = bbp_get_topic_tag_slug();
  170. $default['taxonomy'] = bbp_get_topic_tag_tax_id();
  171. }
  172. /** Setup *****************************************************************/
  173. // Parse arguments against default values
  174. $r = bbp_parse_args( $args, $default, 'has_topics' );
  175. // Get bbPress
  176. $bbp = bbpress();
  177. // Call the query
  178. $bbp->topic_query = new WP_Query( $r );
  179. // Set post_parent back to 0 if originally set to 'any'
  180. if ( 'any' === $r['post_parent'] )
  181. $r['post_parent'] = 0;
  182. // Limited the number of pages shown
  183. if ( !empty( $r['max_num_pages'] ) )
  184. $bbp->topic_query->max_num_pages = $r['max_num_pages'];
  185. /** Stickies **************************************************************/
  186. // Put sticky posts at the top of the posts array
  187. if ( !empty( $r['show_stickies'] ) && $r['paged'] <= 1 ) {
  188. // Get super stickies and stickies in this forum
  189. $stickies = bbp_get_super_stickies();
  190. // Get stickies for current forum
  191. if ( !empty( $r['post_parent'] ) ) {
  192. $stickies = array_merge( $stickies, bbp_get_stickies( $r['post_parent'] ) );
  193. }
  194. // Remove any duplicate stickies
  195. $stickies = array_unique( $stickies );
  196. // We have stickies
  197. if ( is_array( $stickies ) && !empty( $stickies ) ) {
  198. // Start the offset at -1 so first sticky is at correct 0 offset
  199. $sticky_offset = -1;
  200. // Loop over topics and relocate stickies to the front.
  201. foreach ( $stickies as $sticky_index => $sticky_ID ) {
  202. // Get the post offset from the posts array
  203. $post_offsets = wp_filter_object_list( $bbp->topic_query->posts, array( 'ID' => $sticky_ID ), 'OR', 'ID' );
  204. // Continue if no post offsets
  205. if ( empty( $post_offsets ) ) {
  206. continue;
  207. }
  208. // Loop over posts in current query and splice them into position
  209. foreach ( array_keys( $post_offsets ) as $post_offset ) {
  210. $sticky_offset++;
  211. $sticky = $bbp->topic_query->posts[$post_offset];
  212. // Remove sticky from current position
  213. array_splice( $bbp->topic_query->posts, $post_offset, 1 );
  214. // Move to front, after other stickies
  215. array_splice( $bbp->topic_query->posts, $sticky_offset, 0, array( $sticky ) );
  216. // Cleanup
  217. unset( $stickies[$sticky_index] );
  218. unset( $sticky );
  219. }
  220. // Cleanup
  221. unset( $post_offsets );
  222. }
  223. // Cleanup
  224. unset( $sticky_offset );
  225. // If any posts have been excluded specifically, Ignore those that are sticky.
  226. if ( !empty( $stickies ) && !empty( $r['post__not_in'] ) ) {
  227. $stickies = array_diff( $stickies, $r['post__not_in'] );
  228. }
  229. // Fetch sticky posts that weren't in the query results
  230. if ( !empty( $stickies ) ) {
  231. // Query to use in get_posts to get sticky posts
  232. $sticky_query = array(
  233. 'post_type' => bbp_get_topic_post_type(),
  234. 'post_parent' => 'any',
  235. 'meta_key' => '_bbp_last_active_time',
  236. 'orderby' => 'meta_value',
  237. 'order' => 'DESC',
  238. 'include' => $stickies
  239. );
  240. // Cleanup
  241. unset( $stickies );
  242. // Conditionally exclude private/hidden forum ID's
  243. $exclude_forum_ids = bbp_exclude_forum_ids( 'array' );
  244. if ( ! empty( $exclude_forum_ids ) ) {
  245. $sticky_query['post_parent__not_in'] = $exclude_forum_ids;
  246. }
  247. // What are the default allowed statuses (based on user caps)
  248. if ( bbp_get_view_all() ) {
  249. $sticky_query['post_status'] = $r['post_status'];
  250. // Lean on the 'perm' query var value of 'readable' to provide statuses
  251. } else {
  252. $sticky_query['post_status'] = $r['perm'];
  253. }
  254. // Get all stickies
  255. $sticky_posts = get_posts( $sticky_query );
  256. if ( !empty( $sticky_posts ) ) {
  257. // Get a count of the visible stickies
  258. $sticky_count = count( $sticky_posts );
  259. // Merge the stickies topics with the query topics .
  260. $bbp->topic_query->posts = array_merge( $sticky_posts, $bbp->topic_query->posts );
  261. // Adjust loop and counts for new sticky positions
  262. $bbp->topic_query->found_posts = (int) $bbp->topic_query->found_posts + (int) $sticky_count;
  263. $bbp->topic_query->post_count = (int) $bbp->topic_query->post_count + (int) $sticky_count;
  264. // Cleanup
  265. unset( $sticky_posts );
  266. }
  267. }
  268. }
  269. }
  270. // If no limit to posts per page, set it to the current post_count
  271. if ( -1 === $r['posts_per_page'] )
  272. $r['posts_per_page'] = $bbp->topic_query->post_count;
  273. // Add pagination values to query object
  274. $bbp->topic_query->posts_per_page = $r['posts_per_page'];
  275. $bbp->topic_query->paged = $r['paged'];
  276. // Only add pagination if query returned results
  277. if ( ( (int) $bbp->topic_query->post_count || (int) $bbp->topic_query->found_posts ) && (int) $bbp->topic_query->posts_per_page ) {
  278. // Limit the number of topics shown based on maximum allowed pages
  279. if ( ( !empty( $r['max_num_pages'] ) ) && $bbp->topic_query->found_posts > $bbp->topic_query->max_num_pages * $bbp->topic_query->post_count )
  280. $bbp->topic_query->found_posts = $bbp->topic_query->max_num_pages * $bbp->topic_query->post_count;
  281. // If pretty permalinks are enabled, make our pagination pretty
  282. if ( $wp_rewrite->using_permalinks() ) {
  283. // User's topics
  284. if ( bbp_is_single_user_topics() ) {
  285. $base = bbp_get_user_topics_created_url( bbp_get_displayed_user_id() );
  286. // User's favorites
  287. } elseif ( bbp_is_favorites() ) {
  288. $base = bbp_get_favorites_permalink( bbp_get_displayed_user_id() );
  289. // User's subscriptions
  290. } elseif ( bbp_is_subscriptions() ) {
  291. $base = bbp_get_subscriptions_permalink( bbp_get_displayed_user_id() );
  292. // Root profile page
  293. } elseif ( bbp_is_single_user() ) {
  294. $base = bbp_get_user_profile_url( bbp_get_displayed_user_id() );
  295. // View
  296. } elseif ( bbp_is_single_view() ) {
  297. $base = bbp_get_view_url();
  298. // Topic tag
  299. } elseif ( bbp_is_topic_tag() ) {
  300. $base = bbp_get_topic_tag_link();
  301. // Page or single post
  302. } elseif ( is_page() || is_single() ) {
  303. $base = get_permalink();
  304. // Forum archive
  305. } elseif ( bbp_is_forum_archive() ) {
  306. $base = bbp_get_forums_url();
  307. // Topic archive
  308. } elseif ( bbp_is_topic_archive() ) {
  309. $base = bbp_get_topics_url();
  310. // Default
  311. } else {
  312. $base = get_permalink( (int) $r['post_parent'] );
  313. }
  314. // Use pagination base
  315. $base = trailingslashit( $base ) . user_trailingslashit( $wp_rewrite->pagination_base . '/%#%/' );
  316. // Unpretty pagination
  317. } else {
  318. $base = add_query_arg( 'paged', '%#%' );
  319. }
  320. // Pagination settings with filter
  321. $bbp_topic_pagination = apply_filters( 'bbp_topic_pagination', array (
  322. 'base' => $base,
  323. 'format' => '',
  324. 'total' => $r['posts_per_page'] === $bbp->topic_query->found_posts ? 1 : ceil( (int) $bbp->topic_query->found_posts / (int) $r['posts_per_page'] ),
  325. 'current' => (int) $bbp->topic_query->paged,
  326. 'prev_text' => is_rtl() ? '&rarr;' : '&larr;',
  327. 'next_text' => is_rtl() ? '&larr;' : '&rarr;',
  328. 'mid_size' => 1
  329. ) );
  330. // Add pagination to query object
  331. $bbp->topic_query->pagination_links = paginate_links( $bbp_topic_pagination );
  332. // Remove first page from pagination
  333. $bbp->topic_query->pagination_links = str_replace( $wp_rewrite->pagination_base . "/1/'", "'", $bbp->topic_query->pagination_links );
  334. }
  335. // Return object
  336. return apply_filters( 'bbp_has_topics', $bbp->topic_query->have_posts(), $bbp->topic_query );
  337. }
  338. /**
  339. * Whether there are more topics available in the loop
  340. *
  341. * @since bbPress (r2485)
  342. *
  343. * @uses WP_Query bbPress::topic_query::have_posts()
  344. * @return object Topic information
  345. */
  346. function bbp_topics() {
  347. // Put into variable to check against next
  348. $have_posts = bbpress()->topic_query->have_posts();
  349. // Reset the post data when finished
  350. if ( empty( $have_posts ) )
  351. wp_reset_postdata();
  352. return $have_posts;
  353. }
  354. /**
  355. * Loads up the current topic in the loop
  356. *
  357. * @since bbPress (r2485)
  358. *
  359. * @uses WP_Query bbPress::topic_query::the_post()
  360. * @return object Topic information
  361. */
  362. function bbp_the_topic() {
  363. return bbpress()->topic_query->the_post();
  364. }
  365. /**
  366. * Output the topic id
  367. *
  368. * @since bbPress (r2485)
  369. *
  370. * @uses bbp_get_topic_id() To get the topic id
  371. */
  372. function bbp_topic_id( $topic_id = 0) {
  373. echo bbp_get_topic_id( $topic_id );
  374. }
  375. /**
  376. * Return the topic id
  377. *
  378. * @since bbPress (r2485)
  379. *
  380. * @param $topic_id Optional. Used to check emptiness
  381. * @uses bbPress::topic_query::post::ID To get the topic id
  382. * @uses bbp_is_topic() To check if the search result is a topic
  383. * @uses bbp_is_single_topic() To check if it's a topic page
  384. * @uses bbp_is_topic_edit() To check if it's a topic edit page
  385. * @uses bbp_is_single_reply() To check if it it's a reply page
  386. * @uses bbp_is_reply_edit() To check if it's a reply edit page
  387. * @uses bbp_get_reply_topic_edit() To get the reply topic id
  388. * @uses get_post_field() To get the post's post type
  389. * @uses WP_Query::post::ID To get the topic id
  390. * @uses bbp_get_topic_post_type() To get the topic post type
  391. * @uses apply_filters() Calls 'bbp_get_topic_id' with the topic id and
  392. * supplied topic id
  393. * @return int The topic id
  394. */
  395. function bbp_get_topic_id( $topic_id = 0 ) {
  396. global $wp_query;
  397. $bbp = bbpress();
  398. // Easy empty checking
  399. if ( !empty( $topic_id ) && is_numeric( $topic_id ) ) {
  400. $bbp_topic_id = $topic_id;
  401. // Currently inside a topic loop
  402. } elseif ( !empty( $bbp->topic_query->in_the_loop ) && isset( $bbp->topic_query->post->ID ) ) {
  403. $bbp_topic_id = $bbp->topic_query->post->ID;
  404. // Currently inside a search loop
  405. } elseif ( !empty( $bbp->search_query->in_the_loop ) && isset( $bbp->search_query->post->ID ) && bbp_is_topic( $bbp->search_query->post->ID ) ) {
  406. $bbp_topic_id = $bbp->search_query->post->ID;
  407. // Currently viewing/editing a topic, likely alone
  408. } elseif ( ( bbp_is_single_topic() || bbp_is_topic_edit() ) && !empty( $bbp->current_topic_id ) ) {
  409. $bbp_topic_id = $bbp->current_topic_id;
  410. // Currently viewing/editing a topic, likely in a loop
  411. } elseif ( ( bbp_is_single_topic() || bbp_is_topic_edit() ) && isset( $wp_query->post->ID ) ) {
  412. $bbp_topic_id = $wp_query->post->ID;
  413. // Currently viewing/editing a reply
  414. } elseif ( bbp_is_single_reply() || bbp_is_reply_edit() ) {
  415. $bbp_topic_id = bbp_get_reply_topic_id();
  416. // Fallback
  417. } else {
  418. $bbp_topic_id = 0;
  419. }
  420. return (int) apply_filters( 'bbp_get_topic_id', (int) $bbp_topic_id, $topic_id );
  421. }
  422. /**
  423. * Gets a topic
  424. *
  425. * @since bbPress (r2787)
  426. *
  427. * @param int|object $topic Topic id or topic object
  428. * @param string $output Optional. OBJECT, ARRAY_A, or ARRAY_N. Default = OBJECT
  429. * @param string $filter Optional Sanitation filter. See {@link sanitize_post()}
  430. * @uses get_post() To get the topic
  431. * @uses apply_filters() Calls 'bbp_get_topic' with the topic, output type and
  432. * sanitation filter
  433. * @return mixed Null if error or topic (in specified form) if success
  434. */
  435. function bbp_get_topic( $topic, $output = OBJECT, $filter = 'raw' ) {
  436. // Use topic ID
  437. if ( empty( $topic ) || is_numeric( $topic ) )
  438. $topic = bbp_get_topic_id( $topic );
  439. // Attempt to load the topic
  440. $topic = get_post( $topic, OBJECT, $filter );
  441. if ( empty( $topic ) )
  442. return $topic;
  443. // Bail if post_type is not a topic
  444. if ( $topic->post_type !== bbp_get_topic_post_type() )
  445. return null;
  446. // Tweak the data type to return
  447. if ( $output === OBJECT ) {
  448. return $topic;
  449. } elseif ( $output === ARRAY_A ) {
  450. $_topic = get_object_vars( $topic );
  451. return $_topic;
  452. } elseif ( $output === ARRAY_N ) {
  453. $_topic = array_values( get_object_vars( $topic ) );
  454. return $_topic;
  455. }
  456. return apply_filters( 'bbp_get_topic', $topic, $output, $filter );
  457. }
  458. /**
  459. * Output the link to the topic in the topic loop
  460. *
  461. * @since bbPress (r2485)
  462. *
  463. * @param int $topic_id Optional. Topic id
  464. * @param $string $redirect_to Optional. Pass a redirect value for use with
  465. * shortcodes and other fun things.
  466. * @uses bbp_get_topic_permalink() To get the topic permalink
  467. */
  468. function bbp_topic_permalink( $topic_id = 0, $redirect_to = '' ) {
  469. echo esc_url( bbp_get_topic_permalink( $topic_id, $redirect_to ) );
  470. }
  471. /**
  472. * Return the link to the topic
  473. *
  474. * @since bbPress (r2485)
  475. *
  476. * @param int $topic_id Optional. Topic id
  477. * @param $string $redirect_to Optional. Pass a redirect value for use with
  478. * shortcodes and other fun things.
  479. * @uses bbp_get_topic_id() To get the topic id
  480. * @uses get_permalink() To get the topic permalink
  481. * @uses esc_url_raw() To clean the redirect_to url
  482. * @uses apply_filters() Calls 'bbp_get_topic_permalink' with the link
  483. * and topic id
  484. * @return string Permanent link to topic
  485. */
  486. function bbp_get_topic_permalink( $topic_id = 0, $redirect_to = '' ) {
  487. $topic_id = bbp_get_topic_id( $topic_id );
  488. // Use the redirect address
  489. if ( !empty( $redirect_to ) ) {
  490. $topic_permalink = esc_url_raw( $redirect_to );
  491. // Use the topic permalink
  492. } else {
  493. $topic_permalink = get_permalink( $topic_id );
  494. }
  495. return apply_filters( 'bbp_get_topic_permalink', $topic_permalink, $topic_id );
  496. }
  497. /**
  498. * Output the title of the topic
  499. *
  500. * @since bbPress (r2485)
  501. *
  502. * @param int $topic_id Optional. Topic id
  503. * @uses bbp_get_topic_title() To get the topic title
  504. */
  505. function bbp_topic_title( $topic_id = 0 ) {
  506. echo bbp_get_topic_title( $topic_id );
  507. }
  508. /**
  509. * Return the title of the topic
  510. *
  511. * @since bbPress (r2485)
  512. *
  513. * @param int $topic_id Optional. Topic id
  514. * @uses bbp_get_topic_id() To get the topic id
  515. * @uses get_the_title() To get the title
  516. * @uses apply_filters() Calls 'bbp_get_topic_title' with the title and
  517. * topic id
  518. * @return string Title of topic
  519. */
  520. function bbp_get_topic_title( $topic_id = 0 ) {
  521. $topic_id = bbp_get_topic_id( $topic_id );
  522. $title = get_the_title( $topic_id );
  523. return apply_filters( 'bbp_get_topic_title', $title, $topic_id );
  524. }
  525. /**
  526. * Output the topic archive title
  527. *
  528. * @since bbPress (r3249)
  529. *
  530. * @param string $title Default text to use as title
  531. */
  532. function bbp_topic_archive_title( $title = '' ) {
  533. echo bbp_get_topic_archive_title( $title );
  534. }
  535. /**
  536. * Return the topic archive title
  537. *
  538. * @since bbPress (r3249)
  539. *
  540. * @param string $title Default text to use as title
  541. *
  542. * @uses bbp_get_page_by_path() Check if page exists at root path
  543. * @uses get_the_title() Use the page title at the root path
  544. * @uses get_post_type_object() Load the post type object
  545. * @uses bbp_get_topic_post_type() Get the topic post type ID
  546. * @uses get_post_type_labels() Get labels for topic post type
  547. * @uses apply_filters() Allow output to be manipulated
  548. *
  549. * @return string The topic archive title
  550. */
  551. function bbp_get_topic_archive_title( $title = '' ) {
  552. // If no title was passed
  553. if ( empty( $title ) ) {
  554. // Set root text to page title
  555. $page = bbp_get_page_by_path( bbp_get_topic_archive_slug() );
  556. if ( !empty( $page ) ) {
  557. $title = get_the_title( $page->ID );
  558. // Default to topic post type name label
  559. } else {
  560. $tto = get_post_type_object( bbp_get_topic_post_type() );
  561. $title = $tto->labels->name;
  562. }
  563. }
  564. return apply_filters( 'bbp_get_topic_archive_title', $title );
  565. }
  566. /**
  567. * Output the content of the topic
  568. *
  569. * @since bbPress (r2780)
  570. *
  571. * @param int $topic_id Optional. Topic id
  572. * @uses bbp_get_topic_content() To get the topic content
  573. */
  574. function bbp_topic_content( $topic_id = 0 ) {
  575. echo bbp_get_topic_content( $topic_id );
  576. }
  577. /**
  578. * Return the content of the topic
  579. *
  580. * @since bbPress (r2780)
  581. *
  582. * @param int $topic_id Optional. Topic id
  583. * @uses bbp_get_topic_id() To get the topic id
  584. * @uses post_password_required() To check if the topic requires pass
  585. * @uses get_the_password_form() To get the password form
  586. * @uses get_post_field() To get the content post field
  587. * @uses apply_filters() Calls 'bbp_get_topic_content' with the content
  588. * and topic id
  589. * @return string Content of the topic
  590. */
  591. function bbp_get_topic_content( $topic_id = 0 ) {
  592. $topic_id = bbp_get_topic_id( $topic_id );
  593. // Check if password is required
  594. if ( post_password_required( $topic_id ) )
  595. return get_the_password_form();
  596. $content = get_post_field( 'post_content', $topic_id );
  597. return apply_filters( 'bbp_get_topic_content', $content, $topic_id );
  598. }
  599. /**
  600. * Output the excerpt of the topic
  601. *
  602. * @since bbPress (r2780)
  603. *
  604. * @param int $topic_id Optional. Topic id
  605. * @param int $length Optional. Length of the excerpt. Defaults to 100 letters
  606. * @uses bbp_get_topic_excerpt() To get the topic excerpt
  607. */
  608. function bbp_topic_excerpt( $topic_id = 0, $length = 100 ) {
  609. echo bbp_get_topic_excerpt( $topic_id, $length );
  610. }
  611. /**
  612. * Return the excerpt of the topic
  613. *
  614. * @since bbPress (r2780)
  615. *
  616. * @param int $topic_id Optional. topic id
  617. * @param int $length Optional. Length of the excerpt. Defaults to 100
  618. * letters
  619. * @uses bbp_get_topic_id() To get the topic id
  620. * @uses get_post_field() To get the excerpt
  621. * @uses bbp_get_topic_content() To get the topic content
  622. * @uses apply_filters() Calls 'bbp_get_topic_excerpt' with the excerpt,
  623. * topic id and length
  624. * @return string topic Excerpt
  625. */
  626. function bbp_get_topic_excerpt( $topic_id = 0, $length = 100 ) {
  627. $topic_id = bbp_get_topic_id( $topic_id );
  628. $length = (int) $length;
  629. $excerpt = get_post_field( 'post_excerpt', $topic_id );
  630. if ( empty( $excerpt ) ) {
  631. $excerpt = bbp_get_topic_content( $topic_id );
  632. }
  633. $excerpt = trim( strip_tags( $excerpt ) );
  634. // Multibyte support
  635. if ( function_exists( 'mb_strlen' ) ) {
  636. $excerpt_length = mb_strlen( $excerpt );
  637. } else {
  638. $excerpt_length = strlen( $excerpt );
  639. }
  640. if ( !empty( $length ) && ( $excerpt_length > $length ) ) {
  641. $excerpt = substr( $excerpt, 0, $length - 1 );
  642. $excerpt .= '&hellip;';
  643. }
  644. return apply_filters( 'bbp_get_topic_excerpt', $excerpt, $topic_id, $length );
  645. }
  646. /**
  647. * Output the post date and time of a topic
  648. *
  649. * @since bbPress (r4155)
  650. *
  651. * @param int $topic_id Optional. Topic id.
  652. * @param bool $humanize Optional. Humanize output using time_since
  653. * @param bool $gmt Optional. Use GMT
  654. * @uses bbp_get_topic_post_date() to get the output
  655. */
  656. function bbp_topic_post_date( $topic_id = 0, $humanize = false, $gmt = false ) {
  657. echo bbp_get_topic_post_date( $topic_id, $humanize, $gmt );
  658. }
  659. /**
  660. * Return the post date and time of a topic
  661. *
  662. * @since bbPress (r4155)
  663. *
  664. * @param int $topic_id Optional. Topic id.
  665. * @param bool $humanize Optional. Humanize output using time_since
  666. * @param bool $gmt Optional. Use GMT
  667. * @uses bbp_get_topic_id() To get the topic id
  668. * @uses get_post_time() to get the topic post time
  669. * @uses bbp_get_time_since() to maybe humanize the topic post time
  670. * @return string
  671. */
  672. function bbp_get_topic_post_date( $topic_id = 0, $humanize = false, $gmt = false ) {
  673. $topic_id = bbp_get_topic_id( $topic_id );
  674. // 4 days, 4 hours ago
  675. if ( !empty( $humanize ) ) {
  676. $gmt_s = !empty( $gmt ) ? 'U' : 'G';
  677. $date = get_post_time( $gmt_s, $gmt, $topic_id );
  678. $time = false; // For filter below
  679. $result = bbp_get_time_since( $date );
  680. // August 4, 2012 at 2:37 pm
  681. } else {
  682. $date = get_post_time( get_option( 'date_format' ), $gmt, $topic_id, true );
  683. $time = get_post_time( get_option( 'time_format' ), $gmt, $topic_id, true );
  684. $result = sprintf( _x( '%1$s at %2$s', 'date at time', 'bbpress' ), $date, $time );
  685. }
  686. return apply_filters( 'bbp_get_topic_post_date', $result, $topic_id, $humanize, $gmt, $date, $time );
  687. }
  688. /**
  689. * Output pagination links of a topic within the topic loop
  690. *
  691. * @since bbPress (r2966)
  692. *
  693. * @param mixed $args See {@link bbp_get_topic_pagination()}
  694. * @uses bbp_get_topic_pagination() To get the topic pagination links
  695. */
  696. function bbp_topic_pagination( $args = '' ) {
  697. echo bbp_get_topic_pagination( $args );
  698. }
  699. /**
  700. * Returns pagination links of a topic within the topic loop
  701. *
  702. * @since bbPress (r2966)
  703. *
  704. * @param mixed $args This function supports these arguments:
  705. * - topic_id: Topic id
  706. * - before: Before the links
  707. * - after: After the links
  708. * @uses bbp_get_topic_id() To get the topic id
  709. * @uses WP_Rewrite::using_permalinks() To check if the blog is using
  710. * permalinks
  711. * @uses user_trailingslashit() To add a trailing slash
  712. * @uses trailingslashit() To add a trailing slash
  713. * @uses get_permalink() To get the permalink of the topic
  714. * @uses add_query_arg() To add query args
  715. * @uses bbp_get_topic_reply_count() To get topic reply count
  716. * @uses bbp_show_topic_lead() Are we showing the topic as a lead?
  717. * @uses get_option() To get replies per page option
  718. * @uses paginate_links() To paginate the links
  719. * @uses apply_filters() Calls 'bbp_get_topic_pagination' with the links
  720. * and arguments
  721. * @return string Pagination links
  722. */
  723. function bbp_get_topic_pagination( $args = '' ) {
  724. global $wp_rewrite;
  725. // Bail if threading replies
  726. if ( bbp_thread_replies() ) {
  727. return;
  728. }
  729. // Parse arguments against default values
  730. $r = bbp_parse_args( $args, array(
  731. 'topic_id' => bbp_get_topic_id(),
  732. 'before' => '<span class="bbp-topic-pagination">',
  733. 'after' => '</span>',
  734. ), 'get_topic_pagination' );
  735. // If pretty permalinks are enabled, make our pagination pretty
  736. if ( $wp_rewrite->using_permalinks() ) {
  737. $base = trailingslashit( get_permalink( $r['topic_id'] ) ) . user_trailingslashit( $wp_rewrite->pagination_base . '/%#%/' );
  738. } else {
  739. $base = add_query_arg( 'paged', '%#%', get_permalink( $r['topic_id'] ) );
  740. }
  741. // Get total and add 1 if topic is included in the reply loop
  742. $total = bbp_get_topic_reply_count( $r['topic_id'], true );
  743. // Bump if topic is in loop
  744. if ( !bbp_show_lead_topic() )
  745. $total++;
  746. // Pagination settings
  747. $pagination = array(
  748. 'base' => $base,
  749. 'format' => '',
  750. 'total' => ceil( (int) $total / (int) bbp_get_replies_per_page() ),
  751. 'current' => 0,
  752. 'prev_next' => false,
  753. 'mid_size' => 2,
  754. 'end_size' => 3,
  755. 'add_args' => ( bbp_get_view_all() ) ? array( 'view' => 'all' ) : false
  756. );
  757. // Add pagination to query object
  758. $pagination_links = paginate_links( $pagination );
  759. if ( !empty( $pagination_links ) ) {
  760. // Remove first page from pagination
  761. if ( $wp_rewrite->using_permalinks() ) {
  762. $pagination_links = str_replace( $wp_rewrite->pagination_base . '/1/', '', $pagination_links );
  763. } else {
  764. $pagination_links = str_replace( '&#038;paged=1', '', $pagination_links );
  765. }
  766. // Add before and after to pagination links
  767. $pagination_links = $r['before'] . $pagination_links . $r['after'];
  768. }
  769. return apply_filters( 'bbp_get_topic_pagination', $pagination_links, $args );
  770. }
  771. /**
  772. * Append revisions to the topic content
  773. *
  774. * @since bbPress (r2782)
  775. *
  776. * @param string $content Optional. Content to which we need to append the revisions to
  777. * @param int $topic_id Optional. Topic id
  778. * @uses bbp_get_topic_revision_log() To get the topic revision log
  779. * @uses apply_filters() Calls 'bbp_topic_append_revisions' with the processed
  780. * content, original content and topic id
  781. * @return string Content with the revisions appended
  782. */
  783. function bbp_topic_content_append_revisions( $content = '', $topic_id = 0 ) {
  784. // Bail if in admin or feed
  785. if ( is_admin() || is_feed() )
  786. return;
  787. // Validate the ID
  788. $topic_id = bbp_get_topic_id( $topic_id );
  789. return apply_filters( 'bbp_topic_append_revisions', $content . bbp_get_topic_revision_log( $topic_id ), $content, $topic_id );
  790. }
  791. /**
  792. * Output the revision log of the topic
  793. *
  794. * @since bbPress (r2782)
  795. *
  796. * @param int $topic_id Optional. Topic id
  797. * @uses bbp_get_topic_revision_log() To get the topic revision log
  798. */
  799. function bbp_topic_revision_log( $topic_id = 0 ) {
  800. echo bbp_get_topic_revision_log( $topic_id );
  801. }
  802. /**
  803. * Return the formatted revision log of the topic
  804. *
  805. * @since bbPress (r2782)
  806. *
  807. * @param int $topic_id Optional. Topic id
  808. * @uses bbp_get_topic_id() To get the topic id
  809. * @uses bbp_get_topic_revisions() To get the topic revisions
  810. * @uses bbp_get_topic_raw_revision_log() To get the raw revision log
  811. * @uses bbp_get_topic_author_display_name() To get the topic author
  812. * @uses bbp_get_author_link() To get the topic author link
  813. * @uses bbp_convert_date() To convert the date
  814. * @uses bbp_get_time_since() To get the time in since format
  815. * @uses apply_filters() Calls 'bbp_get_topic_revision_log' with the
  816. * log and topic id
  817. * @return string Revision log of the topic
  818. */
  819. function bbp_get_topic_revision_log( $topic_id = 0 ) {
  820. // Create necessary variables
  821. $topic_id = bbp_get_topic_id( $topic_id );
  822. $revision_log = bbp_get_topic_raw_revision_log( $topic_id );
  823. if ( empty( $topic_id ) || empty( $revision_log ) || !is_array( $revision_log ) )
  824. return false;
  825. $revisions = bbp_get_topic_revisions( $topic_id );
  826. if ( empty( $revisions ) )
  827. return false;
  828. $r = "\n\n" . '<ul id="bbp-topic-revision-log-' . esc_attr( $topic_id ) . '" class="bbp-topic-revision-log">' . "\n\n";
  829. // Loop through revisions
  830. foreach ( (array) $revisions as $revision ) {
  831. if ( empty( $revision_log[$revision->ID] ) ) {
  832. $author_id = $revision->post_author;
  833. $reason = '';
  834. } else {
  835. $author_id = $revision_log[$revision->ID]['author'];
  836. $reason = $revision_log[$revision->ID]['reason'];
  837. }
  838. $author = bbp_get_author_link( array( 'size' => 14, 'link_text' => bbp_get_topic_author_display_name( $revision->ID ), 'post_id' => $revision->ID ) );
  839. $since = bbp_get_time_since( bbp_convert_date( $revision->post_modified ) );
  840. $r .= "\t" . '<li id="bbp-topic-revision-log-' . esc_attr( $topic_id ) . '-item-' . esc_attr( $revision->ID ) . '" class="bbp-topic-revision-log-item">' . "\n";
  841. if ( !empty( $reason ) ) {
  842. $r .= "\t\t" . sprintf( __( 'This topic was modified %1$s by %2$s. Reason: %3$s', 'bbpress' ), esc_html( $since ), $author, esc_html( $reason ) ) . "\n";
  843. } else {
  844. $r .= "\t\t" . sprintf( __( 'This topic was modified %1$s by %2$s.', 'bbpress' ), esc_html( $since ), $author ) . "\n";
  845. }
  846. $r .= "\t" . '</li>' . "\n";
  847. }
  848. $r .= "\n" . '</ul>' . "\n\n";
  849. return apply_filters( 'bbp_get_topic_revision_log', $r, $topic_id );
  850. }
  851. /**
  852. * Return the raw revision log of the topic
  853. *
  854. * @since bbPress (r2782)
  855. *
  856. * @param int $topic_id Optional. Topic id
  857. * @uses bbp_get_topic_id() To get the topic id
  858. * @uses get_post_meta() To get the revision log meta
  859. * @uses apply_filters() Calls 'bbp_get_topic_raw_revision_log'
  860. * with the log and topic id
  861. * @return string Raw revision log of the topic
  862. */
  863. function bbp_get_topic_raw_revision_log( $topic_id = 0 ) {
  864. $topic_id = bbp_get_topic_id( $topic_id );
  865. $revision_log = get_post_meta( $topic_id, '_bbp_revision_log', true );
  866. $revision_log = empty( $revision_log ) ? array() : $revision_log;
  867. return apply_filters( 'bbp_get_topic_raw_revision_log', $revision_log, $topic_id );
  868. }
  869. /**
  870. * Return the revisions of the topic
  871. *
  872. * @since bbPress (r2782)
  873. *
  874. * @param int $topic_id Optional. Topic id
  875. * @uses bbp_get_topic_id() To get the topic id
  876. * @uses wp_get_post_revisions() To get the topic revisions
  877. * @uses apply_filters() Calls 'bbp_get_topic_revisions'
  878. * with the revisions and topic id
  879. * @return string Topic revisions
  880. */
  881. function bbp_get_topic_revisions( $topic_id = 0 ) {
  882. $topic_id = bbp_get_topic_id( $topic_id );
  883. $revisions = wp_get_post_revisions( $topic_id, array( 'order' => 'ASC' ) );
  884. return apply_filters( 'bbp_get_topic_revisions', $revisions, $topic_id );
  885. }
  886. /**
  887. * Return the revision count of the topic
  888. *
  889. * @since bbPress (r2782)
  890. *
  891. * @param int $topic_id Optional. Topic id
  892. * @uses bbp_get_topic_revisions() To get the topic revisions
  893. * @uses apply_filters() Calls 'bbp_get_topic_revision_count'
  894. * with the revision count and topic id
  895. * @return string Topic revision count
  896. */
  897. function bbp_get_topic_revision_count( $topic_id = 0, $integer = false ) {
  898. $count = (int) count( bbp_get_topic_revisions( $topic_id ) );
  899. $filter = ( true === $integer ) ? 'bbp_get_topic_revision_count_int' : 'bbp_get_topic_revision_count';
  900. return apply_filters( $filter, $count, $topic_id );
  901. }
  902. /**
  903. * Output the status of the topic
  904. *
  905. * @since bbPress (r2667)
  906. *
  907. * @param int $topic_id Optional. Topic id
  908. * @uses bbp_get_topic_status() To get the topic status
  909. */
  910. function bbp_topic_status( $topic_id = 0 ) {
  911. echo bbp_get_topic_status( $topic_id );
  912. }
  913. /**
  914. * Return the status of the topic
  915. *
  916. * @since bbPress (r2667)
  917. *
  918. * @param int $topic_id Optional. Topic id
  919. * @uses bbp_get_topic_id() To get the topic id
  920. * @uses get_post_status() To get the topic status
  921. * @uses apply_filters() Calls 'bbp_get_topic_status' with the status
  922. * and topic id
  923. * @return string Status of topic
  924. */
  925. function bbp_get_topic_status( $topic_id = 0 ) {
  926. $topic_id = bbp_get_topic_id( $topic_id );
  927. return apply_filters( 'bbp_get_topic_status', get_post_status( $topic_id ), $topic_id );
  928. }
  929. /**
  930. * Is the topic open to new replies?
  931. *
  932. * @since bbPress (r2727)
  933. *
  934. * @uses bbp_get_topic_status()
  935. *
  936. * @param int $topic_id Optional. Topic id
  937. * @uses bbp_is_topic_closed() To check if the topic is closed
  938. * @return bool True if open, false if closed.
  939. */
  940. function bbp_is_topic_open( $topic_id = 0 ) {
  941. return !bbp_is_topic_closed( $topic_id );
  942. }
  943. /**
  944. * Is the topic closed to new replies?
  945. *
  946. * @since bbPress (r2746)
  947. *
  948. * @param int $topic_id Optional. Topic id
  949. * @uses bbp_get_topic_status() To get the topic status
  950. * @uses apply_filters() Calls 'bbp_is_topic_closed' with the topic id
  951. *
  952. * @return bool True if closed, false if not.
  953. */
  954. function bbp_is_topic_closed( $topic_id = 0 ) {
  955. $closed = bbp_get_topic_status( $topic_id ) === bbp_get_closed_status_id();
  956. return (bool) apply_filters( 'bbp_is_topic_closed', (bool) $closed, $topic_id );
  957. }
  958. /**
  959. * Is the topic a sticky or super sticky?
  960. *
  961. * @since bbPress (r2754)
  962. *
  963. * @param int $topic_id Optional. Topic id
  964. * @param int $check_super Optional. If set to true and if the topic is not a
  965. * normal sticky, it is checked if it is a super
  966. * sticky or not. Defaults to true.
  967. * @uses bbp_get_topic_id() To get the topic id
  968. * @uses bbp_get_topic_forum_id() To get the topic forum id
  969. * @uses bbp_get_stickies() To get the stickies
  970. * @uses bbp_is_topic_super_sticky() To check if the topic is a super sticky
  971. * @return bool True if sticky or super sticky, false if not.
  972. */
  973. function bbp_is_topic_sticky( $topic_id = 0, $check_super = true ) {
  974. $topic_id = bbp_get_topic_id( $topic_id );
  975. $forum_id = bbp_get_topic_forum_id( $topic_id );
  976. $stickies = bbp_get_stickies( $forum_id );
  977. if ( in_array( $topic_id, $stickies ) || ( !empty( $check_super ) && bbp_is_topic_super_sticky( $topic_id ) ) )
  978. return true;
  979. return false;
  980. }
  981. /**
  982. * Is the topic a super sticky?
  983. *
  984. * @since bbPress (r2754)
  985. *
  986. * @param int $topic_id Optional. Topic id
  987. * @uses bbp_get_topic_id() To get the topic id
  988. * @uses bbp_get_super_stickies() To get the super stickies
  989. * @return bool True if super sticky, false if not.
  990. */
  991. function bbp_is_topic_super_sticky( $topic_id = 0 ) {
  992. $topic_id = bbp_get_topic_id( $topic_id );
  993. $stickies = bbp_get_super_stickies( $topic_id );
  994. return in_array( $topic_id, $stickies );
  995. }
  996. /**
  997. * Is the topic not spam or deleted?
  998. *
  999. * @since bbPress (r3496)
  1000. *
  1001. * @param int $topic_id Optional. Topic id
  1002. * @uses bbp_get_topic_id() To get the topic id
  1003. * @uses bbp_get_topic_status() To get the topic status
  1004. * @uses apply_filters() Calls 'bbp_is_topic_published' with the topic id
  1005. * @return bool True if published, false if not.
  1006. */
  1007. function bbp_is_topic_published( $topic_id = 0 ) {
  1008. $topic_status = bbp_get_topic_status( bbp_get_topic_id( $topic_id ) ) === bbp_get_public_status_id();
  1009. return (bool) apply_filters( 'bbp_is_topic_published', (bool) $topic_status, $topic_id );
  1010. }
  1011. /**
  1012. * Is the topic marked as spam?
  1013. *
  1014. * @since bbPress (r2727)
  1015. *
  1016. * @param int $topic_id Optional. Topic id
  1017. * @uses bbp_get_topic_id() To get the topic id
  1018. * @uses bbp_get_topic_status() To get the topic status
  1019. * @uses apply_filters() Calls 'bbp_is_topic_spam' with the topic id
  1020. * @return bool True if spam, false if not.
  1021. */
  1022. function bbp_is_topic_spam( $topic_id = 0 ) {
  1023. $topic_status = bbp_get_topic_status( bbp_get_topic_id( $topic_id ) ) === bbp_get_spam_status_id();
  1024. return (bool) apply_filters( 'bbp_is_topic_spam', (bool) $topic_status, $topic_id );
  1025. }
  1026. /**
  1027. * Is the topic trashed?
  1028. *
  1029. * @since bbPress (r2888)
  1030. *
  1031. * @param int $topic_id Optional. Topic id
  1032. * @uses bbp_get_topic_id() To get the topic id
  1033. * @uses bbp_get_topic_status() To get the topic status
  1034. * @uses apply_filters() Calls 'bbp_is_topic_trash' with the topic id
  1035. * @return bool True if trashed, false if not.
  1036. */
  1037. function bbp_is_topic_trash( $topic_id = 0 ) {
  1038. $topic_status = bbp_get_topic_status( bbp_get_topic_id( $topic_id ) ) === bbp_get_trash_status_id();
  1039. return (bool) apply_filters( 'bbp_is_topic_trash', (bool) $topic_status, $topic_id );
  1040. }
  1041. /**
  1042. * Is the posted by an anonymous user?
  1043. *
  1044. * @since bbPress (r2753)
  1045. *
  1046. * @param int $topic_id Optional. Topic id
  1047. * @uses bbp_get_topic_id() To get the topic id
  1048. * @uses bbp_get_topic_author_id() To get the topic author id
  1049. * @uses get_post_meta() To get the anonymous user name and email meta
  1050. * @uses apply_filters() Calls 'bbp_is_topic_anonymous' with the topic id
  1051. * @return bool True if the post is by an anonymous user, false if not.
  1052. */
  1053. function bbp_is_topic_anonymous( $topic_id = 0 ) {
  1054. $topic_id = bbp_get_topic_id( $topic_id );
  1055. $retval = false;
  1056. if ( !bbp_get_topic_author_id( $topic_id ) )
  1057. $retval = true;
  1058. elseif ( get_post_meta( $topic_id, '_bbp_anonymous_name', true ) )
  1059. $retval = true;
  1060. elseif ( get_post_meta( $topic_id, '_bbp_anonymous_email', true ) )
  1061. $retval = true;
  1062. // The topic is by an anonymous user
  1063. return (bool) apply_filters( 'bbp_is_topic_anonymous', $retval, $topic_id );
  1064. }
  1065. /**
  1066. * Deprecated. Use bbp_topic_author_display_name() instead.
  1067. *
  1068. * Output the author of the topic.
  1069. *
  1070. * @since bbPress (r2590)
  1071. * @deprecated bbPress (r5119)
  1072. *
  1073. * @param int $topic_id Optional. Topic id
  1074. * @uses bbp_get_topic_author() To get the topic author
  1075. */
  1076. function bbp_topic_author( $topic_id = 0 ) {
  1077. echo bbp_get_topic_author( $topic_id );
  1078. }
  1079. /**
  1080. * Deprecated. Use bbp_get_topic_author_display_name() instead.
  1081. *
  1082. * Return the author of the topic
  1083. *
  1084. * @since bbPress (r2590)
  1085. * @deprecated bbPress (r5119)
  1086. *
  1087. * @param int $topic_id Optional. Topic id
  1088. * @uses bbp_get_topic_id() To get the topic id
  1089. * @uses bbp_is_topic_anonymous() To check if the topic is by an
  1090. * anonymous user
  1091. * @uses bbp_get_topic_author_id() To get the topic author id
  1092. * @uses get_the_author_meta() To get the display name of the author
  1093. * @uses get_post_meta() To get the name of the anonymous poster
  1094. * @uses apply_filters() Calls 'bbp_get_topic_author' with the author
  1095. * and topic id
  1096. * @return string Author of topic
  1097. */
  1098. function bbp_get_topic_author( $topic_id = 0 ) {
  1099. $topic_id = bbp_get_topic_id( $topic_id );
  1100. if ( !bbp_is_topic_anonymous( $topic_id ) ) {
  1101. $author = get_the_author_meta( 'display_name', bbp_get_topic_author_id( $topic_id ) );
  1102. } else {
  1103. $author = get_post_meta( $topic_id, '_bbp_anonymous_name', true );
  1104. }
  1105. return apply_filters( 'bbp_get_topic_author', $author, $topic_id );
  1106. }
  1107. /**
  1108. * Output the author ID of the topic
  1109. *
  1110. * @since bbPress (r2590)
  1111. *
  1112. * @param int $topic_id Optional. Topic id
  1113. * @uses bbp_get_topic_author_id() To get the topic author id
  1114. */
  1115. function bbp_topic_author_id( $topic_id = 0 ) {
  1116. echo bbp_get_topic_author_id( $topic_id );
  1117. }
  1118. /**
  1119. * Return the author ID of the topic
  1120. *
  1121. * @since bbPress (r2590)
  1122. *
  1123. * @param int $topic_id Optional. Topic id
  1124. * @uses bbp_get_topic_id() To get the topic id
  1125. * @uses get_post_field() To get the topic author id
  1126. * @uses apply_filters() Calls 'bbp_get_topic_author_id' with the author
  1127. * id and topic id
  1128. * @return string Author of topic
  1129. */
  1130. function bbp_get_topic_author_id( $topic_id = 0 ) {
  1131. $topic_id = bbp_get_topic_id( $topic_id );
  1132. $author_id = get_post_field( 'post_author', $topic_id );
  1133. return (int) apply_filters( 'bbp_get_topic_author_id', (int) $author_id, $topic_id );
  1134. }
  1135. /**
  1136. * Output the author display_name of the topic
  1137. *
  1138. * @since bbPress (r2590)
  1139. *
  1140. * @param int $topic_id Optional. Topic id
  1141. * @uses bbp_get_topic_author_display_name() To get the topic author's display
  1142. * name
  1143. */
  1144. function bbp_topic_author_display_name( $topic_id = 0 ) {
  1145. echo bbp_get_topic_author_display_name( $topic_id );
  1146. }
  1147. /**
  1148. * Return the author display_name of the topic
  1149. *
  1150. * @since bbPress (r2485)
  1151. *
  1152. * @param int $topic_id Optional. Topic id
  1153. * @uses bbp_get_topic_id() To get the topic id
  1154. * @uses bbp_is_topic_anonymous() To check if the topic is by an
  1155. * anonymous user
  1156. * @uses bbp_get_topic_author_id() To get the topic author id
  1157. * @uses get_the_author_meta() To get the author meta
  1158. * @uses get_post_meta() To get the anonymous user name
  1159. * @uses apply_filters() Calls 'bbp_get_topic_author_id' with the
  1160. * display name and topic id
  1161. * @return string Topic's author's display name
  1162. */
  1163. function bbp_get_topic_author_display_name( $topic_id = 0 ) {
  1164. $topic_id = bbp_get_topic_id( $topic_id );
  1165. // Check for anonymous user
  1166. if ( !bbp_is_topic_anonymous( $topic_id ) ) {
  1167. // Get the author ID
  1168. $author_id = bbp_get_topic_author_id( $topic_id );
  1169. // Try to get a display name
  1170. $author_name = get_the_author_meta( 'display_name', $author_id );
  1171. // Fall back to user login
  1172. if ( empty( $author_name ) ) {
  1173. $author_name = get_the_author_meta( 'user_login', $author_id );
  1174. }
  1175. // User does not have an account
  1176. } else {
  1177. $author_name = get_post_meta( $topic_id, '_bbp_anonymous_name', true );
  1178. }
  1179. // If nothing could be found anywhere, use Anonymous
  1180. if ( empty( $author_name ) )
  1181. $author_name = __( 'Anonymous', 'bbpress' );
  1182. // Encode possible UTF8 display names
  1183. if ( seems_utf8( $author_name ) === false )
  1184. $author_name = utf8_encode( $author_name );
  1185. return apply_filters( 'bbp_get_topic_author_display_name', $author_name, $topic_id );
  1186. }
  1187. /**
  1188. * Output the author avatar of the topic
  1189. *
  1190. * @since bbPress (r2590)
  1191. *
  1192. * @param int $topic_id Optional. Topic id
  1193. * @param int $size Optional. Avatar size. Defaults to 40
  1194. * @uses bbp_get_topic_author_avatar() To get the topic author avatar
  1195. */
  1196. function bbp_topic_author_avatar( $topic_id = 0, $size = 40 ) {
  1197. echo bbp_get_topic_author_avatar( $topic_id, $size );
  1198. }
  1199. /**
  1200. * Return the author avatar of the topic
  1201. *
  1202. * @since bbPress (r2590)
  1203. *
  1204. * @param int $topic_id Optional. Topic id
  1205. * @param int $size Optional. Avatar size. Defaults to 40
  1206. * @uses bbp_get_topic_id() To get the topic id
  1207. * @uses bbp_is_topic_anonymous() To check if the topic is by an
  1208. * anonymous user
  1209. * @uses bbp_get_topic_author_id() To get the topic author id
  1210. * @uses get_post_meta() To get the anonymous user's email
  1211. * @uses get_avatar() To get the avatar
  1212. * @uses apply_filters() Calls 'bbp_get_topic_author_avatar' with the
  1213. * avatar, topic id and size
  1214. * @return string Avatar of the author of the topic
  1215. */
  1216. function bbp_get_topic_author_avatar( $topic_id = 0, $size = 40 ) {
  1217. $author_avatar = '';
  1218. $topic_id = bbp_get_topic_id( $topic_id );
  1219. if ( !empty( $topic_id ) ) {
  1220. if ( !bbp_is_topic_anonymous( $topic_id ) ) {
  1221. $author_avatar = get_avatar( bbp_get_topic_author_id( $topic_id ), $size );
  1222. } else {
  1223. $author_avatar = get_avatar( get_post_meta( $topic_id, '_bbp_anonymous_email', true ), $size );
  1224. }
  1225. }
  1226. return apply_filters( 'bbp_get_topic_author_avatar', $author_avatar, $topic_id, $size );
  1227. }
  1228. /**
  1229. * Output the author link of the topic
  1230. *
  1231. * @since bbPress (r2717)
  1232. *
  1233. * @param mixed|int $args If it is an integer, it is used as topic_id. Optional.
  1234. * @uses bbp_get_topic_author_link() To get the topic author link
  1235. */
  1236. function bbp_topic_author_link( $args = '' ) {
  1237. echo bbp_get_topic_author_link( $args );
  1238. }
  1239. /**
  1240. * Return the author link of the topic
  1241. *
  1242. * @since bbPress (r2717)
  1243. *
  1244. * @param mixed|int $args If it is an integer, it is used as topic id.
  1245. * Optional.
  1246. * @uses bbp_get_topic_id() To get the topic id
  1247. * @uses bbp_get_topic_author_display_name() To get the topic author
  1248. * @uses bbp_is_topic_anonymous() To check if the topic is by an
  1249. * anonymous user
  1250. * @uses bbp_get_topic_author_url() To get the topic author url
  1251. * @uses bbp_get_topic_author_avatar() To get the topic author avatar
  1252. * @uses bbp_get_topic_author_display_name() To get the topic author display
  1253. * name
  1254. * @uses bbp_get_user_display_role() To get the topic author display role
  1255. * @uses bbp_get_topic_author_id() To get the topic author id
  1256. * @uses apply_filters() Calls 'bbp_get_topic_author_link' with the link
  1257. * and args
  1258. * @return string Author link of topic
  1259. */
  1260. function bbp_get_topic_author_link( $args = '' ) {
  1261. // Parse arguments against default values
  1262. $r = bbp_parse_args( $args, array(
  1263. 'post_id' => 0,
  1264. 'link_title' => '',
  1265. 'type' => 'both',
  1266. 'size' => 80,
  1267. 'sep' => '&nbsp;',
  1268. 'show_role' => false
  1269. ), 'get_topic_author_link' );
  1270. // Used as topic_id
  1271. if ( is_numeric( $args ) ) {
  1272. $topic_id = bbp_get_topic_id( $args );
  1273. } else {
  1274. $topic_id = bbp_get_topic_id( $r['post_id'] );
  1275. }
  1276. // Topic ID is good
  1277. if ( !empty( $topic_id ) ) {
  1278. // Get some useful topic information
  1279. $author_url = bbp_get_topic_author_url( $topic_id );
  1280. $anonymous = bbp_is_topic_anonymous( $topic_id );
  1281. // Tweak link title if empty
  1282. if ( empty( $r['link_title'] ) ) {
  1283. $link_title = sprintf( empty( $anonymous ) ? __( 'View %s\'s profile', 'bbpress' ) : __( 'Visit %s\'s website', 'bbpress' ), bbp_get_topic_author_display_name( $topic_id ) );
  1284. // Use what was passed if not
  1285. } else {
  1286. $link_title = $r['link_title'];
  1287. }
  1288. // Setup title and author_links array
  1289. $link_title = !empty( $link_title ) ? ' title="' . esc_attr( $link_title ) . '"' : '';
  1290. $author_links = array();
  1291. // Get avatar
  1292. if ( 'avatar' === $r['type'] || 'both' === $r['type'] ) {
  1293. $author_links['avatar'] = bbp_get_topic_author_avatar( $topic_id, $r['size'] );
  1294. }
  1295. // Get display name
  1296. if ( 'name' === $r['type'] || 'both' === $r['type'] ) {
  1297. $author_links['name'] = bbp_get_topic_author_display_name( $topic_id );
  1298. }
  1299. // Link class
  1300. $link_class = ' class="bbp-author-' . esc_attr( $r['type'] ) . '"';
  1301. // Add links if not anonymous
  1302. if ( empty( $anonymous ) && bbp_user_has_profile( bbp_get_topic_author_id( $topic_id ) ) ) {
  1303. // Assemble the links
  1304. foreach ( $author_links as $link => $link_text ) {
  1305. $link_class = ' class="bbp-author-' . esc_attr( $link ) . '"';
  1306. $author_link[] = sprintf( '<a href="%1$s"%2$s%3$s>%4$s</a>', esc_url( $author_url ), $link_title, $link_class, $link_text );
  1307. }
  1308. if ( true === $r['show_role'] ) {
  1309. $author_link[] = bbp_get_topic_author_role( array( 'topic_id' => $topic_id ) );
  1310. }
  1311. $author_link = implode( $r['sep'], $author_link );
  1312. // No links if anonymous
  1313. } else {
  1314. $author_link = implode( $r['sep'], $author_links );
  1315. }
  1316. } else {
  1317. $author_link = '';
  1318. }
  1319. return apply_filters( 'bbp_get_topic_author_link', $author_link, $args );
  1320. }
  1321. /**
  1322. * Output the author url of the topic
  1323. *
  1324. * @since bbPress (r2590)
  1325. *
  1326. * @param int $topic_id Optional. Topic id
  1327. * @uses bbp_get_topic_author_url() To get the topic author url
  1328. */
  1329. function bbp_topic_author_url( $topic_id = 0 ) {
  1330. echo esc_url( bbp_get_topic_author_url( $topic_id ) );
  1331. }
  1332. /**
  1333. * Return the author url of the topic
  1334. *
  1335. * @since bbPress (r2590)
  1336. *
  1337. * @param int $topic_id Optional. Topic id
  1338. * @uses bbp_get_topic_id() To get the topic id
  1339. * @uses bbp_is_topic_anonymous() To check if the topic is by an anonymous
  1340. * user or not
  1341. * @uses bbp_user_has_profile() To check if the user has a profile
  1342. * @uses bbp_get_topic_author_id() To get topic author id
  1343. * @uses bbp_get_user_profile_url() To get profile url
  1344. * @uses get_post_meta() To get anonmous user's website
  1345. * @uses apply_filters() Calls 'bbp_get_topic_author_url' with the link &
  1346. * topic id
  1347. * @return string Author URL of topic
  1348. */
  1349. function bbp_get_topic_author_url( $topic_id = 0 ) {
  1350. $topic_id = bbp_get_topic_id( $topic_id );
  1351. // Check for anonymous user or non-existant user
  1352. if ( !bbp_is_topic_anonymous( $topic_id ) && bbp_user_has_profile( bbp_get_topic_author_id( $topic_id ) ) ) {
  1353. $author_url = bbp_get_user_profile_url( bbp_get_topic_author_id( $topic_id ) );
  1354. } else {
  1355. $author_url = get_post_meta( $topic_id, '_bbp_anonymous_website', true );
  1356. // Set empty author_url as empty string
  1357. if ( empty( $author_url ) ) {
  1358. $author_url = '';
  1359. }
  1360. }
  1361. return apply_filters( 'bbp_get_topic_author_url', $author_url, $topic_id );
  1362. }
  1363. /**
  1364. * Output the topic author email address
  1365. *
  1366. * @since bbPress (r3445)
  1367. *
  1368. * @param int $topic_id Optional. Reply id
  1369. * @uses bbp_get_topic_author_email() To get the topic author email
  1370. */
  1371. function bbp_topic_author_email( $topic_id = 0 ) {
  1372. echo bbp_get_topic_author_email( $topic_id );
  1373. }
  1374. /**
  1375. * Return the topic author email address
  1376. *
  1377. * @since bbPress (r3445)
  1378. *
  1379. * @param int $topic_id Optional. Reply id
  1380. * @uses bbp_get_topic_id() To get the topic id
  1381. * @uses bbp_is_topic_anonymous() To check if the topic is by an anonymous
  1382. * user
  1383. * @uses bbp_get_topic_author_id() To get the topic author id
  1384. * @uses get_userdata() To get the user data
  1385. * @uses get_post_meta() To get the anonymous poster's email
  1386. * @uses apply_filters() Calls bbp_get_topic_author_email with the author
  1387. * email & topic id
  1388. * @return string Topic author email address
  1389. */
  1390. function bbp_get_topic_author_email( $topic_id = 0 ) {
  1391. $topic_id = bbp_get_topic_id( $topic_id );
  1392. // Not anonymous user
  1393. if ( !bbp_is_topic_anonymous( $topic_id ) ) {
  1394. // Use topic author email address
  1395. $user_id = bbp_get_topic_author_id( $topic_id );
  1396. $user = get_userdata( $user_id );
  1397. $author_email = !empty( $user->user_email ) ? $user->user_email : '';
  1398. // Anonymous
  1399. } else {
  1400. // Get email from post meta
  1401. $author_email = get_post_meta( $topic_id, '_bbp_anonymous_email', true );
  1402. // Sanity check for missing email address
  1403. if ( empty( $author_email ) ) {
  1404. $author_email = '';
  1405. }
  1406. }
  1407. return apply_filters( 'bbp_get_topic_author_email', $author_email, $topic_id );
  1408. }
  1409. /**
  1410. * Output the topic author role
  1411. *
  1412. * @since bbPress (r3860)
  1413. *
  1414. * @param array $args Optional.
  1415. * @uses bbp_get_topic_author_role() To get the topic author role
  1416. */
  1417. function bbp_topic_author_role( $args = array() ) {
  1418. echo bbp_get_topic_author_role( $args );
  1419. }
  1420. /**
  1421. * Return the topic author role
  1422. *
  1423. * @since bbPress (r3860)
  1424. *
  1425. * @param array $args Optional.
  1426. * @uses bbp_get_topic_id() To get the topic id
  1427. * @uses bbp_get_user_display_role() To get the user display role
  1428. * @uses bbp_get_topic_author_id() To get the topic author id
  1429. * @uses apply_filters() Calls bbp_get_topic_author_role with the author
  1430. * role & args
  1431. * @return string topic author role
  1432. */
  1433. function bbp_get_topic_author_role( $args = array() ) {
  1434. // Parse arguments against default values
  1435. $r = bbp_parse_args( $args, array(
  1436. 'topic_id' => 0,
  1437. 'class' => 'bbp-author-role',
  1438. 'before' => '',
  1439. 'after' => ''
  1440. ), 'get_topic_author_role' );
  1441. $topic_id = bbp_get_topic_id( $r['topic_id'] );
  1442. $role = bbp_get_user_display_role( bbp_get_topic_author_id( $topic_id ) );
  1443. $author_role = sprintf( '%1$s<div class="%2$s">%3$s</div>%4$s', $r['before'], $r['class'], $role, $r['after'] );
  1444. return apply_filters( 'bbp_get_topic_author_role', $author_role, $r );
  1445. }
  1446. /**
  1447. * Output the title of the forum a topic belongs to
  1448. *
  1449. * @since bbPress (r2485)
  1450. *
  1451. * @param int $topic_id Optional. Topic id
  1452. * @uses bbp_get_topic_forum_title() To get the topic's forum title
  1453. */
  1454. function bbp_topic_forum_title( $topic_id = 0 ) {
  1455. echo bbp_get_topic_forum_title( $topic_id );
  1456. }
  1457. /**
  1458. * Return the title of the forum a topic belongs to
  1459. *
  1460. * @since bbPress (r2485)
  1461. *
  1462. * @param int $topic_id Optional. Topic id
  1463. * @uses bbp_get_topic_id() To get topic id
  1464. * @uses bbp_get_topic_forum_id() To get topic's forum id
  1465. * @uses apply_filters() Calls 'bbp_get_topic_forum' with the forum
  1466. * title and topic id
  1467. * @return string Topic forum title
  1468. */
  1469. function bbp_get_topic_forum_title( $topic_id = 0 ) {
  1470. $topic_id = bbp_get_topic_id( $topic_id );
  1471. $forum_id = bbp_get_topic_forum_id( $topic_id );
  1472. return apply_filters( 'bbp_get_topic_forum', bbp_get_forum_title( $forum_id ), $topic_id, $forum_id );
  1473. }
  1474. /**
  1475. * Output the forum id a topic belongs to
  1476. *
  1477. * @since bbPress (r2491)
  1478. *
  1479. * @param int $topic_id Optional. Topic id
  1480. * @uses bbp_get_topic_forum_id()
  1481. */
  1482. function bbp_topic_forum_id( $topic_id = 0 ) {
  1483. echo bbp_get_topic_forum_id( $topic_id );
  1484. }
  1485. /**
  1486. * Return the forum id a topic belongs to
  1487. *
  1488. * @since bbPress (r2491)
  1489. *
  1490. * @param int $topic_id Optional. Topic id
  1491. * @uses bbp_get_topic_id() To get topic id
  1492. * @uses get_post_meta() To retrieve get topic's forum id meta
  1493. * @uses apply_filters() Calls 'bbp_get_topic_forum_id' with the forum
  1494. * id and topic id
  1495. * @return int Topic forum id
  1496. */
  1497. function bbp_get_topic_forum_id( $topic_id = 0 ) {
  1498. $topic_id = bbp_get_topic_id( $topic_id );
  1499. $forum_id = get_post_meta( $topic_id, '_bbp_forum_id', true );
  1500. return (int) apply_filters( 'bbp_get_topic_forum_id', (int) $forum_id, $topic_id );
  1501. }
  1502. /**
  1503. * Output the topics last active ID
  1504. *
  1505. * @since bbPress (r2860)
  1506. *
  1507. * @param int $topic_id Optional. Forum id
  1508. * @uses bbp_get_topic_last_active_id() To get the topic's last active id
  1509. */
  1510. function bbp_topic_last_active_id( $topic_id = 0 ) {
  1511. echo bbp_get_topic_last_active_id( $topic_id );
  1512. }
  1513. /**
  1514. * Return the topics last active ID
  1515. *
  1516. * @since bbPress (r2860)
  1517. *
  1518. * @param int $topic_id Optional. Forum id
  1519. * @uses bbp_get_topic_id() To get the topic id
  1520. * @uses get_post_meta() To get the topic's last active id
  1521. * @uses apply_filters() Calls 'bbp_get_topic_last_active_id' with
  1522. * the last active id and topic id
  1523. * @return int Forum's last active id
  1524. */
  1525. function bbp_get_topic_last_active_id( $topic_id = 0 ) {
  1526. $topic_id = bbp_get_topic_id( $topic_id );
  1527. $active_id = get_post_meta( $topic_id, '_bbp_last_active_id', true );
  1528. return (int) apply_filters( 'bbp_get_topic_last_active_id', (int) $active_id, $topic_id );
  1529. }
  1530. /**
  1531. * Output the topics last update date/time (aka freshness)
  1532. *
  1533. * @since bbPress (r2625)
  1534. *
  1535. * @param int $topic_id Optional. Topic id
  1536. * @uses bbp_get_topic_last_active_time() To get topic freshness
  1537. */
  1538. function bbp_topic_last_active_time( $topic_id = 0 ) {
  1539. echo bbp_get_topic_last_active_time( $topic_id );
  1540. }
  1541. /**
  1542. * Return the topics last update date/time (aka freshness)
  1543. *
  1544. * @since bbPress (r2625)
  1545. *
  1546. * @param int $topic_id Optional. Topic id
  1547. * @uses bbp_get_topic_id() To get topic id
  1548. * @uses get_post_meta() To get the topic lst active meta
  1549. * @uses bbp_get_topic_last_reply_id() To get topic last reply id
  1550. * @uses get_post_field() To get the post date of topic/reply
  1551. * @uses bbp_convert_date() To convert date
  1552. * @uses bbp_get_time_since() To get time in since format
  1553. * @uses apply_filters() Calls 'bbp_get_topic_last_active' with topic
  1554. * freshness and topic id
  1555. * @return string Topic freshness
  1556. */
  1557. function bbp_get_topic_last_active_time( $topic_id = 0 ) {
  1558. $topic_id = bbp_get_topic_id( $topic_id );
  1559. // Try to get the most accurate freshness time possible
  1560. $last_active = get_post_meta( $topic_id, '_bbp_last_active_time', true );
  1561. if ( empty( $last_active ) ) {
  1562. $reply_id = bbp_get_topic_last_reply_id( $topic_id );
  1563. if ( !empty( $reply_id ) ) {
  1564. $last_active = get_post_field( 'post_date', $reply_id );
  1565. } else {
  1566. $last_active = get_post_field( 'post_date', $topic_id );
  1567. }
  1568. }
  1569. $last_active = !empty( $last_active ) ? bbp_get_time_since( bbp_convert_date( $last_active ) ) : '';
  1570. // Return the time since
  1571. return apply_filters( 'bbp_get_topic_last_active', $last_active, $topic_id );
  1572. }
  1573. /** Topic Subscriptions *******************************************************/
  1574. /**
  1575. * Output the topic subscription link
  1576. *
  1577. * @since bbPress (r5156)
  1578. *
  1579. * @uses bbp_get_topic_subscription_link()
  1580. */
  1581. function bbp_topic_subscription_link( $args = array() ) {
  1582. echo bbp_get_topic_subscription_link( $args );
  1583. }
  1584. /**
  1585. * Get the forum subscription link
  1586. *
  1587. * A custom wrapper for bbp_get_user_subscribe_link()
  1588. *
  1589. * @since bbPress (r5156)
  1590. *
  1591. * @uses bbp_parse_args()
  1592. * @uses bbp_get_user_subscribe_link()
  1593. * @uses apply_filters() Calls 'bbp_get_topic_subscribe_link'
  1594. */
  1595. function bbp_get_topic_subscription_link( $args = array() ) {
  1596. // No link
  1597. $retval = false;
  1598. // Parse the arguments
  1599. $r = bbp_parse_args( $args, array(
  1600. 'user_id' => 0,
  1601. 'topic_id' => 0,
  1602. 'before' => '&nbsp;|&nbsp;',
  1603. 'after' => '',
  1604. 'subscribe' => __( 'Subscribe', 'bbpress' ),
  1605. 'unsubscribe' => __( 'Unsubscribe', 'bbpress' )
  1606. ), 'get_forum_subscribe_link' );
  1607. // Get the link
  1608. $retval = bbp_get_user_subscribe_link( $r );
  1609. return apply_filters( 'bbp_get_topic_subscribe_link', $retval, $r );
  1610. }
  1611. /** Topic Favorites ***********************************************************/
  1612. /**
  1613. * Output the topic favorite link
  1614. *
  1615. * @since bbPress (r5156)
  1616. *
  1617. * @uses bbp_get_topic_favorite_link()
  1618. */
  1619. function bbp_topic_favorite_link( $args = array() ) {
  1620. echo bbp_get_topic_favorite_link( $args );
  1621. }
  1622. /**
  1623. * Get the forum favorite link
  1624. *
  1625. * A custom wrapper for bbp_get_user_favorite_link()
  1626. *
  1627. * @since bbPress (r5156)
  1628. *
  1629. * @uses bbp_parse_args()
  1630. * @uses bbp_get_user_favorites_link()
  1631. * @uses apply_filters() Calls 'bbp_get_topic_favorite_link'
  1632. */
  1633. function bbp_get_topic_favorite_link( $args = array() ) {
  1634. // No link
  1635. $retval = false;
  1636. // Parse the arguments
  1637. $r = bbp_parse_args( $args, array(
  1638. 'user_id' => 0,
  1639. 'topic_id' => 0,
  1640. 'before' => '',
  1641. 'after' => '',
  1642. 'favorite' => __( 'Favorite', 'bbpress' ),
  1643. 'favorited' => __( 'Unfavorite', 'bbpress' )
  1644. ), 'get_forum_favorite_link' );
  1645. // Get the link
  1646. $retval = bbp_get_user_favorites_link( $r );
  1647. return apply_filters( 'bbp_get_topic_favorite_link', $retval, $r );
  1648. }
  1649. /** Topic Last Reply **********************************************************/
  1650. /**
  1651. * Output the id of the topics last reply
  1652. *
  1653. * @since bbPress (r2625)
  1654. *
  1655. * @param int $topic_id Optional. Topic id
  1656. * @uses bbp_get_topic_last_reply_id() To get the topic last reply id
  1657. */
  1658. function bbp_topic_last_reply_id( $topic_id = 0 ) {
  1659. echo bbp_get_topic_last_reply_id( $topic_id );
  1660. }
  1661. /**
  1662. * Return the topics last update date/time (aka freshness)
  1663. *
  1664. * @since bbPress (r2625)
  1665. *
  1666. * @param int $topic_id Optional. Topic id
  1667. * @uses bbp_get_topic_id() To get the topic id
  1668. * @uses get_post_meta() To get the last reply id meta
  1669. * @uses apply_filters() Calls 'bbp_get_topic_last_reply_id' with the
  1670. * last reply id and topic id
  1671. * @return int Topic last reply id
  1672. */
  1673. function bbp_get_topic_last_reply_id( $topic_id = 0 ) {
  1674. $topic_id = bbp_get_topic_id( $topic_id );
  1675. $reply_id = get_post_meta( $topic_id, '_bbp_last_reply_id', true );
  1676. if ( empty( $reply_id ) )
  1677. $reply_id = $topic_id;
  1678. return (int) apply_filters( 'bbp_get_topic_last_reply_id', (int) $reply_id, $topic_id );
  1679. }
  1680. /**
  1681. * Output the title of the last reply inside a topic
  1682. *
  1683. * @param int $topic_id Optional. Topic id
  1684. * @uses bbp_get_topic_last_reply_title() To get the topic last reply title
  1685. */
  1686. function bbp_topic_last_reply_title( $topic_id = 0 ) {
  1687. echo bbp_get_topic_last_reply_title( $topic_id );
  1688. }
  1689. /**
  1690. * Return the title of the last reply inside a topic
  1691. *
  1692. * @param int $topic_id Optional. Topic id
  1693. * @uses bbp_get_topic_id() To get the topic id
  1694. * @uses bbp_get_topic_last_reply_id() To get the topic last reply id
  1695. * @uses bbp_get_reply_title() To get the reply title
  1696. * @uses apply_filters() Calls 'bbp_get_topic_last_topic_title' with
  1697. * the reply title and topic id
  1698. * @return string Topic last reply title
  1699. */
  1700. function bbp_get_topic_last_reply_title( $topic_id = 0 ) {
  1701. $topic_id = bbp_get_topic_id( $topic_id );
  1702. return apply_filters( 'bbp_get_topic_last_topic_title', bbp_get_reply_title( bbp_get_topic_last_reply_id( $topic_id ) ), $topic_id );
  1703. }
  1704. /**
  1705. * Output the link to the last reply in a topic
  1706. *
  1707. * @since bbPress (r2464)
  1708. *
  1709. * @param int $topic_id Optional. Topic id
  1710. * @uses bbp_get_topic_last_reply_permalink() To get the topic's last reply link
  1711. */
  1712. function bbp_topic_last_reply_permalink( $topic_id = 0 ) {
  1713. echo esc_url( bbp_get_topic_last_reply_permalink( $topic_id ) );
  1714. }
  1715. /**
  1716. * Return the link to the last reply in a topic
  1717. *
  1718. * @since bbPress (r2464)
  1719. *
  1720. * @param int $topic_id Optional. Topic id
  1721. * @uses bbp_get_topic_id() To get the topic id
  1722. * @uses bbp_get_topic_last_reply_id() To get the topic last reply id
  1723. * @uses bbp_get_reply_permalink() To get the reply permalink
  1724. * @uses apply_filters() Calls 'bbp_get_topic_last_topic_permalink' with
  1725. * the reply permalink and topic id
  1726. * @return string Permanent link to the reply
  1727. */
  1728. function bbp_get_topic_last_reply_permalink( $topic_id = 0 ) {
  1729. $topic_id = bbp_get_topic_id( $topic_id );
  1730. return apply_filters( 'bbp_get_topic_last_reply_permalink', bbp_get_reply_permalink( bbp_get_topic_last_reply_id( $topic_id ) ) );
  1731. }
  1732. /**
  1733. * Output the link to the last reply in a topic
  1734. *
  1735. * @since bbPress (r2683)
  1736. *
  1737. * @param int $topic_id Optional. Topic id
  1738. * @uses bbp_get_topic_last_reply_url() To get the topic last reply url
  1739. */
  1740. function bbp_topic_last_reply_url( $topic_id = 0 ) {
  1741. echo esc_url( bbp_get_topic_last_reply_url( $topic_id ) );
  1742. }
  1743. /**
  1744. * Return the link to the last reply in a topic
  1745. *
  1746. * @since bbPress (r2683)
  1747. *
  1748. * @param int $topic_id Optional. Topic id
  1749. * @uses bbp_get_topic_id() To get the topic id
  1750. * @uses bbp_get_topic_last_reply_id() To get the topic last reply id
  1751. * @uses bbp_get_reply_url() To get the reply url
  1752. * @uses bbp_get_reply_permalink() To get the reply permalink
  1753. * @uses apply_filters() Calls 'bbp_get_topic_last_topic_url' with
  1754. * the reply url and topic id
  1755. * @return string Topic last reply url
  1756. */
  1757. function bbp_get_topic_last_reply_url( $topic_id = 0 ) {
  1758. $topic_id = bbp_get_topic_id( $topic_id );
  1759. $reply_id = bbp_get_topic_last_reply_id( $topic_id );
  1760. if ( !empty( $reply_id ) && ( $reply_id !== $topic_id ) ) {
  1761. $reply_url = bbp_get_reply_url( $reply_id );
  1762. } else {
  1763. $reply_url = bbp_get_topic_permalink( $topic_id );
  1764. }
  1765. return apply_filters( 'bbp_get_topic_last_reply_url', $reply_url );
  1766. }
  1767. /**
  1768. * Output link to the most recent activity inside a topic, complete with link
  1769. * attributes and content.
  1770. *
  1771. * @since bbPress (r2625)
  1772. *
  1773. * @param int $topic_id Optional. Topic id
  1774. * @uses bbp_get_topic_freshness_link() To get the topic freshness link
  1775. */
  1776. function bbp_topic_freshness_link( $topic_id = 0 ) {
  1777. echo bbp_get_topic_freshness_link( $topic_id );
  1778. }
  1779. /**
  1780. * Returns link to the most recent activity inside a topic, complete
  1781. * with link attributes and content.
  1782. *
  1783. * @since bbPress (r2625)
  1784. *
  1785. * @param int $topic_id Optional. Topic id
  1786. * @uses bbp_get_topic_id() To get the topic id
  1787. * @uses bbp_get_topic_last_reply_url() To get the topic last reply url
  1788. * @uses bbp_get_topic_last_reply_title() To get the reply title
  1789. * @uses bbp_get_topic_last_active_time() To get the topic freshness
  1790. * @uses apply_filters() Calls 'bbp_get_topic_freshness_link' with the
  1791. * link and topic id
  1792. * @return string Topic freshness link
  1793. */
  1794. function bbp_get_topic_freshness_link( $topic_id = 0 ) {
  1795. $topic_id = bbp_get_topic_id( $topic_id );
  1796. $link_url = bbp_get_topic_last_reply_url( $topic_id );
  1797. $title = bbp_get_topic_last_reply_title( $topic_id );
  1798. $time_since = bbp_get_topic_last_active_time( $topic_id );
  1799. if ( !empty( $time_since ) )
  1800. $anchor = '<a href="' . esc_url( $link_url ) . '" title="' . esc_attr( $title ) . '">' . esc_html( $time_since ) . '</a>';
  1801. else
  1802. $anchor = __( 'No Replies', 'bbpress' );
  1803. return apply_filters( 'bbp_get_topic_freshness_link', $anchor, $topic_id, $time_since, $link_url, $title );
  1804. }
  1805. /**
  1806. * Output the replies link of the topic
  1807. *
  1808. * @since bbPress (r2740)
  1809. *
  1810. * @param int $topic_id Optional. Topic id
  1811. * @uses bbp_get_topic_replies_link() To get the topic replies link
  1812. */
  1813. function bbp_topic_replies_link( $topic_id = 0 ) {
  1814. echo bbp_get_topic_replies_link( $topic_id );
  1815. }
  1816. /**
  1817. * Return the replies link of the topic
  1818. *
  1819. * @since bbPress (r2740)
  1820. *
  1821. * @param int $topic_id Optional. Topic id
  1822. * @uses bbp_get_topic_id() To get the topic id
  1823. * @uses bbp_get_topic() To get the topic
  1824. * @uses bbp_get_topic_reply_count() To get the topic reply count
  1825. * @uses bbp_get_topic_permalink() To get the topic permalink
  1826. * @uses remove_query_arg() To remove args from the url
  1827. * @uses bbp_get_topic_reply_count_hidden() To get the topic hidden
  1828. * reply count
  1829. * @uses current_user_can() To check if the current user can edit others
  1830. * replies
  1831. * @uses add_query_arg() To add custom args to the url
  1832. * @uses apply_filters() Calls 'bbp_get_topic_replies_link' with the
  1833. * replies link and topic id
  1834. */
  1835. function bbp_get_topic_replies_link( $topic_id = 0 ) {
  1836. $topic = bbp_get_topic( bbp_get_topic_id( (int) $topic_id ) );
  1837. $topic_id = $topic->ID;
  1838. $replies = sprintf( _n( '%s reply', '%s replies', bbp_get_topic_reply_count( $topic_id, true ), 'bbpress' ), bbp_get_topic_reply_count( $topic_id ) );
  1839. $retval = '';
  1840. // First link never has view=all
  1841. if ( bbp_get_view_all( 'edit_others_replies' ) )
  1842. $retval .= "<a href='" . esc_url( bbp_remove_view_all( bbp_get_topic_permalink( $topic_id ) ) ) . "'>$replies</a>";
  1843. else
  1844. $retval .= $replies;
  1845. // Any deleted replies?
  1846. $deleted = bbp_get_topic_reply_count_hidden( $topic_id );
  1847. // This forum has hidden topics
  1848. if ( !empty( $deleted ) && current_user_can( 'edit_others_replies' ) ) {
  1849. // Extra text
  1850. $extra = sprintf( __( ' (+ %d hidden)', 'bbpress' ), $deleted );
  1851. // No link
  1852. if ( bbp_get_view_all() ) {
  1853. $retval .= " $extra";
  1854. // Link
  1855. } else {
  1856. $retval .= " <a href='" . esc_url( bbp_add_view_all( bbp_get_topic_permalink( $topic_id ), true ) ) . "'>$extra</a>";
  1857. }
  1858. }
  1859. return apply_filters( 'bbp_get_topic_replies_link', $retval, $topic_id );
  1860. }
  1861. /**
  1862. * Output total reply count of a topic
  1863. *
  1864. * @since bbPress (r2485)
  1865. *
  1866. * @param int $topic_id Optional. Topic id
  1867. * @param boolean $integer Optional. Whether or not to format the result
  1868. * @uses bbp_get_topic_reply_count() To get the topic reply count
  1869. */
  1870. function bbp_topic_reply_count( $topic_id = 0, $integer = false ) {
  1871. echo bbp_get_topic_reply_count( $topic_id, $integer );
  1872. }
  1873. /**
  1874. * Return total reply count of a topic
  1875. *
  1876. * @since bbPress (r2485)
  1877. *
  1878. * @param int $topic_id Optional. Topic id
  1879. * @param boolean $integer Optional. Whether or not to format the result
  1880. * @uses bbp_get_topic_id() To get the topic id
  1881. * @uses get_post_meta() To get the topic reply count meta
  1882. * @uses apply_filters() Calls 'bbp_get_topic_reply_count' with the
  1883. * reply count and topic id
  1884. * @return int Reply count
  1885. */
  1886. function bbp_get_topic_reply_count( $topic_id = 0, $integer = false ) {
  1887. $topic_id = bbp_get_topic_id( $topic_id );
  1888. $replies = (int) get_post_meta( $topic_id, '_bbp_reply_count', true );
  1889. $filter = ( true === $integer ) ? 'bbp_get_topic_reply_count_int' : 'bbp_get_topic_reply_count';
  1890. return apply_filters( $filter, $replies, $topic_id );
  1891. }
  1892. /**
  1893. * Output total post count of a topic
  1894. *
  1895. * @since bbPress (r2954)
  1896. *
  1897. * @param int $topic_id Optional. Topic id
  1898. * @param boolean $integer Optional. Whether or not to format the result
  1899. * @uses bbp_get_topic_post_count() To get the topic post count
  1900. */
  1901. function bbp_topic_post_count( $topic_id = 0, $integer = false ) {
  1902. echo bbp_get_topic_post_count( $topic_id, $integer );
  1903. }
  1904. /**
  1905. * Return total post count of a topic
  1906. *
  1907. * @since bbPress (r2954)
  1908. *
  1909. * @param int $topic_id Optional. Topic id
  1910. * @param boolean $integer Optional. Whether or not to format the result
  1911. * @uses bbp_get_topic_id() To get the topic id
  1912. * @uses get_post_meta() To get the topic post count meta
  1913. * @uses apply_filters() Calls 'bbp_get_topic_post_count' with the
  1914. * post count and topic id
  1915. * @return int Post count
  1916. */
  1917. function bbp_get_topic_post_count( $topic_id = 0, $integer = false ) {
  1918. $topic_id = bbp_get_topic_id( $topic_id );
  1919. $replies = (int) get_post_meta( $topic_id, '_bbp_reply_count', true ) + 1;
  1920. $filter = ( true === $integer ) ? 'bbp_get_topic_post_count_int' : 'bbp_get_topic_post_count';
  1921. return apply_filters( $filter, $replies, $topic_id );
  1922. }
  1923. /**
  1924. * Output total hidden reply count of a topic (hidden includes trashed and
  1925. * spammed replies)
  1926. *
  1927. * @since bbPress (r2740)
  1928. *
  1929. * @param int $topic_id Optional. Topic id
  1930. * @param boolean $integer Optional. Whether or not to format the result
  1931. * @uses bbp_get_topic_reply_count_hidden() To get the topic hidden reply count
  1932. */
  1933. function bbp_topic_reply_count_hidden( $topic_id = 0, $integer = false ) {
  1934. echo bbp_get_topic_reply_count_hidden( $topic_id, $integer );
  1935. }
  1936. /**
  1937. * Return total hidden reply count of a topic (hidden includes trashed
  1938. * and spammed replies)
  1939. *
  1940. * @since bbPress (r2740)
  1941. *
  1942. * @param int $topic_id Optional. Topic id
  1943. * @param boolean $integer Optional. Whether or not to format the result
  1944. * @uses bbp_get_topic_id() To get the topic id
  1945. * @uses get_post_meta() To get the hidden reply count
  1946. * @uses apply_filters() Calls 'bbp_get_topic_reply_count_hidden' with
  1947. * the hidden reply count and topic id
  1948. * @return int Topic hidden reply count
  1949. */
  1950. function bbp_get_topic_reply_count_hidden( $topic_id = 0, $integer = false ) {
  1951. $topic_id = bbp_get_topic_id( $topic_id );
  1952. $replies = (int) get_post_meta( $topic_id, '_bbp_reply_count_hidden', true );
  1953. $filter = ( true === $integer ) ? 'bbp_get_topic_reply_count_hidden_int' : 'bbp_get_topic_reply_count_hidden';
  1954. return apply_filters( $filter, $replies, $topic_id );
  1955. }
  1956. /**
  1957. * Output total voice count of a topic
  1958. *
  1959. * @since bbPress (r2567)
  1960. *
  1961. * @param int $topic_id Optional. Topic id
  1962. * @uses bbp_get_topic_voice_count() To get the topic voice count
  1963. */
  1964. function bbp_topic_voice_count( $topic_id = 0, $integer = false ) {
  1965. echo bbp_get_topic_voice_count( $topic_id, $integer );
  1966. }
  1967. /**
  1968. * Return total voice count of a topic
  1969. *
  1970. * @since bbPress (r2567)
  1971. *
  1972. * @param int $topic_id Optional. Topic id
  1973. * @uses bbp_get_topic_id() To get the topic id
  1974. * @uses get_post_meta() To get the voice count meta
  1975. * @uses apply_filters() Calls 'bbp_get_topic_voice_count' with the
  1976. * voice count and topic id
  1977. * @return int Voice count of the topic
  1978. */
  1979. function bbp_get_topic_voice_count( $topic_id = 0, $integer = false ) {
  1980. $topic_id = bbp_get_topic_id( $topic_id );
  1981. $voices = (int) get_post_meta( $topic_id, '_bbp_voice_count', true );
  1982. $filter = ( true === $integer ) ? 'bbp_get_topic_voice_count_int' : 'bbp_get_topic_voice_count';
  1983. return apply_filters( $filter, $voices, $topic_id );
  1984. }
  1985. /**
  1986. * Output a the tags of a topic
  1987. *
  1988. * @param int $topic_id Optional. Topic id
  1989. * @param mixed $args See {@link bbp_get_topic_tag_list()}
  1990. * @uses bbp_get_topic_tag_list() To get the topic tag list
  1991. */
  1992. function bbp_topic_tag_list( $topic_id = 0, $args = '' ) {
  1993. echo bbp_get_topic_tag_list( $topic_id, $args );
  1994. }
  1995. /**
  1996. * Return the tags of a topic
  1997. *
  1998. * @param int $topic_id Optional. Topic id
  1999. * @param array $args This function supports these arguments:
  2000. * - before: Before the tag list
  2001. * - sep: Tag separator
  2002. * - after: After the tag list
  2003. * @uses bbp_get_topic_id() To get the topic id
  2004. * @uses get_the_term_list() To get the tags list
  2005. * @return string Tag list of the topic
  2006. */
  2007. function bbp_get_topic_tag_list( $topic_id = 0, $args = '' ) {
  2008. // Bail if topic-tags are off
  2009. if ( ! bbp_allow_topic_tags() )
  2010. return;
  2011. // Parse arguments against default values
  2012. $r = bbp_parse_args( $args, array(
  2013. 'before' => '<div class="bbp-topic-tags"><p>' . esc_html__( 'Tagged:', 'bbpress' ) . '&nbsp;',
  2014. 'sep' => ', ',
  2015. 'after' => '</p></div>'
  2016. ), 'get_topic_tag_list' );
  2017. $topic_id = bbp_get_topic_id( $topic_id );
  2018. // Topic is spammed, so display pre-spam terms
  2019. if ( bbp_is_topic_spam( $topic_id ) ) {
  2020. // Get pre-spam terms
  2021. $terms = get_post_meta( $topic_id, '_bbp_spam_topic_tags', true );
  2022. // If terms exist, explode them and compile the return value
  2023. if ( !empty( $terms ) ) {
  2024. $terms = implode( $r['sep'], $terms );
  2025. $retval = $r['before'] . $terms . $r['after'];
  2026. // No terms so return emty string
  2027. } else {
  2028. $retval = '';
  2029. }
  2030. // Topic is not spam so display a clickable term list
  2031. } else {
  2032. $retval = get_the_term_list( $topic_id, bbp_get_topic_tag_tax_id(), $r['before'], $r['sep'], $r['after'] );
  2033. }
  2034. return $retval;
  2035. }
  2036. /**
  2037. * Output the row class of a topic
  2038. *
  2039. * @since bbPress (r2667)
  2040. *
  2041. * @param int $topic_id Optional. Topic id
  2042. * @param array Extra classes you can pass when calling this function
  2043. * @uses bbp_get_topic_class() To get the topic class
  2044. */
  2045. function bbp_topic_class( $topic_id = 0, $classes = array() ) {
  2046. echo bbp_get_topic_class( $topic_id, $classes );
  2047. }
  2048. /**
  2049. * Return the row class of a topic
  2050. *
  2051. * @since bbPress (r2667)
  2052. *
  2053. * @param int $topic_id Optional. Topic id
  2054. * @param array Extra classes you can pass when calling this function
  2055. * @uses bbp_is_topic_sticky() To check if the topic is a sticky
  2056. * @uses bbp_is_topic_super_sticky() To check if the topic is a super sticky
  2057. * @uses bbp_get_topic_forum_id() To get the topic forum id
  2058. * @uses get_post_class() To get the topic classes
  2059. * @uses apply_filters() Calls 'bbp_get_topic_class' with the classes
  2060. * and topic id
  2061. * @return string Row class of a topic
  2062. */
  2063. function bbp_get_topic_class( $topic_id = 0, $classes = array() ) {
  2064. $bbp = bbpress();
  2065. $topic_id = bbp_get_topic_id( $topic_id );
  2066. $count = isset( $bbp->topic_query->current_post ) ? $bbp->topic_query->current_post : 1;
  2067. $classes = (array) $classes;
  2068. $classes[] = ( (int) $count % 2 ) ? 'even' : 'odd';
  2069. $classes[] = bbp_is_topic_sticky( $topic_id, false ) ? 'sticky' : '';
  2070. $classes[] = bbp_is_topic_super_sticky( $topic_id ) ? 'super-sticky' : '';
  2071. $classes[] = 'bbp-parent-forum-' . bbp_get_topic_forum_id( $topic_id );
  2072. $classes[] = 'user-id-' . bbp_get_topic_author_id( $topic_id );
  2073. $classes = array_filter( $classes );
  2074. $classes = get_post_class( $classes, $topic_id );
  2075. $classes = apply_filters( 'bbp_get_topic_class', $classes, $topic_id );
  2076. $retval = 'class="' . implode( ' ', $classes ) . '"';
  2077. return $retval;
  2078. }
  2079. /** Topic Admin Links *********************************************************/
  2080. /**
  2081. * Output admin links for topic
  2082. *
  2083. * @param array $args See {@link bbp_get_topic_admin_links()}
  2084. * @uses bbp_get_topic_admin_links() To get the topic admin links
  2085. */
  2086. function bbp_topic_admin_links( $args = array() ) {
  2087. echo bbp_get_topic_admin_links( $args );
  2088. }
  2089. /**
  2090. * Return admin links for topic.
  2091. *
  2092. * Move topic functionality is handled by the edit topic page.
  2093. *
  2094. * @param array $args This function supports these arguments:
  2095. * - id: Optional. Topic id
  2096. * - before: Before the links
  2097. * - after: After the links
  2098. * - sep: Links separator
  2099. * - links: Topic admin links array
  2100. * @uses current_user_can() To check if the current user can edit/delete
  2101. * the topic
  2102. * @uses bbp_get_topic_edit_link() To get the topic edit link
  2103. * @uses bbp_get_topic_trash_link() To get the topic trash link
  2104. * @uses bbp_get_topic_close_link() To get the topic close link
  2105. * @uses bbp_get_topic_spam_link() To get the topic spam link
  2106. * @uses bbp_get_topic_stick_link() To get the topic stick link
  2107. * @uses bbp_get_topic_merge_link() To get the topic merge link
  2108. * @uses bbp_get_topic_status() To get the topic status
  2109. * @uses apply_filters() Calls 'bbp_get_topic_admin_links' with the
  2110. * topic admin links and args
  2111. * @return string Topic admin links
  2112. */
  2113. function bbp_get_topic_admin_links( $args = array() ) {
  2114. // Parse arguments against default values
  2115. $r = bbp_parse_args( $args, array (
  2116. 'id' => bbp_get_topic_id(),
  2117. 'before' => '<span class="bbp-admin-links">',
  2118. 'after' => '</span>',
  2119. 'sep' => ' | ',
  2120. 'links' => array()
  2121. ), 'get_topic_admin_links' );
  2122. if ( empty( $r['links'] ) ) {
  2123. $r['links'] = apply_filters( 'bbp_topic_admin_links', array(
  2124. 'edit' => bbp_get_topic_edit_link ( $r ),
  2125. 'reply' => bbp_get_topic_reply_link( $r )
  2126. ), $r['id'] );
  2127. }
  2128. // See if links need to be unset
  2129. $topic_status = bbp_get_topic_status( $r['id'] );
  2130. if ( in_array( $topic_status, array( bbp_get_spam_status_id(), bbp_get_trash_status_id() ) ) ) {
  2131. // Close link shouldn't be visible on trashed/spammed topics
  2132. unset( $r['links']['close'] );
  2133. // Spam link shouldn't be visible on trashed topics
  2134. if ( bbp_get_trash_status_id() === $topic_status ) {
  2135. unset( $r['links']['spam'] );
  2136. // Trash link shouldn't be visible on spam topics
  2137. } elseif ( bbp_get_spam_status_id() === $topic_status ) {
  2138. unset( $r['links']['trash'] );
  2139. }
  2140. }
  2141. // Process the admin links
  2142. $links = implode( $r['sep'], array_filter( $r['links'] ) );
  2143. $retval = $r['before'] . $links . $r['after'];
  2144. return apply_filters( 'bbp_get_topic_admin_links', $retval, $r, $args );
  2145. }
  2146. /**
  2147. * Output the edit link of the topic
  2148. *
  2149. * @since bbPress (r2727)
  2150. *
  2151. * @param mixed $args See {@link bbp_get_topic_edit_link()}
  2152. * @uses bbp_get_topic_edit_link() To get the topic edit link
  2153. */
  2154. function bbp_topic_edit_link( $args = '' ) {
  2155. echo bbp_get_topic_edit_link( $args );
  2156. }
  2157. /**
  2158. * Return the edit link of the topic
  2159. *
  2160. * @since bbPress (r2727)
  2161. *
  2162. * @param mixed $args This function supports these args:
  2163. * - id: Optional. Topic id
  2164. * - link_before: Before the link
  2165. * - link_after: After the link
  2166. * - edit_text: Edit text
  2167. * @uses bbp_get_topic_id() To get the topic id
  2168. * @uses bbp_get_topic() To get the topic
  2169. * @uses current_user_can() To check if the current user can edit the
  2170. * topic
  2171. * @uses bbp_get_topic_edit_url() To get the topic edit url
  2172. * @uses apply_filters() Calls 'bbp_get_topic_edit_link' with the link
  2173. * and args
  2174. * @return string Topic edit link
  2175. */
  2176. function bbp_get_topic_edit_link( $args = '' ) {
  2177. // Parse arguments against default values
  2178. $r = bbp_parse_args( $args, array(
  2179. 'id' => 0,
  2180. 'link_before' => '',
  2181. 'link_after' => '',
  2182. 'edit_text' => esc_html__( 'Edit', 'bbpress' )
  2183. ), 'get_topic_edit_link' );
  2184. $topic = bbp_get_topic( bbp_get_topic_id( (int) $r['id'] ) );
  2185. // Bypass check if user has caps
  2186. if ( !current_user_can( 'edit_others_topics' ) ) {
  2187. // User cannot edit or it is past the lock time
  2188. if ( empty( $topic ) || !current_user_can( 'edit_topic', $topic->ID ) || bbp_past_edit_lock( $topic->post_date_gmt ) ) {
  2189. return;
  2190. }
  2191. }
  2192. // Get uri
  2193. $uri = bbp_get_topic_edit_url( $r['id'] );
  2194. // Bail if no uri
  2195. if ( empty( $uri ) )
  2196. return;
  2197. $retval = $r['link_before'] . '<a href="' . esc_url( $uri ) . '" class="bbp-topic-edit-link">' . $r['edit_text'] . '</a>' . $r['link_after'];
  2198. return apply_filters( 'bbp_get_topic_edit_link', $retval, $r );
  2199. }
  2200. /**
  2201. * Output URL to the topic edit page
  2202. *
  2203. * @since bbPress (r2753)
  2204. *
  2205. * @param int $topic_id Optional. Topic id
  2206. * @uses bbp_get_topic_edit_url() To get the topic edit url
  2207. */
  2208. function bbp_topic_edit_url( $topic_id = 0 ) {
  2209. echo esc_url( bbp_get_topic_edit_url( $topic_id ) );
  2210. }
  2211. /**
  2212. * Return URL to the topic edit page
  2213. *
  2214. * @since bbPress (r2753)
  2215. *
  2216. * @param int $topic_id Optional. Topic id
  2217. * @uses bbp_get_topic_id() To get the topic id
  2218. * @uses bbp_get_topic() To get the topic
  2219. * @uses add_query_arg() To add custom args to the url
  2220. * @uses apply_filters() Calls 'bbp_get_topic_edit_url' with the edit
  2221. * url and topic id
  2222. * @return string Topic edit url
  2223. */
  2224. function bbp_get_topic_edit_url( $topic_id = 0 ) {
  2225. global $wp_rewrite;
  2226. $bbp = bbpress();
  2227. $topic = bbp_get_topic( bbp_get_topic_id( $topic_id ) );
  2228. if ( empty( $topic ) )
  2229. return;
  2230. // Remove view=all link from edit
  2231. $topic_link = bbp_remove_view_all( bbp_get_topic_permalink( $topic_id ) );
  2232. // Pretty permalinks
  2233. if ( $wp_rewrite->using_permalinks() ) {
  2234. $url = trailingslashit( $topic_link ) . $bbp->edit_id;
  2235. $url = trailingslashit( $url );
  2236. // Unpretty permalinks
  2237. } else {
  2238. $url = add_query_arg( array( bbp_get_topic_post_type() => $topic->post_name, $bbp->edit_id => '1' ), $topic_link );
  2239. }
  2240. // Maybe add view=all
  2241. $url = bbp_add_view_all( $url );
  2242. return apply_filters( 'bbp_get_topic_edit_url', $url, $topic_id );
  2243. }
  2244. /**
  2245. * Output the trash link of the topic
  2246. *
  2247. * @since bbPress (r2727)
  2248. *
  2249. * @param mixed $args See {@link bbp_get_topic_trash_link()}
  2250. * @uses bbp_get_topic_trash_link() To get the topic trash link
  2251. */
  2252. function bbp_topic_trash_link( $args = '' ) {
  2253. echo bbp_get_topic_trash_link( $args );
  2254. }
  2255. /**
  2256. * Return the trash link of the topic
  2257. *
  2258. * @since bbPress (r2727)
  2259. *
  2260. * @param mixed $args This function supports these args:
  2261. * - id: Optional. Topic id
  2262. * - link_before: Before the link
  2263. * - link_after: After the link
  2264. * - sep: Links separator
  2265. * - trash_text: Trash text
  2266. * - restore_text: Restore text
  2267. * - delete_text: Delete text
  2268. * @uses bbp_get_topic_id() To get the topic id
  2269. * @uses bbp_get_topic() To get the topic
  2270. * @uses current_user_can() To check if the current user can delete the
  2271. * topic
  2272. * @uses bbp_is_topic_trash() To check if the topic is trashed
  2273. * @uses bbp_get_topic_status() To get the topic status
  2274. * @uses add_query_arg() To add custom args to the url
  2275. * @uses wp_nonce_url() To nonce the url
  2276. * @uses esc_url() To escape the url
  2277. * @uses apply_filters() Calls 'bbp_get_topic_trash_link' with the link
  2278. * and args
  2279. * @return string Topic trash link
  2280. */
  2281. function bbp_get_topic_trash_link( $args = '' ) {
  2282. // Parse arguments against default values
  2283. $r = bbp_parse_args( $args, array(
  2284. 'id' => 0,
  2285. 'link_before' => '',
  2286. 'link_after' => '',
  2287. 'sep' => ' | ',
  2288. 'trash_text' => esc_html__( 'Trash', 'bbpress' ),
  2289. 'restore_text' => esc_html__( 'Restore', 'bbpress' ),
  2290. 'delete_text' => esc_html__( 'Delete', 'bbpress' )
  2291. ), 'get_topic_trash_link' );
  2292. $actions = array();
  2293. $topic = bbp_get_topic( bbp_get_topic_id( (int) $r['id'] ) );
  2294. if ( empty( $topic ) || !current_user_can( 'delete_topic', $topic->ID ) ) {
  2295. return;
  2296. }
  2297. if ( bbp_is_topic_trash( $topic->ID ) ) {
  2298. $actions['untrash'] = '<a title="' . esc_attr__( 'Restore this item from the Trash', 'bbpress' ) . '" href="' . esc_url( wp_nonce_url( add_query_arg( array( 'action' => 'bbp_toggle_topic_trash', 'sub_action' => 'untrash', 'topic_id' => $topic->ID ) ), 'untrash-' . $topic->post_type . '_' . $topic->ID ) ) . '" class="bbp-topic-restore-link">' . $r['restore_text'] . '</a>';
  2299. } elseif ( EMPTY_TRASH_DAYS ) {
  2300. $actions['trash'] = '<a title="' . esc_attr__( 'Move this item to the Trash', 'bbpress' ) . '" href="' . esc_url( wp_nonce_url( add_query_arg( array( 'action' => 'bbp_toggle_topic_trash', 'sub_action' => 'trash', 'topic_id' => $topic->ID ) ), 'trash-' . $topic->post_type . '_' . $topic->ID ) ) . '" class="bbp-topic-trash-link">' . $r['trash_text'] . '</a>';
  2301. }
  2302. if ( bbp_is_topic_trash( $topic->ID ) || !EMPTY_TRASH_DAYS ) {
  2303. $actions['delete'] = '<a title="' . esc_attr__( 'Delete this item permanently', 'bbpress' ) . '" href="' . esc_url( wp_nonce_url( add_query_arg( array( 'action' => 'bbp_toggle_topic_trash', 'sub_action' => 'delete', 'topic_id' => $topic->ID ) ), 'delete-' . $topic->post_type . '_' . $topic->ID ) ) . '" onclick="return confirm(\'' . esc_js( __( 'Are you sure you want to delete that permanently?', 'bbpress' ) ) . '\' );" class="bbp-topic-delete-link">' . $r['delete_text'] . '</a>';
  2304. }
  2305. // Process the admin links
  2306. $retval = $r['link_before'] . implode( $r['sep'], $actions ) . $r['link_after'];
  2307. return apply_filters( 'bbp_get_topic_trash_link', $retval, $r );
  2308. }
  2309. /**
  2310. * Output the close link of the topic
  2311. *
  2312. * @since bbPress (r2727)
  2313. *
  2314. * @param mixed $args See {@link bbp_get_topic_close_link()}
  2315. * @uses bbp_get_topic_close_link() To get the topic close link
  2316. */
  2317. function bbp_topic_close_link( $args = '' ) {
  2318. echo bbp_get_topic_close_link( $args );
  2319. }
  2320. /**
  2321. * Return the close link of the topic
  2322. *
  2323. * @since bbPress (r2727)
  2324. *
  2325. * @param mixed $args This function supports these args:
  2326. * - id: Optional. Topic id
  2327. * - link_before: Before the link
  2328. * - link_after: After the link
  2329. * - close_text: Close text
  2330. * - open_text: Open text
  2331. * @uses bbp_get_topic_id() To get the topic id
  2332. * @uses bbp_get_topic() To get the topic
  2333. * @uses current_user_can() To check if the current user can edit the topic
  2334. * @uses bbp_is_topic_open() To check if the topic is open
  2335. * @uses add_query_arg() To add custom args to the url
  2336. * @uses wp_nonce_url() To nonce the url
  2337. * @uses esc_url() To escape the url
  2338. * @uses apply_filters() Calls 'bbp_get_topic_close_link' with the link
  2339. * and args
  2340. * @return string Topic close link
  2341. */
  2342. function bbp_get_topic_close_link( $args = '' ) {
  2343. // Parse arguments against default values
  2344. $r = bbp_parse_args( $args, array(
  2345. 'id' => 0,
  2346. 'link_before' => '',
  2347. 'link_after' => '',
  2348. 'sep' => ' | ',
  2349. 'close_text' => _x( 'Close', 'Topic Status', 'bbpress' ),
  2350. 'open_text' => _x( 'Open', 'Topic Status', 'bbpress' )
  2351. ), 'get_topic_close_link' );
  2352. $topic = bbp_get_topic( bbp_get_topic_id( (int) $r['id'] ) );
  2353. if ( empty( $topic ) || !current_user_can( 'moderate', $topic->ID ) )
  2354. return;
  2355. $display = bbp_is_topic_open( $topic->ID ) ? $r['close_text'] : $r['open_text'];
  2356. $uri = add_query_arg( array( 'action' => 'bbp_toggle_topic_close', 'topic_id' => $topic->ID ) );
  2357. $uri = wp_nonce_url( $uri, 'close-topic_' . $topic->ID );
  2358. $retval = $r['link_before'] . '<a href="' . esc_url( $uri ) . '" class="bbp-topic-close-link">' . $display . '</a>' . $r['link_after'];
  2359. return apply_filters( 'bbp_get_topic_close_link', $retval, $r );
  2360. }
  2361. /**
  2362. * Output the stick link of the topic
  2363. *
  2364. * @since bbPress (r2754)
  2365. *
  2366. * @param mixed $args See {@link bbp_get_topic_stick_link()}
  2367. * @uses bbp_get_topic_stick_link() To get the topic stick link
  2368. */
  2369. function bbp_topic_stick_link( $args = '' ) {
  2370. echo bbp_get_topic_stick_link( $args );
  2371. }
  2372. /**
  2373. * Return the stick link of the topic
  2374. *
  2375. * @since bbPress (r2754)
  2376. *
  2377. * @param mixed $args This function supports these args:
  2378. * - id: Optional. Topic id
  2379. * - link_before: Before the link
  2380. * - link_after: After the link
  2381. * - stick_text: Stick text
  2382. * - unstick_text: Unstick text
  2383. * - super_text: Stick to front text
  2384. * @uses bbp_get_topic_id() To get the topic id
  2385. * @uses bbp_get_topic() To get the topic
  2386. * @uses current_user_can() To check if the current user can edit the
  2387. * topic
  2388. * @uses bbp_is_topic_sticky() To check if the topic is a sticky
  2389. * @uses add_query_arg() To add custom args to the url
  2390. * @uses wp_nonce_url() To nonce the url
  2391. * @uses esc_url() To escape the url
  2392. * @uses apply_filters() Calls 'bbp_get_topic_stick_link' with the link
  2393. * and args
  2394. * @return string Topic stick link
  2395. */
  2396. function bbp_get_topic_stick_link( $args = '' ) {
  2397. // Parse arguments against default values
  2398. $r = bbp_parse_args( $args, array(
  2399. 'id' => 0,
  2400. 'link_before' => '',
  2401. 'link_after' => '',
  2402. 'stick_text' => esc_html__( 'Stick', 'bbpress' ),
  2403. 'unstick_text' => esc_html__( 'Unstick', 'bbpress' ),
  2404. 'super_text' => esc_html__( '(to front)', 'bbpress' ),
  2405. ), 'get_topic_stick_link' );
  2406. $topic = bbp_get_topic( bbp_get_topic_id( (int) $r['id'] ) );
  2407. if ( empty( $topic ) || !current_user_can( 'moderate', $topic->ID ) )
  2408. return;
  2409. $is_sticky = bbp_is_topic_sticky( $topic->ID );
  2410. $stick_uri = add_query_arg( array( 'action' => 'bbp_toggle_topic_stick', 'topic_id' => $topic->ID ) );
  2411. $stick_uri = wp_nonce_url( $stick_uri, 'stick-topic_' . $topic->ID );
  2412. $stick_display = ( true === $is_sticky ) ? $r['unstick_text'] : $r['stick_text'];
  2413. $stick_display = '<a href="' . esc_url( $stick_uri ) . '" class="bbp-topic-sticky-link">' . $stick_display . '</a>';
  2414. if ( empty( $is_sticky ) ) {
  2415. $super_uri = add_query_arg( array( 'action' => 'bbp_toggle_topic_stick', 'topic_id' => $topic->ID, 'super' => 1 ) );
  2416. $super_uri = wp_nonce_url( $super_uri, 'stick-topic_' . $topic->ID );
  2417. $super_display = ' <a href="' . esc_url( $super_uri ) . '" class="bbp-topic-super-sticky-link">' . $r['super_text'] . '</a>';
  2418. } else {
  2419. $super_display = '';
  2420. }
  2421. // Combine the HTML into 1 string
  2422. $retval = $r['link_before'] . $stick_display . $super_display . $r['link_after'];
  2423. return apply_filters( 'bbp_get_topic_stick_link', $retval, $r );
  2424. }
  2425. /**
  2426. * Output the merge link of the topic
  2427. *
  2428. * @since bbPress (r2756)
  2429. *
  2430. * @param mixed $args
  2431. * @uses bbp_get_topic_merge_link() To get the topic merge link
  2432. */
  2433. function bbp_topic_merge_link( $args = '' ) {
  2434. echo bbp_get_topic_merge_link( $args );
  2435. }
  2436. /**
  2437. * Return the merge link of the topic
  2438. *
  2439. * @since bbPress (r2756)
  2440. *
  2441. * @param mixed $args This function supports these args:
  2442. * - id: Optional. Topic id
  2443. * - link_before: Before the link
  2444. * - link_after: After the link
  2445. * - merge_text: Merge text
  2446. * @uses bbp_get_topic_id() To get the topic id
  2447. * @uses bbp_get_topic() To get the topic
  2448. * @uses bbp_get_topic_edit_url() To get the topic edit url
  2449. * @uses add_query_arg() To add custom args to the url
  2450. * @uses esc_url() To escape the url
  2451. * @uses apply_filters() Calls 'bbp_get_topic_merge_link' with the link
  2452. * and args
  2453. * @return string Topic merge link
  2454. */
  2455. function bbp_get_topic_merge_link( $args = '' ) {
  2456. // Parse arguments against default values
  2457. $r = bbp_parse_args( $args, array(
  2458. 'id' => 0,
  2459. 'link_before' => '',
  2460. 'link_after' => '',
  2461. 'merge_text' => esc_html__( 'Merge', 'bbpress' ),
  2462. ), 'get_topic_merge_link' );
  2463. $topic = bbp_get_topic( bbp_get_topic_id( (int) $r['id'] ) );
  2464. if ( empty( $topic ) || !current_user_can( 'moderate', $topic->ID ) )
  2465. return;
  2466. $uri = add_query_arg( array( 'action' => 'merge' ), bbp_get_topic_edit_url( $topic->ID ) );
  2467. $retval = $r['link_before'] . '<a href="' . esc_url( $uri ) . '" class="bbp-topic-merge-link">' . $r['merge_text'] . '</a>' . $r['link_after'];
  2468. return apply_filters( 'bbp_get_topic_merge_link', $retval, $args );
  2469. }
  2470. /**
  2471. * Output the spam link of the topic
  2472. *
  2473. * @since bbPress (r2727)
  2474. *
  2475. * @param mixed $args See {@link bbp_get_topic_spam_link()}
  2476. * @uses bbp_get_topic_spam_link() Topic spam link
  2477. */
  2478. function bbp_topic_spam_link( $args = '' ) {
  2479. echo bbp_get_topic_spam_link( $args );
  2480. }
  2481. /**
  2482. * Return the spam link of the topic
  2483. *
  2484. * @since bbPress (r2727)
  2485. *
  2486. * @param mixed $args This function supports these args:
  2487. * - id: Optional. Topic id
  2488. * - link_before: Before the link
  2489. * - link_after: After the link
  2490. * - spam_text: Spam text
  2491. * - unspam_text: Unspam text
  2492. * @uses bbp_get_topic_id() To get the topic id
  2493. * @uses bbp_get_topic() To get the topic
  2494. * @uses current_user_can() To check if the current user can edit the topic
  2495. * @uses bbp_is_topic_spam() To check if the topic is marked as spam
  2496. * @uses add_query_arg() To add custom args to the url
  2497. * @uses wp_nonce_url() To nonce the url
  2498. * @uses esc_url() To escape the url
  2499. * @uses apply_filters() Calls 'bbp_get_topic_spam_link' with the link
  2500. * and args
  2501. * @return string Topic spam link
  2502. */
  2503. function bbp_get_topic_spam_link( $args = '' ) {
  2504. // Parse arguments against default values
  2505. $r = bbp_parse_args( $args, array(
  2506. 'id' => 0,
  2507. 'link_before' => '',
  2508. 'link_after' => '',
  2509. 'sep' => ' | ',
  2510. 'spam_text' => esc_html__( 'Spam', 'bbpress' ),
  2511. 'unspam_text' => esc_html__( 'Unspam', 'bbpress' )
  2512. ), 'get_topic_spam_link' );
  2513. $topic = bbp_get_topic( bbp_get_topic_id( (int) $r['id'] ) );
  2514. if ( empty( $topic ) || !current_user_can( 'moderate', $topic->ID ) )
  2515. return;
  2516. $display = bbp_is_topic_spam( $topic->ID ) ? $r['unspam_text'] : $r['spam_text'];
  2517. $uri = add_query_arg( array( 'action' => 'bbp_toggle_topic_spam', 'topic_id' => $topic->ID ) );
  2518. $uri = wp_nonce_url( $uri, 'spam-topic_' . $topic->ID );
  2519. $retval = $r['link_before'] . '<a href="' . esc_url( $uri ) . '" class="bbp-topic-spam-link">' . $display . '</a>' . $r['link_after'];
  2520. return apply_filters( 'bbp_get_topic_spam_link', $retval, $r );
  2521. }
  2522. /**
  2523. * Output the link to go directly to the reply form
  2524. *
  2525. * @since bbPress (r4966)
  2526. *
  2527. * @param array $args
  2528. * @uses bbp_get_reply_to_link() To get the reply to link
  2529. */
  2530. function bbp_topic_reply_link( $args = array() ) {
  2531. echo bbp_get_topic_reply_link( $args );
  2532. }
  2533. /**
  2534. * Return the link to go directly to the reply form
  2535. *
  2536. * @since bbPress (r4966)
  2537. *
  2538. * @param array $args Arguments
  2539. * @uses bbp_current_user_can_access_create_reply_form() To check permissions
  2540. * @uses bbp_get_topic_id() To validate the topic id
  2541. * @uses bbp_get_topic() To get the topic
  2542. * @uses apply_filters() Calls 'bbp_get_topic_reply_link' with the formatted link,
  2543. * the arguments array, and the topic
  2544. * @return string Link for a reply to a topic
  2545. */
  2546. function bbp_get_topic_reply_link( $args = array() ) {
  2547. // Parse arguments against default values
  2548. $r = bbp_parse_args( $args, array(
  2549. 'id' => 0,
  2550. 'link_before' => '',
  2551. 'link_after' => '',
  2552. 'reply_text' => esc_html__( 'Reply', 'bbpress' ),
  2553. ), 'get_topic_reply_link' );
  2554. // Get the reply to use it's ID and post_parent
  2555. $topic = bbp_get_topic( bbp_get_topic_id( (int) $r['id'] ) );
  2556. // Bail if no reply or user cannot reply
  2557. if ( empty( $topic ) || ! bbp_current_user_can_access_create_reply_form() )
  2558. return;
  2559. $uri = '#new-post';
  2560. // Add $uri to the array, to be passed through the filter
  2561. $r['uri'] = $uri;
  2562. $retval = $r['link_before'] . '<a href="' . esc_url( $r['uri'] ) . '" class="bbp-topic-reply-link">' . $r['reply_text'] . '</a>' . $r['link_after'];
  2563. return apply_filters( 'bbp_get_topic_reply_link', $retval, $r, $args );
  2564. }
  2565. /** Topic Pagination **********************************************************/
  2566. /**
  2567. * Output the pagination count
  2568. *
  2569. * @since bbPress (r2519)
  2570. *
  2571. * @uses bbp_get_forum_pagination_count() To get the forum pagination count
  2572. */
  2573. function bbp_forum_pagination_count() {
  2574. echo bbp_get_forum_pagination_count();
  2575. }
  2576. /**
  2577. * Return the pagination count
  2578. *
  2579. * @since bbPress (r2519)
  2580. *
  2581. * @uses bbp_number_format() To format the number value
  2582. * @uses apply_filters() Calls 'bbp_get_forum_pagination_count' with the
  2583. * pagination count
  2584. * @return string Forum Pagintion count
  2585. */
  2586. function bbp_get_forum_pagination_count() {
  2587. $bbp = bbpress();
  2588. if ( empty( $bbp->topic_query ) )
  2589. return false;
  2590. // Set pagination values
  2591. $start_num = intval( ( $bbp->topic_query->paged - 1 ) * $bbp->topic_query->posts_per_page ) + 1;
  2592. $from_num = bbp_number_format( $start_num );
  2593. $to_num = bbp_number_format( ( $start_num + ( $bbp->topic_query->posts_per_page - 1 ) > $bbp->topic_query->found_posts ) ? $bbp->topic_query->found_posts : $start_num + ( $bbp->topic_query->posts_per_page - 1 ) );
  2594. $total_int = (int) !empty( $bbp->topic_query->found_posts ) ? $bbp->topic_query->found_posts : $bbp->topic_query->post_count;
  2595. $total = bbp_number_format( $total_int );
  2596. // Several topics in a forum with a single page
  2597. if ( empty( $to_num ) ) {
  2598. $retstr = sprintf( _n( 'Viewing %1$s topic', 'Viewing %1$s topics', $total_int, 'bbpress' ), $total );
  2599. // Several topics in a forum with several pages
  2600. } else {
  2601. $retstr = sprintf( _n( 'Viewing topic %2$s (of %4$s total)', 'Viewing %1$s topics - %2$s through %3$s (of %4$s total)', $total_int, 'bbpress' ), $bbp->topic_query->post_count, $from_num, $to_num, $total );
  2602. }
  2603. // Filter and return
  2604. return apply_filters( 'bbp_get_forum_pagination_count', esc_html( $retstr ) );
  2605. }
  2606. /**
  2607. * Output pagination links
  2608. *
  2609. * @since bbPress (r2519)
  2610. *
  2611. * @uses bbp_get_forum_pagination_links() To get the pagination links
  2612. */
  2613. function bbp_forum_pagination_links() {
  2614. echo bbp_get_forum_pagination_links();
  2615. }
  2616. /**
  2617. * Return pagination links
  2618. *
  2619. * @since bbPress (r2519)
  2620. *
  2621. * @uses bbPress::topic_query::pagination_links To get the links
  2622. * @return string Pagination links
  2623. */
  2624. function bbp_get_forum_pagination_links() {
  2625. $bbp = bbpress();
  2626. if ( empty( $bbp->topic_query ) )
  2627. return false;
  2628. return apply_filters( 'bbp_get_forum_pagination_links', $bbp->topic_query->pagination_links );
  2629. }
  2630. /**
  2631. * Displays topic notices
  2632. *
  2633. * @since bbPress (r2744)
  2634. *
  2635. * @uses bbp_is_single_topic() To check if it's a topic page
  2636. * @uses bbp_get_topic_status() To get the topic status
  2637. * @uses bbp_get_topic_id() To get the topic id
  2638. * @uses apply_filters() Calls 'bbp_topic_notices' with the notice text, topic
  2639. * status and topic id
  2640. * @uses bbp_add_error() To add an error message
  2641. */
  2642. function bbp_topic_notices() {
  2643. // Bail if not viewing a topic
  2644. if ( !bbp_is_single_topic() )
  2645. return;
  2646. // Get the topic_status
  2647. $topic_status = bbp_get_topic_status();
  2648. // Get the topic status
  2649. switch ( $topic_status ) {
  2650. // Spam notice
  2651. case bbp_get_spam_status_id() :
  2652. $notice_text = __( 'This topic is marked as spam.', 'bbpress' );
  2653. break;
  2654. // Trashed notice
  2655. case bbp_get_trash_status_id() :
  2656. $notice_text = __( 'This topic is in the trash.', 'bbpress' );
  2657. break;
  2658. // Standard status
  2659. default :
  2660. $notice_text = '';
  2661. break;
  2662. }
  2663. // Filter notice text and bail if empty
  2664. $notice_text = apply_filters( 'bbp_topic_notices', $notice_text, $topic_status, bbp_get_topic_id() );
  2665. if ( empty( $notice_text ) )
  2666. return;
  2667. bbp_add_error( 'topic_notice', $notice_text, 'message' );
  2668. }
  2669. /**
  2670. * Displays topic type select box (normal/sticky/super sticky)
  2671. *
  2672. * @since bbPress (r5059)
  2673. * @deprecated since bbPress (r5059)
  2674. *
  2675. * @param $args This function supports these arguments:
  2676. * - select_id: Select id. Defaults to bbp_stick_topic
  2677. * - tab: Tabindex
  2678. * - topic_id: Topic id
  2679. * - selected: Override the selected option
  2680. */
  2681. function bbp_topic_type_select( $args = '' ) {
  2682. echo bbp_get_form_topic_type_dropdown( $args );
  2683. }
  2684. /**
  2685. * Displays topic type select box (normal/sticky/super sticky)
  2686. *
  2687. * @since bbPress (r5059)
  2688. *
  2689. * @param $args This function supports these arguments:
  2690. * - select_id: Select id. Defaults to bbp_stick_topic
  2691. * - tab: Tabindex
  2692. * - topic_id: Topic id
  2693. * - selected: Override the selected option
  2694. */
  2695. function bbp_form_topic_type_dropdown( $args = '' ) {
  2696. echo bbp_get_form_topic_type_dropdown( $args );
  2697. }
  2698. /**
  2699. * Returns topic type select box (normal/sticky/super sticky)
  2700. *
  2701. * @since bbPress (r5059)
  2702. *
  2703. * @param $args This function supports these arguments:
  2704. * - select_id: Select id. Defaults to bbp_stick_topic
  2705. * - tab: Tabindex
  2706. * - topic_id: Topic id
  2707. * - selected: Override the selected option
  2708. * @uses bbp_get_topic_id() To get the topic id
  2709. * @uses bbp_is_single_topic() To check if we're viewing a single topic
  2710. * @uses bbp_is_topic_edit() To check if it is the topic edit page
  2711. * @uses bbp_is_topic_super_sticky() To check if the topic is a super sticky
  2712. * @uses bbp_is_topic_sticky() To check if the topic is a sticky
  2713. */
  2714. function bbp_get_form_topic_type_dropdown( $args = '' ) {
  2715. // Parse arguments against default values
  2716. $r = bbp_parse_args( $args, array(
  2717. 'select_id' => 'bbp_stick_topic',
  2718. 'tab' => bbp_get_tab_index(),
  2719. 'topic_id' => 0,
  2720. 'selected' => false
  2721. ), 'topic_type_select' );
  2722. // No specific selected value passed
  2723. if ( empty( $r['selected'] ) ) {
  2724. // Post value is passed
  2725. if ( bbp_is_post_request() && isset( $_POST[ $r['select_id'] ] ) ) {
  2726. $r['selected'] = $_POST[ $r['select_id'] ];
  2727. // No Post value passed
  2728. } else {
  2729. // Edit topic
  2730. if ( bbp_is_single_topic() || bbp_is_topic_edit() ) {
  2731. // Get current topic id
  2732. $topic_id = bbp_get_topic_id( $r['topic_id'] );
  2733. // Topic is super sticky
  2734. if ( bbp_is_topic_super_sticky( $topic_id ) ) {
  2735. $r['selected'] = 'super';
  2736. // Topic is sticky or normal
  2737. } else {
  2738. $r['selected'] = bbp_is_topic_sticky( $topic_id, false ) ? 'stick' : 'unstick';
  2739. }
  2740. }
  2741. }
  2742. }
  2743. // Used variables
  2744. $tab = !empty( $r['tab'] ) ? ' tabindex="' . (int) $r['tab'] . '"' : '';
  2745. // Start an output buffer, we'll finish it after the select loop
  2746. ob_start(); ?>
  2747. <select name="<?php echo esc_attr( $r['select_id'] ); ?>" id="<?php echo esc_attr( $r['select_id'] ); ?>_select"<?php echo $tab; ?>>
  2748. <?php foreach ( bbp_get_topic_types() as $key => $label ) : ?>
  2749. <option value="<?php echo esc_attr( $key ); ?>"<?php selected( $key, $r['selected'] ); ?>><?php echo esc_html( $label ); ?></option>
  2750. <?php endforeach; ?>
  2751. </select>
  2752. <?php
  2753. // Return the results
  2754. return apply_filters( 'bbp_get_form_topic_type_dropdown', ob_get_clean(), $r );
  2755. }
  2756. /**
  2757. * Output value topic status dropdown
  2758. *
  2759. * @since bbPress (r5059)
  2760. *
  2761. * @param int $topic_id The topic id to use
  2762. */
  2763. function bbp_form_topic_status_dropdown( $args = '' ) {
  2764. echo bbp_get_form_topic_status_dropdown( $args );
  2765. }
  2766. /**
  2767. * Returns topic status downdown
  2768. *
  2769. * This dropdown is only intended to be seen by users with the 'moderate'
  2770. * capability. Because of this, no additional capablitiy checks are performed
  2771. * within this function to check available topic statuses.
  2772. *
  2773. * @since bbPress (r5059)
  2774. *
  2775. * @param $args This function supports these arguments:
  2776. * - select_id: Select id. Defaults to bbp_open_close_topic
  2777. * - tab: Tabindex
  2778. * - topic_id: Topic id
  2779. * - selected: Override the selected option
  2780. */
  2781. function bbp_get_form_topic_status_dropdown( $args = '' ) {
  2782. // Parse arguments against default values
  2783. $r = bbp_parse_args( $args, array(
  2784. 'select_id' => 'bbp_topic_status',
  2785. 'tab' => bbp_get_tab_index(),
  2786. 'topic_id' => 0,
  2787. 'selected' => false
  2788. ), 'topic_open_close_select' );
  2789. // No specific selected value passed
  2790. if ( empty( $r['selected'] ) ) {
  2791. // Post value is passed
  2792. if ( bbp_is_post_request() && isset( $_POST[ $r['select_id'] ] ) ) {
  2793. $r['selected'] = $_POST[ $r['select_id'] ];
  2794. // No Post value was passed
  2795. } else {
  2796. // Edit topic
  2797. if ( bbp_is_topic_edit() ) {
  2798. $r['topic_id'] = bbp_get_topic_id( $r['topic_id'] );
  2799. $r['selected'] = bbp_get_topic_status( $r['topic_id'] );
  2800. // New topic
  2801. } else {
  2802. $r['selected'] = bbp_get_public_status_id();
  2803. }
  2804. }
  2805. }
  2806. // Used variables
  2807. $tab = ! empty( $r['tab'] ) ? ' tabindex="' . (int) $r['tab'] . '"' : '';
  2808. // Start an output buffer, we'll finish it after the select loop
  2809. ob_start(); ?>
  2810. <select name="<?php echo esc_attr( $r['select_id'] ) ?>" id="<?php echo esc_attr( $r['select_id'] ); ?>_select"<?php echo $tab; ?>>
  2811. <?php foreach ( bbp_get_topic_statuses( $r['topic_id'] ) as $key => $label ) : ?>
  2812. <option value="<?php echo esc_attr( $key ); ?>"<?php selected( $key, $r['selected'] ); ?>><?php echo esc_html( $label ); ?></option>
  2813. <?php endforeach; ?>
  2814. </select>
  2815. <?php
  2816. // Return the results
  2817. return apply_filters( 'bbp_get_form_topic_status_dropdown', ob_get_clean(), $r );
  2818. }
  2819. /** Single Topic **************************************************************/
  2820. /**
  2821. * Output a fancy description of the current topic, including total topics,
  2822. * total replies, and last activity.
  2823. *
  2824. * @since bbPress (r2860)
  2825. *
  2826. * @param array $args See {@link bbp_get_single_topic_description()}
  2827. * @uses bbp_get_single_topic_description() Return the eventual output
  2828. */
  2829. function bbp_single_topic_description( $args = '' ) {
  2830. echo bbp_get_single_topic_description( $args );
  2831. }
  2832. /**
  2833. * Return a fancy description of the current topic, including total topics,
  2834. * total replies, and last activity.
  2835. *
  2836. * @since bbPress (r2860)
  2837. *
  2838. * @param mixed $args This function supports these arguments:
  2839. * - topic_id: Topic id
  2840. * - before: Before the text
  2841. * - after: After the text
  2842. * - size: Size of the avatar
  2843. * @uses bbp_get_topic_id() To get the topic id
  2844. * @uses bbp_get_topic_voice_count() To get the topic voice count
  2845. * @uses bbp_get_topic_reply_count() To get the topic reply count
  2846. * @uses bbp_get_topic_freshness_link() To get the topic freshness link
  2847. * @uses bbp_get_topic_last_active_id() To get the topic last active id
  2848. * @uses bbp_get_reply_author_link() To get the reply author link
  2849. * @uses apply_filters() Calls 'bbp_get_single_topic_description' with
  2850. * the description and args
  2851. * @return string Filtered topic description
  2852. */
  2853. function bbp_get_single_topic_description( $args = '' ) {
  2854. // Parse arguments against default values
  2855. $r = bbp_parse_args( $args, array(
  2856. 'topic_id' => 0,
  2857. 'before' => '<div class="bbp-template-notice info"><p class="bbp-topic-description">',
  2858. 'after' => '</p></div>',
  2859. 'size' => 14
  2860. ), 'get_single_topic_description' );
  2861. // Validate topic_id
  2862. $topic_id = bbp_get_topic_id( $r['topic_id'] );
  2863. // Unhook the 'view all' query var adder
  2864. remove_filter( 'bbp_get_topic_permalink', 'bbp_add_view_all' );
  2865. // Build the topic description
  2866. $vc_int = bbp_get_topic_voice_count ( $topic_id, true );
  2867. $voice_count = bbp_get_topic_voice_count ( $topic_id, false );
  2868. $reply_count = bbp_get_topic_replies_link ( $topic_id );
  2869. $time_since = bbp_get_topic_freshness_link( $topic_id );
  2870. // Singular/Plural
  2871. $voice_count = sprintf( _n( '%s voice', '%s voices', $vc_int, 'bbpress' ), $voice_count );
  2872. // Topic has replies
  2873. $last_reply = bbp_get_topic_last_reply_id( $topic_id );
  2874. if ( !empty( $last_reply ) ) {
  2875. $last_updated_by = bbp_get_author_link( array( 'post_id' => $last_reply, 'size' => $r['size'] ) );
  2876. $retstr = sprintf( esc_html__( 'This topic contains %1$s, has %2$s, and was last updated by %3$s %4$s.', 'bbpress' ), $reply_count, $voice_count, $last_updated_by, $time_since );
  2877. // Topic has no replies
  2878. } elseif ( ! empty( $voice_count ) && ! empty( $reply_count ) ) {
  2879. $retstr = sprintf( esc_html__( 'This topic contains %1$s and has %2$s.', 'bbpress' ), $voice_count, $reply_count );
  2880. // Topic has no replies and no voices
  2881. } elseif ( empty( $voice_count ) && empty( $reply_count ) ) {
  2882. $retstr = sprintf( esc_html__( 'This topic has no replies.', 'bbpress' ), $voice_count, $reply_count );
  2883. }
  2884. // Add the 'view all' filter back
  2885. add_filter( 'bbp_get_topic_permalink', 'bbp_add_view_all' );
  2886. // Combine the elements together
  2887. $retstr = $r['before'] . $retstr . $r['after'];
  2888. // Return filtered result
  2889. return apply_filters( 'bbp_get_single_topic_description', $retstr, $r );
  2890. }
  2891. /** Topic Tags ****************************************************************/
  2892. /**
  2893. * Output the unique id of the topic tag taxonomy
  2894. *
  2895. * @since bbPress (r3348)
  2896. *
  2897. * @uses bbp_get_topic_post_type() To get the topic post type
  2898. */
  2899. function bbp_topic_tag_tax_id() {
  2900. echo bbp_get_topic_tag_tax_id();
  2901. }
  2902. /**
  2903. * Return the unique id of the topic tag taxonomy
  2904. *
  2905. * @since bbPress (r3348)
  2906. *
  2907. * @uses apply_filters() Calls 'bbp_get_topic_tag_tax_id' with the topic tax id
  2908. * @return string The unique topic tag taxonomy
  2909. */
  2910. function bbp_get_topic_tag_tax_id() {
  2911. return apply_filters( 'bbp_get_topic_tag_tax_id', bbpress()->topic_tag_tax_id );
  2912. }
  2913. /**
  2914. * Return array of labels used by the topic-tag taxonomy
  2915. *
  2916. * @since bbPress (r5129)
  2917. *
  2918. * @return array
  2919. */
  2920. function bbp_get_topic_tag_tax_labels() {
  2921. return apply_filters( 'bbp_get_topic_tag_tax_labels', array(
  2922. 'name' => __( 'Topic Tags', 'bbpress' ),
  2923. 'singular_name' => __( 'Topic Tag', 'bbpress' ),
  2924. 'search_items' => __( 'Search Tags', 'bbpress' ),
  2925. 'popular_items' => __( 'Popular Tags', 'bbpress' ),
  2926. 'all_items' => __( 'All Tags', 'bbpress' ),
  2927. 'edit_item' => __( 'Edit Tag', 'bbpress' ),
  2928. 'update_item' => __( 'Update Tag', 'bbpress' ),
  2929. 'add_new_item' => __( 'Add New Tag', 'bbpress' ),
  2930. 'new_item_name' => __( 'New Tag Name', 'bbpress' ),
  2931. 'view_item' => __( 'View Topic Tag', 'bbpress' )
  2932. ) );
  2933. }
  2934. /**
  2935. * Return an array of topic-tag taxonomy rewrite settings
  2936. *
  2937. * @since bbPress (r5129)
  2938. *
  2939. * @return array
  2940. */
  2941. function bbp_get_topic_tag_tax_rewrite() {
  2942. return apply_filters( 'bbp_get_topic_tag_tax_rewrite', array(
  2943. 'slug' => bbp_get_topic_tag_tax_slug(),
  2944. 'with_front' => false
  2945. ) );
  2946. }
  2947. /**
  2948. * Output the id of the current tag
  2949. *
  2950. * @since bbPress (r3109)
  2951. *
  2952. * @uses bbp_get_topic_tag_id()
  2953. */
  2954. function bbp_topic_tag_id( $tag = '' ) {
  2955. echo bbp_get_topic_tag_id( $tag );
  2956. }
  2957. /**
  2958. * Return the id of the current tag
  2959. *
  2960. * @since bbPress (r3109)
  2961. *
  2962. * @uses get_term_by()
  2963. * @uses get_queried_object()
  2964. * @uses get_query_var()
  2965. * @uses apply_filters()
  2966. *
  2967. * @return string Term Name
  2968. */
  2969. function bbp_get_topic_tag_id( $tag = '' ) {
  2970. // Get the term
  2971. if ( ! empty( $tag ) ) {
  2972. $term = get_term_by( 'slug', $tag, bbp_get_topic_tag_tax_id() );
  2973. } else {
  2974. $tag = get_query_var( 'term' );
  2975. $term = get_queried_object();
  2976. }
  2977. // Add before and after if description exists
  2978. if ( !empty( $term->term_id ) ) {
  2979. $retval = $term->term_id;
  2980. // No id
  2981. } else {
  2982. $retval = '';
  2983. }
  2984. return (int) apply_filters( 'bbp_get_topic_tag_id', (int) $retval, $tag );
  2985. }
  2986. /**
  2987. * Output the name of the current tag
  2988. *
  2989. * @since bbPress (r3109)
  2990. *
  2991. * @uses bbp_get_topic_tag_name()
  2992. */
  2993. function bbp_topic_tag_name( $tag = '' ) {
  2994. echo bbp_get_topic_tag_name( $tag );
  2995. }
  2996. /**
  2997. * Return the name of the current tag
  2998. *
  2999. * @since bbPress (r3109)
  3000. *
  3001. * @uses get_term_by()
  3002. * @uses get_queried_object()
  3003. * @uses get_query_var()
  3004. * @uses apply_filters()
  3005. *
  3006. * @return string Term Name
  3007. */
  3008. function bbp_get_topic_tag_name( $tag = '' ) {
  3009. // Get the term
  3010. if ( ! empty( $tag ) ) {
  3011. $term = get_term_by( 'slug', $tag, bbp_get_topic_tag_tax_id() );
  3012. } else {
  3013. $tag = get_query_var( 'term' );
  3014. $term = get_queried_object();
  3015. }
  3016. // Add before and after if description exists
  3017. if ( !empty( $term->name ) ) {
  3018. $retval = $term->name;
  3019. // No name
  3020. } else {
  3021. $retval = '';
  3022. }
  3023. return apply_filters( 'bbp_get_topic_tag_name', $retval );
  3024. }
  3025. /**
  3026. * Output the slug of the current tag
  3027. *
  3028. * @since bbPress (r3109)
  3029. *
  3030. * @uses bbp_get_topic_tag_slug()
  3031. */
  3032. function bbp_topic_tag_slug( $tag = '' ) {
  3033. echo bbp_get_topic_tag_slug( $tag );
  3034. }
  3035. /**
  3036. * Return the slug of the current tag
  3037. *
  3038. * @since bbPress (r3109)
  3039. *
  3040. * @uses get_term_by()
  3041. * @uses get_queried_object()
  3042. * @uses get_query_var()
  3043. * @uses apply_filters()
  3044. *
  3045. * @return string Term Name
  3046. */
  3047. function bbp_get_topic_tag_slug( $tag = '' ) {
  3048. // Get the term
  3049. if ( ! empty( $tag ) ) {
  3050. $term = get_term_by( 'slug', $tag, bbp_get_topic_tag_tax_id() );
  3051. } else {
  3052. $tag = get_query_var( 'term' );
  3053. $term = get_queried_object();
  3054. }
  3055. // Add before and after if description exists
  3056. if ( !empty( $term->slug ) ) {
  3057. $retval = $term->slug;
  3058. // No slug
  3059. } else {
  3060. $retval = '';
  3061. }
  3062. return apply_filters( 'bbp_get_topic_tag_slug', $retval );
  3063. }
  3064. /**
  3065. * Output the link of the current tag
  3066. *
  3067. * @since bbPress (r3348)
  3068. *
  3069. * @uses bbp_get_topic_tag_link()
  3070. */
  3071. function bbp_topic_tag_link( $tag = '' ) {
  3072. echo esc_url( bbp_get_topic_tag_link( $tag ) );
  3073. }
  3074. /**
  3075. * Return the link of the current tag
  3076. *
  3077. * @since bbPress (r3348)
  3078. *
  3079. * @uses get_term_by()
  3080. * @uses get_queried_object()
  3081. * @uses get_query_var()
  3082. * @uses apply_filters()
  3083. *
  3084. * @return string Term Name
  3085. */
  3086. function bbp_get_topic_tag_link( $tag = '' ) {
  3087. // Get the term
  3088. if ( ! empty( $tag ) ) {
  3089. $term = get_term_by( 'slug', $tag, bbp_get_topic_tag_tax_id() );
  3090. } else {
  3091. $tag = get_query_var( 'term' );
  3092. $term = get_queried_object();
  3093. }
  3094. // Add before and after if description exists
  3095. if ( !empty( $term->term_id ) ) {
  3096. $retval = get_term_link( $term, bbp_get_topic_tag_tax_id() );
  3097. // No link
  3098. } else {
  3099. $retval = '';
  3100. }
  3101. return apply_filters( 'bbp_get_topic_tag_link', $retval, $tag );
  3102. }
  3103. /**
  3104. * Output the link of the current tag
  3105. *
  3106. * @since bbPress (r3348)
  3107. *
  3108. * @uses bbp_get_topic_tag_edit_link()
  3109. */
  3110. function bbp_topic_tag_edit_link( $tag = '' ) {
  3111. echo esc_url( bbp_get_topic_tag_edit_link( $tag ) );
  3112. }
  3113. /**
  3114. * Return the link of the current tag
  3115. *
  3116. * @since bbPress (r3348)
  3117. *
  3118. * @uses get_term_by()
  3119. * @uses get_queried_object()
  3120. * @uses get_query_var()
  3121. * @uses apply_filters()
  3122. *
  3123. * @return string Term Name
  3124. */
  3125. function bbp_get_topic_tag_edit_link( $tag = '' ) {
  3126. global $wp_rewrite;
  3127. // Get the term
  3128. if ( ! empty( $tag ) ) {
  3129. $term = get_term_by( 'slug', $tag, bbp_get_topic_tag_tax_id() );
  3130. } else {
  3131. $tag = get_query_var( 'term' );
  3132. $term = get_queried_object();
  3133. }
  3134. // Add before and after if description exists
  3135. if ( !empty( $term->term_id ) ) {
  3136. $bbp = bbpress();
  3137. // Pretty
  3138. if ( $wp_rewrite->using_permalinks() ) {
  3139. $retval = user_trailingslashit( trailingslashit( bbp_get_topic_tag_link() ) . $bbp->edit_id );
  3140. // Ugly
  3141. } else {
  3142. $retval = add_query_arg( array( $bbp->edit_id => '1' ), bbp_get_topic_tag_link() );
  3143. }
  3144. // No link
  3145. } else {
  3146. $retval = '';
  3147. }
  3148. return apply_filters( 'bbp_get_topic_tag_edit_link', $retval, $tag );
  3149. }
  3150. /**
  3151. * Output the description of the current tag
  3152. *
  3153. * @since bbPress (r3109)
  3154. *
  3155. * @uses bbp_get_topic_tag_description()
  3156. */
  3157. function bbp_topic_tag_description( $args = array() ) {
  3158. echo bbp_get_topic_tag_description( $args );
  3159. }
  3160. /**
  3161. * Return the description of the current tag
  3162. *
  3163. * @since bbPress (r3109)
  3164. *
  3165. * @uses get_term_by()
  3166. * @uses get_queried_object()
  3167. * @uses get_query_var()
  3168. * @uses apply_filters()
  3169. * @param array $args before|after|tag
  3170. *
  3171. * @return string Term Name
  3172. */
  3173. function bbp_get_topic_tag_description( $args = array() ) {
  3174. // Parse arguments against default values
  3175. $r = bbp_parse_args( $args, array(
  3176. 'before' => '<div class="bbp-topic-tag-description"><p>',
  3177. 'after' => '</p></div>',
  3178. 'tag' => ''
  3179. ), 'get_topic_tag_description' );
  3180. // Get the term
  3181. if ( ! empty( $r['tag'] ) ) {
  3182. $term = get_term_by( 'slug', $r['tag'], bbp_get_topic_tag_tax_id() );
  3183. } else {
  3184. $tag = get_query_var( 'term' );
  3185. $r['tag'] = $tag;
  3186. $term = get_queried_object();
  3187. }
  3188. // Add before and after if description exists
  3189. if ( !empty( $term->description ) ) {
  3190. $retval = $r['before'] . $term->description . $r['after'];
  3191. // No description, no HTML
  3192. } else {
  3193. $retval = '';
  3194. }
  3195. return apply_filters( 'bbp_get_topic_tag_description', $retval, $r );
  3196. }
  3197. /** Forms *********************************************************************/
  3198. /**
  3199. * Output the value of topic title field
  3200. *
  3201. * @since bbPress (r2976)
  3202. *
  3203. * @uses bbp_get_form_topic_title() To get the value of topic title field
  3204. */
  3205. function bbp_form_topic_title() {
  3206. echo bbp_get_form_topic_title();
  3207. }
  3208. /**
  3209. * Return the value of topic title field
  3210. *
  3211. * @since bbPress (r2976)
  3212. *
  3213. * @uses bbp_is_topic_edit() To check if it's topic edit page
  3214. * @uses apply_filters() Calls 'bbp_get_form_topic_title' with the title
  3215. * @return string Value of topic title field
  3216. */
  3217. function bbp_get_form_topic_title() {
  3218. // Get _POST data
  3219. if ( bbp_is_post_request() && isset( $_POST['bbp_topic_title'] ) ) {
  3220. $topic_title = $_POST['bbp_topic_title'];
  3221. // Get edit data
  3222. } elseif ( bbp_is_topic_edit() ) {
  3223. $topic_title = bbp_get_global_post_field( 'post_title', 'raw' );
  3224. // No data
  3225. } else {
  3226. $topic_title = '';
  3227. }
  3228. return apply_filters( 'bbp_get_form_topic_title', esc_attr( $topic_title ) );
  3229. }
  3230. /**
  3231. * Output the value of topic content field
  3232. *
  3233. * @since bbPress (r2976)
  3234. *
  3235. * @uses bbp_get_form_topic_content() To get value of topic content field
  3236. */
  3237. function bbp_form_topic_content() {
  3238. echo bbp_get_form_topic_content();
  3239. }
  3240. /**
  3241. * Return the value of topic content field
  3242. *
  3243. * @since bbPress (r2976)
  3244. *
  3245. * @uses bbp_is_topic_edit() To check if it's the topic edit page
  3246. * @uses apply_filters() Calls 'bbp_get_form_topic_content' with the content
  3247. * @return string Value of topic content field
  3248. */
  3249. function bbp_get_form_topic_content() {
  3250. // Get _POST data
  3251. if ( bbp_is_post_request() && isset( $_POST['bbp_topic_content'] ) ) {
  3252. $topic_content = stripslashes( $_POST['bbp_topic_content'] );
  3253. // Get edit data
  3254. } elseif ( bbp_is_topic_edit() ) {
  3255. $topic_content = bbp_get_global_post_field( 'post_content', 'raw' );
  3256. // No data
  3257. } else {
  3258. $topic_content = '';
  3259. }
  3260. return apply_filters( 'bbp_get_form_topic_content', $topic_content );
  3261. }
  3262. /**
  3263. * Allow topic rows to have adminstrative actions
  3264. *
  3265. * @since bbPress (r3653)
  3266. * @uses do_action()
  3267. * @todo Links and filter
  3268. */
  3269. function bbp_topic_row_actions() {
  3270. do_action( 'bbp_topic_row_actions' );
  3271. }
  3272. /**
  3273. * Output value of topic tags field
  3274. *
  3275. * @since bbPress (r2976)
  3276. * @uses bbp_get_form_topic_tags() To get the value of topic tags field
  3277. */
  3278. function bbp_form_topic_tags() {
  3279. echo bbp_get_form_topic_tags();
  3280. }
  3281. /**
  3282. * Return value of topic tags field
  3283. *
  3284. * @since bbPress (r2976)
  3285. *
  3286. * @uses bbp_is_topic_edit() To check if it's the topic edit page
  3287. * @uses apply_filters() Calls 'bbp_get_form_topic_tags' with the tags
  3288. * @return string Value of topic tags field
  3289. */
  3290. function bbp_get_form_topic_tags() {
  3291. // Get _POST data
  3292. if ( bbp_is_post_request() && isset( $_POST['bbp_topic_tags'] ) ) {
  3293. $topic_tags = $_POST['bbp_topic_tags'];
  3294. // Get edit data
  3295. } elseif ( bbp_is_single_topic() || bbp_is_single_reply() || bbp_is_topic_edit() || bbp_is_reply_edit() ) {
  3296. // Determine the topic id based on the post type
  3297. switch ( get_post_type() ) {
  3298. // Post is a topic
  3299. case bbp_get_topic_post_type() :
  3300. $topic_id = get_the_ID();
  3301. break;
  3302. // Post is a reply
  3303. case bbp_get_reply_post_type() :
  3304. $topic_id = bbp_get_reply_topic_id( get_the_ID() );
  3305. break;
  3306. }
  3307. // Topic exists
  3308. if ( !empty( $topic_id ) ) {
  3309. // Topic is spammed so display pre-spam terms
  3310. if ( bbp_is_topic_spam( $topic_id ) ) {
  3311. // Get pre-spam terms
  3312. $new_terms = get_post_meta( $topic_id, '_bbp_spam_topic_tags', true );
  3313. // If terms exist, explode them and compile the return value
  3314. if ( empty( $new_terms ) ) {
  3315. $new_terms = '';
  3316. }
  3317. // Topic is not spam so get real terms
  3318. } else {
  3319. $terms = array_filter( (array) get_the_terms( $topic_id, bbp_get_topic_tag_tax_id() ) );
  3320. // Loop through them
  3321. foreach ( $terms as $term ) {
  3322. $new_terms[] = $term->name;
  3323. }
  3324. }
  3325. // Define local variable(s)
  3326. } else {
  3327. $new_terms = '';
  3328. }
  3329. // Set the return value
  3330. $topic_tags = ( !empty( $new_terms ) ) ? implode( ', ', $new_terms ) : '';
  3331. // No data
  3332. } else {
  3333. $topic_tags = '';
  3334. }
  3335. return apply_filters( 'bbp_get_form_topic_tags', esc_attr( $topic_tags ) );
  3336. }
  3337. /**
  3338. * Output value of topic forum
  3339. *
  3340. * @since bbPress (r2976)
  3341. *
  3342. * @uses bbp_get_form_topic_forum() To get the topic's forum id
  3343. */
  3344. function bbp_form_topic_forum() {
  3345. echo bbp_get_form_topic_forum();
  3346. }
  3347. /**
  3348. * Return value of topic forum
  3349. *
  3350. * @since bbPress (r2976)
  3351. *
  3352. * @uses bbp_is_topic_edit() To check if it's the topic edit page
  3353. * @uses bbp_get_topic_forum_id() To get the topic forum id
  3354. * @uses apply_filters() Calls 'bbp_get_form_topic_forum' with the forum
  3355. * @return string Value of topic content field
  3356. */
  3357. function bbp_get_form_topic_forum() {
  3358. // Get _POST data
  3359. if ( bbp_is_post_request() && isset( $_POST['bbp_forum_id'] ) ) {
  3360. $topic_forum = (int) $_POST['bbp_forum_id'];
  3361. // Get edit data
  3362. } elseif ( bbp_is_topic_edit() ) {
  3363. $topic_forum = bbp_get_topic_forum_id();
  3364. // No data
  3365. } else {
  3366. $topic_forum = 0;
  3367. }
  3368. return apply_filters( 'bbp_get_form_topic_forum', $topic_forum );
  3369. }
  3370. /**
  3371. * Output checked value of topic subscription
  3372. *
  3373. * @since bbPress (r2976)
  3374. *
  3375. * @uses bbp_get_form_topic_subscribed() To get the subscribed checkbox value
  3376. */
  3377. function bbp_form_topic_subscribed() {
  3378. echo bbp_get_form_topic_subscribed();
  3379. }
  3380. /**
  3381. * Return checked value of topic subscription
  3382. *
  3383. * @since bbPress (r2976)
  3384. *
  3385. * @uses bbp_is_topic_edit() To check if it's the topic edit page
  3386. * @uses bbp_is_user_subscribed_to_topic() To check if the user is
  3387. * subscribed to the topic
  3388. * @uses apply_filters() Calls 'bbp_get_form_topic_subscribed' with the
  3389. * option
  3390. * @return string Checked value of topic subscription
  3391. */
  3392. function bbp_get_form_topic_subscribed() {
  3393. // Get _POST data
  3394. if ( bbp_is_post_request() && isset( $_POST['bbp_topic_subscription'] ) ) {
  3395. $topic_subscribed = (bool) $_POST['bbp_topic_subscription'];
  3396. // Get edit data
  3397. } elseif ( bbp_is_topic_edit() || bbp_is_reply_edit() ) {
  3398. // Get current posts author
  3399. $post_author = bbp_get_global_post_field( 'post_author', 'raw' );
  3400. // Post author is not the current user
  3401. if ( bbp_get_current_user_id() !== $post_author ) {
  3402. $topic_subscribed = bbp_is_user_subscribed_to_topic( $post_author );
  3403. // Post author is the current user
  3404. } else {
  3405. $topic_subscribed = bbp_is_user_subscribed_to_topic( bbp_get_current_user_id() );
  3406. }
  3407. // Get current status
  3408. } elseif ( bbp_is_single_topic() ) {
  3409. $topic_subscribed = bbp_is_user_subscribed_to_topic( bbp_get_current_user_id() );
  3410. // No data
  3411. } else {
  3412. $topic_subscribed = false;
  3413. }
  3414. // Get checked output
  3415. $checked = checked( $topic_subscribed, true, false );
  3416. return apply_filters( 'bbp_get_form_topic_subscribed', $checked, $topic_subscribed );
  3417. }
  3418. /**
  3419. * Output checked value of topic log edit field
  3420. *
  3421. * @since bbPress (r2976)
  3422. *
  3423. * @uses bbp_get_form_topic_log_edit() To get the topic log edit value
  3424. */
  3425. function bbp_form_topic_log_edit() {
  3426. echo bbp_get_form_topic_log_edit();
  3427. }
  3428. /**
  3429. * Return checked value of topic log edit field
  3430. *
  3431. * @since bbPress (r2976)
  3432. *
  3433. * @uses apply_filters() Calls 'bbp_get_form_topic_log_edit' with the
  3434. * log edit value
  3435. * @return string Topic log edit checked value
  3436. */
  3437. function bbp_get_form_topic_log_edit() {
  3438. // Get _POST data
  3439. if ( bbp_is_post_request() && isset( $_POST['bbp_log_topic_edit'] ) ) {
  3440. $topic_revision = (int) $_POST['bbp_log_topic_edit'];
  3441. // No data
  3442. } else {
  3443. $topic_revision = 1;
  3444. }
  3445. // Get checked output
  3446. $checked = checked( $topic_revision, true, false );
  3447. return apply_filters( 'bbp_get_form_topic_log_edit', $checked, $topic_revision );
  3448. }
  3449. /**
  3450. * Output the value of the topic edit reason
  3451. *
  3452. * @since bbPress (r2976)
  3453. *
  3454. * @uses bbp_get_form_topic_edit_reason() To get the topic edit reason value
  3455. */
  3456. function bbp_form_topic_edit_reason() {
  3457. echo bbp_get_form_topic_edit_reason();
  3458. }
  3459. /**
  3460. * Return the value of the topic edit reason
  3461. *
  3462. * @since bbPress (r2976)
  3463. *
  3464. * @uses apply_filters() Calls 'bbp_get_form_topic_edit_reason' with the
  3465. * topic edit reason value
  3466. * @return string Topic edit reason value
  3467. */
  3468. function bbp_get_form_topic_edit_reason() {
  3469. // Get _POST data
  3470. if ( bbp_is_post_request() && isset( $_POST['bbp_topic_edit_reason'] ) ) {
  3471. $topic_edit_reason = $_POST['bbp_topic_edit_reason'];
  3472. // No data
  3473. } else {
  3474. $topic_edit_reason = '';
  3475. }
  3476. return apply_filters( 'bbp_get_form_topic_edit_reason', esc_attr( $topic_edit_reason ) );
  3477. }