/** * Gets a single BuddyDrive items * * @param string|int $name the post name or the id of the item to get * @param string $type the BuddyDrive post type * @uses buddydrive_get_file_post_type() to default to the BuddyFile post type * @uses BuddyDrive_Item::get() to get the BuddyDrive item * @uses buddydrive_get_root_url() to get BuddyDrive root url * @uses get_post_meta() to get item's privacy options * @return object the BuddyDrive item */ function buddydrive_get_buddyfile($name = false, $type = false) { if (empty($name)) { return false; } if (empty($type)) { $type = buddydrive_get_file_post_type(); } $buddydrive_file = new BuddyDrive_Item(); if (is_numeric($name)) { $args = array('id' => $name, 'type' => $type); } else { $args = array('name' => $name, 'type' => $type); } $buddydrive_file->get($args); if (empty($buddydrive_file->query->post->ID)) { return false; } $buddyfile = new stdClass(); $buddyfile->ID = $buddydrive_file->query->post->ID; $buddyfile->user_id = $buddydrive_file->query->post->post_author; $buddyfile->title = $buddydrive_file->query->post->post_title; $buddyfile->content = $buddydrive_file->query->post->post_content; $buddyfile->post_parent = $buddydrive_file->query->post->post_parent; $buddyfile->post_type = $buddydrive_file->query->post->post_type; $buddyfile->guid = $buddydrive_file->query->post->guid; // let's default to a folder $buddyitem_slug = $buddyfile->mime_type = 'folder'; // do we have a file ? if ($buddyfile->post_type == buddydrive_get_file_post_type()) { $buddyitem_slug = 'file'; $buddyfile->file = basename($buddydrive_file->query->post->guid); $buddyfile->path = buddydrive()->upload_dir . '/' . $buddyfile->file; $buddyfile->mime_type = $buddydrive_file->query->post->post_mime_type; } $slug = trailingslashit($buddyitem_slug . '/' . $buddydrive_file->query->post->post_name); $link = buddydrive_get_root_url() . '/' . $slug; $buddyfile->link = $link; /* privacy */ $privacy = get_post_meta($buddyfile->ID, '_buddydrive_sharing_option', true); // by default check for user_id $buddyfile->check_for = 'private'; if (!empty($privacy)) { switch ($privacy) { case 'private': $buddyfile->check_for = 'private'; break; case 'password': $buddyfile->check_for = 'password'; $buddyfile->password = !empty($buddydrive_file->query->post->post_password) ? $buddydrive_file->query->post->post_password : false; break; case 'public': $buddyfile->check_for = 'public'; break; case 'friends': $buddyfile->check_for = 'friends'; break; case 'groups': $buddyfile->check_for = 'groups'; $buddyfile->group = get_post_meta($buddyfile->ID, '_buddydrive_sharing_groups', true); break; default: $buddyfile->check_for = apply_filters('buddydrive_default_check_for', 'private', $buddyfile); break; } } return $buddyfile; }
/** * BuddyDrive Loop : do we have items for the query asked * * @param array $args the arguments of the query * @global object $buddydrive_template * @uses buddydrive_get_folder_post_type() to get BuddyFolder post type * @uses buddydrive_get_file_post_type() to get BuddyFile post type * @uses bp_displayed_user_id() to default to current displayed user * @uses bp_current_action() to get the current action ( files / friends / admin) * @uses bp_is_active() to check if groups component is active * @uses buddydrive_is_group() are we on a group's BuddyDrive ? * @uses wp_parse_args() to merge defaults and args * @uses BuddyDrive_Item::get() to request the DB * @uses BuddyDrive_Item::have_posts to know if BuddyItems matched the query * @return the result of the query */ function buddydrive_has_items($args = '') { global $buddydrive_template; // This keeps us from firing the query more than once if (empty($buddydrive_template)) { $defaulttype = array(buddydrive_get_folder_post_type(), buddydrive_get_file_post_type()); $user = $group_id = $buddyscope = false; if (bp_displayed_user_id()) { $user = bp_displayed_user_id(); } $buddyscope = bp_current_action(); if ($buddyscope == buddydrive_get_friends_subnav_slug()) { $buddyscope = 'friends'; } if (is_admin()) { $buddyscope = 'admin'; } if (bp_is_active('groups') && buddydrive_is_group()) { $group = groups_get_current_group(); $group_id = $group->id; $buddyscope = 'groups'; } /*** * Set the defaults for the parameters you are accepting via the "buddydrive_has_items()" * function call */ $defaults = array('id' => false, 'name' => false, 'group_id' => $group_id, 'user_id' => $user, 'per_page' => 10, 'paged' => 1, 'type' => $defaulttype, 'buddydrive_scope' => $buddyscope, 'search' => false, 'buddydrive_parent' => 0, 'exclude' => 0, 'orderby' => 'title', 'order' => 'ASC'); $r = bp_parse_args($args, $defaults, 'buddydrive_has_items'); if ('admin' === $r['buddydrive_scope'] && !bp_current_user_can('bp_moderate')) { $r['buddydrive_scope'] = 'files'; } $buddydrive_template = new BuddyDrive_Item(); if (!empty($search)) { $buddydrive_template->get(array('per_page' => $r['per_page'], 'paged' => $r['paged'], 'type' => $r['type'], 'buddydrive_scope' => $r['buddydrive_scope'], 'search' => $r['search'], 'orderby' => $r['orderby'], 'order' => $r['order'])); } else { $buddydrive_template->get(array('id' => $r['id'], 'name' => $r['name'], 'group_id' => $r['group_id'], 'user_id' => $r['user_id'], 'per_page' => $r['per_page'], 'paged' => $r['paged'], 'type' => $r['type'], 'buddydrive_scope' => $r['buddydrive_scope'], 'buddydrive_parent' => $r['buddydrive_parent'], 'exclude' => $r['exclude'], 'orderby' => $r['orderby'], 'order' => $r['order'])); } do_action('buddydrive_has_items_catch_total_count', $buddydrive_template->query->found_posts); } return apply_filters('buddydrive_has_items', $buddydrive_template->have_posts()); }
/** * @group get * @group scope */ public function test_buddydrive_item_get_by_scope() { $u2 = $this->factory->user->create(); // Admin $this->set_current_user(1); $by_scope = new BuddyDrive_Item(); // Get by scope $by_scope->get(array('type' => buddydrive_get_file_post_type(), 'buddydrive_scope' => 'admin')); // Admin should see everything $this->assertTrue((int) $by_scope->query->found_posts === 2); // Update the privacy of the file $file_object = buddydrive_get_buddyfile($this->expected_ids['foo']); buddydrive_update_item(array('privacy' => 'public'), $file_object); // Any user $this->set_current_user($u2); add_filter('bp_displayed_user_id', array($this, 'set_displayed_user_id'), 10, 1); $by_scope = new BuddyDrive_Item(); // Get by scope $by_scope->get(array('type' => buddydrive_get_file_post_type(), 'buddydrive_scope' => 'files')); $file = wp_list_pluck($by_scope->query->posts, 'ID'); $this->assertTrue($this->expected_ids['foo'] === (int) $file[0], 'only public files should be listed'); // The owner $this->set_current_user($this->user_id); $by_scope = new BuddyDrive_Item(); // Get by scope $by_scope->get(array('type' => buddydrive_get_file_post_type(), 'buddydrive_scope' => 'files')); // Owner should see everything $this->assertTrue((int) $by_scope->query->found_posts === 2); remove_filter('bp_displayed_user_id', array($this, 'set_displayed_user_id'), 10, 1); // Any user $this->set_current_user($u2); // Update the privacy and owner of the file $file_object = buddydrive_get_buddyfile($this->expected_ids['bar']); buddydrive_update_item(array('privacy' => 'public', 'user_id' => $u2), $file_object); $by_scope = new BuddyDrive_Item(); // Get by scope $by_scope->get(array('type' => buddydrive_get_file_post_type(), 'buddydrive_scope' => 'public')); // Custom loops should be able to list all public files $this->assertTrue((int) $by_scope->query->found_posts === 2); buddydrive_update_item(array('privacy' => 'private'), $file_object); $by_scope = new BuddyDrive_Item(); // Get by scope $by_scope->get(array('type' => buddydrive_get_file_post_type(), 'buddydrive_scope' => 'public')); // Custom loops should be able to list all public files $this->assertTrue((int) $by_scope->query->found_posts === 1); }