]> scripts.mit.edu Git - autoinstalls/wordpress.git/blob - wp-admin/includes/menu.php
WordPress 3.9.1
[autoinstalls/wordpress.git] / wp-admin / includes / menu.php
1 <?php
2 /**
3  * Build Administration Menu.
4  *
5  * @package WordPress
6  * @subpackage Administration
7  */
8
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 }
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.
74 foreach ( array( 'submenu' ) as $sub_loop ) {
75         foreach ($$sub_loop as $parent => $sub) {
76                 foreach ($sub as $index => $data) {
77                         if ( ! current_user_can($data[1]) ) {
78                                 unset(${$sub_loop}[$parent][$index]);
79                                 $_wp_submenu_nopriv[$parent][$data[2]] = true;
80                         }
81                 }
82                 unset($index, $data);
83
84                 if ( empty(${$sub_loop}[$parent]) )
85                         unset(${$sub_loop}[$parent]);
86         }
87         unset($sub, $parent);
88 }
89 unset($sub_loop);
90
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  */
96 foreach ( $menu as $id => $data ) {
97         if ( empty($submenu[$data[2]]) )
98                 continue;
99         $subs = $submenu[$data[2]];
100         $first_sub = array_shift($subs);
101         $old_parent = $data[2];
102         $new_parent = $first_sub[2];
103         // If the first submenu is not the same as the assigned parent,
104         // make the first submenu the new parent.
105         if ( $new_parent != $old_parent ) {
106                 $_wp_real_parent_file[$old_parent] = $new_parent;
107                 $menu[$id][2] = $new_parent;
108
109                 foreach ($submenu[$old_parent] as $index => $data) {
110                         $submenu[$new_parent][$index] = $submenu[$old_parent][$index];
111                         unset($submenu[$old_parent][$index]);
112                 }
113                 unset($submenu[$old_parent], $index);
114
115                 if ( isset($_wp_submenu_nopriv[$old_parent]) )
116                         $_wp_submenu_nopriv[$new_parent] = $_wp_submenu_nopriv[$old_parent];
117         }
118 }
119 unset($id, $data, $subs, $first_sub, $old_parent, $new_parent);
120
121 if ( is_network_admin() ) {
122
123         /**
124          * Fires before the administration menu loads in the Network Admin.
125          *
126          * @since 3.1.0
127          *
128          * @param string $context Empty context.
129          */
130         do_action( 'network_admin_menu', '' );
131 } elseif ( is_user_admin() ) {
132
133         /**
134          * Fires before the administration menu loads in the User Admin.
135          *
136          * @since 3.1.0
137          *
138          * @param string $context Empty context.
139          */
140         do_action( 'user_admin_menu', '' );
141 } else {
142
143         /**
144          * Fires before the administration menu loads in the admin.
145          *
146          * @since 1.5.0
147          *
148          * @param string $context Empty context.
149          */
150         do_action( 'admin_menu', '' );
151 }
152
153 /*
154  * Remove menus that have no accessible submenus and require privileges
155  * that the user does not have. Run re-parent loop again.
156  */
157 foreach ( $menu as $id => $data ) {
158         if ( ! current_user_can($data[1]) )
159                 $_wp_menu_nopriv[$data[2]] = true;
160
161         /*
162          * If there is only one submenu and it is has same destination as the parent,
163          * remove the submenu.
164          */
165         if ( ! empty( $submenu[$data[2]] ) && 1 == count ( $submenu[$data[2]] ) ) {
166                 $subs = $submenu[$data[2]];
167                 $first_sub = array_shift($subs);
168                 if ( $data[2] == $first_sub[2] )
169                         unset( $submenu[$data[2]] );
170         }
171
172         // If submenu is empty...
173         if ( empty($submenu[$data[2]]) ) {
174                 // And user doesn't have privs, remove menu.
175                 if ( isset( $_wp_menu_nopriv[$data[2]] ) ) {
176                         unset($menu[$id]);
177                 }
178         }
179 }
180 unset($id, $data, $subs, $first_sub);
181
182 // Remove any duplicated separators
183 $separator_found = false;
184 foreach ( $menu as $id => $data ) {
185         if ( 0 == strcmp('wp-menu-separator', $data[4] ) ) {
186                 if (false == $separator_found) {
187                         $separator_found = true;
188                 } else {
189                         unset($menu[$id]);
190                         $separator_found = false;
191                 }
192         } else {
193                 $separator_found = false;
194         }
195 }
196 unset($id, $data);
197
198 function add_cssclass($add, $class) {
199         $class = empty($class) ? $add : $class .= ' ' . $add;
200         return $class;
201 }
202
203 function add_menu_classes($menu) {
204
205         $first = $lastorder = false;
206         $i = 0;
207         $mc = count($menu);
208         foreach ( $menu as $order => $top ) {
209                 $i++;
210
211                 if ( 0 == $order ) { // dashboard is always shown/single
212                         $menu[0][4] = add_cssclass('menu-top-first', $top[4]);
213                         $lastorder = 0;
214                         continue;
215                 }
216
217                 if ( 0 === strpos($top[2], 'separator') && false !== $lastorder ) { // if separator
218                         $first = true;
219                         $c = $menu[$lastorder][4];
220                         $menu[$lastorder][4] = add_cssclass('menu-top-last', $c);
221                         continue;
222                 }
223
224                 if ( $first ) {
225                         $c = $menu[$order][4];
226                         $menu[$order][4] = add_cssclass('menu-top-first', $c);
227                         $first = false;
228                 }
229
230                 if ( $mc == $i ) { // last item
231                         $c = $menu[$order][4];
232                         $menu[$order][4] = add_cssclass('menu-top-last', $c);
233                 }
234
235                 $lastorder = $order;
236         }
237
238         /**
239          * Filter administration menus array with classes added for top-level items.
240          *
241          * @since 2.7.0
242          *
243          * @param array $menu Associative array of administration menu items.
244          */
245         return apply_filters( 'add_menu_classes', $menu );
246 }
247
248 uksort($menu, "strnatcasecmp"); // make it all pretty
249
250 /**
251  * Filter whether to enable custom ordering of the administration menu.
252  *
253  * See the 'menu_order' filter for reordering menu items.
254  *
255  * @since 2.8.0
256  *
257  * @param bool $custom Whether custom ordering is enabled. Default false.
258  */
259 if ( apply_filters( 'custom_menu_order', false ) ) {
260         $menu_order = array();
261         foreach ( $menu as $menu_item ) {
262                 $menu_order[] = $menu_item[2];
263         }
264         unset($menu_item);
265         $default_menu_order = $menu_order;
266
267         /**
268          * Filter the order of administration menu items.
269          *
270          * A truthy value must first be passed to the 'custom_menu_order' filter
271          * for this filter to work. Use the following to enable custom menu ordering:
272          *
273          *     add_filter( 'custom_menu_order', '__return_true' );
274          *
275          * @since 2.8.0
276          *
277          * @param array $menu_order An ordered array of menu items.
278          */
279         $menu_order = apply_filters( 'menu_order', $menu_order );
280         $menu_order = array_flip($menu_order);
281         $default_menu_order = array_flip($default_menu_order);
282
283         function sort_menu($a, $b) {
284                 global $menu_order, $default_menu_order;
285                 $a = $a[2];
286                 $b = $b[2];
287                 if ( isset($menu_order[$a]) && !isset($menu_order[$b]) ) {
288                         return -1;
289                 } elseif ( !isset($menu_order[$a]) && isset($menu_order[$b]) ) {
290                         return 1;
291                 } elseif ( isset($menu_order[$a]) && isset($menu_order[$b]) ) {
292                         if ( $menu_order[$a] == $menu_order[$b] )
293                                 return 0;
294                         return ($menu_order[$a] < $menu_order[$b]) ? -1 : 1;
295                 } else {
296                         return ($default_menu_order[$a] <= $default_menu_order[$b]) ? -1 : 1;
297                 }
298         }
299
300         usort($menu, 'sort_menu');
301         unset($menu_order, $default_menu_order);
302 }
303
304 // Remove the last menu item if it is a separator.
305 $last_menu_key = array_keys( $menu );
306 $last_menu_key = array_pop( $last_menu_key );
307 if ( !empty( $menu ) && 'wp-menu-separator' == $menu[ $last_menu_key ][ 4 ] )
308         unset( $menu[ $last_menu_key ] );
309 unset( $last_menu_key );
310
311 if ( !user_can_access_admin_page() ) {
312
313         /**
314          * Fires when access to an admin page is denied.
315          *
316          * @since 2.5.0
317          */
318         do_action( 'admin_page_access_denied' );
319
320         wp_die( __('You do not have sufficient permissions to access this page.') );
321 }
322
323 $menu = add_menu_classes($menu);