function bb_flatten_array($array, $cut_branch = 0, $keep_child_array_keys = true) { if (!is_array($array)) { return $array; } if (empty($array)) { return null; } $temp = array(); foreach ($array as $k => $v) { if ($cut_branch && $k == $cut_branch) { continue; } if (is_array($v)) { if ($keep_child_array_keys) { $temp[$k] = true; } $temp += bb_flatten_array($v, $cut_branch, $keep_child_array_keys); } else { $temp[$k] = $v; } } return $temp; }
function bb_get_forums($args = null) { global $bbdb; if (is_numeric($args)) { $args = array('child_of' => $args, 'hierarchical' => 1, 'depth' => 0); } elseif (is_callable($args)) { $args = array('callback' => $args); if (1 < func_num_args()) { $args['callback_args'] = func_get_arg(1); } } $defaults = array('callback' => false, 'callback_args' => false, 'child_of' => 0, 'hierarchical' => 0, 'depth' => 0, 'cut_branch' => 0, 'where' => '', 'order_by' => 'forum_order'); $args = wp_parse_args($args, $defaults); extract($args, EXTR_SKIP); $child_of = (int) $child_of; $hierarchical = 'false' === $hierarchical ? false : (bool) $hierarchical; $depth = (int) $depth; $where = apply_filters('get_forums_where', $where); $key = md5(serialize($where . '|' . $order_by)); // The keys that change the SQL query if (false !== ($forum_ids = wp_cache_get($key, 'bb_forums'))) { $forums = _bb_get_cached_data($forum_ids, 'bb_forum', 'get_forum'); } else { $forum_ids = array(); $forums = array(); $_forums = (array) $bbdb->get_results("SELECT * FROM {$bbdb->forums} {$where} ORDER BY `{$order_by}`;"); $_forums = bb_append_meta($_forums, 'forum'); foreach ($_forums as $f) { $forums[(int) $f->forum_id] = $f; $forum_ids[] = (int) $f->forum_id; wp_cache_add($f->forum_id, $f, 'bb_forum'); wp_cache_add($f->forum_slug, $f->forum_id, 'bb_forum_slug'); } wp_cache_set($key, $forum_ids, 'bb_forums'); } $forums = (array) apply_filters('get_forums', $forums); if ($child_of || $hierarchical || $depth) { $_forums = bb_get_forums_hierarchical($child_of, $depth, $forums); if (!is_array($_forums)) { return false; } $_forums = (array) bb_flatten_array($_forums, $cut_branch); foreach (array_keys($_forums) as $_id) { $_forums[$_id] = $forums[$_id]; } $forums = $_forums; } if (!is_callable($callback)) { return $forums; } if (!is_array($callback_args)) { $callback_args = array(); } foreach (array_keys($forums) as $f) { $_callback_args = $callback_args; array_push($_callback_args, $forums[$f]->forum_id); if (false == call_user_func_array($callback, $_callback_args)) { // $forum_id will be last arg; unset($forums[$f]); } } return $forums; }