/bp-friends/bp-friends-notifications.php

  1. <?php 
  2. /** 
  3. * BuddyPress Friends Activity Functions. 
  4. * 
  5. * These functions handle the recording, deleting and formatting of activity 
  6. * for the user and for this specific component. 
  7. * 
  8. * @package BuddyPress 
  9. * @subpackage FriendsNotifications 
  10. * @since 1.0.0 
  11. */ 
  12.  
  13. // Exit if accessed directly. 
  14. defined( 'ABSPATH' ) || exit; 
  15.  
  16. /** 
  17. * Notification formatting callback for bp-friends notifications. 
  18. * 
  19. * @since 1.0.0 
  20. * 
  21. * @param string $action The kind of notification being rendered. 
  22. * @param int $item_id The primary item ID. 
  23. * @param int $secondary_item_id The secondary item ID. 
  24. * @param int $total_items The total number of messaging-related notifications 
  25. * waiting for the user. 
  26. * @param string $format 'string' for BuddyBar-compatible notifications; 
  27. * 'array' for WP Toolbar. Default: 'string'. 
  28. * @return array|string 
  29. */ 
  30. function friends_format_notifications( $action, $item_id, $secondary_item_id, $total_items, $format = 'string' ) { 
  31.  
  32. switch ( $action ) { 
  33. case 'friendship_accepted': 
  34. $link = trailingslashit( bp_loggedin_user_domain() . bp_get_friends_slug() . '/my-friends' ); 
  35.  
  36. // $action and $amount are used to generate dynamic filter names. 
  37. $action = 'accepted'; 
  38.  
  39. // Set up the string and the filter. 
  40. if ( (int) $total_items > 1 ) { 
  41. $text = sprintf( __( '%d friends accepted your friendship requests', 'buddypress' ), (int) $total_items ); 
  42. $amount = 'multiple'; 
  43. } else { 
  44. $text = sprintf( __( '%s accepted your friendship request', 'buddypress' ), bp_core_get_user_displayname( $item_id ) ); 
  45. $amount = 'single'; 
  46.  
  47. break; 
  48.  
  49. case 'friendship_request': 
  50. $link = bp_loggedin_user_domain() . bp_get_friends_slug() . '/requests/?new'; 
  51.  
  52. $action = 'request'; 
  53.  
  54. // Set up the string and the filter. 
  55. if ( (int) $total_items > 1 ) { 
  56. $text = sprintf( __( 'You have %d pending friendship requests', 'buddypress' ), (int) $total_items ); 
  57. $amount = 'multiple'; 
  58. } else { 
  59. $text = sprintf( __( 'You have a friendship request from %s', 'buddypress' ), bp_core_get_user_displayname( $item_id ) ); 
  60. $amount = 'single'; 
  61.  
  62. break; 
  63.  
  64. // Return either an HTML link or an array, depending on the requested format. 
  65. if ( 'string' == $format ) { 
  66.  
  67. /** 
  68. * Filters the format of friendship notifications based on type and amount * of notifications pending. 
  69. * 
  70. * This is a variable filter that has four possible versions. 
  71. * The four possible versions are: 
  72. * - bp_friends_single_friendship_accepted_notification 
  73. * - bp_friends_multiple_friendship_accepted_notification 
  74. * - bp_friends_single_friendship_request_notification 
  75. * - bp_friends_multiple_friendship_request_notification 
  76. * 
  77. * @since 1.0.0 
  78. * 
  79. * @param string|array $value Depending on format, an HTML link to new requests profile 
  80. * tab or array with link and text. 
  81. * @param int $total_items The total number of messaging-related notifications 
  82. * waiting for the user. 
  83. * @param int $item_id The primary item ID. 
  84. */ 
  85. $return = apply_filters( 'bp_friends_' . $amount . '_friendship_' . $action . '_notification', '<a href="' . esc_url( $link ) . '">' . esc_html( $text ) . '</a>', (int) $total_items, $item_id ); 
  86. } else { 
  87. /** This filter is documented in bp-friends/bp-friends-notifications.php */ 
  88. $return = apply_filters( 'bp_friends_' . $amount . '_friendship_' . $action . '_notification', array( 
  89. 'link' => $link,  
  90. 'text' => $text 
  91. ), (int) $total_items, $item_id ); 
  92.  
  93. /** 
  94. * Fires at the end of the bp-friends notification format callback. 
  95. * 
  96. * @since 1.0.0 
  97. * 
  98. * @param string $action The kind of notification being rendered. 
  99. * @param int $item_id The primary item ID. 
  100. * @param int $secondary_item_id The secondary item ID. 
  101. * @param int $total_items The total number of messaging-related notifications 
  102. * waiting for the user. 
  103. * @param array|string $return Notification text string or array of link and text. 
  104. */ 
  105. do_action( 'friends_format_notifications', $action, $item_id, $secondary_item_id, $total_items, $return ); 
  106.  
  107. return $return; 
  108.  
  109. /** 
  110. * Clear friend-related notifications when ?new=1 
  111. * 
  112. * @since 1.2.0 
  113. */ 
  114. function friends_clear_friend_notifications() { 
  115. if ( isset( $_GET['new'] ) ) { 
  116. bp_notifications_mark_notifications_by_type( bp_loggedin_user_id(), buddypress()->friends->id, 'friendship_accepted' ); 
  117. add_action( 'bp_activity_screen_my_activity', 'friends_clear_friend_notifications' ); 
  118.  
  119. /** 
  120. * Delete any friendship request notifications for the logged in user. 
  121. * 
  122. * @since 1.9.0 
  123. */ 
  124. function bp_friends_mark_friendship_request_notifications_by_type() { 
  125. if ( isset( $_GET['new'] ) ) { 
  126. bp_notifications_mark_notifications_by_type( bp_loggedin_user_id(), buddypress()->friends->id, 'friendship_request' ); 
  127. add_action( 'friends_screen_requests', 'bp_friends_mark_friendship_request_notifications_by_type' ); 
  128.  
  129. /** 
  130. * Delete any friendship acceptance notifications for the logged in user. 
  131. * 
  132. * @since 1.9.0 
  133. */ 
  134. function bp_friends_mark_friendship_accepted_notifications_by_type() { 
  135. bp_notifications_mark_notifications_by_type( bp_loggedin_user_id(), buddypress()->friends->id, 'friendship_accepted' ); 
  136. add_action( 'friends_screen_my_friends', 'bp_friends_mark_friendship_accepted_notifications_by_type' ); 
  137.  
  138. /** 
  139. * Notify one use that another user has requested their virtual friendship. 
  140. * 
  141. * @since 1.9.0 
  142. * 
  143. * @param int $friendship_id The unique ID of the friendship. 
  144. * @param int $initiator_user_id The friendship initiator user ID. 
  145. * @param int $friend_user_id The friendship request receiver user ID. 
  146. */ 
  147. function bp_friends_friendship_requested_notification( $friendship_id, $initiator_user_id, $friend_user_id ) { 
  148. bp_notifications_add_notification( array( 
  149. 'user_id' => $friend_user_id,  
  150. 'item_id' => $initiator_user_id,  
  151. 'secondary_item_id' => $friendship_id,  
  152. 'component_name' => buddypress()->friends->id,  
  153. 'component_action' => 'friendship_request',  
  154. 'date_notified' => bp_core_current_time(),  
  155. 'is_new' => 1,  
  156. ) ); 
  157. add_action( 'friends_friendship_requested', 'bp_friends_friendship_requested_notification', 10, 3 ); 
  158.  
  159. /** 
  160. * Remove friend request notice when a member rejects another members 
  161. * 
  162. * @since 1.9.0 
  163. * 
  164. * @param int $friendship_id Friendship ID (not used). 
  165. * @param object $friendship Friendship object. 
  166. */ 
  167. function bp_friends_mark_friendship_rejected_notifications_by_item_id( $friendship_id, $friendship ) { 
  168. bp_notifications_mark_notifications_by_item_id( $friendship->friend_user_id, $friendship->initiator_user_id, buddypress()->friends->id, 'friendship_request' ); 
  169. add_action( 'friends_friendship_rejected', 'bp_friends_mark_friendship_rejected_notifications_by_item_id', 10, 2 ); 
  170.  
  171. /** 
  172. * Notify a member when another member accepts their virtual friendship request. 
  173. * 
  174. * @since 1.9.0 
  175. * 
  176. * @param int $friendship_id The unique ID of the friendship. 
  177. * @param int $initiator_user_id The friendship initiator user ID. 
  178. * @param int $friend_user_id The friendship request receiver user ID. 
  179. */ 
  180. function bp_friends_add_friendship_accepted_notification( $friendship_id, $initiator_user_id, $friend_user_id ) { 
  181. // Remove the friend request notice. 
  182. bp_notifications_mark_notifications_by_item_id( $friend_user_id, $initiator_user_id, buddypress()->friends->id, 'friendship_request' ); 
  183.  
  184. // Add a friend accepted notice for the initiating user. 
  185. bp_notifications_add_notification( array( 
  186. 'user_id' => $initiator_user_id,  
  187. 'item_id' => $friend_user_id,  
  188. 'secondary_item_id' => $friendship_id,  
  189. 'component_name' => buddypress()->friends->id,  
  190. 'component_action' => 'friendship_accepted',  
  191. 'date_notified' => bp_core_current_time(),  
  192. 'is_new' => 1,  
  193. ) ); 
  194. add_action( 'friends_friendship_accepted', 'bp_friends_add_friendship_accepted_notification', 10, 3 ); 
  195.  
  196. /** 
  197. * Remove friend request notice when a member withdraws their friend request. 
  198. * 
  199. * @since 1.9.0 
  200. * 
  201. * @param int $friendship_id Friendship ID (not used). 
  202. * @param object $friendship Friendship Object. 
  203. */ 
  204. function bp_friends_mark_friendship_withdrawn_notifications_by_item_id( $friendship_id, $friendship ) { 
  205. bp_notifications_delete_notifications_by_item_id( $friendship->friend_user_id, $friendship->initiator_user_id, buddypress()->friends->id, 'friendship_request' ); 
  206. add_action( 'friends_friendship_withdrawn', 'bp_friends_mark_friendship_withdrawn_notifications_by_item_id', 10, 2 ); 
  207.  
  208. /** 
  209. * Remove friendship requests FROM user, used primarily when a user is deleted. 
  210. * 
  211. * @since 1.9.0 
  212. * 
  213. * @param int $user_id ID of the user whose notifications are removed. 
  214. */ 
  215. function bp_friends_remove_notifications_data( $user_id = 0 ) { 
  216. bp_notifications_delete_notifications_from_user( $user_id, buddypress()->friends->id, 'friendship_request' ); 
  217. add_action( 'friends_remove_data', 'bp_friends_remove_notifications_data', 10, 1 ); 
.