Skip to:
Content

BuddyPress.org

Ticket #5266: 5266.friends.3.patch

File 5266.friends.3.patch, 25.7 KB (added by johnjamesjacoby, 12 years ago)

Commit worthy patch. Includes the best of both friends patches, plus some surrounding clean-up

  • plugins/buddypress/bp-friends/bp-friends-activity.php

    diff --git a/plugins/buddypress/bp-friends/bp-friends-activity.php b/plugins/buddypress/bp-friends/bp-friends-activity.php
    index fb9f585..996c48a 100644
    a b  
    3232 * @return bool See {@link bp_activity_add()}.
    3333 */
    3434function friends_record_activity( $args = '' ) {
    35         global $bp;
    3635
    37         if ( !bp_is_active( 'activity' ) )
     36        if ( ! bp_is_active( 'activity' ) ) {
    3837                return false;
     38        }
    3939
    40         $defaults = array (
     40        $r = wp_parse_args( $args, array(
    4141                'user_id'           => bp_loggedin_user_id(),
    4242                'action'            => '',
    4343                'content'           => '',
    4444                'primary_link'      => '',
    45                 'component'         => $bp->friends->id,
     45                'component'         => buddypress()->friends->id,
    4646                'type'              => false,
    4747                'item_id'           => false,
    4848                'secondary_item_id' => false,
    4949                'recorded_time'     => bp_core_current_time(),
    5050                'hide_sitewide'     => false
    51         );
     51        ) );
    5252
    53         $r = wp_parse_args( $args, $defaults );
    54         extract( $r, EXTR_SKIP );
    55 
    56         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 ) );
     53        return bp_activity_add( $r );
    5754}
    5855
    5956/**
     
    7168 * @return bool True on success, false on failure.
    7269 */
    7370function friends_delete_activity( $args ) {
    74         global $bp;
    75 
    76         if ( bp_is_active( 'activity' ) ) {
    77                 extract( (array) $args );
    78                 bp_activity_delete_by_item_id( array( 'item_id' => $item_id, 'component' => $bp->friends->id, 'type' => $type, 'user_id' => $user_id ) );
     71        if ( ! bp_is_active( 'activity' ) ) {
     72                return;
    7973        }
     74
     75        bp_activity_delete_by_item_id( array(
     76                'component' => buddypress()->friends->id,
     77                'item_id'   => $args['item_id'],
     78                'type'      => $args['type'],
     79                'user_id'   => $args['user_id']
     80        ) );
    8081}
    8182
    8283/**
    8384 * Register the activity actions for bp-friends.
    8485 */
    8586function friends_register_activity_actions() {
    86         global $bp;
    8787
    88         if ( !bp_is_active( 'activity' ) )
     88        if ( !bp_is_active( 'activity' ) ) {
    8989                return false;
     90        }
     91
     92        $bp = buddypress();
    9093
    9194        // These two added in BP 1.6
    9295        bp_activity_set_action( $bp->friends->id, 'friendship_accepted', __( 'Friendships accepted', 'buddypress' ) );
    93         bp_activity_set_action( $bp->friends->id, 'friendship_created', __( 'New friendships', 'buddypress' ) );
     96        bp_activity_set_action( $bp->friends->id, 'friendship_created',  __( 'New friendships',      'buddypress' ) );
    9497
    9598        // < BP 1.6 backpat
    9699        bp_activity_set_action( $bp->friends->id, 'friends_register_activity_action', __( 'New friendship created', 'buddypress' ) );
     
    100103add_action( 'bp_register_activity_actions', 'friends_register_activity_actions' );
    101104
    102105/**
    103  * Notification formatting callback for bp-friends notifications.
     106 * Add activity stream items when one members accepts another members request
     107 * for virtual friendship.
    104108 *
    105  * @param string $action The kind of notification being rendered.
    106  * @param int $item_id The primary item ID.
    107  * @param int $secondary_item_id The secondary item ID.
    108  * @param int $total_items The total number of messaging-related notifications
    109  *        waiting for the user.
    110  * @param string $format 'string' for BuddyBar-compatible notifications;
    111  *        'array' for WP Toolbar. Default: 'string'.
    112  * @return array|string
     109 * @since BuddyPress (1.9.0)
     110 *
     111 * @param int $friendship_id
     112 * @param int $initiator_user_id
     113 * @param int $friend_user_id
     114 * @param object $friendship
    113115 */
    114 function friends_format_notifications( $action, $item_id, $secondary_item_id, $total_items, $format = 'string' ) {
     116function bp_friends_friendship_accepted_activity( $friendship_id, $initiator_user_id, $friend_user_id, $friendship ) {
    115117
    116         switch ( $action ) {
    117                 case 'friendship_accepted':
    118                         $link = trailingslashit( bp_loggedin_user_domain() . bp_get_friends_slug() . '/my-friends' );
    119 
    120                         // Set up the string and the filter
    121                         if ( (int) $total_items > 1 ) {
    122                                 $text = sprintf( __( '%d friends accepted your friendship requests', 'buddypress' ), (int) $total_items );
    123                                 $filter = 'bp_friends_multiple_friendship_accepted_notification';
    124                         } else {
    125                                 $text = sprintf( __( '%s accepted your friendship request', 'buddypress' ),  bp_core_get_user_displayname( $item_id ) );
    126                                 $filter = 'bp_friends_single_friendship_accepted_notification';
    127                         }
    128 
    129                         break;
    130 
    131                 case 'friendship_request':
    132                         $link = bp_loggedin_user_domain() . bp_get_friends_slug() . '/requests/?new';
    133 
    134                         // Set up the string and the filter
    135                         if ( (int) $total_items > 1 ) {
    136                                 $text = sprintf( __( 'You have %d pending friendship requests', 'buddypress' ), (int) $total_items );
    137                                 $filter = 'bp_friends_multiple_friendship_request_notification';
    138                         } else {
    139                                 $text = sprintf( __( 'You have a friendship request from %s', 'buddypress' ),  bp_core_get_user_displayname( $item_id ) );
    140                                 $filter = 'bp_friends_single_friendship_request_notification';
    141                         }
    142 
    143                         break;
     118        // Bail if Activity component is not active
     119        if ( ! bp_is_active( 'activity' ) ) {
     120                return;
    144121        }
    145122
    146         // Return either an HTML link or an array, depending on the requested format
    147         if ( 'string' == $format ) {
    148                 $return = apply_filters( $filter, '<a href="' . $link . '">' . $text . '</a>', (int) $total_items );
    149         } else {
    150                 $return = apply_filters( $filter, array(
    151                         'link' => $link,
    152                         'text' => $text
    153                 ), (int) $total_items );
    154         }
     123        // Get links to both members profiles
     124        $initiator_link = bp_core_get_userlink( $initiator_user_id );
     125        $friend_link    = bp_core_get_userlink( $friend_user_id    );
    155126
    156         do_action( 'friends_format_notifications', $action, $item_id, $secondary_item_id, $total_items, $return );
     127        // Record in activity streams for the initiator
     128        friends_record_activity( array(
     129                'user_id'           => $initiator_user_id,
     130                'type'              => 'friendship_created',
     131                'action'            => apply_filters( 'friends_activity_friendship_accepted_action', sprintf( __( '%1$s and %2$s are now friends', 'buddypress' ), $initiator_link, $friend_link ), $friendship ),
     132                'item_id'           => $friendship_id,
     133                'secondary_item_id' => $friend_user_id
     134        ) );
    157135
    158         return $return;
     136        // Record in activity streams for the friend
     137        friends_record_activity( array(
     138                'user_id'           => $friend_user_id,
     139                'type'              => 'friendship_created',
     140                'action'            => apply_filters( 'friends_activity_friendship_accepted_action', sprintf( __( '%1$s and %2$s are now friends', 'buddypress' ), $friend_link, $initiator_link ), $friendship ),
     141                'item_id'           => $friendship_id,
     142                'secondary_item_id' => $initiator_user_id,
     143                'hide_sitewide'     => true // We've already got the first entry site wide
     144        ) );
    159145}
     146add_action( 'friends_friendship_accepted', 'bp_friends_friendship_accepted_activity', 10, 4 );
  • plugins/buddypress/bp-friends/bp-friends-cache.php

    diff --git a/plugins/buddypress/bp-friends/bp-friends-cache.php b/plugins/buddypress/bp-friends/bp-friends-cache.php
    index 4464105..ce465e8 100644
    a b  
    1010 * @subpackage FriendsCaching
    1111 */
    1212
    13 
    1413// Exit if accessed directly
    1514if ( !defined( 'ABSPATH' ) ) exit;
    1615
     
    2726        wp_cache_delete( 'friends_friend_ids_' .    $friendship->initiator_user_id, 'bp' );
    2827        wp_cache_delete( 'friends_friend_ids_' .    $friendship->friend_user_id,    'bp' );
    2928}
    30 
    31 /**
    32  * Clear friend-related notifications when ?new=1.
    33  */
    34 function friends_clear_friend_notifications() {
    35         if ( isset( $_GET['new'] ) ) {
    36                 bp_core_mark_notifications_by_type( bp_loggedin_user_id(), buddypress()->friends->id, 'friendship_accepted' );
    37         }
    38 }
    39 add_action( 'bp_activity_screen_my_activity', 'friends_clear_friend_notifications' );
    4029
    4130// List actions to clear object caches on
    4231add_action( 'friends_friendship_accepted', 'friends_clear_friend_object_cache' );
  • plugins/buddypress/bp-friends/bp-friends-functions.php

    diff --git a/plugins/buddypress/bp-friends/bp-friends-functions.php b/plugins/buddypress/bp-friends/bp-friends-functions.php
    index dbce712..0bef80d 100644
    a b  
    2929 * @return bool True on success, false on failure.
    3030 */
    3131function friends_add_friend( $initiator_userid, $friend_userid, $force_accept = false ) {
    32         global $bp;
    3332
     33        // Check if already friends, and bail if so
    3434        $friendship = new BP_Friends_Friendship;
    35 
    36         if ( (int) $friendship->is_confirmed )
     35        if ( (int) $friendship->is_confirmed ) {
    3736                return true;
     37        }
    3838
     39        // Setup the friendship data
    3940        $friendship->initiator_user_id = $initiator_userid;
    4041        $friendship->friend_user_id    = $friend_userid;
    4142        $friendship->is_confirmed      = 0;
    4243        $friendship->is_limited        = 0;
    4344        $friendship->date_created      = bp_core_current_time();
    4445
    45         if ( $force_accept )
     46        if ( !empty( $force_accept ) ) {
    4647                $friendship->is_confirmed = 1;
    47 
    48         if ( $friendship->save() ) {
    49 
    50                 if ( !$force_accept ) {
    51                         // Add the on screen notification
    52                         bp_core_add_notification( $friendship->initiator_user_id, $friendship->friend_user_id, $bp->friends->id, 'friendship_request' );
    53 
    54                         // Send the email notification
    55                         friends_notification_new_request( $friendship->id, $friendship->initiator_user_id, $friendship->friend_user_id );
    56 
    57                         do_action( 'friends_friendship_requested', $friendship->id, $friendship->initiator_user_id, $friendship->friend_user_id );
    58                 } else {
    59                         // Update friend totals
    60                         friends_update_friend_totals( $friendship->initiator_user_id, $friendship->friend_user_id, 'add' );
    61 
    62                         do_action( 'friends_friendship_accepted', $friendship->id, $friendship->initiator_user_id, $friendship->friend_user_id );
    63                 }
    64 
    65                 return true;
    6648        }
    6749
    68         return false;
     50        // Bail if friendship could not be saved (how sad!)
     51        if ( ! $friendship->save() ) {
     52                return false;
     53        }
     54
     55        // Send notifications
     56        if ( empty( $force_accept ) ) {
     57                $action = 'friends_friendship_requested';
     58
     59        // Update friend totals
     60        } else {
     61                $action = 'friends_friendship_accepted';
     62                friends_update_friend_totals( $friendship->initiator_user_id, $friendship->friend_user_id, 'add' );
     63        }
     64
     65        // Call the above titled action and pass friendship data into it
     66        do_action( $action, $friendship->id, $friendship->initiator_user_id, $friendship->friend_user_id );
     67
     68        return true;
    6969}
    7070
    7171/**
     
    111111 * @return bool True on success, false on failure.
    112112 */
    113113function friends_accept_friendship( $friendship_id ) {
    114         global $bp;
    115114
     115        // Get the friesdhip data
    116116        $friendship = new BP_Friends_Friendship( $friendship_id, true, false );
    117117
    118         if ( !$friendship->is_confirmed && BP_Friends_Friendship::accept( $friendship_id ) ) {
     118        // Accepting friendship
     119        if ( empty( $friendship->is_confirmed ) && BP_Friends_Friendship::accept( $friendship_id ) ) {
     120
     121                // Bump the friendship counts
    119122                friends_update_friend_totals( $friendship->initiator_user_id, $friendship->friend_user_id );
    120123
    121                 // Remove the friend request notice
    122                 bp_core_mark_notifications_by_item_id( $friendship->friend_user_id, $friendship->initiator_user_id, $bp->friends->id, 'friendship_request' );
    123 
    124                 // Add a friend accepted notice for the initiating user
    125                 bp_core_add_notification( $friendship->friend_user_id, $friendship->initiator_user_id, $bp->friends->id, 'friendship_accepted' );
    126 
    127                 $initiator_link = bp_core_get_userlink( $friendship->initiator_user_id );
    128                 $friend_link = bp_core_get_userlink( $friendship->friend_user_id );
    129 
    130                 // Record in activity streams for the initiator
    131                 friends_record_activity( array(
    132                         'user_id'           => $friendship->initiator_user_id,
    133                         'type'              => 'friendship_created',
    134                         'action'            => apply_filters( 'friends_activity_friendship_accepted_action', sprintf( __( '%1$s and %2$s are now friends', 'buddypress' ), $initiator_link, $friend_link ), $friendship ),
    135                         'item_id'           => $friendship_id,
    136                         'secondary_item_id' => $friendship->friend_user_id
    137                 ) );
    138 
    139                 // Record in activity streams for the friend
    140                 friends_record_activity( array(
    141                         'user_id'           => $friendship->friend_user_id,
    142                         'type'              => 'friendship_created',
    143                         'action'            => apply_filters( 'friends_activity_friendship_accepted_action', sprintf( __( '%1$s and %2$s are now friends', 'buddypress' ), $friend_link, $initiator_link ), $friendship ),
    144                         'item_id'           => $friendship_id,
    145                         'secondary_item_id' => $friendship->initiator_user_id,
    146                         'hide_sitewide'     => true // We've already got the first entry site wide
    147                 ) );
    148 
    149                 // Send the email notification
    150                 friends_notification_accepted_request( $friendship->id, $friendship->initiator_user_id, $friendship->friend_user_id );
    151 
    152                 do_action( 'friends_friendship_accepted', $friendship->id, $friendship->initiator_user_id, $friendship->friend_user_id );
     124                do_action( 'friends_friendship_accepted', $friendship->id, $friendship->initiator_user_id, $friendship->friend_user_id, $friendship );
    153125
    154126                return true;
    155127        }
     
    164136 * @return bool True on success, false on failure.
    165137 */
    166138function friends_reject_friendship( $friendship_id ) {
    167         global $bp;
    168 
    169139        $friendship = new BP_Friends_Friendship( $friendship_id, true, false );
    170140
    171         if ( !$friendship->is_confirmed && BP_Friends_Friendship::reject( $friendship_id ) ) {
    172 
    173                 // Remove the friend request notice
    174                 bp_core_mark_notifications_by_item_id( $friendship->friend_user_id, $friendship->initiator_user_id, $bp->friends->id, 'friendship_request' );
    175 
     141        if ( empty( $friendship->is_confirmed ) && BP_Friends_Friendship::reject( $friendship_id ) ) {
    176142                do_action_ref_array( 'friends_friendship_rejected', array( $friendship_id, &$friendship ) );
    177143                return true;
    178144        }
     
    189155 * @return bool True on success, false on failure.
    190156 */
    191157function friends_withdraw_friendship( $initiator_userid, $friend_userid ) {
    192         global $bp;
    193 
    194158        $friendship_id = BP_Friends_Friendship::get_friendship_id( $initiator_userid, $friend_userid );
    195159        $friendship    = new BP_Friends_Friendship( $friendship_id, true, false );
    196160
    197         if ( !$friendship->is_confirmed && BP_Friends_Friendship::withdraw( $friendship_id ) ) {
    198                 // Remove the friend request notice
    199                 bp_core_delete_notifications_by_item_id( $friendship->friend_user_id, $friendship->initiator_user_id, $bp->friends->id, 'friendship_request' );
    200 
     161        if ( empty( $friendship->is_confirmed ) && BP_Friends_Friendship::withdraw( $friendship_id ) ) {
    201162                do_action_ref_array( 'friends_friendship_whithdrawn', array( $friendship_id, &$friendship ) );
    202163                return true;
    203164        }
     
    566527 * @param int $user_id ID of the user whose friend data is being removed.
    567528 */
    568529function friends_remove_data( $user_id ) {
    569         global $bp;
    570530
    571531        do_action( 'friends_before_remove_data', $user_id );
    572532
     
    574534
    575535        // Remove usermeta
    576536        bp_delete_user_meta( $user_id, 'total_friend_count' );
    577 
    578         // Remove friendship requests FROM user
    579         bp_core_delete_notifications_from_user( $user_id, $bp->friends->id, 'friendship_request' );
    580537
    581538        do_action( 'friends_remove_data', $user_id );
    582539}
  • plugins/buddypress/bp-friends/bp-friends-notifications.php

    diff --git a/plugins/buddypress/bp-friends/bp-friends-notifications.php b/plugins/buddypress/bp-friends/bp-friends-notifications.php
    index a11bb35..a3a87f9 100644
    a b  
    1313// Exit if accessed directly
    1414if ( !defined( 'ABSPATH' ) ) exit;
    1515
     16/** Emails ********************************************************************/
     17
    1618/**
    1719 * Send notifications related to a new friendship request.
    1820 *
     
    5456                $message .= sprintf( __( 'To disable these notifications please log in and go to: %s', 'buddypress' ), $settings_link );
    5557        }
    5658
    57         /* Send the message */
    58         $to = apply_filters( 'friends_notification_new_request_to', $to );
     59        // Send the message
     60        $to      = apply_filters( 'friends_notification_new_request_to', $to );
    5961        $subject = apply_filters( 'friends_notification_new_request_subject', $subject, $initiator_name );
    6062        $message = apply_filters( 'friends_notification_new_request_message', $message, $initiator_name, $initiator_link, $all_requests_link, $settings_link );
    6163
     
    6365
    6466        do_action( 'bp_friends_sent_request_email', $friend_id, $subject, $message, $friendship_id, $initiator_id );
    6567}
     68add_action( 'friends_friendship_requested', 'friends_notification_new_request', 10, 3 );
    6669
    6770/**
    6871 * Send notifications related to the acceptance of a friendship request.
     
    102105                $message .= sprintf( __( 'To disable these notifications please log in and go to: %s', 'buddypress' ), $settings_link );
    103106        }
    104107
    105         /* Send the message */
    106         $to = apply_filters( 'friends_notification_accepted_request_to', $to );
     108        // Send the message
     109        $to      = apply_filters( 'friends_notification_accepted_request_to', $to );
    107110        $subject = apply_filters( 'friends_notification_accepted_request_subject', $subject, $friend_name );
    108111        $message = apply_filters( 'friends_notification_accepted_request_message', $message, $friend_name, $friend_link, $settings_link );
    109112
     
    111114
    112115        do_action( 'bp_friends_sent_accepted_email', $initiator_id, $subject, $message, $friendship_id, $friend_id );
    113116}
     117add_action( 'friends_friendship_accepted', 'friends_notification_accepted_request', 10, 3 );
     118
     119/** Notifications *************************************************************/
     120
     121/**
     122 * Notification formatting callback for bp-friends notifications.
     123 *
     124 * @param string $action The kind of notification being rendered.
     125 * @param int $item_id The primary item ID.
     126 * @param int $secondary_item_id The secondary item ID.
     127 * @param int $total_items The total number of messaging-related notifications
     128 *        waiting for the user.
     129 * @param string $format 'string' for BuddyBar-compatible notifications;
     130 *        'array' for WP Toolbar. Default: 'string'.
     131 * @return array|string
     132 */
     133function friends_format_notifications( $action, $item_id, $secondary_item_id, $total_items, $format = 'string' ) {
     134
     135        switch ( $action ) {
     136                case 'friendship_accepted':
     137                        $link = trailingslashit( bp_loggedin_user_domain() . bp_get_friends_slug() . '/my-friends' );
     138
     139                        // Set up the string and the filter
     140                        if ( (int) $total_items > 1 ) {
     141                                $text = sprintf( __( '%d friends accepted your friendship requests', 'buddypress' ), (int) $total_items );
     142                                $filter = 'bp_friends_multiple_friendship_accepted_notification';
     143                        } else {
     144                                $text = sprintf( __( '%s accepted your friendship request', 'buddypress' ),  bp_core_get_user_displayname( $item_id ) );
     145                                $filter = 'bp_friends_single_friendship_accepted_notification';
     146                        }
     147
     148                        break;
     149
     150                case 'friendship_request':
     151                        $link = bp_loggedin_user_domain() . bp_get_friends_slug() . '/requests/?new';
     152
     153                        // Set up the string and the filter
     154                        if ( (int) $total_items > 1 ) {
     155                                $text = sprintf( __( 'You have %d pending friendship requests', 'buddypress' ), (int) $total_items );
     156                                $filter = 'bp_friends_multiple_friendship_request_notification';
     157                        } else {
     158                                $text = sprintf( __( 'You have a friendship request from %s', 'buddypress' ),  bp_core_get_user_displayname( $item_id ) );
     159                                $filter = 'bp_friends_single_friendship_request_notification';
     160                        }
     161
     162                        break;
     163        }
     164
     165        // Return either an HTML link or an array, depending on the requested format
     166        if ( 'string' == $format ) {
     167                $return = apply_filters( $filter, '<a href="' . esc_url( $link ) . '">' . esc_html( $text ) . '</a>', (int) $total_items );
     168        } else {
     169                $return = apply_filters( $filter, array(
     170                        'link' => $link,
     171                        'text' => $text
     172                ), (int) $total_items );
     173        }
     174
     175        do_action( 'friends_format_notifications', $action, $item_id, $secondary_item_id, $total_items, $return );
     176
     177        return $return;
     178}
     179
     180/**
     181 * Clear friend-related notifications when ?new=1
     182 */
     183function friends_clear_friend_notifications() {
     184        if ( isset( $_GET['new'] ) && bp_is_active( 'notifications' ) ) {
     185                bp_notifications_mark_notifications_by_type( bp_loggedin_user_id(), buddypress()->friends->id, 'friendship_accepted' );
     186        }
     187}
     188add_action( 'bp_activity_screen_my_activity', 'friends_clear_friend_notifications' );
     189
     190/**
     191 * Delete any friendship request notifications for the logged in user.
     192 *
     193 * @since BuddyPress (1.9.0)
     194 */
     195function bp_friends_mark_friendship_request_notifications_by_type() {
     196        if ( isset( $_GET['new'] ) && bp_is_active( 'notifications' ) ) {
     197                bp_notifications_mark_notifications_by_type( bp_loggedin_user_id(), buddypress()->friends->id, 'friendship_request' );
     198        }
     199}
     200add_action( 'friends_screen_requests', 'bp_friends_mark_friendship_request_notifications_by_type' );
     201
     202/**
     203 * Delete any friendship acceptance notifications for the logged in user.
     204 *
     205 * @since BuddyPress (1.9.0)
     206 */
     207function bp_friends_mark_friendship_accepted_notifications_by_type() {
     208        if ( bp_is_active( 'notifications' ) ) {
     209                bp_notifications_mark_notifications_by_type( bp_loggedin_user_id(), buddypress()->friends->id, 'friendship_accepted' );
     210        }
     211}
     212add_action( 'friends_screen_my_friends', 'bp_friends_mark_friendship_accepted_notifications_by_type' );
     213
     214/**
     215 * Notify one use that another user has requested their virtual friendship.
     216 *
     217 * @since BuddyPress (1.9.0)
     218 * @param int $friendship_id The unique ID of the friendship
     219 * @param int $initiator_user_id The friendship initiator user ID
     220 * @param int $friend_user_id The friendship request reciever user ID
     221 */
     222function bp_friends_friendship_requested_notification( $friendship_id, $initiator_user_id, $friend_user_id ) {
     223        if ( bp_is_active( 'notifications' ) ) {
     224                bp_notifications_add_notification( array(
     225                        'user_id'           => $friend_user_id,
     226                        'item_id'           => $initiator_user_id,
     227                        'secondary_item_id' => $friendship_id,
     228                        'component_name'    => buddypress()->friends->id,
     229                        'component_action'  => 'friendship_request',
     230                        'date_notified'     => bp_core_current_time(),
     231                        'is_new'            => 1,
     232                ) );
     233        }
     234}
     235add_action( 'friends_friendship_requested', 'bp_friends_friendship_requested_notification', 10, 3 );
     236
     237/**
     238 * Remove friend request notice when a member rejects another members
     239 *
     240 * @since BuddyPress (1.9.0)
     241 *
     242 * @param int $friendship_id (not used)
     243 * @param object $friendship
     244 */
     245function bp_friends_mark_friendship_rejected_notifications_by_item_id( $friendship_id, $friendship ) {
     246        if ( bp_is_active( 'notifications' ) ) {
     247                bp_notifications_mark_notifications_by_item_id( $friendship->friend_user_id, $friendship->initiator_user_id, buddypress()->friends->id, 'friendship_request' );
     248        }
     249}
     250add_action( 'friends_friendship_rejected', 'bp_friends_mark_friendship_rejected_notifications_by_item_id', 10, 2 );
     251
     252/**
     253 * Notify a member when another member accepts their virtual friendsip request.
     254 *
     255 * @since BuddyPress (1.9.0)
     256 * @param int $friendship_id The unique ID of the friendship
     257 * @param int $initiator_user_id The friendship initiator user ID
     258 * @param int $friend_user_id The friendship request reciever user ID
     259 */
     260function bp_friends_add_friendship_accepted_notification( $friendship_id, $initiator_user_id, $friend_user_id ) {
     261
     262        // Bail if notifications is not active
     263        if ( ! bp_is_active( 'notifications' ) ) {
     264                return;
     265        }
     266
     267        // Remove the friend request notice
     268        bp_notifications_mark_notifications_by_item_id( $friend_user_id, $initiator_user_id, buddypress()->friends->id, 'friendship_request' );
     269
     270        // Add a friend accepted notice for the initiating user
     271        bp_notifications_add_notification(  array(
     272                'user_id'           => $initiator_user_id,
     273                'item_id'           => $friend_user_id,
     274                'secondary_item_id' => $friendship_id,
     275                'component_name'    => buddypress()->friends->id,
     276                'component_action'  => 'friendship_accepted',
     277                'date_notified'     => bp_core_current_time(),
     278                'is_new'            => 1,
     279        ) );
     280}
     281add_action( 'friends_friendship_accepted', 'bp_friends_add_friendship_accepted_notification', 10, 3 );
     282
     283/**
     284 * Remove friend request notice when a member withdraws their friend request
     285 *
     286 * @since BuddyPress (1.9.0)
     287 *
     288 * @param int $friendship_id (not used)
     289 * @param object $friendship
     290 */
     291function bp_friends_mark_friendship_withdrawn_notifications_by_item_id( $friendship_id, $friendship ) {
     292        if ( bp_is_active( 'notifications' ) ) {
     293                bp_notifications_delete_notifications_by_item_id( $friendship->friend_user_id, $friendship->initiator_user_id, buddypress()->friends->id, 'friendship_request' );
     294        }
     295}
     296add_action( 'friends_friendship_withdrawn', 'bp_friends_mark_friendship_withdrawn_notifications_by_item_id', 10, 2 );
     297
     298/**
     299 * Remove friendship requests FROM user, used primarily when a user is deleted
     300 *
     301 * @since BuddyPress (1.9.0)
     302 * @param int $user_id
     303 */
     304function bp_friends_remove_notifications_data( $user_id = 0 ) {
     305        if ( bp_is_active( 'notifications' ) ) {
     306                bp_notifications_delete_notifications_from_user( $user_id, buddypress()->friends->id, 'friendship_request' );
     307        }
     308}
     309add_action( 'friends_remove_data', 'bp_friends_remove_notifications_data', 10, 1 );
  • plugins/buddypress/bp-friends/bp-friends-screens.php

    diff --git a/plugins/buddypress/bp-friends/bp-friends-screens.php b/plugins/buddypress/bp-friends/bp-friends-screens.php
    index 2972dad..9beddd3 100644
    a b  
    1919 */
    2020function friends_screen_my_friends() {
    2121
    22         // Delete any friendship acceptance notifications for the user when viewing a profile
    23         bp_core_mark_notifications_by_type( bp_loggedin_user_id(), buddypress()->friends->id, 'friendship_accepted' );
    24 
    2522        do_action( 'friends_screen_my_friends' );
    2623
    2724        bp_core_load_template( apply_filters( 'friends_template_my_friends', 'members/single/home' ) );
     
    6663        }
    6764
    6865        do_action( 'friends_screen_requests' );
    69 
    70         if ( isset( $_GET['new'] ) ) {
    71                 bp_core_mark_notifications_by_type( bp_loggedin_user_id(), buddypress()->friends->id, 'friendship_request' );
    72         }
    7366
    7467        bp_core_load_template( apply_filters( 'friends_template_requests', 'members/single/home' ) );
    7568}