Skip to:
Content

BuddyPress.org


Ignore:
Timestamp:
01/09/2015 12:39:36 AM (9 years ago)
Author:
r-a-y
Message:

Properly highlight BuddyPress pages in a WordPress nav or page menu.

r8932 attempted to address how BuddyPress pages are highlighted in a
WordPress menu after the refactoring of bp_core_load_template() (see
r8821). r8932 resulted in declaring the BuddyPress parent page as the
current page even when it wasn't.

This commit removes this declaration and changes the highlighting method
to rely on the WP nav and page menu filters. This also fixes issues
with highlighting the BP parent page when the root profiles feature is
enabled (this previously did not work).

Fixes #5997.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/bp-core/bp-core-filters.php

    r9318 r9322  
    139139}
    140140add_filter( 'nav_menu_meta_box_object', 'bp_core_exclude_pages_from_nav_menu_admin', 11, 1 );
     141
     142/**
     143 * Adds current page CSS classes to the parent BP page in a WP Page Menu.
     144 *
     145 * Because BuddyPress primarily uses virtual pages, we need a way to highlight
     146 * the BP parent page during WP menu generation.  This function checks the
     147 * current BP component against the current page in the WP menu to see if we
     148 * should highlight the WP page.
     149 *
     150 * @since BuddyPress (2.2.0)
     151 *
     152 * @param array   $retval CSS classes for the current menu page in the menu
     153 * @param WP_Post $page   The page properties for the current menu item
     154 * @return array
     155 */
     156function bp_core_menu_highlight_parent_page( $retval, $page ) {
     157    if ( ! is_buddypress() ) {
     158        return $retval;
     159    }
     160
     161    $page_id = false;
     162
     163    // loop against all BP component pages
     164    foreach ( (array) buddypress()->pages as $component => $bp_page ) {
     165        // handles the majority of components
     166        if ( bp_is_current_component( $component ) ) {
     167                    $page_id = (int) $bp_page->id;
     168        }
     169
     170        // stop if not on a user page
     171        if ( ! bp_is_user() && ! empty( $page_id ) ) {
     172            break;
     173        }
     174
     175        // members component requires an explicit check due to overlapping components
     176        if ( bp_is_user() && 'members' === $component ) {
     177            $page_id = (int) $bp_page->id;
     178            break;
     179        }
     180    }
     181
     182    // duplicate some logic from Walker_Page::start_el() to highlight menu items
     183    if ( ! empty( $page_id ) ) {
     184        $_bp_page = get_post( $page_id );
     185        if ( in_array( $page->ID, $_bp_page->ancestors, true ) ) {
     186            $retval[] = 'current_page_ancestor';
     187        }
     188        if ( $page->ID === $page_id ) {
     189            $retval[] = 'current_page_item';
     190        } elseif ( $_bp_page && $page->ID === $_bp_page->post_parent ) {
     191            $retval[] = 'current_page_parent';
     192        }
     193    }
     194
     195    $retval = array_unique( $retval );
     196
     197    return $retval;
     198}
     199add_filter( 'page_css_class', 'bp_core_menu_highlight_parent_page', 10, 2 );
     200
     201/**
     202 * Adds current page CSS classes to the parent BP page in a WP Nav Menu.
     203 *
     204 * When {@link wp_nav_menu()} is used, this function helps to highlight the
     205 * current BP parent page during nav menu generation.
     206 *
     207 * @since BuddyPress (2.2.0)
     208 *
     209 * @param array   $retval CSS classes for the current nav menu item in the menu
     210 * @param WP_Post $item   The properties for the current nav menu item
     211 * @return array
     212 */
     213function bp_core_menu_highlight_nav_menu_item( $retval, $item ) {
     214    // If we're not on a BP page or if the current nav item is not a page, stop!
     215    if ( ! is_buddypress() || 'page' !== $item->object ) {
     216        return $retval;
     217    }
     218
     219    // get the WP page
     220    $page   = get_post( $item->object_id );
     221
     222    // see if we should add our highlight CSS classes for the page
     223    $retval = bp_core_menu_highlight_parent_page( $retval, $page );
     224
     225    return $retval;
     226}
     227add_filter( 'nav_menu_css_class', 'bp_core_menu_highlight_nav_menu_item', 10, 2 );
    141228
    142229/**
Note: See TracChangeset for help on using the changeset viewer.