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