1 | <?php |
---|
2 | |
---|
3 | /** |
---|
4 | * Display menu. |
---|
5 | * |
---|
6 | * @access private |
---|
7 | * @since 2.7.0 |
---|
8 | * |
---|
9 | * @param array $menu |
---|
10 | * @param array $submenu |
---|
11 | * @param bool $submenu_as_parent |
---|
12 | */ |
---|
13 | function _wp_menu_output( $menu, $submenu, $submenu_as_parent = true ) { |
---|
14 | global $self, $parent_file, $submenu_file, $plugin_page, $pagenow, $typenow; |
---|
15 | |
---|
16 | $first_key = key( $menu ); |
---|
17 | |
---|
18 | // 0 = title, 1 = capability, 2 = file, 3 = page title (ignore), 4 = class, 5 = id, 6 = icon src |
---|
19 | foreach ( $menu as $key => $item ) { |
---|
20 | |
---|
21 | $item = array_pad( $item, 7, '' ); |
---|
22 | list( $title, $capability, $slug, $ignore, $class, $id, $icon_src ) = $item; |
---|
23 | |
---|
24 | // @todo: Clarify whether we should texturize or not. |
---|
25 | $title = wptexturize( $title ); |
---|
26 | |
---|
27 | $has_submenu = ! empty( $submenu[ $slug ] ); |
---|
28 | $submenu_items = ( $has_submenu ) ? $submenu[ $slug ] : false; |
---|
29 | |
---|
30 | $parent = $slug; |
---|
31 | |
---|
32 | if ( $key == $first_key ) |
---|
33 | $class .= ' wp-first-item'; |
---|
34 | |
---|
35 | if ( $has_submenu ) |
---|
36 | $class .= ' wp-has-submenu'; |
---|
37 | |
---|
38 | if ( ( $parent_file && $slug == $parent_file ) || ( empty( $typenow ) && $self == $slug ) ) |
---|
39 | $class .= ( $has_submenu ) ? ' wp-has-current-submenu wp-menu-open' : ' current'; |
---|
40 | else |
---|
41 | $class .= ' wp-not-current-submenu'; |
---|
42 | |
---|
43 | // GENERATE MENU ITEM |
---|
44 | if ( $submenu_as_parent && $has_submenu ) { |
---|
45 | $submenu_items = array_values( $submenu_items ); // Re-index. |
---|
46 | $item_slug = $submenu_items[0][2]; |
---|
47 | $item_hook = get_plugin_page_hook( $item_slug, $slug ); |
---|
48 | |
---|
49 | } elseif ( ! empty( $slug ) && current_user_can( $capability ) ) { |
---|
50 | $item_slug = $slug; |
---|
51 | $item_hook = get_plugin_page_hook( $item_slug, 'admin.php' ); |
---|
52 | } |
---|
53 | |
---|
54 | if ( isset( $item_slug ) ) { |
---|
55 | $item_file = $item_slug; |
---|
56 | if ( false !== ( $pos = strpos( $item_file, '?' ) ) ) |
---|
57 | $item_file = substr( $item_file, 0, $pos ); |
---|
58 | |
---|
59 | $item_url = $item_slug; |
---|
60 | if ( ! empty( $item_hook ) || ( ('index.php' != $item_slug ) && file_exists( WP_PLUGIN_DIR . "/$item_file" ) ) ) { |
---|
61 | $parent = 'admin.php'; |
---|
62 | $item_url = "admin.php?page=$item_slug"; |
---|
63 | } |
---|
64 | } |
---|
65 | |
---|
66 | // RENDER MENU ITEM |
---|
67 | echo '<li id="' . esc_attr( $id ) . '" class="' . esc_attr( $class ) . '">'; |
---|
68 | |
---|
69 | if ( false !== strpos( $class, 'wp-menu-separator' ) ) { |
---|
70 | echo '<div class="separator"></div>'; |
---|
71 | |
---|
72 | } elseif ( isset( $item_slug ) ) { |
---|
73 | // Menu image |
---|
74 | echo '<div class="wp-menu-image">'; |
---|
75 | echo '<a href="' . esc_url( $item_url ) . '">'; |
---|
76 | |
---|
77 | if ( ! empty( $icon_src ) ) { |
---|
78 | if ( 'div' === $icon_src ) |
---|
79 | echo '<br />'; |
---|
80 | else |
---|
81 | echo '<img src="' . esc_url( $icon_src ) . '" alt="" />'; |
---|
82 | } |
---|
83 | |
---|
84 | echo '</a></div>'; |
---|
85 | |
---|
86 | // Menu arrow |
---|
87 | echo '<div class="wp-menu-arrow"><div></div></div>'; |
---|
88 | |
---|
89 | // Menu title |
---|
90 | echo '<a href="' . esc_url( $item_url ) . '" class="' . esc_attr( $class ) . '" tabindex="1">'; |
---|
91 | echo $title; |
---|
92 | echo '</a>'; |
---|
93 | } |
---|
94 | |
---|
95 | |
---|
96 | // HANDLE SUBMENUS |
---|
97 | if ( $has_submenu ) { |
---|
98 | |
---|
99 | // Submenu wrapper. |
---|
100 | echo "<div class='wp-submenu'><div class='wp-submenu-wrap'>"; |
---|
101 | echo "<div class='wp-submenu-head'>$title</div><ul>"; |
---|
102 | |
---|
103 | $menu_file = $slug; |
---|
104 | if ( false !== ( $pos = strpos( $menu_file, '?' ) ) ) |
---|
105 | $menu_file = substr( $menu_file, 0, $pos ); |
---|
106 | |
---|
107 | // Clarify the location of the parent. |
---|
108 | if ( ( 'admin.php' != $parent && file_exists( WP_PLUGIN_DIR . "/$menu_file" ) && ! is_dir( WP_PLUGIN_DIR . "/$slug" ) ) || file_exists( $menu_file ) ) |
---|
109 | $parent = $slug; |
---|
110 | else |
---|
111 | $parent = 'admin.php'; |
---|
112 | |
---|
113 | // Handle current for post_type=post|page|foo pages, which won't match $self. |
---|
114 | $self_type = ! empty( $typenow ) ? $self . '?post_type=' . $typenow : 'nothing'; |
---|
115 | |
---|
116 | $first_sub_key = key( $submenu_items ); |
---|
117 | |
---|
118 | foreach ( $submenu_items as $sub_key => $sub_item ) { |
---|
119 | // 0 = title, 1 = cap, 2 = slug, 3 = page title |
---|
120 | list( $sub_title, $sub_cap, $sub_slug ) = $sub_item; |
---|
121 | |
---|
122 | if ( ! current_user_can( $sub_cap ) ) |
---|
123 | continue; |
---|
124 | |
---|
125 | $sub_title = wptexturize( $sub_title ); |
---|
126 | $class = ''; |
---|
127 | |
---|
128 | if ( $sub_key == $first_sub_key ) |
---|
129 | $class .= ' wp-first-item'; |
---|
130 | |
---|
131 | // Determine if we are on the current page. |
---|
132 | if ( isset( $submenu_file ) ) { |
---|
133 | if ( $submenu_file == $sub_slug ) |
---|
134 | $class .= ' current'; |
---|
135 | |
---|
136 | // If plugin_page is set the parent must either match the current page or not physically exist. |
---|
137 | // This allows plugin pages with the same hook to exist under different parents. |
---|
138 | } else if ( |
---|
139 | ( ! isset( $plugin_page ) && $self == $sub_slug ) || |
---|
140 | ( isset( $plugin_page ) && $plugin_page == $sub_slug && |
---|
141 | ( $slug == $self_type || $slug == $self || file_exists( $menu_file ) === false ) |
---|
142 | ) |
---|
143 | ) { |
---|
144 | $class .= ' current'; |
---|
145 | } |
---|
146 | |
---|
147 | $sub_hook = get_plugin_page_hook( $sub_slug, $slug ); |
---|
148 | $sub_file = $sub_slug; |
---|
149 | if ( false !== ( $pos = strpos( $sub_file, '?' ) ) ) |
---|
150 | $sub_file = substr( $sub_file, 0, $pos ); |
---|
151 | |
---|
152 | |
---|
153 | $sub_url = $sub_slug; |
---|
154 | if ( ! empty( $sub_hook ) || ( ( 'index.php' != $sub_slug ) && file_exists( WP_PLUGIN_DIR . "/$sub_file" ) ) ) { |
---|
155 | $sub_url = add_query_arg( array( 'page' => $sub_slug ), $parent ); |
---|
156 | } |
---|
157 | |
---|
158 | // RENDER SUBMENU ITEM |
---|
159 | echo '<li class="' . esc_attr( $class ) . '">'; |
---|
160 | echo '<a href="' . esc_url( $sub_url ) . '" class="' . esc_attr( $class ) . '" tabindex="1">'; |
---|
161 | echo $sub_title; |
---|
162 | echo '</a></li>'; |
---|
163 | } |
---|
164 | |
---|
165 | echo '</ul></div></div>'; |
---|
166 | } |
---|
167 | echo '</li>'; |
---|
168 | } |
---|
169 | |
---|
170 | echo '<li id="collapse-menu" class="hide-if-no-js"><div id="collapse-button"><div></div></div>'; |
---|
171 | echo '<span>' . esc_html__( 'Collapse menu' ) . '</span>'; |
---|
172 | echo '</li>'; |
---|
173 | } |
---|