BP_Media::delete( array $args = array(), bool $from = false )
Delete media items from the database.
Description
To delete a specific media item, pass an ‘id’ parameter. Otherwise use the filters.
Parameters
- $args
-
(Optional)
Default value: array()
- $from
-
(Optional) Context of deletion from. ex. attachment, activity etc.
Default value: false
Return
(array|bool) An array of deleted media IDs on success, false on failure.
Source
File: bp-media/classes/class-bp-media.php
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;
}
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.