Skip to content

Commit 22d9c1b

Browse files
committed
Menus: Add current_page_parent for CPT archive on singular views
1 parent b10d2f9 commit 22d9c1b

2 files changed

Lines changed: 115 additions & 0 deletions

File tree

src/wp-includes/nav-menu-template.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -394,6 +394,13 @@ function _wp_menu_item_classes_by_context( &$menu_items ) {
394394
$front_page_id = (int) get_option( 'page_on_front' );
395395
$privacy_policy_page_id = (int) get_option( 'wp_page_for_privacy_policy' );
396396

397+
$public_custom_post_types = get_post_types(
398+
array(
399+
'public' => true,
400+
'_builtin' => false,
401+
)
402+
);
403+
397404
foreach ( (array) $menu_items as $key => $menu_item ) {
398405

399406
$menu_items[ $key ]->current = false;
@@ -526,6 +533,15 @@ function _wp_menu_item_classes_by_context( &$menu_items ) {
526533
}
527534
}
528535

536+
// If a public custom post type single is being viewed, the archive menu item is a logical parent (#38836).
537+
if ( is_singular( $public_custom_post_types ) ) {
538+
$post_type = get_post_type();
539+
540+
if ( $post_type === $menu_item->object && 'post_type_archive' === $menu_item->type ) {
541+
$classes[] = 'current_page_parent';
542+
}
543+
}
544+
529545
// Back-compat with wp_page_menu(): add "current_page_parent" to static home page link for any non-page query.
530546
if ( ! empty( $home_page_id ) && 'post_type' === $menu_item->type
531547
&& empty( $wp_query->is_page ) && $home_page_id === (int) $menu_item->object_id

tests/phpunit/tests/post/nav-menu.php

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1392,4 +1392,103 @@ public function test_wp_update_nav_menu_item_with_post_date() {
13921392
$post = get_post( $menu_item_id );
13931393
$this->assertEqualsWithDelta( strtotime( gmdate( 'Y-m-d H:i:s' ) ), strtotime( $post->post_date ), 2, 'The dates should be equal' );
13941394
}
1395+
1396+
/**
1397+
* Post type archive menu items should receive current_page_parent on public CPT single views.
1398+
*
1399+
* @ticket 38836
1400+
*/
1401+
public function test_post_type_archive_menu_item_gets_current_page_parent_on_public_cpt_single() {
1402+
$page_id = self::factory()->post->create(
1403+
array(
1404+
'post_type' => 'page',
1405+
'post_title' => 'Test Page',
1406+
)
1407+
);
1408+
1409+
register_post_type(
1410+
'cpt38836',
1411+
array(
1412+
'public' => true,
1413+
'has_archive' => true,
1414+
)
1415+
);
1416+
1417+
$cpt_id = self::factory()->post->create(
1418+
array(
1419+
'post_type' => 'cpt38836',
1420+
'post_name' => 'cpt-name',
1421+
'post_status' => 'publish',
1422+
)
1423+
);
1424+
1425+
wp_update_nav_menu_item(
1426+
$this->menu_id,
1427+
0,
1428+
array(
1429+
'menu-item-type' => 'post_type_archive',
1430+
'menu-item-object' => 'cpt38836',
1431+
'menu-item-status' => 'publish',
1432+
)
1433+
);
1434+
1435+
wp_update_nav_menu_item(
1436+
$this->menu_id,
1437+
0,
1438+
array(
1439+
'menu-item-type' => 'post_type',
1440+
'menu-item-object' => 'page',
1441+
'menu-item-object-id' => $page_id,
1442+
'menu-item-status' => 'publish',
1443+
)
1444+
);
1445+
1446+
$this->go_to( get_permalink( $cpt_id ) );
1447+
1448+
$menu_items = wp_get_nav_menu_items( $this->menu_id );
1449+
_wp_menu_item_classes_by_context( $menu_items );
1450+
1451+
$this->assertContains( 'current_page_parent', $menu_items[0]->classes, 'Archive item should be marked parent on CPT single.' );
1452+
$this->assertNotContains( 'current_page_parent', $menu_items[1]->classes, 'Unrelated page item should not get the class.' );
1453+
}
1454+
1455+
/**
1456+
* Non-public CPT singles should not add current_page_parent to the archive menu item.
1457+
*
1458+
* @ticket 38836
1459+
*/
1460+
public function test_post_type_archive_menu_item_is_not_current_page_parent_for_non_public_cpt_single() {
1461+
register_post_type(
1462+
'cpt38836b',
1463+
array(
1464+
'has_archive' => true,
1465+
)
1466+
);
1467+
1468+
$cpt_id = self::factory()->post->create(
1469+
array(
1470+
'post_type' => 'cpt38836b',
1471+
'post_name' => 'cpt-name',
1472+
'post_status' => 'publish',
1473+
)
1474+
);
1475+
1476+
wp_update_nav_menu_item(
1477+
$this->menu_id,
1478+
0,
1479+
array(
1480+
'menu-item-type' => 'post_type_archive',
1481+
'menu-item-object' => 'cpt38836b',
1482+
'menu-item-status' => 'publish',
1483+
)
1484+
);
1485+
1486+
$this->go_to( get_permalink( $cpt_id ) );
1487+
1488+
$menu_items = wp_get_nav_menu_items( $this->menu_id );
1489+
_wp_menu_item_classes_by_context( $menu_items );
1490+
1491+
$cpt_archive_classes = $menu_items[0]->classes;
1492+
$this->assertNotContains( 'current_page_parent', $cpt_archive_classes );
1493+
}
13951494
}

0 commit comments

Comments
 (0)