/bp-notifications/bp-notifications-template.php

  1. <?php 
  2. /** 
  3. * BuddyPress Notifications Template Functions. 
  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. * Output the notifications component slug. 
  15. * 
  16. * @since 1.9.0 
  17. */ 
  18. function bp_notifications_slug() { 
  19. echo bp_get_notifications_slug(); 
  20. /** 
  21. * Return the notifications component slug. 
  22. * 
  23. * @since 1.9.0 
  24. * 
  25. * @return string Slug of the Notifications component. 
  26. */ 
  27. function bp_get_notifications_slug() { 
  28.  
  29. /** 
  30. * Filters the notifications component slug. 
  31. * 
  32. * @since 1.9.0 
  33. * 
  34. * @param string $slug Notifications component slug. 
  35. */ 
  36. return apply_filters( 'bp_get_notifications_slug', buddypress()->notifications->slug ); 
  37.  
  38. /** 
  39. * Output the notifications permalink for a user. 
  40. * 
  41. * @since 1.9.0 
  42. * @since 2.6.0 Added $user_id as a parameter. 
  43. * 
  44. * @param int $user_id The user ID. 
  45. */ 
  46. function bp_notifications_permalink( $user_id = 0 ) { 
  47. echo bp_get_notifications_permalink( $user_id ); 
  48. /** 
  49. * Return the notifications permalink. 
  50. * 
  51. * @since 1.9.0 
  52. * @since 2.6.0 Added $user_id as a parameter. 
  53. * 
  54. * @param int $user_id The user ID. 
  55. * @return string Notifications permalink. 
  56. */ 
  57. function bp_get_notifications_permalink( $user_id = 0 ) { 
  58. if ( 0 === $user_id ) { 
  59. $user_id = bp_loggedin_user_id(); 
  60. $domain = bp_loggedin_user_domain(); 
  61. } else { 
  62. $domain = bp_core_get_user_domain( (int) $user_id ); 
  63.  
  64. $retval = trailingslashit( $domain . bp_get_notifications_slug() ); 
  65.  
  66. /** 
  67. * Filters the notifications permalink. 
  68. * 
  69. * @since 1.9.0 
  70. * @since 2.6.0 Added $user_id as a parameter. 
  71. * 
  72. * @param string $retval Permalink for the notifications. 
  73. * @param int $user_id The user ID. 
  74. */ 
  75. return apply_filters( 'bp_get_notifications_permalink', $retval, $user_id ); 
  76.  
  77. /** 
  78. * Output the unread notifications permalink for a user. 
  79. * 
  80. * @since 1.9.0 
  81. * @since 2.6.0 Added $user_id as a parameter. 
  82. * 
  83. * @param int $user_id The user ID. 
  84. */ 
  85. function bp_notifications_unread_permalink( $user_id = 0 ) { 
  86. echo bp_get_notifications_unread_permalink( $user_id ); 
  87. /** 
  88. * Return the unread notifications permalink. 
  89. * 
  90. * @since 2.6.0 Added $user_id as a parameter. 
  91. * 
  92. * @param int $user_id The user ID. 
  93. * @return string Unread notifications permalink. 
  94. */ 
  95. function bp_get_notifications_unread_permalink( $user_id = 0 ) { 
  96. if ( 0 === $user_id ) { 
  97. $user_id = bp_loggedin_user_id(); 
  98. $domain = bp_loggedin_user_domain(); 
  99. } else { 
  100. $domain = bp_core_get_user_domain( (int) $user_id ); 
  101.  
  102. $retval = trailingslashit( $domain . bp_get_notifications_slug() . '/unread' ); 
  103.  
  104. /** 
  105. * Filters the unread notifications permalink. 
  106. * 
  107. * @since 1.9.0 
  108. * @since 2.6.0 Added $user_id as a parameter. 
  109. * 
  110. * @param string $retval Permalink for the unread notifications. 
  111. * @param int $user_id The user ID. 
  112. */ 
  113. return apply_filters( 'bp_get_notifications_unread_permalink', $retval, $user_id ); 
  114.  
  115. /** 
  116. * Output the read notifications permalink for a user. 
  117. * 
  118. * @since 1.9.0 
  119. * @since 2.6.0 Added $user_id as a parameter. 
  120. * 
  121. * @param int $user_id The user ID. 
  122. */ 
  123. function bp_notifications_read_permalink( $user_id = 0 ) { 
  124. echo bp_get_notifications_read_permalink( $user_id ); 
  125. /** 
  126. * Return the read notifications permalink. 
  127. * 
  128. * @since 1.9.0 
  129. * 
  130. * @return string Read notifications permalink. 
  131. */ 
  132. function bp_get_notifications_read_permalink( $user_id = 0 ) { 
  133. if ( 0 === $user_id ) { 
  134. $user_id = bp_loggedin_user_id(); 
  135. $domain = bp_loggedin_user_domain(); 
  136. } else { 
  137. $domain = bp_core_get_user_domain( (int) $user_id ); 
  138.  
  139. $retval = trailingslashit( $domain . bp_get_notifications_slug() . '/read' ); 
  140.  
  141. /** 
  142. * Filters the read notifications permalink. 
  143. * 
  144. * @since 1.9.0 
  145. * @since 2.6.0 Added $user_id as a parameter. 
  146. * 
  147. * @param string $retval Permalink for the read notifications. 
  148. * @param int $user_id The user ID. 
  149. */ 
  150. return apply_filters( 'bp_get_notifications_unread_permalink', $retval, $user_id ); 
  151.  
  152. /** The Loop ******************************************************************/ 
  153.  
  154. /** 
  155. * Initialize the notifications loop. 
  156. * 
  157. * Based on the $args passed, bp_has_notifications() populates 
  158. * buddypress()->notifications->query_loop global, enabling the use of BP 
  159. * templates and template functions to display a list of notifications. 
  160. * 
  161. * @since 1.9.0 
  162. * 
  163. * @param array|string $args { 
  164. * Arguments for limiting the contents of the notifications loop. Can be 
  165. * passed as an associative array, or as a URL query string. 
  166. * 
  167. * See {@link BP_Notifications_Notification::get()} for detailed 
  168. * information on the arguments. In addition, also supports: 
  169. * 
  170. * @type int $max Optional. Max items to display. Default: false. 
  171. * @type string $page_arg URL argument to use for pagination. 
  172. * Default: 'npage'. 
  173. * } 
  174. * @return bool 
  175. */ 
  176. function bp_has_notifications( $args = '' ) { 
  177.  
  178. // Get the default is_new argument. 
  179. if ( bp_is_current_action( 'unread' ) ) { 
  180. $is_new = 1; 
  181. } elseif ( bp_is_current_action( 'read' ) ) { 
  182. $is_new = 0; 
  183.  
  184. // Not on a notifications page? default to fetch new notifications. 
  185. } else { 
  186. $is_new = 1; 
  187.  
  188. // Get the user ID. 
  189. if ( bp_displayed_user_id() ) { 
  190. $user_id = bp_displayed_user_id(); 
  191. } else { 
  192. $user_id = bp_loggedin_user_id(); 
  193.  
  194. // Set the component action (by default false to get all actions) 
  195. $component_action = false; 
  196.  
  197. if ( isset( $_REQUEST['type'] ) ) { 
  198. $component_action = sanitize_key( $_REQUEST['type'] ); 
  199.  
  200. // Set the search terms (by default an empty string to get all notifications) 
  201. $search_terms = ''; 
  202.  
  203. if ( isset( $_REQUEST['s'] ) ) { 
  204. $search_terms = stripslashes( $_REQUEST['s'] ); 
  205.  
  206. // Parse the args. 
  207. $r = bp_parse_args( $args, array( 
  208. 'id' => false,  
  209. 'user_id' => $user_id,  
  210. 'secondary_item_id' => false,  
  211. 'component_name' => bp_notifications_get_registered_components(),  
  212. 'component_action' => $component_action,  
  213. 'is_new' => $is_new,  
  214. 'search_terms' => $search_terms,  
  215. 'order_by' => 'date_notified',  
  216. 'sort_order' => 'DESC',  
  217. 'meta_query' => false,  
  218. 'date_query' => false,  
  219. 'page' => 1,  
  220. 'per_page' => 25,  
  221.  
  222. // These are additional arguments that are not available in 
  223. // BP_Notifications_Notification::get(). 
  224. 'max' => false,  
  225. 'page_arg' => 'npage',  
  226. ), 'has_notifications' ); 
  227.  
  228. // Get the notifications. 
  229. $query_loop = new BP_Notifications_Template( $r ); 
  230.  
  231. // Setup the global query loop. 
  232. buddypress()->notifications->query_loop = $query_loop; 
  233.  
  234. /** 
  235. * Filters whether or not the user has notifications to display. 
  236. * 
  237. * @since 1.9.0 
  238. * @since 2.6.0 Added the `$r` parameter. 
  239. * 
  240. * @param bool $value Whether or not there are notifications to display. 
  241. * @param BP_Notifications_Template $query_loop BP_Notifications_Template object instance. 
  242. * @param array $r Array of arguments passed into the BP_Notifications_Template class. 
  243. */ 
  244. return apply_filters( 'bp_has_notifications', $query_loop->has_notifications(), $query_loop, $r ); 
  245.  
  246. /** 
  247. * Get the notifications returned by the template loop. 
  248. * 
  249. * @since 1.9.0 
  250. * 
  251. * @return array List of notifications. 
  252. */ 
  253. function bp_the_notifications() { 
  254. return buddypress()->notifications->query_loop->notifications(); 
  255.  
  256. /** 
  257. * Get the current notification object in the loop. 
  258. * 
  259. * @since 1.9.0 
  260. * 
  261. * @return object The current notification within the loop. 
  262. */ 
  263. function bp_the_notification() { 
  264. return buddypress()->notifications->query_loop->the_notification(); 
  265.  
  266. /** Loop Output ***************************************************************/ 
  267.  
  268. /** 
  269. * Output the ID of the notification currently being iterated on. 
  270. * 
  271. * @since 1.9.0 
  272. */ 
  273. function bp_the_notification_id() { 
  274. echo bp_get_the_notification_id(); 
  275. /** 
  276. * Return the ID of the notification currently being iterated on. 
  277. * 
  278. * @since 1.9.0 
  279. * 
  280. * @return int ID of the current notification. 
  281. */ 
  282. function bp_get_the_notification_id() { 
  283.  
  284. /** 
  285. * Filters the ID of the notification currently being iterated on. 
  286. * 
  287. * @since 1.9.0 
  288. * 
  289. * @param int $id ID of the notification being iterated on. 
  290. */ 
  291. return apply_filters( 'bp_get_the_notification_id', buddypress()->notifications->query_loop->notification->id ); 
  292.  
  293. /** 
  294. * Output the associated item ID of the notification currently being iterated on. 
  295. * 
  296. * @since 1.9.0 
  297. */ 
  298. function bp_the_notification_item_id() { 
  299. echo bp_get_the_notification_item_id(); 
  300. /** 
  301. * Return the associated item ID of the notification currently being iterated on. 
  302. * 
  303. * @since 1.9.0 
  304. * 
  305. * @return int ID of the item associated with the current notification. 
  306. */ 
  307. function bp_get_the_notification_item_id() { 
  308.  
  309. /** 
  310. * Filters the associated item ID of the notification currently being iterated on. 
  311. * 
  312. * @since 1.9.0 
  313. * 
  314. * @param int $item_id ID of the associated item. 
  315. */ 
  316. return apply_filters( 'bp_get_the_notification_item_id', buddypress()->notifications->query_loop->notification->item_id ); 
  317.  
  318. /** 
  319. * Output the secondary associated item ID of the notification currently being iterated on. 
  320. * 
  321. * @since 1.9.0 
  322. */ 
  323. function bp_the_notification_secondary_item_id() { 
  324. echo bp_get_the_notification_secondary_item_id(); 
  325. /** 
  326. * Return the secondary associated item ID of the notification currently being iterated on. 
  327. * 
  328. * @since 1.9.0 
  329. * 
  330. * @return int ID of the secondary item associated with the current notification. 
  331. */ 
  332. function bp_get_the_notification_secondary_item_id() { 
  333.  
  334. /** 
  335. * Filters the secondary associated item ID of the notification currently being iterated on. 
  336. * 
  337. * @since 1.9.0 
  338. * 
  339. * @param int $secondary_item_id ID of the secondary associated item. 
  340. */ 
  341. return apply_filters( 'bp_get_the_notification_secondary_item_id', buddypress()->notifications->query_loop->notification->secondary_item_id ); 
  342.  
  343. /** 
  344. * Output the name of the component associated with the notification currently being iterated on. 
  345. * 
  346. * @since 1.9.0 
  347. */ 
  348. function bp_the_notification_component_name() { 
  349. echo bp_get_the_notification_component_name(); 
  350. /** 
  351. * Return the name of the component associated with the notification currently being iterated on. 
  352. * 
  353. * @since 1.9.0 
  354. * 
  355. * @return int Name of the component associated with the current notification. 
  356. */ 
  357. function bp_get_the_notification_component_name() { 
  358.  
  359. /** 
  360. * Filters the name of the component associated with the notification currently being iterated on. 
  361. * 
  362. * @since 1.9.0 
  363. * 
  364. * @param int $component_name Name of the component associated with the current notification. 
  365. */ 
  366. return apply_filters( 'bp_get_the_notification_component_name', buddypress()->notifications->query_loop->notification->component_name ); 
  367.  
  368. /** 
  369. * Output the name of the action associated with the notification currently being iterated on. 
  370. * 
  371. * @since 1.9.0 
  372. */ 
  373. function bp_the_notification_component_action() { 
  374. echo bp_get_the_notification_component_action(); 
  375. /** 
  376. * Return the name of the action associated with the notification currently being iterated on. 
  377. * 
  378. * @since 1.9.0 
  379. * 
  380. * @return int Name of the action associated with the current notification. 
  381. */ 
  382. function bp_get_the_notification_component_action() { 
  383.  
  384. /** 
  385. * Filters the name of the action associated with the notification currently being iterated on. 
  386. * 
  387. * @since 1.9.0 
  388. * 
  389. * @param int $component_action Name of the action associated with the current notification. 
  390. */ 
  391. return apply_filters( 'bp_get_the_notification_component_action', buddypress()->notifications->query_loop->notification->component_action ); 
  392.  
  393. /** 
  394. * Output the timestamp of the current notification. 
  395. * 
  396. * @since 1.9.0 
  397. */ 
  398. function bp_the_notification_date_notified() { 
  399. echo bp_get_the_notification_date_notified(); 
  400. /** 
  401. * Return the timestamp of the current notification. 
  402. * 
  403. * @since 1.9.0 
  404. * 
  405. * @return string Timestamp of the current notification. 
  406. */ 
  407. function bp_get_the_notification_date_notified() { 
  408.  
  409. /** 
  410. * Filters the timestamp of the current notification. 
  411. * 
  412. * @since 1.9.0 
  413. * 
  414. * @param string $date_notified Timestamp of the current notification. 
  415. */ 
  416. return apply_filters( 'bp_get_the_notification_date_notified', buddypress()->notifications->query_loop->notification->date_notified ); 
  417.  
  418. /** 
  419. * Output the timestamp of the current notification. 
  420. * 
  421. * @since 1.9.0 
  422. */ 
  423. function bp_the_notification_time_since() { 
  424. echo bp_get_the_notification_time_since(); 
  425. /** 
  426. * Return the timestamp of the current notification. 
  427. * 
  428. * @since 1.9.0 
  429. * 
  430. * @return string Timestamp of the current notification. 
  431. */ 
  432. function bp_get_the_notification_time_since() { 
  433.  
  434. // Get the notified date. 
  435. $date_notified = bp_get_the_notification_date_notified(); 
  436.  
  437. // Notified date has legitimate data. 
  438. if ( '0000-00-00 00:00:00' !== $date_notified ) { 
  439. $retval = bp_core_time_since( $date_notified ); 
  440.  
  441. // Notified date is empty, so return a fun string. 
  442. } else { 
  443. $retval = __( 'Date not found', 'buddypress' ); 
  444.  
  445. /** 
  446. * Filters the time since value of the current notification. 
  447. * 
  448. * @since 1.9.0 
  449. * 
  450. * @param string $retval Time since value for current notification. 
  451. */ 
  452. return apply_filters( 'bp_get_the_notification_time_since', $retval ); 
  453.  
  454. /** 
  455. * Output full-text description for a specific notification. 
  456. * 
  457. * @since 1.9.0 
  458. */ 
  459. function bp_the_notification_description() { 
  460. echo bp_get_the_notification_description(); 
  461.  
  462. /** 
  463. * Get full-text description for a specific notification. 
  464. * 
  465. * @since 1.9.0 
  466. * 
  467. * @return string 
  468. */ 
  469. function bp_get_the_notification_description() { 
  470. $bp = buddypress(); 
  471. $notification = $bp->notifications->query_loop->notification; 
  472.  
  473. // Callback function exists. 
  474. if ( isset( $bp->{ $notification->component_name }->notification_callback ) && is_callable( $bp->{ $notification->component_name }->notification_callback ) ) { 
  475. $description = call_user_func( $bp->{ $notification->component_name }->notification_callback, $notification->component_action, $notification->item_id, $notification->secondary_item_id, 1, 'string', $notification->id ); 
  476.  
  477. // @deprecated format_notification_function - 1.5 
  478. } elseif ( isset( $bp->{ $notification->component_name }->format_notification_function ) && function_exists( $bp->{ $notification->component_name }->format_notification_function ) ) { 
  479. $description = call_user_func( $bp->{ $notification->component_name }->format_notification_function, $notification->component_action, $notification->item_id, $notification->secondary_item_id, 1 ); 
  480.  
  481. // Allow non BuddyPress components to hook in. 
  482. } else { 
  483.  
  484. /** This filter is documented in bp-notifications/bp-notifications-functions.php */ 
  485. $description = apply_filters_ref_array( 'bp_notifications_get_notifications_for_user', array( $notification->component_action, $notification->item_id, $notification->secondary_item_id, 1, 'string', $notification->component_action, $notification->component_name, $notification->id ) ); 
  486.  
  487. /** 
  488. * Filters the full-text description for a specific notification. 
  489. * 
  490. * @since 1.9.0 
  491. * @since 2.3.0 Added the `$notification` parameter. 
  492. * 
  493. * @param string $description Full-text description for a specific notification. 
  494. * @param object $notification Notification object. 
  495. */ 
  496. return apply_filters( 'bp_get_the_notification_description', $description, $notification ); 
  497.  
  498. /** 
  499. * Output the mark read link for the current notification. 
  500. * 
  501. * @since 1.9.0 
  502. * @since 2.6.0 Added $user_id as a parameter. 
  503. * 
  504. * @param int $user_id The user ID. 
  505. */ 
  506. function bp_the_notification_mark_read_link( $user_id = 0 ) { 
  507. echo bp_get_the_notification_mark_read_link( $user_id ); 
  508. /** 
  509. * Return the mark read link for the current notification. 
  510. * 
  511. * @since 1.9.0 
  512. * @since 2.6.0 Added $user_id as a parameter. 
  513. * 
  514. * @param int $user_id The user ID. 
  515. * @return string 
  516. */ 
  517. function bp_get_the_notification_mark_read_link( $user_id = 0 ) { 
  518. // Set default user ID to use. 
  519. $user_id = 0 === $user_id ? bp_displayed_user_id() : $user_id; 
  520.  
  521. $retval = sprintf( '<a href="%1$s" class="mark-read primary">%2$s</a>', esc_url( bp_get_the_notification_mark_read_url( $user_id ) ), __( 'Read', 'buddypress' ) ); 
  522.  
  523. /** 
  524. * Filters the mark read link for the current notification. 
  525. * 
  526. * @since 1.9.0 
  527. * @since 2.6.0 Added $user_id as a parameter. 
  528. * 
  529. * @param string $retval HTML for the mark read link for the current notification. 
  530. * @param int $user_id The user ID. 
  531. */ 
  532. return apply_filters( 'bp_get_the_notification_mark_read_link', $retval, $user_id ); 
  533.  
  534. /** 
  535. * Output the URL used for marking a single notification as read. 
  536. * 
  537. * Since this function directly outputs a URL, it is escaped. 
  538. * 
  539. * @since 2.1.0 
  540. * @since 2.6.0 Added $user_id as a parameter. 
  541. * 
  542. * @param int $user_id The user ID. 
  543. */ 
  544. function bp_the_notification_mark_read_url( $user_id = 0 ) { 
  545. echo esc_url( bp_get_the_notification_mark_read_url( $user_id ) ); 
  546. /** 
  547. * Return the URL used for marking a single notification as read. 
  548. * 
  549. * @since 2.1.0 
  550. * @since 2.6.0 Added $user_id as a parameter. 
  551. * 
  552. * @param int $user_id The user ID. 
  553. * @return string 
  554. */ 
  555. function bp_get_the_notification_mark_read_url( $user_id = 0 ) { 
  556.  
  557. // Get the notification ID. 
  558. $id = bp_get_the_notification_id(); 
  559.  
  560. // Get the args to add to the URL. 
  561. $args = array( 
  562. 'action' => 'read',  
  563. 'notification_id' => $id 
  564. ); 
  565.  
  566. // Set default user ID to use. 
  567. $user_id = 0 === $user_id ? bp_displayed_user_id() : $user_id; 
  568.  
  569. // Add the args to the URL. 
  570. $url = add_query_arg( $args, bp_get_notifications_unread_permalink( $user_id ) ); 
  571.  
  572. // Add the nonce. 
  573. $url = wp_nonce_url( $url, 'bp_notification_mark_read_' . $id ); 
  574.  
  575. /** 
  576. * Filters the URL used for marking a single notification as read. 
  577. * 
  578. * @since 2.1.0 
  579. * @since 2.6.0 Added $user_id as a parameter. 
  580. * 
  581. * @param string $url URL to use for marking the single notification as read. 
  582. * @param int $user_id The user ID. 
  583. */ 
  584. return apply_filters( 'bp_get_the_notification_mark_read_url', $url, $user_id ); 
  585.  
  586. /** 
  587. * Output the mark unread link for the current notification. 
  588. * 
  589. * @since 1.9.0 
  590. * @since 2.6.0 Added $user_id as a parameter. 
  591. * 
  592. * @param int $user_id The user ID. 
  593. */ 
  594. function bp_the_notification_mark_unread_link( $user_id = 0 ) { 
  595. echo bp_get_the_notification_mark_unread_link(); 
  596. /** 
  597. * Return the mark unread link for the current notification. 
  598. * 
  599. * @since 1.9.0 
  600. * @since 2.6.0 Added $user_id as a parameter. 
  601. * 
  602. * @param int $user_id The user ID. 
  603. * @return string 
  604. */ 
  605. function bp_get_the_notification_mark_unread_link( $user_id = 0 ) { 
  606. // Set default user ID to use. 
  607. $user_id = 0 === $user_id ? bp_displayed_user_id() : $user_id; 
  608.  
  609. $retval = sprintf( '<a href="%1$s" class="mark-unread primary">%2$s</a>', esc_url( bp_get_the_notification_mark_unread_url( $user_id ) ), __( 'Unread', 'buddypress' ) ); 
  610.  
  611. /** 
  612. * Filters the link used for marking a single notification as unread. 
  613. * 
  614. * @since 1.9.0 
  615. * @since 2.6.0 Added $user_id as a parameter. 
  616. * 
  617. * @param string $retval HTML for the mark unread link for the current notification. 
  618. * @param int $user_id The user ID. 
  619. */ 
  620. return apply_filters( 'bp_get_the_notification_mark_unread_link', $retval, $user_id ); 
  621.  
  622. /** 
  623. * Output the URL used for marking a single notification as unread. 
  624. * 
  625. * Since this function directly outputs a URL, it is escaped. 
  626. * 
  627. * @since 2.1.0 
  628. * @since 2.6.0 Added $user_id as a parameter. 
  629. * 
  630. * @param int $user_id The user ID. 
  631. */ 
  632. function bp_the_notification_mark_unread_url( $user_id = 0 ) { 
  633. echo esc_url( bp_get_the_notification_mark_unread_url( $user_id ) ); 
  634. /** 
  635. * Return the URL used for marking a single notification as unread. 
  636. * 
  637. * @since 2.1.0 
  638. * @since 2.6.0 Added $user_id as a parameter. 
  639. * 
  640. * @param int $user_id The user ID. 
  641. * @return string 
  642. */ 
  643. function bp_get_the_notification_mark_unread_url( $user_id = 0 ) { 
  644.  
  645. // Get the notification ID. 
  646. $id = bp_get_the_notification_id(); 
  647.  
  648. // Get the args to add to the URL. 
  649. $args = array( 
  650. 'action' => 'unread',  
  651. 'notification_id' => $id 
  652. ); 
  653.  
  654. // Set default user ID to use. 
  655. $user_id = 0 === $user_id ? bp_displayed_user_id() : $user_id; 
  656.  
  657. // Add the args to the URL. 
  658. $url = add_query_arg( $args, bp_get_notifications_read_permalink( $user_id ) ); 
  659.  
  660. // Add the nonce. 
  661. $url = wp_nonce_url( $url, 'bp_notification_mark_unread_' . $id ); 
  662.  
  663. /** 
  664. * Filters the URL used for marking a single notification as unread. 
  665. * 
  666. * @since 2.1.0 
  667. * @since 2.6.0 Added $user_id as a parameter. 
  668. * 
  669. * @param string $url URL to use for marking the single notification as unread. 
  670. * @param int $user_id The user ID. 
  671. */ 
  672. return apply_filters( 'bp_get_the_notification_mark_unread_url', $url, $user_id ); 
  673.  
  674. /** 
  675. * Output the mark link for the current notification. 
  676. * 
  677. * @since 1.9.0 
  678. * @since 2.6.0 Added $user_id as a parameter. 
  679. * 
  680. * @param int $user_id The user ID. 
  681. */ 
  682. function bp_the_notification_mark_link( $user_id = 0 ) { 
  683. echo bp_get_the_notification_mark_link( $user_id ); 
  684. /** 
  685. * Return the mark link for the current notification. 
  686. * 
  687. * @since 1.9.0 
  688. * @since 2.6.0 Added $user_id as a parameter. 
  689. * 
  690. * @param int $user_id The user ID. 
  691. * @return string 
  692. */ 
  693. function bp_get_the_notification_mark_link( $user_id = 0 ) { 
  694. // Set default user ID to use. 
  695. $user_id = 0 === $user_id ? bp_displayed_user_id() : $user_id; 
  696.  
  697. if ( bp_is_current_action( 'read' ) ) { 
  698. $retval = bp_get_the_notification_mark_unread_link( $user_id ); 
  699. } else { 
  700. $retval = bp_get_the_notification_mark_read_link( $user_id ); 
  701.  
  702. /** 
  703. * Filters the mark link for the current notification. 
  704. * 
  705. * @since 1.9.0 
  706. * @since 2.6.0 Added $user_id as a parameter. 
  707. * 
  708. * @param string $retval The mark link for the current notification. 
  709. * @param int $user_id The user ID. 
  710. */ 
  711. return apply_filters( 'bp_get_the_notification_mark_link', $retval, $user_id ); 
  712.  
  713. /** 
  714. * Output the delete link for the current notification. 
  715. * 
  716. * @since 1.9.0 
  717. * @since 2.6.0 Added $user_id as a parameter. 
  718. * 
  719. * @param int $user_id The user ID. 
  720. */ 
  721. function bp_the_notification_delete_link( $user_id = 0 ) { 
  722. echo bp_get_the_notification_delete_link( $user_id ); 
  723. /** 
  724. * Return the delete link for the current notification. 
  725. * 
  726. * @since 1.9.0 
  727. * @since 2.6.0 Added $user_id as a parameter. 
  728. * 
  729. * @param int $user_id The user ID. 
  730. * @return string 
  731. */ 
  732. function bp_get_the_notification_delete_link( $user_id = 0 ) { 
  733. // Set default user ID to use. 
  734. $user_id = 0 === $user_id ? bp_displayed_user_id() : $user_id; 
  735.  
  736. $retval = sprintf( '<a href="%1$s" class="delete secondary confirm">%2$s</a>', esc_url( bp_get_the_notification_delete_url( $user_id ) ), __( 'Delete', 'buddypress' ) ); 
  737.  
  738. /** 
  739. * Filters the delete link for the current notification. 
  740. * 
  741. * @since 1.9.0 
  742. * @since 2.6.0 Added $user_id as a parameter. 
  743. * 
  744. * @param string $retval HTML for the delete link for the current notification. 
  745. * @param int $user_id The user ID. 
  746. */ 
  747. return apply_filters( 'bp_get_the_notification_delete_link', $retval, $user_id ); 
  748.  
  749. /** 
  750. * Output the URL used for deleting a single notification. 
  751. * 
  752. * Since this function directly outputs a URL, it is escaped. 
  753. * 
  754. * @since 2.1.0 
  755. * @since 2.6.0 Added $user_id as a parameter. 
  756. * 
  757. * @param int $user_id The user ID. 
  758. */ 
  759. function bp_the_notification_delete_url( $user_id = 0 ) { 
  760. echo esc_url( bp_get_the_notification_delete_url( $user_id ) ); 
  761. /** 
  762. * Return the URL used for deleting a single notification. 
  763. * 
  764. * @since 2.1.0 
  765. * @since 2.6.0 Added $user_id as a parameter. 
  766. * 
  767. * @param int $user_id The user ID. 
  768. * @return string 
  769. */ 
  770. function bp_get_the_notification_delete_url( $user_id = 0 ) { 
  771. // Set default user ID to use. 
  772. $user_id = 0 === $user_id ? bp_displayed_user_id() : $user_id; 
  773.  
  774. // URL to add nonce to. 
  775. if ( bp_is_current_action( 'unread' ) ) { 
  776. $link = bp_get_notifications_unread_permalink( $user_id ); 
  777. } elseif ( bp_is_current_action( 'read' ) ) { 
  778. $link = bp_get_notifications_read_permalink( $user_id ); 
  779.  
  780. // Get the ID. 
  781. $id = bp_get_the_notification_id(); 
  782.  
  783. // Get the args to add to the URL. 
  784. $args = array( 
  785. 'action' => 'delete',  
  786. 'notification_id' => $id 
  787. ); 
  788.  
  789. // Add the args. 
  790. $url = add_query_arg( $args, $link ); 
  791.  
  792. // Add the nonce. 
  793. $url = wp_nonce_url( $url, 'bp_notification_delete_' . $id ); 
  794.  
  795. /** 
  796. * Filters the URL used for deleting a single notification. 
  797. * 
  798. * @since 2.1.0 
  799. * @since 2.6.0 Added $user_id as a parameter. 
  800. * 
  801. * @param string $url URL used for deleting a single notification. 
  802. * @param int $user_id The user ID. 
  803. */ 
  804. return apply_filters( 'bp_get_the_notification_delete_url', $url, $user_id ); 
  805.  
  806. /** 
  807. * Output the action links for the current notification. 
  808. * 
  809. * @since 1.9.0 
  810. * @since 2.6.0 Added $user_id as a parameter to $args. 
  811. * 
  812. * @param array|string $args Array of arguments. 
  813. */ 
  814. function bp_the_notification_action_links( $args = '' ) { 
  815. echo bp_get_the_notification_action_links( $args ); 
  816. /** 
  817. * Return the action links for the current notification. 
  818. * 
  819. * @since 1.9.0 
  820. * @since 2.6.0 Added $user_id as a parameter to $args. 
  821. * 
  822. * @param array|string $args { 
  823. * @type string $before HTML before the links. 
  824. * @type string $after HTML after the links. 
  825. * @type string $sep HTML between the links. 
  826. * @type array $links Array of links to implode by 'sep'. 
  827. * @type int $user_id User ID to fetch action links for. Defaults to displayed user ID. 
  828. * } 
  829. * @return string HTML links for actions to take on single notifications. 
  830. */ 
  831. function bp_get_the_notification_action_links( $args = '' ) { 
  832. // Set default user ID to use. 
  833. $user_id = isset( $args['user_id'] ) ? $args['user_id'] : bp_displayed_user_id(); 
  834.  
  835. // Parse. 
  836. $r = wp_parse_args( $args, array( 
  837. 'before' => '',  
  838. 'after' => '',  
  839. 'sep' => ' | ',  
  840. 'links' => array( 
  841. bp_get_the_notification_mark_link( $user_id ),  
  842. bp_get_the_notification_delete_link( $user_id ) 
  843. ) ); 
  844.  
  845. // Build the links. 
  846. $retval = $r['before'] . implode( $r['links'], $r['sep'] ) . $r['after']; 
  847.  
  848. /** 
  849. * Filters the action links for the current notification. 
  850. * 
  851. * @since 1.9.0 
  852. * @since 2.6.0 Added the `$r` parameter. 
  853. * 
  854. * @param string $retval HTML links for actions to take on single notifications. 
  855. * @param array $r Array of parsed arguments. 
  856. */ 
  857. return apply_filters( 'bp_get_the_notification_action_links', $retval, $r ); 
  858.  
  859. /** 
  860. * Output the pagination count for the current notification loop. 
  861. * 
  862. * @since 1.9.0 
  863. */ 
  864. function bp_notifications_pagination_count() { 
  865. echo bp_get_notifications_pagination_count(); 
  866. /** 
  867. * Return the pagination count for the current notification loop. 
  868. * 
  869. * @since 1.9.0 
  870. * 
  871. * @return string HTML for the pagination count. 
  872. */ 
  873. function bp_get_notifications_pagination_count() { 
  874. $query_loop = buddypress()->notifications->query_loop; 
  875. $start_num = intval( ( $query_loop->pag_page - 1 ) * $query_loop->pag_num ) + 1; 
  876. $from_num = bp_core_number_format( $start_num ); 
  877. $to_num = bp_core_number_format( ( $start_num + ( $query_loop->pag_num - 1 ) > $query_loop->total_notification_count ) ? $query_loop->total_notification_count : $start_num + ( $query_loop->pag_num - 1 ) ); 
  878. $total = bp_core_number_format( $query_loop->total_notification_count ); 
  879.  
  880. if ( 1 == $query_loop->total_notification_count ) { 
  881. $pag = __( 'Viewing 1 notification', 'buddypress' ); 
  882. } else { 
  883. $pag = sprintf( _n( 'Viewing %1$s - %2$s of %3$s notification', 'Viewing %1$s - %2$s of %3$s notifications', $query_loop->total_notification_count, 'buddypress' ), $from_num, $to_num, $total ); 
  884.  
  885. /** 
  886. * Filters the pagination count for the current notification loop. 
  887. * 
  888. * @since 1.9.0 
  889. * 
  890. * @param string $pag HTML for the pagination count. 
  891. */ 
  892. return apply_filters( 'bp_notifications_pagination_count', $pag ); 
  893.  
  894. /** 
  895. * Output the pagination links for the current notification loop. 
  896. * 
  897. * @since 1.9.0 
  898. */ 
  899. function bp_notifications_pagination_links() { 
  900. echo bp_get_notifications_pagination_links(); 
  901. /** 
  902. * Return the pagination links for the current notification loop. 
  903. * 
  904. * @since 1.9.0 
  905. * 
  906. * @return string HTML for the pagination links. 
  907. */ 
  908. function bp_get_notifications_pagination_links() { 
  909.  
  910. /** 
  911. * Filters the pagination links for the current notification loop. 
  912. * 
  913. * @since 1.9.0 
  914. * 
  915. * @param string $pag_links HTML for the pagination links. 
  916. */ 
  917. return apply_filters( 'bp_get_notifications_pagination_links', buddypress()->notifications->query_loop->pag_links ); 
  918.  
  919. /** Form Helpers **************************************************************/ 
  920.  
  921. /** 
  922. * Output the form for changing the sort order of notifications. 
  923. * 
  924. * @since 1.9.0 
  925. */ 
  926. function bp_notifications_sort_order_form() { 
  927.  
  928. // Setup local variables. 
  929. $orders = array( 'DESC', 'ASC' ); 
  930. $selected = 'DESC'; 
  931.  
  932. // Check for a custom sort_order. 
  933. if ( !empty( $_REQUEST['sort_order'] ) ) { 
  934. if ( in_array( $_REQUEST['sort_order'], $orders ) ) { 
  935. $selected = $_REQUEST['sort_order']; 
  936. } ?> 
  937.  
  938. <form action="" method="get" id="notifications-sort-order"> 
  939. <label for="notifications-sort-order-list"><?php esc_html_e( 'Order By:', 'buddypress' ); ?></label> 
  940.  
  941. <select id="notifications-sort-order-list" name="sort_order" onchange="this.form.submit();"> 
  942. <option value="DESC" <?php selected( $selected, 'DESC' ); ?>><?php _e( 'Newest First', 'buddypress' ); ?></option> 
  943. <option value="ASC" <?php selected( $selected, 'ASC' ); ?>><?php _e( 'Oldest First', 'buddypress' ); ?></option> 
  944. </select> 
  945.  
  946. <noscript> 
  947. <input id="submit" type="submit" name="form-submit" class="submit" value="<?php esc_attr_e( 'Go', 'buddypress' ); ?>" /> 
  948. </noscript> 
  949. </form> 
  950.  
  951. <?php 
  952.  
  953. /** 
  954. * Output the dropdown for bulk management of notifications. 
  955. * 
  956. * @since 2.2.0 
  957. */ 
  958. function bp_notifications_bulk_management_dropdown() { 
  959. ?> 
  960. <label class="bp-screen-reader-text" for="notification-select"><?php 
  961. /** translators: accessibility text */ 
  962. _e( 'Select Bulk Action', 'buddypress' ); 
  963. ?></label> 
  964. <select name="notification_bulk_action" id="notification-select"> 
  965. <option value="" selected="selected"><?php _e( 'Bulk Actions', 'buddypress' ); ?></option> 
  966.  
  967. <?php if ( bp_is_current_action( 'unread' ) ) : ?> 
  968. <option value="read"><?php _e( 'Mark read', 'buddypress' ); ?></option> 
  969. <?php elseif ( bp_is_current_action( 'read' ) ) : ?> 
  970. <option value="unread"><?php _e( 'Mark unread', 'buddypress' ); ?></option> 
  971. <?php endif; ?> 
  972. <option value="delete"><?php _e( 'Delete', 'buddypress' ); ?></option> 
  973. </select> 
  974. <input type="submit" id="notification-bulk-manage" class="button action" value="<?php esc_attr_e( 'Apply', 'buddypress' ); ?>"> 
  975. <?php 
.