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