BP_REST_Document_Endpoint::bp_rest_create_document( array $args )

Create the Document IDs from Upload IDs.

Description

Parameters

$args

(Required) Key value array of query var to query value.

Return

(array|WP_Error)

Source

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

	public function bp_rest_create_document( $args ) {

		$document_privacy    = ( ! empty( $args['privacy'] ) ? $args['privacy'] : 'public' );
		$document_upload_ids = ( ! empty( $args['document_ids'] ) ? $args['document_ids'] : '' );
		$activity_id         = ( ! empty( $args['activity_id'] ) ? $args['activity_id'] : false );
		$content             = ( ! empty( $args['content'] ) ? $args['content'] : false );
		$user_id             = ( ! empty( $args['user_id'] ) ? $args['user_id'] : get_current_user_id() );
		$id                  = ( ! empty( $args['id'] ) ? $args['id'] : '' );

		$group_id  = ( ! empty( $args['group_id'] ) ? $args['group_id'] : false );
		$folder_id = ( ! empty( $args['folder_id'] ) ? $args['folder_id'] : false );

		// Override the privacy if Folder ID is given.
		if ( ! empty( $folder_id ) ) {
			$folders = bp_folder_get_specific( array( 'folder_ids' => array( $folder_id ) ) );
			if ( ! empty( $folders['folders'] ) ) {
				$folder           = array_pop( $folders['folders'] );
				$document_privacy = $folder->privacy;
			}
		}

		// Update Document.
		if ( ! empty( $id ) ) {

			$wp_attachment_id  = $args['attachment_id'];
			$wp_attachment_url = wp_get_attachment_url( $wp_attachment_id );

			// when the file found to be empty it's means it's not a valid attachment.
			if ( empty( $wp_attachment_url ) ) {
				return;
			}

			$document_activity_id = $activity_id;

			// extract the nice title name.
			$title = get_the_title( $wp_attachment_id );

			$document_id = bp_document_add(
				array(
					'id'            => $id,
					'attachment_id' => $wp_attachment_id,
					'title'         => $title,
					'activity_id'   => $document_activity_id,
					'folder_id'     => ( ! empty( $args['folder_id'] ) ? $args['folder_id'] : false ),
					'group_id'      => ( ! empty( $args['group_id'] ) ? $args['group_id'] : false ),
					'privacy'       => $document_privacy,
					'user_id'       => $user_id,
					'error_type'    => 'wp_error',
				)
			);

			if ( is_int( $document_id ) ) {

				// save document is saved in attachment.
				update_post_meta( $wp_attachment_id, 'bp_document_saved', true );

				// save document meta for activity.
				if ( ! empty( $document_activity_id ) ) {
					update_post_meta( $wp_attachment_id, 'bp_document_activity_id', $document_activity_id );
				}

				$created_document_ids[] = $document_id;

			}
		}

		// created Documents.
		if ( ! empty( $document_upload_ids ) ) {
			$valid_upload_ids = array();

			foreach ( $document_upload_ids as $wp_attachment_id ) {
				$wp_attachment_url = wp_get_attachment_url( $wp_attachment_id );

				// when the file found to be empty it's means it's not a valid attachment.
				if ( empty( $wp_attachment_url ) ) {
					continue;
				}

				$valid_upload_ids[] = $wp_attachment_id;
			}

			$documents = array();
			if ( ! empty( $valid_upload_ids ) ) {
				foreach ( $valid_upload_ids as $wp_attachment_id ) {

					// extract the nice title name.
					$title = get_the_title( $wp_attachment_id );

					$documents[] = array(
						'id'   => $wp_attachment_id,
						'name' => $title,
					);
				}
			}

			if ( ! empty( $documents ) ) {
				$created_document_ids = bp_document_add_handler( $documents, $document_privacy, $content, $group_id, $folder_id );
			}
		}

		if ( empty( $created_document_ids ) ) {
			return new WP_Error(
				'bp_rest_document_creation_error',
				__( 'Error creating document, please try again.', 'buddyboss' ),
				array(
					'status' => 400,
				)
			);
		}

		// Link all uploaded document to main activity.
		if ( ! empty( $activity_id ) && empty( $id ) ) {
			$created_document_ids_joined = implode( ',', $created_document_ids );
			bp_activity_update_meta( $activity_id, 'bp_document_ids', $created_document_ids_joined );

			$main_activity = new BP_Activity_Activity( $activity_id );
			if ( ! empty( $main_activity ) && empty( $group_id ) ) {
				$main_activity->privacy = $document_privacy;
				$main_activity->save();
			}
		}

		return $created_document_ids;
	}

Changelog

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