bp_dtheme_ajax_addremove_friend

Friend/un-friend a user via a POST request.

Description

(string) bp_dtheme_ajax_addremove_friend(); 

Returns (string)

HTML


Usage

  1. if ( !function_exists( 'bp_dtheme_ajax_addremove_friend' ) ) { 
  2. require_once ABSPATH . PLUGINDIR . 'buddypress/bp-themes/bp-default/_inc/ajax.php'; 
  3.  
  4.  
  5. // NOTICE! Understand what this does before running. 
  6. $result = bp_dtheme_ajax_addremove_friend(); 
  7.  

Defined (1)

The function is defined in the following location(s).

/bp-themes/bp-default/_inc/ajax.php  
  1. function bp_dtheme_ajax_addremove_friend() { 
  2. // Bail if not a POST action 
  3. if ( 'POST' !== strtoupper( $_SERVER['REQUEST_METHOD'] ) ) 
  4. return; 
  5.  
  6. // Cast fid as an integer 
  7. $friend_id = (int) $_POST['fid']; 
  8.  
  9. // Trying to cancel friendship 
  10. if ( 'is_friend' == BP_Friends_Friendship::check_is_friend( bp_loggedin_user_id(), $friend_id ) ) { 
  11. check_ajax_referer( 'friends_remove_friend' ); 
  12.  
  13. if ( ! friends_remove_friend( bp_loggedin_user_id(), $friend_id ) ) { 
  14. echo __( 'Friendship could not be canceled.', buddypress ); 
  15. } else { 
  16. echo '<a id="friend-' . esc_attr( $friend_id ) . '" class="add" rel="add" title="' . __( 'Add Friend', buddypress ) . '" href="' . wp_nonce_url( bp_loggedin_user_domain() . bp_get_friends_slug() . '/add-friend/' . $friend_id, 'friends_add_friend' ) . '">' . __( 'Add Friend', buddypress ) . '</a>'; 
  17.  
  18. // Trying to request friendship 
  19. } elseif ( 'not_friends' == BP_Friends_Friendship::check_is_friend( bp_loggedin_user_id(), $friend_id ) ) { 
  20. check_ajax_referer( 'friends_add_friend' ); 
  21.  
  22. if ( ! friends_add_friend( bp_loggedin_user_id(), $friend_id ) ) { 
  23. echo __(' Friendship could not be requested.', buddypress ); 
  24. } else { 
  25. echo '<a id="friend-' . esc_attr( $friend_id ) . '" class="remove" rel="remove" title="' . __( 'Cancel Friendship Request', buddypress ) . '" href="' . wp_nonce_url( bp_loggedin_user_domain() . bp_get_friends_slug() . '/requests/cancel/' . $friend_id . '/', 'friends_withdraw_friendship' ) . '" class="requested">' . __( 'Cancel Friendship Request', buddypress ) . '</a>'; 
  26.  
  27. // Trying to cancel pending request 
  28. } elseif ( 'pending' == BP_Friends_Friendship::check_is_friend( bp_loggedin_user_id(), $friend_id ) ) { 
  29. check_ajax_referer( 'friends_withdraw_friendship' ); 
  30.  
  31. echo '<a id="friend-' . esc_attr( $friend_id ) . '" class="add" rel="add" title="' . __( 'Add Friend', buddypress ) . '" href="' . wp_nonce_url( bp_loggedin_user_domain() . bp_get_friends_slug() . '/add-friend/' . $friend_id, 'friends_add_friend' ) . '">' . __( 'Add Friend', buddypress ) . '</a>'; 
  32. } else { 
  33. echo __("Friendship request could not be cancelled.", buddypress); 
  34.  
  35. // Request already pending 
  36. } else { 
  37. echo __( 'Request Pending', buddypress ); 
  38.  
  39. exit;