author | ymh <ymh.work@gmail.com> |
Fri, 05 Sep 2025 18:52:52 +0200 | |
changeset 22 | 8c2e4d02f4ef |
parent 21 | 48c4eec2b7e6 |
permissions | -rw-r--r-- |
0 | 1 |
<?php |
2 |
/** |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3 |
* Nav Menu API: Template functions |
0 | 4 |
* |
5 |
* @package WordPress |
|
6 |
* @subpackage Nav_Menus |
|
7 |
* @since 3.0.0 |
|
8 |
*/ |
|
9 |
||
22
8c2e4d02f4ef
Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents:
21
diff
changeset
|
10 |
// Don't load directly. |
8c2e4d02f4ef
Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents:
21
diff
changeset
|
11 |
if ( ! defined( 'ABSPATH' ) ) { |
8c2e4d02f4ef
Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents:
21
diff
changeset
|
12 |
die( '-1' ); |
8c2e4d02f4ef
Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents:
21
diff
changeset
|
13 |
} |
8c2e4d02f4ef
Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents:
21
diff
changeset
|
14 |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
15 |
/** Walker_Nav_Menu class */ |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
16 |
require_once ABSPATH . WPINC . '/class-walker-nav-menu.php'; |
0 | 17 |
|
18 |
/** |
|
19 |
* Displays a navigation menu. |
|
20 |
* |
|
21 |
* @since 3.0.0 |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
22 |
* @since 4.7.0 Added the `item_spacing` argument. |
16 | 23 |
* @since 5.5.0 Added the `container_aria_label` argument. |
0 | 24 |
* |
5 | 25 |
* @param array $args { |
26 |
* Optional. Array of nav menu arguments. |
|
27 |
* |
|
16 | 28 |
* @type int|string|WP_Term $menu Desired menu. Accepts a menu ID, slug, name, or object. |
29 |
* Default empty. |
|
30 |
* @type string $menu_class CSS class to use for the ul element which forms the menu. |
|
31 |
* Default 'menu'. |
|
32 |
* @type string $menu_id The ID that is applied to the ul element which forms the menu. |
|
33 |
* Default is the menu slug, incremented. |
|
34 |
* @type string $container Whether to wrap the ul, and what to wrap it with. |
|
35 |
* Default 'div'. |
|
36 |
* @type string $container_class Class that is applied to the container. |
|
37 |
* Default 'menu-{menu slug}-container'. |
|
38 |
* @type string $container_id The ID that is applied to the container. Default empty. |
|
39 |
* @type string $container_aria_label The aria-label attribute that is applied to the container |
|
40 |
* when it's a nav element. Default empty. |
|
18 | 41 |
* @type callable|false $fallback_cb If the menu doesn't exist, a callback function will fire. |
16 | 42 |
* Default is 'wp_page_menu'. Set to false for no fallback. |
43 |
* @type string $before Text before the link markup. Default empty. |
|
44 |
* @type string $after Text after the link markup. Default empty. |
|
45 |
* @type string $link_before Text before the link text. Default empty. |
|
46 |
* @type string $link_after Text after the link text. Default empty. |
|
47 |
* @type bool $echo Whether to echo the menu or return it. Default true. |
|
48 |
* @type int $depth How many levels of the hierarchy are to be included. |
|
49 |
* 0 means all. Default 0. |
|
50 |
* Default 0. |
|
51 |
* @type object $walker Instance of a custom walker class. Default empty. |
|
52 |
* @type string $theme_location Theme location to be used. Must be registered with |
|
53 |
* register_nav_menu() in order to be selectable by the user. |
|
54 |
* @type string $items_wrap How the list items should be wrapped. Uses printf() format with |
|
55 |
* numbered placeholders. Default is a ul with an id and class. |
|
56 |
* @type string $item_spacing Whether to preserve whitespace within the menu's HTML. |
|
57 |
* Accepts 'preserve' or 'discard'. Default 'preserve'. |
|
5 | 58 |
* } |
16 | 59 |
* @return void|string|false Void if 'echo' argument is true, menu output if 'echo' is false. |
60 |
* False if there are no items or no menu was found. |
|
0 | 61 |
*/ |
62 |
function wp_nav_menu( $args = array() ) { |
|
63 |
static $menu_id_slugs = array(); |
|
64 |
||
9 | 65 |
$defaults = array( |
16 | 66 |
'menu' => '', |
67 |
'container' => 'div', |
|
68 |
'container_class' => '', |
|
69 |
'container_id' => '', |
|
70 |
'container_aria_label' => '', |
|
71 |
'menu_class' => 'menu', |
|
72 |
'menu_id' => '', |
|
73 |
'echo' => true, |
|
74 |
'fallback_cb' => 'wp_page_menu', |
|
75 |
'before' => '', |
|
76 |
'after' => '', |
|
77 |
'link_before' => '', |
|
78 |
'link_after' => '', |
|
79 |
'items_wrap' => '<ul id="%1$s" class="%2$s">%3$s</ul>', |
|
80 |
'item_spacing' => 'preserve', |
|
81 |
'depth' => 0, |
|
82 |
'walker' => '', |
|
83 |
'theme_location' => '', |
|
9 | 84 |
); |
0 | 85 |
|
86 |
$args = wp_parse_args( $args, $defaults ); |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
87 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
88 |
if ( ! in_array( $args['item_spacing'], array( 'preserve', 'discard' ), true ) ) { |
16 | 89 |
// Invalid value, fall back to default. |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
90 |
$args['item_spacing'] = $defaults['item_spacing']; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
91 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
92 |
|
0 | 93 |
/** |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
94 |
* Filters the arguments used to display a navigation menu. |
0 | 95 |
* |
96 |
* @since 3.0.0 |
|
97 |
* |
|
5 | 98 |
* @see wp_nav_menu() |
99 |
* |
|
100 |
* @param array $args Array of wp_nav_menu() arguments. |
|
0 | 101 |
*/ |
102 |
$args = apply_filters( 'wp_nav_menu_args', $args ); |
|
103 |
$args = (object) $args; |
|
104 |
||
5 | 105 |
/** |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
106 |
* Filters whether to short-circuit the wp_nav_menu() output. |
5 | 107 |
* |
16 | 108 |
* Returning a non-null value from the filter will short-circuit wp_nav_menu(), |
109 |
* echoing that value if $args->echo is true, returning that value otherwise. |
|
5 | 110 |
* |
111 |
* @since 3.9.0 |
|
112 |
* |
|
113 |
* @see wp_nav_menu() |
|
114 |
* |
|
115 |
* @param string|null $output Nav menu output to short-circuit with. Default null. |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
116 |
* @param stdClass $args An object containing wp_nav_menu() arguments. |
5 | 117 |
*/ |
118 |
$nav_menu = apply_filters( 'pre_wp_nav_menu', null, $args ); |
|
119 |
||
120 |
if ( null !== $nav_menu ) { |
|
121 |
if ( $args->echo ) { |
|
122 |
echo $nav_menu; |
|
123 |
return; |
|
124 |
} |
|
125 |
||
126 |
return $nav_menu; |
|
127 |
} |
|
128 |
||
16 | 129 |
// Get the nav menu based on the requested menu. |
0 | 130 |
$menu = wp_get_nav_menu_object( $args->menu ); |
131 |
||
16 | 132 |
// Get the nav menu based on the theme_location. |
133 |
$locations = get_nav_menu_locations(); |
|
134 |
if ( ! $menu && $args->theme_location && $locations && isset( $locations[ $args->theme_location ] ) ) { |
|
0 | 135 |
$menu = wp_get_nav_menu_object( $locations[ $args->theme_location ] ); |
9 | 136 |
} |
0 | 137 |
|
16 | 138 |
// Get the first menu that has items if we still can't find a menu. |
9 | 139 |
if ( ! $menu && ! $args->theme_location ) { |
0 | 140 |
$menus = wp_get_nav_menus(); |
141 |
foreach ( $menus as $menu_maybe ) { |
|
16 | 142 |
$menu_items = wp_get_nav_menu_items( $menu_maybe->term_id, array( 'update_post_term_cache' => false ) ); |
143 |
if ( $menu_items ) { |
|
0 | 144 |
$menu = $menu_maybe; |
145 |
break; |
|
146 |
} |
|
147 |
} |
|
148 |
} |
|
149 |
||
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
150 |
if ( empty( $args->menu ) ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
151 |
$args->menu = $menu; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
152 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
153 |
|
0 | 154 |
// If the menu exists, get its items. |
9 | 155 |
if ( $menu && ! is_wp_error( $menu ) && ! isset( $menu_items ) ) { |
0 | 156 |
$menu_items = wp_get_nav_menu_items( $menu->term_id, array( 'update_post_term_cache' => false ) ); |
9 | 157 |
} |
0 | 158 |
|
159 |
/* |
|
160 |
* If no menu was found: |
|
161 |
* - Fall back (if one was specified), or bail. |
|
162 |
* |
|
163 |
* If no menu items were found: |
|
164 |
* - Fall back, but only if no theme location was specified. |
|
165 |
* - Otherwise, bail. |
|
166 |
*/ |
|
9 | 167 |
if ( ( ! $menu || is_wp_error( $menu ) || ( isset( $menu_items ) && empty( $menu_items ) && ! $args->theme_location ) ) |
168 |
&& isset( $args->fallback_cb ) && $args->fallback_cb && is_callable( $args->fallback_cb ) ) { |
|
0 | 169 |
return call_user_func( $args->fallback_cb, (array) $args ); |
9 | 170 |
} |
0 | 171 |
|
9 | 172 |
if ( ! $menu || is_wp_error( $menu ) ) { |
0 | 173 |
return false; |
9 | 174 |
} |
0 | 175 |
|
16 | 176 |
$nav_menu = ''; |
177 |
$items = ''; |
|
0 | 178 |
|
179 |
$show_container = false; |
|
180 |
if ( $args->container ) { |
|
181 |
/** |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
182 |
* Filters the list of HTML tags that are valid for use as menu containers. |
0 | 183 |
* |
184 |
* @since 3.0.0 |
|
185 |
* |
|
16 | 186 |
* @param string[] $tags The acceptable HTML tags for use as menu containers. |
187 |
* Default is array containing 'div' and 'nav'. |
|
0 | 188 |
*/ |
189 |
$allowed_tags = apply_filters( 'wp_nav_menu_container_allowedtags', array( 'div', 'nav' ) ); |
|
16 | 190 |
|
191 |
if ( is_string( $args->container ) && in_array( $args->container, $allowed_tags, true ) ) { |
|
0 | 192 |
$show_container = true; |
9 | 193 |
$class = $args->container_class ? ' class="' . esc_attr( $args->container_class ) . '"' : ' class="menu-' . $menu->slug . '-container"'; |
194 |
$id = $args->container_id ? ' id="' . esc_attr( $args->container_id ) . '"' : ''; |
|
16 | 195 |
$aria_label = ( 'nav' === $args->container && $args->container_aria_label ) ? ' aria-label="' . esc_attr( $args->container_aria_label ) . '"' : ''; |
196 |
$nav_menu .= '<' . $args->container . $id . $class . $aria_label . '>'; |
|
0 | 197 |
} |
198 |
} |
|
199 |
||
16 | 200 |
// Set up the $menu_item variables. |
0 | 201 |
_wp_menu_item_classes_by_context( $menu_items ); |
202 |
||
16 | 203 |
$sorted_menu_items = array(); |
204 |
$menu_items_with_children = array(); |
|
0 | 205 |
foreach ( (array) $menu_items as $menu_item ) { |
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
206 |
/* |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
207 |
* Fix invalid `menu_item_parent`. See: https://core.trac.wordpress.org/ticket/56926. |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
208 |
* Compare as strings. Plugins may change the ID to a string. |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
209 |
*/ |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
210 |
if ( (string) $menu_item->ID === (string) $menu_item->menu_item_parent ) { |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
211 |
$menu_item->menu_item_parent = 0; |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
212 |
} |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
213 |
|
0 | 214 |
$sorted_menu_items[ $menu_item->menu_order ] = $menu_item; |
9 | 215 |
if ( $menu_item->menu_item_parent ) { |
0 | 216 |
$menu_items_with_children[ $menu_item->menu_item_parent ] = true; |
9 | 217 |
} |
0 | 218 |
} |
219 |
||
16 | 220 |
// Add the menu-item-has-children class where applicable. |
0 | 221 |
if ( $menu_items_with_children ) { |
222 |
foreach ( $sorted_menu_items as &$menu_item ) { |
|
9 | 223 |
if ( isset( $menu_items_with_children[ $menu_item->ID ] ) ) { |
0 | 224 |
$menu_item->classes[] = 'menu-item-has-children'; |
9 | 225 |
} |
0 | 226 |
} |
227 |
} |
|
228 |
||
229 |
unset( $menu_items, $menu_item ); |
|
230 |
||
231 |
/** |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
232 |
* Filters the sorted list of menu item objects before generating the menu's HTML. |
0 | 233 |
* |
234 |
* @since 3.1.0 |
|
235 |
* |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
236 |
* @param array $sorted_menu_items The menu items, sorted by each menu item's menu order. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
237 |
* @param stdClass $args An object containing wp_nav_menu() arguments. |
0 | 238 |
*/ |
239 |
$sorted_menu_items = apply_filters( 'wp_nav_menu_objects', $sorted_menu_items, $args ); |
|
240 |
||
241 |
$items .= walk_nav_menu_tree( $sorted_menu_items, $args->depth, $args ); |
|
9 | 242 |
unset( $sorted_menu_items ); |
0 | 243 |
|
16 | 244 |
// Attributes. |
0 | 245 |
if ( ! empty( $args->menu_id ) ) { |
246 |
$wrap_id = $args->menu_id; |
|
247 |
} else { |
|
248 |
$wrap_id = 'menu-' . $menu->slug; |
|
16 | 249 |
|
250 |
while ( in_array( $wrap_id, $menu_id_slugs, true ) ) { |
|
9 | 251 |
if ( preg_match( '#-(\d+)$#', $wrap_id, $matches ) ) { |
252 |
$wrap_id = preg_replace( '#-(\d+)$#', '-' . ++$matches[1], $wrap_id ); |
|
253 |
} else { |
|
0 | 254 |
$wrap_id = $wrap_id . '-1'; |
9 | 255 |
} |
0 | 256 |
} |
257 |
} |
|
258 |
$menu_id_slugs[] = $wrap_id; |
|
259 |
||
260 |
$wrap_class = $args->menu_class ? $args->menu_class : ''; |
|
261 |
||
262 |
/** |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
263 |
* Filters the HTML list content for navigation menus. |
0 | 264 |
* |
265 |
* @since 3.0.0 |
|
266 |
* |
|
5 | 267 |
* @see wp_nav_menu() |
268 |
* |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
269 |
* @param string $items The HTML list content for the menu items. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
270 |
* @param stdClass $args An object containing wp_nav_menu() arguments. |
0 | 271 |
*/ |
272 |
$items = apply_filters( 'wp_nav_menu_items', $items, $args ); |
|
273 |
/** |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
274 |
* Filters the HTML list content for a specific navigation menu. |
0 | 275 |
* |
276 |
* @since 3.0.0 |
|
277 |
* |
|
5 | 278 |
* @see wp_nav_menu() |
279 |
* |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
280 |
* @param string $items The HTML list content for the menu items. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
281 |
* @param stdClass $args An object containing wp_nav_menu() arguments. |
0 | 282 |
*/ |
283 |
$items = apply_filters( "wp_nav_menu_{$menu->slug}_items", $items, $args ); |
|
284 |
||
285 |
// Don't print any markup if there are no items at this point. |
|
9 | 286 |
if ( empty( $items ) ) { |
0 | 287 |
return false; |
9 | 288 |
} |
0 | 289 |
|
290 |
$nav_menu .= sprintf( $args->items_wrap, esc_attr( $wrap_id ), esc_attr( $wrap_class ), $items ); |
|
291 |
unset( $items ); |
|
292 |
||
9 | 293 |
if ( $show_container ) { |
0 | 294 |
$nav_menu .= '</' . $args->container . '>'; |
9 | 295 |
} |
0 | 296 |
|
297 |
/** |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
298 |
* Filters the HTML content for navigation menus. |
0 | 299 |
* |
300 |
* @since 3.0.0 |
|
301 |
* |
|
5 | 302 |
* @see wp_nav_menu() |
303 |
* |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
304 |
* @param string $nav_menu The HTML content for the navigation menu. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
305 |
* @param stdClass $args An object containing wp_nav_menu() arguments. |
0 | 306 |
*/ |
307 |
$nav_menu = apply_filters( 'wp_nav_menu', $nav_menu, $args ); |
|
308 |
||
9 | 309 |
if ( $args->echo ) { |
0 | 310 |
echo $nav_menu; |
9 | 311 |
} else { |
0 | 312 |
return $nav_menu; |
9 | 313 |
} |
0 | 314 |
} |
315 |
||
316 |
/** |
|
16 | 317 |
* Adds the class property classes for the current context, if applicable. |
0 | 318 |
* |
319 |
* @access private |
|
5 | 320 |
* @since 3.0.0 |
0 | 321 |
* |
16 | 322 |
* @global WP_Query $wp_query WordPress Query object. |
323 |
* @global WP_Rewrite $wp_rewrite WordPress rewrite component. |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
324 |
* |
0 | 325 |
* @param array $menu_items The current menu item objects to which to add the class property information. |
326 |
*/ |
|
327 |
function _wp_menu_item_classes_by_context( &$menu_items ) { |
|
328 |
global $wp_query, $wp_rewrite; |
|
329 |
||
9 | 330 |
$queried_object = $wp_query->get_queried_object(); |
0 | 331 |
$queried_object_id = (int) $wp_query->queried_object_id; |
332 |
||
9 | 333 |
$active_object = ''; |
334 |
$active_ancestor_item_ids = array(); |
|
335 |
$active_parent_item_ids = array(); |
|
336 |
$active_parent_object_ids = array(); |
|
0 | 337 |
$possible_taxonomy_ancestors = array(); |
9 | 338 |
$possible_object_parents = array(); |
339 |
$home_page_id = (int) get_option( 'page_for_posts' ); |
|
0 | 340 |
|
341 |
if ( $wp_query->is_singular && ! empty( $queried_object->post_type ) && ! is_post_type_hierarchical( $queried_object->post_type ) ) { |
|
342 |
foreach ( (array) get_object_taxonomies( $queried_object->post_type ) as $taxonomy ) { |
|
343 |
if ( is_taxonomy_hierarchical( $taxonomy ) ) { |
|
344 |
$term_hierarchy = _get_term_hierarchy( $taxonomy ); |
|
9 | 345 |
$terms = wp_get_object_terms( $queried_object_id, $taxonomy, array( 'fields' => 'ids' ) ); |
0 | 346 |
if ( is_array( $terms ) ) { |
347 |
$possible_object_parents = array_merge( $possible_object_parents, $terms ); |
|
9 | 348 |
$term_to_ancestor = array(); |
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
349 |
foreach ( (array) $term_hierarchy as $ancestor => $descendents ) { |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
350 |
foreach ( (array) $descendents as $desc ) { |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
351 |
$term_to_ancestor[ $desc ] = $ancestor; |
9 | 352 |
} |
0 | 353 |
} |
354 |
||
355 |
foreach ( $terms as $desc ) { |
|
356 |
do { |
|
357 |
$possible_taxonomy_ancestors[ $taxonomy ][] = $desc; |
|
358 |
if ( isset( $term_to_ancestor[ $desc ] ) ) { |
|
359 |
$_desc = $term_to_ancestor[ $desc ]; |
|
360 |
unset( $term_to_ancestor[ $desc ] ); |
|
361 |
$desc = $_desc; |
|
362 |
} else { |
|
363 |
$desc = 0; |
|
364 |
} |
|
365 |
} while ( ! empty( $desc ) ); |
|
366 |
} |
|
367 |
} |
|
368 |
} |
|
369 |
} |
|
370 |
} elseif ( ! empty( $queried_object->taxonomy ) && is_taxonomy_hierarchical( $queried_object->taxonomy ) ) { |
|
9 | 371 |
$term_hierarchy = _get_term_hierarchy( $queried_object->taxonomy ); |
0 | 372 |
$term_to_ancestor = array(); |
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
373 |
foreach ( (array) $term_hierarchy as $ancestor => $descendents ) { |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
374 |
foreach ( (array) $descendents as $desc ) { |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
375 |
$term_to_ancestor[ $desc ] = $ancestor; |
9 | 376 |
} |
0 | 377 |
} |
378 |
$desc = $queried_object->term_id; |
|
379 |
do { |
|
380 |
$possible_taxonomy_ancestors[ $queried_object->taxonomy ][] = $desc; |
|
381 |
if ( isset( $term_to_ancestor[ $desc ] ) ) { |
|
382 |
$_desc = $term_to_ancestor[ $desc ]; |
|
383 |
unset( $term_to_ancestor[ $desc ] ); |
|
384 |
$desc = $_desc; |
|
385 |
} else { |
|
386 |
$desc = 0; |
|
387 |
} |
|
388 |
} while ( ! empty( $desc ) ); |
|
389 |
} |
|
390 |
||
391 |
$possible_object_parents = array_filter( $possible_object_parents ); |
|
392 |
||
9 | 393 |
$front_page_url = home_url(); |
394 |
$front_page_id = (int) get_option( 'page_on_front' ); |
|
395 |
$privacy_policy_page_id = (int) get_option( 'wp_page_for_privacy_policy' ); |
|
0 | 396 |
|
397 |
foreach ( (array) $menu_items as $key => $menu_item ) { |
|
398 |
||
9 | 399 |
$menu_items[ $key ]->current = false; |
0 | 400 |
|
9 | 401 |
$classes = (array) $menu_item->classes; |
0 | 402 |
$classes[] = 'menu-item'; |
403 |
$classes[] = 'menu-item-type-' . $menu_item->type; |
|
404 |
$classes[] = 'menu-item-object-' . $menu_item->object; |
|
405 |
||
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
406 |
// This menu item is set as the 'Front Page'. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
407 |
if ( 'post_type' === $menu_item->type && $front_page_id === (int) $menu_item->object_id ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
408 |
$classes[] = 'menu-item-home'; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
409 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
410 |
|
9 | 411 |
// This menu item is set as the 'Privacy Policy Page'. |
412 |
if ( 'post_type' === $menu_item->type && $privacy_policy_page_id === (int) $menu_item->object_id ) { |
|
413 |
$classes[] = 'menu-item-privacy-policy'; |
|
414 |
} |
|
415 |
||
16 | 416 |
// If the menu item corresponds to a taxonomy term for the currently queried non-hierarchical post object. |
417 |
if ( $wp_query->is_singular && 'taxonomy' === $menu_item->type |
|
418 |
&& in_array( (int) $menu_item->object_id, $possible_object_parents, true ) |
|
419 |
) { |
|
0 | 420 |
$active_parent_object_ids[] = (int) $menu_item->object_id; |
9 | 421 |
$active_parent_item_ids[] = (int) $menu_item->db_id; |
422 |
$active_object = $queried_object->post_type; |
|
0 | 423 |
|
16 | 424 |
// If the menu item corresponds to the currently queried post or taxonomy object. |
0 | 425 |
} elseif ( |
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
426 |
(int) $menu_item->object_id === $queried_object_id |
16 | 427 |
&& ( |
428 |
( ! empty( $home_page_id ) && 'post_type' === $menu_item->type |
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
429 |
&& $wp_query->is_home && $home_page_id === (int) $menu_item->object_id ) |
16 | 430 |
|| ( 'post_type' === $menu_item->type && $wp_query->is_singular ) |
431 |
|| ( 'taxonomy' === $menu_item->type |
|
432 |
&& ( $wp_query->is_category || $wp_query->is_tag || $wp_query->is_tax ) |
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
433 |
&& $queried_object->taxonomy === $menu_item->object ) |
0 | 434 |
) |
435 |
) { |
|
9 | 436 |
$classes[] = 'current-menu-item'; |
437 |
$menu_items[ $key ]->current = true; |
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
438 |
$ancestor_id = (int) $menu_item->db_id; |
0 | 439 |
|
9 | 440 |
while ( |
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
441 |
( $ancestor_id = (int) get_post_meta( $ancestor_id, '_menu_item_menu_item_parent', true ) ) |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
442 |
&& ! in_array( $ancestor_id, $active_ancestor_item_ids, true ) |
0 | 443 |
) { |
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
444 |
$active_ancestor_item_ids[] = $ancestor_id; |
0 | 445 |
} |
446 |
||
16 | 447 |
if ( 'post_type' === $menu_item->type && 'page' === $menu_item->object ) { |
448 |
// Back compat classes for pages to match wp_page_menu(). |
|
0 | 449 |
$classes[] = 'page_item'; |
450 |
$classes[] = 'page-item-' . $menu_item->object_id; |
|
451 |
$classes[] = 'current_page_item'; |
|
452 |
} |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
453 |
|
9 | 454 |
$active_parent_item_ids[] = (int) $menu_item->menu_item_parent; |
0 | 455 |
$active_parent_object_ids[] = (int) $menu_item->post_parent; |
9 | 456 |
$active_object = $menu_item->object; |
0 | 457 |
|
16 | 458 |
// If the menu item corresponds to the currently queried post type archive. |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
459 |
} elseif ( |
16 | 460 |
'post_type_archive' === $menu_item->type |
461 |
&& is_post_type_archive( array( $menu_item->object ) ) |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
462 |
) { |
9 | 463 |
$classes[] = 'current-menu-item'; |
464 |
$menu_items[ $key ]->current = true; |
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
465 |
$ancestor_id = (int) $menu_item->db_id; |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
466 |
|
9 | 467 |
while ( |
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
468 |
( $ancestor_id = (int) get_post_meta( $ancestor_id, '_menu_item_menu_item_parent', true ) ) |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
469 |
&& ! in_array( $ancestor_id, $active_ancestor_item_ids, true ) |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
470 |
) { |
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
471 |
$active_ancestor_item_ids[] = $ancestor_id; |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
472 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
473 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
474 |
$active_parent_item_ids[] = (int) $menu_item->menu_item_parent; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
475 |
|
16 | 476 |
// If the menu item corresponds to the currently requested URL. |
477 |
} elseif ( 'custom' === $menu_item->object && isset( $_SERVER['HTTP_HOST'] ) ) { |
|
0 | 478 |
$_root_relative_current = untrailingslashit( $_SERVER['REQUEST_URI'] ); |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
479 |
|
16 | 480 |
// If it's the customize page then it will strip the query var off the URL before entering the comparison block. |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
481 |
if ( is_customize_preview() ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
482 |
$_root_relative_current = strtok( untrailingslashit( $_SERVER['REQUEST_URI'] ), '?' ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
483 |
} |
9 | 484 |
|
485 |
$current_url = set_url_scheme( 'http://' . $_SERVER['HTTP_HOST'] . $_root_relative_current ); |
|
486 |
$raw_item_url = strpos( $menu_item->url, '#' ) ? substr( $menu_item->url, 0, strpos( $menu_item->url, '#' ) ) : $menu_item->url; |
|
487 |
$item_url = set_url_scheme( untrailingslashit( $raw_item_url ) ); |
|
0 | 488 |
$_indexless_current = untrailingslashit( preg_replace( '/' . preg_quote( $wp_rewrite->index, '/' ) . '$/', '', $current_url ) ); |
489 |
||
9 | 490 |
$matches = array( |
491 |
$current_url, |
|
492 |
urldecode( $current_url ), |
|
493 |
$_indexless_current, |
|
494 |
urldecode( $_indexless_current ), |
|
495 |
$_root_relative_current, |
|
496 |
urldecode( $_root_relative_current ), |
|
497 |
); |
|
0 | 498 |
|
16 | 499 |
if ( $raw_item_url && in_array( $item_url, $matches, true ) ) { |
9 | 500 |
$classes[] = 'current-menu-item'; |
501 |
$menu_items[ $key ]->current = true; |
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
502 |
$ancestor_id = (int) $menu_item->db_id; |
9 | 503 |
|
504 |
while ( |
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
505 |
( $ancestor_id = (int) get_post_meta( $ancestor_id, '_menu_item_menu_item_parent', true ) ) |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
506 |
&& ! in_array( $ancestor_id, $active_ancestor_item_ids, true ) |
0 | 507 |
) { |
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
508 |
$active_ancestor_item_ids[] = $ancestor_id; |
0 | 509 |
} |
510 |
||
16 | 511 |
if ( in_array( home_url(), array( untrailingslashit( $current_url ), untrailingslashit( $_indexless_current ) ), true ) ) { |
512 |
// Back compat for home link to match wp_page_menu(). |
|
0 | 513 |
$classes[] = 'current_page_item'; |
514 |
} |
|
9 | 515 |
$active_parent_item_ids[] = (int) $menu_item->menu_item_parent; |
0 | 516 |
$active_parent_object_ids[] = (int) $menu_item->post_parent; |
9 | 517 |
$active_object = $menu_item->object; |
0 | 518 |
|
16 | 519 |
// Give front page item the 'current-menu-item' class when extra query arguments are involved. |
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
520 |
} elseif ( $item_url === $front_page_url && is_front_page() ) { |
0 | 521 |
$classes[] = 'current-menu-item'; |
522 |
} |
|
523 |
||
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
524 |
if ( untrailingslashit( $item_url ) === home_url() ) { |
0 | 525 |
$classes[] = 'menu-item-home'; |
9 | 526 |
} |
0 | 527 |
} |
528 |
||
16 | 529 |
// Back-compat with wp_page_menu(): add "current_page_parent" to static home page link for any non-page query. |
530 |
if ( ! empty( $home_page_id ) && 'post_type' === $menu_item->type |
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
531 |
&& empty( $wp_query->is_page ) && $home_page_id === (int) $menu_item->object_id |
16 | 532 |
) { |
0 | 533 |
$classes[] = 'current_page_parent'; |
9 | 534 |
} |
0 | 535 |
|
9 | 536 |
$menu_items[ $key ]->classes = array_unique( $classes ); |
0 | 537 |
} |
538 |
$active_ancestor_item_ids = array_filter( array_unique( $active_ancestor_item_ids ) ); |
|
9 | 539 |
$active_parent_item_ids = array_filter( array_unique( $active_parent_item_ids ) ); |
0 | 540 |
$active_parent_object_ids = array_filter( array_unique( $active_parent_object_ids ) ); |
541 |
||
16 | 542 |
// Set parent's class. |
0 | 543 |
foreach ( (array) $menu_items as $key => $parent_item ) { |
9 | 544 |
$classes = (array) $parent_item->classes; |
545 |
$menu_items[ $key ]->current_item_ancestor = false; |
|
546 |
$menu_items[ $key ]->current_item_parent = false; |
|
0 | 547 |
|
548 |
if ( |
|
16 | 549 |
isset( $parent_item->type ) |
550 |
&& ( |
|
551 |
// Ancestral post object. |
|
0 | 552 |
( |
16 | 553 |
'post_type' === $parent_item->type |
554 |
&& ! empty( $queried_object->post_type ) |
|
555 |
&& is_post_type_hierarchical( $queried_object->post_type ) |
|
556 |
&& in_array( (int) $parent_item->object_id, $queried_object->ancestors, true ) |
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
557 |
&& (int) $parent_item->object_id !== $queried_object->ID |
0 | 558 |
) || |
559 |
||
16 | 560 |
// Ancestral term. |
0 | 561 |
( |
16 | 562 |
'taxonomy' === $parent_item->type |
563 |
&& isset( $possible_taxonomy_ancestors[ $parent_item->object ] ) |
|
564 |
&& in_array( (int) $parent_item->object_id, $possible_taxonomy_ancestors[ $parent_item->object ], true ) |
|
565 |
&& ( |
|
0 | 566 |
! isset( $queried_object->term_id ) || |
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
567 |
(int) $parent_item->object_id !== $queried_object->term_id |
0 | 568 |
) |
569 |
) |
|
570 |
) |
|
571 |
) { |
|
16 | 572 |
if ( ! empty( $queried_object->taxonomy ) ) { |
573 |
$classes[] = 'current-' . $queried_object->taxonomy . '-ancestor'; |
|
574 |
} else { |
|
575 |
$classes[] = 'current-' . $queried_object->post_type . '-ancestor'; |
|
576 |
} |
|
0 | 577 |
} |
578 |
||
16 | 579 |
if ( in_array( (int) $parent_item->db_id, $active_ancestor_item_ids, true ) ) { |
580 |
$classes[] = 'current-menu-ancestor'; |
|
581 |
||
9 | 582 |
$menu_items[ $key ]->current_item_ancestor = true; |
0 | 583 |
} |
16 | 584 |
if ( in_array( (int) $parent_item->db_id, $active_parent_item_ids, true ) ) { |
585 |
$classes[] = 'current-menu-parent'; |
|
586 |
||
9 | 587 |
$menu_items[ $key ]->current_item_parent = true; |
0 | 588 |
} |
16 | 589 |
if ( in_array( (int) $parent_item->object_id, $active_parent_object_ids, true ) ) { |
0 | 590 |
$classes[] = 'current-' . $active_object . '-parent'; |
9 | 591 |
} |
0 | 592 |
|
16 | 593 |
if ( 'post_type' === $parent_item->type && 'page' === $parent_item->object ) { |
594 |
// Back compat classes for pages to match wp_page_menu(). |
|
595 |
if ( in_array( 'current-menu-parent', $classes, true ) ) { |
|
0 | 596 |
$classes[] = 'current_page_parent'; |
9 | 597 |
} |
16 | 598 |
if ( in_array( 'current-menu-ancestor', $classes, true ) ) { |
0 | 599 |
$classes[] = 'current_page_ancestor'; |
9 | 600 |
} |
0 | 601 |
} |
602 |
||
9 | 603 |
$menu_items[ $key ]->classes = array_unique( $classes ); |
0 | 604 |
} |
605 |
} |
|
606 |
||
607 |
/** |
|
16 | 608 |
* Retrieves the HTML list content for nav menu items. |
0 | 609 |
* |
610 |
* @uses Walker_Nav_Menu to create HTML list content. |
|
611 |
* @since 3.0.0 |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
612 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
613 |
* @param array $items The menu items, sorted by each menu item's menu order. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
614 |
* @param int $depth Depth of the item in reference to parents. |
19 | 615 |
* @param stdClass $args An object containing wp_nav_menu() arguments. |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
616 |
* @return string The HTML list content for the menu items. |
0 | 617 |
*/ |
19 | 618 |
function walk_nav_menu_tree( $items, $depth, $args ) { |
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
619 |
$walker = ( empty( $args->walker ) ) ? new Walker_Nav_Menu() : $args->walker; |
0 | 620 |
|
19 | 621 |
return $walker->walk( $items, $depth, $args ); |
0 | 622 |
} |
623 |
||
624 |
/** |
|
625 |
* Prevents a menu item ID from being used more than once. |
|
626 |
* |
|
627 |
* @since 3.0.1 |
|
628 |
* @access private |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
629 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
630 |
* @param string $id |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
631 |
* @param object $item |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
632 |
* @return string |
0 | 633 |
*/ |
634 |
function _nav_menu_item_id_use_once( $id, $item ) { |
|
635 |
static $_used_ids = array(); |
|
16 | 636 |
|
637 |
if ( in_array( $item->ID, $_used_ids, true ) ) { |
|
0 | 638 |
return ''; |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
639 |
} |
16 | 640 |
|
0 | 641 |
$_used_ids[] = $item->ID; |
16 | 642 |
|
0 | 643 |
return $id; |
644 |
} |
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
645 |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
646 |
/** |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
647 |
* Remove the `menu-item-has-children` class from bottom level menu items. |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
648 |
* |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
649 |
* This runs on the {@see 'nav_menu_css_class'} filter. The $args and $depth |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
650 |
* parameters were added after the filter was originally introduced in |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
651 |
* WordPress 3.0.0 so this needs to allow for cases in which the filter is |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
652 |
* called without them. |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
653 |
* |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
654 |
* @see https://core.trac.wordpress.org/ticket/56926 |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
655 |
* |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
656 |
* @since 6.2.0 |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
657 |
* |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
658 |
* @param string[] $classes Array of the CSS classes that are applied to the menu item's `<li>` element. |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
659 |
* @param WP_Post $menu_item The current menu item object. |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
660 |
* @param stdClass|false $args An object of wp_nav_menu() arguments. Default false ($args unspecified when filter is called). |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
661 |
* @param int|false $depth Depth of menu item. Default false ($depth unspecified when filter is called). |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
662 |
* @return string[] Modified nav menu classes. |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
663 |
*/ |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
664 |
function wp_nav_menu_remove_menu_item_has_children_class( $classes, $menu_item, $args = false, $depth = false ) { |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
665 |
/* |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
666 |
* Account for the filter being called without the $args or $depth parameters. |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
667 |
* |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
668 |
* This occurs when a theme uses a custom walker calling the `nav_menu_css_class` |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
669 |
* filter using the legacy formats prior to the introduction of the $args and |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
670 |
* $depth parameters. |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
671 |
* |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
672 |
* As both of these parameters are required for this function to determine |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
673 |
* both the current and maximum depth of the menu tree, the function does not |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
674 |
* attempt to remove the `menu-item-has-children` class if these parameters |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
675 |
* are not set. |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
676 |
*/ |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
677 |
if ( false === $depth || false === $args ) { |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
678 |
return $classes; |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
679 |
} |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
680 |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
681 |
// Max-depth is 1-based. |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
682 |
$max_depth = isset( $args->depth ) ? (int) $args->depth : 0; |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
683 |
// Depth is 0-based so needs to be increased by one. |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
684 |
$depth = $depth + 1; |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
685 |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
686 |
// Complete menu tree is displayed. |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
687 |
if ( 0 === $max_depth ) { |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
688 |
return $classes; |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
689 |
} |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
690 |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
691 |
/* |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
692 |
* Remove the `menu-item-has-children` class from bottom level menu items. |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
693 |
* -1 is used to display all menu items in one level so the class should |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
694 |
* be removed from all menu items. |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
695 |
*/ |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
696 |
if ( -1 === $max_depth || $depth >= $max_depth ) { |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
697 |
$classes = array_diff( $classes, array( 'menu-item-has-children' ) ); |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
698 |
} |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
699 |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
700 |
return $classes; |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
701 |
} |