Skip to:
Content

BuddyPress.org

Ticket #5266: 5266.friends.2.patch

File 5266.friends.2.patch, 20.6 KB (added by johnjamesjacoby, 12 years ago)
  • bp-friends-activity.php

     
    100100add_action( 'bp_register_activity_actions', 'friends_register_activity_actions' );
    101101
    102102/**
    103  * Notification formatting callback for bp-friends notifications.
     103 * Add activity stream items when one members accepts another members request
     104 * for virtual friendship.
    104105 *
    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
     106 * @since BuddyPress (1.9.0)
     107 *
     108 * @param int $friendship_id
     109 * @param int $initiator_user_id
     110 * @param int $friend_user_id
     111 * @param object $friendship
    113112 */
    114 function friends_format_notifications( $action, $item_id, $secondary_item_id, $total_items, $format = 'string' ) {
     113function bp_friends_friendship_accepted_activity( $friendship_id, $initiator_user_id, $friend_user_id, $friendship ) {
    115114
    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;
     115        // Bail if Activity component is not active
     116        if ( ! bp_is_active( 'activity' ) ) {
     117                return;
    144118        }
    145119
    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         }
     120        // Get links to both members profiles
     121        $initiator_link = bp_core_get_userlink( $initiator_user_id );
     122        $friend_link    = bp_core_get_userlink( $friend_user_id    );
    155123
    156         do_action( 'friends_format_notifications', $action, $item_id, $secondary_item_id, $total_items, $return );
     124        // Record in activity streams for the initiator
     125        friends_record_activity( array(
     126                'user_id'           => $initiator_user_id,
     127                'type'              => 'friendship_created',
     128                'action'            => apply_filters( 'friends_activity_friendship_accepted_action', sprintf( __( '%1$s and %2$s are now friends', 'buddypress' ), $initiator_link, $friend_link ), $friendship ),
     129                'item_id'           => $friendship_id,
     130                'secondary_item_id' => $friend_user_id
     131        ) );
    157132
    158         return $return;
     133        // Record in activity streams for the friend
     134        friends_record_activity( array(
     135                'user_id'           => $friend_user_id,
     136                'type'              => 'friendship_created',
     137                'action'            => apply_filters( 'friends_activity_friendship_accepted_action', sprintf( __( '%1$s and %2$s are now friends', 'buddypress' ), $friend_link, $initiator_link ), $friendship ),
     138                'item_id'           => $friendship_id,
     139                'secondary_item_id' => $initiator_user_id,
     140                'hide_sitewide'     => true // We've already got the first entry site wide
     141        ) );
    159142}
     143add_action( 'friends_friendship_accepted', 'bp_friends_friendship_accepted_activity', 10, 4 );
  • bp-friends-cache.php

     
    1010 * @subpackage FriendsCaching
    1111 */
    1212
    13 
    1413// Exit if accessed directly
    1514if ( !defined( 'ABSPATH' ) ) exit;
    1615
     
    2827        wp_cache_delete( 'friends_friend_ids_' .    $friendship->friend_user_id,    'bp' );
    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' );
    4332add_action( 'friends_friendship_deleted',  'friends_clear_friend_object_cache' );
  • bp-friends-functions.php

     
    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;
     48        }
    4749
    48         if ( $friendship->save() ) {
     50        // Bail if friendship could not be saved (how sad!)
     51        if ( ! $friendship->save() ) {
     52                return false;
     53        }
    4954
    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' );
     55        // Send notifications
     56        if ( empty( $force_accept ) ) {
     57                $action = 'friends_friendship_requested';
    5358
    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;
     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' );
    6663        }
    6764
    68         return false;
     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::reject( $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' );
     124                do_action( 'friends_friendship_accepted', $friendship->id, $friendship->initiator_user_id, $friendship->friend_user_id, $friendship );
    123125
    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 );
    153 
    154126                return true;
    155127        }
    156128
     
    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
     
    575535        // Remove usermeta
    576536        bp_delete_user_meta( $user_id, 'total_friend_count' );
    577537
    578         // Remove friendship requests FROM user
    579         bp_core_delete_notifications_from_user( $user_id, $bp->friends->id, 'friendship_request' );
    580 
    581538        do_action( 'friends_remove_data', $user_id );
    582539}
    583540add_action( 'wpmu_delete_user',  'friends_remove_data' );
  • bp-friends-notifications.php

     
    1313// Exit if accessed directly
    1414if ( !defined( 'ABSPATH' ) ) exit;
    1515
     16/** Emails ********************************************************************/
     17
    1618/**
    1719 * Send notifications related to a new friendship request.
    1820 *
     
    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.
     
    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="' . $link . '">' . $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 */
     219function bp_friends_friendship_requested_notification( $friendship_id, $initiator_user_id, $friend_user_id ) {
     220        if ( bp_is_active( 'notifications' ) ) {
     221                bp_notifications_add_notification( $initiator_user_id, $friend_user_id, $friendship_id, 'friendship_request' );
     222        }
     223}
     224add_action( 'friends_friendship_requested', 'friends_friendship_requested', 10, 3 );
     225
     226/**
     227 * Remove friend request notice when a member rejects another members
     228 *
     229 * @since BuddyPress (1.9.0)
     230 *
     231 * @param int $friendship_id (not used)
     232 * @param object $friendship
     233 */
     234function bp_friends_mark_friendship_rejected_notifications_by_item_id( $friendship_id, $friendship ) {
     235        if ( bp_is_active( 'notifications' ) ) {
     236                bp_notifications_mark_notifications_by_item_id( $friendship->friend_user_id, $friendship->initiator_user_id, buddypress()->friends->id, 'friendship_request' );
     237        }
     238}
     239add_action( 'friends_friendship_rejected', 'bp_friends_mark_friendship_rejected_notifications_by_item_id', 10, 2 );
     240
     241/**
     242 * Notify a member when another member accepts their virtual friendsip request.
     243 *
     244 * @since BuddyPress (1.9.0)
     245 * @param int $relationship_id
     246 * @param int $initiator_user_id
     247 * @param int $friend_user_id
     248 */
     249function bp_friends_add_friendship_accepted_notification( $friendship_id, $initiator_user_id, $friend_user_id ) {
     250
     251        // Bail if notifications is not active
     252        if ( ! bp_is_active( 'notifications' ) ) {
     253                return;
     254        }
     255
     256        // Remove the friend request notice
     257        bp_notifications_mark_notifications_by_item_id( $friend_user_id, $initiator_user_id, buddypress()->friends->id, 'friendship_request' );
     258
     259        // Add a friend accepted notice for the initiating user
     260        bp_notifications_add_notification(  array(
     261                'user_id'           => $initiator_user_id,
     262                'item_id'           => $friend_user_id,
     263                'secondary_item_id' => 0,
     264                'component_name'    => buddypress()->friends->id,
     265                'component_action'  => 'friendship_accepted',
     266                'date_notified'     => bp_core_current_time(),
     267                'is_new'            => 1,
     268        ) );
     269}
     270add_action( 'friends_friendship_accepted', 'bp_friends_add_friendship_accepted_notification', 10, 3 );
     271
     272/**
     273 * Remove friend request notice when a member withdraws their friend request
     274 *
     275 * @since BuddyPress (1.9.0)
     276 *
     277 * @param int $friendship_id (not used)
     278 * @param object $friendship
     279 */
     280function bp_friends_mark_friendship_withdrawn_notifications_by_item_id( $friendship_id, $friendship ) {
     281        if ( bp_is_active( 'notifications' ) ) {
     282                bp_notifications_delete_notifications_by_item_id( $friendship->friend_user_id, $friendship->initiator_user_id, buddypress()->friends->id, 'friendship_request' );
     283        }
     284}
     285add_action( 'friends_friendship_withdrawn', 'bp_friends_mark_friendship_withdrawn_notifications_by_item_id', 10, 2 );
     286
     287/**
     288 * Remove friendship requests FROM user, used primarily when a user is deleted
     289 *
     290 * @since BuddyPress (1.9.0)
     291 * @param int $user_id
     292 */
     293function bp_friends_remove_notifications_data( $user_id = 0 ) {
     294        if ( bp_is_active( 'notifications' ) ) {
     295                bp_notifications_delete_notifications_from_user( $user_id, buddypress()->friends->id, 'friendship_request' );
     296        }
     297}
     298add_action( 'friends_remove_data', 'bp_friends_remove_notifications_data', 10, 1 );
  • bp-friends-screens.php

     
    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' ) );
     
    6764
    6865        do_action( 'friends_screen_requests' );
    6966
    70         if ( isset( $_GET['new'] ) ) {
    71                 bp_core_mark_notifications_by_type( bp_loggedin_user_id(), buddypress()->friends->id, 'friendship_request' );
    72         }
    73 
    7467        bp_core_load_template( apply_filters( 'friends_template_requests', 'members/single/home' ) );
    7568}
    7669