]> scripts.mit.edu Git - autoinstalls/wordpress.git/blob - wp-admin/includes/plugin.php
Wordpress 2.3.2
[autoinstalls/wordpress.git] / wp-admin / includes / plugin.php
1 <?php
2
3 function get_plugin_data( $plugin_file ) {
4         $plugin_data = implode( '', file( $plugin_file ));
5         preg_match( '|Plugin Name:(.*)$|mi', $plugin_data, $plugin_name );
6         preg_match( '|Plugin URI:(.*)$|mi', $plugin_data, $plugin_uri );
7         preg_match( '|Description:(.*)$|mi', $plugin_data, $description );
8         preg_match( '|Author:(.*)$|mi', $plugin_data, $author_name );
9         preg_match( '|Author URI:(.*)$|mi', $plugin_data, $author_uri );
10
11         if ( preg_match( "|Version:(.*)|i", $plugin_data, $version ))
12                 $version = trim( $version[1] );
13         else
14                 $version = '';
15
16         $description = wptexturize( trim( $description[1] ));
17
18         $name = $plugin_name[1];
19         $name = trim( $name );
20         $plugin = $name;
21         if ('' != trim($plugin_uri[1]) && '' != $name ) {
22                 $plugin = '<a href="' . trim( $plugin_uri[1] ) . '" title="'.__( 'Visit plugin homepage' ).'">'.$plugin.'</a>';
23         }
24
25         if ('' == $author_uri[1] ) {
26                 $author = trim( $author_name[1] );
27         } else {
28                 $author = '<a href="' . trim( $author_uri[1] ) . '" title="'.__( 'Visit author homepage' ).'">' . trim( $author_name[1] ) . '</a>';
29         }
30
31         return array('Name' => $name, 'Title' => $plugin, 'Description' => $description, 'Author' => $author, 'Version' => $version);
32 }
33
34 function get_plugins() {
35         global $wp_plugins;
36
37         if ( isset( $wp_plugins ) ) {
38                 return $wp_plugins;
39         }
40
41         $wp_plugins = array ();
42         $plugin_root = ABSPATH . PLUGINDIR;
43
44         // Files in wp-content/plugins directory
45         $plugins_dir = @ opendir( $plugin_root);
46         if ( $plugins_dir ) {
47                 while (($file = readdir( $plugins_dir ) ) !== false ) {
48                         if ( substr($file, 0, 1) == '.' )
49                                 continue;
50                         if ( is_dir( $plugin_root.'/'.$file ) ) {
51                                 $plugins_subdir = @ opendir( $plugin_root.'/'.$file );
52                                 if ( $plugins_subdir ) {
53                                         while (($subfile = readdir( $plugins_subdir ) ) !== false ) {
54                                                 if ( substr($subfile, 0, 1) == '.' )
55                                                         continue;
56                                                 if ( substr($subfile, -4) == '.php' )
57                                                         $plugin_files[] = "$file/$subfile";
58                                         }
59                                 }
60                         } else {
61                                 if ( substr($file, -4) == '.php' )
62                                         $plugin_files[] = $file;
63                         }
64                 }
65         }
66         @closedir( $plugins_dir );
67         @closedir( $plugins_subdir );
68
69         if ( !$plugins_dir || !$plugin_files )
70                 return $wp_plugins;
71
72         foreach ( $plugin_files as $plugin_file ) {
73                 if ( !is_readable( "$plugin_root/$plugin_file" ) )
74                         continue;
75
76                 $plugin_data = get_plugin_data( "$plugin_root/$plugin_file" );
77
78                 if ( empty ( $plugin_data['Name'] ) )
79                         continue;
80
81                 $wp_plugins[plugin_basename( $plugin_file )] = $plugin_data;
82         }
83
84         uasort( $wp_plugins, create_function( '$a, $b', 'return strnatcasecmp( $a["Name"], $b["Name"] );' ));
85
86         return $wp_plugins;
87 }
88
89 //
90 // Menu
91 //
92
93 function add_menu_page( $page_title, $menu_title, $access_level, $file, $function = '' ) {
94         global $menu, $admin_page_hooks;
95
96         $file = plugin_basename( $file );
97
98         $menu[] = array ( $menu_title, $access_level, $file, $page_title );
99
100         $admin_page_hooks[$file] = sanitize_title( $menu_title );
101
102         $hookname = get_plugin_page_hookname( $file, '' );
103         if (!empty ( $function ) && !empty ( $hookname ))
104                 add_action( $hookname, $function );
105
106         return $hookname;
107 }
108
109 function add_submenu_page( $parent, $page_title, $menu_title, $access_level, $file, $function = '' ) {
110         global $submenu;
111         global $menu;
112         global $_wp_real_parent_file;
113         global $_wp_submenu_nopriv;
114         global $_wp_menu_nopriv;
115
116         $file = plugin_basename( $file );
117
118         $parent = plugin_basename( $parent);
119         if ( isset( $_wp_real_parent_file[$parent] ) )
120                 $parent = $_wp_real_parent_file[$parent];
121
122         if ( !current_user_can( $access_level ) ) {
123                 $_wp_submenu_nopriv[$parent][$file] = true;
124                 return false;
125         }
126
127         // If the parent doesn't already have a submenu, add a link to the parent
128         // as the first item in the submenu.  If the submenu file is the same as the
129         // parent file someone is trying to link back to the parent manually.  In
130         // this case, don't automatically add a link back to avoid duplication.
131         if (!isset( $submenu[$parent] ) && $file != $parent  ) {
132                 foreach ( $menu as $parent_menu ) {
133                         if ( $parent_menu[2] == $parent && current_user_can( $parent_menu[1] ) )
134                                 $submenu[$parent][] = $parent_menu;
135                 }
136         }
137
138         $submenu[$parent][] = array ( $menu_title, $access_level, $file, $page_title );
139
140         $hookname = get_plugin_page_hookname( $file, $parent);
141         if (!empty ( $function ) && !empty ( $hookname ))
142                 add_action( $hookname, $function );
143
144         return $hookname;
145 }
146
147 function add_management_page( $page_title, $menu_title, $access_level, $file, $function = '' ) {
148         return add_submenu_page( 'edit.php', $page_title, $menu_title, $access_level, $file, $function );
149 }
150
151 function add_options_page( $page_title, $menu_title, $access_level, $file, $function = '' ) {
152         return add_submenu_page( 'options-general.php', $page_title, $menu_title, $access_level, $file, $function );
153 }
154
155 function add_theme_page( $page_title, $menu_title, $access_level, $file, $function = '' ) {
156         return add_submenu_page( 'themes.php', $page_title, $menu_title, $access_level, $file, $function );
157 }
158
159 function add_users_page( $page_title, $menu_title, $access_level, $file, $function = '' ) {
160         if ( current_user_can('edit_users') )
161                 $parent = 'users.php';
162         else
163                 $parent = 'profile.php';
164         return add_submenu_page( $parent, $page_title, $menu_title, $access_level, $file, $function );
165 }
166
167 //
168 // Pluggable Menu Support -- Private
169 //
170
171 function get_admin_page_parent() {
172         global $parent_file;
173         global $menu;
174         global $submenu;
175         global $pagenow;
176         global $plugin_page;
177         global $_wp_real_parent_file;
178         global $_wp_menu_nopriv;
179         global $_wp_submenu_nopriv;
180
181         if ( !empty ( $parent_file ) ) {
182                 if ( isset( $_wp_real_parent_file[$parent_file] ) )
183                         $parent_file = $_wp_real_parent_file[$parent_file];
184
185                 return $parent_file;
186         }
187
188         if ( $pagenow == 'admin.php' && isset( $plugin_page ) ) {
189                 foreach ( $menu as $parent_menu ) {
190                         if ( $parent_menu[2] == $plugin_page ) {
191                                 $parent_file = $plugin_page;
192                                 if ( isset( $_wp_real_parent_file[$parent_file] ) )
193                                         $parent_file = $_wp_real_parent_file[$parent_file];
194                                 return $parent_file;
195                         }
196                 }
197                 if ( isset( $_wp_menu_nopriv[$plugin_page] ) ) {
198                         $parent_file = $plugin_page;
199                         if ( isset( $_wp_real_parent_file[$parent_file] ) )
200                                         $parent_file = $_wp_real_parent_file[$parent_file];
201                         return $parent_file;
202                 }
203         }
204
205         if ( isset( $plugin_page ) && isset( $_wp_submenu_nopriv[$pagenow][$plugin_page] ) ) {
206                 $parent_file = $pagenow;
207                 if ( isset( $_wp_real_parent_file[$parent_file] ) )
208                         $parent_file = $_wp_real_parent_file[$parent_file];
209                 return $parent_file;
210         }
211
212         foreach (array_keys( $submenu ) as $parent) {
213                 foreach ( $submenu[$parent] as $submenu_array ) {
214                         if ( isset( $_wp_real_parent_file[$parent] ) )
215                                 $parent = $_wp_real_parent_file[$parent];
216                         if ( $submenu_array[2] == $pagenow ) {
217                                 $parent_file = $parent;
218                                 return $parent;
219                         } else
220                                 if ( isset( $plugin_page ) && ($plugin_page == $submenu_array[2] ) ) {
221                                         $parent_file = $parent;
222                                         return $parent;
223                                 }
224                 }
225         }
226
227         $parent_file = '';
228         return '';
229 }
230
231 function get_admin_page_title() {
232         global $title;
233         global $menu;
234         global $submenu;
235         global $pagenow;
236         global $plugin_page;
237
238         if ( isset( $title ) && !empty ( $title ) ) {
239                 return $title;
240         }
241
242         $hook = get_plugin_page_hook( $plugin_page, $pagenow );
243
244         $parent = $parent1 = get_admin_page_parent();
245         if ( empty ( $parent) ) {
246                 foreach ( $menu as $menu_array ) {
247                         if ( isset( $menu_array[3] ) ) {
248                                 if ( $menu_array[2] == $pagenow ) {
249                                         $title = $menu_array[3];
250                                         return $menu_array[3];
251                                 } else
252                                         if ( isset( $plugin_page ) && ($plugin_page == $menu_array[2] ) && ($hook == $menu_array[3] ) ) {
253                                                 $title = $menu_array[3];
254                                                 return $menu_array[3];
255                                         }
256                         } else {
257                                 $title = $menu_array[0];
258                                 return $title;
259                         }
260                 }
261         } else {
262                 foreach (array_keys( $submenu ) as $parent) {
263                         foreach ( $submenu[$parent] as $submenu_array ) {
264                                 if ( isset( $plugin_page ) &&
265                                         ($plugin_page == $submenu_array[2] ) &&
266                                         (($parent == $pagenow ) || ($parent == $plugin_page ) || ($plugin_page == $hook ) || (($pagenow == 'admin.php' ) && ($parent1 != $submenu_array[2] ) ) )
267                                         ) {
268                                                 $title = $submenu_array[3];
269                                                 return $submenu_array[3];
270                                         }
271
272                                 if ( $submenu_array[2] != $pagenow || isset( $_GET['page'] ) ) // not the current page
273                                         continue;
274
275                                 if ( isset( $submenu_array[3] ) ) {
276                                         $title = $submenu_array[3];
277                                         return $submenu_array[3];
278                                 } else {
279                                         $title = $submenu_array[0];
280                                         return $title;
281                                 }
282                         }
283                 }
284         }
285
286         return $title;
287 }
288
289 function get_plugin_page_hook( $plugin_page, $parent_page ) {
290         global $wp_filter;
291
292         $hook = get_plugin_page_hookname( $plugin_page, $parent_page );
293         if ( isset( $wp_filter[$hook] ))
294                 return $hook;
295         else
296                 return null;
297 }
298
299 function get_plugin_page_hookname( $plugin_page, $parent_page ) {
300         global $admin_page_hooks;
301
302         $parent = get_admin_page_parent();
303
304         if ( empty ( $parent_page ) || 'admin.php' == $parent_page ) {
305                 if ( isset( $admin_page_hooks[$plugin_page] ))
306                         $page_type = 'toplevel';
307                 else
308                         if ( isset( $admin_page_hooks[$parent] ))
309                                 $page_type = $admin_page_hooks[$parent];
310         } else
311                 if ( isset( $admin_page_hooks[$parent_page] ) ) {
312                         $page_type = $admin_page_hooks[$parent_page];
313                 } else {
314                         $page_type = 'admin';
315                 }
316
317         $plugin_name = preg_replace( '!\.php!', '', $plugin_page );
318
319         return $page_type.'_page_'.$plugin_name;
320 }
321
322 function user_can_access_admin_page() {
323         global $pagenow;
324         global $menu;
325         global $submenu;
326         global $_wp_menu_nopriv;
327         global $_wp_submenu_nopriv;
328         global $plugin_page;
329
330         $parent = get_admin_page_parent();
331
332         if ( isset( $_wp_submenu_nopriv[$parent][$pagenow] ) )
333                 return false;
334
335         if ( isset( $plugin_page ) && isset( $_wp_submenu_nopriv[$parent][$plugin_page] ) )
336                 return false;
337
338         if ( empty( $parent) ) {
339                 if ( isset( $_wp_menu_nopriv[$pagenow] ) )
340                         return false;
341                 if ( isset( $_wp_submenu_nopriv[$pagenow][$pagenow] ) )
342                         return false;
343                 if ( isset( $plugin_page ) && isset( $_wp_submenu_nopriv[$pagenow][$plugin_page] ) )
344                         return false;
345                 foreach (array_keys( $_wp_submenu_nopriv ) as $key ) {
346                         if ( isset( $_wp_submenu_nopriv[$key][$pagenow] ) )
347                                 return false;
348                         if ( isset( $plugin_page ) && isset( $_wp_submenu_nopriv[$key][$plugin_page] ) )
349                         return false;
350                 }
351                 return true;
352         }
353
354         if ( isset( $submenu[$parent] ) ) {
355                 foreach ( $submenu[$parent] as $submenu_array ) {
356                         if ( isset( $plugin_page ) && ( $submenu_array[2] == $plugin_page ) ) {
357                                 if ( current_user_can( $submenu_array[1] ))
358                                         return true;
359                                 else
360                                         return false;
361                         } else if ( $submenu_array[2] == $pagenow ) {
362                                 if ( current_user_can( $submenu_array[1] ))
363                                         return true;
364                                 else
365                                         return false;
366                         }
367                 }
368         }
369
370         foreach ( $menu as $menu_array ) {
371                 if ( $menu_array[2] == $parent) {
372                         if ( current_user_can( $menu_array[1] ))
373                                 return true;
374                         else
375                                 return false;
376                 }
377         }
378
379         return true;
380 }
381
382 ?>