BP_Media_Album::get_album_data( array $album_ids = array() )

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

Description

Parameters

$album_ids

(Optional) Array of media IDs.

Default value: array()

Return

(array)

Source

File: bp-media/classes/class-bp-media-album.php

	protected static function get_album_data( $album_ids = array() ) {
		global $wpdb;

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

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

		$albums   = array();
		$uncached_ids = bp_get_non_cached_ids( $album_ids, 'bp_media_album' );

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

			// Fetch data from album table, preserving order.
			$queried_adata = $wpdb->get_results( "SELECT * FROM {$bp->media->table_name_albums} WHERE id IN ({$uncached_ids_sql})");

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

		// Now fetch data from the cache.
		foreach ( $album_ids as $album_id ) {
			// Integer casting.
			$album = wp_cache_get( $album_id, 'bp_media_album' );
			if ( ! empty( $album ) ) {
				$album->id       = (int) $album->id;
				$album->user_id  = (int) $album->user_id;
				$album->group_id = (int) $album->group_id;

				$album->media = bp_media_get( array( 'album_id' => $album->id, 'count_total' => true ) );
			}

			$albums[] = $album;
		}

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

		// Associated located user data with albums.
		foreach ( $albums 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 ) ) {
				$albums[ $a_index ]->user_email    = $a_user->user_email;
				$albums[ $a_index ]->user_nicename = $a_user->user_nicename;
				$albums[ $a_index ]->user_login    = $a_user->user_login;
				$albums[ $a_index ]->display_name  = $a_user->display_name;
			}
		}

		return $albums;
	}

Changelog

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