/** * Updates a BuddyDrive item * * @param array $args the arguments to update * @param object $item the BuddyDrive item * @uses wp_parse_args() to merge defaults and args array * @uses buddydrive_get_file_post_type() to get the BuddyFile post type * @uses get_post_meta() to get privacy options * @uses buddydrive_save_item() to update the item * @return integer $modified the id of the item updated */ function buddydrive_update_item($args = '', $item = false) { if (empty($item)) { return false; } $old_pass = false; if (!empty($item->password)) { $old_pass = $item->password; } $old_group = false; if (!empty($item->group)) { $old_group = $item->group; } $defaults = array('id' => $item->ID, 'type' => $item->post_type, 'user_id' => $item->user_id, 'parent_folder_id' => $item->post_parent, 'title' => $item->title, 'content' => $item->content, 'mime_type' => $item->mime_type, 'guid' => $item->guid, 'privacy' => $item->check_for, 'password' => $old_pass, 'group' => $old_group, 'buddydrive_meta' => false); $params = wp_parse_args($args, $defaults); // if the parent folder was set, then we need to define a default privacy status if (!empty($item->post_parent) && empty($params['parent_folder_id'])) { $params['privacy'] = 'private'; } elseif (!empty($params['parent_folder_id']) && $params['type'] === buddydrive_get_file_post_type()) { $params['privacy'] = get_post_meta($params['parent_folder_id'], '_buddydrive_sharing_option', true); } // building the meta object $meta = new stdClass(); $meta->privacy = $params['privacy']; // Delete the thumbnail if the public file became private if ('public' === $item->check_for && 'public' !== $meta->privacy) { buddydrive_delete_thumbnail($item->ID); } if ($meta->privacy === 'password' && !empty($params['password'])) { $meta->password = $params['password']; } if ($meta->privacy === 'groups') { if (!empty($params['group'])) { $meta->groups = $params['group']; } else { $meta->groups = get_post_meta($params['parent_folder_id'], '_buddydrive_sharing_groups', true); } } if (!empty($params['buddydrive_meta'])) { $meta->buddydrive_meta = $params['buddydrive_meta']; } // preparing the args for buddydrive_save_item $params['metas'] = $meta; // we dont need privacy, password and group as it's in $meta unset($params['privacy']); unset($params['password']); unset($params['group']); $modified = buddydrive_save_item($params); if (empty($modified)) { return false; } do_action('buddydrive_update_item', $params, $args, $item); return $modified; }
/** * 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); }