BP_Forums_Template_Topic

The single forum topic 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_Topic { 
  2. /** 
  3. * The loop iterator. 
  4. * @since 1.0.0 
  5. * @var int 
  6. */ 
  7. public $current_post = -1; 
  8.  
  9. /** 
  10. * The number of posts returned by the paged query. 
  11. * @since 1.0.0 
  12. * @var int 
  13. */ 
  14. public $post_count; 
  15.  
  16. /** 
  17. * Array of posts located by the query. 
  18. * @since 1.0.0 
  19. * @var array 
  20. */ 
  21. public $posts; 
  22.  
  23. /** 
  24. * The post object currently being iterated on. 
  25. * @since 1.0.0 
  26. * @var object 
  27. */ 
  28. public $post; 
  29.  
  30. /** 
  31. * The ID of the forum whose topic is being queried. 
  32. * @since 1.5.0 
  33. * @var int 
  34. */ 
  35. public $forum_id; 
  36.  
  37. /** 
  38. * The ID of the topic whose posts are being queried. 
  39. * @since 1.0.0 
  40. * @var int 
  41. */ 
  42. public $topic_id; 
  43.  
  44. /** 
  45. * The topic object to which the posts belong. 
  46. * @since 1.0.0 
  47. * @var object 
  48. */ 
  49. public $topic; 
  50.  
  51. /** 
  52. * A flag for whether the loop is currently being iterated. 
  53. * @since 1.0.0 
  54. * @var bool 
  55. */ 
  56. public $in_the_loop; 
  57.  
  58. /** 
  59. * Contains a 'total_pages' property holding total number of pages in 
  60. * this loop. 
  61. * @since 1.2.0 
  62. * @var stdClass 
  63. */ 
  64. public $pag; 
  65.  
  66. /** 
  67. * The page number being requested. 
  68. * @since 1.0.0 
  69. * @var int 
  70. */ 
  71. public $pag_page; 
  72.  
  73. /** 
  74. * The number of items being requested per page. 
  75. * @since 1.0.0 
  76. * @var int 
  77. */ 
  78. public $pag_num; 
  79.  
  80. /** 
  81. * An HTML string containing pagination links. 
  82. * @since 1.0.0 
  83. * @var string 
  84. */ 
  85. public $pag_links; 
  86.  
  87. /** 
  88. * The total number of posts matching the query parameters. 
  89. * @since 1.0.0 
  90. * @var int 
  91. */ 
  92. public $total_post_count; 
  93.  
  94. /** 
  95. * Whether requesting a single topic. Not currently used. 
  96. * @since 1.0.0 
  97. * @var bool 
  98. */ 
  99. public $single_post = false; 
  100.  
  101. /** 
  102. * Term to sort by. 
  103. * @since 1.0.0 
  104. * @var string 
  105. */ 
  106. public $sort_by; 
  107.  
  108. /** 
  109. * Sort order. 
  110. * @since 1.0.0 
  111. * @var string 
  112. */ 
  113. public $order; 
  114.  
  115. /** 
  116. * Constructor method. 
  117. * @since 1.5.0 
  118. * @param int $topic_id ID of the topic whose posts are being requested. 
  119. * @param int $per_page Number of items to return per page. 
  120. * @param int $max Max records to return. 
  121. * @param string $order Direction to order results. 
  122. */ 
  123. function __construct( $topic_id, $per_page, $max, $order ) { 
  124. global $forum_template; 
  125.  
  126. if ( !isset( $forum_template ) ) { 
  127. $forum_template = new stdClass; 
  128.  
  129. $this->pag_page = isset( $_REQUEST['topic_page'] ) ? intval( $_REQUEST['topic_page'] ) : 1; 
  130. $this->pag_num = isset( $_REQUEST['num'] ) ? intval( $_REQUEST['num'] ) : $per_page; 
  131.  
  132. $this->order = $order; 
  133. $this->topic_id = $topic_id; 
  134. $forum_template->topic = (object) bp_forums_get_topic_details( $this->topic_id ); 
  135. $this->forum_id = $forum_template->topic->forum_id; 
  136.  
  137. $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 ) ); 
  138.  
  139. if ( !$this->posts ) { 
  140. $this->post_count = 0; 
  141. $this->total_post_count = 0; 
  142. } else { 
  143. if ( !$max || $max >= (int) $forum_template->topic->topic_posts ) { 
  144. $this->total_post_count = (int) $forum_template->topic->topic_posts; 
  145. } else { 
  146. $this->total_post_count = (int) $max; 
  147.  
  148. if ( $max ) { 
  149. if ( $max >= count( $this->posts ) ) { 
  150. $this->post_count = count( $this->posts ); 
  151. } else { 
  152. $this->post_count = (int) $max; 
  153. } else { 
  154. $this->post_count = count( $this->posts ); 
  155.  
  156. // Load topic tags. 
  157. $this->topic_tags = bb_get_topic_tags( $this->topic_id ); 
  158.  
  159. $this->pag = new stdClass; 
  160.  
  161. if ( (int) $this->total_post_count && (int) $this->pag_num ) { 
  162. $this->pag_links = paginate_links( array( 
  163. 'base' => add_query_arg( array( 'topic_page' => '%#%', 'num' => (int) $this->pag_num ) ),  
  164. 'format' => '',  
  165. 'total' => ceil( (int) $this->total_post_count / (int) $this->pag_num ),  
  166. 'current' => $this->pag_page,  
  167. 'prev_text' => _x( '←', 'Forum thread pagination previous text', 'buddypress' ),  
  168. 'next_text' => _x( '→', 'Forum thread pagination next text', 'buddypress' ),  
  169. 'mid_size' => 1,  
  170. 'add_args' => array(),  
  171. ) ); 
  172.  
  173. $this->pag->total_pages = ceil( (int) $this->total_post_count / (int) $this->pag_num ); 
  174. } else { 
  175. $this->pag->total_pages = 1; 
  176.  
  177. /** 
  178. * Whether there are posts available in the loop. 
  179. * @since 1.0.0 
  180. * @see bp_has_forum_topic_posts() 
  181. * @return bool True if there are items in the loop, otherwise false. 
  182. */ 
  183. function has_posts() { 
  184. if ( $this->post_count ) 
  185. return true; 
  186.  
  187. return false; 
  188.  
  189. /** 
  190. * Set up the next post and iterate index. 
  191. * @since 1.0.0 
  192. * @return object The next post to iterate over. 
  193. */ 
  194. function next_post() { 
  195. $this->current_post++; 
  196. $this->post = $this->posts[$this->current_post]; 
  197.  
  198. return $this->post; 
  199.  
  200. /** 
  201. * Rewind the posts and reset post index. 
  202. * @since 1.0.0 
  203. */ 
  204. function rewind_posts() { 
  205. $this->current_post = -1; 
  206. if ( $this->post_count > 0 ) { 
  207. $this->post = $this->posts[0]; 
  208.  
  209. /** 
  210. * Whether there are posts left in the loop to iterate over. 
  211. * This method is used by {@link bp_forum_topic_posts()} as part of 
  212. * the while loop that controls iteration inside the blogs loop, eg: 
  213. * while ( bp_forum_topic_posts() ) { ... 
  214. * @since 1.0.0 
  215. * @see bp_forum_topic_posts() 
  216. * @return bool True if there are more posts to show, otherwise false. 
  217. */ 
  218. function user_posts() { 
  219. if ( $this->current_post + 1 < $this->post_count ) { 
  220. return true; 
  221. } elseif ( $this->current_post + 1 == $this->post_count ) { 
  222.  
  223. /** 
  224. * Fires right before the rewinding of user posts. 
  225. * @since 1.5.0 
  226. */ 
  227. do_action('topic_loop_end'); 
  228. // Do some cleaning up after the loop. 
  229. $this->rewind_posts(); 
  230.  
  231. $this->in_the_loop = false; 
  232. return false; 
  233.  
  234. /** 
  235. * Set up the current topic in the loop. 
  236. * @since 1.0.0 
  237. * @see bp_the_forum_topic_post() 
  238. */ 
  239. function the_post() { 
  240.  
  241. $this->in_the_loop = true; 
  242. $this->post = $this->next_post(); 
  243. $this->post = (object)$this->post; 
  244.  
  245. if ( $this->current_post == 0 ) { 
  246.  
  247. /** 
  248. * Fires if the current post item is the first in the topic loop. 
  249. * @since 1.5.0 
  250. */ 
  251. do_action( 'topic_loop_start' );