BP_Forums_Template_Forum

The main forums template loop class.

Defined (1)

The class is defined in the following location(s).

/bp-forums/bp-forums-template.php  
  1. class BP_Forums_Template_Forum { 
  2. /** 
  3. * The loop iterator. 
  4. * @since 1.0.0 
  5. * @var int 
  6. */ 
  7. public $current_topic = -1; 
  8.  
  9. /** 
  10. * The number of topics returned by the paged query. 
  11. * @since 1.0.0 
  12. * @var int 
  13. */ 
  14. public $topic_count; 
  15.  
  16. /** 
  17. * Array of topics located by the query. 
  18. * @since 1.0.0 
  19. * @var array 
  20. */ 
  21. public $topics; 
  22.  
  23. /** 
  24. * The topic object currently being iterated on. 
  25. * @since 1.0.0 
  26. * @var object 
  27. */ 
  28. public $topic; 
  29.  
  30. /** 
  31. * The ID of the forum whose topics are being queried. 
  32. * @since 1.5.0 
  33. * @var int 
  34. */ 
  35. public $forum_id; 
  36.  
  37. /** 
  38. * A flag for whether the loop is currently being iterated. 
  39. * @since 1.0.0 
  40. * @var bool 
  41. */ 
  42. public $in_the_loop; 
  43.  
  44. /** 
  45. * The page number being requested. 
  46. * @since 1.0.0 
  47. * @var int 
  48. */ 
  49. public $pag_page; 
  50.  
  51. /** 
  52. * The number of items being requested per page. 
  53. * @since 1.0.0 
  54. * @var int 
  55. */ 
  56. public $pag_num; 
  57.  
  58. /** 
  59. * An HTML string containing pagination links. 
  60. * @since 1.0.0 
  61. * @var string 
  62. */ 
  63. public $pag_links; 
  64.  
  65. /** 
  66. * The total number of topics matching the query parameters. 
  67. * @since 1.0.0 
  68. * @var int 
  69. */ 
  70. public $total_topic_count; 
  71.  
  72. /** 
  73. * Whether requesting a single topic. Not currently used. 
  74. * @since 1.0.0 
  75. * @var bool 
  76. */ 
  77. public $single_topic = false; 
  78.  
  79. /** 
  80. * Term to sort by. Not currently used. 
  81. * @since 1.0.0 
  82. * @var string 
  83. */ 
  84. public $sort_by; 
  85.  
  86. /** 
  87. * Sort order. Not currently used. 
  88. * @since 1.0.0 
  89. * @var string 
  90. */ 
  91. public $order; 
  92.  
  93. /** 
  94. * Constructor method. 
  95. * @since 1.5.0 
  96. * @param string $type The 'type' is the sort order/kind. 'newest',  
  97. * 'popular', 'unreplied', 'tags'. 
  98. * @param int $forum_id The ID of the forum for which topics are being 
  99. * queried. 
  100. * @param int $user_id The ID of the user to whom topics should be 
  101. * limited. Pass false to remove this filter. 
  102. * @param int $page The number of the page being requested. 
  103. * @param int $per_page The number of items being requested per page. 
  104. * @param int $max Max amount to retrieve. 
  105. * @param string $no_stickies Requested sticky format. 
  106. * @param string $search_terms Filter results by a string. 
  107. * @param int|bool $offset Optional. Offset results by a given numeric value. 
  108. * @param int|bool $number Optional. Total number of items to retrieve. 
  109. */ 
  110. function __construct( $type, $forum_id, $user_id, $page, $per_page, $max, $no_stickies, $search_terms, $offset = false, $number = false ) { 
  111. $bp = buddypress(); 
  112.  
  113. $this->pag_page = $page; 
  114. $this->pag_num = $per_page; 
  115. $this->type = $type; 
  116. $this->search_terms = $search_terms; 
  117. $this->forum_id = $forum_id; 
  118. $this->offset = $offset; 
  119. $this->number = $number; 
  120.  
  121. switch ( $type ) { 
  122. case 'newest': default: 
  123. $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 ) ); 
  124. break; 
  125.  
  126. case 'popular': 
  127. $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 ) ); 
  128. break; 
  129.  
  130. case 'unreplied': 
  131. $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 ) ); 
  132. break; 
  133.  
  134. case 'tags': 
  135. $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 ) ); 
  136. break; 
  137.  
  138. /** 
  139. * Filters the forums template topics. 
  140. * @since 1.1.0 
  141. * @param array $topics Array of topics being set for display. 
  142. * @param string $type The sort order/kind. 'newest',  
  143. * 'popular', 'unreplied', 'tags'. 
  144. * @param int $forum_id ID for the current forum. 
  145. * @param int $per_page The number of the page being requested. 
  146. * @param int $max The max number of posts to show. 
  147. * @param string $no_stickies Requested sticky format. 
  148. */ 
  149. $this->topics = apply_filters( 'bp_forums_template_topics', $this->topics, $type, $forum_id, $per_page, $max, $no_stickies ); 
  150.  
  151. if ( !(int) $this->topics ) { 
  152. $this->topic_count = 0; 
  153. $this->total_topic_count = 0; 
  154. } else { 
  155. // Get a total topic count, for use in pagination. This value will differ 
  156. // depending on scope. 
  157. if ( !empty( $forum_id ) ) { 
  158. // Group forums. 
  159. $topic_count = bp_forums_get_forum( $forum_id ); 
  160. $topic_count = (int) $topic_count->topics; 
  161. } else if ( !empty( $bp->groups->current_group ) ) { 
  162. $topic_count = (int)groups_total_public_forum_topic_count( $type ); 
  163. } else if ( bp_is_user_forums_started() || ( bp_is_directory() && $user_id ) ) { 
  164. // This covers the case of Profile > Forums > Topics Started, as 
  165. // well as Forum Directory > My Topics. 
  166. $topic_count = bp_forums_total_topic_count_for_user( bp_displayed_user_id(), $type ); 
  167. } else if ( bp_is_user_forums_replied_to() ) { 
  168. // Profile > Forums > Replied To. 
  169. $topic_count = bp_forums_total_replied_count_for_user( bp_displayed_user_id(), $type ); 
  170. } else if ( 'tags' == $type ) { 
  171. $tag = bb_get_tag( $search_terms ); 
  172. $topic_count = $tag->count; 
  173. } else { 
  174. // For forum directories (All Topics), get a true count 
  175. $status = bp_current_user_can( 'bp_moderate' ) ? 'all' : 'public'; // @todo: member-of. 
  176. $topic_count = (int)groups_total_forum_topic_count( $status, $search_terms ); 
  177.  
  178. if ( !$max || $max >= $topic_count ) { 
  179. $this->total_topic_count = $topic_count; 
  180. } else { 
  181. $this->total_topic_count = (int) $max; 
  182.  
  183. if ( $max ) { 
  184. if ( $max >= count($this->topics) ) { 
  185. $this->topic_count = count( $this->topics ); 
  186. } else { 
  187. $this->topic_count = (int) $max; 
  188. } else { 
  189. $this->topic_count = count( $this->topics ); 
  190.  
  191. /** 
  192. * Filters the topic count for the forum being displayed. 
  193. * @since 1.1.0 
  194. * @param int $topic_count Topic count to be displayed. 
  195. * @param array $topics Array of topics to be displayed. 
  196. * @param string $type The sort order/kind. 'newest',  
  197. * 'popular', 'unreplied', 'tags'. 
  198. * @param int $forum_id ID for the current forum. 
  199. * @param int $per_page The number of the page being requested. 
  200. * @param int $max The max number of posts to show. 
  201. * @param string $no_stickies Requested sticky format. 
  202. */ 
  203. $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 ) ); 
  204.  
  205. /** 
  206. * Filters the total topic count for the forum being displayed. 
  207. * @since 1.1.0 
  208. * @param int $total_topic_count Total topic count found. 
  209. * @param int $topic_count Topic count to be displayed. 
  210. * @param array $topics Array of topics to be displayed. 
  211. * @param string $type The sort order/kind. 'newest',  
  212. * 'popular', 'unreplied', 'tags'. 
  213. * @param int $forum_id ID for the current forum. 
  214. * @param int $per_page The number of the page being requested. 
  215. * @param int $max The max number of posts to show. 
  216. * @param string $no_stickies Requested sticky format. 
  217. */ 
  218. $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 ) ); 
  219.  
  220. // Fetch extra information for topics, so we don't have to query inside the loop. 
  221. $this->topics = bp_forums_get_topic_extras( $this->topics ); 
  222.  
  223. if ( (int) $this->total_topic_count && (int) $this->pag_num ) { 
  224. $this->pag_links = paginate_links( array( 
  225. 'base' => add_query_arg( array( 'p' => '%#%', 'n' => $this->pag_num ) ),  
  226. 'format' => '',  
  227. 'total' => ceil( (int) $this->total_topic_count / (int) $this->pag_num),  
  228. 'current' => $this->pag_page,  
  229. 'prev_text' => _x( '←', 'Forum topic pagination previous text', 'buddypress' ),  
  230. 'next_text' => _x( '→', 'Forum topic pagination next text', 'buddypress' ),  
  231. 'mid_size' => 1,  
  232. 'add_args' => array(),  
  233. ) ); 
  234.  
  235. /** 
  236. * Whether there are topics available in the loop. 
  237. * @since 1.0.0 
  238. * @see bp_has_forum_topics() 
  239. * @return bool True if there are items in the loop, otherwise false. 
  240. */ 
  241. function has_topics() { 
  242. if ( $this->topic_count ) { 
  243. return true; 
  244.  
  245. return false; 
  246.  
  247. /** 
  248. * Set up the next topic and iterate index. 
  249. * @since 1.0.0 
  250. * @return object The next topic to iterate over. 
  251. */ 
  252. function next_topic() { 
  253. $this->current_topic++; 
  254. $this->topic = $this->topics[$this->current_topic]; 
  255.  
  256. return $this->topic; 
  257.  
  258. /** 
  259. * Rewind the topics and reset topic index. 
  260. * @since 1.0.0 
  261. */ 
  262. function rewind_topics() { 
  263. $this->current_topic = -1; 
  264. if ( $this->topic_count > 0 ) { 
  265. $this->topic = $this->topics[0]; 
  266.  
  267. /** 
  268. * Whether there are blogs left in the loop to iterate over. 
  269. * This method is used by {@link bp_forum_topics()} as part of the while loop 
  270. * that controls iteration inside the blogs loop, eg: 
  271. * while ( bp_forum_topics() ) { ... 
  272. * @see bp_forum_topics() 
  273. * @since 1.0.0 
  274. * @return bool True if there are more topics to show, otherwise false. 
  275. */ 
  276. function user_topics() { 
  277. if ( $this->current_topic + 1 < $this->topic_count ) { 
  278. return true; 
  279. } elseif ( $this->current_topic + 1 == $this->topic_count ) { 
  280.  
  281. /** 
  282. * Fires right before the rewinding of user topics. 
  283. * @since 1.5.0 
  284. */ 
  285. do_action('forum_loop_end'); 
  286. // Do some cleaning up after the loop. 
  287. $this->rewind_topics(); 
  288.  
  289. $this->in_the_loop = false; 
  290. return false; 
  291.  
  292. /** 
  293. * Set up the current topic in the loop. 
  294. * @since 1.0.0 
  295. * @see bp_the_forum_topic() 
  296. */ 
  297. function the_topic() { 
  298. global $topic; 
  299.  
  300. $this->in_the_loop = true; 
  301. $this->topic = $this->next_topic(); 
  302. $this->topic = (object)$this->topic; 
  303.  
  304. if ( $this->current_topic == 0 ) { 
  305.  
  306. /** 
  307. * Fires if the current topic item is the first in the topic loop. 
  308. * @since 1.5.0 
  309. */ 
  310. do_action( 'forum_loop_start' );