/bp-messages/bp-messages-notifications.php

  1. <?php 
  2. /** 
  3. * BuddyPress Messages Notifications. 
  4. * 
  5. * @package BuddyPress 
  6. * @subpackage MessagesNotifications 
  7. * @since 1.0.0 
  8. */ 
  9.  
  10. // Exit if accessed directly. 
  11. defined( 'ABSPATH' ) || exit; 
  12.  
  13. /** 
  14. * Format notifications for the Messages component. 
  15. * 
  16. * @since 1.0.0 
  17. * 
  18. * @param string $action The kind of notification being rendered. 
  19. * @param int $item_id The primary item id. 
  20. * @param int $secondary_item_id The secondary item id. 
  21. * @param int $total_items The total number of messaging-related notifications 
  22. * waiting for the user. 
  23. * @param string $format Return value format. 'string' for BuddyBar-compatible 
  24. * notifications; 'array' for WP Toolbar. Default: 'string'. 
  25. * @return string|array Formatted notifications. 
  26. */ 
  27. function messages_format_notifications( $action, $item_id, $secondary_item_id, $total_items, $format = 'string' ) { 
  28. $total_items = (int) $total_items; 
  29. $text = ''; 
  30. $link = trailingslashit( bp_loggedin_user_domain() . bp_get_messages_slug() . '/inbox' ); 
  31. $title = __( 'Inbox', 'buddypress' ); 
  32. $amount = 'single'; 
  33.  
  34. if ( 'new_message' === $action ) { 
  35. if ( $total_items > 1 ) { 
  36. $amount = 'multiple'; 
  37. $text = sprintf( __( 'You have %d new messages', 'buddypress' ), $total_items ); 
  38.  
  39. } else { 
  40. // Get message thread ID. 
  41. $message = new BP_Messages_Message( $item_id ); 
  42. $thread_id = $message->thread_id; 
  43. $link = ( ! empty( $thread_id ) ) 
  44. ? bp_get_message_thread_view_link( $thread_id ) 
  45. : false; 
  46.  
  47. if ( ! empty( $secondary_item_id ) ) { 
  48. $text = sprintf( __( '%s sent you a new private message', 'buddypress' ), bp_core_get_user_displayname( $secondary_item_id ) ); 
  49. } else { 
  50. $text = sprintf( _n( 'You have %s new private message', 'You have %s new private messages', $total_items, 'buddypress' ), bp_core_number_format( $total_items ) ); 
  51.  
  52. if ( 'string' === $format ) { 
  53. if ( ! empty( $link ) ) { 
  54. $return = '<a href="' . esc_url( $link ) . '" title="' . esc_attr( $title ) . '">' . esc_html( $text ) . '</a>'; 
  55. } else { 
  56. $return = esc_html( $text ); 
  57.  
  58. /** 
  59. * Filters the new message notification text before the notification is created. 
  60. * 
  61. * This is a dynamic filter. Possible filter names are: 
  62. * - 'bp_messages_multiple_new_message_notification'. 
  63. * - 'bp_messages_single_new_message_notification'. 
  64. * 
  65. * @param string $return Notification text. 
  66. * @param int $total_items Number of messages referred to by the notification. 
  67. * @param string $text The raw notification test (ie, not wrapped in a link). 
  68. * @param int $item_id ID of the associated item. 
  69. * @param int $secondary_item_id ID of the secondary associated item. 
  70. */ 
  71. $return = apply_filters( 'bp_messages_' . $amount . '_new_message_notification', $return, (int) $total_items, $text, $link, $item_id, $secondary_item_id ); 
  72. } else { 
  73. /** This filter is documented in bp-messages/bp-messages-notifications.php */ 
  74. $return = apply_filters( 'bp_messages_' . $amount . '_new_message_notification', array( 
  75. 'text' => $text,  
  76. 'link' => $link 
  77. ), $link, (int) $total_items, $text, $link, $item_id, $secondary_item_id ); 
  78.  
  79. // Custom notification action for the Messages component 
  80. } else { 
  81. if ( 'string' === $format ) { 
  82. $return = $text; 
  83. } else { 
  84. $return = array( 
  85. 'text' => $text,  
  86. 'link' => $link 
  87. ); 
  88.  
  89. /** 
  90. * Backcompat for plugins that used to filter bp_messages_single_new_message_notification 
  91. * for their custom actions. These plugins should now use 'bp_messages_' . $action . '_notification' 
  92. */ 
  93. if ( has_filter( 'bp_messages_single_new_message_notification' ) ) { 
  94. if ( 'string' === $format ) { 
  95. /** This filter is documented in bp-messages/bp-messages-notifications.php */ 
  96. $return = apply_filters( 'bp_messages_single_new_message_notification', $return, (int) $total_items, $text, $link, $item_id, $secondary_item_id ); 
  97.  
  98. // Notice that there are seven parameters instead of six? Ugh... 
  99. } else { 
  100. /** This filter is documented in bp-messages/bp-messages-notifications.php */ 
  101. $return = apply_filters( 'bp_messages_single_new_message_notification', $return, $link, (int) $total_items, $text, $link, $item_id, $secondary_item_id ); 
  102.  
  103. /** 
  104. * Filters the custom action notification before the notification is created. 
  105. * 
  106. * This is a dynamic filter based on the message notification action. 
  107. * 
  108. * @since 2.6.0 
  109. * 
  110. * @param array $value An associative array containing the text and the link of the notification 
  111. * @param int $item_id ID of the associated item. 
  112. * @param int $secondary_item_id ID of the secondary associated item. 
  113. * @param int $total_items Number of messages referred to by the notification. 
  114. * @param string $format Return value format. 'string' for BuddyBar-compatible 
  115. * notifications; 'array' for WP Toolbar. Default: 'string'. 
  116. */ 
  117. $return = apply_filters( "bp_messages_{$action}_notification", $return, $item_id, $secondary_item_id, $total_items, $format ); 
  118.  
  119. /** 
  120. * Fires right before returning the formatted message notifications. 
  121. * 
  122. * @since 1.0.0 
  123. * 
  124. * @param string $action The type of message notification. 
  125. * @param int $item_id The primary item ID. 
  126. * @param int $secondary_item_id The secondary item ID. 
  127. * @param int $total_items Total amount of items to format. 
  128. */ 
  129. do_action( 'messages_format_notifications', $action, $item_id, $secondary_item_id, $total_items ); 
  130.  
  131. return $return; 
  132.  
  133. /** 
  134. * Send notifications to message recipients. 
  135. * 
  136. * @since 1.9.0 
  137. * 
  138. * @param BP_Messages_Message $message Message object. 
  139. */ 
  140. function bp_messages_message_sent_add_notification( $message ) { 
  141. if ( ! empty( $message->recipients ) ) { 
  142. foreach ( (array) $message->recipients as $recipient ) { 
  143. bp_notifications_add_notification( array( 
  144. 'user_id' => $recipient->user_id,  
  145. 'item_id' => $message->id,  
  146. 'secondary_item_id' => $message->sender_id,  
  147. 'component_name' => buddypress()->messages->id,  
  148. 'component_action' => 'new_message',  
  149. 'date_notified' => bp_core_current_time(),  
  150. 'is_new' => 1,  
  151. ) ); 
  152. add_action( 'messages_message_sent', 'bp_messages_message_sent_add_notification', 10 ); 
  153.  
  154. /** 
  155. * Mark new message notification when member reads a message thread directly. 
  156. * 
  157. * @since 1.9.0 
  158. */ 
  159. function bp_messages_screen_conversation_mark_notifications() { 
  160. global $thread_template; 
  161.  
  162. // Get unread PM notifications for the user. 
  163. $new_pm_notifications = BP_Notifications_Notification::get( array( 
  164. 'user_id' => bp_loggedin_user_id(),  
  165. 'component_name' => buddypress()->messages->id,  
  166. 'component_action' => 'new_message',  
  167. 'is_new' => 1,  
  168. ) ); 
  169. $unread_message_ids = wp_list_pluck( $new_pm_notifications, 'item_id' ); 
  170.  
  171. // No unread PMs, so stop! 
  172. if ( empty( $unread_message_ids ) ) { 
  173. return; 
  174.  
  175. // Get the unread message ids for this thread only. 
  176. $message_ids = array_intersect( $unread_message_ids, wp_list_pluck( $thread_template->thread->messages, 'id' ) ); 
  177.  
  178. // Mark each notification for each PM message as read. 
  179. foreach ( $message_ids as $message_id ) { 
  180. bp_notifications_mark_notifications_by_item_id( bp_loggedin_user_id(), (int) $message_id, buddypress()->messages->id, 'new_message' ); 
  181. add_action( 'thread_loop_start', 'bp_messages_screen_conversation_mark_notifications', 10 ); 
  182.  
  183. /** 
  184. * When a message is deleted, delete corresponding notifications. 
  185. * 
  186. * @since 2.0.0 
  187. * 
  188. * @param int $thread_id ID of the thread. 
  189. * @param array $message_ids IDs of the messages. 
  190. */ 
  191. function bp_messages_message_delete_notifications( $thread_id, $message_ids ) { 
  192. // For each recipient, delete notifications corresponding to each message. 
  193. $thread = new BP_Messages_Thread( $thread_id ); 
  194. foreach ( $thread->get_recipients() as $recipient ) { 
  195. foreach ( $message_ids as $message_id ) { 
  196. bp_notifications_delete_notifications_by_item_id( $recipient->user_id, (int) $message_id, buddypress()->messages->id, 'new_message' ); 
  197. add_action( 'bp_messages_thread_after_delete', 'bp_messages_message_delete_notifications', 10, 2 ); 
.