/**
  * @group bp_docs_remove_doc_from_folder
  */
 public function test_bp_docs_remove_doc_from_folder()
 {
     $doc_id = $this->factory->doc->create();
     $folder_id = $this->factory->post->create(array('post_type' => 'bp_docs_folder'));
     $this->assertTrue(bp_docs_add_doc_to_folder($doc_id, $folder_id));
     $this->assertTrue(bp_docs_remove_doc_from_folder($doc_id, $folder_id));
     $this->assertEquals(false, bp_docs_get_doc_folder($doc_id));
 }
/**
 * Save folder selections on Doc save.
 *
 * @since 1.9
 *
 * @param int $doc_id
 * @return bool True on success, false on failure.
 */
function bp_docs_save_folder_selection($doc_id)
{
    $retval = false;
    if (empty($_POST['existing-or-new-folder'])) {
        return;
    }
    $check = apply_filters('bp_docs_before_save_folder_selection', true, $doc_id);
    if (!$check) {
        return $retval;
    }
    switch ($_POST['existing-or-new-folder']) {
        case 'existing':
            if (isset($_POST['bp-docs-folder'])) {
                $folder_id = intval($_POST['bp-docs-folder']);
                if (0 === $folder_id) {
                    $existing_folder_id = bp_docs_get_doc_folder($doc_id);
                    $retval = bp_docs_remove_doc_from_folder($doc_id, $existing_folder_id);
                } else {
                    $retval = bp_docs_add_doc_to_folder($doc_id, $folder_id);
                }
            }
            break;
        case 'new':
            $folder_name = trim(stripslashes($_POST['new-folder']));
            if (empty($folder_name)) {
                bp_core_add_message(__('You must provide a folder name.', 'bp-docs'), 'error');
            } else {
                $folder_args = array('name' => $folder_name);
                // Parent
                $folder_args['parent'] = !empty($_POST['new-folder-parent']) ? intval($_POST['new-folder-parent']) : null;
                // Type
                $folder_type = stripslashes($_POST['new-folder-type']);
                if ('global' === $folder_type) {
                    // Nothing to do
                } else {
                    if ('me' === $folder_type) {
                        $folder_args['user_id'] = bp_loggedin_user_id();
                    } else {
                        if (is_numeric($folder_type)) {
                            // This is a group
                            $folder_args['group_id'] = intval($folder_type);
                        }
                    }
                }
                // Create the folder
                $folder_id = bp_docs_create_folder($folder_args);
                // Add the doc to the folder
                $retval = bp_docs_add_doc_to_folder($doc_id, $folder_id);
            }
            break;
    }
    return $retval;
}