BP_REST_Document_Endpoint::update_item( WP_REST_Request $request )

Update a document.

Description

Parameters

$request

(Required) Full details about the request.

Return

(WP_REST_Response) | WP_Error

Source

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

	public function update_item( $request ) {
		$id = $request['id'];

		$documents = $this->assemble_response_data( array( 'document_ids' => array( $id ) ) );

		if ( empty( $documents['documents'] ) ) {
			return new WP_Error(
				'bp_rest_document_invalid_id',
				__( 'Invalid document ID.', 'buddyboss' ),
				array(
					'status' => 404,
				)
			);
		}

		$document = end( $documents['documents'] );

		$args = array(
			'id'            => $document->id,
			'privacy'       => $document->privacy,
			'attachment_id' => $document->attachment_id,
			'group_id'      => $document->group_id,
			'activity_id'   => $document->activity_id,
			'folder_id'     => $document->folder_id,
			'title'         => $document->title,
		);

		if ( isset( $request['group_id'] ) && ! empty( $request['group_id'] ) ) {
			$args['group_id'] = $request['group_id'];
			$args['privacy']  = 'grouponly';
		}

		if ( isset( $request['folder_id'] ) && ! empty( $request['folder_id'] ) ) {
			$args['folder_id'] = $request['folder_id'];
			$parent_folder     = new BP_Document_Folder( $args['folder_id'] );
			$args['privacy']   = $parent_folder->privacy;
		}

		/**
		 * Filter the query arguments for the request.
		 *
		 * @param array           $args    Key value array of query var to query value.
		 * @param WP_REST_Request $request The request sent to the API.
		 *
		 * @since 0.1.0
		 */
		$args = apply_filters( 'bp_rest_document_update_items_query_args', $args, $request );

		if ( ! empty( $request['title'] ) ) {
			$document_rename = bp_document_rename_file( $document->id, $document->attachment_id, $request['title'] );
			if ( ! isset( $document_rename['document_id'] ) || $document_rename['document_id'] < 1 ) {
				return new WP_Error(
					'bp_rest_document_rename',
					$document_rename,
					array(
						'status' => 404,
					)
				);
			}
		}

		if (
			empty( $document->folder_id )
			&& ( ! isset( $request['folder_id'] ) || empty( $request['folder_id'] ) )
			&& isset( $request['privacy'] )
			&& ! empty( $request['privacy'] )
		) {
			bp_document_update_privacy( $document->id, $request['privacy'], 'document' );
		}

		$id     = $this->bp_rest_create_document( $args );
		$status = true;

		if ( is_wp_error( $id ) ) {
			return $id;
		}

		if ( empty( $id ) ) {
			$status = false;
		}

		$documents = $this->assemble_response_data( array( 'document_ids' => array( $request['id'] ) ) );
		$document  = current( $documents['documents'] );

		$fields_update = $this->update_additional_fields_for_object( $document, $request );

		if ( is_wp_error( $fields_update ) ) {
			return $fields_update;
		}

		$retval = $this->prepare_response_for_collection(
			$this->prepare_item_for_response( $document, $request )
		);

		$response = new WP_REST_Response();
		$response->set_data(
			array(
				'updated' => $status,
				'data'    => $retval,
			)
		);

		/**
		 * Fires after an document is updated via the REST API.
		 *
		 * @param WP_REST_Response     $response The response data.
		 * @param WP_REST_Request      $request  The request sent to the API.
		 *
		 * @since 0.1.0
		 */
		do_action( 'bp_rest_document_update_item', $response, $request );

		return $response;
	}

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.