BP_Document::get_document_data( array $document_ids = array() )

Convert document IDs to document objects, as expected in template loop.

Description

Parameters

$document_ids

(array) (Optional) Array of document IDs.

Default value: array()

Return

(array)

Source

File: bp-document/classes/class-bp-document.php

	protected static function get_document_data( $document_ids = array() ) {
		global $wpdb;

		// Bail if no document ID's passed.
		if ( empty( $document_ids ) ) {
			return array();
		}

		// Get BuddyPress.
		$bp = buddypress();

		$documents    = array();
		$uncached_ids = bp_get_non_cached_ids( $document_ids, 'bp_document' );

		// Prime caches as necessary.
		if ( ! empty( $uncached_ids ) ) {
			// Format the document ID's for use in the query below.
			$uncached_ids_sql = implode( ',', wp_parse_id_list( $uncached_ids ) );

			// Fetch data from document table, preserving order.
			$queried_adata = $wpdb->get_results( "SELECT * FROM {$bp->document->table_name} WHERE id IN ({$uncached_ids_sql})" ); // db call ok; no-cache ok;

			// Put that data into the placeholders created earlier,
			// and add it to the cache.
			foreach ( (array) $queried_adata as $adata ) {
				wp_cache_set( $adata->id, $adata, 'bp_document' );
			}
		}

		// Now fetch data from the cache.
		foreach ( $document_ids as $document_id ) {
			// Integer casting.
			$document = wp_cache_get( $document_id, 'bp_document' );
			if ( ! empty( $document ) ) {
				$document->id            = (int) $document->id;
				$document->blog_id       = (int) $document->blog_id;
				$document->user_id       = (int) $document->user_id;
				$document->attachment_id = (int) $document->attachment_id;
				$document->folder_id     = (int) $document->folder_id;
				$document->activity_id   = (int) $document->activity_id;
				$document->group_id      = (int) $document->group_id;
				$document->menu_order    = (int) $document->menu_order;
				$document->parent        = (int) $document->folder_id;
			}

			$group_name = '';
			$visibility = '';
			if ( $document->group_id > 0 ) {
				if ( bp_is_active( 'groups' ) ) {
					$group      = groups_get_group( $document->group_id );
					$group_name = bp_get_group_name( $group );
					$status     = bp_get_group_status( $group );
					if ( 'hidden' === $status || 'private' === $status ) {
						$visibility = esc_html__( 'Group Members', 'buddyboss' );
					} else {
						$visibility = ucfirst( $status );
					}
				} else {
					$visibility = '';
				}
			} else {
				$document_privacy = bp_document_get_visibility_levels();
				if ( 'friends' === $document->privacy && bp_loggedin_user_id() !== (int) $document->user_id ) {
					$visibility = esc_html__( 'Connections', 'buddyboss' );
				} elseif ( 'message' === $document->privacy ) {
					$visibility = esc_html__( 'Message', 'buddyboss' );
				} elseif ( 'forums' === $document->privacy ) {
					$visibility = esc_html__( 'Forums', 'buddyboss' );
				} else {
					$visibility = ( isset( $document_privacy[ $document->privacy ] ) ) ? $document_privacy[ $document->privacy ] : '';
				}
			}

			// fetch attachment data.
			$attachment_data                 = new stdClass();
			$attachment_data->full           = '';
			$attachment_data->thumb          = '';
			$attachment_data->activity_thumb = '';
			$attachment_data->meta           = wp_get_attachment_metadata( $document->attachment_id );
			$document->attachment_data       = $attachment_data;
			$document->group_name            = $group_name;
			$document->visibility            = $visibility;

			$documents[] = $document;
		}

		// Then fetch user data.
		$user_query = new BP_User_Query(
			array(
				'user_ids'        => wp_list_pluck( $documents, 'user_id' ),
				'populate_extras' => false,
			)
		);

		// Associated located user data with document items.
		foreach ( $documents as $a_index => $a_item ) {
			$a_user_id = intval( $a_item->user_id );
			$a_user    = isset( $user_query->results[ $a_user_id ] ) ? $user_query->results[ $a_user_id ] : '';

			if ( ! empty( $a_user ) ) {
				$documents[ $a_index ]->user_email    = $a_user->user_email;
				$documents[ $a_index ]->user_nicename = $a_user->user_nicename;
				$documents[ $a_index ]->user_login    = $a_user->user_login;
				$documents[ $a_index ]->display_name  = $a_user->display_name;
			}
		}

		return $documents;
	}

Changelog

Changelog
Version Description
BuddyBoss 1.4.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.