<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head><meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>[BuddyPress][7622] trunk/bp-activity: Refactor Activity component's approach to Notifications integration:</title>
</head>
<body>

<style type="text/css"><!--
#msg dl.meta { border: 1px #006 solid; background: #369; padding: 6px; color: #fff; }
#msg dl.meta dt { float: left; width: 6em; font-weight: bold; }
#msg dt:after { content:':';}
#msg dl, #msg dt, #msg ul, #msg li, #header, #footer, #logmsg { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt;  }
#msg dl a { font-weight: bold}
#msg dl a:link    { color:#fc3; }
#msg dl a:active  { color:#ff0; }
#msg dl a:visited { color:#cc6; }
h3 { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt; font-weight: bold; }
#msg pre { overflow: auto; background: #ffc; border: 1px #fa0 solid; padding: 6px; }
#logmsg { background: #ffc; border: 1px #fa0 solid; padding: 1em 1em 0 1em; }
#logmsg p, #logmsg pre, #logmsg blockquote { margin: 0 0 1em 0; }
#logmsg p, #logmsg li, #logmsg dt, #logmsg dd { line-height: 14pt; }
#logmsg h1, #logmsg h2, #logmsg h3, #logmsg h4, #logmsg h5, #logmsg h6 { margin: .5em 0; }
#logmsg h1:first-child, #logmsg h2:first-child, #logmsg h3:first-child, #logmsg h4:first-child, #logmsg h5:first-child, #logmsg h6:first-child { margin-top: 0; }
#logmsg ul, #logmsg ol { padding: 0; list-style-position: inside; margin: 0 0 0 1em; }
#logmsg ul { text-indent: -1em; padding-left: 1em; }#logmsg ol { text-indent: -1.5em; padding-left: 1.5em; }
#logmsg > ul, #logmsg > ol { margin: 0 0 1em 0; }
#logmsg pre { background: #eee; padding: 1em; }
#logmsg blockquote { border: 1px solid #fa0; border-left-width: 10px; padding: 1em 1em 0 1em; background: white;}
#logmsg dl { margin: 0; }
#logmsg dt { font-weight: bold; }
#logmsg dd { margin: 0; padding: 0 0 0.5em 0; }
#logmsg dd:before { content:'\00bb';}
#logmsg table { border-spacing: 0px; border-collapse: collapse; border-top: 4px solid #fa0; border-bottom: 1px solid #fa0; background: #fff; }
#logmsg table th { text-align: left; font-weight: normal; padding: 0.2em 0.5em; border-top: 1px dotted #fa0; }
#logmsg table td { text-align: right; border-top: 1px dotted #fa0; padding: 0.2em 0.5em; }
#logmsg table thead th { text-align: center; border-bottom: 1px solid #fa0; }
#logmsg table th.Corner { text-align: left; }
#logmsg hr { border: none 0; border-top: 2px dashed #fa0; height: 1px; }
#header, #footer { color: #fff; background: #636; border: 1px #300 solid; padding: 6px; }
#patch { width: 100%; }
#patch h4 {font-family: verdana,arial,helvetica,sans-serif;font-size:10pt;padding:8px;background:#369;color:#fff;margin:0;}
#patch .propset h4, #patch .binary h4 {margin:0;}
#patch pre {padding:0;line-height:1.2em;margin:0;}
#patch .diff {width:100%;background:#eee;padding: 0 0 10px 0;overflow:auto;}
#patch .propset .diff, #patch .binary .diff  {padding:10px 0;}
#patch span {display:block;padding:0 10px;}
#patch .modfile, #patch .addfile, #patch .delfile, #patch .propset, #patch .binary, #patch .copfile {border:1px solid #ccc;margin:10px 0;}
#patch ins {background:#dfd;text-decoration:none;display:block;padding:0 10px;}
#patch del {background:#fdd;text-decoration:none;display:block;padding:0 10px;}
#patch .lines, .info {color:#888;background:#fff;}
--></style>
<div id="msg">
<dl class="meta">
<dt>Revision</dt> <dd><a href="http://buddypress.trac.wordpress.org/changeset/7622">7622</a></dd>
<dt>Author</dt> <dd>johnjamesjacoby</dd>
<dt>Date</dt> <dd>2013-11-30 05:13:05 +0000 (Sat, 30 Nov 2013)</dd>
</dl>

<h3>Log Message</h3>
<pre>Refactor Activity component's approach to Notifications integration:

* Introduce helper functions for handling the adding/marking/deleting of notifications. Hook these new functions into their respective actions rather than have them hardcoded and interspersed amongst the first-class code.
* See <a href="http://buddypress.trac.wordpress.org/ticket/5266">#5266</a>.</pre>

<h3>Modified Paths</h3>
<ul>
<li><a href="#trunkbpactivitybpactivityfunctionsphp">trunk/bp-activity/bp-activity-functions.php</a></li>
<li><a href="#trunkbpactivitybpactivitynotificationsphp">trunk/bp-activity/bp-activity-notifications.php</a></li>
<li><a href="#trunkbpactivitybpactivityscreensphp">trunk/bp-activity/bp-activity-screens.php</a></li>
</ul>

</div>
<div id="patch">
<h3>Diff</h3>
<a id="trunkbpactivitybpactivityfunctionsphp"></a>
<div class="modfile"><h4>Modified: trunk/bp-activity/bp-activity-functions.php (7621 => 7622)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/bp-activity/bp-activity-functions.php      2013-11-30 04:44:58 UTC (rev 7621)
+++ trunk/bp-activity/bp-activity-functions.php 2013-11-30 05:13:05 UTC (rev 7622)
</span><span class="lines">@@ -191,59 +191,6 @@
</span><span class="cx"> }
</span><span class="cx"> 
</span><span class="cx"> /**
</span><del>- * Format notifications related to activity.
- *
- * @since BuddyPress (1.5)
- *
- * @uses bp_loggedin_user_domain()
- * @uses bp_get_activity_slug()
- * @uses bp_core_get_user_displayname()
- * @uses apply_filters() To call the 'bp_activity_multiple_at_mentions_notification' hook.
- * @uses apply_filters() To call the 'bp_activity_single_at_mentions_notification' hook.
- * @uses do_action() To call 'activity_format_notifications' hook.
- *
- * @param string $action The type of activity item. Just 'new_at_mention' for now.
- * @param int $item_id The activity ID.
- * @param int $secondary_item_id In the case of at-mentions, this is the mentioner's ID.
- * @param int $total_items The total number of notifications to format.
- * @param string $format 'string' to get a BuddyBar-compatible notification, 'array' otherwise.
- * @return string $return Formatted @mention notification.
- */
-function bp_activity_format_notifications( $action, $item_id, $secondary_item_id, $total_items, $format = 'string' ) {
-
-       switch ( $action ) {
-               case 'new_at_mention':
-                       $activity_id      = $item_id;
-                       $poster_user_id   = $secondary_item_id;
-                       $at_mention_link  = bp_loggedin_user_domain() . bp_get_activity_slug() . '/mentions/';
-                       $at_mention_title = sprintf( __( '@%s Mentions', 'buddypress' ), bp_get_loggedin_user_username() );
-
-                       if ( (int) $total_items > 1 ) {
-                               $text = sprintf( __( 'You have %1$d new mentions', 'buddypress' ), (int) $total_items );
-                               $filter = 'bp_activity_multiple_at_mentions_notification';
-                       } else {
-                               $user_fullname = bp_core_get_user_displayname( $poster_user_id );
-                               $text =  sprintf( __( '%1$s mentioned you', 'buddypress' ), $user_fullname );
-                               $filter = 'bp_activity_single_at_mentions_notification';
-                       }
-               break;
-       }
-
-       if ( 'string' == $format ) {
-               $return = apply_filters( $filter, '<a href="' . $at_mention_link . '" title="' . $at_mention_title . '">' . $text . '</a>', $at_mention_link, (int) $total_items, $activity_id, $poster_user_id );
-       } else {
-               $return = apply_filters( $filter, array(
-                       'text' => $text,
-                       'link' => $at_mention_link
-               ), $at_mention_link, (int) $total_items, $activity_id, $poster_user_id );
-       }
-
-       do_action( 'activity_format_notifications', $action, $item_id, $secondary_item_id, $total_items );
-
-       return $return;
-}
-
-/**
</del><span class="cx">  * Determine a user's "mentionname", the name used for that user in @-mentions.
</span><span class="cx">  *
</span><span class="cx">  * @since BuddyPress (1.9.0)
</span><span class="lines">@@ -1266,27 +1213,29 @@
</span><span class="cx">  * @return int|bool The ID of the comment on success, otherwise false.
</span><span class="cx">  */
</span><span class="cx"> function bp_activity_new_comment( $args = '' ) {
</span><del>-       global $bp;
</del><span class="cx"> 
</span><del>-       $defaults = array(
</del><ins>+        $params = wp_parse_args( $args, array(
</ins><span class="cx">           'id'          => false,
</span><span class="cx">          'content'     => false,
</span><span class="cx">          'user_id'     => bp_loggedin_user_id(),
</span><span class="cx">          'activity_id' => false, // ID of the root activity item
</span><span class="cx">          'parent_id'   => false  // ID of a parent comment (optional)
</span><del>-       );
</del><ins>+        ) );
</ins><span class="cx"> 
</span><del>-       $params = wp_parse_args( $args, $defaults );
</del><span class="cx">   extract( $params, EXTR_SKIP );
</span><span class="cx"> 
</span><del>-       if ( empty( $content ) || empty( $user_id ) || empty( $activity_id ) )
</del><ins>+        // Bail if missing necessary data
+       if ( empty( $content ) || empty( $user_id ) || empty( $activity_id ) ) {
</ins><span class="cx">           return false;
</span><ins>+       }
</ins><span class="cx"> 
</span><del>-       if ( empty( $parent_id ) )
</del><ins>+        // Maybe set current activity ID as the parent
+       if ( empty( $parent_id ) ) {
</ins><span class="cx">           $parent_id = $activity_id;
</span><ins>+       }
</ins><span class="cx"> 
</span><span class="cx">  // Check to see if the parent activity is hidden, and if so, hide this comment publically.
</span><del>-       $activity = new BP_Activity_Activity( $activity_id );
</del><ins>+        $activity  = new BP_Activity_Activity( $activity_id );
</ins><span class="cx">   $is_hidden = ( (int) $activity->hide_sitewide ) ? 1 : 0;
</span><span class="cx"> 
</span><span class="cx">  // Insert the activity comment
</span><span class="lines">@@ -1294,7 +1243,7 @@
</span><span class="cx">          'id'                => $id,
</span><span class="cx">          'action'            => apply_filters( 'bp_activity_comment_action', sprintf( __( '%s posted a new activity comment', 'buddypress' ), bp_core_get_userlink( $user_id ) ) ),
</span><span class="cx">          'content'           => apply_filters( 'bp_activity_comment_content', $content ),
</span><del>-               'component'         => $bp->activity->id,
</del><ins>+                'component'         => buddypress()->activity->id,
</ins><span class="cx">           'type'              => 'activity_comment',
</span><span class="cx">          'user_id'           => $user_id,
</span><span class="cx">          'item_id'           => $activity_id,
</span><span class="lines">@@ -1302,9 +1251,6 @@
</span><span class="cx">          'hide_sitewide'     => $is_hidden
</span><span class="cx">  ) );
</span><span class="cx"> 
</span><del>-       // Send an email notification if settings allow
-       bp_activity_new_comment_notification( $comment_id, $user_id, $params );
-
</del><span class="cx">   // Clear the comment cache for this activity
</span><span class="cx">  wp_cache_delete( 'bp_activity_comments_' . $parent_id );
</span><span class="cx"> 
</span></span></pre></div>
<a id="trunkbpactivitybpactivitynotificationsphp"></a>
<div class="modfile"><h4>Modified: trunk/bp-activity/bp-activity-notifications.php (7621 => 7622)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/bp-activity/bp-activity-notifications.php  2013-11-30 04:44:58 UTC (rev 7621)
+++ trunk/bp-activity/bp-activity-notifications.php     2013-11-30 05:13:05 UTC (rev 7622)
</span><span class="lines">@@ -10,12 +10,14 @@
</span><span class="cx"> // Exit if accessed directly
</span><span class="cx"> if ( !defined( 'ABSPATH' ) ) exit;
</span><span class="cx"> 
</span><ins>+/* Emails *********************************************************************/
+
</ins><span class="cx"> /**
</span><span class="cx">  * Send email and BP notifications when a user is mentioned in an update.
</span><span class="cx">  *
</span><span class="cx">  * @since BuddyPress (1.2)
</span><span class="cx">  *
</span><del>- * @uses bp_core_add_notification()
</del><ins>+ * @uses bp_notifications_add_notification()
</ins><span class="cx">  * @uses bp_get_user_meta()
</span><span class="cx">  * @uses bp_core_get_user_displayname()
</span><span class="cx">  * @uses bp_activity_get_permalink()
</span><span class="lines">@@ -54,9 +56,6 @@
</span><span class="cx">  $message = '';
</span><span class="cx">  $content = '';
</span><span class="cx"> 
</span><del>-       // Add the BP notification
-       bp_core_add_notification( $activity_id, $receiver_user_id, 'activity', 'new_at_mention', $activity->user_id );
-
</del><span class="cx">   // Now email the user with the contents of the message (if they have enabled email notifications)
</span><span class="cx">  if ( 'no' != bp_get_user_meta( $receiver_user_id, 'notification_activity_new_mention', true ) ) {
</span><span class="cx">          $poster_name = bp_core_get_user_displayname( $activity->user_id );
</span><span class="lines">@@ -100,7 +99,7 @@
</span><span class="cx">                  $message .= sprintf( __( 'To disable these notifications please log in and go to: %s', 'buddypress' ), $settings_link );
</span><span class="cx">          }
</span><span class="cx"> 
</span><del>-               /* Send the message */
</del><ins>+                // Send the message
</ins><span class="cx">           $to      = apply_filters( 'bp_activity_at_message_notification_to', $to );
</span><span class="cx">          $subject = apply_filters( 'bp_activity_at_message_notification_subject', $subject, $poster_name );
</span><span class="cx">          $message = apply_filters( 'bp_activity_at_message_notification_message', $message, $poster_name, $content, $message_link, $settings_link );
</span><span class="lines">@@ -108,7 +107,7 @@
</span><span class="cx">          wp_mail( $to, $subject, $message );
</span><span class="cx">  }
</span><span class="cx"> 
</span><del>-       do_action( 'bp_activity_sent_mention_email', $activity, $subject, $message, $content );
</del><ins>+        do_action( 'bp_activity_sent_mention_email', $activity, $subject, $message, $content, $receiver_user_id );
</ins><span class="cx"> }
</span><span class="cx"> 
</span><span class="cx"> /**
</span><span class="lines">@@ -140,7 +139,7 @@
</span><span class="cx">  * @param int $commenter_id The ID of the user who posted the comment.
</span><span class="cx">  * @param array $params {@link bp_activity_new_comment()}
</span><span class="cx">  */
</span><del>-function bp_activity_new_comment_notification( $comment_id, $commenter_id, $params ) {
</del><ins>+function bp_activity_new_comment_notification( $comment_id = 0, $commenter_id = 0, $params = array() ) {
</ins><span class="cx"> 
</span><span class="cx">  // Set some default parameters
</span><span class="cx">  $activity_id = 0;
</span><span class="lines">@@ -192,8 +191,9 @@
</span><span class="cx">   * If this is a reply to another comment, send an email notification to the
</span><span class="cx">   * author of the immediate parent comment.
</span><span class="cx">   */
</span><del>-       if ( empty( $parent_id ) || ( $activity_id == $parent_id ) )
</del><ins>+        if ( empty( $parent_id ) || ( $activity_id == $parent_id ) ) {
</ins><span class="cx">           return false;
</span><ins>+       }
</ins><span class="cx"> 
</span><span class="cx">  $parent_comment = new BP_Activity_Activity( $parent_id );
</span><span class="cx"> 
</span><span class="lines">@@ -236,3 +236,118 @@
</span><span class="cx">          do_action( 'bp_activity_sent_reply_to_reply_email', $original_activity->user_id, $subject, $message, $comment_id, $commenter_id, $params );
</span><span class="cx">  }
</span><span class="cx"> }
</span><ins>+
+/**
+ * Helper method to map action arguments to function parameters
+ *
+ * @since BuddyPress (1.9.0)
+ * @param int $comment_id
+ * @param array $params
+ */
+function bp_activity_new_comment_notification_helper( $comment_id, $params ) {
+       bp_activity_new_comment_notification( $comment_id, $params['user_id'], $params );
+}
+add_action( 'bp_activity_comment_posted', 'bp_activity_new_comment_notification_helper', 10, 2 );
+
+/** Notifications *************************************************************/
+
+/**
+ * Format notifications related to activity.
+ *
+ * @since BuddyPress (1.5)
+ *
+ * @uses bp_loggedin_user_domain()
+ * @uses bp_get_activity_slug()
+ * @uses bp_core_get_user_displayname()
+ * @uses apply_filters() To call the 'bp_activity_multiple_at_mentions_notification' hook.
+ * @uses apply_filters() To call the 'bp_activity_single_at_mentions_notification' hook.
+ * @uses do_action() To call 'activity_format_notifications' hook.
+ *
+ * @param string $action The type of activity item. Just 'new_at_mention' for now.
+ * @param int $item_id The activity ID.
+ * @param int $secondary_item_id In the case of at-mentions, this is the mentioner's ID.
+ * @param int $total_items The total number of notifications to format.
+ * @param string $format 'string' to get a BuddyBar-compatible notification, 'array' otherwise.
+ * @return string $return Formatted @mention notification.
+ */
+function bp_activity_format_notifications( $action, $item_id, $secondary_item_id, $total_items, $format = 'string' ) {
+
+       switch ( $action ) {
+               case 'new_at_mention':
+                       $activity_id      = $item_id;
+                       $poster_user_id   = $secondary_item_id;
+                       $at_mention_link  = bp_loggedin_user_domain() . bp_get_activity_slug() . '/mentions/';
+                       $at_mention_title = sprintf( __( '@%s Mentions', 'buddypress' ), bp_get_loggedin_user_username() );
+
+                       if ( (int) $total_items > 1 ) {
+                               $text = sprintf( __( 'You have %1$d new mentions', 'buddypress' ), (int) $total_items );
+                               $filter = 'bp_activity_multiple_at_mentions_notification';
+                       } else {
+                               $user_fullname = bp_core_get_user_displayname( $poster_user_id );
+                               $text =  sprintf( __( '%1$s mentioned you', 'buddypress' ), $user_fullname );
+                               $filter = 'bp_activity_single_at_mentions_notification';
+                       }
+               break;
+       }
+
+       if ( 'string' == $format ) {
+               $return = apply_filters( $filter, '<a href="' . esc_url( $at_mention_link ) . '" title="' . esc_attr( $at_mention_title ) . '">' . esc_html( $text ) . '</a>', $at_mention_link, (int) $total_items, $activity_id, $poster_user_id );
+       } else {
+               $return = apply_filters( $filter, array(
+                       'text' => $text,
+                       'link' => $at_mention_link
+               ), $at_mention_link, (int) $total_items, $activity_id, $poster_user_id );
+       }
+
+       do_action( 'activity_format_notifications', $action, $item_id, $secondary_item_id, $total_items );
+
+       return $return;
+}
+
+/**
+ * Notify a member when their nicename is mentioned in an activity stream item.
+ *
+ * Hooked to the 'bp_activity_sent_mention_email' action, we piggy back off the
+ * existing email code for now, since it does the heavy lifting for us. In the
+ * future when we separate emails from Notifications, this will need its own
+ * 'bp_activity_at_name_send_emails' equivalent helper function.
+ *
+ * @since BuddyPress (1.9.0)
+ *
+ * @param obj $activity
+ * @param string $subject (not used)
+ * @param string $message (not used)
+ * @param string $content (not used)
+ * @param int $receiver_user_id
+ */
+function bp_activity_at_mention_add_notification( $activity, $subject, $message, $content, $receiver_user_id ) {
+       if ( bp_is_active( 'notifications' ) ) {
+               bp_notifications_add_notification( array(
+                       'user_id'           => $receiver_user_id,
+                       'item_id'           => $activity->id,
+                       'secondary_item_id' => $activity->user_id,
+                       'component_name'    => buddypress()->activity->id,
+                       'component_action'  => 'new_at_mention',
+                       'date_notified'     => bp_core_current_time(),
+                       'is_new'            => 1,
+               ) );
+       }
+}
+add_action( 'bp_activity_sent_mention_email', 'bp_activity_at_mention_add_notification', 10, 5 );
+
+/**
+ * Remove activity notifications when a user clicks on them.
+ *
+ * @since BuddyPress (1.5)
+ *
+ * @uses bp_notifications_mark_all_notifications_by_type()
+ */
+function bp_activity_remove_screen_notifications() {
+       if ( bp_is_active( 'notifications' ) ) {
+               bp_notifications_mark_notifications_by_type( bp_loggedin_user_id(), buddypress()->activity->id, 'new_at_mention' );
+       }
+}
+add_action( 'bp_activity_screen_my_activity',               'bp_activity_remove_screen_notifications' );
+add_action( 'bp_activity_screen_single_activity_permalink', 'bp_activity_remove_screen_notifications' );
+add_action( 'bp_activity_screen_mentions',                  'bp_activity_remove_screen_notifications' );
+
</ins></span></pre></div>
<a id="trunkbpactivitybpactivityscreensphp"></a>
<div class="modfile"><h4>Modified: trunk/bp-activity/bp-activity-screens.php (7621 => 7622)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/bp-activity/bp-activity-screens.php        2013-11-30 04:44:58 UTC (rev 7621)
+++ trunk/bp-activity/bp-activity-screens.php   2013-11-30 05:13:05 UTC (rev 7622)
</span><span class="lines">@@ -129,20 +129,6 @@
</span><span class="cx"> }
</span><span class="cx"> 
</span><span class="cx"> /**
</span><del>- * Remove activity notifications when a user clicks on them.
- *
- * @since BuddyPress (1.5)
- *
- * @uses bp_core_mark_all_notifications_by_type()
- */
-function bp_activity_remove_screen_notifications() {
-       bp_core_mark_notifications_by_type( bp_loggedin_user_id(), buddypress()->activity->id, 'new_at_mention' );
-}
-add_action( 'bp_activity_screen_my_activity',               'bp_activity_remove_screen_notifications' );
-add_action( 'bp_activity_screen_single_activity_permalink', 'bp_activity_remove_screen_notifications' );
-add_action( 'bp_activity_screen_mentions',                  'bp_activity_remove_screen_notifications' );
-
-/**
</del><span class="cx">  * Reset the logged-in user's new mentions data when he visits his mentions screen.
</span><span class="cx">  *
</span><span class="cx">  * @since BuddyPress (1.5)
</span></span></pre>
</div>
</div>

</body>
</html>