/bp-notifications/classes/class-bp-notifications-template.php

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