/**
 * 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());
}
/**
 * Removes a single BuddyDrive items from group
 *
 * @param int $item_id  the BuddyDrive item id
 * @param int $group_id the group id
 * @uses groups_get_group() to get the group object for the given group_id
 * @uses BuddyDrive_Item::remove_from_group() to delete the options in the DBs
 * @return boolean true or false
 */
function buddydrive_remove_item_from_group($item_id = false, $group_id = false)
{
    $buddydrive_item = new BuddyDrive_Item();
    $group = groups_get_group(array('group_id' => $group_id));
    if (empty($group)) {
        $new_status = 'private';
    } else {
        $new_status = isset($group->status) && 'public' != $group->status ? 'private' : 'public';
    }
    return $buddydrive_item->remove_from_group($item_id, $new_status);
}
/**
 * Update the privacy of children files linked to a folder if updated
 *
 * @param  array $params associtive array of parameters
 * @param  array $args
 * @param  object $item the folder object
 * @uses buddydrive_get_folder_post_type() to check it's a folder
 * @uses BuddyDrive_Item::update_children() to update the files
 */
function buddydrive_update_children($params, $args, $item)
{
    if ($item->post_type != buddydrive_get_folder_post_type()) {
        return;
    }
    $parent_id = intval($params['id']);
    $metas = $params['metas'];
    $buddydrive_update_children = new BuddyDrive_Item();
    $buddydrive_update_children->update_children($parent_id, $metas);
}
示例#4
0
 /**
  * @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);
 }