/wp-content/plugins/buddypress/bp-activity/bp-activity-notifications.php

https://bitbucket.org/Red54/dianjihun · PHP · 238 lines · 120 code · 38 blank · 80 comment · 24 complexity · 693ca3cffcf9b8deb58e633a1944efc6 MD5 · raw file

  1. <?php
  2. /**
  3. * BuddyPress Activity Notifications
  4. *
  5. * @package BuddyPress
  6. * @subpackage ActivityNotifications
  7. */
  8. // Exit if accessed directly
  9. if ( !defined( 'ABSPATH' ) ) exit;
  10. /**
  11. * Sends an email notification and a BP notification when someone mentions you in an update
  12. *
  13. * @since BuddyPress (1.2)
  14. *
  15. * @param int $activity_id The id of the activity update
  16. * @param int $receiver_user_id The unique user_id of the user who is receiving the update
  17. *
  18. * @uses bp_core_add_notification()
  19. * @uses bp_get_user_meta()
  20. * @uses bp_core_get_user_displayname()
  21. * @uses bp_activity_get_permalink()
  22. * @uses bp_core_get_user_domain()
  23. * @uses bp_get_settings_slug()
  24. * @uses bp_activity_filter_kses()
  25. * @uses bp_core_get_core_userdata()
  26. * @uses wp_specialchars_decode()
  27. * @uses get_blog_option()
  28. * @uses bp_is_active()
  29. * @uses bp_is_group()
  30. * @uses bp_get_current_group_name()
  31. * @uses apply_filters() To call the 'bp_activity_at_message_notification_to' hook
  32. * @uses apply_filters() To call the 'bp_activity_at_message_notification_subject' hook
  33. * @uses apply_filters() To call the 'bp_activity_at_message_notification_message' hook
  34. * @uses wp_mail()
  35. * @uses do_action() To call the 'bp_activity_sent_mention_email' hook
  36. */
  37. function bp_activity_at_message_notification( $activity_id, $receiver_user_id ) {
  38. // Don't leave multiple notifications for the same activity item
  39. $notifications = BP_Core_Notification::get_all_for_user( $receiver_user_id, 'all' );
  40. foreach( $notifications as $notification ) {
  41. if ( $activity_id == $notification->item_id ) {
  42. return;
  43. }
  44. }
  45. $activity = new BP_Activity_Activity( $activity_id );
  46. $subject = '';
  47. $message = '';
  48. $content = '';
  49. // Add the BP notification
  50. bp_core_add_notification( $activity_id, $receiver_user_id, 'activity', 'new_at_mention', $activity->user_id );
  51. // Now email the user with the contents of the message (if they have enabled email notifications)
  52. if ( 'no' != bp_get_user_meta( $receiver_user_id, 'notification_activity_new_mention', true ) ) {
  53. $poster_name = bp_core_get_user_displayname( $activity->user_id );
  54. $message_link = bp_activity_get_permalink( $activity_id );
  55. $settings_slug = function_exists( 'bp_get_settings_slug' ) ? bp_get_settings_slug() : 'settings';
  56. $settings_link = bp_core_get_user_domain( $receiver_user_id ) . $settings_slug . '/notifications/';
  57. $poster_name = stripslashes( $poster_name );
  58. $content = bp_activity_filter_kses( strip_tags( stripslashes( $activity->content ) ) );
  59. // Set up and send the message
  60. $ud = bp_core_get_core_userdata( $receiver_user_id );
  61. $to = $ud->user_email;
  62. $subject = bp_get_email_subject( array( 'text' => sprintf( __( '%s mentioned you in an update', 'buddypress' ), $poster_name ) ) );
  63. if ( bp_is_active( 'groups' ) && bp_is_group() ) {
  64. $message = sprintf( __(
  65. '%1$s mentioned you in the group "%2$s":
  66. "%3$s"
  67. To view and respond to the message, log in and visit: %4$s
  68. ---------------------
  69. ', 'buddypress' ), $poster_name, bp_get_current_group_name(), $content, $message_link );
  70. } else {
  71. $message = sprintf( __(
  72. '%1$s mentioned you in an update:
  73. "%2$s"
  74. To view and respond to the message, log in and visit: %3$s
  75. ---------------------
  76. ', 'buddypress' ), $poster_name, $content, $message_link );
  77. }
  78. // Only show the disable notifications line if the settings component is enabled
  79. if ( bp_is_active( 'settings' ) ) {
  80. $message .= sprintf( __( 'To disable these notifications please log in and go to: %s', 'buddypress' ), $settings_link );
  81. }
  82. /* Send the message */
  83. $to = apply_filters( 'bp_activity_at_message_notification_to', $to );
  84. $subject = apply_filters( 'bp_activity_at_message_notification_subject', $subject, $poster_name );
  85. $message = apply_filters( 'bp_activity_at_message_notification_message', $message, $poster_name, $content, $message_link, $settings_link );
  86. wp_mail( $to, $subject, $message );
  87. }
  88. do_action( 'bp_activity_sent_mention_email', $activity, $subject, $message, $content );
  89. }
  90. /**
  91. * Sends an email notification and a BP notification when someone mentions you in an update
  92. *
  93. * @since BuddyPress (1.2)
  94. *
  95. * @param int $comment_id The comment id
  96. * @param int $commenter_id The unique user_id of the user who posted the comment
  97. * @param array $params {@link bp_activity_new_comment()}
  98. *
  99. * @uses bp_get_user_meta()
  100. * @uses bp_core_get_user_displayname()
  101. * @uses bp_activity_get_permalink()
  102. * @uses bp_core_get_user_domain()
  103. * @uses bp_get_settings_slug()
  104. * @uses bp_activity_filter_kses()
  105. * @uses bp_core_get_core_userdata()
  106. * @uses wp_specialchars_decode()
  107. * @uses get_blog_option()
  108. * @uses bp_get_root_blog_id()
  109. * @uses apply_filters() To call the 'bp_activity_new_comment_notification_to' hook
  110. * @uses apply_filters() To call the 'bp_activity_new_comment_notification_subject' hook
  111. * @uses apply_filters() To call the 'bp_activity_new_comment_notification_message' hook
  112. * @uses wp_mail()
  113. * @uses do_action() To call the 'bp_activity_sent_reply_to_update_email' hook
  114. * @uses apply_filters() To call the 'bp_activity_new_comment_notification_comment_author_to' hook
  115. * @uses apply_filters() To call the 'bp_activity_new_comment_notification_comment_author_subject' hook
  116. * @uses apply_filters() To call the 'bp_activity_new_comment_notification_comment_author_message' hook
  117. * @uses do_action() To call the 'bp_activity_sent_reply_to_reply_email' hook
  118. */
  119. function bp_activity_new_comment_notification( $comment_id, $commenter_id, $params ) {
  120. // Set some default parameters
  121. $activity_id = 0;
  122. $parent_id = 0;
  123. extract( $params );
  124. $original_activity = new BP_Activity_Activity( $activity_id );
  125. if ( $original_activity->user_id != $commenter_id && 'no' != bp_get_user_meta( $original_activity->user_id, 'notification_activity_new_reply', true ) ) {
  126. $poster_name = bp_core_get_user_displayname( $commenter_id );
  127. $thread_link = bp_activity_get_permalink( $activity_id );
  128. $settings_slug = function_exists( 'bp_get_settings_slug' ) ? bp_get_settings_slug() : 'settings';
  129. $settings_link = bp_core_get_user_domain( $original_activity->user_id ) . $settings_slug . '/notifications/';
  130. $poster_name = stripslashes( $poster_name );
  131. $content = bp_activity_filter_kses( stripslashes($content) );
  132. // Set up and send the message
  133. $ud = bp_core_get_core_userdata( $original_activity->user_id );
  134. $to = $ud->user_email;
  135. $subject = bp_get_email_subject( array( 'text' => sprintf( __( '%s replied to one of your updates', 'buddypress' ), $poster_name ) ) );
  136. $message = sprintf( __(
  137. '%1$s replied to one of your updates:
  138. "%2$s"
  139. To view your original update and all comments, log in and visit: %3$s
  140. ---------------------
  141. ', 'buddypress' ), $poster_name, $content, $thread_link );
  142. // Only show the disable notifications line if the settings component is enabled
  143. if ( bp_is_active( 'settings' ) ) {
  144. $message .= sprintf( __( 'To disable these notifications please log in and go to: %s', 'buddypress' ), $settings_link );
  145. }
  146. /* Send the message */
  147. $to = apply_filters( 'bp_activity_new_comment_notification_to', $to );
  148. $subject = apply_filters( 'bp_activity_new_comment_notification_subject', $subject, $poster_name );
  149. $message = apply_filters( 'bp_activity_new_comment_notification_message', $message, $poster_name, $content, $thread_link, $settings_link );
  150. wp_mail( $to, $subject, $message );
  151. do_action( 'bp_activity_sent_reply_to_update_email', $original_activity->user_id, $subject, $message, $comment_id, $commenter_id, $params );
  152. }
  153. /***
  154. * If this is a reply to another comment, send an email notification to the
  155. * author of the immediate parent comment.
  156. */
  157. if ( empty( $parent_id ) || ( $activity_id == $parent_id ) )
  158. return false;
  159. $parent_comment = new BP_Activity_Activity( $parent_id );
  160. if ( $parent_comment->user_id != $commenter_id && $original_activity->user_id != $parent_comment->user_id && 'no' != bp_get_user_meta( $parent_comment->user_id, 'notification_activity_new_reply', true ) ) {
  161. $poster_name = bp_core_get_user_displayname( $commenter_id );
  162. $thread_link = bp_activity_get_permalink( $activity_id );
  163. $settings_slug = function_exists( 'bp_get_settings_slug' ) ? bp_get_settings_slug() : 'settings';
  164. $settings_link = bp_core_get_user_domain( $parent_comment->user_id ) . $settings_slug . '/notifications/';
  165. // Set up and send the message
  166. $ud = bp_core_get_core_userdata( $parent_comment->user_id );
  167. $to = $ud->user_email;
  168. $subject = bp_get_email_subject( array( 'text' => sprintf( __( '%s replied to one of your comments', 'buddypress' ), $poster_name ) ) );
  169. $poster_name = stripslashes( $poster_name );
  170. $content = bp_activity_filter_kses( stripslashes( $content ) );
  171. $message = sprintf( __(
  172. '%1$s replied to one of your comments:
  173. "%2$s"
  174. To view the original activity, your comment and all replies, log in and visit: %3$s
  175. ---------------------
  176. ', 'buddypress' ), $poster_name, $content, $thread_link );
  177. // Only show the disable notifications line if the settings component is enabled
  178. if ( bp_is_active( 'settings' ) ) {
  179. $message .= sprintf( __( 'To disable these notifications please log in and go to: %s', 'buddypress' ), $settings_link );
  180. }
  181. /* Send the message */
  182. $to = apply_filters( 'bp_activity_new_comment_notification_comment_author_to', $to );
  183. $subject = apply_filters( 'bp_activity_new_comment_notification_comment_author_subject', $subject, $poster_name );
  184. $message = apply_filters( 'bp_activity_new_comment_notification_comment_author_message', $message, $poster_name, $content, $settings_link, $thread_link );
  185. wp_mail( $to, $subject, $message );
  186. do_action( 'bp_activity_sent_reply_to_reply_email', $original_activity->user_id, $subject, $message, $comment_id, $commenter_id, $params );
  187. }
  188. }