BP_REST_Friends_Endpoint::delete_items( WP_REST_Request $request )

Unfriend a friendship.

Description

Parameters

$request

(Required) Full details about the request.

Return

(WP_REST_Response) | WP_Error

Source

File: bp-friends/classes/class-bp-rest-friends-endpoint.php

558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
public function delete_items( $request ) {
    $request->set_param( 'context', 'edit' );
 
    if ( empty( $request['friend_id'] ) ) {
        return new WP_Error(
            'bp_rest_invalid_id',
            __( 'Invalid ID of the friend member.', 'buddyboss' ),
            array(
                'status' => 404,
            )
        );
    }
 
    // Check if user is valid.
    $user = get_user_by( 'id', $request['friend_id'] );
    if ( ! $user instanceof WP_User ) {
        return new WP_Error(
            'bp_rest_invalid_friend_user',
            __( 'There was a problem confirming if friend user is a valid one.', 'buddyboss' ),
            array(
                'status' => 500,
            )
        );
    }
 
    $user_id   = bp_loggedin_user_id();
    $friend_id = $request['friend_id'];
 
    $friendship_id = BP_Friends_Friendship::get_friendship_id( $user_id, $friend_id );
    $friendship    = $this->get_friendship_object( $friendship_id );
    $previous      = $this->prepare_item_for_response( $friendship, $request );
 
    if (
        'is_friend' === BP_Friends_Friendship::check_is_friend( $user_id, $friend_id )
        && friends_remove_friend( $user_id, $friend_id )
    ) {
        $status = true;
    } else {
        $status = new WP_Error(
            'bp_rest_friends_cannot_unfriend_friendship',
            __( 'Connection could not be cancelled.', 'buddyboss' ),
            array(
                'status' => 500,
            )
        );
    }
 
    // Build the response.
    $response = new WP_REST_Response();
    $response->set_data(
        array(
            'unfriend' => $status,
            'previous' => $previous->get_data(),
        )
    );
 
    /**
     * Fires after a friendship is deleted via the REST API.
     *
     * @param BP_Friends_Friendship $friendship Friendship object.
     * @param WP_REST_Response      $response   The response data.
     * @param WP_REST_Request       $request    The request sent to the API.
     *
     * @since 0.1.0
     */
    do_action( 'bp_rest_friends_delete_items', $friendship, $response, $request );
 
    return $response;
}

Changelog

Changelog
Version Description
0.1.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.