$protected++;
        }
        $location = add_query_arg(array('mgjp-mv-protected' => $protected, 'ids' => join(',', $media_ids)), $location);
        break;
    case 'mgjp-mv-unprotect':
        if (!current_user_can('edit_posts')) {
            wp_die(__('You are not allowed to remove attachments from the protected directory.', 'media-vault'));
        }
        $unprotected = 0;
        foreach ((array) $media_ids as $media_id) {
            if (!current_user_can('edit_post', $media_id)) {
                continue;
            }
            if (!mgjp_mv_is_protected($media_id)) {
                continue;
            }
            $move = mgjp_mv_move_attachment_from_protected($media_id);
            if (is_wp_error($move)) {
                wp_die(__('There was an error moving the files from the protected directory.', 'media-vault') . '<br/>' . $move->get_error_message());
            }
            delete_post_meta($media_id, '_mgjp_mv_permission');
            $unprotected++;
        }
        $location = add_query_arg(array('mgjp-mv-unprotected' => $unprotected, 'ids' => join(',', $media_ids)), $location);
        break;
    default:
        return;
}
$location = remove_query_arg(array('action', 'action2', 'media'), $location);
wp_redirect($location);
exit;
/**
 * Save Media Vault attachment metabox data on
 * edit attachments
 *
 * @since 0.7.1
 *
 * @uses mgjp_mv_move_attachment_from_protected()
 * @uses mgjp_mv_move_attachment_to_protected()
 * @uses mgjp_mv_get_the_permissions()
 * @param $attachment_id the id of the current attachment
 * @return void if any of the validations fail
 */
function mgjp_mv_save_attachment_metabox_data($attachment_id)
{
    if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) {
        return;
    }
    if (!isset($_POST['mgjp_mv_protection_metabox_nonce']) || !wp_verify_nonce($_POST['mgjp_mv_protection_metabox_nonce'], 'mgjp_mv_protection_metabox')) {
        return;
    }
    if (!current_user_can('edit_post', $attachment_id)) {
        return;
    }
    if (!isset($_POST['mgjp_mv_protection_toggle'])) {
        return;
    }
    switch ($_POST['mgjp_mv_protection_toggle']) {
        case 'off':
            remove_action('edit_attachment', 'mgjp_mv_save_attachment_metabox_data');
            $move = mgjp_mv_move_attachment_from_protected($attachment_id);
            add_action('edit_attachment', 'mgjp_mv_save_attachment_metabox_data');
            if (is_wp_error($move)) {
                return;
            }
            delete_post_meta($attachment_id, '_mgjp_mv_permission');
            return;
        case 'on':
            remove_action('edit_attachment', 'mgjp_mv_save_attachment_metabox_data');
            $move = mgjp_mv_move_attachment_to_protected($attachment_id);
            add_action('edit_attachment', 'mgjp_mv_save_attachment_metabox_data');
            if (is_wp_error($move)) {
                return;
            }
            if (!isset($_POST['mgjp_mv_permission_select']) || empty($_POST['mgjp_mv_permission_select'])) {
                return;
            }
            $permissions = mgjp_mv_get_the_permissions();
            if ('default' == $_POST['mgjp_mv_permission_select'] || !isset($permissions[$_POST['mgjp_mv_permission_select']])) {
                delete_post_meta($attachment_id, '_mgjp_mv_permission');
            } else {
                update_post_meta($attachment_id, '_mgjp_mv_permission', $_POST['mgjp_mv_permission_select']);
            }
            return;
        default:
            return;
    }
}