Exemplo 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');
 }
/**
 * Upload a file
 *
 * @since  1.3.0
 *
 * @param  array   $file    the $_FILES var
 * @param  int     $user_id the ID of the user submitting the file
 * @return array            the upload result
 */
function buddydrive_upload_item($file = array(), $user_id = 0)
{
    if (empty($file) || empty($user_id)) {
        return false;
    }
    // In multisite, we need to remove some filters
    if (is_multisite()) {
        remove_filter('upload_mimes', 'check_upload_mimes');
        remove_filter('upload_size_limit', 'upload_size_limit_filter');
    }
    // Accents can be problematic.
    add_filter('sanitize_file_name', 'remove_accents', 10, 1);
    $buddydrive_attachment = new BuddyDrive_Attachment();
    $upload = $buddydrive_attachment->upload($file);
    // Restore/remove filters
    if (is_multisite()) {
        add_filter('upload_mimes', 'check_upload_mimes');
        add_filter('upload_size_limit', 'upload_size_limit_filter');
    }
    // Others can deal with Accents in filename the way they want.
    remove_filter('sanitize_file_name', 'remove_accents', 10, 1);
    $action_suffix = '_failed';
    /**
     * file was uploaded !!
     * Now we can update the user's space
     */
    if (isset($upload['file']) && empty($upload['error'])) {
        $action_suffix = '_succeeded';
        buddydrive_update_user_space($user_id, $file['buddyfile-upload']['size']);
    }
    /**
     * Allow actions once the file is processed
     *
     * Use buddydrive_upload_item_failed to do actions in case the file was not uploaded
     * Use buddydrive_upload_item_succeeded to do actions in case the file was uploaded
     *
     * @since 1.3
     *
     * @param array $upload upload results
     * @param array $file the file before being moved to upload dir
     * @param int   $user_id the ID of the user who uploaded the file.
     */
    do_action("buddydrive_upload_item{$action_suffix}", $upload, $file, $user_id);
    return $upload;
}
 /**
  * Deletes a list of items or all the items of a given user
  *
  * @param  array $ids array of BuddyDrive Item ids
  * @param  int $user_id the id of a user
  * @global object $wpdb
  * @uses get_user_meta() to get the quota of the user id
  * @uses buddydrive_get_buddyfile() to get the BuddyDrive item
  * @uses wp_delete_post() to delete the BuddyDrive post type
  * @uses update_user_meta() to eventually update user's quota
  * @return int number of deleted items
  */
 public function delete($ids = false, $user_id = false)
 {
     global $wpdb;
     $buddydrive_ids = array();
     $spaces = array();
     $new_space = false;
     $ids = array_filter(wp_parse_id_list($ids));
     if (!empty($ids)) {
         //we need to get the children
         $in = '("' . implode('","', $ids) . '")';
         $buddydrive_ids = $wpdb->get_col("SELECT ID FROM {$wpdb->base_prefix}posts WHERE post_parent IN {$in}");
         $buddydrive_ids = array_merge($buddydrive_ids, $ids);
     } elseif (!empty($user_id) && empty($ids)) {
         // in case a user is deleted
         $buddydrive_ids = $wpdb->get_col($wpdb->prepare("SELECT ID FROM {$wpdb->base_prefix}posts WHERE post_author = %d AND post_type IN (%s, %s)", $user_id, buddydrive_get_folder_post_type(), buddydrive_get_file_post_type()));
         $new_user = (int) apply_filters('buddydrive_set_owner_on_user_deleted', 0);
         // The new user must have the power to post in any group
         if (!empty($new_user) && user_can($new_user, 'bp_moderate') && !empty($buddydrive_ids)) {
             $wpdb->query($wpdb->prepare("UPDATE {$wpdb->base_prefix}posts SET post_author = %d WHERE post_author = %d AND post_type IN (%s, %s)", $new_user, $user_id, buddydrive_get_folder_post_type(), buddydrive_get_file_post_type()));
             foreach ($buddydrive_ids as $post_id) {
                 clean_post_cache($post_id);
             }
         }
     }
     if (empty($buddydrive_ids)) {
         return false;
     }
     if (empty($new_user)) {
         foreach ($buddydrive_ids as $id) {
             $buddyfile = buddydrive_get_buddyfile($id);
             if (!empty($buddyfile)) {
                 if (!empty($buddyfile->path) && file_exists($buddyfile->path)) {
                     if (!isset($spaces[$buddyfile->user_id])) {
                         $spaces[$buddyfile->user_id] = filesize($buddyfile->path);
                     } else {
                         $spaces[$buddyfile->user_id] += filesize($buddyfile->path);
                     }
                     unlink($buddyfile->path);
                 }
                 // Delete the thumbnail
                 if ('public' === $buddyfile->check_for) {
                     buddydrive_delete_thumbnail($buddyfile->ID);
                 }
             }
             wp_delete_post($id, true);
         }
     }
     if (!empty($spaces)) {
         foreach ($spaces as $u_id => $space) {
             $user_total_space = get_user_meta($u_id, '_buddydrive_total_space', true);
             $user_total_space = intval($user_total_space);
             if ($space < $user_total_space) {
                 buddydrive_update_user_space($u_id, -1 * absint($space));
             } else {
                 delete_user_meta($u_id, '_buddydrive_total_space');
             }
         }
     }
     return count($buddydrive_ids);
 }