PageRenderTime 49ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 1ms

/svntrunk/bp-forums/bp-forums-template.php

https://bitbucket.org/simplemediacode/bptrunk
PHP | 1467 lines | 989 code | 292 blank | 186 comment | 152 complexity | 6824cf1ff2d3791c58ed6449d98da50f MD5 | raw file
  1. <?php
  2. // Exit if accessed directly
  3. if ( !defined( 'ABSPATH' ) ) exit;
  4. /**
  5. * Output the forums component slug
  6. *
  7. * @package BuddyPress
  8. * @subpackage Forums Template
  9. * @since BuddyPress (1.5)
  10. *
  11. * @uses bp_get_forums_slug()
  12. */
  13. function bp_forums_slug() {
  14. echo bp_get_forums_slug();
  15. }
  16. /**
  17. * Return the forums component slug
  18. *
  19. * @package BuddyPress
  20. * @subpackage Forums Template
  21. * @since BuddyPress (1.5)
  22. */
  23. function bp_get_forums_slug() {
  24. global $bp;
  25. return apply_filters( 'bp_get_forums_slug', $bp->forums->slug );
  26. }
  27. /**
  28. * Output the forums component root slug
  29. *
  30. * @package BuddyPress
  31. * @subpackage Forums Template
  32. * @since BuddyPress (1.5)
  33. *
  34. * @uses bp_get_forums_root_slug()
  35. */
  36. function bp_forums_root_slug() {
  37. echo bp_get_forums_root_slug();
  38. }
  39. /**
  40. * Return the forums component root slug
  41. *
  42. * @package BuddyPress
  43. * @subpackage Forums Template
  44. * @since BuddyPress (1.5)
  45. */
  46. function bp_get_forums_root_slug() {
  47. global $bp;
  48. return apply_filters( 'bp_get_forums_root_slug', $bp->forums->root_slug );
  49. }
  50. /**
  51. * Output forum directory permalink
  52. *
  53. * @package BuddyPress
  54. * @subpackage Forums Template
  55. * @since BuddyPress (1.5)
  56. * @uses bp_get_forums_directory_permalink()
  57. */
  58. function bp_forums_directory_permalink() {
  59. echo bp_get_forums_directory_permalink();
  60. }
  61. /**
  62. * Return forum directory permalink
  63. *
  64. * @package BuddyPress
  65. * @subpackage Forums Template
  66. * @since BuddyPress (1.5)
  67. * @uses apply_filters()
  68. * @uses traisingslashit()
  69. * @uses bp_get_root_domain()
  70. * @uses bp_get_forums_root_slug()
  71. * @return string
  72. */
  73. function bp_get_forums_directory_permalink() {
  74. return apply_filters( 'bp_get_forums_directory_permalink', trailingslashit( bp_get_root_domain() . '/' . bp_get_forums_root_slug() ) );
  75. }
  76. class BP_Forums_Template_Forum {
  77. var $current_topic = -1;
  78. var $topic_count;
  79. var $topics;
  80. var $topic;
  81. var $forum_id;
  82. var $in_the_loop;
  83. var $pag_page;
  84. var $pag_num;
  85. var $pag_links;
  86. var $total_topic_count;
  87. var $single_topic = false;
  88. var $sort_by;
  89. var $order;
  90. function __construct( $type, $forum_id, $user_id, $page, $per_page, $max, $no_stickies, $search_terms, $offset = false, $number = false ) {
  91. global $bp;
  92. $this->pag_page = $page;
  93. $this->pag_num = $per_page;
  94. $this->type = $type;
  95. $this->search_terms = $search_terms;
  96. $this->forum_id = $forum_id;
  97. $this->offset = $offset;
  98. $this->number = $number;
  99. switch ( $type ) {
  100. case 'newest': default:
  101. $this->topics = bp_forums_get_forum_topics( array( 'user_id' => $user_id, 'forum_id' => $forum_id, 'filter' => $search_terms, 'page' => $this->pag_page, 'per_page' => $this->pag_num, 'show_stickies' => $no_stickies, 'offset' => $offset, 'number' => $number ) );
  102. break;
  103. case 'popular':
  104. $this->topics = bp_forums_get_forum_topics( array( 'user_id' => $user_id, 'type' => 'popular', 'filter' => $search_terms, 'forum_id' => $forum_id, 'page' => $this->pag_page, 'per_page' => $this->pag_num, 'show_stickies' => $no_stickies, 'offset' => $offset, 'number' => $number ) );
  105. break;
  106. case 'unreplied':
  107. $this->topics = bp_forums_get_forum_topics( array( 'user_id' => $user_id, 'type' => 'unreplied', 'filter' => $search_terms, 'forum_id' => $forum_id, 'page' => $this->pag_page, 'per_page' => $this->pag_num, 'show_stickies' => $no_stickies, 'offset' => $offset, 'number' => $number ) );
  108. break;
  109. case 'tags':
  110. $this->topics = bp_forums_get_forum_topics( array( 'user_id' => $user_id, 'type' => 'tags', 'filter' => $search_terms, 'forum_id' => $forum_id, 'page' => $this->pag_page, 'per_page' => $this->pag_num, 'show_stickies' => $no_stickies, 'offset' => $offset, 'number' => $number ) );
  111. break;
  112. }
  113. $this->topics = apply_filters( 'bp_forums_template_topics', $this->topics, $type, $forum_id, $per_page, $max, $no_stickies );
  114. if ( !(int) $this->topics ) {
  115. $this->topic_count = 0;
  116. $this->total_topic_count = 0;
  117. } else {
  118. // Get a total topic count, for use in pagination. This value will differ
  119. // depending on scope
  120. if ( !empty( $forum_id ) ) {
  121. // Group forums
  122. $topic_count = bp_forums_get_forum( $forum_id );
  123. $topic_count = (int) $topic_count->topics;
  124. } else if ( !empty( $bp->groups->current_group ) ) {
  125. $topic_count = (int)groups_total_public_forum_topic_count( $type );
  126. } else if ( bp_is_user_forums_started() || ( bp_is_directory() && $user_id ) ) {
  127. // This covers the case of Profile > Forums > Topics Started, as
  128. // well as Forum Directory > My Topics
  129. $topic_count = bp_forums_total_topic_count_for_user( bp_displayed_user_id(), $type );
  130. } else if ( bp_is_user_forums_replied_to() ) {
  131. // Profile > Forums > Replied To
  132. $topic_count = bp_forums_total_replied_count_for_user( bp_displayed_user_id(), $type );
  133. } else if ( 'tags' == $type ) {
  134. $tag = bb_get_tag( $search_terms );
  135. $topic_count = $tag->count;
  136. } else {
  137. // For forum directories (All Topics), get a true count
  138. $status = bp_current_user_can( 'bp_moderate' ) ? 'all' : 'public'; // todo: member-of
  139. $topic_count = (int)groups_total_forum_topic_count( $status, $search_terms );
  140. }
  141. if ( !$max || $max >= $topic_count ) {
  142. $this->total_topic_count = $topic_count;
  143. } else {
  144. $this->total_topic_count = (int) $max;
  145. }
  146. if ( $max ) {
  147. if ( $max >= count($this->topics) ) {
  148. $this->topic_count = count( $this->topics );
  149. } else {
  150. $this->topic_count = (int) $max;
  151. }
  152. } else {
  153. $this->topic_count = count( $this->topics );
  154. }
  155. }
  156. $this->topic_count = apply_filters_ref_array( 'bp_forums_template_topic_count', array( $this->topic_count, &$this->topics, $type, $forum_id, $per_page, $max, $no_stickies ) );
  157. $this->total_topic_count = apply_filters_ref_array( 'bp_forums_template_total_topic_count', array( $this->total_topic_count, $this->topic_count, &$this->topics, $type, $forum_id, $per_page, $max, $no_stickies ) );
  158. // Fetch extra information for topics, so we don't have to query inside the loop
  159. $this->topics = bp_forums_get_topic_extras( $this->topics );
  160. if ( (int) $this->total_topic_count && (int) $this->pag_num ) {
  161. $this->pag_links = paginate_links( array(
  162. 'base' => add_query_arg( array( 'p' => '%#%', 'n' => $this->pag_num ) ),
  163. 'format' => '',
  164. 'total' => ceil( (int) $this->total_topic_count / (int) $this->pag_num),
  165. 'current' => $this->pag_page,
  166. 'prev_text' => _x( '&larr;', 'Forum topic pagination previous text', 'buddypress' ),
  167. 'next_text' => _x( '&rarr;', 'Forum topic pagination next text', 'buddypress' ),
  168. 'mid_size' => 1
  169. ) );
  170. }
  171. }
  172. function has_topics() {
  173. if ( $this->topic_count )
  174. return true;
  175. return false;
  176. }
  177. function next_topic() {
  178. $this->current_topic++;
  179. $this->topic = $this->topics[$this->current_topic];
  180. return $this->topic;
  181. }
  182. function rewind_topics() {
  183. $this->current_topic = -1;
  184. if ( $this->topic_count > 0 ) {
  185. $this->topic = $this->topics[0];
  186. }
  187. }
  188. function user_topics() {
  189. if ( $this->current_topic + 1 < $this->topic_count ) {
  190. return true;
  191. } elseif ( $this->current_topic + 1 == $this->topic_count ) {
  192. do_action('forum_loop_end');
  193. // Do some cleaning up after the loop
  194. $this->rewind_topics();
  195. }
  196. $this->in_the_loop = false;
  197. return false;
  198. }
  199. function the_topic() {
  200. global $topic;
  201. $this->in_the_loop = true;
  202. $this->topic = $this->next_topic();
  203. $this->topic = (object)$this->topic;
  204. if ( $this->current_topic == 0 ) // loop has just started
  205. do_action('forum_loop_start');
  206. }
  207. }
  208. /**
  209. * Initiate the forum topics loop.
  210. *
  211. * Like other BuddyPress custom loops, the default arguments for this function are determined
  212. * dynamically, depending on your current page. All of these $defaults can be overridden in the
  213. * $args parameter.
  214. *
  215. * @package BuddyPress
  216. * @uses apply_filters() Filter bp_has_topics to manipulate the $forums_template global before
  217. * it's rendered, or to modify the value of has_topics().
  218. *
  219. * @param array $args See inline definition of $defaults for explanation of arguments
  220. * @return bool Returns true when forum topics are found corresponding to the args, false otherwise.
  221. */
  222. function bp_has_forum_topics( $args = '' ) {
  223. global $forum_template, $bp;
  224. /***
  225. * Set the defaults based on the current page. Any of these will be overridden
  226. * if arguments are directly passed into the loop. Custom plugins should always
  227. * pass their parameters directly to the loop.
  228. */
  229. $type = 'newest';
  230. $user_id = 0;
  231. $forum_id = false;
  232. $search_terms = false;
  233. $do_stickies = false;
  234. // User filtering
  235. if ( bp_displayed_user_id() )
  236. $user_id = bp_displayed_user_id();
  237. // "Replied" query must be manually modified
  238. if ( 'replies' == bp_current_action() ) {
  239. $user_id = 0; // User id must be handled manually by the filter, not by BB_Query
  240. add_filter( 'get_topics_distinct', 'bp_forums_add_replied_distinct_sql', 20 );
  241. add_filter( 'get_topics_join', 'bp_forums_add_replied_join_sql', 20 );
  242. add_filter( 'get_topics_where', 'bp_forums_add_replied_where_sql', 20 );
  243. }
  244. // If we're in a single group, set this group's forum_id
  245. if ( !$forum_id && !empty( $bp->groups->current_group ) ) {
  246. $bp->groups->current_group->forum_id = groups_get_groupmeta( $bp->groups->current_group->id, 'forum_id' );
  247. // If it turns out there is no forum for this group, return false so
  248. // we don't fetch all global topics
  249. if ( empty( $bp->groups->current_group->forum_id ) )
  250. return false;
  251. $forum_id = $bp->groups->current_group->forum_id;
  252. }
  253. // If $_GET['fs'] is set, let's auto populate the search_terms var
  254. if ( bp_is_directory() && !empty( $_GET['fs'] ) )
  255. $search_terms = $_GET['fs'];
  256. // Get the pagination arguments from $_REQUEST
  257. $page = isset( $_REQUEST['p'] ) ? intval( $_REQUEST['p'] ) : 1;
  258. $per_page = isset( $_REQUEST['n'] ) ? intval( $_REQUEST['n'] ) : 20;
  259. // By default, stickies are only pushed to the top of the order on individual group forums
  260. if ( bp_is_group_forum() )
  261. $do_stickies = true;
  262. $defaults = array(
  263. 'type' => $type,
  264. 'forum_id' => $forum_id,
  265. 'user_id' => $user_id,
  266. 'page' => $page,
  267. 'per_page' => $per_page,
  268. 'max' => false,
  269. 'number' => false,
  270. 'offset' => false,
  271. 'search_terms' => $search_terms,
  272. 'do_stickies' => $do_stickies
  273. );
  274. $r = wp_parse_args( $args, $defaults );
  275. extract( $r );
  276. // If we're viewing a tag URL in the directory, let's override the type and
  277. // set it to tags and the filter to the tag name
  278. if ( bp_is_current_action( 'tag' ) && $search_terms = bp_action_variable( 0 ) ) {
  279. $type = 'tags';
  280. }
  281. /** Sticky logic ******************************************************************/
  282. if ( $do_stickies ) {
  283. // Fetch the stickies
  284. $stickies_template = new BP_Forums_Template_Forum( $type, $forum_id, $user_id, 0, 0, $max, 'sticky', $search_terms );
  285. // If stickies are found, try merging them
  286. if ( $stickies_template->has_topics() ) {
  287. // If stickies are for current $page
  288. $page_start_num = ( ( $page - 1 ) * $per_page ) + 1;
  289. $page_end_num = $page * $per_page <= $stickies_template->total_topic_count ? $page * $per_page : $stickies_template->total_topic_count;
  290. // Calculate the number of sticky topics that will be shown on this page
  291. if ( $stickies_template->topic_count < $page_start_num ) {
  292. $this_page_stickies = 0;
  293. } else {
  294. $this_page_stickies = $stickies_template->topic_count - $per_page * floor( $stickies_template->topic_count / $per_page ) * ( $page - 1 ); // Total stickies minus sticky count through this page
  295. // $this_page_stickies cannot be more than $per_page or less than 0
  296. if ( $this_page_stickies > $per_page )
  297. $this_page_stickies = $per_page;
  298. else if ( $this_page_stickies < 0 )
  299. $this_page_stickies = 0;
  300. }
  301. // Calculate the total number of topics that will be shown on this page
  302. $this_page_topics = $stickies_template->total_topic_count >= ( $page * $per_page ) ? $per_page : $page_end_num - ( $page_start_num - 1 );
  303. // If the number of stickies to be shown is less than $per_page, fetch some
  304. // non-stickies to fill in the rest
  305. if ( $this_page_stickies < $this_page_topics ) {
  306. // How many non-stickies do we need?
  307. $non_sticky_number = $this_page_topics - $this_page_stickies;
  308. // Calculate the non-sticky offset
  309. // How many non-stickies on all pages up to this point?
  310. $non_sticky_total = $page_end_num - $stickies_template->topic_count;
  311. // The offset is the number of total non-stickies, less the number
  312. // to be shown on this page
  313. $non_sticky_offset = $non_sticky_total - $non_sticky_number;
  314. // Fetch the non-stickies
  315. $forum_template = new BP_Forums_Template_Forum( $type, $forum_id, $user_id, 1, $per_page, $max, 'no', $search_terms, $non_sticky_offset, $non_sticky_number );
  316. // If there are stickies to merge on this page, do it now
  317. if ( $this_page_stickies ) {
  318. // Correct the topic_count
  319. $forum_template->topic_count += (int) $this_page_stickies;
  320. // Figure out which stickies need to be included
  321. $this_page_sticky_topics = array_slice( $stickies_template->topics, 0 - $this_page_stickies );
  322. // Merge these topics into the forum template
  323. $forum_template->topics = array_merge( $this_page_sticky_topics, (array) $forum_template->topics );
  324. }
  325. } else {
  326. // This page has no non-stickies
  327. $forum_template = $stickies_template;
  328. // Adjust the topic count and trim the topics
  329. $forum_template->topic_count = $this_page_stickies;
  330. $forum_template->topics = array_slice( $forum_template->topics, $page - 1 );
  331. }
  332. // Because we're using a manual offset and number for the topic query, we
  333. // must set the page number manually, and recalculate the pagination links
  334. $forum_template->pag_num = $per_page;
  335. $forum_template->pag_page = $page;
  336. $forum_template->pag_links = paginate_links( array(
  337. 'base' => add_query_arg( array( 'p' => '%#%', 'n' => $forum_template->pag_num ) ),
  338. 'format' => '',
  339. 'total' => ceil( (int) $forum_template->total_topic_count / (int) $forum_template->pag_num ),
  340. 'current' => $forum_template->pag_page,
  341. 'prev_text' => _x( '&larr;', 'Forum topic pagination previous text', 'buddypress' ),
  342. 'next_text' => _x( '&rarr;', 'Forum topic pagination next text', 'buddypress' ),
  343. 'mid_size' => 1
  344. ) );
  345. } else {
  346. // Fetch the non-sticky topics if no stickies were found
  347. $forum_template = new BP_Forums_Template_Forum( $type, $forum_id, $user_id, $page, $per_page, $max, 'all', $search_terms );
  348. }
  349. } else {
  350. // When skipping the sticky logic, just pull up the forum topics like usual
  351. $forum_template = new BP_Forums_Template_Forum( $type, $forum_id, $user_id, $page, $per_page, $max, 'all', $search_terms );
  352. }
  353. return apply_filters( 'bp_has_topics', $forum_template->has_topics(), $forum_template );
  354. }
  355. function bp_forum_topics() {
  356. global $forum_template;
  357. return $forum_template->user_topics();
  358. }
  359. function bp_the_forum_topic() {
  360. global $forum_template;
  361. return $forum_template->the_topic();
  362. }
  363. function bp_the_topic_id() {
  364. echo bp_get_the_topic_id();
  365. }
  366. function bp_get_the_topic_id() {
  367. global $forum_template;
  368. return apply_filters( 'bp_get_the_topic_id', $forum_template->topic->topic_id );
  369. }
  370. function bp_the_topic_title() {
  371. echo bp_get_the_topic_title();
  372. }
  373. function bp_get_the_topic_title() {
  374. global $forum_template;
  375. return apply_filters( 'bp_get_the_topic_title', stripslashes( $forum_template->topic->topic_title ) );
  376. }
  377. function bp_the_topic_slug() {
  378. echo bp_get_the_topic_slug();
  379. }
  380. function bp_get_the_topic_slug() {
  381. global $forum_template;
  382. return apply_filters( 'bp_get_the_topic_slug', $forum_template->topic->topic_slug );
  383. }
  384. function bp_the_topic_text() {
  385. echo bp_get_the_topic_text();
  386. }
  387. function bp_get_the_topic_text() {
  388. global $forum_template;
  389. $post = bb_get_first_post( (int) $forum_template->topic->topic_id, false );
  390. return apply_filters( 'bp_get_the_topic_text', esc_attr( $post->post_text ) );
  391. }
  392. function bp_the_topic_poster_id() {
  393. echo bp_get_the_topic_poster_id();
  394. }
  395. function bp_get_the_topic_poster_id() {
  396. global $forum_template;
  397. return apply_filters( 'bp_get_the_topic_poster_id', $forum_template->topic->topic_poster );
  398. }
  399. function bp_the_topic_poster_avatar( $args = '' ) {
  400. echo bp_get_the_topic_poster_avatar( $args );
  401. }
  402. function bp_get_the_topic_poster_avatar( $args = '' ) {
  403. global $forum_template;
  404. $defaults = array(
  405. 'type' => 'thumb',
  406. 'width' => false,
  407. 'height' => false,
  408. 'alt' => sprintf( __( 'Profile picture of %s', 'buddypress' ), bp_core_get_user_displayname( $forum_template->topic->topic_poster ) )
  409. );
  410. $r = wp_parse_args( $args, $defaults );
  411. extract( $r, EXTR_SKIP );
  412. return apply_filters( 'bp_get_the_topic_poster_avatar', bp_core_fetch_avatar( array( 'item_id' => $forum_template->topic->topic_poster, 'type' => $type, 'width' => $width, 'height' => $height, 'alt' => $alt ) ) );
  413. }
  414. function bp_the_topic_poster_name() {
  415. echo bp_get_the_topic_poster_name();
  416. }
  417. function bp_get_the_topic_poster_name() {
  418. global $forum_template;
  419. $poster_id = ( empty( $forum_template->topic->poster_id ) ) ? $forum_template->topic->topic_poster : $forum_template->topic->poster_id;
  420. if ( !$name = bp_core_get_userlink( $poster_id ) )
  421. return __( 'Deleted User', 'buddypress' );
  422. return apply_filters( 'bp_get_the_topic_poster_name', $name );
  423. }
  424. function bp_the_topic_object_id() {
  425. echo bp_get_the_topic_object_id();
  426. }
  427. function bp_get_the_topic_object_id() {
  428. global $forum_template;
  429. return apply_filters( 'bp_get_the_topic_object_id', $forum_template->topic->object_id );
  430. }
  431. function bp_the_topic_object_name() {
  432. echo bp_get_the_topic_object_name();
  433. }
  434. function bp_get_the_topic_object_name() {
  435. global $forum_template;
  436. if ( isset( $forum_template->topic->object_name ) )
  437. $retval = $forum_template->topic->object_name;
  438. else
  439. $retval = '';
  440. return apply_filters( 'bp_get_the_topic_object_name', $retval );
  441. }
  442. function bp_the_topic_object_slug() {
  443. echo bp_get_the_topic_object_slug();
  444. }
  445. function bp_get_the_topic_object_slug() {
  446. global $forum_template;
  447. return apply_filters( 'bp_get_the_topic_object_slug', $forum_template->topic->object_slug );
  448. }
  449. function bp_the_topic_object_permalink() {
  450. echo bp_get_the_topic_object_permalink();
  451. }
  452. function bp_get_the_topic_object_permalink() {
  453. // Currently this will only work with group forums, extended support in the future
  454. if ( bp_is_active( 'groups' ) )
  455. $permalink = trailingslashit( bp_get_root_domain() . '/' . bp_get_groups_root_slug() . '/' . bp_get_the_topic_object_slug() . '/forum' );
  456. else
  457. $permalink = '';
  458. return apply_filters( 'bp_get_the_topic_object_permalink', $permalink );
  459. }
  460. function bp_the_topic_last_poster_name() {
  461. echo bp_get_the_topic_last_poster_name();
  462. }
  463. function bp_get_the_topic_last_poster_name() {
  464. global $forum_template;
  465. $domain = bp_core_get_user_domain( $forum_template->topic->topic_last_poster, $forum_template->topic->topic_last_poster_nicename, $forum_template->topic->topic_last_poster_login ) ;
  466. // In the case where no user is found, bp_core_get_user_domain() may return the URL
  467. // of the Members directory
  468. if ( !$domain || $domain == bp_core_get_root_domain() . '/' . bp_get_members_root_slug() . '/' )
  469. return __( 'Deleted User', 'buddypress' );
  470. return apply_filters( 'bp_get_the_topic_last_poster_name', '<a href="' . $domain . '">' . $forum_template->topic->topic_last_poster_displayname . '</a>' );
  471. }
  472. function bp_the_topic_object_avatar( $args = '' ) {
  473. echo bp_get_the_topic_object_avatar( $args );
  474. }
  475. function bp_get_the_topic_object_avatar( $args = '' ) {
  476. global $forum_template;
  477. if ( !isset( $forum_template->topic->object_id ) )
  478. return false;
  479. $defaults = array(
  480. 'type' => 'thumb',
  481. 'width' => false,
  482. 'height' => false,
  483. 'alt' => __( 'Group logo for %s', 'buddypress' )
  484. );
  485. $r = wp_parse_args( $args, $defaults );
  486. extract( $r, EXTR_SKIP );
  487. return apply_filters( 'bp_get_the_topic_object_avatar', bp_core_fetch_avatar( array( 'item_id' => $forum_template->topic->object_id, 'type' => $type, 'object' => 'group', 'width' => $width, 'height' => $height, 'alt' => $alt ) ) );
  488. }
  489. function bp_the_topic_last_poster_avatar( $args = '' ) {
  490. echo bp_get_the_topic_last_poster_avatar( $args );
  491. }
  492. function bp_get_the_topic_last_poster_avatar( $args = '' ) {
  493. global $forum_template;
  494. $defaults = array(
  495. 'type' => 'thumb',
  496. 'width' => false,
  497. 'height' => false,
  498. 'alt' => __( 'Profile picture of %s', 'buddypress' )
  499. );
  500. $r = wp_parse_args( $args, $defaults );
  501. extract( $r, EXTR_SKIP );
  502. return apply_filters( 'bp_get_the_topic_last_poster_avatar', bp_core_fetch_avatar( array( 'email' => $forum_template->topic->topic_last_poster_email, 'item_id' => $forum_template->topic->topic_last_poster, 'type' => $type, 'width' => $width, 'height' => $height, 'alt' => $alt ) ) );
  503. }
  504. function bp_the_topic_start_time() {
  505. echo bp_get_the_topic_start_time();
  506. }
  507. function bp_get_the_topic_start_time() {
  508. global $forum_template;
  509. return apply_filters( 'bp_get_the_topic_start_time', $forum_template->topic->topic_start_time );
  510. }
  511. function bp_the_topic_time() {
  512. echo bp_get_the_topic_time();
  513. }
  514. function bp_get_the_topic_time() {
  515. global $forum_template;
  516. return apply_filters( 'bp_get_the_topic_time', $forum_template->topic->topic_time );
  517. }
  518. function bp_the_topic_forum_id() {
  519. echo bp_get_the_topic_forum_id();
  520. }
  521. function bp_get_the_topic_forum_id() {
  522. global $forum_template;
  523. return apply_filters( 'bp_get_the_topic_forum_id', $forum_template->topic->forum_id );
  524. }
  525. function bp_the_topic_status() {
  526. echo bp_get_the_topic_status();
  527. }
  528. function bp_get_the_topic_status() {
  529. global $forum_template;
  530. return apply_filters( 'bp_get_the_topic_status', $forum_template->topic->topic_status );
  531. }
  532. function bp_the_topic_is_topic_open() {
  533. echo bp_get_the_topic_is_topic_open();
  534. }
  535. function bp_get_the_topic_is_topic_open() {
  536. global $forum_template;
  537. return apply_filters( 'bp_get_the_topic_is_topic_open', $forum_template->topic->topic_open );
  538. }
  539. function bp_the_topic_last_post_id() {
  540. echo bp_get_the_topic_last_post_id();
  541. }
  542. function bp_get_the_topic_last_post_id() {
  543. global $forum_template;
  544. return apply_filters( 'bp_get_the_topic_last_post_id', $forum_template->topic->topic_last_post_id );
  545. }
  546. function bp_the_topic_is_sticky() {
  547. echo bp_get_the_topic_is_sticky();
  548. }
  549. function bp_get_the_topic_is_sticky() {
  550. global $forum_template;
  551. return apply_filters( 'bp_get_the_topic_is_sticky', $forum_template->topic->topic_sticky );
  552. }
  553. function bp_the_topic_total_post_count() {
  554. echo bp_get_the_topic_total_post_count();
  555. }
  556. function bp_get_the_topic_total_post_count() {
  557. global $forum_template;
  558. if ( $forum_template->topic->topic_posts == 1 )
  559. return apply_filters( 'bp_get_the_topic_total_post_count', sprintf( __( '%d post', 'buddypress' ), $forum_template->topic->topic_posts ) );
  560. else
  561. return apply_filters( 'bp_get_the_topic_total_post_count', sprintf( __( '%d posts', 'buddypress' ), $forum_template->topic->topic_posts ) );
  562. }
  563. function bp_the_topic_total_posts() {
  564. echo bp_get_the_topic_total_posts();
  565. }
  566. function bp_get_the_topic_total_posts() {
  567. global $forum_template;
  568. return apply_filters( 'bp_get_the_topic_total_posts', $forum_template->topic->topic_posts );
  569. }
  570. function bp_the_topic_tag_count() {
  571. echo bp_get_the_topic_tag_count();
  572. }
  573. function bp_get_the_topic_tag_count() {
  574. global $forum_template;
  575. return apply_filters( 'bp_get_the_topic_tag_count', $forum_template->topic->tag_count );
  576. }
  577. function bp_the_topic_permalink() {
  578. echo bp_get_the_topic_permalink();
  579. }
  580. function bp_get_the_topic_permalink() {
  581. global $forum_template, $bp;
  582. // The topic is in a loop where its parent object is loaded
  583. if ( bp_get_the_topic_object_slug() ) {
  584. $permalink = trailingslashit( bp_get_root_domain() . '/' . bp_get_groups_root_slug() . '/' . bp_get_the_topic_object_slug() . '/forum' );
  585. // We are viewing a single group topic, so use the current item
  586. } elseif ( bp_is_group_forum_topic() ) {
  587. $permalink = trailingslashit( bp_get_root_domain() . '/' . bp_get_groups_root_slug() . '/' . bp_current_item() . '/forum' );
  588. // We are unsure what the context is, so fallback to forum root slug
  589. } elseif ( bp_is_single_item() ) {
  590. $permalink = trailingslashit( bp_get_root_domain() . '/' . bp_get_forums_root_slug() . '/' . bp_current_item() );
  591. // This is some kind of error situation, so use forum root
  592. } else {
  593. $permalink = trailingslashit( bp_get_root_domain() . '/' . bp_get_forums_root_slug() );
  594. }
  595. return apply_filters( 'bp_get_the_topic_permalink', trailingslashit( $permalink . 'topic/' . $forum_template->topic->topic_slug ) );
  596. }
  597. function bp_the_topic_time_since_created() {
  598. echo bp_get_the_topic_time_since_created();
  599. }
  600. function bp_get_the_topic_time_since_created() {
  601. global $forum_template;
  602. return apply_filters( 'bp_get_the_topic_time_since_created', bp_core_time_since( strtotime( $forum_template->topic->topic_start_time ) ) );
  603. }
  604. function bp_the_topic_latest_post_excerpt( $args = '' ) {
  605. echo bp_get_the_topic_latest_post_excerpt( $args );
  606. }
  607. function bp_get_the_topic_latest_post_excerpt( $args = '' ) {
  608. global $forum_template;
  609. $defaults = array(
  610. 'length' => 225
  611. );
  612. $r = wp_parse_args( $args, $defaults );
  613. extract( $r, EXTR_SKIP );
  614. $post = bp_forums_get_post( $forum_template->topic->topic_last_post_id );
  615. $post = bp_create_excerpt( $post->post_text, $length );
  616. return apply_filters( 'bp_get_the_topic_latest_post_excerpt', $post, $length );
  617. }
  618. function bp_the_topic_time_since_last_post() {
  619. echo bp_get_the_topic_time_since_last_post();
  620. }
  621. function bp_get_the_topic_time_since_last_post() {
  622. global $forum_template;
  623. return apply_filters( 'bp_get_the_topic_time_since_last_post', bp_core_time_since( strtotime( $forum_template->topic->topic_time ) ) );
  624. }
  625. function bp_the_topic_is_mine() {
  626. echo bp_get_the_topic_is_mine();
  627. }
  628. function bp_get_the_topic_is_mine() {
  629. global $forum_template;
  630. return bp_loggedin_user_id() == $forum_template->topic->topic_poster;
  631. }
  632. function bp_the_topic_admin_links( $args = '' ) {
  633. echo bp_get_the_topic_admin_links( $args );
  634. }
  635. function bp_get_the_topic_admin_links( $args = '' ) {
  636. global $forum_template;
  637. $defaults = array(
  638. 'seperator' => '|'
  639. );
  640. $r = wp_parse_args( $args, $defaults );
  641. extract( $r, EXTR_SKIP );
  642. $links[] = '<a href="' . wp_nonce_url( bp_get_the_topic_permalink() . 'edit', 'bp_forums_edit_topic' ) . '">' . __( 'Edit Topic', 'buddypress' ) . '</a>';
  643. if ( bp_is_item_admin() || bp_is_item_mod() || bp_current_user_can( 'bp_moderate' ) ) {
  644. if ( 0 == (int) $forum_template->topic->topic_sticky )
  645. $links[] = '<a href="' . wp_nonce_url( bp_get_the_topic_permalink() . 'stick', 'bp_forums_stick_topic' ) . '">' . __( 'Sticky Topic', 'buddypress' ) . '</a>';
  646. else
  647. $links[] = '<a href="' . wp_nonce_url( bp_get_the_topic_permalink() . 'unstick', 'bp_forums_unstick_topic' ) . '">' . __( 'Un-stick Topic', 'buddypress' ) . '</a>';
  648. if ( 0 == (int) $forum_template->topic->topic_open )
  649. $links[] = '<a href="' . wp_nonce_url( bp_get_the_topic_permalink() . 'open', 'bp_forums_open_topic' ) . '">' . __( 'Open Topic', 'buddypress' ) . '</a>';
  650. else
  651. $links[] = '<a href="' . wp_nonce_url( bp_get_the_topic_permalink() . 'close', 'bp_forums_close_topic' ) . '">' . __( 'Close Topic', 'buddypress' ) . '</a>';
  652. $links[] = '<a class="confirm" id="topic-delete-link" href="' . wp_nonce_url( bp_get_the_topic_permalink() . 'delete', 'bp_forums_delete_topic' ) . '">' . __( 'Delete Topic', 'buddypress' ) . '</a>';
  653. }
  654. return implode( ' ' . $seperator . ' ', (array) $links );
  655. }
  656. function bp_the_topic_css_class() {
  657. echo bp_get_the_topic_css_class();
  658. }
  659. function bp_get_the_topic_css_class() {
  660. global $forum_template;
  661. $class = false;
  662. if ( $forum_template->current_topic % 2 == 1 )
  663. $class .= 'alt';
  664. if ( isset( $forum_template->topic->topic_sticky ) && 1 == (int) $forum_template->topic->topic_sticky )
  665. $class .= ' sticky';
  666. if ( !isset( $forum_template->topic->topic_open ) || 0 == (int) $forum_template->topic->topic_open )
  667. $class .= ' closed';
  668. return apply_filters( 'bp_get_the_topic_css_class', trim( $class ) );
  669. }
  670. function bp_my_forum_topics_link() {
  671. echo bp_get_my_forum_topics_link();
  672. }
  673. function bp_get_my_forum_topics_link() {
  674. global $bp;
  675. return apply_filters( 'bp_get_my_forum_topics_link', bp_get_root_domain() . '/' . bp_get_forums_root_slug() . '/personal/' );
  676. }
  677. function bp_unreplied_forum_topics_link() {
  678. echo bp_get_unreplied_forum_topics_link();
  679. }
  680. function bp_get_unreplied_forum_topics_link() {
  681. global $bp;
  682. return apply_filters( 'bp_get_unreplied_forum_topics_link', bp_get_root_domain() . '/' . bp_get_forums_root_slug() . '/unreplied/' );
  683. }
  684. function bp_popular_forum_topics_link() {
  685. echo bp_get_popular_forum_topics_link();
  686. }
  687. function bp_get_popular_forum_topics_link() {
  688. global $bp;
  689. return apply_filters( 'bp_get_popular_forum_topics_link', bp_get_root_domain() . '/' . bp_get_forums_root_slug() . '/popular/' );
  690. }
  691. function bp_newest_forum_topics_link() {
  692. echo bp_get_newest_forum_topics_link();
  693. }
  694. function bp_get_newest_forum_topics_link() {
  695. global $bp;
  696. return apply_filters( 'bp_get_newest_forum_topics_link', bp_get_root_domain() . '/' . bp_get_forums_root_slug() . '/' );
  697. }
  698. function bp_forum_topic_type() {
  699. echo bp_get_forum_topic_type();
  700. }
  701. function bp_get_forum_topic_type() {
  702. global $bp;
  703. if ( !bp_is_directory() || !bp_current_action() )
  704. return 'newest';
  705. return apply_filters( 'bp_get_forum_topic_type', bp_current_action() );
  706. }
  707. /**
  708. * Echoes the output of bp_get_forum_topic_new_reply_link()
  709. *
  710. * @package BuddyPress
  711. * @since BuddyPress (1.5)
  712. */
  713. function bp_forum_topic_new_reply_link() {
  714. echo bp_get_forum_topic_new_reply_link();
  715. }
  716. /**
  717. * Returns the permalink for the New Reply button at the top of forum topics
  718. *
  719. * @package BuddyPress
  720. * @since BuddyPress (1.5)
  721. *
  722. * @uses apply_filters() Filter bp_get_forum_topic_new_reply_link to modify
  723. * @return string The URL for the New Reply link
  724. */
  725. function bp_get_forum_topic_new_reply_link() {
  726. global $topic_template;
  727. if ( $topic_template->pag->total_pages == $topic_template->pag_page ) {
  728. // If we are on the last page, no need for a URL base
  729. $link = '';
  730. } else {
  731. // Create a link to the last page for the topic
  732. $link = add_query_arg( array(
  733. 'topic_page' => $topic_template->pag->total_pages,
  734. 'num' => $topic_template->pag_num
  735. ), bp_get_the_topic_permalink() );
  736. }
  737. // Tack on the #post-topic-reply anchor before returning
  738. return apply_filters( 'bp_get_forum_topic_new_reply_link', $link . '#post-topic-reply', $link );
  739. }
  740. /**
  741. * Echoes the output of bp_get_forums_tag_name()
  742. *
  743. * @package BuddyPress
  744. * @todo Deprecate?
  745. */
  746. function bp_forums_tag_name() {
  747. echo bp_get_forums_tag_name();
  748. }
  749. /**
  750. * Outputs the currently viewed tag name
  751. *
  752. * @package BuddyPress
  753. * @todo Deprecate? Seems unused
  754. */
  755. function bp_get_forums_tag_name() {
  756. $tag_name = bp_is_directory() && bp_is_forums_component() ? bp_action_variable( 0 ) : false;
  757. return apply_filters( 'bp_get_forums_tag_name', $tag_name );
  758. }
  759. function bp_forum_pagination() {
  760. echo bp_get_forum_pagination();
  761. }
  762. function bp_get_forum_pagination() {
  763. global $forum_template;
  764. return apply_filters( 'bp_get_forum_pagination', $forum_template->pag_links );
  765. }
  766. function bp_forum_pagination_count() {
  767. echo bp_get_forum_pagination_count();
  768. }
  769. function bp_get_forum_pagination_count() {
  770. global $bp, $forum_template;
  771. $start_num = intval( ( $forum_template->pag_page - 1 ) * $forum_template->pag_num ) + 1;
  772. $from_num = bp_core_number_format( $start_num );
  773. $to_num = bp_core_number_format( ( $start_num + ( $forum_template->pag_num - 1 ) > $forum_template->total_topic_count ) ? $forum_template->total_topic_count : $start_num + ( $forum_template->pag_num - 1 ) );
  774. $total = bp_core_number_format( $forum_template->total_topic_count );
  775. $pag_filter = false;
  776. if ( 'tags' == $forum_template->type && !empty( $forum_template->search_terms ) )
  777. $pag_filter = sprintf( __( ' matching tag "%s"', 'buddypress' ), $forum_template->search_terms );
  778. return apply_filters( 'bp_get_forum_pagination_count', sprintf( __( 'Viewing topic %s to %s (of %s total topics%s)', 'buddypress' ), $from_num, $to_num, $total, $pag_filter ) );
  779. }
  780. function bp_is_edit_topic() {
  781. global $bp;
  782. if ( bp_is_action_variable( 'post' ) && bp_is_action_variable( 'edit' ) )
  783. return false;
  784. return true;
  785. }
  786. class BP_Forums_Template_Topic {
  787. var $current_post = -1;
  788. var $post_count;
  789. var $posts;
  790. var $post;
  791. var $forum_id;
  792. var $topic_id;
  793. var $topic;
  794. var $in_the_loop;
  795. /**
  796. * Contains a 'total_pages' property holding total number of pages in this loop.
  797. *
  798. * @since BuddyPress (1.2)
  799. * @var stdClass
  800. */
  801. public $pag;
  802. var $pag_page;
  803. var $pag_num;
  804. var $pag_links;
  805. var $total_post_count;
  806. var $single_post = false;
  807. var $sort_by;
  808. var $order;
  809. function __construct( $topic_id, $per_page, $max, $order ) {
  810. global $bp, $current_user, $forum_template;
  811. if ( !isset( $forum_template ) ) {
  812. $forum_template = new stdClass;
  813. }
  814. $this->pag_page = isset( $_REQUEST['topic_page'] ) ? intval( $_REQUEST['topic_page'] ) : 1;
  815. $this->pag_num = isset( $_REQUEST['num'] ) ? intval( $_REQUEST['num'] ) : $per_page;
  816. $this->order = $order;
  817. $this->topic_id = $topic_id;
  818. $forum_template->topic = (object) bp_forums_get_topic_details( $this->topic_id );
  819. $this->forum_id = $forum_template->topic->forum_id;
  820. $this->posts = bp_forums_get_topic_posts( array( 'topic_id' => $this->topic_id, 'page' => $this->pag_page, 'per_page' => $this->pag_num, 'order' => $this->order ) );
  821. if ( !$this->posts ) {
  822. $this->post_count = 0;
  823. $this->total_post_count = 0;
  824. } else {
  825. if ( !$max || $max >= (int) $forum_template->topic->topic_posts ) {
  826. $this->total_post_count = (int) $forum_template->topic->topic_posts;
  827. } else {
  828. $this->total_post_count = (int) $max;
  829. }
  830. if ( $max ) {
  831. if ( $max >= count( $this->posts ) ) {
  832. $this->post_count = count( $this->posts );
  833. } else {
  834. $this->post_count = (int) $max;
  835. }
  836. } else {
  837. $this->post_count = count( $this->posts );
  838. }
  839. }
  840. // Load topic tags
  841. $this->topic_tags = bb_get_topic_tags( $this->topic_id );
  842. $this->pag = new stdClass;
  843. if ( (int) $this->total_post_count && (int) $this->pag_num ) {
  844. $this->pag_links = paginate_links( array(
  845. 'base' => add_query_arg( array( 'topic_page' => '%#%', 'num' => (int) $this->pag_num ) ),
  846. 'format' => '',
  847. 'total' => ceil( (int) $this->total_post_count / (int) $this->pag_num ),
  848. 'current' => $this->pag_page,
  849. 'prev_text' => _x( '&larr;', 'Forum thread pagination previous text', 'buddypress' ),
  850. 'next_text' => _x( '&rarr;', 'Forum thread pagination next text', 'buddypress' ),
  851. 'mid_size' => 1
  852. ) );
  853. $this->pag->total_pages = ceil( (int) $this->total_post_count / (int) $this->pag_num );
  854. } else {
  855. $this->pag->total_pages = 1;
  856. }
  857. }
  858. function has_posts() {
  859. if ( $this->post_count )
  860. return true;
  861. return false;
  862. }
  863. function next_post() {
  864. $this->current_post++;
  865. $this->post = $this->posts[$this->current_post];
  866. return $this->post;
  867. }
  868. function rewind_posts() {
  869. $this->current_post = -1;
  870. if ( $this->post_count > 0 ) {
  871. $this->post = $this->posts[0];
  872. }
  873. }
  874. function user_posts() {
  875. if ( $this->current_post + 1 < $this->post_count ) {
  876. return true;
  877. } elseif ( $this->current_post + 1 == $this->post_count ) {
  878. do_action('topic_loop_end');
  879. // Do some cleaning up after the loop
  880. $this->rewind_posts();
  881. }
  882. $this->in_the_loop = false;
  883. return false;
  884. }
  885. function the_post() {
  886. global $post;
  887. $this->in_the_loop = true;
  888. $this->post = $this->next_post();
  889. $this->post = (object)$this->post;
  890. if ( $this->current_post == 0 ) // loop has just started
  891. do_action('topic_loop_start');
  892. }
  893. }
  894. function bp_has_forum_topic_posts( $args = '' ) {
  895. global $topic_template;
  896. $defaults = array(
  897. 'topic_id' => false,
  898. 'per_page' => 15,
  899. 'max' => false,
  900. 'order' => 'ASC'
  901. );
  902. $r = wp_parse_args( $args, $defaults );
  903. extract( $r, EXTR_SKIP );
  904. if ( empty( $topic_id ) && bp_is_groups_component() && bp_is_current_action( 'forum' ) && bp_is_action_variable( 'topic', 0 ) && bp_action_variable( 1 ) )
  905. $topic_id = bp_forums_get_topic_id_from_slug( bp_action_variable( 1 ) );
  906. elseif ( empty( $topic_id ) && bp_is_forums_component() && bp_is_current_action( 'topic' ) && bp_action_variable( 0 ) )
  907. $topic_id = bp_forums_get_topic_id_from_slug( bp_action_variable( 0 ) );
  908. if ( empty( $topic_id ) ) {
  909. return false;
  910. } else {
  911. $topic_template = new BP_Forums_Template_Topic( (int) $topic_id, $per_page, $max, $order );
  912. // Current topic forum_id needs to match current_group forum_id
  913. if ( bp_is_groups_component() && $topic_template->forum_id != groups_get_groupmeta( bp_get_current_group_id(), 'forum_id' ) )
  914. return false;
  915. }
  916. return apply_filters( 'bp_has_topic_posts', $topic_template->has_posts(), $topic_template );
  917. }
  918. function bp_forum_topic_posts() {
  919. global $topic_template;
  920. return $topic_template->user_posts();
  921. }
  922. function bp_the_forum_topic_post() {
  923. global $topic_template;
  924. return $topic_template->the_post();
  925. }
  926. function bp_the_topic_post_id() {
  927. echo bp_get_the_topic_post_id();
  928. }
  929. function bp_get_the_topic_post_id() {
  930. global $topic_template;
  931. return apply_filters( 'bp_get_the_topic_post_id', $topic_template->post->post_id );
  932. }
  933. function bp_the_topic_post_content() {
  934. echo bp_get_the_topic_post_content();
  935. }
  936. function bp_get_the_topic_post_content() {
  937. global $topic_template;
  938. return apply_filters( 'bp_get_the_topic_post_content', stripslashes( $topic_template->post->post_text ) );
  939. }
  940. function bp_the_topic_post_css_class() {
  941. echo bp_get_the_topic_post_css_class();
  942. }
  943. function bp_get_the_topic_post_css_class() {
  944. global $topic_template;
  945. $class = false;
  946. if ( $topic_template->current_post % 2 == 1 )
  947. $class .= 'alt';
  948. if ( 1 == (int) $topic_template->post->post_status )
  949. $class .= ' deleted';
  950. if ( 0 == (int) $topic_template->post->post_status )
  951. $class .= ' open';
  952. return apply_filters( 'bp_get_the_topic_post_css_class', trim( $class ) );
  953. }
  954. function bp_the_topic_post_poster_avatar( $args = '' ) {
  955. echo bp_get_the_topic_post_poster_avatar( $args );
  956. }
  957. function bp_get_the_topic_post_poster_avatar( $args = '' ) {
  958. global $topic_template;
  959. $defaults = array(
  960. 'type' => 'thumb',
  961. 'width' => 20,
  962. 'height' => 20,
  963. 'alt' => __( 'Profile picture of %s', 'buddypress' )
  964. );
  965. $r = wp_parse_args( $args, $defaults );
  966. extract( $r, EXTR_SKIP );
  967. return apply_filters( 'bp_get_the_topic_post_poster_avatar', bp_core_fetch_avatar( array( 'item_id' => $topic_template->post->poster_id, 'type' => $type, 'width' => $width, 'height' => $height, 'alt' => $alt ) ) );
  968. }
  969. function bp_the_topic_post_poster_name() {
  970. echo bp_get_the_topic_post_poster_name();
  971. }
  972. function bp_get_the_topic_post_poster_name() {
  973. global $topic_template;
  974. if ( empty( $topic_template->post->poster_name ) || ( !$link = bp_core_get_user_domain( $topic_template->post->poster_id ) ) )
  975. return __( 'Deleted User', 'buddypress' );
  976. return apply_filters( 'bp_get_the_topic_post_poster_name', '<a href="' . $link . '" title="' . $topic_template->post->poster_name . '">' . $topic_template->post->poster_name . '</a>' );
  977. }
  978. function bp_the_topic_post_poster_link() {
  979. echo bp_get_the_topic_post_poster_link();
  980. }
  981. function bp_get_the_topic_post_poster_link() {
  982. global $topic_template;
  983. return apply_filters( 'bp_the_topic_post_poster_link', bp_core_get_user_domain( $topic_template->post->poster_id ) );
  984. }
  985. function bp_the_topic_post_time_since() {
  986. echo bp_get_the_topic_post_time_since();
  987. }
  988. function bp_get_the_topic_post_time_since() {
  989. global $topic_template;
  990. return apply_filters( 'bp_get_the_topic_post_time_since', bp_core_time_since( strtotime( $topic_template->post->post_time ) ) );
  991. }
  992. function bp_the_topic_post_is_mine() {
  993. echo bp_the_topic_post_is_mine();
  994. }
  995. function bp_get_the_topic_post_is_mine() {
  996. global $bp, $topic_template;
  997. return bp_loggedin_user_id() == $topic_template->post->poster_id;
  998. }
  999. function bp_the_topic_post_admin_links( $args = '' ) {
  1000. echo bp_get_the_topic_post_admin_links( $args );
  1001. }
  1002. function bp_get_the_topic_post_admin_links( $args = '' ) {
  1003. global $topic_template;
  1004. // Never show for the first post in a topic.
  1005. if ( 0 == $topic_template->current_post && 1 == $topic_template->pag_page )
  1006. return;
  1007. $defaults = array(
  1008. 'separator' => ' | '
  1009. );
  1010. $r = wp_parse_args( $args, $defaults );
  1011. extract( $r, EXTR_SKIP );
  1012. $query_vars = '';
  1013. if ( $_SERVER['QUERY_STRING'] )
  1014. $query_vars = '?' . $_SERVER['QUERY_STRING'];
  1015. $links = array();
  1016. $links[] = '<a href="' . wp_nonce_url( bp_get_the_topic_permalink() . 'edit/post/' . $topic_template->post->post_id . '/' . $query_vars, 'bp_forums_edit_post' ) . '">' . __( 'Edit', 'buddypress' ) . '</a>';
  1017. $links[] .= '<a class="confirm" id="post-delete-link" href="' . wp_nonce_url( bp_get_the_topic_permalink() . 'delete/post/' . $topic_template->post->post_id, 'bp_forums_delete_post' ) . '">' . __( 'Delete', 'buddypress' ) . '</a>';
  1018. return apply_filters( 'bp_get_the_topic_post_admin_links', implode( $separator, $links ), $links, $r );
  1019. }
  1020. function bp_the_topic_post_edit_text() {
  1021. echo bp_get_the_topic_post_edit_text();
  1022. }
  1023. function bp_get_the_topic_post_edit_text() {
  1024. $post = bp_forums_get_post( bp_action_variable( 4 ) );
  1025. return apply_filters( 'bp_get_the_topic_post_edit_text', esc_attr( $post->post_text ) );
  1026. }
  1027. function bp_the_topic_pagination() {
  1028. echo bp_get_the_topic_pagination();
  1029. }
  1030. function bp_get_the_topic_pagination() {
  1031. global $topic_template;
  1032. return apply_filters( 'bp_get_the_topic_pagination', $topic_template->pag_links );
  1033. }
  1034. function bp_the_topic_pagination_count() {
  1035. global $bp, $topic_template;
  1036. $start_num = intval( ( $topic_template->pag_page - 1 ) * $topic_template->pag_num ) + 1;
  1037. $from_num = bp_core_number_format( $start_num );
  1038. $to_num = bp_core_number_format( ( $start_num + ( $topic_template->pag_num - 1 ) > $topic_template->total_post_count ) ? $topic_template->total_post_count : $start_num + ( $topic_template->pag_num - 1 ) );
  1039. $total = bp_core_number_format( $topic_template->total_post_count );
  1040. echo apply_filters( 'bp_the_topic_pagination_count', sprintf( __( 'Viewing post %1$s to %2$s (%3$s total posts)', 'buddypress' ), $from_num, $to_num, $total ) );
  1041. }
  1042. function bp_the_topic_is_last_page() {
  1043. echo bp_get_the_topic_is_last_page();
  1044. }
  1045. function bp_get_the_topic_is_last_page() {
  1046. global $topic_template;
  1047. return apply_filters( 'bp_get_the_topic_is_last_page', $topic_template->pag_page == $topic_template->pag->total_pages );
  1048. }
  1049. function bp_directory_forums_search_form() {
  1050. global $bp;
  1051. $default_search_value = bp_get_search_default_text( 'forums' );
  1052. $search_value = !empty( $_REQUEST['fs'] ) ? stripslashes( $_REQUEST['fs'] ) : $default_search_value; ?>
  1053. <form action="" method="get" id="search-forums-form">
  1054. <label><input type="text" name="s" id="forums_search" placeholder="<?php echo esc_attr( $search_value ); ?>" /></label>
  1055. <input type="submit" id="forums_search_submit" name="forums_search_submit" value="<?php _e( 'Search', 'buddypress' ); ?>" />
  1056. </form>
  1057. <?php
  1058. }
  1059. function bp_forum_permalink( $forum_id = 0 ) {
  1060. echo bp_get_forum_permalink( $forum_id );
  1061. }
  1062. function bp_get_forum_permalink( $forum_id = 0 ) {
  1063. global $bp;
  1064. if ( bp_is_groups_component() ) {
  1065. $permalink = trailingslashit( bp_get_root_domain() . '/' . bp_get_groups_root_slug() . '/' . bp_current_item() . '/forum' );
  1066. } else {
  1067. if ( empty( $forum_id ) ) {
  1068. global $topic_template;
  1069. if ( isset( $topic_template->forum_id ) )
  1070. $forum_id = $topic_template->forum_id;
  1071. }
  1072. if ( $forum = bp_forums_get_forum( $forum_id ) )
  1073. $permalink = trailingslashit( bp_get_root_domain() . '/' . bp_get_forums_root_slug() . '/forum/' . $forum->forum_slug );
  1074. else
  1075. return false;
  1076. }
  1077. return apply_filters( 'bp_get_forum_permalink', trailingslashit( $permalink ) );
  1078. }
  1079. function bp_forum_name( $forum_id = 0 ) {
  1080. echo bp_get_forum_name( $forum_id );
  1081. }
  1082. function bp_get_forum_name( $forum_id = 0 ) {
  1083. global $bp;
  1084. if ( empty( $forum_id ) ) {
  1085. global $topic_template;
  1086. if ( isset( $topic_template->forum_id ) )
  1087. $forum_id = $topic_template->forum_id;
  1088. }
  1089. if ( $forum = bp_forums_get_forum( $forum_id ) )
  1090. return apply_filters( 'bp_get_forum_name', $forum->forum_name, $forum->forum_id );
  1091. else
  1092. return false;
  1093. }
  1094. function bp_forums_tag_heat_map( $args = '' ) {
  1095. $defaults = array(
  1096. 'smallest' => '10',
  1097. 'largest' => '42',
  1098. 'sizing' => 'px',
  1099. 'limit' => '50'
  1100. );
  1101. $r = wp_parse_args( $args, $defaults );
  1102. extract( $r, EXTR_SKIP );
  1103. bb_tag_heat_map( $smallest, $largest, $sizing, $limit );
  1104. }
  1105. /**
  1106. * Echo the current topic's tag list, comma-separated
  1107. *
  1108. * @package BuddyPress
  1109. * @since BuddyPress (1.5)
  1110. */
  1111. function bp_forum_topic_tag_list() {
  1112. echo bp_get_forum_topic_tag_list();
  1113. }
  1114. /**
  1115. * Get the current topic's tag list
  1116. *
  1117. * @package BuddyPress
  1118. * @since BuddyPress (1.5)
  1119. *
  1120. * @param string $format 'string' returns comma-separated string; otherwise returns array
  1121. * @return mixed $tags
  1122. */
  1123. function bp_get_forum_topic_tag_list( $format = 'string' ) {
  1124. global $topic_template;
  1125. $tags_data = !empty( $topic_template->topic_tags ) ? $topic_template->topic_tags : false;
  1126. $tags = array();
  1127. if ( $tags_data ) {
  1128. foreach( $tags_data as $tag_data ) {
  1129. $tags[] = $tag_data->name;
  1130. }
  1131. }
  1132. if ( 'string' == $format )
  1133. $tags = implode( ', ', $tags );
  1134. return apply_filters( 'bp_forum_topic_tag_list', $tags, $format );
  1135. }
  1136. /**
  1137. * Returns true if the current topic has tags
  1138. *
  1139. * @package BuddyPress
  1140. * @since BuddyPress (1.5)
  1141. *
  1142. * @return bool
  1143. */
  1144. function bp_forum_topic_has_tags() {
  1145. global $topic_template;
  1146. $has_tags = false;
  1147. if ( !empty( $topic_template->topic_tags ) )
  1148. $has_tags = true;
  1149. return apply_filters( 'bp_forum_topic_has_tags', $has_tags );
  1150. }
  1151. function bp_forum_action() {
  1152. echo bp_get_forum_action();
  1153. }
  1154. function bp_get_forum_action() {
  1155. global $topic_template;
  1156. return apply_filters( 'bp_get_forum_action', bp_get_root_domain() . esc_attr( $_SERVER['REQUEST_URI'] ) );
  1157. }
  1158. function bp_forum_topic_action() {
  1159. echo bp_get_forum_topic_action();
  1160. }
  1161. function bp_get_forum_topic_action() {
  1162. return apply_filters( 'bp_get_forum_topic_action', $_SERVER['REQUEST_URI'] );
  1163. }
  1164. function bp_forum_topic_count_for_user( $user_id = 0 ) {
  1165. echo bp_get_forum_topic_count_for_user( $user_id );
  1166. }
  1167. function bp_get_forum_topic_count_for_user( $user_id = 0 ) {
  1168. return apply_filters( 'bp_get_forum_topic_count_for_user', bp_forums_total_topic_count_for_user( $user_id ) );
  1169. }
  1170. function bp_forum_topic_count( $user_id = 0 ) {
  1171. echo bp_get_forum_topic_count( $user_id );
  1172. }
  1173. function bp_get_forum_topic_count( $user_id = 0 ) {
  1174. return apply_filters( 'bp_get_forum_topic_count', bp_forums_total_topic_count( $user_id ) );
  1175. }