bp_nouveau_ajax_document_folder_save()

Save folder

Description

Source

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

function bp_nouveau_ajax_document_folder_save() {
	$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 );
	}

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

	// 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 );
	}

	if ( ! is_user_logged_in() ) {
		$response['feedback'] = esc_html__( 'Please login to create a folder.', 'buddyboss' );
		wp_send_json_error( $response );
	}

	if ( empty( $_POST['title'] ) ) {
		$response['feedback'] = esc_html__( 'Please enter title of folder.', 'buddyboss' );

		wp_send_json_error( $response );
	}

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

	// save media.
	$id        = ! empty( $_POST['folder_id'] ) ? filter_input( INPUT_POST, 'folder_id', FILTER_VALIDATE_INT ) : false;
	$group_id  = ! empty( $_POST['group_id'] ) ? (int) $_POST['group_id'] : false;
	$title     = wp_strip_all_tags( $_POST['title'] );
	$privacy   = ! empty( $_POST['privacy'] ) ? filter_input( INPUT_POST, 'privacy', FILTER_SANITIZE_STRING ) : 'public';
	$parent    = ! empty( $_POST['parent'] ) ? (int) filter_input( INPUT_POST, 'parent', FILTER_VALIDATE_INT ) : 0;
	$folder_id = ! empty( $_POST['folder_id'] ) ? (int) filter_input( INPUT_POST, 'folder_id', FILTER_VALIDATE_INT ) : 0;

	if ( $parent > 0 ) {
		$id = false;
	}

	if ( ! $id && ! $parent ) {
		$parent = $folder_id;
	}

	if ( $parent > 0 ) {
		$parent_folder = BP_Document_Folder::get_folder_data( array( $parent ) );
		$privacy       = $parent_folder[0]->privacy;
	}

	if ( (int) $parent > 0 ) {
		$has_access = bp_folder_user_can_edit( $parent );
		if ( ! $has_access ) {
			$response['feedback'] = esc_html__( 'You don\'t have a permission to create a folder inside this folder.', 'buddyboss' );
			wp_send_json_error( $response );
		}
	}

	$folder_id = bp_folder_add(
		array(
			'id'       => $id,
			'title'    => $title,
			'privacy'  => $privacy,
			'group_id' => $group_id,
			'parent'   => $parent,
		)
	);

	if ( ! $folder_id ) {
		$response['feedback'] = esc_html__( 'There was a problem when trying to create the folder.', 'buddyboss' );
		wp_send_json_error( $response );
	}

	$folder = new BP_Document_Folder( $folder_id );

	if ( $group_id > 0 ) {
		$ul = bp_document_user_document_folder_tree_view_li_html( $folder->user_id, $group_id );
	} else {
		$ul = bp_document_user_document_folder_tree_view_li_html( bp_loggedin_user_id() );
	}

	$document = '';
	if ( ! empty( $folder_id ) ) {
		ob_start();
		if ( bp_has_folders( array( 'include' => $folder_id ) ) ) {
			while ( bp_folder() ) {
				bp_the_folder();
				bp_get_template_part( 'document/document-entry' );
			}
		}
		$document = ob_get_contents();
		ob_end_clean();
	}

	$response = array(
		'document'  => $document,
		'tree_view' => $ul,
		'folder_id' => $folder_id,
	);

	wp_send_json_success( $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.