Skip to:
Content

BuddyPress.org


Ignore:
Timestamp:
10/14/2013 11:28:49 PM (11 years ago)
Author:
boonebgorges
Message:

Introduce a BuddyPress links box to nav-menus.php

The new BuddyPress metabox on Dashboard > Appearance > Menus allows site
administrators to build navigation menus that include dynamic, user-specific
BuddyPress links. Features:

  • Separate sets of links for logged-in and logged-out users.
  • Logged-in user links are automatically generated based on the logged-in user. For example, adding a 'Settings' item from the BuddyPress menu will create a menu item that points to the logged-in user's Settings page, and will not be shown to logged-out visitors.
  • The list of available Logged-In links is automatically populated with all components that are registered in the BuddyPress navigation (ie, those items that are visible as top-level nav links when viewing a user's profile).
  • Log In and Register links are visible only to logged-out users.

Fixes #5122

Props imath, r-a-y

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/bp-core/bp-core-functions.php

    r7426 r7427  
    9494 * The main purpose for this function is so that you can avoid having to create
    9595 * your own awkward callback function for usort().
     96 *
     97 * @since BuddyPress (1.9.0)
    9698 *
    9799 * @param array $items The array to be sorted. Its constituent items can be
     
    14561458}
    14571459add_action( 'wp_footer', 'bp_core_print_generation_time' );
     1460
     1461/** Nav Menu ******************************************************************/
     1462
     1463/**
     1464 * Create fake "post" objects for BP's logged-in nav menu for use in the WordPress "Menus" settings page.
     1465 *
     1466 * WordPress nav menus work by representing post or tax term data as a custom
     1467 * post type, which is then used to populate the checkboxes that appear on
     1468 * Dashboard > Appearance > Menu as well as the menu as rendered on the front
     1469 * end. Most of the items in the BuddyPress set of nav items are neither posts
     1470 * nor tax terms, so we fake a post-like object so as to be compatible with the
     1471 * menu.
     1472 *
     1473 * This technique also allows us to generate links dynamically, so that, for
     1474 * example, "My Profile" will always point to the URL of the profile of the
     1475 * logged-in user.
     1476 *
     1477 * @since BuddyPress (1.9.0)
     1478 *
     1479 * @return mixed A URL or an array of dummy pages.
     1480 */
     1481function bp_nav_menu_get_loggedin_pages() {
     1482
     1483    // Try to catch the cached version first
     1484    if ( ! empty( buddypress()->wp_nav_menu_items ) ) {
     1485        return buddypress()->wp_nav_menu_items;
     1486    }
     1487
     1488    // Pull up a list of items registered in BP's top-level nav array
     1489    $bp_menu_items = buddypress()->bp_nav;
     1490
     1491    // Alphabetize
     1492    $bp_menu_items = bp_alpha_sort_by_key( $bp_menu_items, 'name' );
     1493
     1494    // Some BP nav menu items will not be represented in bp_nav, because
     1495    // they are not real BP components. We add them manually here.
     1496    $bp_menu_items[] = array(
     1497        'name' => __( 'Log Out', 'buddypress' ),
     1498        'slug' => 'logout',
     1499        'link' => wp_logout_url(),
     1500    );
     1501
     1502    // If there's nothing to show, we're done
     1503    if ( count( $bp_menu_items ) < 1 ) {
     1504        return false;
     1505    }
     1506
     1507    $page_args = array();
     1508
     1509    foreach ( $bp_menu_items as $bp_item ) {
     1510        $item_name = '';
     1511
     1512        // Remove <span>number</span>
     1513        $item_name = preg_replace( '/([.0-9]+)/', '', $bp_item['name'] );
     1514        $item_name = trim( strip_tags( $item_name ) );
     1515
     1516        $page_args[ $bp_item['slug'] ] = (object) array(
     1517            'ID'             => -1,
     1518            'post_title'     => $item_name,
     1519            'post_author'    => 0,
     1520            'post_date'      => 0,
     1521            'post_excerpt'   => $bp_item['slug'],
     1522            'post_type'      => 'page',
     1523            'post_status'    => 'publish',
     1524            'comment_status' => 'closed',
     1525            'guid'           => $bp_item['link']
     1526        );
     1527    }
     1528
     1529    if ( empty( buddypress()->wp_nav_menu_items ) ) {
     1530        buddypress()->wp_nav_menu_items = new stdClass;
     1531    }
     1532
     1533    buddypress()->wp_nav_menu_items->loggedin = $page_args;
     1534
     1535    return $page_args;
     1536}
     1537
     1538/**
     1539 * Create fake "post" objects for BP's logged-out nav menu for use in the WordPress "Menus" settings page.
     1540 *
     1541 * WordPress nav menus work by representing post or tax term data as a custom
     1542 * post type, which is then used to populate the checkboxes that appear on
     1543 * Dashboard > Appearance > Menu as well as the menu as rendered on the front
     1544 * end. Most of the items in the BuddyPress set of nav items are neither posts
     1545 * nor tax terms, so we fake a post-like object so as to be compatible with the
     1546 * menu.
     1547 *
     1548 * @since BuddyPress (1.9.0)
     1549 *
     1550 * @return mixed A URL or an array of dummy pages.
     1551 */
     1552function bp_nav_menu_get_loggedout_pages() {
     1553
     1554    // Try to catch the cached version first
     1555    if ( ! empty( buddypress()->wp_nav_menu_items->loggedout ) ) {
     1556        return buddypress()->wp_nav_menu_items->loggedout;
     1557    }
     1558
     1559    $bp_menu_items = array();
     1560
     1561    // Some BP nav menu items will not be represented in bp_nav, because
     1562    // they are not real BP components. We add them manually here.
     1563    $bp_menu_items[] = array(
     1564        'name' => __( 'Log In', 'buddypress' ),
     1565        'slug' => 'login',
     1566        'link' => wp_login_url(),
     1567    );
     1568
     1569    // The Register page will not always be available (ie, when
     1570    // registration is disabled)
     1571    $bp_directory_page_ids = bp_core_get_directory_page_ids();
     1572
     1573    if( ! empty( $bp_directory_page_ids['register'] ) ) {
     1574        $register_page = get_post( $bp_directory_page_ids['register'] );
     1575        $bp_menu_items[] = array(
     1576            'name' => $register_page->post_title,
     1577            'slug' => $register_page->post_name,
     1578            'link' => get_permalink( $register_page->ID ),
     1579        );
     1580    }
     1581
     1582    // If there's nothing to show, we're done
     1583    if ( count( $bp_menu_items ) < 1 ) {
     1584        return false;
     1585    }
     1586
     1587    $page_args = array();
     1588
     1589    foreach ( $bp_menu_items as $bp_item ) {
     1590        $page_args[ $bp_item['slug'] ] = (object) array(
     1591            'ID'             => -1,
     1592            'post_title'     => $bp_item['name'],
     1593            'post_author'    => 0,
     1594            'post_date'      => 0,
     1595            'post_excerpt'   => $bp_item['slug'],
     1596            'post_type'      => 'page',
     1597            'post_status'    => 'publish',
     1598            'comment_status' => 'closed',
     1599            'guid'           => $bp_item['link']
     1600        );
     1601    }
     1602
     1603    if ( empty( buddypress()->wp_nav_menu_items ) ) {
     1604        buddypress()->wp_nav_menu_items = new stdClass;
     1605    }
     1606
     1607    buddypress()->wp_nav_menu_items->loggedout = $page_args;
     1608
     1609    return $page_args;
     1610}
     1611
     1612/**
     1613 * Get the URL for a BuddyPress WP nav menu item, based on slug.
     1614 *
     1615 * BuddyPress-specific WP nav menu items have dynamically generated URLs,
     1616 * based on the identity of the current user. This function lets you fetch the
     1617 * proper URL for a given nav item slug (such as 'login' or 'messages').
     1618 *
     1619 * @since BuddyPress (1.9.0)
     1620 *
     1621 * @param string $slug The slug of the nav item: login, register, or one of the
     1622 *        slugs from buddypress()->bp_nav.
     1623 * @return string $nav_item_url The URL generated for the current user.
     1624 */
     1625function bp_nav_menu_get_item_url( $slug ) {
     1626    $nav_item_url   = '';
     1627    $nav_menu_items = bp_nav_menu_get_loggedin_pages();
     1628
     1629    if ( isset( $nav_menu_items[ $slug ] ) ) {
     1630        $nav_item_url = $nav_menu_items[ $slug ]->guid;
     1631    }
     1632
     1633    return $nav_item_url;
     1634}
Note: See TracChangeset for help on using the changeset viewer.