<!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][7619] trunk/bp-friends: Refactor Friends 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/7619">7619</a></dd>
<dt>Author</dt> <dd>johnjamesjacoby</dd>
<dt>Date</dt> <dd>2013-11-30 03:47:27 +0000 (Sat, 30 Nov 2013)</dd>
</dl>

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

* Relocate `friends_clear_friendship_notifications()` out of `bp-friends-cache.php` and into `bp-friends-notifications.php`
* 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>. Hat-tip r-a-y.</pre>

<h3>Modified Paths</h3>
<ul>
<li><a href="#trunkbpfriendsbpfriendsactivityphp">trunk/bp-friends/bp-friends-activity.php</a></li>
<li><a href="#trunkbpfriendsbpfriendscachephp">trunk/bp-friends/bp-friends-cache.php</a></li>
<li><a href="#trunkbpfriendsbpfriendsfunctionsphp">trunk/bp-friends/bp-friends-functions.php</a></li>
<li><a href="#trunkbpfriendsbpfriendsnotificationsphp">trunk/bp-friends/bp-friends-notifications.php</a></li>
<li><a href="#trunkbpfriendsbpfriendsscreensphp">trunk/bp-friends/bp-friends-screens.php</a></li>
</ul>

</div>
<div id="patch">
<h3>Diff</h3>
<a id="trunkbpfriendsbpfriendsactivityphp"></a>
<div class="modfile"><h4>Modified: trunk/bp-friends/bp-friends-activity.php (7618 => 7619)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/bp-friends/bp-friends-activity.php 2013-11-29 20:35:18 UTC (rev 7618)
+++ trunk/bp-friends/bp-friends-activity.php    2013-11-30 03:47:27 UTC (rev 7619)
</span><span class="lines">@@ -32,28 +32,25 @@
</span><span class="cx">  * @return bool See {@link bp_activity_add()}.
</span><span class="cx">  */
</span><span class="cx"> function friends_record_activity( $args = '' ) {
</span><del>-       global $bp;
</del><span class="cx"> 
</span><del>-       if ( !bp_is_active( 'activity' ) )
</del><ins>+        if ( ! bp_is_active( 'activity' ) ) {
</ins><span class="cx">           return false;
</span><ins>+       }
</ins><span class="cx"> 
</span><del>-       $defaults = array (
</del><ins>+        $r = wp_parse_args( $args, array(
</ins><span class="cx">           'user_id'           => bp_loggedin_user_id(),
</span><span class="cx">          'action'            => '',
</span><span class="cx">          'content'           => '',
</span><span class="cx">          'primary_link'      => '',
</span><del>-               'component'         => $bp->friends->id,
</del><ins>+                'component'         => buddypress()->friends->id,
</ins><span class="cx">           'type'              => false,
</span><span class="cx">          'item_id'           => false,
</span><span class="cx">          'secondary_item_id' => false,
</span><span class="cx">          'recorded_time'     => bp_core_current_time(),
</span><span class="cx">          'hide_sitewide'     => false
</span><del>-       );
</del><ins>+        ) );
</ins><span class="cx"> 
</span><del>-       $r = wp_parse_args( $args, $defaults );
-       extract( $r, EXTR_SKIP );
-
-       return bp_activity_add( array( 'user_id' => $user_id, 'action' => $action, 'content' => $content, 'primary_link' => $primary_link, 'component' => $component, 'type' => $type, 'item_id' => $item_id, 'secondary_item_id' => $secondary_item_id, 'recorded_time' => $recorded_time, 'hide_sitewide' => $hide_sitewide ) );
</del><ins>+        return bp_activity_add( $r );
</ins><span class="cx"> }
</span><span class="cx"> 
</span><span class="cx"> /**
</span><span class="lines">@@ -71,26 +68,32 @@
</span><span class="cx">  * @return bool True on success, false on failure.
</span><span class="cx">  */
</span><span class="cx"> function friends_delete_activity( $args ) {
</span><del>-       global $bp;
</del><ins>+        if ( ! bp_is_active( 'activity' ) ) {
+               return;
+       }
</ins><span class="cx"> 
</span><del>-       if ( bp_is_active( 'activity' ) ) {
-               extract( (array) $args );
-               bp_activity_delete_by_item_id( array( 'item_id' => $item_id, 'component' => $bp->friends->id, 'type' => $type, 'user_id' => $user_id ) );
-       }
</del><ins>+        bp_activity_delete_by_item_id( array(
+               'component' => buddypress()->friends->id,
+               'item_id'   => $args['item_id'],
+               'type'      => $args['type'],
+               'user_id'   => $args['user_id']
+       ) );
</ins><span class="cx"> }
</span><span class="cx"> 
</span><span class="cx"> /**
</span><span class="cx">  * Register the activity actions for bp-friends.
</span><span class="cx">  */
</span><span class="cx"> function friends_register_activity_actions() {
</span><del>-       global $bp;
</del><span class="cx"> 
</span><del>-       if ( !bp_is_active( 'activity' ) )
</del><ins>+        if ( !bp_is_active( 'activity' ) ) {
</ins><span class="cx">           return false;
</span><ins>+       }
</ins><span class="cx"> 
</span><ins>+       $bp = buddypress();
+
</ins><span class="cx">   // These two added in BP 1.6
</span><span class="cx">  bp_activity_set_action( $bp->friends->id, 'friendship_accepted', __( 'Friendships accepted', 'buddypress' ) );
</span><del>-       bp_activity_set_action( $bp->friends->id, 'friendship_created', __( 'New friendships', 'buddypress' ) );
</del><ins>+        bp_activity_set_action( $bp->friends->id, 'friendship_created',  __( 'New friendships',      'buddypress' ) );
</ins><span class="cx"> 
</span><span class="cx">  // < BP 1.6 backpat
</span><span class="cx">  bp_activity_set_action( $bp->friends->id, 'friends_register_activity_action', __( 'New friendship created', 'buddypress' ) );
</span><span class="lines">@@ -100,60 +103,44 @@
</span><span class="cx"> add_action( 'bp_register_activity_actions', 'friends_register_activity_actions' );
</span><span class="cx"> 
</span><span class="cx"> /**
</span><del>- * Notification formatting callback for bp-friends notifications.
</del><ins>+ * Add activity stream items when one members accepts another members request
+ * for virtual friendship.
</ins><span class="cx">  *
</span><del>- * @param string $action The kind of notification being rendered.
- * @param int $item_id The primary item ID.
- * @param int $secondary_item_id The secondary item ID.
- * @param int $total_items The total number of messaging-related notifications
- *        waiting for the user.
- * @param string $format 'string' for BuddyBar-compatible notifications;
- *        'array' for WP Toolbar. Default: 'string'.
- * @return array|string
</del><ins>+ * @since BuddyPress (1.9.0)
+ *
+ * @param int $friendship_id
+ * @param int $initiator_user_id
+ * @param int $friend_user_id
+ * @param object $friendship
</ins><span class="cx">  */
</span><del>-function friends_format_notifications( $action, $item_id, $secondary_item_id, $total_items, $format = 'string' ) {
</del><ins>+function bp_friends_friendship_accepted_activity( $friendship_id, $initiator_user_id, $friend_user_id, $friendship ) {
</ins><span class="cx"> 
</span><del>-       switch ( $action ) {
-               case 'friendship_accepted':
-                       $link = trailingslashit( bp_loggedin_user_domain() . bp_get_friends_slug() . '/my-friends' );
-
-                       // Set up the string and the filter
-                       if ( (int) $total_items > 1 ) {
-                               $text = sprintf( __( '%d friends accepted your friendship requests', 'buddypress' ), (int) $total_items );
-                               $filter = 'bp_friends_multiple_friendship_accepted_notification';
-                       } else {
-                               $text = sprintf( __( '%s accepted your friendship request', 'buddypress' ),  bp_core_get_user_displayname( $item_id ) );
-                               $filter = 'bp_friends_single_friendship_accepted_notification';
-                       }
-
-                       break;
-
-               case 'friendship_request':
-                       $link = bp_loggedin_user_domain() . bp_get_friends_slug() . '/requests/?new';
-
-                       // Set up the string and the filter
-                       if ( (int) $total_items > 1 ) {
-                               $text = sprintf( __( 'You have %d pending friendship requests', 'buddypress' ), (int) $total_items );
-                               $filter = 'bp_friends_multiple_friendship_request_notification';
-                       } else {
-                               $text = sprintf( __( 'You have a friendship request from %s', 'buddypress' ),  bp_core_get_user_displayname( $item_id ) );
-                               $filter = 'bp_friends_single_friendship_request_notification';
-                       }
-
-                       break;
</del><ins>+        // Bail if Activity component is not active
+       if ( ! bp_is_active( 'activity' ) ) {
+               return;
</ins><span class="cx">   }
</span><span class="cx"> 
</span><del>-       // Return either an HTML link or an array, depending on the requested format
-       if ( 'string' == $format ) {
-               $return = apply_filters( $filter, '<a href="' . $link . '">' . $text . '</a>', (int) $total_items );
-       } else {
-               $return = apply_filters( $filter, array(
-                       'link' => $link,
-                       'text' => $text
-               ), (int) $total_items );
-       }
</del><ins>+        // Get links to both members profiles
+       $initiator_link = bp_core_get_userlink( $initiator_user_id );
+       $friend_link    = bp_core_get_userlink( $friend_user_id    );
</ins><span class="cx"> 
</span><del>-       do_action( 'friends_format_notifications', $action, $item_id, $secondary_item_id, $total_items, $return );
</del><ins>+        // Record in activity streams for the initiator
+       friends_record_activity( array(
+               'user_id'           => $initiator_user_id,
+               'type'              => 'friendship_created',
+               'action'            => apply_filters( 'friends_activity_friendship_accepted_action', sprintf( __( '%1$s and %2$s are now friends', 'buddypress' ), $initiator_link, $friend_link ), $friendship ),
+               'item_id'           => $friendship_id,
+               'secondary_item_id' => $friend_user_id
+       ) );
</ins><span class="cx"> 
</span><del>-       return $return;
</del><ins>+        // Record in activity streams for the friend
+       friends_record_activity( array(
+               'user_id'           => $friend_user_id,
+               'type'              => 'friendship_created',
+               'action'            => apply_filters( 'friends_activity_friendship_accepted_action', sprintf( __( '%1$s and %2$s are now friends', 'buddypress' ), $friend_link, $initiator_link ), $friendship ),
+               'item_id'           => $friendship_id,
+               'secondary_item_id' => $initiator_user_id,
+               'hide_sitewide'     => true // We've already got the first entry site wide
+       ) );
</ins><span class="cx"> }
</span><ins>+add_action( 'friends_friendship_accepted', 'bp_friends_friendship_accepted_activity', 10, 4 );
</ins></span></pre></div>
<a id="trunkbpfriendsbpfriendscachephp"></a>
<div class="modfile"><h4>Modified: trunk/bp-friends/bp-friends-cache.php (7618 => 7619)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/bp-friends/bp-friends-cache.php    2013-11-29 20:35:18 UTC (rev 7618)
+++ trunk/bp-friends/bp-friends-cache.php       2013-11-30 03:47:27 UTC (rev 7619)
</span><span class="lines">@@ -10,7 +10,6 @@
</span><span class="cx">  * @subpackage FriendsCaching
</span><span class="cx">  */
</span><span class="cx"> 
</span><del>-
</del><span class="cx"> // Exit if accessed directly
</span><span class="cx"> if ( !defined( 'ABSPATH' ) ) exit;
</span><span class="cx"> 
</span><span class="lines">@@ -28,16 +27,6 @@
</span><span class="cx">  wp_cache_delete( 'friends_friend_ids_' .    $friendship->friend_user_id,    'bp' );
</span><span class="cx"> }
</span><span class="cx"> 
</span><del>-/**
- * Clear friend-related notifications when ?new=1.
- */
-function friends_clear_friend_notifications() {
-       if ( isset( $_GET['new'] ) ) {
-               bp_core_mark_notifications_by_type( bp_loggedin_user_id(), buddypress()->friends->id, 'friendship_accepted' );
-       }
-}
-add_action( 'bp_activity_screen_my_activity', 'friends_clear_friend_notifications' );
-
</del><span class="cx"> // List actions to clear object caches on
</span><span class="cx"> add_action( 'friends_friendship_accepted', 'friends_clear_friend_object_cache' );
</span><span class="cx"> add_action( 'friends_friendship_deleted',  'friends_clear_friend_object_cache' );
</span></span></pre></div>
<a id="trunkbpfriendsbpfriendsfunctionsphp"></a>
<div class="modfile"><h4>Modified: trunk/bp-friends/bp-friends-functions.php (7618 => 7619)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/bp-friends/bp-friends-functions.php        2013-11-29 20:35:18 UTC (rev 7618)
+++ trunk/bp-friends/bp-friends-functions.php   2013-11-30 03:47:27 UTC (rev 7619)
</span><span class="lines">@@ -29,43 +29,43 @@
</span><span class="cx">  * @return bool True on success, false on failure.
</span><span class="cx">  */
</span><span class="cx"> function friends_add_friend( $initiator_userid, $friend_userid, $force_accept = false ) {
</span><del>-       global $bp;
</del><span class="cx"> 
</span><ins>+       // Check if already friends, and bail if so
</ins><span class="cx">   $friendship = new BP_Friends_Friendship;
</span><del>-
-       if ( (int) $friendship->is_confirmed )
</del><ins>+        if ( (int) $friendship->is_confirmed ) {
</ins><span class="cx">           return true;
</span><ins>+       }
</ins><span class="cx"> 
</span><ins>+       // Setup the friendship data
</ins><span class="cx">   $friendship->initiator_user_id = $initiator_userid;
</span><span class="cx">  $friendship->friend_user_id    = $friend_userid;
</span><span class="cx">  $friendship->is_confirmed      = 0;
</span><span class="cx">  $friendship->is_limited        = 0;
</span><span class="cx">  $friendship->date_created      = bp_core_current_time();
</span><span class="cx"> 
</span><del>-       if ( $force_accept )
</del><ins>+        if ( !empty( $force_accept ) ) {
</ins><span class="cx">           $friendship->is_confirmed = 1;
</span><ins>+       }
</ins><span class="cx"> 
</span><del>-       if ( $friendship->save() ) {
</del><ins>+        // Bail if friendship could not be saved (how sad!)
+       if ( ! $friendship->save() ) {
+               return false;
+       }
</ins><span class="cx"> 
</span><del>-               if ( !$force_accept ) {
-                       // Add the on screen notification
-                       bp_core_add_notification( $friendship->initiator_user_id, $friendship->friend_user_id, $bp->friends->id, 'friendship_request' );
</del><ins>+        // Send notifications
+       if ( empty( $force_accept ) ) {
+               $action = 'friends_friendship_requested';
</ins><span class="cx"> 
</span><del>-                       // Send the email notification
-                       friends_notification_new_request( $friendship->id, $friendship->initiator_user_id, $friendship->friend_user_id );
</del><ins>+        // Update friend totals
+       } else {
+               $action = 'friends_friendship_accepted';
+               friends_update_friend_totals( $friendship->initiator_user_id, $friendship->friend_user_id, 'add' );
+       }
</ins><span class="cx"> 
</span><del>-                       do_action( 'friends_friendship_requested', $friendship->id, $friendship->initiator_user_id, $friendship->friend_user_id );
-               } else {
-                       // Update friend totals
-                       friends_update_friend_totals( $friendship->initiator_user_id, $friendship->friend_user_id, 'add' );
</del><ins>+        // Call the above titled action and pass friendship data into it
+       do_action( $action, $friendship->id, $friendship->initiator_user_id, $friendship->friend_user_id );
</ins><span class="cx"> 
</span><del>-                       do_action( 'friends_friendship_accepted', $friendship->id, $friendship->initiator_user_id, $friendship->friend_user_id );
-               }
-
-               return true;
-       }
-
-       return false;
</del><ins>+        return true;
</ins><span class="cx"> }
</span><span class="cx"> 
</span><span class="cx"> /**
</span><span class="lines">@@ -111,46 +111,18 @@
</span><span class="cx">  * @return bool True on success, false on failure.
</span><span class="cx">  */
</span><span class="cx"> function friends_accept_friendship( $friendship_id ) {
</span><del>-       global $bp;
</del><span class="cx"> 
</span><ins>+       // Get the friesdhip data
</ins><span class="cx">   $friendship = new BP_Friends_Friendship( $friendship_id, true, false );
</span><span class="cx"> 
</span><del>-       if ( !$friendship->is_confirmed && BP_Friends_Friendship::accept( $friendship_id ) ) {
</del><ins>+        // Accepting friendship
+       if ( empty( $friendship->is_confirmed ) && BP_Friends_Friendship::accept( $friendship_id ) ) {
+
+               // Bump the friendship counts
</ins><span class="cx">           friends_update_friend_totals( $friendship->initiator_user_id, $friendship->friend_user_id );
</span><span class="cx"> 
</span><del>-               // Remove the friend request notice
-               bp_core_mark_notifications_by_item_id( $friendship->friend_user_id, $friendship->initiator_user_id, $bp->friends->id, 'friendship_request' );
</del><ins>+                do_action( 'friends_friendship_accepted', $friendship->id, $friendship->initiator_user_id, $friendship->friend_user_id, $friendship );
</ins><span class="cx"> 
</span><del>-               // Add a friend accepted notice for the initiating user
-               bp_core_add_notification( $friendship->friend_user_id, $friendship->initiator_user_id, $bp->friends->id, 'friendship_accepted' );
-
-               $initiator_link = bp_core_get_userlink( $friendship->initiator_user_id );
-               $friend_link = bp_core_get_userlink( $friendship->friend_user_id );
-
-               // Record in activity streams for the initiator
-               friends_record_activity( array(
-                       'user_id'           => $friendship->initiator_user_id,
-                       'type'              => 'friendship_created',
-                       'action'            => apply_filters( 'friends_activity_friendship_accepted_action', sprintf( __( '%1$s and %2$s are now friends', 'buddypress' ), $initiator_link, $friend_link ), $friendship ),
-                       'item_id'           => $friendship_id,
-                       'secondary_item_id' => $friendship->friend_user_id
-               ) );
-
-               // Record in activity streams for the friend
-               friends_record_activity( array(
-                       'user_id'           => $friendship->friend_user_id,
-                       'type'              => 'friendship_created',
-                       'action'            => apply_filters( 'friends_activity_friendship_accepted_action', sprintf( __( '%1$s and %2$s are now friends', 'buddypress' ), $friend_link, $initiator_link ), $friendship ),
-                       'item_id'           => $friendship_id,
-                       'secondary_item_id' => $friendship->initiator_user_id,
-                       'hide_sitewide'     => true // We've already got the first entry site wide
-               ) );
-
-               // Send the email notification
-               friends_notification_accepted_request( $friendship->id, $friendship->initiator_user_id, $friendship->friend_user_id );
-
-               do_action( 'friends_friendship_accepted', $friendship->id, $friendship->initiator_user_id, $friendship->friend_user_id );
-
</del><span class="cx">           return true;
</span><span class="cx">  }
</span><span class="cx"> 
</span><span class="lines">@@ -164,15 +136,9 @@
</span><span class="cx">  * @return bool True on success, false on failure.
</span><span class="cx">  */
</span><span class="cx"> function friends_reject_friendship( $friendship_id ) {
</span><del>-       global $bp;
-
</del><span class="cx">   $friendship = new BP_Friends_Friendship( $friendship_id, true, false );
</span><span class="cx"> 
</span><del>-       if ( !$friendship->is_confirmed && BP_Friends_Friendship::reject( $friendship_id ) ) {
-
-               // Remove the friend request notice
-               bp_core_mark_notifications_by_item_id( $friendship->friend_user_id, $friendship->initiator_user_id, $bp->friends->id, 'friendship_request' );
-
</del><ins>+        if ( empty( $friendship->is_confirmed ) && BP_Friends_Friendship::reject( $friendship_id ) ) {
</ins><span class="cx">           do_action_ref_array( 'friends_friendship_rejected', array( $friendship_id, &$friendship ) );
</span><span class="cx">          return true;
</span><span class="cx">  }
</span><span class="lines">@@ -189,15 +155,10 @@
</span><span class="cx">  * @return bool True on success, false on failure.
</span><span class="cx">  */
</span><span class="cx"> function friends_withdraw_friendship( $initiator_userid, $friend_userid ) {
</span><del>-       global $bp;
-
</del><span class="cx">   $friendship_id = BP_Friends_Friendship::get_friendship_id( $initiator_userid, $friend_userid );
</span><span class="cx">  $friendship    = new BP_Friends_Friendship( $friendship_id, true, false );
</span><span class="cx"> 
</span><del>-       if ( !$friendship->is_confirmed && BP_Friends_Friendship::withdraw( $friendship_id ) ) {
-               // Remove the friend request notice
-               bp_core_delete_notifications_by_item_id( $friendship->friend_user_id, $friendship->initiator_user_id, $bp->friends->id, 'friendship_request' );
-
</del><ins>+        if ( empty( $friendship->is_confirmed ) && BP_Friends_Friendship::withdraw( $friendship_id ) ) {
</ins><span class="cx">           do_action_ref_array( 'friends_friendship_whithdrawn', array( $friendship_id, &$friendship ) );
</span><span class="cx">          return true;
</span><span class="cx">  }
</span><span class="lines">@@ -566,7 +527,6 @@
</span><span class="cx">  * @param int $user_id ID of the user whose friend data is being removed.
</span><span class="cx">  */
</span><span class="cx"> function friends_remove_data( $user_id ) {
</span><del>-       global $bp;
</del><span class="cx"> 
</span><span class="cx">  do_action( 'friends_before_remove_data', $user_id );
</span><span class="cx"> 
</span><span class="lines">@@ -575,9 +535,6 @@
</span><span class="cx">  // Remove usermeta
</span><span class="cx">  bp_delete_user_meta( $user_id, 'total_friend_count' );
</span><span class="cx"> 
</span><del>-       // Remove friendship requests FROM user
-       bp_core_delete_notifications_from_user( $user_id, $bp->friends->id, 'friendship_request' );
-
</del><span class="cx">   do_action( 'friends_remove_data', $user_id );
</span><span class="cx"> }
</span><span class="cx"> add_action( 'wpmu_delete_user',  'friends_remove_data' );
</span></span></pre></div>
<a id="trunkbpfriendsbpfriendsnotificationsphp"></a>
<div class="modfile"><h4>Modified: trunk/bp-friends/bp-friends-notifications.php (7618 => 7619)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/bp-friends/bp-friends-notifications.php    2013-11-29 20:35:18 UTC (rev 7618)
+++ trunk/bp-friends/bp-friends-notifications.php       2013-11-30 03:47:27 UTC (rev 7619)
</span><span class="lines">@@ -13,6 +13,8 @@
</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 notifications related to a new friendship request.
</span><span class="cx">  *
</span><span class="lines">@@ -54,8 +56,8 @@
</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 */
-       $to = apply_filters( 'friends_notification_new_request_to', $to );
</del><ins>+        // Send the message
+       $to      = apply_filters( 'friends_notification_new_request_to', $to );
</ins><span class="cx">   $subject = apply_filters( 'friends_notification_new_request_subject', $subject, $initiator_name );
</span><span class="cx">  $message = apply_filters( 'friends_notification_new_request_message', $message, $initiator_name, $initiator_link, $all_requests_link, $settings_link );
</span><span class="cx"> 
</span><span class="lines">@@ -63,6 +65,7 @@
</span><span class="cx"> 
</span><span class="cx">  do_action( 'bp_friends_sent_request_email', $friend_id, $subject, $message, $friendship_id, $initiator_id );
</span><span class="cx"> }
</span><ins>+add_action( 'friends_friendship_requested', 'friends_notification_new_request', 10, 3 );
</ins><span class="cx"> 
</span><span class="cx"> /**
</span><span class="cx">  * Send notifications related to the acceptance of a friendship request.
</span><span class="lines">@@ -102,8 +105,8 @@
</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 */
-       $to = apply_filters( 'friends_notification_accepted_request_to', $to );
</del><ins>+        // Send the message
+       $to      = apply_filters( 'friends_notification_accepted_request_to', $to );
</ins><span class="cx">   $subject = apply_filters( 'friends_notification_accepted_request_subject', $subject, $friend_name );
</span><span class="cx">  $message = apply_filters( 'friends_notification_accepted_request_message', $message, $friend_name, $friend_link, $settings_link );
</span><span class="cx"> 
</span><span class="lines">@@ -111,3 +114,196 @@
</span><span class="cx"> 
</span><span class="cx">  do_action( 'bp_friends_sent_accepted_email', $initiator_id, $subject, $message, $friendship_id, $friend_id );
</span><span class="cx"> }
</span><ins>+add_action( 'friends_friendship_accepted', 'friends_notification_accepted_request', 10, 3 );
+
+/** Notifications *************************************************************/
+
+/**
+ * Notification formatting callback for bp-friends notifications.
+ *
+ * @param string $action The kind of notification being rendered.
+ * @param int $item_id The primary item ID.
+ * @param int $secondary_item_id The secondary item ID.
+ * @param int $total_items The total number of messaging-related notifications
+ *        waiting for the user.
+ * @param string $format 'string' for BuddyBar-compatible notifications;
+ *        'array' for WP Toolbar. Default: 'string'.
+ * @return array|string
+ */
+function friends_format_notifications( $action, $item_id, $secondary_item_id, $total_items, $format = 'string' ) {
+
+       switch ( $action ) {
+               case 'friendship_accepted':
+                       $link = trailingslashit( bp_loggedin_user_domain() . bp_get_friends_slug() . '/my-friends' );
+
+                       // Set up the string and the filter
+                       if ( (int) $total_items > 1 ) {
+                               $text = sprintf( __( '%d friends accepted your friendship requests', 'buddypress' ), (int) $total_items );
+                               $filter = 'bp_friends_multiple_friendship_accepted_notification';
+                       } else {
+                               $text = sprintf( __( '%s accepted your friendship request', 'buddypress' ),  bp_core_get_user_displayname( $item_id ) );
+                               $filter = 'bp_friends_single_friendship_accepted_notification';
+                       }
+
+                       break;
+
+               case 'friendship_request':
+                       $link = bp_loggedin_user_domain() . bp_get_friends_slug() . '/requests/?new';
+
+                       // Set up the string and the filter
+                       if ( (int) $total_items > 1 ) {
+                               $text = sprintf( __( 'You have %d pending friendship requests', 'buddypress' ), (int) $total_items );
+                               $filter = 'bp_friends_multiple_friendship_request_notification';
+                       } else {
+                               $text = sprintf( __( 'You have a friendship request from %s', 'buddypress' ),  bp_core_get_user_displayname( $item_id ) );
+                               $filter = 'bp_friends_single_friendship_request_notification';
+                       }
+
+                       break;
+       }
+
+       // Return either an HTML link or an array, depending on the requested format
+       if ( 'string' == $format ) {
+               $return = apply_filters( $filter, '<a href="' . esc_url( $link ) . '">' . esc_html( $text ) . '</a>', (int) $total_items );
+       } else {
+               $return = apply_filters( $filter, array(
+                       'link' => $link,
+                       'text' => $text
+               ), (int) $total_items );
+       }
+
+       do_action( 'friends_format_notifications', $action, $item_id, $secondary_item_id, $total_items, $return );
+
+       return $return;
+}
+
+/**
+ * Clear friend-related notifications when ?new=1
+ */
+function friends_clear_friend_notifications() {
+       if ( isset( $_GET['new'] ) && bp_is_active( 'notifications' ) ) {
+               bp_notifications_mark_notifications_by_type( bp_loggedin_user_id(), buddypress()->friends->id, 'friendship_accepted' );
+       }
+}
+add_action( 'bp_activity_screen_my_activity', 'friends_clear_friend_notifications' );
+
+/**
+ * Delete any friendship request notifications for the logged in user.
+ *
+ * @since BuddyPress (1.9.0)
+ */
+function bp_friends_mark_friendship_request_notifications_by_type() {
+       if ( isset( $_GET['new'] ) && bp_is_active( 'notifications' ) ) {
+               bp_notifications_mark_notifications_by_type( bp_loggedin_user_id(), buddypress()->friends->id, 'friendship_request' );
+       }
+}
+add_action( 'friends_screen_requests', 'bp_friends_mark_friendship_request_notifications_by_type' );
+
+/**
+ * Delete any friendship acceptance notifications for the logged in user.
+ *
+ * @since BuddyPress (1.9.0)
+ */
+function bp_friends_mark_friendship_accepted_notifications_by_type() {
+       if ( bp_is_active( 'notifications' ) ) {
+               bp_notifications_mark_notifications_by_type( bp_loggedin_user_id(), buddypress()->friends->id, 'friendship_accepted' );
+       }
+}
+add_action( 'friends_screen_my_friends', 'bp_friends_mark_friendship_accepted_notifications_by_type' );
+
+/**
+ * Notify one use that another user has requested their virtual friendship.
+ *
+ * @since BuddyPress (1.9.0)
+ * @param int $friendship_id The unique ID of the friendship
+ * @param int $initiator_user_id The friendship initiator user ID
+ * @param int $friend_user_id The friendship request reciever user ID
+ */
+function bp_friends_friendship_requested_notification( $friendship_id, $initiator_user_id, $friend_user_id ) {
+       if ( bp_is_active( 'notifications' ) ) {
+               bp_notifications_add_notification( array(
+                       'user_id'           => $friend_user_id,
+                       'item_id'           => $initiator_user_id,
+                       'secondary_item_id' => $friendship_id,
+                       'component_name'    => buddypress()->friends->id,
+                       'component_action'  => 'friendship_request',
+                       'date_notified'     => bp_core_current_time(),
+                       'is_new'            => 1,
+               ) );
+       }
+}
+add_action( 'friends_friendship_requested', 'bp_friends_friendship_requested_notification', 10, 3 );
+
+/**
+ * Remove friend request notice when a member rejects another members
+ *
+ * @since BuddyPress (1.9.0)
+ *
+ * @param int $friendship_id (not used)
+ * @param object $friendship
+ */
+function bp_friends_mark_friendship_rejected_notifications_by_item_id( $friendship_id, $friendship ) {
+       if ( bp_is_active( 'notifications' ) ) {
+               bp_notifications_mark_notifications_by_item_id( $friendship->friend_user_id, $friendship->initiator_user_id, buddypress()->friends->id, 'friendship_request' );
+       }
+}
+add_action( 'friends_friendship_rejected', 'bp_friends_mark_friendship_rejected_notifications_by_item_id', 10, 2 );
+
+/**
+ * Notify a member when another member accepts their virtual friendship request.
+ *
+ * @since BuddyPress (1.9.0)
+ * @param int $friendship_id The unique ID of the friendship
+ * @param int $initiator_user_id The friendship initiator user ID
+ * @param int $friend_user_id The friendship request reciever user ID
+ */
+function bp_friends_add_friendship_accepted_notification( $friendship_id, $initiator_user_id, $friend_user_id ) {
+
+       // Bail if notifications is not active
+       if ( ! bp_is_active( 'notifications' ) ) {
+               return;
+       }
+
+       // Remove the friend request notice
+       bp_notifications_mark_notifications_by_item_id( $friend_user_id, $initiator_user_id, buddypress()->friends->id, 'friendship_request' );
+
+       // Add a friend accepted notice for the initiating user
+       bp_notifications_add_notification(  array(
+               'user_id'           => $initiator_user_id,
+               'item_id'           => $friend_user_id,
+               'secondary_item_id' => $friendship_id,
+               'component_name'    => buddypress()->friends->id,
+               'component_action'  => 'friendship_accepted',
+               'date_notified'     => bp_core_current_time(),
+               'is_new'            => 1,
+       ) );
+}
+add_action( 'friends_friendship_accepted', 'bp_friends_add_friendship_accepted_notification', 10, 3 );
+
+/**
+ * Remove friend request notice when a member withdraws their friend request
+ *
+ * @since BuddyPress (1.9.0)
+ *
+ * @param int $friendship_id (not used)
+ * @param object $friendship
+ */
+function bp_friends_mark_friendship_withdrawn_notifications_by_item_id( $friendship_id, $friendship ) {
+       if ( bp_is_active( 'notifications' ) ) {
+               bp_notifications_delete_notifications_by_item_id( $friendship->friend_user_id, $friendship->initiator_user_id, buddypress()->friends->id, 'friendship_request' );
+       }
+}
+add_action( 'friends_friendship_withdrawn', 'bp_friends_mark_friendship_withdrawn_notifications_by_item_id', 10, 2 );
+
+/**
+ * Remove friendship requests FROM user, used primarily when a user is deleted
+ *
+ * @since BuddyPress (1.9.0)
+ * @param int $user_id
+ */
+function bp_friends_remove_notifications_data( $user_id = 0 ) {
+       if ( bp_is_active( 'notifications' ) ) {
+               bp_notifications_delete_notifications_from_user( $user_id, buddypress()->friends->id, 'friendship_request' );
+       }
+}
+add_action( 'friends_remove_data', 'bp_friends_remove_notifications_data', 10, 1 );
</ins></span></pre></div>
<a id="trunkbpfriendsbpfriendsscreensphp"></a>
<div class="modfile"><h4>Modified: trunk/bp-friends/bp-friends-screens.php (7618 => 7619)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/bp-friends/bp-friends-screens.php  2013-11-29 20:35:18 UTC (rev 7618)
+++ trunk/bp-friends/bp-friends-screens.php     2013-11-30 03:47:27 UTC (rev 7619)
</span><span class="lines">@@ -19,9 +19,6 @@
</span><span class="cx">  */
</span><span class="cx"> function friends_screen_my_friends() {
</span><span class="cx"> 
</span><del>-       // Delete any friendship acceptance notifications for the user when viewing a profile
-       bp_core_mark_notifications_by_type( bp_loggedin_user_id(), buddypress()->friends->id, 'friendship_accepted' );
-
</del><span class="cx">   do_action( 'friends_screen_my_friends' );
</span><span class="cx"> 
</span><span class="cx">  bp_core_load_template( apply_filters( 'friends_template_my_friends', 'members/single/home' ) );
</span><span class="lines">@@ -67,10 +64,6 @@
</span><span class="cx"> 
</span><span class="cx">  do_action( 'friends_screen_requests' );
</span><span class="cx"> 
</span><del>-       if ( isset( $_GET['new'] ) ) {
-               bp_core_mark_notifications_by_type( bp_loggedin_user_id(), buddypress()->friends->id, 'friendship_request' );
-       }
-
</del><span class="cx">   bp_core_load_template( apply_filters( 'friends_template_requests', 'members/single/home' ) );
</span><span class="cx"> }
</span><span class="cx"> 
</span></span></pre>
</div>
</div>

</body>
</html>