2010-02-24 09:06:36 +01:00
|
|
|
<?php
|
2010-03-31 09:54:09 +02:00
|
|
|
/**
|
2016-06-06 17:18:31 +02:00
|
|
|
* Nav Menu API: Template functions
|
2010-03-31 09:54:09 +02:00
|
|
|
*
|
|
|
|
* @package WordPress
|
|
|
|
* @subpackage Nav_Menus
|
|
|
|
* @since 3.0.0
|
|
|
|
*/
|
|
|
|
|
2016-08-31 18:31:29 +02:00
|
|
|
/** Walker_Nav_Menu class */
|
|
|
|
require_once ABSPATH . WPINC . '/class-walker-nav-menu.php';
|
|
|
|
|
2010-02-24 09:06:36 +01:00
|
|
|
/**
|
2010-02-28 21:00:49 +01:00
|
|
|
* Displays a navigation menu.
|
2010-02-24 09:06:36 +01:00
|
|
|
*
|
|
|
|
* @since 3.0.0
|
2016-09-08 07:32:03 +02:00
|
|
|
* @since 4.7.0 Added the `item_spacing` argument.
|
2020-07-06 22:44:02 +02:00
|
|
|
* @since 5.5.0 Added the `container_aria_label` argument.
|
2010-02-24 09:06:36 +01:00
|
|
|
*
|
2014-02-21 19:32:15 +01:00
|
|
|
* @param array $args {
|
|
|
|
* Optional. Array of nav menu arguments.
|
|
|
|
*
|
2020-07-07 11:28:01 +02:00
|
|
|
* @type int|string|WP_Term $menu Desired menu. Accepts a menu ID, slug, name, or object.
|
|
|
|
* Default empty.
|
|
|
|
* @type string $menu_class CSS class to use for the ul element which forms the menu.
|
|
|
|
* Default 'menu'.
|
2020-07-06 22:44:02 +02:00
|
|
|
* @type string $menu_id The ID that is applied to the ul element which forms the menu.
|
|
|
|
* Default is the menu slug, incremented.
|
2020-07-07 11:28:01 +02:00
|
|
|
* @type string $container Whether to wrap the ul, and what to wrap it with.
|
|
|
|
* Default 'div'.
|
|
|
|
* @type string $container_class Class that is applied to the container.
|
|
|
|
* Default 'menu-{menu slug}-container'.
|
2020-07-06 22:44:02 +02:00
|
|
|
* @type string $container_id The ID that is applied to the container. Default empty.
|
2020-07-07 11:28:01 +02:00
|
|
|
* @type string $container_aria_label The aria-label attribute that is applied to the container
|
|
|
|
* when it's a nav element. Default empty.
|
2021-01-03 23:04:04 +01:00
|
|
|
* @type callable|false $fallback_cb If the menu doesn't exist, a callback function will fire.
|
2020-07-06 22:44:02 +02:00
|
|
|
* Default is 'wp_page_menu'. Set to false for no fallback.
|
|
|
|
* @type string $before Text before the link markup. Default empty.
|
|
|
|
* @type string $after Text after the link markup. Default empty.
|
|
|
|
* @type string $link_before Text before the link text. Default empty.
|
|
|
|
* @type string $link_after Text after the link text. Default empty.
|
|
|
|
* @type bool $echo Whether to echo the menu or return it. Default true.
|
2020-07-07 11:28:01 +02:00
|
|
|
* @type int $depth How many levels of the hierarchy are to be included.
|
|
|
|
* 0 means all. Default 0.
|
|
|
|
* Default 0.
|
2020-07-06 22:44:02 +02:00
|
|
|
* @type object $walker Instance of a custom walker class. Default empty.
|
2020-07-07 11:28:01 +02:00
|
|
|
* @type string $theme_location Theme location to be used. Must be registered with
|
|
|
|
* register_nav_menu() in order to be selectable by the user.
|
|
|
|
* @type string $items_wrap How the list items should be wrapped. Uses printf() format with
|
|
|
|
* numbered placeholders. Default is a ul with an id and class.
|
|
|
|
* @type string $item_spacing Whether to preserve whitespace within the menu's HTML.
|
|
|
|
* Accepts 'preserve' or 'discard'. Default 'preserve'.
|
2014-02-21 19:32:15 +01:00
|
|
|
* }
|
2020-01-21 21:13:05 +01:00
|
|
|
* @return void|string|false Void if 'echo' argument is true, menu output if 'echo' is false.
|
|
|
|
* False if there are no items or no menu was found.
|
2010-02-24 09:06:36 +01:00
|
|
|
*/
|
|
|
|
function wp_nav_menu( $args = array() ) {
|
2010-06-02 19:04:07 +02:00
|
|
|
static $menu_id_slugs = array();
|
|
|
|
|
2017-12-01 00:11:00 +01:00
|
|
|
$defaults = array(
|
2020-07-06 22:44:02 +02:00
|
|
|
'menu' => '',
|
|
|
|
'container' => 'div',
|
|
|
|
'container_class' => '',
|
|
|
|
'container_id' => '',
|
|
|
|
'container_aria_label' => '',
|
|
|
|
'menu_class' => 'menu',
|
|
|
|
'menu_id' => '',
|
|
|
|
'echo' => true,
|
|
|
|
'fallback_cb' => 'wp_page_menu',
|
|
|
|
'before' => '',
|
|
|
|
'after' => '',
|
|
|
|
'link_before' => '',
|
|
|
|
'link_after' => '',
|
|
|
|
'items_wrap' => '<ul id="%1$s" class="%2$s">%3$s</ul>',
|
|
|
|
'item_spacing' => 'preserve',
|
|
|
|
'depth' => 0,
|
|
|
|
'walker' => '',
|
|
|
|
'theme_location' => '',
|
2017-12-01 00:11:00 +01:00
|
|
|
);
|
2010-03-17 17:27:25 +01:00
|
|
|
|
2010-02-28 00:06:56 +01:00
|
|
|
$args = wp_parse_args( $args, $defaults );
|
2016-09-06 11:06:31 +02:00
|
|
|
|
|
|
|
if ( ! in_array( $args['item_spacing'], array( 'preserve', 'discard' ), true ) ) {
|
2020-01-29 01:45:18 +01:00
|
|
|
// Invalid value, fall back to default.
|
2016-09-06 11:06:31 +02:00
|
|
|
$args['item_spacing'] = $defaults['item_spacing'];
|
|
|
|
}
|
|
|
|
|
2013-09-12 20:48:09 +02:00
|
|
|
/**
|
2016-05-22 20:28:27 +02:00
|
|
|
* Filters the arguments used to display a navigation menu.
|
2013-09-12 20:48:09 +02:00
|
|
|
*
|
|
|
|
* @since 3.0.0
|
|
|
|
*
|
2014-02-20 06:08:13 +01:00
|
|
|
* @see wp_nav_menu()
|
|
|
|
*
|
|
|
|
* @param array $args Array of wp_nav_menu() arguments.
|
2013-09-12 20:48:09 +02:00
|
|
|
*/
|
2010-02-28 21:00:49 +01:00
|
|
|
$args = apply_filters( 'wp_nav_menu_args', $args );
|
2010-02-24 09:45:26 +01:00
|
|
|
$args = (object) $args;
|
2010-03-01 13:53:51 +01:00
|
|
|
|
2014-03-03 21:43:15 +01:00
|
|
|
/**
|
2016-05-22 20:28:27 +02:00
|
|
|
* Filters whether to short-circuit the wp_nav_menu() output.
|
2014-03-03 21:43:15 +01:00
|
|
|
*
|
2020-06-26 20:49:09 +02:00
|
|
|
* Returning a non-null value from the filter will short-circuit wp_nav_menu(),
|
|
|
|
* echoing that value if $args->echo is true, returning that value otherwise.
|
2014-03-03 21:43:15 +01:00
|
|
|
*
|
|
|
|
* @since 3.9.0
|
|
|
|
*
|
|
|
|
* @see wp_nav_menu()
|
|
|
|
*
|
|
|
|
* @param string|null $output Nav menu output to short-circuit with. Default null.
|
2016-09-07 18:15:30 +02:00
|
|
|
* @param stdClass $args An object containing wp_nav_menu() arguments.
|
2014-03-03 21:43:15 +01:00
|
|
|
*/
|
|
|
|
$nav_menu = apply_filters( 'pre_wp_nav_menu', null, $args );
|
|
|
|
|
|
|
|
if ( null !== $nav_menu ) {
|
|
|
|
if ( $args->echo ) {
|
|
|
|
echo $nav_menu;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $nav_menu;
|
|
|
|
}
|
|
|
|
|
2020-01-29 01:45:18 +01:00
|
|
|
// Get the nav menu based on the requested menu.
|
2010-02-28 21:00:49 +01:00
|
|
|
$menu = wp_get_nav_menu_object( $args->menu );
|
2010-03-17 17:27:25 +01:00
|
|
|
|
2020-01-29 01:45:18 +01:00
|
|
|
// Get the nav menu based on the theme_location.
|
2019-07-03 01:42:58 +02:00
|
|
|
$locations = get_nav_menu_locations();
|
|
|
|
if ( ! $menu && $args->theme_location && $locations && isset( $locations[ $args->theme_location ] ) ) {
|
2010-05-14 08:20:30 +02:00
|
|
|
$menu = wp_get_nav_menu_object( $locations[ $args->theme_location ] );
|
2017-12-01 00:11:00 +01:00
|
|
|
}
|
2010-05-14 00:14:54 +02:00
|
|
|
|
2020-01-29 01:45:18 +01:00
|
|
|
// Get the first menu that has items if we still can't find a menu.
|
2017-12-01 00:11:00 +01:00
|
|
|
if ( ! $menu && ! $args->theme_location ) {
|
2014-09-29 17:45:16 +02:00
|
|
|
$menus = wp_get_nav_menus();
|
2010-02-24 09:45:26 +01:00
|
|
|
foreach ( $menus as $menu_maybe ) {
|
2019-07-03 01:42:58 +02:00
|
|
|
$menu_items = wp_get_nav_menu_items( $menu_maybe->term_id, array( 'update_post_term_cache' => false ) );
|
|
|
|
if ( $menu_items ) {
|
2010-02-24 09:45:26 +01:00
|
|
|
$menu = $menu_maybe;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2010-05-03 22:26:11 +02:00
|
|
|
|
2015-06-16 22:33:25 +02:00
|
|
|
if ( empty( $args->menu ) ) {
|
|
|
|
$args->menu = $menu;
|
|
|
|
}
|
|
|
|
|
2010-04-27 03:05:58 +02:00
|
|
|
// If the menu exists, get its items.
|
2017-12-01 00:11:00 +01:00
|
|
|
if ( $menu && ! is_wp_error( $menu ) && ! isset( $menu_items ) ) {
|
2012-10-15 16:17:50 +02:00
|
|
|
$menu_items = wp_get_nav_menu_items( $menu->term_id, array( 'update_post_term_cache' => false ) );
|
2017-12-01 00:11:00 +01:00
|
|
|
}
|
2010-03-26 20:36:49 +01:00
|
|
|
|
2012-09-16 19:07:56 +02:00
|
|
|
/*
|
|
|
|
* If no menu was found:
|
2013-07-10 07:14:43 +02:00
|
|
|
* - Fall back (if one was specified), or bail.
|
2012-09-16 19:07:56 +02:00
|
|
|
*
|
|
|
|
* If no menu items were found:
|
2013-07-10 07:14:43 +02:00
|
|
|
* - Fall back, but only if no theme location was specified.
|
2012-09-16 19:07:56 +02:00
|
|
|
* - Otherwise, bail.
|
|
|
|
*/
|
2017-12-01 00:11:00 +01:00
|
|
|
if ( ( ! $menu || is_wp_error( $menu ) || ( isset( $menu_items ) && empty( $menu_items ) && ! $args->theme_location ) )
|
|
|
|
&& isset( $args->fallback_cb ) && $args->fallback_cb && is_callable( $args->fallback_cb ) ) {
|
2010-04-07 17:25:48 +02:00
|
|
|
return call_user_func( $args->fallback_cb, (array) $args );
|
2017-12-01 00:11:00 +01:00
|
|
|
}
|
2010-05-03 22:26:11 +02:00
|
|
|
|
2017-12-01 00:11:00 +01:00
|
|
|
if ( ! $menu || is_wp_error( $menu ) ) {
|
2010-05-04 00:03:52 +02:00
|
|
|
return false;
|
2017-12-01 00:11:00 +01:00
|
|
|
}
|
2010-05-04 00:03:52 +02:00
|
|
|
|
2019-07-03 01:42:58 +02:00
|
|
|
$nav_menu = '';
|
|
|
|
$items = '';
|
2010-04-07 17:25:48 +02:00
|
|
|
|
2010-06-02 19:04:07 +02:00
|
|
|
$show_container = false;
|
|
|
|
if ( $args->container ) {
|
2013-09-12 20:48:09 +02:00
|
|
|
/**
|
2016-05-22 20:28:27 +02:00
|
|
|
* Filters the list of HTML tags that are valid for use as menu containers.
|
2013-09-12 20:48:09 +02:00
|
|
|
*
|
|
|
|
* @since 3.0.0
|
|
|
|
*
|
2019-10-26 23:09:04 +02:00
|
|
|
* @param string[] $tags The acceptable HTML tags for use as menu containers.
|
|
|
|
* Default is array containing 'div' and 'nav'.
|
2013-09-12 20:48:09 +02:00
|
|
|
*/
|
2010-06-02 19:04:07 +02:00
|
|
|
$allowed_tags = apply_filters( 'wp_nav_menu_container_allowedtags', array( 'div', 'nav' ) );
|
2020-04-05 05:02:11 +02:00
|
|
|
|
|
|
|
if ( is_string( $args->container ) && in_array( $args->container, $allowed_tags, true ) ) {
|
2010-06-02 19:04:07 +02:00
|
|
|
$show_container = true;
|
2017-12-01 00:11:00 +01:00
|
|
|
$class = $args->container_class ? ' class="' . esc_attr( $args->container_class ) . '"' : ' class="menu-' . $menu->slug . '-container"';
|
|
|
|
$id = $args->container_id ? ' id="' . esc_attr( $args->container_id ) . '"' : '';
|
2020-07-06 22:44:02 +02:00
|
|
|
$aria_label = ( 'nav' === $args->container && $args->container_aria_label ) ? ' aria-label="' . esc_attr( $args->container_aria_label ) . '"' : '';
|
|
|
|
$nav_menu .= '<' . $args->container . $id . $class . $aria_label . '>';
|
2010-06-02 19:04:07 +02:00
|
|
|
}
|
2010-04-07 17:25:48 +02:00
|
|
|
}
|
2010-03-26 20:36:49 +01:00
|
|
|
|
2020-01-29 01:45:18 +01:00
|
|
|
// Set up the $menu_item variables.
|
2010-05-31 16:54:50 +02:00
|
|
|
_wp_menu_item_classes_by_context( $menu_items );
|
2010-05-25 15:30:10 +02:00
|
|
|
|
2019-07-03 01:42:58 +02:00
|
|
|
$sorted_menu_items = array();
|
|
|
|
$menu_items_with_children = array();
|
2013-09-24 18:01:09 +02:00
|
|
|
foreach ( (array) $menu_items as $menu_item ) {
|
2023-01-13 00:24:12 +01:00
|
|
|
/*
|
|
|
|
* Fix invalid `menu_item_parent`. See: https://core.trac.wordpress.org/ticket/56926.
|
2023-02-16 01:04:21 +01:00
|
|
|
* Compare as strings. Plugins may change the ID to a string.
|
2023-01-13 00:24:12 +01:00
|
|
|
*/
|
2023-02-16 01:04:21 +01:00
|
|
|
if ( (string) $menu_item->ID === (string) $menu_item->menu_item_parent ) {
|
2022-12-14 23:54:14 +01:00
|
|
|
$menu_item->menu_item_parent = 0;
|
|
|
|
}
|
|
|
|
|
2013-09-24 18:01:09 +02:00
|
|
|
$sorted_menu_items[ $menu_item->menu_order ] = $menu_item;
|
2017-12-01 00:11:00 +01:00
|
|
|
if ( $menu_item->menu_item_parent ) {
|
Menus: Prevent infinite loop in menus.
This modifies how the `menu-item-has-children` class is removed from bottom level menu items. Instead of removing the class within `wp_nav_menu()` a filter is applied to the `nav_menu_css_class` hook to remove the class as required.
Introduces `wp_nav_menu_remove_menu_item_has_children_class()` for removing the class.
Reverts source code changes in [54478,54801], the tests are retained.
Props davidbinda, SergeyBiryukov, mhkuu, JeffPaul, jmdodd, priethor, desrosj, hellofromTonya, azaozz, peterwilsoncc.
Fixes #56926.
See #28620.
Built from https://develop.svn.wordpress.org/trunk@54999
git-svn-id: http://core.svn.wordpress.org/trunk@54532 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2022-12-16 03:40:15 +01:00
|
|
|
$menu_items_with_children[ $menu_item->menu_item_parent ] = true;
|
2017-12-01 00:11:00 +01:00
|
|
|
}
|
2013-09-24 18:01:09 +02:00
|
|
|
}
|
|
|
|
|
2020-01-29 01:45:18 +01:00
|
|
|
// Add the menu-item-has-children class where applicable.
|
2013-09-24 18:01:09 +02:00
|
|
|
if ( $menu_items_with_children ) {
|
|
|
|
foreach ( $sorted_menu_items as &$menu_item ) {
|
Menus: Prevent infinite loop in menus.
This modifies how the `menu-item-has-children` class is removed from bottom level menu items. Instead of removing the class within `wp_nav_menu()` a filter is applied to the `nav_menu_css_class` hook to remove the class as required.
Introduces `wp_nav_menu_remove_menu_item_has_children_class()` for removing the class.
Reverts source code changes in [54478,54801], the tests are retained.
Props davidbinda, SergeyBiryukov, mhkuu, JeffPaul, jmdodd, priethor, desrosj, hellofromTonya, azaozz, peterwilsoncc.
Fixes #56926.
See #28620.
Built from https://develop.svn.wordpress.org/trunk@54999
git-svn-id: http://core.svn.wordpress.org/trunk@54532 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2022-12-16 03:40:15 +01:00
|
|
|
if ( isset( $menu_items_with_children[ $menu_item->ID ] ) ) {
|
2013-09-24 18:01:09 +02:00
|
|
|
$menu_item->classes[] = 'menu-item-has-children';
|
2017-12-01 00:11:00 +01:00
|
|
|
}
|
2013-09-24 18:01:09 +02:00
|
|
|
}
|
|
|
|
}
|
2010-05-26 04:42:15 +02:00
|
|
|
|
Menus: Prevent infinite loop in menus.
This modifies how the `menu-item-has-children` class is removed from bottom level menu items. Instead of removing the class within `wp_nav_menu()` a filter is applied to the `nav_menu_css_class` hook to remove the class as required.
Introduces `wp_nav_menu_remove_menu_item_has_children_class()` for removing the class.
Reverts source code changes in [54478,54801], the tests are retained.
Props davidbinda, SergeyBiryukov, mhkuu, JeffPaul, jmdodd, priethor, desrosj, hellofromTonya, azaozz, peterwilsoncc.
Fixes #56926.
See #28620.
Built from https://develop.svn.wordpress.org/trunk@54999
git-svn-id: http://core.svn.wordpress.org/trunk@54532 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2022-12-16 03:40:15 +01:00
|
|
|
unset( $menu_items, $menu_item );
|
2010-10-19 09:48:22 +02:00
|
|
|
|
2013-09-12 20:48:09 +02:00
|
|
|
/**
|
2016-05-22 20:28:27 +02:00
|
|
|
* Filters the sorted list of menu item objects before generating the menu's HTML.
|
2013-09-12 20:48:09 +02:00
|
|
|
*
|
|
|
|
* @since 3.1.0
|
|
|
|
*
|
2016-09-07 18:15:30 +02:00
|
|
|
* @param array $sorted_menu_items The menu items, sorted by each menu item's menu order.
|
|
|
|
* @param stdClass $args An object containing wp_nav_menu() arguments.
|
2013-09-12 20:48:09 +02:00
|
|
|
*/
|
2010-08-27 18:13:39 +02:00
|
|
|
$sorted_menu_items = apply_filters( 'wp_nav_menu_objects', $sorted_menu_items, $args );
|
2010-03-26 20:36:49 +01:00
|
|
|
|
2010-04-27 03:05:58 +02:00
|
|
|
$items .= walk_nav_menu_tree( $sorted_menu_items, $args->depth, $args );
|
2017-12-01 00:11:00 +01:00
|
|
|
unset( $sorted_menu_items );
|
2010-03-26 20:36:49 +01:00
|
|
|
|
2020-01-29 01:45:18 +01:00
|
|
|
// Attributes.
|
2010-06-02 19:04:07 +02:00
|
|
|
if ( ! empty( $args->menu_id ) ) {
|
2010-11-21 14:37:09 +01:00
|
|
|
$wrap_id = $args->menu_id;
|
2010-06-02 19:04:07 +02:00
|
|
|
} else {
|
2010-11-21 14:37:09 +01:00
|
|
|
$wrap_id = 'menu-' . $menu->slug;
|
2020-04-05 05:02:11 +02:00
|
|
|
|
|
|
|
while ( in_array( $wrap_id, $menu_id_slugs, true ) ) {
|
2017-12-01 00:11:00 +01:00
|
|
|
if ( preg_match( '#-(\d+)$#', $wrap_id, $matches ) ) {
|
|
|
|
$wrap_id = preg_replace( '#-(\d+)$#', '-' . ++$matches[1], $wrap_id );
|
|
|
|
} else {
|
2010-11-21 14:37:09 +01:00
|
|
|
$wrap_id = $wrap_id . '-1';
|
2017-12-01 00:11:00 +01:00
|
|
|
}
|
2010-06-02 19:04:07 +02:00
|
|
|
}
|
2010-05-28 18:32:57 +02:00
|
|
|
}
|
2010-11-21 14:37:09 +01:00
|
|
|
$menu_id_slugs[] = $wrap_id;
|
|
|
|
|
2010-11-21 14:32:02 +01:00
|
|
|
$wrap_class = $args->menu_class ? $args->menu_class : '';
|
2010-03-26 20:36:49 +01:00
|
|
|
|
2013-09-12 20:48:09 +02:00
|
|
|
/**
|
2016-05-22 20:28:27 +02:00
|
|
|
* Filters the HTML list content for navigation menus.
|
2013-09-12 20:48:09 +02:00
|
|
|
*
|
|
|
|
* @since 3.0.0
|
|
|
|
*
|
2014-02-20 06:08:13 +01:00
|
|
|
* @see wp_nav_menu()
|
|
|
|
*
|
2016-09-07 18:15:30 +02:00
|
|
|
* @param string $items The HTML list content for the menu items.
|
|
|
|
* @param stdClass $args An object containing wp_nav_menu() arguments.
|
2013-09-12 20:48:09 +02:00
|
|
|
*/
|
2010-05-13 05:50:26 +02:00
|
|
|
$items = apply_filters( 'wp_nav_menu_items', $items, $args );
|
2013-09-12 20:48:09 +02:00
|
|
|
/**
|
2016-05-22 20:28:27 +02:00
|
|
|
* Filters the HTML list content for a specific navigation menu.
|
2013-09-12 20:48:09 +02:00
|
|
|
*
|
|
|
|
* @since 3.0.0
|
|
|
|
*
|
2014-02-20 06:08:13 +01:00
|
|
|
* @see wp_nav_menu()
|
|
|
|
*
|
2016-09-07 18:15:30 +02:00
|
|
|
* @param string $items The HTML list content for the menu items.
|
|
|
|
* @param stdClass $args An object containing wp_nav_menu() arguments.
|
2013-09-12 20:48:09 +02:00
|
|
|
*/
|
2010-05-13 05:50:26 +02:00
|
|
|
$items = apply_filters( "wp_nav_menu_{$menu->slug}_items", $items, $args );
|
2010-11-21 14:37:09 +01:00
|
|
|
|
2013-07-10 07:14:43 +02:00
|
|
|
// Don't print any markup if there are no items at this point.
|
2017-12-01 00:11:00 +01:00
|
|
|
if ( empty( $items ) ) {
|
2013-07-10 07:14:43 +02:00
|
|
|
return false;
|
2017-12-01 00:11:00 +01:00
|
|
|
}
|
2013-07-10 07:14:43 +02:00
|
|
|
|
2010-11-21 14:37:09 +01:00
|
|
|
$nav_menu .= sprintf( $args->items_wrap, esc_attr( $wrap_id ), esc_attr( $wrap_class ), $items );
|
|
|
|
unset( $items );
|
2010-03-26 20:36:49 +01:00
|
|
|
|
2017-12-01 00:11:00 +01:00
|
|
|
if ( $show_container ) {
|
2010-06-02 19:04:07 +02:00
|
|
|
$nav_menu .= '</' . $args->container . '>';
|
2017-12-01 00:11:00 +01:00
|
|
|
}
|
2010-04-07 17:25:48 +02:00
|
|
|
|
2013-09-12 20:48:09 +02:00
|
|
|
/**
|
2016-05-22 20:28:27 +02:00
|
|
|
* Filters the HTML content for navigation menus.
|
2013-09-12 20:48:09 +02:00
|
|
|
*
|
|
|
|
* @since 3.0.0
|
|
|
|
*
|
2014-02-20 06:08:13 +01:00
|
|
|
* @see wp_nav_menu()
|
|
|
|
*
|
2016-09-07 18:15:30 +02:00
|
|
|
* @param string $nav_menu The HTML content for the navigation menu.
|
|
|
|
* @param stdClass $args An object containing wp_nav_menu() arguments.
|
2013-09-12 20:48:09 +02:00
|
|
|
*/
|
2010-04-07 17:25:48 +02:00
|
|
|
$nav_menu = apply_filters( 'wp_nav_menu', $nav_menu, $args );
|
|
|
|
|
2017-12-01 00:11:00 +01:00
|
|
|
if ( $args->echo ) {
|
2010-04-07 17:25:48 +02:00
|
|
|
echo $nav_menu;
|
2017-12-01 00:11:00 +01:00
|
|
|
} else {
|
2010-04-07 17:25:48 +02:00
|
|
|
return $nav_menu;
|
2017-12-01 00:11:00 +01:00
|
|
|
}
|
2010-02-24 09:06:36 +01:00
|
|
|
}
|
|
|
|
|
2010-05-25 15:30:10 +02:00
|
|
|
/**
|
2020-07-08 15:15:03 +02:00
|
|
|
* Adds the class property classes for the current context, if applicable.
|
2010-05-25 15:30:10 +02:00
|
|
|
*
|
|
|
|
* @access private
|
2013-12-24 19:57:12 +01:00
|
|
|
* @since 3.0.0
|
2010-05-25 15:30:10 +02:00
|
|
|
*
|
2019-08-04 03:59:56 +02:00
|
|
|
* @global WP_Query $wp_query WordPress Query object.
|
2019-08-04 03:19:56 +02:00
|
|
|
* @global WP_Rewrite $wp_rewrite WordPress rewrite component.
|
2015-05-27 00:10:25 +02:00
|
|
|
*
|
2010-05-25 15:30:10 +02:00
|
|
|
* @param array $menu_items The current menu item objects to which to add the class property information.
|
|
|
|
*/
|
2010-05-26 18:25:29 +02:00
|
|
|
function _wp_menu_item_classes_by_context( &$menu_items ) {
|
2013-01-21 15:44:30 +01:00
|
|
|
global $wp_query, $wp_rewrite;
|
2010-05-25 15:30:10 +02:00
|
|
|
|
2017-12-01 00:11:00 +01:00
|
|
|
$queried_object = $wp_query->get_queried_object();
|
2010-05-25 15:30:10 +02:00
|
|
|
$queried_object_id = (int) $wp_query->queried_object_id;
|
|
|
|
|
2017-12-01 00:11:00 +01:00
|
|
|
$active_object = '';
|
|
|
|
$active_ancestor_item_ids = array();
|
|
|
|
$active_parent_item_ids = array();
|
|
|
|
$active_parent_object_ids = array();
|
2010-06-11 18:00:29 +02:00
|
|
|
$possible_taxonomy_ancestors = array();
|
2017-12-01 00:11:00 +01:00
|
|
|
$possible_object_parents = array();
|
|
|
|
$home_page_id = (int) get_option( 'page_for_posts' );
|
2010-05-26 04:42:15 +02:00
|
|
|
|
2010-05-25 15:30:10 +02:00
|
|
|
if ( $wp_query->is_singular && ! empty( $queried_object->post_type ) && ! is_post_type_hierarchical( $queried_object->post_type ) ) {
|
|
|
|
foreach ( (array) get_object_taxonomies( $queried_object->post_type ) as $taxonomy ) {
|
|
|
|
if ( is_taxonomy_hierarchical( $taxonomy ) ) {
|
2010-06-11 18:00:29 +02:00
|
|
|
$term_hierarchy = _get_term_hierarchy( $taxonomy );
|
2017-12-01 00:11:00 +01:00
|
|
|
$terms = wp_get_object_terms( $queried_object_id, $taxonomy, array( 'fields' => 'ids' ) );
|
2010-06-11 18:00:29 +02:00
|
|
|
if ( is_array( $terms ) ) {
|
2010-05-25 15:30:10 +02:00
|
|
|
$possible_object_parents = array_merge( $possible_object_parents, $terms );
|
2017-12-01 00:11:00 +01:00
|
|
|
$term_to_ancestor = array();
|
2010-06-11 18:00:29 +02:00
|
|
|
foreach ( (array) $term_hierarchy as $anc => $descs ) {
|
2017-12-01 00:11:00 +01:00
|
|
|
foreach ( (array) $descs as $desc ) {
|
2010-06-11 18:00:29 +02:00
|
|
|
$term_to_ancestor[ $desc ] = $anc;
|
2017-12-01 00:11:00 +01:00
|
|
|
}
|
2010-06-11 18:00:29 +02:00
|
|
|
}
|
2010-06-11 22:19:35 +02:00
|
|
|
|
2010-06-11 18:00:29 +02:00
|
|
|
foreach ( $terms as $desc ) {
|
|
|
|
do {
|
|
|
|
$possible_taxonomy_ancestors[ $taxonomy ][] = $desc;
|
|
|
|
if ( isset( $term_to_ancestor[ $desc ] ) ) {
|
|
|
|
$_desc = $term_to_ancestor[ $desc ];
|
|
|
|
unset( $term_to_ancestor[ $desc ] );
|
|
|
|
$desc = $_desc;
|
|
|
|
} else {
|
|
|
|
$desc = 0;
|
|
|
|
}
|
|
|
|
} while ( ! empty( $desc ) );
|
|
|
|
}
|
|
|
|
}
|
2010-05-25 15:30:10 +02:00
|
|
|
}
|
|
|
|
}
|
2010-06-11 18:00:29 +02:00
|
|
|
} elseif ( ! empty( $queried_object->taxonomy ) && is_taxonomy_hierarchical( $queried_object->taxonomy ) ) {
|
2017-12-01 00:11:00 +01:00
|
|
|
$term_hierarchy = _get_term_hierarchy( $queried_object->taxonomy );
|
2010-06-11 18:00:29 +02:00
|
|
|
$term_to_ancestor = array();
|
|
|
|
foreach ( (array) $term_hierarchy as $anc => $descs ) {
|
2017-12-01 00:11:00 +01:00
|
|
|
foreach ( (array) $descs as $desc ) {
|
2010-06-11 18:00:29 +02:00
|
|
|
$term_to_ancestor[ $desc ] = $anc;
|
2017-12-01 00:11:00 +01:00
|
|
|
}
|
2010-06-11 18:00:29 +02:00
|
|
|
}
|
|
|
|
$desc = $queried_object->term_id;
|
|
|
|
do {
|
|
|
|
$possible_taxonomy_ancestors[ $queried_object->taxonomy ][] = $desc;
|
|
|
|
if ( isset( $term_to_ancestor[ $desc ] ) ) {
|
|
|
|
$_desc = $term_to_ancestor[ $desc ];
|
|
|
|
unset( $term_to_ancestor[ $desc ] );
|
|
|
|
$desc = $_desc;
|
|
|
|
} else {
|
|
|
|
$desc = 0;
|
|
|
|
}
|
|
|
|
} while ( ! empty( $desc ) );
|
2010-05-25 15:30:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
$possible_object_parents = array_filter( $possible_object_parents );
|
|
|
|
|
2019-03-21 20:48:50 +01:00
|
|
|
$front_page_url = home_url();
|
|
|
|
$front_page_id = (int) get_option( 'page_on_front' );
|
|
|
|
$privacy_policy_page_id = (int) get_option( 'wp_page_for_privacy_policy' );
|
2010-12-06 04:54:57 +01:00
|
|
|
|
2010-05-25 20:54:42 +02:00
|
|
|
foreach ( (array) $menu_items as $key => $menu_item ) {
|
2010-10-21 21:55:28 +02:00
|
|
|
|
2017-12-01 00:11:00 +01:00
|
|
|
$menu_items[ $key ]->current = false;
|
2010-10-21 21:55:28 +02:00
|
|
|
|
2017-12-01 00:11:00 +01:00
|
|
|
$classes = (array) $menu_item->classes;
|
2010-06-02 15:48:26 +02:00
|
|
|
$classes[] = 'menu-item';
|
|
|
|
$classes[] = 'menu-item-type-' . $menu_item->type;
|
2010-10-30 00:49:42 +02:00
|
|
|
$classes[] = 'menu-item-object-' . $menu_item->object;
|
2010-06-02 15:48:26 +02:00
|
|
|
|
2016-10-26 03:58:30 +02:00
|
|
|
// This menu item is set as the 'Front Page'.
|
|
|
|
if ( 'post_type' === $menu_item->type && $front_page_id === (int) $menu_item->object_id ) {
|
|
|
|
$classes[] = 'menu-item-home';
|
|
|
|
}
|
|
|
|
|
2019-03-21 20:48:50 +01:00
|
|
|
// This menu item is set as the 'Privacy Policy Page'.
|
|
|
|
if ( 'post_type' === $menu_item->type && $privacy_policy_page_id === (int) $menu_item->object_id ) {
|
|
|
|
$classes[] = 'menu-item-privacy-policy';
|
|
|
|
}
|
|
|
|
|
2020-01-29 01:45:18 +01:00
|
|
|
// If the menu item corresponds to a taxonomy term for the currently queried non-hierarchical post object.
|
2020-05-16 20:42:12 +02:00
|
|
|
if ( $wp_query->is_singular && 'taxonomy' === $menu_item->type
|
2020-04-09 17:43:10 +02:00
|
|
|
&& in_array( (int) $menu_item->object_id, $possible_object_parents, true )
|
|
|
|
) {
|
2010-05-25 15:30:10 +02:00
|
|
|
$active_parent_object_ids[] = (int) $menu_item->object_id;
|
2017-12-01 00:11:00 +01:00
|
|
|
$active_parent_item_ids[] = (int) $menu_item->db_id;
|
|
|
|
$active_object = $queried_object->post_type;
|
2010-05-26 04:42:15 +02:00
|
|
|
|
2020-01-29 01:45:18 +01:00
|
|
|
// If the menu item corresponds to the currently queried post or taxonomy object.
|
2010-05-25 15:30:10 +02:00
|
|
|
} elseif (
|
2020-04-05 05:02:11 +02:00
|
|
|
$menu_item->object_id == $queried_object_id
|
|
|
|
&& (
|
2020-05-16 20:42:12 +02:00
|
|
|
( ! empty( $home_page_id ) && 'post_type' === $menu_item->type
|
|
|
|
&& $wp_query->is_home && $home_page_id == $menu_item->object_id )
|
|
|
|
|| ( 'post_type' === $menu_item->type && $wp_query->is_singular )
|
|
|
|
|| ( 'taxonomy' === $menu_item->type
|
2020-04-09 17:43:10 +02:00
|
|
|
&& ( $wp_query->is_category || $wp_query->is_tag || $wp_query->is_tax )
|
|
|
|
&& $queried_object->taxonomy == $menu_item->object )
|
2010-05-26 04:42:15 +02:00
|
|
|
)
|
2010-05-25 15:30:10 +02:00
|
|
|
) {
|
2017-12-01 00:11:00 +01:00
|
|
|
$classes[] = 'current-menu-item';
|
|
|
|
$menu_items[ $key ]->current = true;
|
|
|
|
$_anc_id = (int) $menu_item->db_id;
|
2010-06-14 23:19:55 +02:00
|
|
|
|
2017-12-01 00:11:00 +01:00
|
|
|
while (
|
2020-04-05 05:02:11 +02:00
|
|
|
( $_anc_id = (int) get_post_meta( $_anc_id, '_menu_item_menu_item_parent', true ) )
|
|
|
|
&& ! in_array( $_anc_id, $active_ancestor_item_ids, true )
|
2010-06-14 23:19:55 +02:00
|
|
|
) {
|
|
|
|
$active_ancestor_item_ids[] = $_anc_id;
|
|
|
|
}
|
|
|
|
|
2020-05-16 20:42:12 +02:00
|
|
|
if ( 'post_type' === $menu_item->type && 'page' === $menu_item->object ) {
|
2020-01-29 01:45:18 +01:00
|
|
|
// Back compat classes for pages to match wp_page_menu().
|
2010-06-02 15:48:26 +02:00
|
|
|
$classes[] = 'page_item';
|
|
|
|
$classes[] = 'page-item-' . $menu_item->object_id;
|
|
|
|
$classes[] = 'current_page_item';
|
|
|
|
}
|
2016-10-24 02:32:32 +02:00
|
|
|
|
2017-12-01 00:11:00 +01:00
|
|
|
$active_parent_item_ids[] = (int) $menu_item->menu_item_parent;
|
2010-05-25 15:30:10 +02:00
|
|
|
$active_parent_object_ids[] = (int) $menu_item->post_parent;
|
2017-12-01 00:11:00 +01:00
|
|
|
$active_object = $menu_item->object;
|
2010-05-25 15:30:10 +02:00
|
|
|
|
2020-01-29 01:45:18 +01:00
|
|
|
// If the menu item corresponds to the currently queried post type archive.
|
2015-10-24 19:46:25 +02:00
|
|
|
} elseif (
|
2020-05-16 20:42:12 +02:00
|
|
|
'post_type_archive' === $menu_item->type
|
2020-04-05 05:02:11 +02:00
|
|
|
&& is_post_type_archive( array( $menu_item->object ) )
|
2015-10-24 19:46:25 +02:00
|
|
|
) {
|
2017-12-01 00:11:00 +01:00
|
|
|
$classes[] = 'current-menu-item';
|
|
|
|
$menu_items[ $key ]->current = true;
|
|
|
|
$_anc_id = (int) $menu_item->db_id;
|
2017-07-05 23:31:44 +02:00
|
|
|
|
2017-12-01 00:11:00 +01:00
|
|
|
while (
|
2020-04-05 05:02:11 +02:00
|
|
|
( $_anc_id = (int) get_post_meta( $_anc_id, '_menu_item_menu_item_parent', true ) )
|
|
|
|
&& ! in_array( $_anc_id, $active_ancestor_item_ids, true )
|
2017-07-05 23:31:44 +02:00
|
|
|
) {
|
|
|
|
$active_ancestor_item_ids[] = $_anc_id;
|
|
|
|
}
|
|
|
|
|
|
|
|
$active_parent_item_ids[] = (int) $menu_item->menu_item_parent;
|
|
|
|
|
2020-01-29 01:45:18 +01:00
|
|
|
// If the menu item corresponds to the currently requested URL.
|
2020-05-16 20:42:12 +02:00
|
|
|
} elseif ( 'custom' === $menu_item->object && isset( $_SERVER['HTTP_HOST'] ) ) {
|
2011-09-22 04:25:57 +02:00
|
|
|
$_root_relative_current = untrailingslashit( $_SERVER['REQUEST_URI'] );
|
2017-05-12 22:35:43 +02:00
|
|
|
|
2020-01-29 01:45:18 +01:00
|
|
|
// If it's the customize page then it will strip the query var off the URL before entering the comparison block.
|
2017-05-12 22:35:43 +02:00
|
|
|
if ( is_customize_preview() ) {
|
|
|
|
$_root_relative_current = strtok( untrailingslashit( $_SERVER['REQUEST_URI'] ), '?' );
|
|
|
|
}
|
2018-02-24 14:44:31 +01:00
|
|
|
|
2017-12-01 00:11:00 +01:00
|
|
|
$current_url = set_url_scheme( 'http://' . $_SERVER['HTTP_HOST'] . $_root_relative_current );
|
|
|
|
$raw_item_url = strpos( $menu_item->url, '#' ) ? substr( $menu_item->url, 0, strpos( $menu_item->url, '#' ) ) : $menu_item->url;
|
|
|
|
$item_url = set_url_scheme( untrailingslashit( $raw_item_url ) );
|
2013-01-18 14:44:22 +01:00
|
|
|
$_indexless_current = untrailingslashit( preg_replace( '/' . preg_quote( $wp_rewrite->index, '/' ) . '$/', '', $current_url ) );
|
2010-10-21 21:55:28 +02:00
|
|
|
|
2018-02-24 14:44:31 +01:00
|
|
|
$matches = array(
|
2018-08-17 03:51:36 +02:00
|
|
|
$current_url,
|
|
|
|
urldecode( $current_url ),
|
|
|
|
$_indexless_current,
|
|
|
|
urldecode( $_indexless_current ),
|
|
|
|
$_root_relative_current,
|
|
|
|
urldecode( $_root_relative_current ),
|
2018-02-24 14:44:31 +01:00
|
|
|
);
|
|
|
|
|
2020-04-05 05:02:11 +02:00
|
|
|
if ( $raw_item_url && in_array( $item_url, $matches, true ) ) {
|
2017-12-01 00:11:00 +01:00
|
|
|
$classes[] = 'current-menu-item';
|
|
|
|
$menu_items[ $key ]->current = true;
|
|
|
|
$_anc_id = (int) $menu_item->db_id;
|
2010-06-14 23:19:55 +02:00
|
|
|
|
2017-12-01 00:11:00 +01:00
|
|
|
while (
|
2020-04-05 05:02:11 +02:00
|
|
|
( $_anc_id = (int) get_post_meta( $_anc_id, '_menu_item_menu_item_parent', true ) )
|
|
|
|
&& ! in_array( $_anc_id, $active_ancestor_item_ids, true )
|
2010-06-14 23:19:55 +02:00
|
|
|
) {
|
|
|
|
$active_ancestor_item_ids[] = $_anc_id;
|
|
|
|
}
|
|
|
|
|
2020-04-05 05:02:11 +02:00
|
|
|
if ( in_array( home_url(), array( untrailingslashit( $current_url ), untrailingslashit( $_indexless_current ) ), true ) ) {
|
2020-01-29 01:45:18 +01:00
|
|
|
// Back compat for home link to match wp_page_menu().
|
2010-06-02 15:48:26 +02:00
|
|
|
$classes[] = 'current_page_item';
|
|
|
|
}
|
2017-12-01 00:11:00 +01:00
|
|
|
$active_parent_item_ids[] = (int) $menu_item->menu_item_parent;
|
2010-05-25 15:30:10 +02:00
|
|
|
$active_parent_object_ids[] = (int) $menu_item->post_parent;
|
2017-12-01 00:11:00 +01:00
|
|
|
$active_object = $menu_item->object;
|
2010-12-06 04:54:57 +01:00
|
|
|
|
2020-01-29 01:45:18 +01:00
|
|
|
// Give front page item the 'current-menu-item' class when extra query arguments are involved.
|
2010-12-06 04:54:57 +01:00
|
|
|
} elseif ( $item_url == $front_page_url && is_front_page() ) {
|
|
|
|
$classes[] = 'current-menu-item';
|
2010-05-25 15:30:10 +02:00
|
|
|
}
|
2010-10-21 21:55:28 +02:00
|
|
|
|
2017-12-01 00:11:00 +01:00
|
|
|
if ( untrailingslashit( $item_url ) == home_url() ) {
|
2010-06-23 00:05:43 +02:00
|
|
|
$classes[] = 'menu-item-home';
|
2017-12-01 00:11:00 +01:00
|
|
|
}
|
2010-05-25 15:30:10 +02:00
|
|
|
}
|
2010-06-02 22:04:07 +02:00
|
|
|
|
2020-01-29 01:45:18 +01:00
|
|
|
// Back-compat with wp_page_menu(): add "current_page_parent" to static home page link for any non-page query.
|
2020-05-16 20:42:12 +02:00
|
|
|
if ( ! empty( $home_page_id ) && 'post_type' === $menu_item->type
|
|
|
|
&& empty( $wp_query->is_page ) && $home_page_id == $menu_item->object_id
|
|
|
|
) {
|
2010-06-02 15:48:26 +02:00
|
|
|
$classes[] = 'current_page_parent';
|
2017-12-01 00:11:00 +01:00
|
|
|
}
|
2010-06-02 15:48:26 +02:00
|
|
|
|
2017-12-01 00:11:00 +01:00
|
|
|
$menu_items[ $key ]->classes = array_unique( $classes );
|
2010-05-25 15:30:10 +02:00
|
|
|
}
|
2010-06-14 23:19:55 +02:00
|
|
|
$active_ancestor_item_ids = array_filter( array_unique( $active_ancestor_item_ids ) );
|
2017-12-01 00:11:00 +01:00
|
|
|
$active_parent_item_ids = array_filter( array_unique( $active_parent_item_ids ) );
|
2010-05-25 15:30:10 +02:00
|
|
|
$active_parent_object_ids = array_filter( array_unique( $active_parent_object_ids ) );
|
|
|
|
|
2020-01-29 01:45:18 +01:00
|
|
|
// Set parent's class.
|
2010-05-25 20:54:42 +02:00
|
|
|
foreach ( (array) $menu_items as $key => $parent_item ) {
|
2017-12-01 00:11:00 +01:00
|
|
|
$classes = (array) $parent_item->classes;
|
|
|
|
$menu_items[ $key ]->current_item_ancestor = false;
|
|
|
|
$menu_items[ $key ]->current_item_parent = false;
|
2010-10-21 21:55:28 +02:00
|
|
|
|
2010-05-26 04:42:15 +02:00
|
|
|
if (
|
2020-04-05 05:02:11 +02:00
|
|
|
isset( $parent_item->type )
|
|
|
|
&& (
|
2020-01-29 01:45:18 +01:00
|
|
|
// Ancestral post object.
|
2010-06-11 18:00:29 +02:00
|
|
|
(
|
2020-05-16 20:42:12 +02:00
|
|
|
'post_type' === $parent_item->type
|
2020-04-05 05:02:11 +02:00
|
|
|
&& ! empty( $queried_object->post_type )
|
|
|
|
&& is_post_type_hierarchical( $queried_object->post_type )
|
2020-04-09 17:43:10 +02:00
|
|
|
&& in_array( (int) $parent_item->object_id, $queried_object->ancestors, true )
|
2020-04-05 05:02:11 +02:00
|
|
|
&& $parent_item->object != $queried_object->ID
|
2010-06-11 18:00:29 +02:00
|
|
|
) ||
|
|
|
|
|
2020-01-29 01:45:18 +01:00
|
|
|
// Ancestral term.
|
2010-06-11 18:00:29 +02:00
|
|
|
(
|
2020-05-16 20:42:12 +02:00
|
|
|
'taxonomy' === $parent_item->type
|
2020-04-05 05:02:11 +02:00
|
|
|
&& isset( $possible_taxonomy_ancestors[ $parent_item->object ] )
|
2020-04-09 17:43:10 +02:00
|
|
|
&& in_array( (int) $parent_item->object_id, $possible_taxonomy_ancestors[ $parent_item->object ], true )
|
2020-04-05 05:02:11 +02:00
|
|
|
&& (
|
2010-12-22 18:40:51 +01:00
|
|
|
! isset( $queried_object->term_id ) ||
|
|
|
|
$parent_item->object_id != $queried_object->term_id
|
|
|
|
)
|
2010-06-11 18:00:29 +02:00
|
|
|
)
|
|
|
|
)
|
2010-06-02 15:48:26 +02:00
|
|
|
) {
|
2020-04-05 05:02:11 +02:00
|
|
|
if ( ! empty( $queried_object->taxonomy ) ) {
|
|
|
|
$classes[] = 'current-' . $queried_object->taxonomy . '-ancestor';
|
|
|
|
} else {
|
|
|
|
$classes[] = 'current-' . $queried_object->post_type . '-ancestor';
|
|
|
|
}
|
2010-06-02 15:48:26 +02:00
|
|
|
}
|
2010-06-11 18:00:29 +02:00
|
|
|
|
2020-04-05 05:02:11 +02:00
|
|
|
if ( in_array( (int) $parent_item->db_id, $active_ancestor_item_ids, true ) ) {
|
|
|
|
$classes[] = 'current-menu-ancestor';
|
|
|
|
|
2017-12-01 00:11:00 +01:00
|
|
|
$menu_items[ $key ]->current_item_ancestor = true;
|
2010-06-14 23:19:55 +02:00
|
|
|
}
|
2020-04-05 05:02:11 +02:00
|
|
|
if ( in_array( (int) $parent_item->db_id, $active_parent_item_ids, true ) ) {
|
|
|
|
$classes[] = 'current-menu-parent';
|
|
|
|
|
2017-12-01 00:11:00 +01:00
|
|
|
$menu_items[ $key ]->current_item_parent = true;
|
2010-08-27 18:13:39 +02:00
|
|
|
}
|
2020-04-05 05:02:11 +02:00
|
|
|
if ( in_array( (int) $parent_item->object_id, $active_parent_object_ids, true ) ) {
|
2010-06-02 15:48:26 +02:00
|
|
|
$classes[] = 'current-' . $active_object . '-parent';
|
2017-12-01 00:11:00 +01:00
|
|
|
}
|
2010-06-02 22:04:07 +02:00
|
|
|
|
2020-05-16 20:42:12 +02:00
|
|
|
if ( 'post_type' === $parent_item->type && 'page' === $parent_item->object ) {
|
2020-01-29 01:45:18 +01:00
|
|
|
// Back compat classes for pages to match wp_page_menu().
|
2020-04-05 05:02:11 +02:00
|
|
|
if ( in_array( 'current-menu-parent', $classes, true ) ) {
|
2010-06-02 15:48:26 +02:00
|
|
|
$classes[] = 'current_page_parent';
|
2017-12-01 00:11:00 +01:00
|
|
|
}
|
2020-04-05 05:02:11 +02:00
|
|
|
if ( in_array( 'current-menu-ancestor', $classes, true ) ) {
|
2010-06-02 15:48:26 +02:00
|
|
|
$classes[] = 'current_page_ancestor';
|
2017-12-01 00:11:00 +01:00
|
|
|
}
|
2010-06-02 15:48:26 +02:00
|
|
|
}
|
2010-06-02 22:04:07 +02:00
|
|
|
|
2017-12-01 00:11:00 +01:00
|
|
|
$menu_items[ $key ]->classes = array_unique( $classes );
|
2010-05-25 15:30:10 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-02-28 21:00:49 +01:00
|
|
|
/**
|
2020-07-08 15:15:03 +02:00
|
|
|
* Retrieves the HTML list content for nav menu items.
|
2010-02-28 21:00:49 +01:00
|
|
|
*
|
2010-04-27 03:05:58 +02:00
|
|
|
* @uses Walker_Nav_Menu to create HTML list content.
|
2010-07-13 23:30:59 +02:00
|
|
|
* @since 3.0.0
|
2015-05-27 00:10:25 +02:00
|
|
|
*
|
2016-09-07 18:15:30 +02:00
|
|
|
* @param array $items The menu items, sorted by each menu item's menu order.
|
|
|
|
* @param int $depth Depth of the item in reference to parents.
|
2022-02-07 15:01:05 +01:00
|
|
|
* @param stdClass $args An object containing wp_nav_menu() arguments.
|
2016-09-07 18:15:30 +02:00
|
|
|
* @return string The HTML list content for the menu items.
|
2010-03-15 17:26:46 +01:00
|
|
|
*/
|
2022-02-07 15:01:05 +01:00
|
|
|
function walk_nav_menu_tree( $items, $depth, $args ) {
|
2022-11-29 16:51:14 +01:00
|
|
|
$walker = ( empty( $args->walker ) ) ? new Walker_Nav_Menu() : $args->walker;
|
2010-03-26 20:36:49 +01:00
|
|
|
|
2022-02-07 15:01:05 +01:00
|
|
|
return $walker->walk( $items, $depth, $args );
|
2010-02-24 09:06:36 +01:00
|
|
|
}
|
2010-04-27 03:05:58 +02:00
|
|
|
|
2010-07-13 23:30:59 +02:00
|
|
|
/**
|
|
|
|
* Prevents a menu item ID from being used more than once.
|
|
|
|
*
|
|
|
|
* @since 3.0.1
|
|
|
|
* @access private
|
2015-05-27 00:10:25 +02:00
|
|
|
*
|
|
|
|
* @param string $id
|
|
|
|
* @param object $item
|
|
|
|
* @return string
|
2010-07-13 23:30:59 +02:00
|
|
|
*/
|
|
|
|
function _nav_menu_item_id_use_once( $id, $item ) {
|
|
|
|
static $_used_ids = array();
|
2020-04-05 05:02:11 +02:00
|
|
|
|
|
|
|
if ( in_array( $item->ID, $_used_ids, true ) ) {
|
2010-07-13 23:30:59 +02:00
|
|
|
return '';
|
2015-05-27 00:10:25 +02:00
|
|
|
}
|
2020-04-05 05:02:11 +02:00
|
|
|
|
2010-07-13 23:30:59 +02:00
|
|
|
$_used_ids[] = $item->ID;
|
2020-04-05 05:02:11 +02:00
|
|
|
|
2010-07-13 23:30:59 +02:00
|
|
|
return $id;
|
|
|
|
}
|
Menus: Prevent infinite loop in menus.
This modifies how the `menu-item-has-children` class is removed from bottom level menu items. Instead of removing the class within `wp_nav_menu()` a filter is applied to the `nav_menu_css_class` hook to remove the class as required.
Introduces `wp_nav_menu_remove_menu_item_has_children_class()` for removing the class.
Reverts source code changes in [54478,54801], the tests are retained.
Props davidbinda, SergeyBiryukov, mhkuu, JeffPaul, jmdodd, priethor, desrosj, hellofromTonya, azaozz, peterwilsoncc.
Fixes #56926.
See #28620.
Built from https://develop.svn.wordpress.org/trunk@54999
git-svn-id: http://core.svn.wordpress.org/trunk@54532 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2022-12-16 03:40:15 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Remove the `menu-item-has-children` class from bottom level menu items.
|
|
|
|
*
|
2022-12-19 01:01:14 +01:00
|
|
|
* This runs on the {@see 'nav_menu_css_class'} filter. The $args and $depth
|
|
|
|
* parameters were added after the filter was originally introduced in
|
|
|
|
* WordPress 3.0.0 so this needs to allow for cases in which the filter is
|
|
|
|
* called without them.
|
|
|
|
*
|
2024-01-01 15:17:16 +01:00
|
|
|
* @see https://core.trac.wordpress.org/ticket/56926
|
2022-12-19 01:01:14 +01:00
|
|
|
*
|
2023-02-15 23:56:20 +01:00
|
|
|
* @since 6.2.0
|
Menus: Prevent infinite loop in menus.
This modifies how the `menu-item-has-children` class is removed from bottom level menu items. Instead of removing the class within `wp_nav_menu()` a filter is applied to the `nav_menu_css_class` hook to remove the class as required.
Introduces `wp_nav_menu_remove_menu_item_has_children_class()` for removing the class.
Reverts source code changes in [54478,54801], the tests are retained.
Props davidbinda, SergeyBiryukov, mhkuu, JeffPaul, jmdodd, priethor, desrosj, hellofromTonya, azaozz, peterwilsoncc.
Fixes #56926.
See #28620.
Built from https://develop.svn.wordpress.org/trunk@54999
git-svn-id: http://core.svn.wordpress.org/trunk@54532 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2022-12-16 03:40:15 +01:00
|
|
|
*
|
2022-12-19 01:01:14 +01:00
|
|
|
* @param string[] $classes Array of the CSS classes that are applied to the menu item's `<li>` element.
|
|
|
|
* @param WP_Post $menu_item The current menu item object.
|
|
|
|
* @param stdClass|false $args An object of wp_nav_menu() arguments. Default false ($args unspecified when filter is called).
|
|
|
|
* @param int|false $depth Depth of menu item. Default false ($depth unspecified when filter is called).
|
Menus: Prevent infinite loop in menus.
This modifies how the `menu-item-has-children` class is removed from bottom level menu items. Instead of removing the class within `wp_nav_menu()` a filter is applied to the `nav_menu_css_class` hook to remove the class as required.
Introduces `wp_nav_menu_remove_menu_item_has_children_class()` for removing the class.
Reverts source code changes in [54478,54801], the tests are retained.
Props davidbinda, SergeyBiryukov, mhkuu, JeffPaul, jmdodd, priethor, desrosj, hellofromTonya, azaozz, peterwilsoncc.
Fixes #56926.
See #28620.
Built from https://develop.svn.wordpress.org/trunk@54999
git-svn-id: http://core.svn.wordpress.org/trunk@54532 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2022-12-16 03:40:15 +01:00
|
|
|
* @return string[] Modified nav menu classes.
|
|
|
|
*/
|
2022-12-19 01:01:14 +01:00
|
|
|
function wp_nav_menu_remove_menu_item_has_children_class( $classes, $menu_item, $args = false, $depth = false ) {
|
|
|
|
/*
|
|
|
|
* Account for the filter being called without the $args or $depth parameters.
|
|
|
|
*
|
|
|
|
* This occurs when a theme uses a custom walker calling the `nav_menu_css_class`
|
|
|
|
* filter using the legacy formats prior to the introduction of the $args and
|
|
|
|
* $depth parameters.
|
|
|
|
*
|
|
|
|
* As both of these parameters are required for this function to determine
|
|
|
|
* both the current and maximum depth of the menu tree, the function does not
|
|
|
|
* attempt to remove the `menu-item-has-children` class if these parameters
|
|
|
|
* are not set.
|
|
|
|
*/
|
|
|
|
if ( false === $depth || false === $args ) {
|
|
|
|
return $classes;
|
|
|
|
}
|
|
|
|
|
Menus: Prevent infinite loop in menus.
This modifies how the `menu-item-has-children` class is removed from bottom level menu items. Instead of removing the class within `wp_nav_menu()` a filter is applied to the `nav_menu_css_class` hook to remove the class as required.
Introduces `wp_nav_menu_remove_menu_item_has_children_class()` for removing the class.
Reverts source code changes in [54478,54801], the tests are retained.
Props davidbinda, SergeyBiryukov, mhkuu, JeffPaul, jmdodd, priethor, desrosj, hellofromTonya, azaozz, peterwilsoncc.
Fixes #56926.
See #28620.
Built from https://develop.svn.wordpress.org/trunk@54999
git-svn-id: http://core.svn.wordpress.org/trunk@54532 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2022-12-16 03:40:15 +01:00
|
|
|
// Max-depth is 1-based.
|
|
|
|
$max_depth = isset( $args->depth ) ? (int) $args->depth : 0;
|
|
|
|
// Depth is 0-based so needs to be increased by one.
|
|
|
|
$depth = $depth + 1;
|
|
|
|
|
|
|
|
// Complete menu tree is displayed.
|
|
|
|
if ( 0 === $max_depth ) {
|
|
|
|
return $classes;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Remove the `menu-item-has-children` class from bottom level menu items.
|
|
|
|
* -1 is used to display all menu items in one level so the class should
|
|
|
|
* be removed from all menu items.
|
|
|
|
*/
|
|
|
|
if ( -1 === $max_depth || $depth >= $max_depth ) {
|
|
|
|
$classes = array_diff( $classes, array( 'menu-item-has-children' ) );
|
|
|
|
}
|
|
|
|
|
|
|
|
return $classes;
|
|
|
|
}
|