Ejemplo n.º 1
0
 /**
  * @group bulk_delete
  */
 public function test_buddydrive_bulk_delete_items()
 {
     // create the upload dir
     $upload_dir = buddydrive_get_upload_data();
     $expected_ids = array();
     $quota_left = 0;
     $u1 = $this->factory->user->create();
     $u2 = $this->factory->user->create();
     $items_users = array('screenshot-1.png' => $u1, 'screenshot-2.png' => $u1, 'screenshot-3.png' => $u2);
     $args = array('type' => buddydrive_get_file_post_type(), 'mime_type' => 'image/png');
     foreach ($items_users as $item => $user) {
         $f = trailingslashit(buddydrive()->plugin_dir) . $item;
         copy($f, trailingslashit($upload_dir['dir']) . $item);
         $fs = filesize($item);
         buddydrive_update_user_space($user, filesize($item));
         if ('screenshot-1.png' === $item) {
             $quota_left = $fs;
         }
         $args = array_merge($args, array('user_id' => $user, 'title' => $item, 'guid' => trailingslashit($upload_dir['url']) . $item));
         $expected_ids[$item] = buddydrive_save_item($args);
     }
     unset($expected_ids['screenshot-1.png']);
     $count = buddydrive_delete_item(array('ids' => $expected_ids));
     $this->assertTrue($count === count($expected_ids));
     $not_deleted = buddydrive_get_buddyfiles_by_ids($expected_ids);
     $this->assertTrue(empty($not_deleted));
     $this->assertEquals($quota_left, (int) get_user_meta($u1, '_buddydrive_total_space', true), 'u1 quota should be set');
     $this->assertEmpty((int) get_user_meta($u2, '_buddydrive_total_space', true), 'u2 quota should be 0');
 }
Ejemplo n.º 2
0
/**
 * Ajax deletes folder or files
 *
 * @uses check_admin_referer() for security reasons
 * @uses buddydrive_delete_item() deletes the post_type (file or folder)
 * @return array the result with the item ids deleted
 */
function buddydrive_delete_items()
{
    // Bail if not a POST action
    if ('POST' !== strtoupper($_SERVER['REQUEST_METHOD'])) {
        return;
    }
    // Check the nonce
    check_admin_referer('buddydrive_actions', '_wpnonce_buddydrive_actions');
    $items = $_POST['items'];
    $items = substr($items, 0, strlen($items) - 1);
    $items = explode(',', $items);
    $items_nbre = buddydrive_delete_item(array('ids' => $items, 'user_id' => false));
    if (!empty($items_nbre)) {
        echo json_encode(array('result' => $items_nbre, 'items' => $items));
    } else {
        echo json_encode(array('result' => 0));
    }
    die;
}
/**
 * Removes Buddyfiles, BuddyFolders and files of a deleted user
 *
 * @param  int $user_id the id of the deleted user
 * @uses buddydrive_delete_item() to remove user's BuddyDrive content
 */
function buddydrive_remove_user($user_id)
{
    buddydrive_delete_item(array('user_id' => $user_id));
}
Ejemplo n.º 4
0
 /**
  * @group delete
  */
 public function test_buddydrive_delete_item_zero()
 {
     // create the upload dir
     $upload_dir = buddydrive_get_upload_data();
     $expected_id = buddydrive_save_item(array('type' => buddydrive_get_file_post_type(), 'user_id' => $this->user_id, 'title' => 'screenshot-1.png', 'content' => 'foo file', 'mime_type' => 'image/png', 'guid' => trailingslashit($upload_dir['url']) . 'screenshot-1.png'));
     $count = buddydrive_delete_item(array('ids' => 0, 'user_id' => false));
     $this->assertEmpty($count);
     $not_deleted = buddydrive_get_buddyfiles_by_ids($expected_id);
     $this->assertTrue(!empty($not_deleted));
 }
Ejemplo n.º 5
0
/**
 * Loads and choose the right action to do in the administration of BuddyDrive Items
 *
 * @global object $buddydrive_list_table
 * @uses remove_query_arg() to remove some args to the url
 * @uses check_admin_referer() for security reasons
 * @uses wp_parse_id_list() to parse ids from a comma separated list
 * @uses buddydrive_delete_item() to delete one or more items
 * @uses add_query_arg() to add args to the url
 * @uses bp_core_redirect() to safely redirect to the right admin area
 * @uses add_screen_option() to organize the layout
 * @uses get_current_screen() to get the admin screen
 * @uses add_meta_box() to register the meta boxes
 * @uses wp_enqueue_script() to enqueue the needed scripts
 * @uses BuddyDrive_List_Table() to init the list of items
 * @uses buddydrive_get_buddyfile() to get a single BuddyDrive item
 * @uses buddydrive_get_folder_post_type() to get the BuddyFolder post type
 * @uses buddydrive_get_file_post_type() to get the BuddyFile post type
 * @uses wp_kses() to sanitize datas
 * @uses buddydrive_update_item() to update a BuddyDrive item
 * @uses wp_redirect() to redirect to the right admin area
 */
function buddydrive_files_admin_load()
{
    global $buddydrive_list_table;
    $doaction = !empty($_REQUEST['action']) ? $_REQUEST['action'] : '';
    // If the bottom is set, let it override the action
    if (!empty($_REQUEST['action2']) && $_REQUEST['action2'] != "-1") {
        $doaction = $_REQUEST['action2'];
    }
    $redirect_to = remove_query_arg(array('action', 'action2', 'bid', 'deleted', 'error', 'updated'), $_SERVER['REQUEST_URI']);
    do_action('buddydrive_files_admin_load', $doaction);
    if ('do_delete' == $doaction && !empty($_GET['bid'])) {
        check_admin_referer('buddydrive-delete');
        $item_ids = wp_parse_id_list($_GET['bid']);
        $count = buddydrive_delete_item(array('ids' => $item_ids, 'user_id' => false));
        $redirect_to = add_query_arg('deleted', $count, $redirect_to);
        bp_core_redirect($redirect_to);
    } elseif ('edit' == $doaction && !empty($_GET['bid'])) {
        // columns screen option
        add_screen_option('layout_columns', array('default' => 2, 'max' => 2));
        get_current_screen()->add_help_tab(array('id' => 'buddydrive-edit-overview', 'title' => __('Overview', 'buddydrive'), 'content' => '<p>' . __('This page is a convenient way to edit the details associated with one of your file or folder.', 'buddydrive') . '</p>' . '<p>' . __('The Name and Description box is fixed in place, but you can reposition all the other boxes using drag and drop, and can minimize or expand them by clicking the title bar of each box. Use the Screen Options tab to hide or unhide, or to choose a 1- or 2-column layout for this screen.', 'buddydrive') . '</p>'));
        // Register metaboxes for the edit screen.
        add_meta_box('submitdiv', _x('Save', 'buddydrive-item admin edit screen', 'buddydrive'), 'buddydrive_admin_edit_metabox_status', get_current_screen()->id, 'side', 'high');
        add_meta_box('buddydrive_item_privacy', _x('Privacy', 'buddydrive-item admin edit screen', 'buddydrive'), 'buddydrive_admin_edit_metabox_privacy', get_current_screen()->id, 'side', 'core');
        add_meta_box('buddydrive_item_children', _x('Files', 'buddydrive-item admin edit screen', 'buddydrive'), 'buddydrive_admin_edit_metabox_list_files', get_current_screen()->id, 'normal', 'core');
        do_action('buddydrive_files_admin_meta_boxes');
        // Enqueue javascripts
        wp_enqueue_script('postbox');
        wp_enqueue_script('dashboard');
        wp_enqueue_script('comment');
        // Index screen
    } else {
        $buddydrive_list_table = new BuddyDrive_List_Table();
    }
    if ($doaction && 'save' == $doaction) {
        // Get item ID
        $item_id = isset($_REQUEST['bid']) ? (int) $_REQUEST['bid'] : '';
        $redirect_to = add_query_arg(array('bid' => (int) $item_id, 'action' => 'edit'), $redirect_to);
        // Check this is a valid form submission
        check_admin_referer('edit-buddydrive-item_' . $item_id);
        $item = buddydrive_get_buddyfile($item_id, array(buddydrive_get_folder_post_type(), buddydrive_get_file_post_type()));
        if (empty($item->title)) {
            wp_redirect($redirect_to);
            exit;
        }
        $args = array();
        if (!empty($_POST['buddydrive-edit']['item-title'])) {
            $args['title'] = wp_kses($_POST['buddydrive-edit']['item-title'], array());
        }
        if (!empty($_POST['buddydrive-edit']['item-content'])) {
            $args['content'] = wp_kses($_POST['buddydrive-edit']['item-content'], array());
        }
        if (!empty($_POST['buddydrive-edit']['sharing'])) {
            $args['privacy'] = $_POST['buddydrive-edit']['sharing'];
        }
        if (!empty($_POST['buddydrive-edit']['password'])) {
            $args['password'] = wp_kses($_POST['buddydrive-edit']['password'], array());
        }
        if (!empty($_POST['buddydrive-edit']['buddygroup'])) {
            $args['group'] = $_POST['buddydrive-edit']['buddygroup'];
        }
        $args['parent_folder_id'] = !empty($_POST['buddydrive-edit']['folder']) ? intval($_POST['buddydrive-edit']['folder']) : 0;
        $updated = buddydrive_update_item($args, $item);
        if (!empty($updated)) {
            $redirect_to = add_query_arg('updated', 1, $redirect_to);
        } else {
            $redirect_to = add_query_arg('error', 1, $redirect_to);
        }
        wp_redirect(apply_filters('buddydrive_item_admin_edit_redirect', $redirect_to));
        exit;
    }
}