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