bp_activity_edit_update_document( $document_ids )

Update document and activity for document updation and deletion while editing the activity.

Description

Parameters

$document_ids

(Required)

Return

(mixed)

Source

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

function bp_activity_edit_update_document( $document_ids ) {
	global $bp_activity_edit, $bp_activity_post_update_id;

	if ( ( true === $bp_activity_edit || isset( $_POST['edit'] ) ) && ! empty( $bp_activity_post_update_id ) ) {
		$old_document_ids = bp_activity_get_meta( $bp_activity_post_update_id, 'bp_document_ids', true );
		$old_document_ids = explode( ',', $old_document_ids );

		if ( ! empty( $old_document_ids ) ) {

			// old document count 1 and new document uploaded count is greater than 1.
			if ( 1 === count( $old_document_ids ) && 1 < count( $document_ids ) ) {
				$old_document_id = $old_document_ids[0];

				// check if old document id is in new document uploaded.
				if ( in_array( $old_document_id, $document_ids ) ) {

					// Create new document activity for old document because it has only parent activity to show right now.
					$old_document = new BP_Document( $old_document_id );
					$args         = array(
						'hide_sitewide' => true,
						'privacy'       => 'document'
					);

					if ( ! empty( $old_document->group_id ) && bp_is_active( 'groups' ) ) {
						$args['item_id'] = $old_document->group_id;
						$args['type']    = 'activity_update';
						$current_group   = groups_get_group( $old_document->group_id );
						$args['action']  = sprintf( __( '%1$s posted an update in the group %2$s', 'buddyboss' ), bp_core_get_userlink( $old_document->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 );
					}

					// document activity for old document is created and it is being assigned to the old document.
					// And document activity is being saved with needed data to figure out everything for it.
					if ( $activity_id ) {
						$old_document->activity_id = $activity_id;
						$old_document->save();

						$document_activity                    = new BP_Activity_Activity( $activity_id );
						$document_activity->secondary_item_id = $bp_activity_post_update_id;
						$document_activity->save();

						// update activity meta to tell it is document activity.
						bp_activity_update_meta( $activity_id, 'bp_document_activity', '1' );

						// save attachment meta for activity.
						update_post_meta( $old_document->attachment_id, 'bp_document_activity_id', $activity_id );

						//save parent activity id in attachment meta.
						update_post_meta( $old_document->attachment_id, 'bp_document_parent_activity_id', $bp_activity_post_update_id );
					}
				}

				// old document count is greater than 1 and new document uploaded count is only 1 now.
			} else if ( 1 < count( $old_document_ids ) && 1 === count( $document_ids ) ) {
				$new_document_id = $document_ids[0];

				// check if new document is in old document uploaded, if yes then delete that document's document activity first.
				if ( in_array( $new_document_id, $old_document_ids ) ) {
					$new_document         = new BP_Document( $new_document_id );
					$document_activity_id = $new_document->activity_id;

					// delete document's assigned document activity.
					remove_action( 'bp_activity_after_delete', 'bp_document_delete_activity_document' );
					bp_activity_delete( array( 'id' => $document_activity_id ) );
					add_action( 'bp_activity_after_delete', 'bp_document_delete_activity_document' );

					//save parent activity id in document.
					$new_document->activity_id = $bp_activity_post_update_id;
					$new_document->save();

					//save parent activity id in attachment meta.
					update_post_meta( $new_document->attachment_id, 'bp_document_parent_activity_id', $bp_activity_post_update_id );
				}
			}
		}
	}

	return $document_ids;
}

Changelog

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