| 96 | | // Cast fid as an integer. |
| 97 | | $friend_id = (int) $_POST['item_id']; |
| 98 | | |
| 99 | | $user = get_user_by( 'id', $friend_id ); |
| 100 | | if ( ! $user ) { |
| 101 | | wp_send_json_error( |
| 102 | | array( |
| 103 | | 'feedback' => sprintf( |
| 104 | | '<div class="bp-feedback error">%s</div>', |
| 105 | | esc_html__( 'No member found by that ID.', 'buddypress' ) |
| 106 | | ), |
| 107 | | ) |
| 108 | | ); |
| 109 | | } |
| | 96 | // If the action relates to a friendship, the item ID is a friendship ID. |
| | 97 | if ( isset( $_POST['action'] ) && ( $_POST['action'] === 'friends_accept_friendship' || $_POST['action'] === 'friends_reject_friendship' ) ) { |
| | 98 | $friendship_id = (int) $_POST['item_id']; |
| | 99 | |
| | 100 | if ( 'friends_accept_friendship' === $_POST['action'] ) { |
| | 101 | if ( ! friends_accept_friendship( $friendship_id ) ) { |
| | 102 | wp_send_json_error( |
| | 103 | array( |
| | 104 | 'feedback' => sprintf( |
| | 105 | '<div class="bp-feedback error">%s</div>', |
| | 106 | esc_html__( 'There was a problem accepting that request. Please try again.', 'buddypress' ) |
| | 107 | ), |
| | 108 | ) |
| | 109 | ); |
| | 110 | } else { |
| | 111 | wp_send_json_success( |
| | 112 | array( |
| | 113 | 'feedback' => sprintf( |
| | 114 | '<div class="bp-feedback success">%s</div>', |
| | 115 | esc_html__( 'Friendship accepted.', 'buddypress' ) |
| | 116 | ), |
| | 117 | 'type' => 'success', |
| | 118 | 'is_user' => true, |
| | 119 | ) |
| | 120 | ); |
| | 121 | } |
| 111 | | // In the 2 first cases the $friend_id is a friendship id. |
| 112 | | if ( ! empty( $_POST['action'] ) && 'friends_accept_friendship' === $_POST['action'] ) { |
| 113 | | if ( ! friends_accept_friendship( $friend_id ) ) { |
| 114 | | wp_send_json_error( |
| 115 | | array( |
| 116 | | 'feedback' => sprintf( |
| 117 | | '<div class="bp-feedback error">%s</div>', |
| 118 | | esc_html__( 'There was a problem accepting that request. Please try again.', 'buddypress' ) |
| 119 | | ), |
| 120 | | ) |
| 121 | | ); |
| | 123 | // Rejecting a friendship |
| | 124 | } elseif ( 'friends_reject_friendship' === $_POST['action'] ) { |
| | 125 | if ( ! friends_reject_friendship( $friendship_id ) ) { |
| | 126 | wp_send_json_error( |
| | 127 | array( |
| | 128 | 'feedback' => sprintf( |
| | 129 | '<div class="bp-feedback error">%s</div>', |
| | 130 | esc_html__( 'There was a problem rejecting that request. Please try again.', 'buddypress' ) |
| | 131 | ), |
| | 132 | ) |
| | 133 | ); |
| | 134 | } else { |
| | 135 | wp_send_json_success( |
| | 136 | array( |
| | 137 | 'feedback' => sprintf( |
| | 138 | '<div class="bp-feedback success">%s</div>', |
| | 139 | esc_html__( 'Friendship rejected.', 'buddypress' ) |
| | 140 | ), |
| | 141 | 'type' => 'success', |
| | 142 | 'is_user' => true, |
| | 143 | ) |
| | 144 | ); |
| | 145 | } |
| 159 | | // Trying to cancel friendship. |
| 160 | | } elseif ( 'is_friend' === BP_Friends_Friendship::check_is_friend( bp_loggedin_user_id(), $friend_id ) ) { |
| 161 | | if ( ! friends_remove_friend( bp_loggedin_user_id(), $friend_id ) ) { |
| 162 | | $response['feedback'] = sprintf( |
| 163 | | '<div class="bp-feedback error">%s</div>', |
| 164 | | esc_html__( 'Friendship could not be cancelled.', 'buddypress' ) |
| 165 | | ); |
| | 166 | if ( 'is_friend' === BP_Friends_Friendship::check_is_friend( bp_loggedin_user_id(), $user_id ) ) { |
| | 167 | if ( ! friends_remove_friend( bp_loggedin_user_id(), $user_id ) ) { |
| | 168 | $response['feedback'] = sprintf( |
| | 169 | '<div class="bp-feedback error">%s</div>', |
| | 170 | esc_html__( 'Friendship could not be cancelled.', 'buddypress' ) |
| | 171 | ); |
| | 172 | |
| | 173 | wp_send_json_error( $response ); |
| | 174 | } else { |
| | 175 | $is_user = bp_is_my_profile(); |
| | 176 | |
| | 177 | if ( ! $is_user ) { |
| | 178 | $response = array( 'contents' => bp_get_add_friend_button( $user_id ) ); |
| | 179 | } else { |
| | 180 | $response = array( |
| | 181 | 'feedback' => sprintf( |
| | 182 | '<div class="bp-feedback success">%s</div>', |
| | 183 | esc_html__( 'Friendship cancelled.', 'buddypress' ) |
| | 184 | ), |
| | 185 | 'type' => 'success', |
| | 186 | 'is_user' => $is_user, |
| | 187 | ); |
| | 188 | } |
| | 189 | |
| | 190 | wp_send_json_success( $response ); |
| | 191 | } |
| | 192 | |
| | 193 | // Trying to request friendship. |
| | 194 | } elseif ( 'not_friends' === BP_Friends_Friendship::check_is_friend( bp_loggedin_user_id(), $user_id ) ) { |
| | 195 | if ( ! friends_add_friend( bp_loggedin_user_id(), $user_id ) ) { |
| | 196 | $response['feedback'] = sprintf( |
| | 197 | '<div class="bp-feedback error">%s</div>', |
| | 198 | esc_html__( 'Friendship could not be requested.', 'buddypress' ) |
| | 199 | ); |
| | 200 | |
| | 201 | wp_send_json_error( $response ); |
| | 202 | } else { |
| | 203 | wp_send_json_success( array( 'contents' => bp_get_add_friend_button( $user_id ) ) ); |
| | 204 | } |
| | 205 | |
| | 206 | // Trying to cancel pending request. |
| | 207 | } elseif ( 'pending' === BP_Friends_Friendship::check_is_friend( bp_loggedin_user_id(), $user_id ) ) { |
| | 208 | if ( friends_withdraw_friendship( bp_loggedin_user_id(), $user_id ) ) { |
| | 209 | wp_send_json_success( array( 'contents' => bp_get_add_friend_button( $user_id ) ) ); |
| | 210 | } else { |
| | 211 | $response['feedback'] = sprintf( |
| | 212 | '<div class="bp-feedback error">%s</div>', |
| | 213 | esc_html__( 'Friendship request could not be cancelled.', 'buddypress' ) |
| | 214 | ); |
| 184 | | wp_send_json_success( $response ); |
| 185 | | } |
| 186 | | |
| 187 | | // Trying to request friendship. |
| 188 | | } elseif ( 'not_friends' === BP_Friends_Friendship::check_is_friend( bp_loggedin_user_id(), $friend_id ) ) { |
| 189 | | if ( ! friends_add_friend( bp_loggedin_user_id(), $friend_id ) ) { |
| 190 | | $response['feedback'] = sprintf( |
| 191 | | '<div class="bp-feedback error">%s</div>', |
| 192 | | esc_html__( 'Friendship could not be requested.', 'buddypress' ) |
| 193 | | ); |
| 194 | | |
| 195 | | wp_send_json_error( $response ); |
| 196 | | } else { |
| 197 | | wp_send_json_success( array( 'contents' => bp_get_add_friend_button( $friend_id ) ) ); |
| 198 | | } |
| 199 | | |
| 200 | | // Trying to cancel pending request. |
| 201 | | } elseif ( 'pending' === BP_Friends_Friendship::check_is_friend( bp_loggedin_user_id(), $friend_id ) ) { |
| 202 | | if ( friends_withdraw_friendship( bp_loggedin_user_id(), $friend_id ) ) { |
| 203 | | wp_send_json_success( array( 'contents' => bp_get_add_friend_button( $friend_id ) ) ); |
| 204 | | } else { |
| 205 | | $response['feedback'] = sprintf( |
| 206 | | '<div class="bp-feedback error">%s</div>', |
| 207 | | esc_html__( 'Friendship request could not be cancelled.', 'buddypress' ) |
| 208 | | ); |
| 209 | | |
| 210 | | wp_send_json_error( $response ); |
| | 226 | wp_send_json_error( $response ); |
| | 227 | } |