]> scripts.mit.edu Git - autoinstalls/wordpress.git/blob - wp-admin/includes/menu.php
Wordpress 3.1.3
[autoinstalls/wordpress.git] / wp-admin / includes / menu.php
1 <?php
2
3 /**
4  * Build Administration Menu.
5  *
6  * @package WordPress
7  * @subpackage Administration
8  */
9
10 if ( is_network_admin() )
11         do_action('_network_admin_menu');
12 elseif ( is_user_admin() )
13         do_action('_user_admin_menu');
14 else
15         do_action('_admin_menu');
16
17 // Create list of page plugin hook names.
18 foreach ($menu as $menu_page) {
19         if ( false !== $pos = strpos($menu_page[2], '?') ) {
20                 // Handle post_type=post|page|foo pages.
21                 $hook_name = substr($menu_page[2], 0, $pos);
22                 $hook_args = substr($menu_page[2], $pos + 1);
23                 wp_parse_str($hook_args, $hook_args);
24                 // Set the hook name to be the post type.
25                 if ( isset($hook_args['post_type']) )
26                         $hook_name = $hook_args['post_type'];
27                 else
28                         $hook_name = basename($hook_name, '.php');
29                 unset($hook_args);
30         } else {
31                 $hook_name = basename($menu_page[2], '.php');
32         }
33         $hook_name = sanitize_title($hook_name);
34
35         if ( isset($compat[$hook_name]) )
36                 $hook_name = $compat[$hook_name];
37         elseif ( !$hook_name )
38                 continue;
39
40         $admin_page_hooks[$menu_page[2]] = $hook_name;
41 }
42 unset($menu_page, $compat);
43
44 $_wp_submenu_nopriv = array();
45 $_wp_menu_nopriv = array();
46 // Loop over submenus and remove pages for which the user does not have privs.
47 foreach ( array( 'submenu' ) as $sub_loop ) {
48         foreach ($$sub_loop as $parent => $sub) {
49                 foreach ($sub as $index => $data) {
50                         if ( ! current_user_can($data[1]) ) {
51                                 unset(${$sub_loop}[$parent][$index]);
52                                 $_wp_submenu_nopriv[$parent][$data[2]] = true;
53                         }
54                 }
55                 unset($index, $data);
56
57                 if ( empty(${$sub_loop}[$parent]) )
58                         unset(${$sub_loop}[$parent]);
59         }
60         unset($sub, $parent);
61 }
62 unset($sub_loop);
63
64 // Loop over the top-level menu.
65 // Menus for which the original parent is not accessible due to lack of privs will have the next
66 // submenu in line be assigned as the new menu parent.
67 foreach ( $menu as $id => $data ) {
68         if ( empty($submenu[$data[2]]) )
69                 continue;
70         $subs = $submenu[$data[2]];
71         $first_sub = array_shift($subs);
72         $old_parent = $data[2];
73         $new_parent = $first_sub[2];
74         // If the first submenu is not the same as the assigned parent,
75         // make the first submenu the new parent.
76         if ( $new_parent != $old_parent ) {
77                 $_wp_real_parent_file[$old_parent] = $new_parent;
78                 $menu[$id][2] = $new_parent;
79
80                 foreach ($submenu[$old_parent] as $index => $data) {
81                         $submenu[$new_parent][$index] = $submenu[$old_parent][$index];
82                         unset($submenu[$old_parent][$index]);
83                 }
84                 unset($submenu[$old_parent], $index);
85
86                 if ( isset($_wp_submenu_nopriv[$old_parent]) )
87                         $_wp_submenu_nopriv[$new_parent] = $_wp_submenu_nopriv[$old_parent];
88         }
89 }
90 unset($id, $data, $subs, $first_sub, $old_parent, $new_parent);
91
92 if ( is_network_admin() )
93         do_action('network_admin_menu', '');
94 elseif ( is_user_admin() )
95         do_action('user_admin_menu', '');
96 else
97         do_action('admin_menu', '');
98
99 // Remove menus that have no accessible submenus and require privs that the user does not have.
100 // Run re-parent loop again.
101 foreach ( $menu as $id => $data ) {
102         if ( ! current_user_can($data[1]) )
103                 $_wp_menu_nopriv[$data[2]] = true;
104
105         // If submenu is empty...
106         if ( empty($submenu[$data[2]]) ) {
107                 // And user doesn't have privs, remove menu.
108                 if ( isset( $_wp_menu_nopriv[$data[2]] ) ) {
109                         unset($menu[$id]);
110                 }
111         }
112 }
113 unset($id, $data);
114
115 // Remove any duplicated seperators
116 $seperator_found = false;
117 foreach ( $menu as $id => $data ) {
118         if ( 0 == strcmp('wp-menu-separator', $data[4] ) ) {
119                 if (false == $seperator_found) {
120                         $seperator_found = true;
121                 } else {
122                         unset($menu[$id]);
123                         $seperator_found = false;
124                 }
125         } else {
126                 $seperator_found = false;
127         }
128 }
129 unset($id, $data);
130
131 function add_cssclass($add, $class) {
132         $class = empty($class) ? $add : $class .= ' ' . $add;
133         return $class;
134 }
135
136 function add_menu_classes($menu) {
137
138         $first = $lastorder = false;
139         $i = 0;
140         $mc = count($menu);
141         foreach ( $menu as $order => $top ) {
142                 $i++;
143
144                 if ( 0 == $order ) { // dashboard is always shown/single
145                         $menu[0][4] = add_cssclass('menu-top-first', $top[4]);
146                         $lastorder = 0;
147                         continue;
148                 }
149
150                 if ( 0 === strpos($top[2], 'separator') ) { // if separator
151                         $first = true;
152                         $c = $menu[$lastorder][4];
153                         $menu[$lastorder][4] = add_cssclass('menu-top-last', $c);
154                         continue;
155                 }
156
157                 if ( $first ) {
158                         $c = $menu[$order][4];
159                         $menu[$order][4] = add_cssclass('menu-top-first', $c);
160                         $first = false;
161                 }
162
163                 if ( $mc == $i ) { // last item
164                         $c = $menu[$order][4];
165                         $menu[$order][4] = add_cssclass('menu-top-last', $c);
166                 }
167
168                 $lastorder = $order;
169         }
170
171         return apply_filters( 'add_menu_classes', $menu );
172 }
173
174 uksort($menu, "strnatcasecmp"); // make it all pretty
175
176 if ( apply_filters('custom_menu_order', false) ) {
177         $menu_order = array();
178         foreach ( $menu as $menu_item ) {
179                 $menu_order[] = $menu_item[2];
180         }
181         unset($menu_item);
182         $default_menu_order = $menu_order;
183         $menu_order = apply_filters('menu_order', $menu_order);
184         $menu_order = array_flip($menu_order);
185         $default_menu_order = array_flip($default_menu_order);
186
187         function sort_menu($a, $b) {
188                 global $menu_order, $default_menu_order;
189                 $a = $a[2];
190                 $b = $b[2];
191                 if ( isset($menu_order[$a]) && !isset($menu_order[$b]) ) {
192                         return -1;
193                 } elseif ( !isset($menu_order[$a]) && isset($menu_order[$b]) ) {
194                         return 1;
195                 } elseif ( isset($menu_order[$a]) && isset($menu_order[$b]) ) {
196                         if ( $menu_order[$a] == $menu_order[$b] )
197                                 return 0;
198                         return ($menu_order[$a] < $menu_order[$b]) ? -1 : 1;
199                 } else {
200                         return ($default_menu_order[$a] <= $default_menu_order[$b]) ? -1 : 1;
201                 }
202         }
203
204         usort($menu, 'sort_menu');
205         unset($menu_order, $default_menu_order);
206 }
207
208 if ( !user_can_access_admin_page() ) {
209         do_action('admin_page_access_denied');
210         wp_die( __('You do not have sufficient permissions to access this page.') );
211 }
212
213 $menu = add_menu_classes($menu);
214
215 ?>