BP_Document::get_folder_data( array $folder_ids = array() )

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

Description

Parameters

$folder_ids

(Optional) Array of document IDs.

Default value: array()

Return

(array)

Source

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

	protected static function get_folder_data( $folder_ids = array() ) {
		global $wpdb;

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

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

		$documents    = array();
		$uncached_ids = bp_get_non_cached_ids( $folder_ids, 'bp_document_folder' );

		// 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_folder} 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_folder' );
			}
		}

		// Now fetch data from the cache.
		foreach ( $folder_ids as $document_id ) {
			// Integer casting.
			$document = wp_cache_get( $document_id, 'bp_document_folder' );
			if ( ! empty( $document ) ) {
				$document->id           = (int) $document->id;
				$document->user_id      = (int) $document->user_id;
				$document->group_id     = (int) $document->group_id;
				$document->date_created = $document->date_created;
				$document->title        = $document->title;
				$document->privacy      = $document->privacy;
				$document->parent       = $document->parent;
				$document->folder_id    = (int) $document->id;

				if ( (int) $document->group_id > 0 ) {
					$document->folder = 'group';
					if ( bp_is_active( 'groups' ) ) {
						$group              = groups_get_group( array( 'group_id' => $document->group_id ) );
						$document->link     = bp_get_group_permalink( $group ) . bp_get_document_slug() . '/folder/' . (int) $document->id;
					}
					$document->link = '';

				} else {
					$document->folder = 'profile';
					$document->link   = bp_core_get_user_domain( (int) $document->user_id ) . bp_get_document_slug() . '/folder/' . (int) $document->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' );
				} else {
					$visibility = (isset( $document_privacy[ $document->privacy ] ) ) ? $document_privacy[ $document->privacy ] : $document->privacy ;
				}
			}

			$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.