コード例 #1
0
 /**
  * @group bp_docs_get_folder_in_group_term_slug
  */
 public function test_bp_docs_get_folder_in_group_term_slug()
 {
     $this->assertSame('bp_docs_folder_in_group_0', bp_docs_get_folder_in_group_term_slug('rchrchcrh'));
     $this->assertSame('bp_docs_folder_in_group_0', bp_docs_get_folder_in_group_term_slug(0));
     $this->assertSame('bp_docs_folder_in_group_9', bp_docs_get_folder_in_group_term_slug(9));
 }
コード例 #2
0
/**
 * Get a list of folders.
 *
 * @since 1.9
 *
 * @param array $args {
 *     List of arguments.
 *     @type int $group_id Optional. ID of the group.
 *     @type int $user_id Optional. ID of the user.
 *     @type string $display Optional. Format of return value. 'tree' to
 *           display as hierarchical tree, 'flat' to return flat list. Default:
 *           'tree'.
 *     @type bool $force_all_folders Optional. Set to 'true' to include all
 *           folders, 'false' to exclude folders associated with users or
 *           groups (ie, include only "global" folders). Default: false.
 * }
 * @return array
 */
function bp_docs_get_folders($args = array())
{
    $group_id = null;
    if (bp_is_active('groups') && bp_is_group()) {
        $group_id = bp_get_current_group_id();
    }
    $user_id = null;
    if (bp_is_user()) {
        $user_id = bp_displayed_user_id();
    }
    $parent_id = 0;
    if (isset($_GET['folder'])) {
        $parent_id = intval($_GET['folder']);
    }
    // Don't try to do a tree display with a parent ID
    if (!is_null($parent_id)) {
        $display = 'flat';
    } else {
        $display = 'tree';
    }
    // Default order
    if (isset($_GET['order']) && 'desc' === strtolower($_GET['order'])) {
        $order = 'DESC';
    } else {
        $order = 'ASC';
    }
    // Default search terms
    $search_terms = null;
    if (!empty($_GET['s'])) {
        $search_terms = urldecode($_GET['s']);
    }
    $defaults = array('group_id' => $group_id, 'user_id' => $user_id, 'display' => $display, 'force_all_folders' => false, 'parent_id' => $parent_id, 'include' => null, 'search_terms' => $search_terms);
    if (function_exists('bp_parse_args')) {
        $r = bp_parse_args($args, $defaults, 'bp_docs_get_folders');
    } else {
        $r = wp_parse_args($args, $defaults);
    }
    $last_changed = wp_cache_get('last_changed', 'bp_docs_folders');
    if (false === $last_changed) {
        $last_changed = microtime();
        wp_cache_set('last_changed', $last_changed, 'bp_docs_folders');
    }
    ksort($r);
    $cache_key = 'bp_docs_folders:' . md5(serialize($r) . $last_changed);
    $cached = wp_cache_get($cache_key, 'bp_docs_folders');
    if (false !== $cached) {
        return $cached;
    }
    $post_args = array('post_type' => 'bp_docs_folder', 'orderby' => 'title', 'order' => $order, 'posts_per_page' => '-1', 'tax_query' => array());
    // @todo support for multiple items
    if (!empty($r['group_id'])) {
        $post_args['tax_query'][] = array('taxonomy' => 'bp_docs_folder_in_group', 'terms' => array(bp_docs_get_folder_in_group_term_slug($r['group_id'])), 'field' => 'slug');
    } else {
        if (!empty($r['user_id'])) {
            $post_args['tax_query'][] = array('taxonomy' => 'bp_docs_folder_in_user', 'terms' => array(bp_docs_get_folder_in_user_term_slug($r['user_id'])), 'field' => 'slug');
            // Must exclude all user and group folders
            // @todo Find better way to do this
        } else {
            if (empty($r['force_all_folders'])) {
                $folder_taxonomies = array('bp_docs_folder_in_user');
                if (bp_is_active('groups')) {
                    $folder_taxonomies[] = 'bp_docs_folder_in_group';
                }
                $object_folders = get_terms($folder_taxonomies, array('hide_empty' => false, 'fields' => 'ids'));
                if (empty($object_folders)) {
                    $object_folders = array(0);
                }
                if (bp_is_active('groups')) {
                    $post_args['tax_query'][] = array('taxonomy' => 'bp_docs_folder_in_group', 'terms' => wp_parse_id_list($object_folders), 'field' => 'term_id', 'operator' => 'NOT IN');
                }
                $post_args['tax_query'][] = array('taxonomy' => 'bp_docs_folder_in_user', 'terms' => wp_parse_id_list($object_folders), 'field' => 'term_id', 'operator' => 'NOT IN');
            }
        }
    }
    if (!is_null($r['parent_id'])) {
        $post_args['post_parent__in'] = wp_parse_id_list($r['parent_id']);
    }
    if (!is_null($r['include'])) {
        $post_args['post__in'] = wp_parse_id_list($r['include']);
    }
    if (!is_null($r['search_terms'])) {
        $post_args['s'] = $r['search_terms'];
    }
    $folders = get_posts($post_args);
    // Poor-man's walker
    if ('tree' === $r['display']) {
        $ref = $tree = array();
        // Populate top-level items first
        foreach ($folders as $folder_index => $folder) {
            if (empty($folder->post_parent)) {
                $folder->children = array();
                // Place in the top level of the tree
                $tree[$folder->ID] = $folder;
                // Set the reference index
                $ref[$folder->ID] =& $tree[$folder->ID];
                // Remove from original folder index
                unset($folders[$folder_index]);
            }
        }
        while (!empty($folders)) {
            foreach ($folders as $folder_index => $folder) {
                // Nested
                if (!empty($folder->post_parent) && isset($ref[$folder->post_parent])) {
                    $folder->children = array();
                    // Place in the tree
                    $children = $ref[$folder->post_parent]->children;
                    $children[$folder->ID] = $folder;
                    $ref[$folder->post_parent]->children = $children;
                    // Set the reference index
                    $ref[$folder->ID] =& $ref[$folder->post_parent]->children[$folder->ID];
                    // Remove from original folders array
                    unset($folders[$folder_index]);
                }
            }
        }
        $folders = $tree;
    }
    wp_cache_set($cache_key, $folders, 'bp_docs_folders');
    return $folders;
}