<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head><meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>[BuddyPress][7427] trunk/bp-core: Introduce a BuddyPress links box to nav-menus.php</title>
</head>
<body>
<style type="text/css"><!--
#msg dl.meta { border: 1px #006 solid; background: #369; padding: 6px; color: #fff; }
#msg dl.meta dt { float: left; width: 6em; font-weight: bold; }
#msg dt:after { content:':';}
#msg dl, #msg dt, #msg ul, #msg li, #header, #footer, #logmsg { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt; }
#msg dl a { font-weight: bold}
#msg dl a:link { color:#fc3; }
#msg dl a:active { color:#ff0; }
#msg dl a:visited { color:#cc6; }
h3 { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt; font-weight: bold; }
#msg pre { overflow: auto; background: #ffc; border: 1px #fa0 solid; padding: 6px; }
#logmsg { background: #ffc; border: 1px #fa0 solid; padding: 1em 1em 0 1em; }
#logmsg p, #logmsg pre, #logmsg blockquote { margin: 0 0 1em 0; }
#logmsg p, #logmsg li, #logmsg dt, #logmsg dd { line-height: 14pt; }
#logmsg h1, #logmsg h2, #logmsg h3, #logmsg h4, #logmsg h5, #logmsg h6 { margin: .5em 0; }
#logmsg h1:first-child, #logmsg h2:first-child, #logmsg h3:first-child, #logmsg h4:first-child, #logmsg h5:first-child, #logmsg h6:first-child { margin-top: 0; }
#logmsg ul, #logmsg ol { padding: 0; list-style-position: inside; margin: 0 0 0 1em; }
#logmsg ul { text-indent: -1em; padding-left: 1em; }#logmsg ol { text-indent: -1.5em; padding-left: 1.5em; }
#logmsg > ul, #logmsg > ol { margin: 0 0 1em 0; }
#logmsg pre { background: #eee; padding: 1em; }
#logmsg blockquote { border: 1px solid #fa0; border-left-width: 10px; padding: 1em 1em 0 1em; background: white;}
#logmsg dl { margin: 0; }
#logmsg dt { font-weight: bold; }
#logmsg dd { margin: 0; padding: 0 0 0.5em 0; }
#logmsg dd:before { content:'\00bb';}
#logmsg table { border-spacing: 0px; border-collapse: collapse; border-top: 4px solid #fa0; border-bottom: 1px solid #fa0; background: #fff; }
#logmsg table th { text-align: left; font-weight: normal; padding: 0.2em 0.5em; border-top: 1px dotted #fa0; }
#logmsg table td { text-align: right; border-top: 1px dotted #fa0; padding: 0.2em 0.5em; }
#logmsg table thead th { text-align: center; border-bottom: 1px solid #fa0; }
#logmsg table th.Corner { text-align: left; }
#logmsg hr { border: none 0; border-top: 2px dashed #fa0; height: 1px; }
#header, #footer { color: #fff; background: #636; border: 1px #300 solid; padding: 6px; }
#patch { width: 100%; }
#patch h4 {font-family: verdana,arial,helvetica,sans-serif;font-size:10pt;padding:8px;background:#369;color:#fff;margin:0;}
#patch .propset h4, #patch .binary h4 {margin:0;}
#patch pre {padding:0;line-height:1.2em;margin:0;}
#patch .diff {width:100%;background:#eee;padding: 0 0 10px 0;overflow:auto;}
#patch .propset .diff, #patch .binary .diff {padding:10px 0;}
#patch span {display:block;padding:0 10px;}
#patch .modfile, #patch .addfile, #patch .delfile, #patch .propset, #patch .binary, #patch .copfile {border:1px solid #ccc;margin:10px 0;}
#patch ins {background:#dfd;text-decoration:none;display:block;padding:0 10px;}
#patch del {background:#fdd;text-decoration:none;display:block;padding:0 10px;}
#patch .lines, .info {color:#888;background:#fff;}
--></style>
<div id="msg">
<dl class="meta">
<dt>Revision</dt> <dd><a href="http://buddypress.trac.wordpress.org/changeset/7427">7427</a></dd>
<dt>Author</dt> <dd>boonebgorges</dd>
<dt>Date</dt> <dd>2013-10-14 23:28:49 +0000 (Mon, 14 Oct 2013)</dd>
</dl>
<h3>Log Message</h3>
<pre>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 <a href="http://buddypress.trac.wordpress.org/ticket/5122">#5122</a>
Props imath, r-a-y</pre>
<h3>Modified Paths</h3>
<ul>
<li><a href="#trunkbpcoreadminbpcorefunctionsphp">trunk/bp-core/admin/bp-core-functions.php</a></li>
<li><a href="#trunkbpcorebpcoreadminphp">trunk/bp-core/bp-core-admin.php</a></li>
<li><a href="#trunkbpcorebpcoreclassesphp">trunk/bp-core/bp-core-classes.php</a></li>
<li><a href="#trunkbpcorebpcorefiltersphp">trunk/bp-core/bp-core-filters.php</a></li>
<li><a href="#trunkbpcorebpcorefunctionsphp">trunk/bp-core/bp-core-functions.php</a></li>
</ul>
</div>
<div id="patch">
<h3>Diff</h3>
<a id="trunkbpcoreadminbpcorefunctionsphp"></a>
<div class="modfile"><h4>Modified: trunk/bp-core/admin/bp-core-functions.php (7426 => 7427)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/bp-core/admin/bp-core-functions.php 2013-10-14 23:28:45 UTC (rev 7426)
+++ trunk/bp-core/admin/bp-core-functions.php 2013-10-14 23:28:49 UTC (rev 7427)
</span><span class="lines">@@ -642,3 +642,111 @@
</span><span class="cx">
</span><span class="cx"> return $action;
</span><span class="cx"> }
</span><ins>+
+/** Menus *********************************************************************/
+
+/**
+ * Register meta box and associated JS for BuddyPress WP Nav Menu .
+ *
+ * @since BuddyPress (1.9.0)
+ */
+function bp_admin_wp_nav_menu_meta_box() {
+ if ( ! bp_is_root_blog() ) {
+ return;
+ }
+
+ add_meta_box( 'add-buddypress-nav-menu', __( 'BuddyPress', 'buddypress' ), 'bp_admin_do_wp_nav_menu_meta_box', 'nav-menus', 'side', 'default' );
+
+ add_action( 'admin_print_footer_scripts', 'bp_admin_wp_nav_menu_restrict_items' );
+}
+
+/**
+ * Build and populate the BuddyPress accordion on Appearance > Menus.
+ *
+ * @since BuddyPress (1.9.0)
+ *
+ * @global $nav_menu_selected_id
+ */
+function bp_admin_do_wp_nav_menu_meta_box() {
+ global $nav_menu_selected_id;
+
+ $walker = new BP_Walker_Nav_Menu_Checklist( false );
+ $args = array( 'walker' => $walker );
+
+ $post_type_name = 'buddypress';
+ $current_tab = 'loggedin';
+
+ $tabs = array();
+
+ $tabs['loggedin']['label'] = __( 'Logged-In', 'buddypress' );
+ $tabs['loggedin']['pages'] = bp_nav_menu_get_loggedin_pages();
+
+ $tabs['loggedout']['label'] = __( 'Logged-Out', 'buddypress' );
+ $tabs['loggedout']['pages'] = bp_nav_menu_get_loggedout_pages();
+
+ ?>
+
+ <div id="buddypress-menu" class="posttypediv">
+ <h4><?php _e( 'Logged-In', 'buddypress' ) ?></h4>
+ <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>
+
+ <div id="tabs-panel-posttype-<?php echo $post_type_name; ?>-loggedin" class="tabs-panel <?php
+ echo ( 'loggedin' == $current_tab ? 'tabs-panel-active' : 'tabs-panel-inactive' );
+ ?>">
+ <ul id="buddypress-menu-checklist-loggedin" class="categorychecklist form-no-clear">
+ <?php echo walk_nav_menu_tree( array_map( 'wp_setup_nav_menu_item', $tabs['loggedin']['pages'] ), 0, (object) $args );?>
+ </ul>
+ </div>
+
+ <h4><?php _e( 'Logged-Out', 'buddypress' ) ?></h4>
+ <p><?php _e( '<em>Logged-Out</em> links are not visible to users who are logged in.', 'buddypress' ) ?></p>
+
+ <div id="tabs-panel-posttype-<?php echo $post_type_name; ?>-loggedout" class="tabs-panel <?php
+ ?>">
+ <ul id="buddypress-menu-checklist-loggedout" class="categorychecklist form-no-clear">
+ <?php echo walk_nav_menu_tree( array_map( 'wp_setup_nav_menu_item', $tabs['loggedout']['pages'] ), 0, (object) $args );?>
+ </ul>
+ </div>
+
+ <p class="button-controls">
+ <span class="add-to-menu">
+ <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" />
+ <span class="spinner"></span>
+ </span>
+ </p>
+ </div><!-- /#buddypress-menu -->
+
+ <?php
+}
+
+/**
+ * Restrict various items from view if editing a BuddyPress menu.
+ *
+ * If a person is editing a BP menu item, that person should not be able to
+ * see or edit the following fields:
+ *
+ * - CSS Classes - We use the 'bp-menu' CSS class to determine if the
+ * menu item belongs to BP, so we cannot allow manipulation of this field to
+ * occur.
+ * - URL - This field is automatically generated by BP on output, so this
+ * field is useless and can cause confusion.
+ *
+ * Note: These restrictions are only enforced if javascript is enabled.
+ *
+ * @since BuddyPress (1.9.0)
+ */
+function bp_admin_wp_nav_menu_restrict_items() {
+ ?>
+ <script type="text/javascript">
+ jQuery( '#menu-to-edit').on( 'click', 'a.item-edit', function() {
+ var settings = jQuery(this).closest( '.menu-item-bar' ).next( '.menu-item-settings' );
+ var css_class = settings.find( '.edit-menu-item-classes' );
+
+ if( css_class.val().indexOf( 'bp-menu' ) == 0 ) {
+ css_class.attr( 'readonly', 'readonly' );
+ settings.find( '.field-url' ).css( 'display', 'none' );
+ }
+ });
+ </script>
+ <?php
+}
</ins></span></pre></div>
<a id="trunkbpcorebpcoreadminphp"></a>
<div class="modfile"><h4>Modified: trunk/bp-core/bp-core-admin.php (7426 => 7427)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/bp-core/bp-core-admin.php 2013-10-14 23:28:45 UTC (rev 7426)
+++ trunk/bp-core/bp-core-admin.php 2013-10-14 23:28:49 UTC (rev 7427)
</span><span class="lines">@@ -124,6 +124,9 @@
</span><span class="cx">
</span><span class="cx"> /** BuddyPress Actions ************************************************/
</span><span class="cx">
</span><ins>+ // Load the BuddyPress metabox in the WP Nav Menu Admin UI
+ add_action( 'load-nav-menus.php', 'bp_admin_wp_nav_menu_meta_box' );
+
</ins><span class="cx"> // Add settings
</span><span class="cx"> add_action( 'bp_register_admin_settings', array( $this, 'register_admin_settings' ) );
</span><span class="cx">
</span></span></pre></div>
<a id="trunkbpcorebpcoreclassesphp"></a>
<div class="modfile"><h4>Modified: trunk/bp-core/bp-core-classes.php (7426 => 7427)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/bp-core/bp-core-classes.php 2013-10-14 23:28:45 UTC (rev 7426)
+++ trunk/bp-core/bp-core-classes.php 2013-10-14 23:28:49 UTC (rev 7427)
</span><span class="lines">@@ -2010,3 +2010,80 @@
</span><span class="cx"> $output .= apply_filters( 'bp_walker_nav_menu_start_el', $item_output, $item, $depth, $args );
</span><span class="cx"> }
</span><span class="cx"> }
</span><ins>+
+/**
+ * Create a set of BuddyPress-specific links for use in the Menus admin UI
+ *
+ * Borrowed heavily from WP's Walker_Nav_Menu_Checklist, but modified so as not
+ * to require an actual post type or taxonomy, and to force certain CSS classes
+ *
+ * @since BuddyPress (1.9.0)
+ */
+class BP_Walker_Nav_Menu_Checklist extends Walker_Nav_Menu {
+ public function __construct( $fields = false ) {
+ if ( $fields ) {
+ $this->db_fields = $fields;
+ }
+ }
+
+ public function start_lvl( &$output, $depth = 0, $args = array() ) {
+ $indent = str_repeat( "\t", $depth );
+ $output .= "\n$indent<ul class='children'>\n";
+ }
+
+ public function end_lvl( &$output, $depth = 0, $args = array() ) {
+ $indent = str_repeat( "\t", $depth );
+ $output .= "\n$indent</ul>";
+ }
+
+ /**
+ * @see Walker::start_el()
+ *
+ * @param string $output Passed by reference. Used to append additional content.
+ * @param object $item Menu item data object.
+ * @param int $depth Depth of menu item. Used for padding.
+ * @param object $args
+ */
+ function start_el( &$output, $item, $depth = 0, $args = array(), $id = 0 ) {
+ global $_nav_menu_placeholder;
+
+ $_nav_menu_placeholder = ( 0 > $_nav_menu_placeholder ) ? intval($_nav_menu_placeholder) - 1 : -1;
+ $possible_object_id = isset( $item->post_type ) && 'nav_menu_item' == $item->post_type ? $item->object_id : $_nav_menu_placeholder;
+ $possible_db_id = ( ! empty( $item->ID ) ) && ( 0 < $possible_object_id ) ? (int) $item->ID : 0;
+
+ $indent = ( $depth ) ? str_repeat( "\t", $depth ) : '';
+
+ $output .= $indent . '<li>';
+ $output .= '<label class="menu-item-title">';
+ $output .= '<input type="checkbox" class="menu-item-checkbox';
+
+ if ( property_exists( $item, 'label' ) ) {
+ $title = $item->label;
+ }
+
+ $output .= '" name="menu-item[' . $possible_object_id . '][menu-item-object-id]" value="'. esc_attr( $item->object_id ) .'" /> ';
+ $output .= isset( $title ) ? esc_html( $title ) : esc_html( $item->title );
+ $output .= '</label>';
+
+ if ( empty( $item->url ) ) {
+ $item->url = $item->guid;
+ }
+
+ if ( ! in_array( array( 'bp-menu', 'bp-'. $item->post_excerpt .'-nav' ), $item->classes ) ) {
+ $item->classes[] = 'bp-menu';
+ $item->classes[] = 'bp-'. $item->post_excerpt .'-nav';
+ }
+
+ // Menu item hidden fields
+ $output .= '<input type="hidden" class="menu-item-db-id" name="menu-item[' . $possible_object_id . '][menu-item-db-id]" value="' . $possible_db_id . '" />';
+ $output .= '<input type="hidden" class="menu-item-object" name="menu-item[' . $possible_object_id . '][menu-item-object]" value="'. esc_attr( $item->object ) .'" />';
+ $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 ) .'" />';
+ $output .= '<input type="hidden" class="menu-item-type" name="menu-item[' . $possible_object_id . '][menu-item-type]" value="custom" />';
+ $output .= '<input type="hidden" class="menu-item-title" name="menu-item[' . $possible_object_id . '][menu-item-title]" value="'. esc_attr( $item->title ) .'" />';
+ $output .= '<input type="hidden" class="menu-item-url" name="menu-item[' . $possible_object_id . '][menu-item-url]" value="'. esc_attr( $item->url ) .'" />';
+ $output .= '<input type="hidden" class="menu-item-target" name="menu-item[' . $possible_object_id . '][menu-item-target]" value="'. esc_attr( $item->target ) .'" />';
+ $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 ) .'" />';
+ $output .= '<input type="hidden" class="menu-item-classes" name="menu-item[' . $possible_object_id . '][menu-item-classes]" value="'. esc_attr( implode( ' ', $item->classes ) ) .'" />';
+ $output .= '<input type="hidden" class="menu-item-xfn" name="menu-item[' . $possible_object_id . '][menu-item-xfn]" value="'. esc_attr( $item->xfn ) .'" />';
+ }
+}
</ins></span></pre></div>
<a id="trunkbpcorebpcorefiltersphp"></a>
<div class="modfile"><h4>Modified: trunk/bp-core/bp-core-filters.php (7426 => 7427)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/bp-core/bp-core-filters.php 2013-10-14 23:28:45 UTC (rev 7426)
+++ trunk/bp-core/bp-core-filters.php 2013-10-14 23:28:49 UTC (rev 7427)
</span><span class="lines">@@ -386,3 +386,74 @@
</span><span class="cx"> add_filter( 'bp_modify_page_title', 'wptexturize' );
</span><span class="cx"> add_filter( 'bp_modify_page_title', 'convert_chars' );
</span><span class="cx"> add_filter( 'bp_modify_page_title', 'esc_html' );
</span><ins>+
+/**
+ * Add BuddyPress-specific items to the wp_nav_menu.
+ *
+ * @since BuddyPress (1.9.0)
+ *
+ * @param WP_Post $menu_item The menu item.
+ * @return obj The modified WP_Post object.
+ */
+function bp_setup_nav_menu_item( $menu_item ) {
+ if ( is_admin() ) {
+ return $menu_item;
+ }
+
+ // We use information stored in the CSS class to determine what kind of
+ // menu item this is, and how it should be treated
+ $css_target = preg_match( '/\sbp-(.*)-nav/', implode( ' ', $menu_item->classes), $matches );
+
+ // If this isn't a BP menu item, we can stop here
+ if ( empty( $matches[1] ) ) {
+ return $menu_item;
+ }
+
+ switch ( $matches[1] ) {
+ case 'login' :
+ if ( is_user_logged_in() ) {
+ $menu_item->_invalid = true;
+ } else {
+ $menu_item->url = wp_login_url( wp_guess_url() );
+ }
+
+ break;
+
+ case 'logout' :
+ if ( ! is_user_logged_in() ) {
+ $menu_item->_invalid = true;
+ } else {
+ $menu_item->url = wp_logout_url( wp_guess_url() );
+ }
+
+ break;
+
+ // Don't show the Register link to logged-in users
+ case 'register' :
+ if ( is_user_logged_in() ) {
+ $menu_item->_invalid = true;
+ }
+
+ break;
+
+ // All other BP nav items are specific to the logged-in user,
+ // and so are not relevant to logged-out users
+ default:
+ if ( is_user_logged_in() ) {
+ $menu_item->url = bp_nav_menu_get_item_url( $matches[1] );
+ } else {
+ $menu_item->_invalid = true;
+ }
+
+ break;
+ }
+
+ // Highlight the current page
+ $current = bp_get_requested_url();
+ if ( strpos( $current, $menu_item->url ) !== false ) {
+ $menu_item->classes[] = 'current_page_item';
+ }
+
+ return $menu_item;
+}
+add_filter( 'wp_setup_nav_menu_item', 'bp_setup_nav_menu_item', 10, 1 );
</ins></span></pre></div>
<a id="trunkbpcorebpcorefunctionsphp"></a>
<div class="modfile"><h4>Modified: trunk/bp-core/bp-core-functions.php (7426 => 7427)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/bp-core/bp-core-functions.php 2013-10-14 23:28:45 UTC (rev 7426)
+++ trunk/bp-core/bp-core-functions.php 2013-10-14 23:28:49 UTC (rev 7427)
</span><span class="lines">@@ -94,6 +94,8 @@
</span><span class="cx"> * The main purpose for this function is so that you can avoid having to create
</span><span class="cx"> * your own awkward callback function for usort().
</span><span class="cx"> *
</span><ins>+ * @since BuddyPress (1.9.0)
+ *
</ins><span class="cx"> * @param array $items The array to be sorted. Its constituent items can be
</span><span class="cx"> * either associative arrays or objects.
</span><span class="cx"> * @param string|int $key The array index or property name to sort by.
</span><span class="lines">@@ -1455,3 +1457,178 @@
</span><span class="cx"> <?php
</span><span class="cx"> }
</span><span class="cx"> add_action( 'wp_footer', 'bp_core_print_generation_time' );
</span><ins>+
+/** Nav Menu ******************************************************************/
+
+/**
+ * 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 ) ) {
+ return buddypress()->wp_nav_menu_items;
+ }
+
+ // 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 ) {
+ $item_name = '';
+
+ // 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;
+}
+
+/**
+ * Create fake "post" objects for BP's logged-out 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.
+ *
+ * @since BuddyPress (1.9.0)
+ *
+ * @return mixed A URL or an array of dummy pages.
+ */
+function bp_nav_menu_get_loggedout_pages() {
+
+ // Try to catch the cached version first
+ if ( ! empty( buddypress()->wp_nav_menu_items->loggedout ) ) {
+ return buddypress()->wp_nav_menu_items->loggedout;
+ }
+
+ $bp_menu_items = array();
+
+ // 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 In', 'buddypress' ),
+ 'slug' => 'login',
+ 'link' => wp_login_url(),
+ );
+
+ // The Register page will not always be available (ie, when
+ // registration is disabled)
+ $bp_directory_page_ids = bp_core_get_directory_page_ids();
+
+ if( ! empty( $bp_directory_page_ids['register'] ) ) {
+ $register_page = get_post( $bp_directory_page_ids['register'] );
+ $bp_menu_items[] = array(
+ 'name' => $register_page->post_title,
+ 'slug' => $register_page->post_name,
+ 'link' => get_permalink( $register_page->ID ),
+ );
+ }
+
+ // 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 ) {
+ $page_args[ $bp_item['slug'] ] = (object) array(
+ 'ID' => -1,
+ 'post_title' => $bp_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->loggedout = $page_args;
+
+ return $page_args;
+}
+
+/**
+ * Get the URL for a BuddyPress WP nav menu item, based on slug.
+ *
+ * BuddyPress-specific WP nav menu items have dynamically generated URLs,
+ * based on the identity of the current user. This function lets you fetch the
+ * proper URL for a given nav item slug (such as 'login' or 'messages').
+ *
+ * @since BuddyPress (1.9.0)
+ *
+ * @param string $slug The slug of the nav item: login, register, or one of the
+ * slugs from buddypress()->bp_nav.
+ * @return string $nav_item_url The URL generated for the current user.
+ */
+function bp_nav_menu_get_item_url( $slug ) {
+ $nav_item_url = '';
+ $nav_menu_items = bp_nav_menu_get_loggedin_pages();
+
+ if ( isset( $nav_menu_items[ $slug ] ) ) {
+ $nav_item_url = $nav_menu_items[ $slug ]->guid;
+ }
+
+ return $nav_item_url;
+}
</ins></span></pre>
</div>
</div>
</body>
</html>