BP_Notifications_Template

The main notifications template loop class.

Defined (1)

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

/bp-notifications/classes/class-bp-notifications-template.php  
  1. class BP_Notifications_Template { 
  2.  
  3. /** 
  4. * The loop iterator. 
  5. * @since 1.9.0 
  6. * @var int 
  7. */ 
  8. public $current_notification = -1; 
  9.  
  10. /** 
  11. * The number of notifications returned by the paged query. 
  12. * @since 1.9.0 
  13. * @var int 
  14. */ 
  15. public $current_notification_count; 
  16.  
  17. /** 
  18. * Total number of notifications matching the query. 
  19. * @since 1.9.0 
  20. * @var int 
  21. */ 
  22. public $total_notification_count; 
  23.  
  24. /** 
  25. * Array of notifications located by the query. 
  26. * @since 1.9.0 
  27. * @var array 
  28. */ 
  29. public $notifications; 
  30.  
  31. /** 
  32. * The notification object currently being iterated on. 
  33. * @since 1.9.0 
  34. * @var object 
  35. */ 
  36. public $notification; 
  37.  
  38. /** 
  39. * A flag for whether the loop is currently being iterated. 
  40. * @since 1.9.0 
  41. * @var bool 
  42. */ 
  43. public $in_the_loop; 
  44.  
  45. /** 
  46. * The ID of the user to whom the displayed notifications belong. 
  47. * @since 1.9.0 
  48. * @var int 
  49. */ 
  50. public $user_id; 
  51.  
  52. /** 
  53. * The page number being requested. 
  54. * @since 1.9.0 
  55. * @var int 
  56. */ 
  57. public $pag_page; 
  58.  
  59. /** 
  60. * The $_GET argument used in URLs for determining pagination. 
  61. * @since 1.9.0 
  62. * @var int 
  63. */ 
  64. public $pag_arg; 
  65.  
  66. /** 
  67. * The number of items to display per page of results. 
  68. * @since 1.9.0 
  69. * @var int 
  70. */ 
  71. public $pag_num; 
  72.  
  73. /** 
  74. * An HTML string containing pagination links. 
  75. * @since 1.9.0 
  76. * @var string 
  77. */ 
  78. public $pag_links; 
  79.  
  80. /** 
  81. * A string to match against. 
  82. * @since 1.9.0 
  83. * @var string 
  84. */ 
  85. public $search_terms; 
  86.  
  87. /** 
  88. * A database column to order the results by. 
  89. * @since 1.9.0 
  90. * @var string 
  91. */ 
  92. public $order_by; 
  93.  
  94. /** 
  95. * The direction to sort the results (ASC or DESC). 
  96. * @since 1.9.0 
  97. * @var string 
  98. */ 
  99. public $sort_order; 
  100.  
  101. /** 
  102. * Array of variables used in this notification query. 
  103. * @since 2.2.2 
  104. * @var array 
  105. */ 
  106. public $query_vars; 
  107.  
  108. /** 
  109. * Constructor method. 
  110. * @see bp_has_notifications() For information on the array format. 
  111. * @since 1.9.0 
  112. * @param array $args { 
  113. * An array of arguments. See {@link bp_has_notifications()} 
  114. * for more details. 
  115. * } 
  116. */ 
  117. public function __construct( $args = array() ) { 
  118.  
  119. // Parse arguments. 
  120. $r = wp_parse_args( $args, array( 
  121. 'id' => false,  
  122. 'user_id' => 0,  
  123. 'item_id' => false,  
  124. 'secondary_item_id' => false,  
  125. 'component_name' => bp_notifications_get_registered_components(),  
  126. 'component_action' => false,  
  127. 'is_new' => true,  
  128. 'search_terms' => '',  
  129. 'order_by' => 'date_notified',  
  130. 'sort_order' => 'DESC',  
  131. 'page_arg' => 'npage',  
  132. 'page' => 1,  
  133. 'per_page' => 25,  
  134. 'max' => null,  
  135. 'meta_query' => false,  
  136. 'date_query' => false 
  137. ) ); 
  138.  
  139. // Sort order direction. 
  140. $orders = array( 'ASC', 'DESC' ); 
  141. if ( ! empty( $_GET['sort_order'] ) && in_array( $_GET['sort_order'], $orders ) ) { 
  142. $r['sort_order'] = $_GET['sort_order']; 
  143. } else { 
  144. $r['sort_order'] = in_array( $r['sort_order'], $orders ) ? $r['sort_order'] : 'DESC'; 
  145.  
  146. // Setup variables. 
  147. $this->pag_arg = sanitize_key( $r['page_arg'] ); 
  148. $this->pag_page = bp_sanitize_pagination_arg( $this->pag_arg, $r['page'] ); 
  149. $this->pag_num = bp_sanitize_pagination_arg( 'num', $r['per_page'] ); 
  150. $this->user_id = $r['user_id']; 
  151. $this->is_new = $r['is_new']; 
  152. $this->search_terms = $r['search_terms']; 
  153. $this->order_by = $r['order_by']; 
  154. $this->sort_order = $r['sort_order']; 
  155. $this->query_vars = array( 
  156. 'id' => $r['id'],  
  157. 'user_id' => $this->user_id,  
  158. 'item_id' => $r['item_id'],  
  159. 'secondary_item_id' => $r['secondary_item_id'],  
  160. 'component_name' => $r['component_name'],  
  161. 'component_action' => $r['component_action'],  
  162. 'meta_query' => $r['meta_query'],  
  163. 'date_query' => $r['date_query'],  
  164. 'is_new' => $this->is_new,  
  165. 'search_terms' => $this->search_terms,  
  166. 'order_by' => $this->order_by,  
  167. 'sort_order' => $this->sort_order,  
  168. 'page' => $this->pag_page,  
  169. 'per_page' => $this->pag_num,  
  170. ); 
  171.  
  172. // Setup the notifications to loop through. 
  173. $this->notifications = BP_Notifications_Notification::get( $this->query_vars ); 
  174. $this->total_notification_count = BP_Notifications_Notification::get_total_count( $this->query_vars ); 
  175.  
  176. if ( empty( $this->notifications ) ) { 
  177. $this->notification_count = 0; 
  178. $this->total_notification_count = 0; 
  179.  
  180. } else { 
  181. if ( ! empty( $r['max'] ) ) { 
  182. if ( $r['max'] >= count( $this->notifications ) ) { 
  183. $this->notification_count = count( $this->notifications ); 
  184. } else { 
  185. $this->notification_count = (int) $r['max']; 
  186. } else { 
  187. $this->notification_count = count( $this->notifications ); 
  188.  
  189. if ( (int) $this->total_notification_count && (int) $this->pag_num ) { 
  190. $add_args = array( 
  191. 'sort_order' => $this->sort_order,  
  192. ); 
  193.  
  194. $this->pag_links = paginate_links( array( 
  195. 'base' => add_query_arg( $this->pag_arg, '%#%' ),  
  196. 'format' => '',  
  197. 'total' => ceil( (int) $this->total_notification_count / (int) $this->pag_num ),  
  198. 'current' => $this->pag_page,  
  199. 'prev_text' => _x( '←', 'Notifications pagination previous text', 'buddypress' ),  
  200. 'next_text' => _x( '→', 'Notifications pagination next text', 'buddypress' ),  
  201. 'mid_size' => 1,  
  202. 'add_args' => $add_args,  
  203. ) ); 
  204.  
  205. /** 
  206. * Whether there are notifications available in the loop. 
  207. * @since 1.9.0 
  208. * @see bp_has_notifications() 
  209. * @return bool True if there are items in the loop, otherwise false. 
  210. */ 
  211. public function has_notifications() { 
  212. if ( $this->notification_count ) { 
  213. return true; 
  214.  
  215. return false; 
  216.  
  217. /** 
  218. * Set up the next notification and iterate index. 
  219. * @since 1.9.0 
  220. * @return object The next notification to iterate over. 
  221. */ 
  222. public function next_notification() { 
  223.  
  224. $this->current_notification++; 
  225.  
  226. $this->notification = $this->notifications[ $this->current_notification ]; 
  227.  
  228. return $this->notification; 
  229.  
  230. /** 
  231. * Rewind the blogs and reset blog index. 
  232. * @since 1.9.0 
  233. */ 
  234. public function rewind_notifications() { 
  235.  
  236. $this->current_notification = -1; 
  237.  
  238. if ( $this->notification_count > 0 ) { 
  239. $this->notification = $this->notifications[0]; 
  240.  
  241. /** 
  242. * Whether there are notifications left in the loop to iterate over. 
  243. * This method is used by {@link bp_notifications()} as part of the 
  244. * while loop that controls iteration inside the notifications loop, eg: 
  245. * while ( bp_notifications() ) { ... 
  246. * @since 1.9.0 
  247. * @see bp_notifications() 
  248. * @return bool True if there are more notifications to show,  
  249. * otherwise false. 
  250. */ 
  251. public function notifications() { 
  252.  
  253. if ( $this->current_notification + 1 < $this->notification_count ) { 
  254. return true; 
  255.  
  256. } elseif ( $this->current_notification + 1 == $this->notification_count ) { 
  257.  
  258. /** 
  259. * Fires right before the rewinding of notification posts. 
  260. * @since 1.9.0 
  261. */ 
  262. do_action( 'notifications_loop_end'); 
  263.  
  264. $this->rewind_notifications(); 
  265.  
  266. $this->in_the_loop = false; 
  267. return false; 
  268.  
  269. /** 
  270. * Set up the current notification inside the loop. 
  271. * Used by {@link bp_the_notification()} to set up the current 
  272. * notification data while looping, so that template tags used during 
  273. * that iteration make reference to the current notification. 
  274. * @since 1.9.0 
  275. * @see bp_the_notification() 
  276. */ 
  277. public function the_notification() { 
  278. $this->in_the_loop = true; 
  279. $this->notification = $this->next_notification(); 
  280.  
  281. // Loop has just started. 
  282. if ( 0 === $this->current_notification ) { 
  283.  
  284. /** 
  285. * Fires if the current notification item is the first in the notification loop. 
  286. * @since 1.9.0 
  287. */ 
  288. do_action( 'notifications_loop_start' );