BP_Media
Database interaction class for the BuddyBoss media component.
Description
Instance methods are available for creating/editing an media, static methods for querying media.
Source
File: bp-media/classes/class-bp-media.php
class BP_Media { /** Properties ************************************************************/ /** * ID of the media item. * * @since BuddyBoss 1.0.0 * @var int */ var $id; /** * Blog ID of the media item. * * @since BuddyBoss 1.0.0 * @var int */ var $blog_id; /** * Attachment ID of the media item. * * @since BuddyBoss 1.0.0 * @var int */ var $attachment_id; /** * User ID of the media item. * * @since BuddyBoss 1.0.0 * @var int */ var $user_id; /** * Title of the media item. * * @since BuddyBoss 1.0.0 * @var string */ var $title; /** * Album ID of the media item. * * @since BuddyBoss 1.0.0 * @var int */ var $album_id; /** * Activity ID of the media item. * * @since BuddyBoss 1.0.0 * @var int */ var $activity_id; /** * Group ID of the media item. * * @since BuddyBoss 1.0.0 * @var int */ var $group_id; /** * Privacy of the media item. * * @since BuddyBoss 1.0.0 * @var string */ var $privacy; /** * Menu order of the media item. * * @since BuddyBoss 1.0.0 * @var int */ var $menu_order; /** * Upload date of the media item. * * @since BuddyBoss 1.0.0 * @var string */ var $date_created; /** * Error holder. * * @since BuddyBoss 1.0.0 * * @var WP_Error */ public $errors; /** * Error type to return. Either 'bool' or 'wp_error'. * * @since BuddyBoss 1.0.0 * * @var string */ public $error_type = 'bool'; /** * Constructor method. * * @since BuddyBoss 1.0.0 * * @param int|bool $id Optional. The ID of a specific activity item. */ function __construct( $id = false ) { // Instantiate errors object. $this->errors = new WP_Error; if ( !empty( $id ) ) { $this->id = (int) $id; $this->populate(); } } /** * Populate the object with data about the specific media item. * * @since BuddyBoss 1.0.0 */ public function populate() { global $wpdb; $row = wp_cache_get( $this->id, 'bp_media' ); if ( false === $row ) { $bp = buddypress(); $row = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM {$bp->media->table_name} WHERE id = %d", $this->id ) ); wp_cache_set( $this->id, $row, 'bp_media' ); } if ( empty( $row ) ) { $this->id = 0; return; } $this->id = (int) $row->id; $this->blog_id = (int) $row->blog_id; $this->attachment_id = (int) $row->attachment_id; $this->user_id = (int) $row->user_id; $this->title = $row->title; $this->album_id = (int) $row->album_id; $this->activity_id = (int) $row->activity_id; $this->group_id = (int) $row->group_id; $this->privacy = $row->privacy; $this->menu_order = (int) $row->menu_order; $this->date_created = $row->date_created; } /** * Save the media item to the database. * * @since BuddyBoss 1.0.0 * * @return WP_Error|bool True on success. */ public function save() { global $wpdb; $bp = buddypress(); $this->id = apply_filters_ref_array( 'bp_media_id_before_save', array( $this->id, &$this ) ); $this->blog_id = apply_filters_ref_array( 'bp_media_blog_id_before_save', array( $this->blog_id, &$this ) ); $this->attachment_id = apply_filters_ref_array( 'bp_media_attachment_id_before_save', array( $this->attachment_id, &$this ) ); $this->user_id = apply_filters_ref_array( 'bp_media_user_id_before_save', array( $this->user_id, &$this ) ); $this->title = apply_filters_ref_array( 'bp_media_title_before_save', array( $this->title, &$this ) ); $this->album_id = apply_filters_ref_array( 'bp_media_album_id_before_save', array( $this->album_id, &$this ) ); $this->activity_id = apply_filters_ref_array( 'bp_media_activity_id_before_save', array( $this->activity_id, &$this ) ); $this->group_id = apply_filters_ref_array( 'bp_media_group_id_before_save', array( $this->group_id, &$this ) ); $this->privacy = apply_filters_ref_array( 'bp_media_privacy_before_save', array( $this->privacy, &$this ) ); $this->menu_order = apply_filters_ref_array( 'bp_media_menu_order_before_save', array( $this->menu_order, &$this ) ); $this->date_created = apply_filters_ref_array( 'bp_media_date_created_before_save', array( $this->date_created, &$this ) ); /** * Fires before the current media item gets saved. * * Please use this hook to filter the properties above. Each part will be passed in. * * @since BuddyBoss 1.0.0 * * @param BP_Media $this Current instance of the media item being saved. Passed by reference. */ do_action_ref_array( 'bp_media_before_save', array( &$this ) ); if ( 'wp_error' === $this->error_type && $this->errors->get_error_code() ) { return $this->errors; } if ( empty( $this->attachment_id ) //|| empty( $this->activity_id ) //todo: when forums media is saving, it should have activity id assigned if settings enabled need to check this ) { if ( 'bool' === $this->error_type ) { return false; } else { if ( empty( $this->activity_id ) ) { $this->errors->add( 'bp_media_missing_activity' ); } else { $this->errors->add( 'bp_media_missing_attachment' ); } return $this->errors; } } // If we have an existing ID, update the media item, otherwise insert it. if ( ! empty( $this->id ) ) { $q = $wpdb->prepare( "UPDATE {$bp->media->table_name} SET blog_id = %d, attachment_id = %d, user_id = %d, title = %s, album_id = %d, activity_id = %d, group_id = %d, privacy = %s, menu_order = %d, date_created = %s WHERE id = %d", $this->blog_id, $this->attachment_id, $this->user_id, $this->title, $this->album_id, $this->activity_id, $this->group_id, $this->privacy, $this->menu_order, $this->date_created, $this->id ); } else { $q = $wpdb->prepare( "INSERT INTO {$bp->media->table_name} ( blog_id, attachment_id, user_id, title, album_id, activity_id, group_id, privacy, menu_order, date_created ) VALUES ( %d, %d, %d, %s, %d, %d, %d, %s, %d, %s )", $this->blog_id, $this->attachment_id, $this->user_id, $this->title, $this->album_id, $this->activity_id, $this->group_id, $this->privacy, $this->menu_order, $this->date_created ); } if ( false === $wpdb->query( $q ) ) { return false; } // If this is a new media item, set the $id property. if ( empty( $this->id ) ) { $this->id = $wpdb->insert_id; } /** * Fires after an media item has been saved to the database. * * @since BuddyBoss 1.0.0 * * @param BP_Media $this Current instance of media item being saved. Passed by reference. */ do_action_ref_array( 'bp_media_after_save', array( &$this ) ); return true; } /** Static Methods ***************************************************/ /** * Get media items, as specified by parameters. * * @since BuddyBoss 1.0.0 * * @param array $args { * An array of arguments. All items are optional. * @type int $page Which page of results to fetch. Using page=1 without per_page will result * in no pagination. Default: 1. * @type int|bool $per_page Number of results per page. Default: 20. * @type int|bool $max Maximum number of results to return. Default: false (unlimited). * @type string $fields Media fields to return. Pass 'ids' to get only the media IDs. * 'all' returns full media objects. * @type string $sort ASC or DESC. Default: 'DESC'. * @type string $order_by Column to order results by. * @type array $exclude Array of media IDs to exclude. Default: false. * @type string $search_terms Limit results by a search term. Default: false. * @type string|bool $count_total If true, an additional DB query is run to count the total media items * for the query. Default: false. * } * @return array The array returned has two keys: * - 'total' is the count of located medias * - 'medias' is an array of the located medias */ public static function get( $args = array() ) { global $wpdb; $bp = buddypress(); $r = wp_parse_args( $args, array( 'page' => 1, // The current page. 'per_page' => 20, // Media items per page. 'max' => false, // Max number of items to return. 'fields' => 'all', // Fields to include. 'sort' => 'DESC', // ASC or DESC. 'order_by' => 'date_created', // Column to order by. 'exclude' => false, // Array of ids to exclude. 'in' => false, // Array of ids to limit query by (IN). 'search_terms' => false, // Terms to search by. 'privacy' => false, // public, loggedin, onlyme, friends, grouponly, message. 'count_total' => false, // Whether or not to use count_total. ) ); // Select conditions. $select_sql = "SELECT DISTINCT m.id"; $from_sql = " FROM {$bp->media->table_name} m"; $join_sql = ''; // Where conditions. $where_conditions = array(); // Searching. if ( $r['search_terms'] ) { $search_terms_like = '%' . bp_esc_like( $r['search_terms'] ) . '%'; $where_conditions['search_sql'] = $wpdb->prepare( 'm.title LIKE %s', $search_terms_like ); /** * Filters whether or not to include users for search parameters. * * @since BuddyBoss 1.0.0 * * @param bool $value Whether or not to include user search. Default false. */ if ( apply_filters( 'bp_media_get_include_user_search', false ) ) { $user_search = get_user_by( 'slug', $r['search_terms'] ); if ( false !== $user_search ) { $user_id = $user_search->ID; $where_conditions['search_sql'] .= $wpdb->prepare( ' OR m.user_id = %d', $user_id ); } } } // Sorting. $sort = $r['sort']; if ( $sort != 'ASC' && $sort != 'DESC' ) { $sort = 'DESC'; } switch( $r['order_by'] ) { case 'id' : case 'user_id' : case 'blog_id' : case 'attachment_id' : case 'title' : case 'album_id' : case 'activity_id' : case 'group_id' : case 'menu_order' : break; default : $r['order_by'] = 'date_created'; break; } $order_by = 'm.' . $r['order_by']; // Exclude specified items. if ( ! empty( $r['exclude'] ) ) { $exclude = implode( ',', wp_parse_id_list( $r['exclude'] ) ); $where_conditions['exclude'] = "m.id NOT IN ({$exclude})"; } // The specific ids to which you want to limit the query. if ( ! empty( $r['in'] ) ) { $in = implode( ',', wp_parse_id_list( $r['in'] ) ); $where_conditions['in'] = "m.id IN ({$in})"; // we want to disable limit query when include media ids $r['page'] = false; $r['per_page'] = false; } if ( ! empty( $r['activity_id'] ) ) { $where_conditions['activity'] = "m.activity_id = {$r['activity_id']}"; } // existing-media check to query media which has no albums assigned if ( ! empty( $r['album_id'] ) && 'existing-media' != $r['album_id'] ) { $where_conditions['album'] = "m.album_id = {$r['album_id']}"; } else if ( ! empty( $r['album_id'] ) && 'existing-media' == $r['album_id'] ) { $where_conditions['album'] = "m.album_id = 0"; } if ( ! empty( $r['user_id'] ) ) { $where_conditions['user'] = "m.user_id = {$r['user_id']}"; } if ( ! empty( $r['group_id'] ) ) { $where_conditions['user'] = "m.group_id = {$r['group_id']}"; } if ( ! empty( $r['privacy'] ) ) { $privacy = "'" . implode ( "', '", $r['privacy'] ) . "'"; $where_conditions['privacy'] = "m.privacy IN ({$privacy})"; } /** * Filters the MySQL WHERE conditions for the Media items get method. * * @since BuddyBoss 1.0.0 * * @param array $where_conditions Current conditions for MySQL WHERE statement. * @param array $r Parsed arguments passed into method. * @param string $select_sql Current SELECT MySQL statement at point of execution. * @param string $from_sql Current FROM MySQL statement at point of execution. * @param string $join_sql Current INNER JOIN MySQL statement at point of execution. */ $where_conditions = apply_filters( 'bp_media_get_where_conditions', $where_conditions, $r, $select_sql, $from_sql, $join_sql ); if ( empty( $where_conditions ) ) { $where_conditions['2'] = '2'; } // Join the where conditions together. $where_sql = 'WHERE ' . join( ' AND ', $where_conditions ); /** * Filter the MySQL JOIN clause for the main media query. * * @since BuddyBoss 1.0.0 * * @param string $join_sql JOIN clause. * @param array $r Method parameters. * @param string $select_sql Current SELECT MySQL statement. * @param string $from_sql Current FROM MySQL statement. * @param string $where_sql Current WHERE MySQL statement. */ $join_sql = apply_filters( 'bp_media_get_join_sql', $join_sql, $r, $select_sql, $from_sql, $where_sql ); // Sanitize page and per_page parameters. $page = absint( $r['page'] ); $per_page = absint( $r['per_page'] ); $retval = array( 'medias' => null, 'total' => null, 'has_more_items' => null, ); // Query first for media IDs. $media_ids_sql = "{$select_sql} {$from_sql} {$join_sql} {$where_sql} ORDER BY {$order_by} {$sort}, m.id {$sort}"; if ( ! empty( $per_page ) && ! empty( $page ) ) { // We query for $per_page + 1 items in order to // populate the has_more_items flag. $media_ids_sql .= $wpdb->prepare( " LIMIT %d, %d", absint( ( $page - 1 ) * $per_page ), $per_page + 1 ); } /** * Filters the paged media MySQL statement. * * @since BuddyBoss 1.0.0 * * @param string $media_ids_sql MySQL statement used to query for Media IDs. * @param array $r Array of arguments passed into method. */ $media_ids_sql = apply_filters( 'bp_media_paged_activities_sql', $media_ids_sql, $r ); $cache_group = 'bp_media'; $cached = bp_core_get_incremented_cache( $media_ids_sql, $cache_group ); if ( false === $cached ) { $media_ids = $wpdb->get_col( $media_ids_sql ); bp_core_set_incremented_cache( $media_ids_sql, $cache_group, $media_ids ); } else { $media_ids = $cached; } $retval['has_more_items'] = ! empty( $per_page ) && count( $media_ids ) > $per_page; // If we've fetched more than the $per_page value, we // can discard the extra now. if ( ! empty( $per_page ) && count( $media_ids ) === $per_page + 1 ) { array_pop( $media_ids ); } if ( 'ids' === $r['fields'] ) { $medias = array_map( 'intval', $media_ids ); } else { $medias = self::get_media_data( $media_ids ); } if ( 'ids' !== $r['fields'] ) { // Get the fullnames of users so we don't have to query in the loop. //$medias = self::append_user_fullnames( $medias ); // Pre-fetch data associated with media users and other objects. $medias = BP_Media::prefetch_object_data( $medias ); } $retval['medias'] = $medias; // If $max is set, only return up to the max results. if ( ! empty( $r['count_total'] ) ) { /** * Filters the total media MySQL statement. * * @since BuddyBoss 1.0.0 * * @param string $value MySQL statement used to query for total medias. * @param string $where_sql MySQL WHERE statement portion. * @param string $sort Sort direction for query. */ $total_medias_sql = apply_filters( 'bp_media_total_medias_sql', "SELECT count(DISTINCT m.id) FROM {$bp->media->table_name} m {$join_sql} {$where_sql}", $where_sql, $sort ); $cached = bp_core_get_incremented_cache( $total_medias_sql, $cache_group ); if ( false === $cached ) { $total_medias = $wpdb->get_var( $total_medias_sql ); bp_core_set_incremented_cache( $total_medias_sql, $cache_group, $total_medias ); } else { $total_medias = $cached; } if ( !empty( $r['max'] ) ) { if ( (int) $total_medias > (int) $r['max'] ) { $total_medias = $r['max']; } } $retval['total'] = $total_medias; } return $retval; } /** * Convert media IDs to media objects, as expected in template loop. * * @since BuddyBoss 1.0.0 * * @param array $media_ids Array of media IDs. * @return array */ 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; } /** * Append xProfile fullnames to an media array. * * @since BuddyBoss 1.0.0 * * @param array $medias Medias array. * @return array */ protected static function append_user_fullnames( $medias ) { if ( bp_is_active( 'xprofile' ) && ! empty( $medias ) ) { $media_user_ids = wp_list_pluck( $medias, 'user_id' ); if ( ! empty( $media_user_ids ) ) { $fullnames = bp_core_get_user_displaynames( $media_user_ids ); if ( ! empty( $fullnames ) ) { foreach ( (array) $medias as $i => $media ) { if ( ! empty( $fullnames[ $media->user_id ] ) ) { $medias[ $i ]->user_fullname = $fullnames[ $media->user_id ]; } } } } } return $medias; } /** * Pre-fetch data for objects associated with media items. * * Media items are associated with users, and often with other * BuddyPress data objects. Here, we pre-fetch data about these * associated objects, so that inline lookups - done primarily when * building action strings - do not result in excess database queries. * * @since BuddyBoss 1.0.0 * * @param array $medias Array of media. * @return array $medias Array of media. */ protected static function prefetch_object_data( $medias ) { /** * Filters inside prefetch_object_data method to aid in pre-fetching object data associated with media item. * * @since BuddyBoss 1.0.0 * * @param array $medias Array of media. */ return apply_filters( 'bp_media_prefetch_object_data', $medias ); } /** * Delete media items from the database. * * To delete a specific media item, pass an 'id' parameter. * Otherwise use the filters. * * @since BuddyBoss 1.0.0 * * @param array $args { * @int $id Optional. The ID of a specific item to delete. * @int $blog_id Optional. The blog ID to filter by. * @int $attachment_id Optional. The attachment ID to filter by. * @int $user_id Optional. The user ID to filter by. * @string $title Optional. The title to filter by. * @int $album_id Optional. The album ID to filter by. * @int $activity_id Optional. The activity ID to filter by. * @int $group_id Optional. The group ID to filter by. * @string $privacy Optional. The privacy to filter by. * @string $date_created Optional. The date to filter by. * } * * @return array|bool An array of deleted media IDs on success, false on failure. */ public static function delete( $args = array() ) { global $wpdb; $bp = buddypress(); $r = wp_parse_args( $args, array( 'id' => false, 'blog_id' => false, 'attachment_id' => false, 'user_id' => false, 'title' => false, 'album_id' => false, 'activity_id' => false, 'group_id' => false, 'privacy' => false, 'date_created' => false, ) ); // Setup empty array from where query arguments. $where_args = array(); // ID. if ( ! empty( $r['id'] ) ) { $where_args[] = $wpdb->prepare( "id = %d", $r['id'] ); } // blog ID. if ( ! empty( $r['blog_id'] ) ) { $where_args[] = $wpdb->prepare( "blog_id = %d", $r['blog_id'] ); } // attachment ID. if ( ! empty( $r['attachment_id'] ) ) { $where_args[] = $wpdb->prepare( "attachment_id = %d", $r['attachment_id'] ); } // User ID. if ( ! empty( $r['user_id'] ) ) { $where_args[] = $wpdb->prepare( "user_id = %d", $r['user_id'] ); } // title. if ( ! empty( $r['title'] ) ) { $where_args[] = $wpdb->prepare( "title = %s", $r['title'] ); } // album ID. if ( ! empty( $r['album_id'] ) ) { $where_args[] = $wpdb->prepare( "album_id = %d", $r['album_id'] ); } // activity ID. if ( ! empty( $r['activity_id'] ) ) { $where_args[] = $wpdb->prepare( "activity_id = %d", $r['activity_id'] ); } // group ID. if ( ! empty( $r['group_id'] ) ) { $where_args[] = $wpdb->prepare( "group_id = %d", $r['group_id'] ); } // privacy. if ( ! empty( $r['privacy'] ) ) { $where_args[] = $wpdb->prepare( "privacy = %s", $r['privacy'] ); } // Date created. if ( ! empty( $r['date_created'] ) ) { $where_args[] = $wpdb->prepare( "date_created = %s", $r['date_created'] ); } // Bail if no where arguments. if ( empty( $where_args ) ) { return false; } // Join the where arguments for querying. $where_sql = 'WHERE ' . join( ' AND ', $where_args ); // Fetch all media being deleted so we can perform more actions. $medias = $wpdb->get_results( "SELECT * FROM {$bp->media->table_name} {$where_sql}" ); /** * Action to allow intercepting media items to be deleted. * * @since BuddyBoss 1.0.0 * * @param array $medias Array of media. * @param array $r Array of parsed arguments. */ do_action_ref_array( 'bp_media_before_delete', array( $medias, $r ) ); // Attempt to delete media from the database. $deleted = $wpdb->query( "DELETE FROM {$bp->media->table_name} {$where_sql}" ); // Bail if nothing was deleted. if ( empty( $deleted ) ) { return false; } /** * Action to allow intercepting media items just deleted. * * @since BuddyBoss 1.0.0 * * @param array $medias Array of media. * @param array $r Array of parsed arguments. */ do_action_ref_array( 'bp_media_after_delete', array( $medias, $r ) ); // Pluck the media IDs out of the $medias array. $media_ids = wp_parse_id_list( wp_list_pluck( $medias, 'id' ) ); $activity_ids = wp_parse_id_list( wp_list_pluck( $medias, 'activity_id' ) ); $attachment_ids = wp_parse_id_list( wp_list_pluck( $medias, 'attachment_id' ) ); // Handle accompanying attachments and meta deletion. if ( ! empty( $attachment_ids ) ) { // Loop through attachment ids and attempt to delete. foreach ( $attachment_ids as $attachment_id ) { if ( bp_is_active( 'activity' ) ) { $parent_activity_id = get_post_meta( $attachment_id, 'bp_media_parent_activity_id', true ); if ( ! empty( $parent_activity_id ) ) { $activity_media_ids = bp_activity_get_meta( $parent_activity_id, 'bp_media_ids', true ); if ( ! empty( $activity_media_ids ) ) { $activity_media_ids = explode( ',', $activity_media_ids ); $activity_media_ids = array_diff( $activity_media_ids, $media_ids ); if ( ! empty( $activity_media_ids ) ) { $activity_media_ids = implode( ',', $activity_media_ids ); bp_activity_update_meta( $parent_activity_id, 'bp_media_ids', $activity_media_ids ); } else { $activity_ids[] = $parent_activity_id; } } } } wp_delete_post( $attachment_id, true ); } } // delete related activity if ( ! empty( $activity_ids ) && bp_is_active( 'activity' ) ) { foreach ( $activity_ids as $activity_id ) { $activity = new BP_Activity_Activity( (int) $activity_id ); // Check access. if ( bp_activity_user_can_delete( $activity ) ) { /** This action is documented in bp-activity/bp-activity-actions.php */ do_action( 'bp_activity_before_action_delete_activity', $activity->id, $activity->user_id ); // Deleting an activity comment. if ( 'activity_comment' == $activity->type ) { if ( bp_activity_delete_comment( $activity->item_id, $activity->id ) ) { /** This action is documented in bp-activity/bp-activity-actions.php */ do_action( 'bp_activity_action_delete_activity', $activity->id, $activity->user_id ); } // Deleting an activity. } else { if ( bp_activity_delete( array( 'id' => $activity->id, 'user_id' => $activity->user_id ) ) ) { /** This action is documented in bp-activity/bp-activity-actions.php */ do_action( 'bp_activity_action_delete_activity', $activity->id, $activity->user_id ); } } } } } return $media_ids; } /** * Count total media for the given user * * @since BuddyBoss 1.0.0 * * @param int $user_id * * @return array|bool|int */ public static function total_media_count( $user_id = 0 ) { global $wpdb; $bp = buddypress(); $privacy = array( 'public' ); if ( is_user_logged_in() ) { $privacy[] = 'loggedin'; if ( bp_is_active( 'friends' ) ) { $is_friend = friends_check_friendship( get_current_user_id(), $user_id ); if( $is_friend ) { $privacy[] = 'friends'; } } } $privacy = "'" . implode( "', '", $privacy ) . "'"; $count_sql = "SELECT COUNT(*) FROM {$bp->media->table_name} WHERE user_id = {$user_id} AND privacy IN ({$privacy})"; $cache_group = 'bp_media_user_media_count'; $cached = bp_core_get_incremented_cache( $count_sql, $cache_group ); if ( false === $cached ) { $results = $wpdb->get_col( $count_sql ); $total_count = intval( $results[0] ); bp_core_set_incremented_cache( $count_sql, $cache_group, $total_count ); } else { $total_count = $cached; } return $total_count; } /** * Count total media for the given group * * @since BuddyBoss 1.0.0 * * @param int $group_id * * @return array|bool|int */ public static function total_group_media_count( $group_id = 0 ) { global $wpdb; $bp = buddypress(); $count_sql = "SELECT COUNT(*) FROM {$bp->media->table_name} WHERE group_id = {$group_id}"; $cache_group = 'bp_media_group_media_count'; $cached = bp_core_get_incremented_cache( $count_sql, $cache_group ); if ( false === $cached ) { $results = $wpdb->get_col( $count_sql ); $total_count = intval( $results[0] ); bp_core_set_incremented_cache( $count_sql, $cache_group, $total_count ); } else { $total_count = $cached; } return $total_count; } /** * Get all media ids for the album * * @since BuddyBoss 1.0.0 * @param bool $album_id * * @return array|bool */ public static function get_album_media_ids( $album_id = false ) { if ( ! $album_id ) { return false; } global $wpdb; $bp = buddypress(); // Select conditions. $select_sql = "SELECT DISTINCT m.id FROM {$bp->media->table_name} m WHERE m.album_id = {$album_id}"; $cache_group = 'bp_media_album_media_ids'; $cached = bp_core_get_incremented_cache( $select_sql, $cache_group ); if ( false === $cached ) { $media_ids = $wpdb->get_col( $select_sql ); bp_core_set_incremented_cache( $select_sql, $cache_group, $media_ids ); } else { $media_ids = $cached; } return $media_ids; } }
Changelog
Version | Description |
---|---|
BuddyPress 1.0.0 | Introduced. |
Methods
- __construct — Constructor method.
- append_user_fullnames — Append xProfile fullnames to an media array.
- delete — Delete media items from the database.
- get — Get media items, as specified by parameters.
- get_activity_attachment_id — Get media attachment id for the activity.
- get_activity_media_id — Get media id for the activity.
- get_album_media_ids — Get all media ids for the album
- get_in_operator_sql — Create SQL IN clause for filter queries.
- get_media_data — Convert media IDs to media objects, as expected in template loop.
- get_scope_query_sql — Get the SQL for the 'scope' param in BP_Media::get().
- populate — Populate the object with data about the specific media item.
- prefetch_object_data — Pre-fetch data for objects associated with media items.
- save — Save the media item to the database.
- total_group_media_count — Count total media for the given group
- total_media_count — Count total media for the given user
- total_user_group_media_count — Count total groups media for the given user.
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.