bp_media_forums_new_post_media_save( $post_id )
Save media when new topic or reply is saved
Description
Parameters
- $post_id
-
(Required)
Source
File: bp-media/bp-media-filters.php
function bp_media_forums_new_post_media_save( $post_id ) {
if ( ! empty( $_POST['bbp_media'] ) ) {
// save activity id if it is saved in forums and enabled in platform settings
$main_activity_id = get_post_meta( $post_id, '_bbp_activity_id', true );
// save media
$medias = json_decode( stripslashes( $_POST['bbp_media'] ), true );
//fetch currently uploaded media ids
$existing_media = [];
$existing_media_ids = get_post_meta( $post_id, 'bp_media_ids', true );
$existing_media_attachment_ids = array();
if ( ! empty( $existing_media_ids ) ) {
$existing_media_ids = explode( ',', $existing_media_ids );
foreach ( $existing_media_ids as $existing_media_id ) {
$existing_media[ $existing_media_id ] = new BP_Media( $existing_media_id );
if ( ! empty( $existing_media[ $existing_media_id ]->attachment_id ) ) {
$existing_media_attachment_ids[] = $existing_media[ $existing_media_id ]->attachment_id;
}
}
}
$media_ids = array();
foreach ( $medias as $media ) {
$title = ! empty( $media['name'] ) ? $media['name'] : '';
$attachment_id = ! empty( $media['id'] ) ? $media['id'] : 0;
$attached_media_id = ! empty( $media['media_id'] ) ? $media['media_id'] : 0;
$album_id = ! empty( $media['album_id'] ) ? $media['album_id'] : 0;
$group_id = ! empty( $media['group_id'] ) ? $media['group_id'] : 0;
$menu_order = ! empty( $media['menu_order'] ) ? $media['menu_order'] : 0;
if ( ! empty( $existing_media_attachment_ids ) ) {
$index = array_search( $attachment_id, $existing_media_attachment_ids );
if ( ! empty( $attachment_id ) && $index !== false && ! empty( $existing_media[ $attached_media_id ] ) ) {
$existing_media[ $attached_media_id ]->menu_order = $menu_order;
$existing_media[ $attached_media_id ]->save();
unset( $existing_media_ids[ $index ] );
$media_ids[] = $attached_media_id;
continue;
}
}
$media_id = bp_media_add( array(
'attachment_id' => $attachment_id,
'title' => $title,
'album_id' => $album_id,
'group_id' => $group_id,
'error_type' => 'wp_error'
) );
if ( ! is_wp_error( $media_id ) ) {
$media_ids[] = $media_id;
//save media is saved in attachment
update_post_meta( $attachment_id, 'bp_media_saved', true );
}
}
$media_ids = implode( ',', $media_ids );
//Save all attachment ids in forums post meta
update_post_meta( $post_id, 'bp_media_ids', $media_ids );
//save media meta for activity
if ( ! empty( $main_activity_id ) && bp_is_active( 'activity' ) ) {
bp_activity_update_meta( $main_activity_id, 'bp_media_ids', $media_ids );
}
// delete medias which were not saved or removed from form
if ( ! empty( $existing_media_ids ) ) {
foreach ( $existing_media_ids as $media_id ) {
bp_media_delete( $media_id );
}
}
}
}
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.