Skip to:
Content

BuddyPress.org


Ignore:
Timestamp:
10/14/2013 11:28:49 PM (10 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/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}
Note: See TracChangeset for help on using the changeset viewer.