BP_REST_Friends_Endpoint::delete_item( WP_REST_Request $request )

Reject/withdraw 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_item( $request ) {
		$request->set_param( 'context', 'edit' );

		// Get friendship object.
		$friendship = $this->get_friendship_object( $request['id'] );

		if ( ! $friendship || empty( $friendship->id ) ) {
			return new WP_Error(
				'bp_rest_invalid_id',
				__( 'Invalid friendship ID.', 'buddyboss' ),
				array(
					'status' => 404,
				)
			);
		}

		$user_id  = bp_loggedin_user_id();
		$previous = $this->prepare_item_for_response( $friendship, $request );

		/**
		 * If this change is being initiated by the initiator,
		 * use the `reject` function.
		 *
		 * This is the user who requested the friendship, and is doing the withdrawing.
		 */
		if ( $user_id === $friendship->initiator_user_id ) {
			$deleted = friends_withdraw_friendship( $friendship->initiator_user_id, $friendship->friend_user_id );
		} else {
			/**
			 * Otherwise, this change is being initiated by the user, friend,
			 * who received the friendship reject.
			 */
			$deleted = friends_reject_friendship( $friendship->id );
		}

		if ( ! $deleted ) {
			return new WP_Error(
				'bp_rest_friends_cannot_delete_item',
				__( 'Could not delete friendship.', 'buddyboss' ),
				array(
					'status' => 500,
				)
			);
		}

		// Build the response.
		$response = new WP_REST_Response();
		$response->set_data(
			array(
				'deleted'  => true,
				'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_item', $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.