Skip to:
Content

BuddyPress.org

Changeset 7619


Ignore:
Timestamp:
11/30/2013 03:47:27 AM (12 years ago)
Author:
johnjamesjacoby
Message:

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 #5266. Hat-tip r-a-y.
Location:
trunk/bp-friends
Files:
5 edited

Legend:

Unmodified
Added
Removed
  • trunk/bp-friends/bp-friends-activity.php

    r7556 r7619  
    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,
     
    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
     
    7269 */
    7370function friends_delete_activity( $args ) {
    74     global $bp;
     71    if ( ! bp_is_active( 'activity' ) ) {
     72        return;
     73    }
    7574
    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 ) );
    79     }
     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
     
    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
     
    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 );
  • trunk/bp-friends/bp-friends-cache.php

    r7556 r7619  
    1010 * @subpackage FriendsCaching
    1111 */
    12 
    1312
    1413// Exit if accessed directly
     
    2928}
    3029
    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' );
    40 
    4130// List actions to clear object caches on
    4231add_action( 'friends_friendship_accepted', 'friends_clear_friend_object_cache' );
  • trunk/bp-friends/bp-friends-functions.php

    r7558 r7619  
    3030 */
    3131function friends_add_friend( $initiator_userid, $friend_userid, $force_accept = false ) {
    32     global $bp;
    33 
     32
     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;
    38 
     37    }
     38
     39    // Setup the friendship data
    3940    $friendship->initiator_user_id = $initiator_userid;
    4041    $friendship->friend_user_id    = $friend_userid;
     
    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;
    66     }
    67 
    68     return false;
     48    }
     49
     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
     
    112112 */
    113113function friends_accept_friendship( $friendship_id ) {
    114     global $bp;
    115 
     114
     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;
     
    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;
     
    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;
     
    567528 */
    568529function friends_remove_data( $user_id ) {
    569     global $bp;
    570530
    571531    do_action( 'friends_before_remove_data', $user_id );
     
    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 );
  • trunk/bp-friends/bp-friends-notifications.php

    r7558 r7619  
    1313// Exit if accessed directly
    1414if ( !defined( 'ABSPATH' ) ) exit;
     15
     16/** Emails ********************************************************************/
    1517
    1618/**
     
    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 );
     
    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/**
     
    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 );
     
    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 friendship 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 );
  • trunk/bp-friends/bp-friends-screens.php

    r7560 r7619  
    1919 */
    2020function friends_screen_my_friends() {
    21 
    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' );
    2421
    2522    do_action( 'friends_screen_my_friends' );
     
    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' ) );
Note: See TracChangeset for help on using the changeset viewer.