BP_REST_Members_Endpoint::user_data( WP_User $user, string $context, WP_REST_Request $request )

Method to facilitate fetching of user data.


Description Description

This was abstracted to be used in other BuddyPress endpoints.


Top ↑

Parameters Parameters

$user

(WP_User) (Required) User object.

$context

(string) (Required) The context of the request. Defaults to 'view'.

$request

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


Top ↑

Return Return

(array)


Source Source

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

	public function user_data( $user, $context, $request ) {
		if ( ! $context ) {
			$context = 'view';
		}

		$data = array(
			'id'                     => $user->ID,
			'name'                   => $user->display_name,
			'user_login'             => $user->user_login,
			'link'                   => bp_core_get_user_domain( $user->ID, $user->user_nicename, $user->user_login ),
			'member_types'           => bp_get_member_type( $user->ID, false ),
			'roles'                  => array(),
			'capabilities'           => array(),
			'extra_capabilities'     => array(),
			'registered_date'        => '',
			'xprofile'               => $this->xprofile_data( $user->ID ),
			'friendship_status'      => false,
			'friendship_status_slug' => '',
		);

		if ( $request->get_param( 'populate_extras' ) ) {
			$data['last_activity']['timediff'] = '';
			$data['last_activity']['date']     = '';

			if ( get_current_user_id() === $user->ID ) {
				$right_now                         = gmdate( 'Y-m-d H:i:s', bp_core_current_time( true, 'timestamp' ) );
				$data['last_activity']['timediff'] = bp_core_time_since( $right_now );
				$data['last_activity']['date']     = bp_rest_prepare_date_response( $right_now );

			} elseif ( $user->last_activity ) {
				$data['last_activity']['timediff'] = bp_core_time_since( $user->last_activity );
				$data['last_activity']['date']     = bp_rest_prepare_date_response( $user->last_activity );
			}

			if ( bp_is_active( 'activity' ) ) {
				$data['latest_update'] = array(
					'id'       => 0,
					'raw'      => '',
					'rendered' => '',
				);

				if ( isset( $user->latest_update ) && $user->latest_update ) {
					$activity_data = maybe_unserialize( $user->latest_update );

					if ( isset( $activity_data['id'], $activity_data['content'] ) ) {
						$data['latest_update']['id']       = $activity_data['id'];
						$data['latest_update']['raw']      = $activity_data['content'];
						$data['latest_update']['rendered'] = apply_filters( 'bp_get_activity_content', $activity_data['content'] );
					}
				}
			}

			if ( bp_is_active( 'friends' ) && isset( $user->total_friend_count ) && $user->total_friend_count ) {
				$data['total_friend_count'] = absint( $user->total_friend_count );
			}
		}

		// Friends related fields.
		if ( bp_is_active( 'friends' ) && get_current_user_id() !== $user->ID ) {
			$friendship_status = friends_check_friendship_status( get_current_user_id(), $user->ID );

			$data['friendship_status_slug'] = $friendship_status;
			$data['friendship_status']      = ( 'is_friend' === $friendship_status );
		}

		if ( 'edit' === $context && current_user_can( 'list_users' ) ) {
			$data['registered_date']    = bp_rest_prepare_date_response( $user->data->user_registered );
			$data['roles']              = (array) array_values( $user->roles );
			$data['capabilities']       = (array) array_keys( $user->allcaps );
			$data['extra_capabilities'] = (array) array_keys( $user->caps );
		}

		// The name used for that user in @-mentions.
		if ( bp_is_active( 'activity' ) ) {
			$data['mention_name'] = bp_activity_get_user_mentionname( $user->ID );
		}

		// Avatars.
		if ( true === buddypress()->avatar->show_avatars ) {
			$data['avatar_urls'] = array(
				'full'  => bp_core_fetch_avatar(
					array(
						'item_id' => $user->ID,
						'html'    => false,
						'type'    => 'full',
					)
				),
				'thumb' => bp_core_fetch_avatar(
					array(
						'item_id' => $user->ID,
						'html'    => false,
						'type'    => 'thumb',
					)
				),
			);
		}

		// Fallback.
		if ( false === $data['member_types'] ) {
			$data['member_types'] = array();
		}

		return $data;
	}


Top ↑

Changelog Changelog

Changelog
Version Description
7.0.0 Add the $request parameter.
5.0.0 Introduced.

Top ↑

User Contributed Notes User Contributed Notes

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