BP_REST_Document_Endpoint::upload_item( WP_REST_Request $request )

Upload 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 upload_item( $request ) {

		$file = $request->get_file_params();

		if ( empty( $file ) ) {
			return new WP_Error(
				'bp_rest_document_file_required',
				__( 'Sorry, you have not uploaded any document.', 'buddyboss' ),
				array(
					'status' => 400,
				)
			);
		}

		add_filter( 'upload_dir', 'bp_document_upload_dir_script' );

		/**
		 * Create and upload the document file.
		 */
		$upload = bp_document_upload();

		remove_filter( 'upload_dir', 'bp_document_upload_dir_script' );

		if ( is_wp_error( $upload ) ) {
			return new WP_Error(
				'bp_rest_document_upload_error',
				$upload->get_error_message(),
				array(
					'status' => 400,
				)
			);
		}

		$retval = array(
			'id'   => $upload['id'],
			'url'  => $upload['url'],
			'name' => $upload['name'],
			'type' => $upload['type'],
		);

		$response = rest_ensure_response( $retval );

		/**
		 * Fires after a document is uploaded 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_upload_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.