bp_nouveau_ajax_document_edit_folder()

Rename folder.

Description

Source

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

function bp_nouveau_ajax_document_edit_folder() {
	$response = array(
		'feedback' => sprintf(
			'<div class="bp-feedback error bp-ajax-message"><span class="bp-icon" aria-hidden="true"></span><p>%s</p></div>',
			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 ( empty( $_POST['title'] ) ) {
		$response['feedback'] = sprintf(
			'<div class="bp-feedback error"><span class="bp-icon" aria-hidden="true"></span><p>%s</p></div>',
			esc_html__( 'Please enter title of folder.', 'buddyboss' )
		);

		wp_send_json_error( $response );
	}

	if ( strpbrk( $_POST['title'], "\\/?%*:|\"<>" ) !== false ) {
		$response['feedback'] = sprintf(
				'<div class="bp-feedback error"><span class="bp-icon" aria-hidden="true"></span><p>%s</p></div>',
				esc_html__( 'Invalid folder name', 'buddyboss' )
		);
		wp_send_json_error( $response );
	}

	// save folder.
	$title    = $_POST['title'];
	$id       = ! empty( $_POST['id'] ) ? (int) $_POST['id'] : 0;
	$group_id = ! empty( $_POST['group_id'] ) ? (int) $_POST['group_id'] : 0;

	if ( (int) $id > 0 ) {
		$has_access = bp_folder_user_can_edit( $id );
		if ( ! $has_access ) {
			$response['feedback'] = sprintf( '<div class="bp-feedback error"><span class="bp-icon" aria-hidden="true"></span><p>%s</p></div>', esc_html__( 'You don\'t have permission to rename this folder', 'buddyboss' ) );
			wp_send_json_error( $response );
		}
	}

	if ( ! empty( $_POST['privacy'] ) ) {
		$privacy = $_POST['privacy'];
	} else {
		$folder_id = bp_document_get_root_parent_id( $_POST['id'] );
		$folder    = new BP_Document_Folder( $folder_id );
		$privacy   = $folder->privacy;
	}

	if ( $group_id > 0 ) {
		$privacy = 'grouponly';
	}

	$folder_id = bp_document_rename_folder( $id, $title, $privacy );

	if ( ! $folder_id ) {
		$response['feedback'] = sprintf(
			'<div class="bp-feedback error"><span class="bp-icon" aria-hidden="true"></span><p>%s</p></div>',
			esc_html__( 'There was a problem when trying to rename the folder.', 'buddyboss' )
		);
		wp_send_json_error( $response );
	}

	wp_send_json_success(
		array(
			'message' => 'success',
		)
	);
}

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.