BP_REST_Document_Endpoint::bbp_document_update_rest_field_callback( object $object, object $value )

The function to use to update the document’s value of the topic REST Field.

Description

Parameters

$object

(Required) Value for the schema.

$value

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

Return

(object)

Source

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

	protected function bbp_document_update_rest_field_callback( $object, $value ) {

		$documents = wp_parse_id_list( $object );
		if ( empty( $documents ) ) {
			$value->bbp_documents = null;

			return $value;
		}

		$post_id = $value->ID;

		$reply_id = 0;
		$topic_id = 0;
		$forum_id = 0;

		// save activity id if it is saved in forums and enabled in platform settings.
		$main_activity_id = get_post_meta( $post_id, '_bbp_activity_id', true );

		// fetch currently uploaded document ids.
		$existing_document_ids            = get_post_meta( $post_id, 'bp_document_ids', true );
		$existing_document_attachment_ids = array();
		$existing_document_attachments    = array();

		if ( ! empty( $existing_document_ids ) ) {
			$existing_document_ids = explode( ',', $existing_document_ids );

			foreach ( $existing_document_ids as $existing_document_id ) {
				$existing_document_object = new BP_Document( $existing_document_id );

				if ( ! empty( $existing_document_object->attachment_id ) ) {
					$existing_document_attachment_ids[]                     = $existing_document_object->attachment_id;
					$existing_document_attachments[ $existing_document_id ] = $existing_document_object->attachment_id;
				}
			}
		}

		$document_ids     = array();
		$menu_order_count = 0;

		if ( ! empty( $documents ) ) {
			foreach ( $documents as $document ) {

				$wp_attachment_url = wp_get_attachment_url( $document );

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

				$menu_order_count ++;

				$attachment_id = ! empty( $document ) ? $document : 0;
				$menu_order    = $menu_order_count;

				if ( ! empty( $existing_document_attachment_ids ) ) {
					$index = array_search( $attachment_id, $existing_document_attachment_ids, true );
					if ( ! empty( $attachment_id ) && false !== $index ) {
						$exisiting_document_id    = array_search( $attachment_id, $existing_document_attachments, true );
						$existing_document_update = new BP_Document( $exisiting_document_id );

						$existing_document_update->menu_order = $menu_order;
						$existing_document_update->save();

						unset( $existing_document_ids[ $index ] );
						$document_ids[] = $exisiting_document_id;
						continue;
					}
				}

				if ( 0 === $reply_id && bbp_get_reply_post_type() === get_post_type( $post_id ) ) {
					$reply_id = $post_id;
					$topic_id = bbp_get_reply_topic_id( $reply_id );
					$forum_id = bbp_get_topic_forum_id( $topic_id );
				} elseif ( 0 === $topic_id && bbp_get_topic_post_type() === get_post_type( $post_id ) ) {
					$topic_id = $post_id;
					$forum_id = bbp_get_topic_forum_id( $topic_id );
				} elseif ( 0 === $forum_id && bbp_get_forum_post_type() === get_post_type( $post_id ) ) {
					$forum_id = $post_id;
				}

				// extract the nice title name.
				$title     = get_the_title( $attachment_id );
				$file      = get_attached_file( $attachment_id );
				$file_type = wp_check_filetype( $file );
				$file_name = basename( $file );

				$document_id = bp_document_add(
					array(
						'attachment_id' => $attachment_id,
						'title'         => $title,
						'folder_id'     => 0,
						'group_id'      => 0,
						'privacy'       => 'forums',
						'error_type'    => 'wp_error',
						'menu_order'    => $menu_order,
					)
				);

				if ( ! is_wp_error( $document_id ) && ! empty( $document_id ) ) {
					$document_ids[] = $document_id;

					// save document meta.
					bp_document_update_meta( $document_id, 'forum_id', $forum_id );
					bp_document_update_meta( $document_id, 'topic_id', $topic_id );
					bp_document_update_meta( $document_id, 'reply_id', $reply_id );
					bp_document_update_meta( $document_id, 'file_name', $file_name );
					bp_document_update_meta( $document_id, 'extension', '.' . $file_type['ext'] );

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

		$document_ids = implode( ',', $document_ids );

		// Save all attachment ids in forums post meta.
		update_post_meta( $post_id, 'bp_document_ids', $document_ids );

		// save document meta for activity.
		if ( ! empty( $main_activity_id ) && bp_is_active( 'activity' ) ) {
			bp_activity_update_meta( $main_activity_id, 'bp_document_ids', $document_ids );
		}

		// delete documents which were not saved or removed from form.
		if ( ! empty( $existing_document_ids ) ) {
			foreach ( $existing_document_ids as $document_id ) {
				bp_document_delete( array( 'id' => $document_id ) );
			}
		}

	}

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.