wp_die(__('There was an error moving the files to the protected directory.', 'media-vault') . '<br/>' . $move->get_error_message());
            }
            $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);
/**
 * Returns the Media Vault file access permission for an attachment
 * or false if the attachment's files are not marked as protected
 *
 * @since 0.7
 *
 * @uses mgjp_mv_is_protected()
 * @return bool false if attachment is not protected
 * @return string the permission name-id set for this attachment if it is protected
 */
function mgjp_mv_get_the_permission($attachment_id, $meta_only = false)
{
    if (!mgjp_mv_is_protected($attachment_id)) {
        return false;
    }
    $permission = get_post_meta($attachment_id, '_mgjp_mv_permission', true);
    return empty($permission) && !$meta_only ? get_option('mgjp_mv_default_permission', 'logged-in') : $permission;
}
/**
 * Rendering function for the Media Vault attachment
 * metabox
 *
 * @since 0.7.1
 *
 * @uses mgjp_mv_get_the_permissions()
 * @uses mgjp_mv_is_protected()
 * @param $post object WP_Post object of current attachment
 */
function mgjp_mv_render_attachment_protection_metabox($post)
{
    wp_nonce_field('mgjp_mv_protection_metabox', 'mgjp_mv_protection_metabox_nonce');
    $permission = get_post_meta($post->ID, '_mgjp_mv_permission', true);
    $permissions = mgjp_mv_get_the_permissions();
    if (empty($permission) || !isset($permissions[$permission])) {
        $permission = 'default';
    }
    $default = array('default' => array('select' => __('Use Default Setting', 'media-vault')));
    $permissions = $default + $permissions;
    ?>

  <!--[if lt IE 9]>
    <script async src="<?php 
    echo esc_url(plugins_url('js/min/mv-attachment-edit-ltIE9.min.js', __FILE__));
    ?>
"></script>
  <![endif]-->

  <input type="hidden" name="mgjp_mv_protection_toggle" value="off">
  <input type="checkbox" id="mgjp_mv_protection_toggle" name="mgjp_mv_protection_toggle" <?php 
    checked(mgjp_mv_is_protected($post->ID));
    ?>
>

  <label class="mgjp-mv-protection-toggle" for="mgjp_mv_protection_toggle">
    <span aria-role="hidden" class="mgjp-on button button-primary" data-mgjp-content="<?php 
    esc_attr_e('Add to Protected', 'media-vault');
    ?>
"></span>
    <span aria-role="hidden" class="mgjp-off" data-mgjp-content="<?php 
    esc_attr_e('Remove from Protected', 'media-vault');
    ?>
"></span>

    <span class="visuallyhidden"><?php 
    esc_html_e('Protect this attachment\'s files with Media Vault.', 'media-vault');
    ?>
</span>
  </label>

  <p class="mgjp-mv-permission-select">
    <label for="mgjp_mv_permission_select">
      <span class="description"><?php 
    esc_html_e('File access permission', 'media-vault');
    ?>
</span>
    </label>

    <select id="mgjp_mv_permission_select" name="mgjp_mv_permission_select">

      <?php 
    foreach ($permissions as $key => $data) {
        ?>

        <option value="<?php 
        echo esc_attr($key);
        ?>
" <?php 
        selected($permission, $key);
        ?>
>
          <?php 
        echo esc_html($data['select']);
        ?>
        </option>

      <?php 
    }
    ?>

    </select>
  </p>

  <?php 
}