bp_nouveau_ajax_document_update_file_name()

Update the document name.

Description

Source

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

function bp_nouveau_ajax_document_update_file_name() {

	$response = array(
		'feedback' => esc_html__( 'There was a problem performing this action. Please try again.', 'buddyboss' ),
	);

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

	$document_id            = ! empty( $_POST['document_id'] ) ? (int) filter_input( INPUT_POST, 'document_id', FILTER_SANITIZE_STRING ) : 0;
	$attachment_document_id = ! empty( $_POST['attachment_document_id'] ) ? (int) filter_input( INPUT_POST, 'attachment_document_id', FILTER_SANITIZE_STRING ) : 0;
	$title                  = ! empty( $_POST['name'] ) ? filter_input( INPUT_POST, 'name', FILTER_SANITIZE_STRING ) : '';
	$type                   = ! empty( $_POST['document_type'] ) ? filter_input( INPUT_POST, 'document_type', FILTER_SANITIZE_STRING ) : '';

	if ( 'document' === $type ) {
		if ( 0 === $document_id || 0 === $attachment_document_id || '' === $title ) {
			wp_send_json_error( $response );
		}

		if ( (int) $document_id > 0 ) {
			$has_access = bp_document_user_can_edit( $document_id );
			if ( ! $has_access ) {
				$response['feedback'] = esc_html__( "You don't have a permission to rename the document.", 'buddyboss' );
				wp_send_json_error( $response );
			}
		}

		$document = bp_document_rename_file( $document_id, $attachment_document_id, $title );

		if ( isset( $document['document_id'] ) && $document['document_id'] > 0 ) {
			wp_send_json_success(
				array(
					'message'  => 'success',
					'response' => $document,
				)
			);
		} else {
			if ( '' === $document ) {
				wp_send_json_error( $response );
			} else {
				$response = array(
						'feedback' => $document,
				);
				wp_send_json_error( $response );
			}
		}
	} else {
		if ( 0 === $document_id || '' === $title ) {
			wp_send_json_error( $response );
		}

		if ( strpbrk( $title, "\\/?%*:|\"<>" ) !== false ) {
			$response['feedback'] = esc_html__( "Invalid folder name", 'buddyboss' );
			wp_send_json_error( $response );
		}

		if ( (int) $document_id > 0 ) {
			$has_access = bp_folder_user_can_edit( $document_id );
			if ( ! $has_access ) {
				$response['feedback'] = esc_html__( 'You don\'t have permission to rename folder', 'buddyboss' );
				wp_send_json_error( $response );
			}
		}

		$folder = bp_document_rename_folder( $document_id, $title );

		$response = array(
			'document_id' => $document_id,
			'title'       => $title,
		);

		if ( $folder > 0 ) {
			wp_send_json_success(
				array(
					'message'  => 'success',
					'response' => $response,
				)
			);
		} else {
			wp_send_json_error( $response );
		}
	}

}

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.