bp_nouveau_ajax_addremove_friend()
Friend/un-friend a user via a POST request.
Description
Return
(string) HTML
Source
File: bp-templates/bp-nouveau/includes/friends/ajax.php
function bp_nouveau_ajax_addremove_friend() { $response = array( 'feedback' => sprintf( '<div class="bp-feedback error bp-ajax-message"><p>%s</p></div>', esc_html__( 'There was a problem performing this action. Please try again.', 'buddyboss' ) ), ); // Bail if not a POST action. if ( ! bp_is_post_request() ) { wp_send_json_error( $response ); } if ( empty( $_POST['nonce'] ) || empty( $_POST['item_id'] ) || ! bp_is_active( 'friends' ) ) { wp_send_json_error( $response ); } // Use default nonce $nonce = $_POST['nonce']; $check = 'bp_nouveau_friends'; // Use a specific one for actions needed it if ( ! empty( $_POST['_wpnonce'] ) && ! empty( $_POST['action'] ) ) { $nonce = $_POST['_wpnonce']; $check = $_POST['action']; } // Nonce check! if ( empty( $nonce ) || ! wp_verify_nonce( $nonce, $check ) ) { wp_send_json_error( $response ); } // Cast fid as an integer. $friend_id = (int) $_POST['item_id']; // Check if the user exists only when the Friend ID is not a Frienship ID. if ( isset( $_POST['action'] ) && $_POST['action'] !== 'friends_accept_friendship' && $_POST['action'] !== 'friends_reject_friendship' ) { $user = get_user_by( 'id', $friend_id ); if ( ! $user ) { wp_send_json_error( array( 'feedback' => sprintf( '<div class="bp-feedback error">%s</div>', esc_html__( 'No member found by that ID.', 'buddyboss' ) ), ) ); } } // In the 2 first cases the $friend_id is a friendship id. if ( ! empty( $_POST['action'] ) && 'friends_accept_friendship' === $_POST['action'] ) { if ( ! friends_accept_friendship( $friend_id ) ) { wp_send_json_error( array( 'feedback' => sprintf( '<div class="bp-feedback error">%s</div>', esc_html__( 'There was a problem accepting that request. Please try again.', 'buddyboss' ) ), ) ); } else { wp_send_json_success( array( 'feedback' => sprintf( '<div class="bp-feedback success">%s</div>', esc_html__( 'Connection accepted.', 'buddyboss' ) ), 'type' => 'success', 'is_user' => true, 'friend_count' => friends_get_friend_count_for_user( bp_loggedin_user_id() ), ) ); } // Rejecting a friendship } elseif ( ! empty( $_POST['action'] ) && 'friends_reject_friendship' === $_POST['action'] ) { if ( ! friends_reject_friendship( $friend_id ) ) { wp_send_json_error( array( 'feedback' => sprintf( '<div class="bp-feedback error">%s</div>', esc_html__( 'There was a problem rejecting that request. Please try again.', 'buddyboss' ) ), ) ); } else { wp_send_json_success( array( 'feedback' => sprintf( '<div class="bp-feedback success">%s</div>', esc_html__( 'Connection rejected.', 'buddyboss' ) ), 'type' => 'success', 'is_user' => true, 'friend_count' => friends_get_friend_count_for_user( bp_loggedin_user_id() ), ) ); } // Trying to cancel friendship. } elseif ( 'is_friend' === BP_Friends_Friendship::check_is_friend( bp_loggedin_user_id(), $friend_id ) ) { if ( ! friends_remove_friend( bp_loggedin_user_id(), $friend_id ) ) { $response['feedback'] = sprintf( '<div class="bp-feedback error">%s</div>', esc_html__( 'Connection could not be cancelled.', 'buddyboss' ) ); wp_send_json_error( $response ); } else { $is_user = bp_is_my_profile(); if ( ! $is_user ) { $response = array( 'contents' => bp_get_add_friend_button( $friend_id, false, array( 'parent_element' => 'li', 'button_element' => 'button' ) ), 'friend_count' => friends_get_friend_count_for_user( bp_loggedin_user_id() ) ); } else { $response = array( 'feedback' => sprintf( '<div class="bp-feedback success">%s</div>', esc_html__( 'Connection removed.', 'buddyboss' ) ), 'type' => 'success', 'is_user' => $is_user, 'friend_count' => friends_get_friend_count_for_user( bp_loggedin_user_id() ), ); } wp_send_json_success( $response ); } // Trying to request friendship. } elseif ( 'not_friends' === BP_Friends_Friendship::check_is_friend( bp_loggedin_user_id(), $friend_id ) ) { if ( ! friends_add_friend( bp_loggedin_user_id(), $friend_id ) ) { $response['feedback'] = sprintf( '<div class="bp-feedback error">%s</div>', esc_html__( 'Connection could not be requested.', 'buddyboss' ) ); wp_send_json_error( $response ); } else { wp_send_json_success( array( 'contents' => bp_get_add_friend_button( $friend_id, false, array( 'parent_element' => 'li', 'button_element' => 'button' ) ) ) ); } // Trying to cancel pending request. } elseif ( 'pending' === BP_Friends_Friendship::check_is_friend( bp_loggedin_user_id(), $friend_id ) ) { if ( friends_withdraw_friendship( bp_loggedin_user_id(), $friend_id ) ) { wp_send_json_success( array( 'contents' => bp_get_add_friend_button( $friend_id, false, array( 'parent_element' => 'li', 'button_element' => 'button' ) ) ) ); } else { $response['feedback'] = sprintf( '<div class="bp-feedback error">%s</div>', esc_html__( 'Connection request could not be cancelled.', 'buddyboss' ) ); wp_send_json_error( $response ); } // Request already pending. } else { $response['feedback'] = sprintf( '<div class="bp-feedback error">%s</div>', esc_html__( 'Request Pending', 'buddyboss' ) ); wp_send_json_error( $response ); } }
Changelog
Version | Description |
---|---|
BuddyPress 3.0.0 | Introduced. |
Questions?
We're always happy to help with code or other questions you might have! Search our developer docs, contact support, or connect with our sales team.