| 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 | */ |
| 156 | function 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 ) ) { |
| 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 | } |
| 199 | add_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 | */ |
| 213 | function 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 | } |
| 227 | add_filter( 'nav_menu_css_class', 'bp_core_menu_highlight_nav_menu_item', 10, 2 ); |
| 228 | |
| 229 | /** |