bp_activity_media_add( $media )

Create media activity for each media uploaded

Description

Parameters

$media

(Required)

Source

File: bp-activity/bp-activity-filters.php

function bp_activity_media_add( $media ) {
	global $bp_media_upload_count, $bp_new_activity_comment;

	if ( ! empty( $media ) ) {
		$parent_activity_id = false;
		if ( isset( $_POST['bp_activity_update'] ) && isset( $_POST['bp_activity_id'] ) ) {
			$parent_activity_id = (int) $_POST['bp_activity_id'];
		}

		if ( $bp_media_upload_count > 1 || ! empty( $bp_new_activity_comment ) ) {

			if ( bp_is_active( 'groups' ) && ! empty( $bp_new_activity_comment ) && empty( $media->group_id ) ) {
				$comment = new BP_Activity_Activity( $bp_new_activity_comment );

				if ( ! empty( $comment->item_id ) ) {
					$comment_activity = new BP_Activity_Activity( $comment->item_id );
					if ( ! empty( $comment_activity->component ) && buddypress()->groups->id === $comment_activity->component ) {
						$media->group_id = $comment_activity->item_id;
						$media->privacy = 'grouponly';
					}
				}
			}

			$args = array(
				'hide_sitewide'     => true,
				'privacy'           => 'media'
			);

			if ( ! empty( $media->group_id ) && bp_is_active( 'groups' ) ) {
				$args['item_id'] = $media->group_id;
				$args['type']    = 'activity_update';
				$current_group   = groups_get_group( $media->group_id );
				$args['action']  = sprintf( __( '%1$s posted an update in the group %2$s', 'buddyboss' ), bp_core_get_userlink( $media->user_id ), '<a href="' . bp_get_group_permalink( $current_group ) . '">' . esc_attr( $current_group->name ) . '</a>' );
				$activity_id     = groups_record_activity( $args );
			} else {
				$activity_id = bp_activity_post_update( $args );
			}

			if ( $activity_id ) {

				//save media activity id in media
				$media->activity_id = $activity_id;
				$media->save();

				// update activity meta
				bp_activity_update_meta( $activity_id, 'bp_media_activity', '1' );

				// save attachment meta for activity
				update_post_meta( $media->attachment_id, 'bp_media_activity_id', $activity_id );

				if ( $parent_activity_id ) {

					$media_activity = new BP_Activity_Activity( $activity_id );
					$media_activity->secondary_item_id = $parent_activity_id;
					$media_activity->save();

					//save parent activity id in attachment meta
					update_post_meta( $media->attachment_id, 'bp_media_parent_activity_id', $parent_activity_id );
				}
			}
		} else {
			if ( $parent_activity_id ) {

				//save media activity id in media
				$media->activity_id = $parent_activity_id;
				$media->save();

				//save parent activity id in attachment meta
				update_post_meta( $media->attachment_id, 'bp_media_parent_activity_id', $parent_activity_id );
			}
		}
	}
}

Changelog

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