author | ymh <ymh.work@gmail.com> |
Mon, 14 Oct 2019 17:39:30 +0200 | |
changeset 7 | cf61fcea0001 |
parent 5 | 5e2f62d02dcd |
child 9 | 177826044cd9 |
permissions | -rw-r--r-- |
0 | 1 |
<?php |
2 |
/** |
|
3 |
* Build Administration Menu. |
|
4 |
* |
|
5 |
* @package WordPress |
|
6 |
* @subpackage Administration |
|
7 |
*/ |
|
8 |
||
5 | 9 |
if ( is_network_admin() ) { |
10 |
||
11 |
/** |
|
12 |
* Fires before the administration menu loads in the Network Admin. |
|
13 |
* |
|
14 |
* The hook fires before menus and sub-menus are removed based on user privileges. |
|
15 |
* |
|
16 |
* @private |
|
17 |
* @since 3.1.0 |
|
18 |
*/ |
|
19 |
do_action( '_network_admin_menu' ); |
|
20 |
} elseif ( is_user_admin() ) { |
|
21 |
||
22 |
/** |
|
23 |
* Fires before the administration menu loads in the User Admin. |
|
24 |
* |
|
25 |
* The hook fires before menus and sub-menus are removed based on user privileges. |
|
26 |
* |
|
27 |
* @private |
|
28 |
* @since 3.1.0 |
|
29 |
*/ |
|
30 |
do_action( '_user_admin_menu' ); |
|
31 |
} else { |
|
32 |
||
33 |
/** |
|
34 |
* Fires before the administration menu loads in the admin. |
|
35 |
* |
|
36 |
* The hook fires before menus and sub-menus are removed based on user privileges. |
|
37 |
* |
|
38 |
* @private |
|
39 |
* @since 2.2.0 |
|
40 |
*/ |
|
41 |
do_action( '_admin_menu' ); |
|
42 |
} |
|
0 | 43 |
|
44 |
// Create list of page plugin hook names. |
|
45 |
foreach ($menu as $menu_page) { |
|
46 |
if ( false !== $pos = strpos($menu_page[2], '?') ) { |
|
47 |
// Handle post_type=post|page|foo pages. |
|
48 |
$hook_name = substr($menu_page[2], 0, $pos); |
|
49 |
$hook_args = substr($menu_page[2], $pos + 1); |
|
50 |
wp_parse_str($hook_args, $hook_args); |
|
51 |
// Set the hook name to be the post type. |
|
52 |
if ( isset($hook_args['post_type']) ) |
|
53 |
$hook_name = $hook_args['post_type']; |
|
54 |
else |
|
55 |
$hook_name = basename($hook_name, '.php'); |
|
56 |
unset($hook_args); |
|
57 |
} else { |
|
58 |
$hook_name = basename($menu_page[2], '.php'); |
|
59 |
} |
|
60 |
$hook_name = sanitize_title($hook_name); |
|
61 |
||
62 |
if ( isset($compat[$hook_name]) ) |
|
63 |
$hook_name = $compat[$hook_name]; |
|
64 |
elseif ( !$hook_name ) |
|
65 |
continue; |
|
66 |
||
67 |
$admin_page_hooks[$menu_page[2]] = $hook_name; |
|
68 |
} |
|
69 |
unset($menu_page, $compat); |
|
70 |
||
71 |
$_wp_submenu_nopriv = array(); |
|
72 |
$_wp_menu_nopriv = array(); |
|
73 |
// Loop over submenus and remove pages for which the user does not have privs. |
|
5 | 74 |
foreach ($submenu as $parent => $sub) { |
75 |
foreach ($sub as $index => $data) { |
|
76 |
if ( ! current_user_can($data[1]) ) { |
|
77 |
unset($submenu[$parent][$index]); |
|
78 |
$_wp_submenu_nopriv[$parent][$data[2]] = true; |
|
0 | 79 |
} |
5 | 80 |
} |
81 |
unset($index, $data); |
|
0 | 82 |
|
5 | 83 |
if ( empty($submenu[$parent]) ) |
84 |
unset($submenu[$parent]); |
|
0 | 85 |
} |
5 | 86 |
unset($sub, $parent); |
0 | 87 |
|
5 | 88 |
/* |
89 |
* Loop over the top-level menu. |
|
90 |
* Menus for which the original parent is not accessible due to lack of privileges |
|
91 |
* will have the next submenu in line be assigned as the new menu parent. |
|
92 |
*/ |
|
0 | 93 |
foreach ( $menu as $id => $data ) { |
94 |
if ( empty($submenu[$data[2]]) ) |
|
95 |
continue; |
|
96 |
$subs = $submenu[$data[2]]; |
|
5 | 97 |
$first_sub = reset( $subs ); |
0 | 98 |
$old_parent = $data[2]; |
99 |
$new_parent = $first_sub[2]; |
|
5 | 100 |
/* |
101 |
* If the first submenu is not the same as the assigned parent, |
|
102 |
* make the first submenu the new parent. |
|
103 |
*/ |
|
0 | 104 |
if ( $new_parent != $old_parent ) { |
105 |
$_wp_real_parent_file[$old_parent] = $new_parent; |
|
106 |
$menu[$id][2] = $new_parent; |
|
107 |
||
108 |
foreach ($submenu[$old_parent] as $index => $data) { |
|
109 |
$submenu[$new_parent][$index] = $submenu[$old_parent][$index]; |
|
110 |
unset($submenu[$old_parent][$index]); |
|
111 |
} |
|
112 |
unset($submenu[$old_parent], $index); |
|
113 |
||
114 |
if ( isset($_wp_submenu_nopriv[$old_parent]) ) |
|
115 |
$_wp_submenu_nopriv[$new_parent] = $_wp_submenu_nopriv[$old_parent]; |
|
116 |
} |
|
117 |
} |
|
118 |
unset($id, $data, $subs, $first_sub, $old_parent, $new_parent); |
|
119 |
||
5 | 120 |
if ( is_network_admin() ) { |
121 |
||
122 |
/** |
|
123 |
* Fires before the administration menu loads in the Network Admin. |
|
124 |
* |
|
125 |
* @since 3.1.0 |
|
126 |
* |
|
127 |
* @param string $context Empty context. |
|
128 |
*/ |
|
129 |
do_action( 'network_admin_menu', '' ); |
|
130 |
} elseif ( is_user_admin() ) { |
|
0 | 131 |
|
5 | 132 |
/** |
133 |
* Fires before the administration menu loads in the User Admin. |
|
134 |
* |
|
135 |
* @since 3.1.0 |
|
136 |
* |
|
137 |
* @param string $context Empty context. |
|
138 |
*/ |
|
139 |
do_action( 'user_admin_menu', '' ); |
|
140 |
} else { |
|
141 |
||
142 |
/** |
|
143 |
* Fires before the administration menu loads in the admin. |
|
144 |
* |
|
145 |
* @since 1.5.0 |
|
146 |
* |
|
147 |
* @param string $context Empty context. |
|
148 |
*/ |
|
149 |
do_action( 'admin_menu', '' ); |
|
150 |
} |
|
151 |
||
152 |
/* |
|
153 |
* Remove menus that have no accessible submenus and require privileges |
|
154 |
* that the user does not have. Run re-parent loop again. |
|
155 |
*/ |
|
0 | 156 |
foreach ( $menu as $id => $data ) { |
157 |
if ( ! current_user_can($data[1]) ) |
|
158 |
$_wp_menu_nopriv[$data[2]] = true; |
|
159 |
||
5 | 160 |
/* |
161 |
* If there is only one submenu and it is has same destination as the parent, |
|
162 |
* remove the submenu. |
|
163 |
*/ |
|
0 | 164 |
if ( ! empty( $submenu[$data[2]] ) && 1 == count ( $submenu[$data[2]] ) ) { |
165 |
$subs = $submenu[$data[2]]; |
|
5 | 166 |
$first_sub = reset( $subs ); |
0 | 167 |
if ( $data[2] == $first_sub[2] ) |
168 |
unset( $submenu[$data[2]] ); |
|
169 |
} |
|
170 |
||
171 |
// If submenu is empty... |
|
172 |
if ( empty($submenu[$data[2]]) ) { |
|
173 |
// And user doesn't have privs, remove menu. |
|
174 |
if ( isset( $_wp_menu_nopriv[$data[2]] ) ) { |
|
175 |
unset($menu[$id]); |
|
176 |
} |
|
177 |
} |
|
178 |
} |
|
179 |
unset($id, $data, $subs, $first_sub); |
|
180 |
||
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
181 |
/** |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
182 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
183 |
* @param string $add |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
184 |
* @param string $class |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
185 |
* @return string |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
186 |
*/ |
0 | 187 |
function add_cssclass($add, $class) { |
188 |
$class = empty($class) ? $add : $class .= ' ' . $add; |
|
189 |
return $class; |
|
190 |
} |
|
191 |
||
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
192 |
/** |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
193 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
194 |
* @param array $menu |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
195 |
* @return array |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
196 |
*/ |
0 | 197 |
function add_menu_classes($menu) { |
198 |
$first = $lastorder = false; |
|
199 |
$i = 0; |
|
200 |
$mc = count($menu); |
|
201 |
foreach ( $menu as $order => $top ) { |
|
202 |
$i++; |
|
203 |
||
204 |
if ( 0 == $order ) { // dashboard is always shown/single |
|
205 |
$menu[0][4] = add_cssclass('menu-top-first', $top[4]); |
|
206 |
$lastorder = 0; |
|
207 |
continue; |
|
208 |
} |
|
209 |
||
210 |
if ( 0 === strpos($top[2], 'separator') && false !== $lastorder ) { // if separator |
|
211 |
$first = true; |
|
212 |
$c = $menu[$lastorder][4]; |
|
213 |
$menu[$lastorder][4] = add_cssclass('menu-top-last', $c); |
|
214 |
continue; |
|
215 |
} |
|
216 |
||
217 |
if ( $first ) { |
|
218 |
$c = $menu[$order][4]; |
|
219 |
$menu[$order][4] = add_cssclass('menu-top-first', $c); |
|
220 |
$first = false; |
|
221 |
} |
|
222 |
||
223 |
if ( $mc == $i ) { // last item |
|
224 |
$c = $menu[$order][4]; |
|
225 |
$menu[$order][4] = add_cssclass('menu-top-last', $c); |
|
226 |
} |
|
227 |
||
228 |
$lastorder = $order; |
|
229 |
} |
|
230 |
||
5 | 231 |
/** |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
232 |
* Filters administration menus array with classes added for top-level items. |
5 | 233 |
* |
234 |
* @since 2.7.0 |
|
235 |
* |
|
236 |
* @param array $menu Associative array of administration menu items. |
|
237 |
*/ |
|
0 | 238 |
return apply_filters( 'add_menu_classes', $menu ); |
239 |
} |
|
240 |
||
241 |
uksort($menu, "strnatcasecmp"); // make it all pretty |
|
242 |
||
5 | 243 |
/** |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
244 |
* Filters whether to enable custom ordering of the administration menu. |
5 | 245 |
* |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
246 |
* See the {@see 'menu_order'} filter for reordering menu items. |
5 | 247 |
* |
248 |
* @since 2.8.0 |
|
249 |
* |
|
250 |
* @param bool $custom Whether custom ordering is enabled. Default false. |
|
251 |
*/ |
|
252 |
if ( apply_filters( 'custom_menu_order', false ) ) { |
|
0 | 253 |
$menu_order = array(); |
254 |
foreach ( $menu as $menu_item ) { |
|
255 |
$menu_order[] = $menu_item[2]; |
|
256 |
} |
|
257 |
unset($menu_item); |
|
258 |
$default_menu_order = $menu_order; |
|
5 | 259 |
|
260 |
/** |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
261 |
* Filters the order of administration menu items. |
5 | 262 |
* |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
263 |
* A truthy value must first be passed to the {@see 'custom_menu_order'} filter |
5 | 264 |
* for this filter to work. Use the following to enable custom menu ordering: |
265 |
* |
|
266 |
* add_filter( 'custom_menu_order', '__return_true' ); |
|
267 |
* |
|
268 |
* @since 2.8.0 |
|
269 |
* |
|
270 |
* @param array $menu_order An ordered array of menu items. |
|
271 |
*/ |
|
272 |
$menu_order = apply_filters( 'menu_order', $menu_order ); |
|
0 | 273 |
$menu_order = array_flip($menu_order); |
274 |
$default_menu_order = array_flip($default_menu_order); |
|
275 |
||
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
276 |
/** |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
277 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
278 |
* @global array $menu_order |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
279 |
* @global array $default_menu_order |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
280 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
281 |
* @param array $a |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
282 |
* @param array $b |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
283 |
* @return int |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
284 |
*/ |
0 | 285 |
function sort_menu($a, $b) { |
286 |
global $menu_order, $default_menu_order; |
|
287 |
$a = $a[2]; |
|
288 |
$b = $b[2]; |
|
289 |
if ( isset($menu_order[$a]) && !isset($menu_order[$b]) ) { |
|
290 |
return -1; |
|
291 |
} elseif ( !isset($menu_order[$a]) && isset($menu_order[$b]) ) { |
|
292 |
return 1; |
|
293 |
} elseif ( isset($menu_order[$a]) && isset($menu_order[$b]) ) { |
|
294 |
if ( $menu_order[$a] == $menu_order[$b] ) |
|
295 |
return 0; |
|
296 |
return ($menu_order[$a] < $menu_order[$b]) ? -1 : 1; |
|
297 |
} else { |
|
298 |
return ($default_menu_order[$a] <= $default_menu_order[$b]) ? -1 : 1; |
|
299 |
} |
|
300 |
} |
|
301 |
||
302 |
usort($menu, 'sort_menu'); |
|
303 |
unset($menu_order, $default_menu_order); |
|
304 |
} |
|
305 |
||
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
306 |
// Prevent adjacent separators |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
307 |
$prev_menu_was_separator = false; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
308 |
foreach ( $menu as $id => $data ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
309 |
if ( false === stristr( $data[4], 'wp-menu-separator' ) ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
310 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
311 |
// This item is not a separator, so falsey the toggler and do nothing |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
312 |
$prev_menu_was_separator = false; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
313 |
} else { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
314 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
315 |
// The previous item was a separator, so unset this one |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
316 |
if ( true === $prev_menu_was_separator ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
317 |
unset( $menu[ $id ] ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
318 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
319 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
320 |
// This item is a separator, so truthy the toggler and move on |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
321 |
$prev_menu_was_separator = true; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
322 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
323 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
324 |
unset( $id, $data, $prev_menu_was_separator ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
325 |
|
0 | 326 |
// Remove the last menu item if it is a separator. |
327 |
$last_menu_key = array_keys( $menu ); |
|
328 |
$last_menu_key = array_pop( $last_menu_key ); |
|
329 |
if ( !empty( $menu ) && 'wp-menu-separator' == $menu[ $last_menu_key ][ 4 ] ) |
|
330 |
unset( $menu[ $last_menu_key ] ); |
|
331 |
unset( $last_menu_key ); |
|
332 |
||
333 |
if ( !user_can_access_admin_page() ) { |
|
5 | 334 |
|
335 |
/** |
|
336 |
* Fires when access to an admin page is denied. |
|
337 |
* |
|
338 |
* @since 2.5.0 |
|
339 |
*/ |
|
340 |
do_action( 'admin_page_access_denied' ); |
|
341 |
||
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
342 |
wp_die( __( 'Sorry, you are not allowed to access this page.' ), 403 ); |
0 | 343 |
} |
344 |
||
345 |
$menu = add_menu_classes($menu); |