BP_REST_Friends_Endpoint::update_item( WP_REST_Request $request )

Update, accept, friendship.


Parameters Parameters

$request

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


Top ↑

Return Return

(WP_REST_Response|WP_Error)


Source Source

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

	public function update_item( $request ) {
		$user = get_user_by( 'id', $request->get_param( 'id' ) );

		// Check if user is valid.
		if ( false === $user ) {
			return new WP_Error(
				'bp_rest_friends_update_item_failed',
				__( 'There was a problem confirming if user is valid.', 'buddypress' ),
				array(
					'status' => 404,
				)
			);
		}

		// Get friendship.
		$friendship = $this->get_friendship_object(
			BP_Friends_Friendship::get_friendship_id( bp_loggedin_user_id(), $user->ID )
		);

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

		// Accept friendship.
		if ( false === friends_accept_friendship( $friendship->id ) ) {
			return new WP_Error(
				'bp_rest_friends_cannot_update_item',
				__( 'Could not accept friendship.', 'buddypress' ),
				array(
					'status' => 500,
				)
			);
		}

		// Getting new, updated, friendship object.
		$friendship = $this->get_friendship_object( $friendship->id );

		$retval = array(
			$this->prepare_response_for_collection(
				$this->prepare_item_for_response( $friendship, $request )
			),
		);

		$response = rest_ensure_response( $retval );

		/**
		 * Fires after a friendship is updated via the REST API.
		 *
		 * @since 6.0.0
		 *
		 * @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.
		 */
		do_action( 'bp_rest_friends_update_item', $friendship, $response, $request );

		return $response;
	}


Top ↑

Changelog Changelog

Changelog
Version Description
6.0.0 Introduced.

Top ↑

User Contributed Notes User Contributed Notes

You must log in before being able to contribute a note or feedback.