/bp-messages/classes/class-bp-messages-thread-template.php

  1. <?php 
  2. /** 
  3. * BuddyPress Messages Thread Template Class. 
  4. * 
  5. * @package BuddyPress 
  6. * @subpackage MessagesTemplate 
  7. * @since 1.5.0 
  8. */ 
  9.  
  10. // Exit if accessed directly. 
  11. defined( 'ABSPATH' ) || exit; 
  12.  
  13. /** 
  14. * Message Thread Template Class 
  15. */ 
  16. class BP_Messages_Thread_Template { 
  17.  
  18. /** 
  19. * The loop iterator. 
  20. * 
  21. * @var int 
  22. */ 
  23. public $current_message = -1; 
  24.  
  25. /** 
  26. * Number of messages returned by the paged query. 
  27. * 
  28. * @var int 
  29. */ 
  30. public $message_count = 0; 
  31.  
  32. /** 
  33. * The message object currently being iterated on. 
  34. * 
  35. * @var object 
  36. */ 
  37. public $message; 
  38.  
  39. /** 
  40. * Thread that the current messages belong to. 
  41. * 
  42. * @var BP_Messages_Thread 
  43. */ 
  44. public $thread; 
  45.  
  46. /** 
  47. * A flag for whether the loop is currently being iterated. 
  48. * 
  49. * @var bool 
  50. */ 
  51. public $in_the_loop = false; 
  52.  
  53. /** 
  54. * The page number being requested. 
  55. * 
  56. * @var int 
  57. */ 
  58. public $pag_page = 1; 
  59.  
  60. /** 
  61. * The number of items being requested per page. 
  62. * 
  63. * @var int 
  64. */ 
  65. public $pag_num = 10; 
  66.  
  67. /** 
  68. * An HTML string containing pagination links. 
  69. * 
  70. * @var string 
  71. */ 
  72. public $pag_links = ''; 
  73.  
  74. /** 
  75. * The total number of messages matching the query. 
  76. * 
  77. * @var int 
  78. */ 
  79. public $total_message_count = 0; 
  80.  
  81. /** 
  82. * Constructor method. 
  83. * 
  84. * @see BP_Messages_Thread::populate() for full parameter info. 
  85. * 
  86. * @param int $thread_id ID of the message thread to display. 
  87. * @param string $order Order to show the thread's messages in. 
  88. * @param array $args Array of arguments for the query. 
  89. */ 
  90. public function __construct( $thread_id = 0, $order = 'ASC', $args = array() ) { 
  91. $this->thread = new BP_Messages_Thread( $thread_id, $order, $args ); 
  92. $this->message_count = count( $this->thread->messages ); 
  93.  
  94. /** 
  95. * Whether there are messages available in the loop. 
  96. * 
  97. * @see bp_thread_has_messages() 
  98. * 
  99. * @return bool True if there are items in the loop, otherwise false. 
  100. */ 
  101. public function has_messages() { 
  102. if ( ! empty( $this->message_count ) ) { 
  103. return true; 
  104.  
  105. return false; 
  106.  
  107. /** 
  108. * Set up the next member and iterate index. 
  109. * 
  110. * @return object The next member to iterate over. 
  111. */ 
  112. public function next_message() { 
  113. $this->current_message++; 
  114. $this->message = $this->thread->messages[ $this->current_message ]; 
  115.  
  116. return $this->message; 
  117.  
  118. /** 
  119. * Rewind the messages and reset message index. 
  120. */ 
  121. public function rewind_messages() { 
  122. $this->current_message = -1; 
  123. if ( $this->message_count > 0 ) { 
  124. $this->message = $this->thread->messages[0]; 
  125.  
  126. /** 
  127. * Whether there are messages left in the loop to iterate over. 
  128. * 
  129. * This method is used by {@link bp_thread_messages()} as part of the 
  130. * while loop that controls iteration inside the messages loop, eg: 
  131. * while ( bp_thread_messages() ) { ... 
  132. * 
  133. * @see bp_thread_messages() 
  134. * 
  135. * @return bool True if there are more messages to show, otherwise false. 
  136. */ 
  137. public function messages() { 
  138. if ( ( $this->current_message + 1 ) < $this->message_count ) { 
  139. return true; 
  140. } elseif ( ( $this->current_message + 1 ) === $this->message_count ) { 
  141.  
  142. /** 
  143. * Fires when at the end of messages to iterate over. 
  144. * 
  145. * @since 1.1.0 
  146. */ 
  147. do_action( 'thread_loop_end' ); 
  148. // Do some cleaning up after the loop. 
  149. $this->rewind_messages(); 
  150.  
  151. $this->in_the_loop = false; 
  152. return false; 
  153.  
  154. /** 
  155. * Set up the current message inside the loop. 
  156. * 
  157. * Used by {@link bp_thread_the_message()} to set up the current 
  158. * message data while looping, so that template tags used during 
  159. * that iteration make reference to the current message. 
  160. * 
  161. * @see bp_thread_the_message() 
  162. */ 
  163. public function the_message() { 
  164. $this->in_the_loop = true; 
  165. $this->message = $this->next_message(); 
  166.  
  167. // Loop has just started. 
  168. if ( 0 === $this->current_message ) { 
  169.  
  170. /** 
  171. * Fires if at the start of the message loop. 
  172. * 
  173. * @since 1.1.0 
  174. */ 
  175. do_action( 'thread_loop_start' ); 
.