/**
 * Create fake "post" objects for BP's logged-in nav menu for use in the WordPress "Menus" settings page.
 *
 * WordPress nav menus work by representing post or tax term data as a custom
 * post type, which is then used to populate the checkboxes that appear on
 * Dashboard > Appearance > Menu as well as the menu as rendered on the front
 * end. Most of the items in the BuddyPress set of nav items are neither posts
 * nor tax terms, so we fake a post-like object so as to be compatible with the
 * menu.
 *
 * This technique also allows us to generate links dynamically, so that, for
 * example, "My Profile" will always point to the URL of the profile of the
 * logged-in user.
 *
 * @since BuddyPress (1.9.0)
 *
 * @return mixed A URL or an array of dummy pages.
 */
function bp_nav_menu_get_loggedin_pages()
{
    // Try to catch the cached version first
    if (!empty(buddypress()->wp_nav_menu_items->loggedin)) {
        return buddypress()->wp_nav_menu_items->loggedin;
    }
    // Pull up a list of items registered in BP's top-level nav array
    $bp_menu_items = buddypress()->bp_nav;
    // Alphabetize
    $bp_menu_items = bp_alpha_sort_by_key($bp_menu_items, 'name');
    // Some BP nav menu items will not be represented in bp_nav, because
    // they are not real BP components. We add them manually here.
    $bp_menu_items[] = array('name' => __('Log Out', 'buddypress'), 'slug' => 'logout', 'link' => wp_logout_url());
    // If there's nothing to show, we're done
    if (count($bp_menu_items) < 1) {
        return false;
    }
    $page_args = array();
    foreach ($bp_menu_items as $bp_item) {
        // Remove <span>number</span>
        $item_name = preg_replace('/([.0-9]+)/', '', $bp_item['name']);
        $item_name = trim(strip_tags($item_name));
        $page_args[$bp_item['slug']] = (object) array('ID' => -1, 'post_title' => $item_name, 'post_author' => 0, 'post_date' => 0, 'post_excerpt' => $bp_item['slug'], 'post_type' => 'page', 'post_status' => 'publish', 'comment_status' => 'closed', 'guid' => $bp_item['link']);
    }
    if (empty(buddypress()->wp_nav_menu_items)) {
        buddypress()->wp_nav_menu_items = new stdClass();
    }
    buddypress()->wp_nav_menu_items->loggedin = $page_args;
    return $page_args;
}
示例#2
0
 /**
  * @group bp_alpha_sort_by_key
  */
 public function test_bp_alpha_sort_by_key_objects()
 {
     $items = array(new stdClass(), new stdClass(), new stdClass());
     $items[0]->foo = 'bar';
     $items[0]->name = 'alpha';
     $items[1]->foo = 'bar';
     $items[1]->name = 'charlie';
     $items[2]->foo = 'bar';
     $items[2]->name = 'beta';
     $expected = array(new stdClass(), new stdClass(), new stdClass());
     $expected[0]->foo = 'bar';
     $expected[0]->name = 'alpha';
     $expected[1]->foo = 'bar';
     $expected[1]->name = 'beta';
     $expected[2]->foo = 'bar';
     $expected[2]->name = 'charlie';
     $this->assertEquals($expected, bp_alpha_sort_by_key($items, 'name'));
 }