[buddypress-trac] [BuddyPress] #5157: Modify Add Friend button logic based on who initiated request

buddypress-trac noreply at wordpress.org
Thu Aug 29 14:15:02 UTC 2013

#5157: Modify Add Friend button logic based on who initiated request
 Reporter:  terraling    |      Owner:
     Type:  enhancement  |     Status:  new
 Priority:  normal       |  Milestone:  Awaiting Review
Component:  Core         |    Version:
 Severity:  normal       |   Keywords:  has-patch
 I propose minor changes to bp-friends-classes.php and bp-friends-
 template.php which will leave them functionally unchanged for most users
 but will allow theme developers to adjust the behaviour of the Add Friend
 buttons via the bp_get_add_friend_button filter to resolve this use case

 John sends a friend request to Lucy.

 Lucy receives an email notification which invites her to check out John’s

 When she does she sees his penchant for S&M and she also sees a ‘Cancel
 Friendship Request’ button which she promptly hits.

 She gets an error message ‘Friendship request cannot be cancelled’, which
 she doesn't understand but which is because she can’t cancel it because
 she didn’t initiate it.

 Currently, check_is_friend (in bp-friends-classes.php) returns
 'is_friend', 'not_friends' or 'pending'. 'Pending' doesn't distinguish
 between whether the user initiated a request or received it.

 I propose to alter the test so that 'pending' is returned if the user
 initiated the request, and 'respond' if they are the recipient of a
 request which is awaiting their response.

 To avoid breaking anything, in bp-friends-template.php the function
 bp_get_add_friend_button adds a test for 'respond' but builds $button
 exactly the same as for 'pending', except for $button[id]='respond'.

 Then if a theme developer wants to handle the 'respond' case differently
 they can hook into the bp_get_add_friend_button filter.

 In my theme I change the button so that it is simply a link to the friend
 request page where the request can either be Accepted or Rejected as

 i.e., in the example above, when Jane looked at John's profile page she
 wouldn't see the 'pending' button she would see my new 'respond' button
 which, when she pressed it, took her to her own friend requests page where
 she could then quickly stab 'Reject'.

 I've tested on my theme and it's working fine, not aware of any wider

 Here is the change to check_is_friend:

         function check_is_friend( $loggedin_userid,
 $possible_friend_userid ) {
                 global $wpdb, $bp;

                 if ( empty( $loggedin_userid ) || empty(
 $possible_friend_userid ) )
                         return false;

                 $result = $wpdb->get_results( $wpdb->prepare( "SELECT id,
 is_confirmed, initiator_user_id FROM {$bp->friends->table_name} WHERE
 (initiator_user_id = %d AND friend_user_id = %d) OR (initiator_user_id =
 %d AND friend_user_id = %d)", $loggedin_userid, $possible_friend_userid,
 $possible_friend_userid, $loggedin_userid ) );
                 if ( !empty( $result ) ) {
                         if ( 0 == (int) $result[0]->is_confirmed ) {
                                 if ( $loggedin_userid ==
 $result[0]->initiator_user_id ) {
                                         return 'pending';
                                 } else {
                                         return 'respond';
                                 return 'pending';
                         } else {
                                 return 'is_friend';
                 } else {
                         return 'not_friends';


 This 'case' is added to function bp_get_add_friend_button in bp-friends-
 template.php which exactly reproduces the 'pending' case but with a
 different $button[id]:

                         case 'respond' :
                                 $button = array(
                                         'id'                => 'respond',
                                         'component'         => 'friends',
                                         'must_be_logged_in' => true,
                                         'block_self'        => true,
                                         'wrapper_class'     =>
 'friendship-button pending_friend',
                                         'wrapper_id'        =>
 'friendship-button-' . $potential_friend_id,
                                         'link_href'         =>
 wp_nonce_url( bp_loggedin_user_domain() . bp_get_friends_slug() .
 '/requests/cancel/' . $potential_friend_id . '/',
 'friends_withdraw_friendship' ),
                                         'link_text'         => __( 'Cancel
 Friendship Request', 'buddypress' ),
                                         'link_title'        => __( 'Cancel
 Friendship Requested', 'buddypress' ),
                                         'link_id'                       =>
 'friend-' . $potential_friend_id,
                                         'link_rel'                      =>
                                         'link_class'        =>
 'friendship-button pending_friend requested'

 This is the first time I've contributed anything (to anything) so if I'm
 not following procedure, apologies...

Ticket URL: <https://buddypress.trac.wordpress.org/ticket/5157>
BuddyPress <http://buddypress.org/>

More information about the buddypress-trac mailing list