From cfe1b4476be912dc2c33d692859961d6e2481b97 Mon Sep 17 00:00:00 2001 From: Sergey Biryukov Date: Thu, 5 Oct 2017 01:37:39 +0000 Subject: [PATCH] Menus: Introduce `wp_get_nav_menu_name()` to retrieve the name of a navigation menu as set in the admin. Props markoheijnen, christophherr, SpencerFinnell, wojtek.szkutnik, sagarprajapati, welcher, SergeyBiryukov. Fixes #13910. git-svn-id: https://develop.svn.wordpress.org/trunk@41766 602fd350-edb4-49c9-b593-d223f7449a82 --- src/wp-includes/nav-menu.php | 32 +++++++++++++++++++++++++++ tests/phpunit/tests/post/nav-menu.php | 19 ++++++++++++++++ 2 files changed, 51 insertions(+) diff --git a/src/wp-includes/nav-menu.php b/src/wp-includes/nav-menu.php index 7adf8e1a4e..53089a946b 100644 --- a/src/wp-includes/nav-menu.php +++ b/src/wp-includes/nav-menu.php @@ -184,6 +184,38 @@ function has_nav_menu( $location ) { return apply_filters( 'has_nav_menu', $has_nav_menu, $location ); } +/** + * Returns the name of a navigation menu. + * + * @since 4.9.0 + * + * @param string $location Menu location identifier. + * @return string Menu name. + */ +function wp_get_nav_menu_name( $location ) { + $menu_name = ''; + + $locations = get_nav_menu_locations(); + + if ( isset( $locations[ $location ] ) ) { + $menu = wp_get_nav_menu_object( $locations[ $location ] ); + + if ( $menu && $menu->name ) { + $menu_name = $menu->name; + } + } + + /** + * Filters the navigation menu name being returned. + * + * @since 4.9.0 + * + * @param string $menu_name Menu name. + * @param string $location Menu location identifier. + */ + return apply_filters( 'wp_get_nav_menu_name', $menu_name, $location ); +} + /** * Determines whether the given ID is a nav menu item. * diff --git a/tests/phpunit/tests/post/nav-menu.php b/tests/phpunit/tests/post/nav-menu.php index 91a68ce64a..47194f5222 100644 --- a/tests/phpunit/tests/post/nav-menu.php +++ b/tests/phpunit/tests/post/nav-menu.php @@ -158,6 +158,25 @@ class Test_Nav_Menus extends WP_UnitTestCase { $this->assertEquals( $t, $menu_items[0]->object_id ); } + /** + * @ticket 13910 + */ + function test_wp_get_nav_menu_name() { + // Register a nav menu location. + register_nav_menu( 'primary', 'Primary Navigation' ); + + // Create a menu with a title. + $menu = wp_create_nav_menu( 'My Menu' ); + + // Assign the menu to the `primary` location. + $locations = get_nav_menu_locations(); + $menu_obj = wp_get_nav_menu_object( $menu ); + $locations['primary'] = $menu_obj->term_id; + set_theme_mod( 'nav_menu_locations', $locations ); + + $this->assertEquals( 'My Menu', wp_get_nav_menu_name( 'primary' ) ); + } + /** * @ticket 29460 */