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
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 | 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.