BP_REST_Document_Endpoint::bp_documents_update_rest_field_callback_messages( object $object, object $value, string $attribute )

The function to use to update the documents value of the messages REST Field.

Description

Parameters

$object

(Required) The BuddyPress component's object that was just created/updated during the request. (in this case the BP_Messages_Message object).

$value

(Required) The value of the REST Field to save.

$attribute

(Required) The REST Field key used into the REST response.

Return

(object)

Source

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

	protected function bp_documents_update_rest_field_callback_messages( $object, $value, $attribute ) {

		if ( 'bp_documents' !== $attribute || empty( $object ) ) {
			$value->bp_documents = null;
			return $value;
		}

		$message_id = $value->id;

		$thread_id = $value->thread_id;

		$documents = wp_parse_id_list( $object );

		if ( empty( $documents ) ) {
			$value->bp_documents = null;
			return $value;
		}

		$args = array(
			'document_ids' => $documents,
			'privacy'      => 'message',
		);

		$document_ids = $this->bp_rest_create_document( $args );

		if ( is_wp_error( $document_ids ) ) {
			$value->bp_documents = $document_ids;
			return $value;
		}

		if ( ! empty( $document_ids ) ) {
			foreach ( $document_ids as $id ) {
				bp_document_add_meta( $id, 'thread_id', $thread_id );
			}
		}

		bp_messages_update_meta( $message_id, 'bp_document_ids', implode( ',', $document_ids ) );
	}

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.