BP_REST_Members_Details_Endpoint::get_item( WP_REST_Request $request )

Retrieve member detail.

Description

Parameters

$request

(Required) Full details about the request.

Return

(WP_REST_Response) | WP_Error

Source

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

	public function get_item( $request ) {
		$retval = array();
		global $bp;

		$current_user_id = $request->get_param( 'id' );
		$this->user_id   = $current_user_id;
		if ( empty( $current_user_id ) ) {
			$current_user_id = bp_loggedin_user_id();
		}

		if ( empty( $current_user_id ) ) {
			return new WP_Error(
				'bp_rest_component_required',
				__( 'Sorry, Invalid member ID.', 'buddyboss' ),
				array(
					'status' => '404',
				)
			);
		}

		$user = bp_rest_get_user( $current_user_id );

		if ( ! $user instanceof WP_User ) {
			return new WP_Error(
				'bp_rest_member_invalid_id',
				__( 'Invalid member ID.', 'buddyboss' ),
				array(
					'status' => 404,
				)
			);
		}

		add_filter( 'bp_displayed_user_id', array( $this, 'bp_rest_get_displayed_user' ), 999 );
		bp_setup_nav();
		remove_filter( 'bp_displayed_user_id', array( $this, 'bp_rest_get_displayed_user' ), 999 );

		$profile_tabs = array();
		$default_tab  = 'profile';
		$navs         = buddypress()->members->nav;

		// if it's nouveau then let it order the tabs.
		if ( function_exists( 'bp_nouveau_set_nav_item_order' ) ) {
			bp_nouveau_set_nav_item_order( $navs, bp_nouveau_get_appearance_settings( 'user_nav_order' ) );
		}

		if ( function_exists( 'bp_nouveau_get_appearance_settings' ) ) {
			$tab         = bp_nouveau_get_appearance_settings( 'user_default_tab' );
			$default_tab = bp_is_active( $tab ) ? $tab : $default_tab;
		}

		$id_map = array(
			'activity' => 'activities',
			'profile'  => 'xprofile',
		);

		if ( ! empty( $navs->get_primary() ) ) {
			foreach ( $navs->get_primary() as $nav ) {

				$name = $nav['name'];
				$id   = $nav['slug'];

				// remove the notification numbers.
				$name = preg_replace( '/^(.*)(<(.*)<\/(.*)>)/', '$1', $name );
				$name = trim( $name );

				if ( isset( $id_map[ $id ] ) ) {
					$id = $id_map[ $id ];
				}

				$tab = array(
					'id'                      => $id,
					'title'                   => $name,
					'default'                 => false,
					'count'                   => ( $this->bp_rest_nav_has_count( $nav ) ? $this->bp_rest_get_nav_count( $nav ) : '' ),
					'show_for_displayed_user' => $nav['show_for_displayed_user'],
					'children'                => array(),
				);

				if ( $default_tab === $nav['slug'] ) {
					$tab['default'] = true;
				}

				$nav_sub = $navs->get_secondary(
					array(
						'parent_slug'     => $id,
						'user_has_access' => true,
					)
				);

				if ( ! empty( $nav_sub ) ) {
					foreach ( $nav_sub as $s_nav ) {
						$sub_name = preg_replace( '/^(.*)(<(.*)<\/(.*)>)/', '$1', $s_nav['name'] );
						$sub_name = trim( $sub_name );
						$sub_nav  = array(
							'id'              => $s_nav['slug'],
							'title'           => $sub_name,
							'count'           => ( $this->bp_rest_nav_has_count( $s_nav ) ? $this->bp_rest_get_nav_count( $s_nav ) : '' ),
							'position'        => $s_nav['position'],
							'user_has_access' => $s_nav['user_has_access'],
						);

						$tab['children'][] = $sub_nav;
					}
				}

				$profile_tabs[] = apply_filters( 'bp_rest_profile_tab', $tab, $nav );
			}
		}

		$retval['tabs'] = array_values( $profile_tabs );

		$response = rest_ensure_response( $retval );

		/**
		 * Fires after a list of members details is fetched via the REST API.
		 *
		 * @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_detail_get_items', $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.