BP_Media::get_media_data( array $media_ids = array() )
Convert media IDs to media objects, as expected in template loop.
Description
Parameters
- $media_ids
-
(array) (Optional) Array of media IDs.
Default value: array()
Return
(array)
Source
File: bp-media/classes/class-bp-media.php
protected static function get_media_data( $media_ids = array() ) { global $wpdb; // Bail if no media ID's passed. if ( empty( $media_ids ) ) { return array(); } // Get BuddyPress. $bp = buddypress(); $medias = array(); $uncached_ids = bp_get_non_cached_ids( $media_ids, 'bp_media' ); // Prime caches as necessary. if ( ! empty( $uncached_ids ) ) { // Format the media ID's for use in the query below. $uncached_ids_sql = implode( ',', wp_parse_id_list( $uncached_ids ) ); // Fetch data from media table, preserving order. $queried_adata = $wpdb->get_results( "SELECT * FROM {$bp->media->table_name} 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' ); } } // Now fetch data from the cache. foreach ( $media_ids as $media_id ) { // Integer casting. $media = wp_cache_get( $media_id, 'bp_media' ); if ( ! empty( $media ) ) { $media->id = (int) $media->id; $media->blog_id = (int) $media->blog_id; $media->user_id = (int) $media->user_id; $media->attachment_id = (int) $media->attachment_id; $media->album_id = (int) $media->album_id; $media->activity_id = (int) $media->activity_id; $media->group_id = (int) $media->group_id; $media->menu_order = (int) $media->menu_order; //fetch attachment data $attachment_data = new stdClass(); $attachment_data->full = wp_get_attachment_image_url( $media->attachment_id, 'full' ); $attachment_data->thumb = wp_get_attachment_image_url( $media->attachment_id, 'bp-media-thumbnail' ); $attachment_data->activity_thumb = wp_get_attachment_image_url( $media->attachment_id, 'bp-activity-media-thumbnail' ); $attachment_data->meta = wp_get_attachment_metadata( $media->attachment_id ); $media->attachment_data = $attachment_data; } $medias[] = $media; } // Then fetch user data. $user_query = new BP_User_Query( array( 'user_ids' => wp_list_pluck( $medias, 'user_id' ), 'populate_extras' => false, ) ); // Associated located user data with media items. foreach ( $medias 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 ) ) { $medias[ $a_index ]->user_email = $a_user->user_email; $medias[ $a_index ]->user_nicename = $a_user->user_nicename; $medias[ $a_index ]->user_login = $a_user->user_login; $medias[ $a_index ]->display_name = $a_user->display_name; } } return $medias; }
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.