Skip to content

Wrong attribution of current_page_parent for menus - Refresh 34839.diff #9039

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: trunk
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 21 additions & 23 deletions src/wp-includes/nav-menu-template.php
Original file line number Diff line number Diff line change
Expand Up @@ -435,14 +435,6 @@ function _wp_menu_item_classes_by_context( &$menu_items ) {
) {
$classes[] = 'current-menu-item';
$menu_items[ $key ]->current = true;
$ancestor_id = (int) $menu_item->db_id;

while (
( $ancestor_id = (int) get_post_meta( $ancestor_id, '_menu_item_menu_item_parent', true ) )
&& ! in_array( $ancestor_id, $active_ancestor_item_ids, true )
) {
$active_ancestor_item_ids[] = $ancestor_id;
}

if ( 'post_type' === $menu_item->type && 'page' === $menu_item->object ) {
// Back compat classes for pages to match wp_page_menu().
Expand All @@ -451,10 +443,6 @@ function _wp_menu_item_classes_by_context( &$menu_items ) {
$classes[] = 'current_page_item';
}

$active_parent_item_ids[] = (int) $menu_item->menu_item_parent;
$active_parent_object_ids[] = (int) $menu_item->post_parent;
$active_object = $menu_item->object;

// If the menu item corresponds to the currently queried post type archive.
} elseif (
'post_type_archive' === $menu_item->type
Expand Down Expand Up @@ -499,22 +487,11 @@ function _wp_menu_item_classes_by_context( &$menu_items ) {
if ( $raw_item_url && in_array( $item_url, $matches, true ) ) {
$classes[] = 'current-menu-item';
$menu_items[ $key ]->current = true;
$ancestor_id = (int) $menu_item->db_id;

while (
( $ancestor_id = (int) get_post_meta( $ancestor_id, '_menu_item_menu_item_parent', true ) )
&& ! in_array( $ancestor_id, $active_ancestor_item_ids, true )
) {
$active_ancestor_item_ids[] = $ancestor_id;
}

if ( in_array( home_url(), array( untrailingslashit( $current_url ), untrailingslashit( $_indexless_current ) ), true ) ) {
// Back compat for home link to match wp_page_menu().
$classes[] = 'current_page_item';
}
$active_parent_item_ids[] = (int) $menu_item->menu_item_parent;
$active_parent_object_ids[] = (int) $menu_item->post_parent;
$active_object = $menu_item->object;

// Give front page item the 'current-menu-item' class when extra query arguments are involved.
} elseif ( $item_url === $front_page_url && is_front_page() ) {
Expand All @@ -526,6 +503,26 @@ function _wp_menu_item_classes_by_context( &$menu_items ) {
}
}

// if current item, set active menu parent and ancestors.
if ( $menu_items[ $key ]->current ) {
$active_parent_item_ids[] = (int) $menu_item->menu_item_parent;
$active_parent_object_ids[] = (int) $menu_item->post_parent;
$active_object = $menu_item->object;

$_anc_id = (int) $menu_item->menu_item_parent;
while (
! in_array( $_anc_id, $active_ancestor_item_ids )
) {
$active_ancestor_item_ids[] = $_anc_id;
foreach ( (array) $menu_items as $_key => $_parent ) {
if ( $_parent->db_id == $_anc_id ) {
$_anc_id = $_parent->menu_item_parent;
break;
}
}
}
}

// Back-compat with wp_page_menu(): add "current_page_parent" to static home page link for any non-page query.
if ( ! empty( $home_page_id ) && 'post_type' === $menu_item->type
&& empty( $wp_query->is_page ) && $home_page_id === (int) $menu_item->object_id
Expand All @@ -535,6 +532,7 @@ function _wp_menu_item_classes_by_context( &$menu_items ) {

$menu_items[ $key ]->classes = array_unique( $classes );
}

$active_ancestor_item_ids = array_filter( array_unique( $active_ancestor_item_ids ) );
$active_parent_item_ids = array_filter( array_unique( $active_parent_item_ids ) );
$active_parent_object_ids = array_filter( array_unique( $active_parent_object_ids ) );
Expand Down
Loading