BP_Messages_Box_Template

Message Box Template Class.

Defined (1)

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

/bp-messages/classes/class-bp-messages-box-template.php  
  1. class BP_Messages_Box_Template { 
  2.  
  3. /** 
  4. * The loop iterator. 
  5. * @var int 
  6. */ 
  7. public $current_thread = -1; 
  8.  
  9. /** 
  10. * The number of threads returned by the paged query. 
  11. * @var int 
  12. */ 
  13. public $current_thread_count = 0; 
  14.  
  15. /** 
  16. * Total number of threads matching the query params. 
  17. * @var int 
  18. */ 
  19. public $total_thread_count = 0; 
  20.  
  21. /** 
  22. * Array of threads located by the query. 
  23. * @var array 
  24. */ 
  25. public $threads = array(); 
  26.  
  27. /** 
  28. * The thread object currently being iterated on. 
  29. * @var object 
  30. */ 
  31. public $thread = false; 
  32.  
  33. /** 
  34. * A flag for whether the loop is currently being iterated. 
  35. * @var bool 
  36. */ 
  37. public $in_the_loop = false; 
  38.  
  39. /** 
  40. * User ID of the current inbox. 
  41. * @var int 
  42. */ 
  43. public $user_id = 0; 
  44.  
  45. /** 
  46. * The current "box" view ('notices', 'sentbox', 'inbox'). 
  47. * @var string 
  48. */ 
  49. public $box = 'inbox'; 
  50.  
  51. /** 
  52. * The page number being requested. 
  53. * @var int 
  54. */ 
  55. public $pag_page = 1; 
  56.  
  57. /** 
  58. * The number of items being requested per page. 
  59. * @var int 
  60. */ 
  61. public $pag_num = 10; 
  62.  
  63. /** 
  64. * An HTML string containing pagination links. 
  65. * @var string 
  66. */ 
  67. public $pag_links = ''; 
  68.  
  69. /** 
  70. * Search terms for limiting the thread query. 
  71. * @var string 
  72. */ 
  73. public $search_terms = ''; 
  74.  
  75. /** 
  76. * Constructor method. 
  77. * @param array $args { 
  78. * Array of arguments. See bp_has_message_threads() for full description. 
  79. * } 
  80. */ 
  81. public function __construct( $args = array() ) { 
  82.  
  83. // Backward compatibility with old method of passing arguments. 
  84. if ( ! is_array( $args ) || func_num_args() > 1 ) { 
  85. _deprecated_argument( __METHOD__, '2.2.0', sprintf( __( 'Arguments passed to %1$s should be in an associative array. See the inline documentation at %2$s for more details.', 'buddypress' ), __METHOD__, __FILE__ ) ); 
  86.  
  87. $old_args_keys = array( 
  88. 0 => 'user_id',  
  89. 1 => 'box',  
  90. 2 => 'per_page',  
  91. 3 => 'max',  
  92. 4 => 'type',  
  93. 5 => 'search_terms',  
  94. 6 => 'page_arg' 
  95. ); 
  96.  
  97. $args = bp_core_parse_args_array( $old_args_keys, func_get_args() ); 
  98.  
  99. $r = wp_parse_args( $args, array( 
  100. 'page' => 1,  
  101. 'per_page' => 10,  
  102. 'page_arg' => 'mpage',  
  103. 'box' => 'inbox',  
  104. 'type' => 'all',  
  105. 'user_id' => bp_loggedin_user_id(),  
  106. 'max' => false,  
  107. 'search_terms' => '',  
  108. 'meta_query' => array(),  
  109. ) ); 
  110.  
  111. $this->pag_arg = sanitize_key( $r['page_arg'] ); 
  112. $this->pag_page = bp_sanitize_pagination_arg( $this->pag_arg, $r['page'] ); 
  113. $this->pag_num = bp_sanitize_pagination_arg( 'num', $r['per_page'] ); 
  114. $this->user_id = $r['user_id']; 
  115. $this->box = $r['box']; 
  116. $this->type = $r['type']; 
  117. $this->search_terms = $r['search_terms']; 
  118.  
  119. if ( 'notices' === $this->box ) { 
  120. $this->threads = BP_Messages_Notice::get_notices( array( 
  121. 'pag_num' => $this->pag_num,  
  122. 'pag_page' => $this->pag_page 
  123. ) ); 
  124. } else { 
  125. $threads = BP_Messages_Thread::get_current_threads_for_user( array( 
  126. 'user_id' => $this->user_id,  
  127. 'box' => $this->box,  
  128. 'type' => $this->type,  
  129. 'limit' => $this->pag_num,  
  130. 'page' => $this->pag_page,  
  131. 'search_terms' => $this->search_terms,  
  132. 'meta_query' => $r['meta_query'],  
  133. ) ); 
  134.  
  135. $this->threads = $threads['threads']; 
  136. $this->total_thread_count = $threads['total']; 
  137.  
  138. if ( !$this->threads ) { 
  139. $this->thread_count = 0; 
  140. $this->total_thread_count = 0; 
  141. } else { 
  142. $total_notice_count = BP_Messages_Notice::get_total_notice_count(); 
  143.  
  144. if ( empty( $r['max'] ) || ( (int) $r['max'] >= (int) $total_notice_count ) ) { 
  145. if ( 'notices' === $this->box ) { 
  146. $this->total_thread_count = (int) $total_notice_count; 
  147. } else { 
  148. $this->total_thread_count = (int) $r['max']; 
  149.  
  150. if ( ! empty( $r['max'] ) ) { 
  151. if ( (int) $r['max'] >= count( $this->threads ) ) { 
  152. $this->thread_count = count( $this->threads ); 
  153. } else { 
  154. $this->thread_count = (int) $r['max']; 
  155. } else { 
  156. $this->thread_count = count( $this->threads ); 
  157.  
  158. if ( (int) $this->total_thread_count && (int) $this->pag_num ) { 
  159. $pag_args = array( 
  160. $r['page_arg'] => '%#%',  
  161. ); 
  162.  
  163. if ( defined( 'DOING_AJAX' ) && true === (bool) DOING_AJAX ) { 
  164. $base = remove_query_arg( 's', wp_get_referer() ); 
  165. } else { 
  166. $base = ''; 
  167.  
  168. $add_args = array(); 
  169.  
  170. if ( ! empty( $this->search_terms ) ) { 
  171. $add_args['s'] = $this->search_terms; 
  172.  
  173. $this->pag_links = paginate_links( array( 
  174. 'base' => add_query_arg( $pag_args, $base ),  
  175. 'format' => '',  
  176. 'total' => ceil( (int) $this->total_thread_count / (int) $this->pag_num ),  
  177. 'current' => $this->pag_page,  
  178. 'prev_text' => _x( '←', 'Message pagination previous text', 'buddypress' ),  
  179. 'next_text' => _x( '→', 'Message pagination next text', 'buddypress' ),  
  180. 'mid_size' => 1,  
  181. 'add_args' => $add_args,  
  182. ) ); 
  183.  
  184. /** 
  185. * Whether there are threads available in the loop. 
  186. * @see bp_has_message_threads() 
  187. * @return bool True if there are items in the loop, otherwise false. 
  188. */ 
  189. public function has_threads() { 
  190. if ( $this->thread_count ) { 
  191. return true; 
  192.  
  193. return false; 
  194.  
  195. /** 
  196. * Set up the next member and iterate index. 
  197. * @return object The next member to iterate over. 
  198. */ 
  199. public function next_thread() { 
  200. $this->current_thread++; 
  201. $this->thread = $this->threads[$this->current_thread]; 
  202.  
  203. return $this->thread; 
  204.  
  205. /** 
  206. * Rewind the threads and reset thread index. 
  207. */ 
  208. public function rewind_threads() { 
  209. $this->current_thread = -1; 
  210. if ( $this->thread_count > 0 ) { 
  211. $this->thread = $this->threads[0]; 
  212.  
  213. /** 
  214. * Whether there are threads left in the loop to iterate over. 
  215. * This method is used by {@link bp_message_threads()} as part of the 
  216. * while loop that controls iteration inside the threads loop, eg: 
  217. * while ( bp_message_threads() ) { ... 
  218. * @see bp_message_threads() 
  219. * @return bool True if there are more threads to show, otherwise false. 
  220. */ 
  221. function message_threads() { 
  222. if ( $this->current_thread + 1 < $this->thread_count ) { 
  223. return true; 
  224. } elseif ( $this->current_thread + 1 == $this->thread_count ) { 
  225.  
  226. /** 
  227. * Fires when at the end of threads to iterate over. 
  228. * @since 1.5.0 
  229. */ 
  230. do_action( 'messages_box_loop_end' ); 
  231. // Do some cleaning up after the loop. 
  232. $this->rewind_threads(); 
  233.  
  234. $this->in_the_loop = false; 
  235. return false; 
  236.  
  237. /** 
  238. * Set up the current thread inside the loop. 
  239. * Used by {@link bp_message_thread()} to set up the current thread data 
  240. * while looping, so that template tags used during that iteration make 
  241. * reference to the current thread. 
  242. * @see bp_message_thread() 
  243. */ 
  244. public function the_message_thread() { 
  245.  
  246. $this->in_the_loop = true; 
  247. $this->thread = $this->next_thread(); 
  248.  
  249. if ( ! bp_is_current_action( 'notices' ) ) { 
  250. $last_message_index = count( $this->thread->messages ) - 1; 
  251. $this->thread->messages = array_reverse( (array) $this->thread->messages ); 
  252.  
  253. // Set up the last message data. 
  254. if ( count($this->thread->messages) > 1 ) { 
  255. if ( 'inbox' == $this->box ) { 
  256. foreach ( (array) $this->thread->messages as $key => $message ) { 
  257. if ( bp_loggedin_user_id() != $message->sender_id ) { 
  258. $last_message_index = $key; 
  259. break; 
  260.  
  261. } elseif ( 'sentbox' == $this->box ) { 
  262. foreach ( (array) $this->thread->messages as $key => $message ) { 
  263. if ( bp_loggedin_user_id() == $message->sender_id ) { 
  264. $last_message_index = $key; 
  265. break; 
  266.  
  267. $this->thread->last_message_id = $this->thread->messages[ $last_message_index ]->id; 
  268. $this->thread->last_message_date = $this->thread->messages[ $last_message_index ]->date_sent; 
  269. $this->thread->last_sender_id = $this->thread->messages[ $last_message_index ]->sender_id; 
  270. $this->thread->last_message_subject = $this->thread->messages[ $last_message_index ]->subject; 
  271. $this->thread->last_message_content = $this->thread->messages[ $last_message_index ]->message; 
  272.  
  273. // Loop has just started. 
  274. if ( 0 == $this->current_thread ) { 
  275.  
  276. /** 
  277. * Fires if at the start of the message thread loop. 
  278. * @since 1.5.0 
  279. */ 
  280. do_action( 'messages_box_loop_start' );