BP_Document::delete( array $args = array(), bool $from = false )

Delete document items from the database.

Description

To delete a specific document 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 document IDs on success, false on failure.

Source

File: bp-document/classes/class-bp-document.php

	public static function delete( $args = array(), $from = false ) {
		global $wpdb;

		$bp = buddypress();
		$r  = wp_parse_args(
			$args,
			array(
				'id'            => false,
				'blog_id'       => false,
				'attachment_id' => false,
				'user_id'       => false,
				'title'         => false,
				'folder_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'] );
		}

		// folder ID.
		if ( ! empty( $r['folder_id'] ) ) {
			$where_args[] = $wpdb->prepare( 'folder_id = %d', $r['folder_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 document being deleted so we can perform more actions.
		$documents = $wpdb->get_results( "SELECT * FROM {$bp->document->table_name} {$where_sql}" ); // db call ok; no-cache ok;

		/**
		 * Action to allow intercepting document items to be deleted.
		 *
		 * @param array $documents Array of document.
		 * @param array $r         Array of parsed arguments.
		 *
		 * @since BuddyBoss 1.4.0
		 */
		do_action_ref_array( 'bp_document_before_delete', array( $documents, $r ) );

		// Attempt to delete document from the database.
		$deleted = $wpdb->query( "DELETE FROM {$bp->document->table_name} {$where_sql}" );           // db call ok; no-cache ok;

		// Bail if nothing was deleted.
		if ( empty( $deleted ) ) {
			return false;
		}

		/**
		 * Action to allow intercepting document items just deleted.
		 *
		 * @param array $documents Array of document.
		 * @param array $r         Array of parsed arguments.
		 *
		 * @since BuddyBoss 1.4.0
		 */
		do_action_ref_array( 'bp_document_after_delete', array( $documents, $r ) );

		// Pluck the document IDs out of the $documents array.
		$document_ids   = wp_parse_id_list( wp_list_pluck( $documents, 'id' ) );
		$activity_ids   = wp_parse_id_list( wp_list_pluck( $documents, 'activity_id' ) );
		$attachment_ids = wp_parse_id_list( wp_list_pluck( $documents, 'attachment_id' ) );

		// Delete preview attachment.
		foreach ( $document_ids as $document_delete ) {
			$preview_id = bp_document_get_meta( $document_delete, 'preview_attachment_id', true );
			if ( $preview_id ) {
				wp_delete_attachment( $preview_id, true );
			}
		}

//		if ( ! empty( $document_ids ) ) {
//			// Loop through attachment ids and attempt to delete.
//			foreach ( $document_ids as $document ) {
//				$preview_attachment_id = bp_document_get_meta( $document, 'preview_attachment_id', true );
//				if ( $preview_attachment_id ) {
//					wp_delete_attachment( $preview_attachment_id, true );
//				}
//			}
//		}

		// Delete meta.
		if ( ! empty( $document_ids ) ) {
			// Delete all document meta entries for document items.
			self::delete_document_meta_entries( wp_list_pluck( $documents, '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_document_parent_activity_id', true );
					if ( ! empty( $parent_activity_id ) ) {
						$activity_document_ids = bp_activity_get_meta( $parent_activity_id, 'bp_document_ids', true );
						if ( ! empty( $activity_document_ids ) ) {
							$activity_document_ids = explode( ',', $activity_document_ids );
							$activity_document_ids = array_diff( $activity_document_ids, $document_ids );
							if ( ! empty( $activity_document_ids ) ) {
								$activity_document_ids = implode( ',', $activity_document_ids );
								bp_activity_update_meta( $parent_activity_id, 'bp_document_ids', $activity_document_ids );
							} else {
								$activity_ids[] = $parent_activity_id;
							}
						}
					}
				}

				if ( empty( $from ) ) {
					wp_delete_attachment( $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 $document_ids;
	}

Changelog

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