Skip to:
Content

BuddyPress.org

Changeset 9322


Ignore:
Timestamp:
01/09/2015 12:39:36 AM (5 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.

Location:
trunk/src/bp-core
Files:
2 edited

Legend:

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

    r9315 r9322  
    343343    global $wp_query;
    344344
    345     // check if BP page belongs to, or is a child of, a BP directory page
    346     $page_id = false;
    347     foreach ( (array) buddypress()->pages as $page ) {
    348         if ( $page->name == buddypress()->unfiltered_uri[buddypress()->unfiltered_uri_offset] ) {
    349             $page_id = $page->id;
    350             break;
    351         }
    352     }
    353 
    354     // Set up reset post args
    355     $reset_post_args = array(
     345    // Reset the post
     346    bp_theme_compat_reset_post( array(
     347        'ID'          => 0,
    356348        'is_404'      => true,
    357349        'post_status' => 'publish',
    358     );
    359 
    360     // BP page exists - fill in the $wp_query->post object
    361     //
    362     // bp_theme_compat_reset_post() looks at the $wp_query->post object to fill in
    363     // the post globals
    364     if ( ! empty( $page_id ) ) {
    365         $wp_query->post = get_post( $page_id );
    366         $reset_post_args['ID'] = $page_id;
    367     } else {
    368         $reset_post_args['ID'] = 0;
    369     }
    370 
    371     // Reset the post
    372     bp_theme_compat_reset_post( $reset_post_args );
     350    ) );
    373351
    374352    // Set theme compat to false since the reset post function automatically sets
     
    394372    $located_template = apply_filters( 'bp_located_template', $template, $filtered_templates );
    395373    if ( !empty( $located_template ) ) {
    396 
    397374        // Template was located, lets set this as a valid page and not a 404.
    398375        status_header( 200 );
  • 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.