Skip to:
Content

BuddyPress.org

Changeset 7427


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

Location:
trunk/bp-core
Files:
5 edited

Legend:

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

    r7228 r7427  
    643643    return $action;
    644644}
     645
     646/** Menus *********************************************************************/
     647
     648/**
     649 * Register meta box and associated JS for BuddyPress WP Nav Menu .
     650 *
     651 * @since BuddyPress (1.9.0)
     652 */
     653function bp_admin_wp_nav_menu_meta_box() {
     654    if ( ! bp_is_root_blog() ) {
     655        return;
     656    }
     657
     658    add_meta_box( 'add-buddypress-nav-menu', __( 'BuddyPress', 'buddypress' ), 'bp_admin_do_wp_nav_menu_meta_box', 'nav-menus', 'side', 'default' );
     659
     660    add_action( 'admin_print_footer_scripts', 'bp_admin_wp_nav_menu_restrict_items' );
     661}
     662
     663/**
     664 * Build and populate the BuddyPress accordion on Appearance > Menus.
     665 *
     666 * @since BuddyPress (1.9.0)
     667 *
     668 * @global $nav_menu_selected_id
     669 */
     670function bp_admin_do_wp_nav_menu_meta_box() {
     671    global $nav_menu_selected_id;
     672
     673    $walker = new BP_Walker_Nav_Menu_Checklist( false );
     674    $args   = array( 'walker' => $walker );
     675
     676    $post_type_name = 'buddypress';
     677    $current_tab    = 'loggedin';
     678
     679    $tabs = array();
     680
     681    $tabs['loggedin']['label']  = __( 'Logged-In', 'buddypress' );
     682    $tabs['loggedin']['pages']  = bp_nav_menu_get_loggedin_pages();
     683
     684    $tabs['loggedout']['label'] = __( 'Logged-Out', 'buddypress' );
     685    $tabs['loggedout']['pages'] = bp_nav_menu_get_loggedout_pages();
     686
     687    ?>
     688
     689    <div id="buddypress-menu" class="posttypediv">
     690        <h4><?php _e( 'Logged-In', 'buddypress' ) ?></h4>
     691        <p><?php _e( '<em>Logged-In</em> links are relative to the current user, and are not visible to visitors who are not logged in.', 'buddypress' ) ?></p>
     692
     693        <div id="tabs-panel-posttype-<?php echo $post_type_name; ?>-loggedin" class="tabs-panel <?php
     694            echo ( 'loggedin' == $current_tab ? 'tabs-panel-active' : 'tabs-panel-inactive' );
     695        ?>">
     696            <ul id="buddypress-menu-checklist-loggedin" class="categorychecklist form-no-clear">
     697                <?php echo walk_nav_menu_tree( array_map( 'wp_setup_nav_menu_item', $tabs['loggedin']['pages'] ), 0, (object) $args );?>
     698            </ul>
     699        </div>
     700
     701        <h4><?php _e( 'Logged-Out', 'buddypress' ) ?></h4>
     702        <p><?php _e( '<em>Logged-Out</em> links are not visible to users who are logged in.', 'buddypress' ) ?></p>
     703
     704        <div id="tabs-panel-posttype-<?php echo $post_type_name; ?>-loggedout" class="tabs-panel <?php
     705        ?>">
     706            <ul id="buddypress-menu-checklist-loggedout" class="categorychecklist form-no-clear">
     707                <?php echo walk_nav_menu_tree( array_map( 'wp_setup_nav_menu_item', $tabs['loggedout']['pages'] ), 0, (object) $args );?>
     708            </ul>
     709        </div>
     710
     711        <p class="button-controls">
     712            <span class="add-to-menu">
     713                <input type="submit"<?php wp_nav_menu_disabled_check( $nav_menu_selected_id ); ?> class="button-secondary submit-add-to-menu right" value="<?php esc_attr_e( 'Add to Menu' ); ?>" name="add-custom-menu-item" id="submit-buddypress-menu" />
     714                <span class="spinner"></span>
     715            </span>
     716        </p>
     717    </div><!-- /#buddypress-menu -->
     718
     719    <?php
     720}
     721
     722/**
     723 * Restrict various items from view if editing a BuddyPress menu.
     724 *
     725 * If a person is editing a BP menu item, that person should not be able to
     726 * see or edit the following fields:
     727 *
     728 * - CSS Classes - We use the 'bp-menu' CSS class to determine if the
     729 *   menu item belongs to BP, so we cannot allow manipulation of this field to
     730 *   occur.
     731 * - URL - This field is automatically generated by BP on output, so this
     732 *   field is useless and can cause confusion.
     733 *
     734 * Note: These restrictions are only enforced if javascript is enabled.
     735 *
     736 * @since BuddyPress (1.9.0)
     737 */
     738function bp_admin_wp_nav_menu_restrict_items() {
     739    ?>
     740    <script type="text/javascript">
     741    jQuery( '#menu-to-edit').on( 'click', 'a.item-edit', function() {
     742        var settings  = jQuery(this).closest( '.menu-item-bar' ).next( '.menu-item-settings' );
     743        var css_class = settings.find( '.edit-menu-item-classes' );
     744
     745        if( css_class.val().indexOf( 'bp-menu' ) == 0 ) {
     746            css_class.attr( 'readonly', 'readonly' );
     747            settings.find( '.field-url' ).css( 'display', 'none' );
     748        }
     749    });
     750    </script>
     751    <?php
     752}
  • trunk/bp-core/bp-core-admin.php

    r7279 r7427  
    124124
    125125        /** BuddyPress Actions ************************************************/
     126
     127        // Load the BuddyPress metabox in the WP Nav Menu Admin UI
     128        add_action( 'load-nav-menus.php', 'bp_admin_wp_nav_menu_meta_box' );
    126129
    127130        // Add settings
  • trunk/bp-core/bp-core-classes.php

    r7338 r7427  
    20112011    }
    20122012}
     2013
     2014/**
     2015 * Create a set of BuddyPress-specific links for use in the Menus admin UI
     2016 *
     2017 * Borrowed heavily from WP's Walker_Nav_Menu_Checklist, but modified so as not
     2018 * to require an actual post type or taxonomy, and to force certain CSS classes
     2019 *
     2020 * @since BuddyPress (1.9.0)
     2021 */
     2022class BP_Walker_Nav_Menu_Checklist extends Walker_Nav_Menu {
     2023    public function __construct( $fields = false ) {
     2024        if ( $fields ) {
     2025            $this->db_fields = $fields;
     2026        }
     2027    }
     2028
     2029    public function start_lvl( &$output, $depth = 0, $args = array() ) {
     2030        $indent = str_repeat( "\t", $depth );
     2031        $output .= "\n$indent<ul class='children'>\n";
     2032    }
     2033
     2034    public function end_lvl( &$output, $depth = 0, $args = array() ) {
     2035        $indent = str_repeat( "\t", $depth );
     2036        $output .= "\n$indent</ul>";
     2037    }
     2038
     2039    /**
     2040     * @see Walker::start_el()
     2041     *
     2042     * @param string $output Passed by reference. Used to append additional content.
     2043     * @param object $item Menu item data object.
     2044     * @param int $depth Depth of menu item. Used for padding.
     2045     * @param object $args
     2046     */
     2047    function start_el( &$output, $item, $depth = 0, $args = array(), $id = 0 ) {
     2048        global $_nav_menu_placeholder;
     2049
     2050        $_nav_menu_placeholder = ( 0 > $_nav_menu_placeholder ) ? intval($_nav_menu_placeholder) - 1 : -1;
     2051        $possible_object_id = isset( $item->post_type ) && 'nav_menu_item' == $item->post_type ? $item->object_id : $_nav_menu_placeholder;
     2052        $possible_db_id = ( ! empty( $item->ID ) ) && ( 0 < $possible_object_id ) ? (int) $item->ID : 0;
     2053
     2054        $indent = ( $depth ) ? str_repeat( "\t", $depth ) : '';
     2055
     2056        $output .= $indent . '<li>';
     2057        $output .= '<label class="menu-item-title">';
     2058        $output .= '<input type="checkbox" class="menu-item-checkbox';
     2059
     2060        if ( property_exists( $item, 'label' ) ) {
     2061            $title = $item->label;
     2062        }
     2063
     2064        $output .= '" name="menu-item[' . $possible_object_id . '][menu-item-object-id]" value="'. esc_attr( $item->object_id ) .'" /> ';
     2065        $output .= isset( $title ) ? esc_html( $title ) : esc_html( $item->title );
     2066        $output .= '</label>';
     2067
     2068        if ( empty( $item->url ) ) {
     2069            $item->url = $item->guid;
     2070        }
     2071
     2072        if ( ! in_array( array( 'bp-menu', 'bp-'. $item->post_excerpt .'-nav' ), $item->classes ) ) {
     2073            $item->classes[] = 'bp-menu';
     2074            $item->classes[] = 'bp-'. $item->post_excerpt .'-nav';
     2075        }
     2076
     2077        // Menu item hidden fields
     2078        $output .= '<input type="hidden" class="menu-item-db-id" name="menu-item[' . $possible_object_id . '][menu-item-db-id]" value="' . $possible_db_id . '" />';
     2079        $output .= '<input type="hidden" class="menu-item-object" name="menu-item[' . $possible_object_id . '][menu-item-object]" value="'. esc_attr( $item->object ) .'" />';
     2080        $output .= '<input type="hidden" class="menu-item-parent-id" name="menu-item[' . $possible_object_id . '][menu-item-parent-id]" value="'. esc_attr( $item->menu_item_parent ) .'" />';
     2081        $output .= '<input type="hidden" class="menu-item-type" name="menu-item[' . $possible_object_id . '][menu-item-type]" value="custom" />';
     2082        $output .= '<input type="hidden" class="menu-item-title" name="menu-item[' . $possible_object_id . '][menu-item-title]" value="'. esc_attr( $item->title ) .'" />';
     2083        $output .= '<input type="hidden" class="menu-item-url" name="menu-item[' . $possible_object_id . '][menu-item-url]" value="'. esc_attr( $item->url ) .'" />';
     2084        $output .= '<input type="hidden" class="menu-item-target" name="menu-item[' . $possible_object_id . '][menu-item-target]" value="'. esc_attr( $item->target ) .'" />';
     2085        $output .= '<input type="hidden" class="menu-item-attr_title" name="menu-item[' . $possible_object_id . '][menu-item-attr_title]" value="'. esc_attr( $item->attr_title ) .'" />';
     2086        $output .= '<input type="hidden" class="menu-item-classes" name="menu-item[' . $possible_object_id . '][menu-item-classes]" value="'. esc_attr( implode( ' ', $item->classes ) ) .'" />';
     2087        $output .= '<input type="hidden" class="menu-item-xfn" name="menu-item[' . $possible_object_id . '][menu-item-xfn]" value="'. esc_attr( $item->xfn ) .'" />';
     2088    }
     2089}
  • trunk/bp-core/bp-core-filters.php

    r7416 r7427  
    387387add_filter( 'bp_modify_page_title', 'convert_chars'   );
    388388add_filter( 'bp_modify_page_title', 'esc_html'        );
     389
     390/**
     391 * Add BuddyPress-specific items to the wp_nav_menu.
     392 *
     393 * @since BuddyPress (1.9.0)
     394 *
     395 * @param WP_Post $menu_item The menu item.
     396 * @return obj The modified WP_Post object.
     397 */
     398function bp_setup_nav_menu_item( $menu_item ) {
     399    if ( is_admin() ) {
     400        return $menu_item;
     401    }
     402
     403    // We use information stored in the CSS class to determine what kind of
     404    // menu item this is, and how it should be treated
     405    $css_target = preg_match( '/\sbp-(.*)-nav/', implode( ' ', $menu_item->classes), $matches );
     406
     407    // If this isn't a BP menu item, we can stop here
     408    if ( empty( $matches[1] ) ) {
     409        return $menu_item;
     410    }
     411
     412    switch ( $matches[1] ) {
     413        case 'login' :
     414            if ( is_user_logged_in() ) {
     415                $menu_item->_invalid = true;
     416            } else {
     417                $menu_item->url = wp_login_url( wp_guess_url() );
     418            }
     419
     420            break;
     421
     422        case 'logout' :
     423            if ( ! is_user_logged_in() ) {
     424                $menu_item->_invalid = true;
     425            } else {
     426                $menu_item->url = wp_logout_url( wp_guess_url() );
     427            }
     428
     429            break;
     430
     431        // Don't show the Register link to logged-in users
     432        case 'register' :
     433            if ( is_user_logged_in() ) {
     434                $menu_item->_invalid = true;
     435            }
     436
     437            break;
     438
     439        // All other BP nav items are specific to the logged-in user,
     440        // and so are not relevant to logged-out users
     441        default:
     442            if ( is_user_logged_in() ) {
     443                $menu_item->url = bp_nav_menu_get_item_url( $matches[1] );
     444            } else {
     445                $menu_item->_invalid = true;
     446            }
     447
     448            break;
     449    }
     450
     451    // Highlight the current page
     452    $current = bp_get_requested_url();
     453    if ( strpos( $current, $menu_item->url ) !== false ) {
     454        $menu_item->classes[] = 'current_page_item';
     455    }
     456
     457    return $menu_item;
     458}
     459add_filter( 'wp_setup_nav_menu_item', 'bp_setup_nav_menu_item', 10, 1 );
  • 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.