bp_nouveau_ajax_document_upload()

Upload a document via a POST request.

Description

Source

File: bp-templates/bp-nouveau/includes/document/ajax.php

function bp_nouveau_ajax_document_upload() {
	$response = array(
		'feedback' => __( 'There was a problem when trying to upload this file.', 'buddyboss' ),
	);

	// Bail if not a POST action.
	if ( ! bp_is_post_request() ) {
		wp_send_json_error( $response, 500 );
	}

	if ( empty( $_POST['_wpnonce'] ) ) {
		wp_send_json_error( $response, 500 );
	}

	// Use default nonce.
	$nonce = filter_input( INPUT_POST, '_wpnonce', FILTER_SANITIZE_STRING );
	$check = 'bp_nouveau_media';

	// Nonce check!
	if ( empty( $nonce ) || ! wp_verify_nonce( $nonce, $check ) ) {
		wp_send_json_error( $response, 500 );
	}

	add_filter( 'upload_dir', 'bp_document_upload_dir' );

	// Upload file.
	$result = bp_document_upload();

	remove_filter( 'upload_dir', 'bp_document_upload_dir' );

	if ( is_wp_error( $result ) ) {

		if ( bp_current_user_can( 'bp_moderate' ) ) {
			$error_msg = $result->get_error_message();
			if ( 'Sorry, this file type is not permitted for security reasons.' === $error_msg ) {
				$response['feedback'] = __( 'Make sure you whitelisted extension and MIME TYPE in media settings and added correct MIME TYPE in extension entry.', 'buddyboss' );
			} else {
				$response['feedback'] = $result->get_error_message();
			}
		} else {
			$response['feedback'] = $result->get_error_message();
		}
		wp_send_json_error( $response, $result->get_error_code() );
	}

	wp_send_json_success( $result );
}

Changelog

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