BP_REST_Members_Actions_Endpoint::update_item( WP_REST_Request $request )

Checks if a given request has access create members.

Description

Parameters

$request

(Required) Full details about the request.

Return

(bool|WP_Error)

Source

File: bp-members/classes/class-bp-rest-members-actions-endpoint.php

	public function update_item( $request ) {
		// Setting context.
		$request->set_param( 'context', 'edit' );

		$args = array(
			'leader_id'   => (int) $request['id'],
			'follower_id' => get_current_user_id(),
		);

		$response           = array();
		$retval             = array();
		$response['action'] = false;

		/**
		 * Filter the query arguments for the request.
		 *
		 * @param array $args Key value array of query var to query value.
		 * @param WP_REST_Request $request The request sent to the API.
		 *
		 * @since 0.1.0
		 */
		$args = apply_filters( 'bp_rest_members_action_query_args', $args, $request );

		$action = $request['action'];

		switch ( $action ) {
			case 'follow':
				if ( ! $this->bp_rest_follow_is_following( $args ) ) {
					$result             = $this->bp_rest_follow_start_following( $args );
					$response['action'] = ( ! empty( $result ) ? true : false );
				}
				break;
			case 'unfollow':
				if ( $this->bp_rest_follow_is_following( $args ) ) {
					$result             = $this->bp_rest_follow_stop_following( $args );
					$response['action'] = ( ! empty( $result ) ? true : false );
				}
				break;
		}

		$member_query = bp_core_get_users( array( 'include' => (int) $request['id'] ) );
		$members      = $member_query['users'];

		$request->set_param( 'context', 'view' );

		foreach ( $members as $member ) {
			$retval[] = $this->prepare_response_for_collection(
				$this->members_endpoint->prepare_item_for_response( $member, $request )
			);
		}

		$response['data'] = ( count( $members ) > 1 ? $retval : ( ! empty( $retval ) ? $retval[0] : '' ) );

		$response = rest_ensure_response( $response );

		/**
		 * Fires after a Member action is updated via the REST API.
		 *
		 * @param BP_XProfile_Field $field Created field 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_members_action_update_item', $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.