BP_REST_Friends_Endpoint::delete_items( WP_REST_Request $request )

Unfriend a friendship.

Description

Parameters

$request

(WP_REST_Request) (Required) Full details about the request.

Return

(WP_REST_Response) | WP_Error

Source

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

	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.