bp_document_add_handler( array $documents = array(), string $privacy = 'public', string $content = '', int $group_id = false, int $folder_id = false )

Document add handler function

Description

Parameters

$documents

(array) (Optional)

Default value: array()

$privacy

(string) (Optional)

Default value: 'public'

$content

(string) (Optional)

Default value: ''

$group_id

(int) (Optional)

Default value: false

$folder_id

(int) (Optional)

Default value: false

Return

(mixed|void)

Source

File: bp-document/bp-document-functions.php

function bp_document_add_handler( $documents = array() ) {
	global $bp_document_upload_count;
	$document_ids = array();

	if ( ! is_user_logged_in() ) {
		return false;
	}

	if ( empty( $documents ) && ! empty( $_POST['document'] ) ) {
		$documents = $_POST['document'];
	}

	$privacy = ! empty( $_POST['privacy'] ) && in_array( $_POST['privacy'], array_keys( bp_document_get_visibility_levels() ) ) ? $_POST['privacy'] : 'public';

	if ( ! empty( $documents ) && is_array( $documents ) ) {

		// update count of documents for later use.
		$bp_document_upload_count = count( $documents );

		// save  document.
		foreach ( $documents as $document ) {

			$attachment_data = get_post( $document['id'] );
			$file            = get_attached_file( $document['id'] );
			$file_type       = wp_check_filetype( $file );
			$file_name       = basename( $file );

			$document_id = bp_document_add(
				array(
					'attachment_id' => $document['id'],
					'title'         => $document['name'],
					'folder_id'     => ! empty( $document['folder_id'] ) ? $document['folder_id'] : false,
					'group_id'      => ! empty( $document['group_id'] ) ? $document['group_id'] : false,
					'privacy'       => ! empty( $document['privacy'] ) && in_array( $document['privacy'], array_merge( array_keys( bp_document_get_visibility_levels() ), array( 'message' ) ) ) ? $document['privacy'] : $privacy,
					'menu_order'    => ! empty( $document['menu_order'] ) ? $document['menu_order'] : 0,
					'error_type'    => 'wp_error',
				)
			);

			if ( ! empty( $document_id ) && ! is_wp_error( $document_id ) ) {
				bp_document_update_meta( $document_id, 'file_name', $file_name );
				bp_document_update_meta( $document_id, 'extension', '.' . $file_type['ext'] );
			}

			if ( $document_id ) {
				$document_ids[] = $document_id;
			}
		}
	}

	/**
	 * Fires at the end of the execution of adding saving a document item, before returning the new document items in ajax response.
	 *
	 * @param array $document_ids document IDs.
	 * @param array $documents    Array of document from POST object or in function parameter.
	 *
	 * @since BuddyBoss 1.4.0
	 */
	return apply_filters( 'bp_document_add_handler', $document_ids, (array) $documents );
}

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.