]> scripts.mit.edu Git - autoinstalls/wordpress.git/blob - wp-admin/includes/plugin.php
Wordpress 2.5.1
[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($plugin_folder = '') {
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         if( !empty($plugin_folder) )
44                 $plugin_root .= $plugin_folder;
45
46         // Files in wp-content/plugins directory
47         $plugins_dir = @ opendir( $plugin_root);
48         if ( $plugins_dir ) {
49                 while (($file = readdir( $plugins_dir ) ) !== false ) {
50                         if ( substr($file, 0, 1) == '.' )
51                                 continue;
52                         if ( is_dir( $plugin_root.'/'.$file ) ) {
53                                 $plugins_subdir = @ opendir( $plugin_root.'/'.$file );
54                                 if ( $plugins_subdir ) {
55                                         while (($subfile = readdir( $plugins_subdir ) ) !== false ) {
56                                                 if ( substr($subfile, 0, 1) == '.' )
57                                                         continue;
58                                                 if ( substr($subfile, -4) == '.php' )
59                                                         $plugin_files[] = "$file/$subfile";
60                                         }
61                                 }
62                         } else {
63                                 if ( substr($file, -4) == '.php' )
64                                         $plugin_files[] = $file;
65                         }
66                 }
67         }
68         @closedir( $plugins_dir );
69         @closedir( $plugins_subdir );
70
71         if ( !$plugins_dir || !$plugin_files )
72                 return $wp_plugins;
73
74         foreach ( $plugin_files as $plugin_file ) {
75                 if ( !is_readable( "$plugin_root/$plugin_file" ) )
76                         continue;
77
78                 $plugin_data = get_plugin_data( "$plugin_root/$plugin_file" );
79
80                 if ( empty ( $plugin_data['Name'] ) )
81                         continue;
82
83                 $wp_plugins[plugin_basename( $plugin_file )] = $plugin_data;
84         }
85
86         uasort( $wp_plugins, create_function( '$a, $b', 'return strnatcasecmp( $a["Name"], $b["Name"] );' ));
87
88         return $wp_plugins;
89 }
90
91 function is_plugin_active($plugin){
92         return in_array($plugin, get_option('active_plugins'));
93 }
94
95 function activate_plugin($plugin, $redirect = '') {
96                 $current = get_option('active_plugins');
97                 $plugin = trim($plugin);
98
99                 $valid = validate_plugin($plugin);
100                 if ( is_wp_error($valid) )
101                         return $valid;
102
103                 if ( !in_array($plugin, $current) ) {
104                         if ( !empty($redirect) )
105                                 wp_redirect(add_query_arg('_error_nonce', wp_create_nonce('plugin-activation-error_' . $plugin), $redirect)); // we'll override this later if the plugin can be included without fatal error
106                         ob_start();
107                         @include(ABSPATH . PLUGINDIR . '/' . $plugin);
108                         $current[] = $plugin;
109                         sort($current);
110                         update_option('active_plugins', $current);
111                         do_action('activate_' . $plugin);
112                         ob_end_clean();
113                 }
114
115                 return null;
116 }
117
118 function deactivate_plugins($plugins, $silent= false) {
119         $current = get_option('active_plugins');
120
121         if ( !is_array($plugins) )
122                 $plugins = array($plugins);
123
124         foreach ( $plugins as $plugin ) {
125                 if( ! is_plugin_active($plugin) )
126                         continue;
127                 array_splice($current, array_search( $plugin, $current), 1 ); // Fixed Array-fu!
128                 if ( ! $silent ) //Used by Plugin updater to internally deactivate plugin, however, not to notify plugins of the fact to prevent plugin output.
129                         do_action('deactivate_' . trim( $plugin ));
130         }
131
132         update_option('active_plugins', $current);
133 }
134
135 function deactivate_all_plugins() {
136         $current = get_option('active_plugins');
137         if ( empty($current) )
138                 return;
139
140         deactivate_plugins($current);
141
142         update_option('deactivated_plugins', $current);
143 }
144
145 function reactivate_all_plugins($redirect = '') {
146         $plugins = get_option('deactivated_plugins');
147
148         if ( empty($plugins) )
149                 return;
150
151         if ( !empty($redirect) )
152                 wp_redirect(add_query_arg('_error_nonce', wp_create_nonce('plugin-activation-error_' . $plugin), $redirect));
153
154         $errors = array();
155         foreach ( (array) $plugins as $plugin ) {
156                 $result = activate_plugin($plugin);
157                 if ( is_wp_error($result) )
158                         $errors[$plugin] = $result;
159         }
160
161         delete_option('deactivated_plugins');
162
163         if ( !empty($errors) )
164                 return new WP_Error('plugins_invalid', __('One of the plugins is invalid.'), $errors);
165
166         return true;
167 }
168
169 function validate_active_plugins() {
170         $check_plugins = get_option('active_plugins');
171
172         // Sanity check.  If the active plugin list is not an array, make it an
173         // empty array.
174         if ( !is_array($check_plugins) ) {
175                 update_option('active_plugins', array());
176                 return;
177         }
178
179         // If a plugin file does not exist, remove it from the list of active
180         // plugins.
181         foreach ( $check_plugins as $check_plugin ) {
182                 if ( !file_exists(ABSPATH . PLUGINDIR . '/' . $check_plugin) ) {
183                         $current = get_option('active_plugins');
184                         $key = array_search($check_plugin, $current);
185                         if ( false !== $key && NULL !== $key ) {
186                                 unset($current[$key]);
187                                 update_option('active_plugins', $current);
188                         }
189                 }
190         }
191 }
192
193 function validate_plugin($plugin) {
194         if ( validate_file($plugin) )
195                 return new WP_Error('plugin_invalid', __('Invalid plugin.'));
196         if ( ! file_exists(ABSPATH . PLUGINDIR . '/' . $plugin) )
197                 return new WP_Error('plugin_not_found', __('Plugin file does not exist.'));
198
199         return 0;
200 }
201
202 //
203 // Menu
204 //
205
206 function add_menu_page( $page_title, $menu_title, $access_level, $file, $function = '' ) {
207         global $menu, $admin_page_hooks;
208
209         $file = plugin_basename( $file );
210
211         $menu[] = array ( $menu_title, $access_level, $file, $page_title );
212
213         $admin_page_hooks[$file] = sanitize_title( $menu_title );
214
215         $hookname = get_plugin_page_hookname( $file, '' );
216         if (!empty ( $function ) && !empty ( $hookname ))
217                 add_action( $hookname, $function );
218
219         return $hookname;
220 }
221
222 function add_submenu_page( $parent, $page_title, $menu_title, $access_level, $file, $function = '' ) {
223         global $submenu;
224         global $menu;
225         global $_wp_real_parent_file;
226         global $_wp_submenu_nopriv;
227
228         $file = plugin_basename( $file );
229
230         $parent = plugin_basename( $parent);
231         if ( isset( $_wp_real_parent_file[$parent] ) )
232                 $parent = $_wp_real_parent_file[$parent];
233
234         if ( !current_user_can( $access_level ) ) {
235                 $_wp_submenu_nopriv[$parent][$file] = true;
236                 return false;
237         }
238
239         // If the parent doesn't already have a submenu, add a link to the parent
240         // as the first item in the submenu.  If the submenu file is the same as the
241         // parent file someone is trying to link back to the parent manually.  In
242         // this case, don't automatically add a link back to avoid duplication.
243         if (!isset( $submenu[$parent] ) && $file != $parent  ) {
244                 foreach ( $menu as $parent_menu ) {
245                         if ( $parent_menu[2] == $parent && current_user_can( $parent_menu[1] ) )
246                                 $submenu[$parent][] = $parent_menu;
247                 }
248         }
249
250         $submenu[$parent][] = array ( $menu_title, $access_level, $file, $page_title );
251
252         $hookname = get_plugin_page_hookname( $file, $parent);
253         if (!empty ( $function ) && !empty ( $hookname ))
254                 add_action( $hookname, $function );
255
256         return $hookname;
257 }
258
259 function add_management_page( $page_title, $menu_title, $access_level, $file, $function = '' ) {
260         return add_submenu_page( 'edit.php', $page_title, $menu_title, $access_level, $file, $function );
261 }
262
263 function add_options_page( $page_title, $menu_title, $access_level, $file, $function = '' ) {
264         return add_submenu_page( 'options-general.php', $page_title, $menu_title, $access_level, $file, $function );
265 }
266
267 function add_theme_page( $page_title, $menu_title, $access_level, $file, $function = '' ) {
268         return add_submenu_page( 'themes.php', $page_title, $menu_title, $access_level, $file, $function );
269 }
270
271 function add_users_page( $page_title, $menu_title, $access_level, $file, $function = '' ) {
272         if ( current_user_can('edit_users') )
273                 $parent = 'users.php';
274         else
275                 $parent = 'profile.php';
276         return add_submenu_page( $parent, $page_title, $menu_title, $access_level, $file, $function );
277 }
278
279 //
280 // Pluggable Menu Support -- Private
281 //
282
283 function get_admin_page_parent() {
284         global $parent_file;
285         global $menu;
286         global $submenu;
287         global $pagenow;
288         global $plugin_page;
289         global $_wp_real_parent_file;
290         global $_wp_menu_nopriv;
291         global $_wp_submenu_nopriv;
292
293         if ( !empty ( $parent_file ) ) {
294                 if ( isset( $_wp_real_parent_file[$parent_file] ) )
295                         $parent_file = $_wp_real_parent_file[$parent_file];
296
297                 return $parent_file;
298         }
299
300         if ( $pagenow == 'admin.php' && isset( $plugin_page ) ) {
301                 foreach ( $menu as $parent_menu ) {
302                         if ( $parent_menu[2] == $plugin_page ) {
303                                 $parent_file = $plugin_page;
304                                 if ( isset( $_wp_real_parent_file[$parent_file] ) )
305                                         $parent_file = $_wp_real_parent_file[$parent_file];
306                                 return $parent_file;
307                         }
308                 }
309                 if ( isset( $_wp_menu_nopriv[$plugin_page] ) ) {
310                         $parent_file = $plugin_page;
311                         if ( isset( $_wp_real_parent_file[$parent_file] ) )
312                                         $parent_file = $_wp_real_parent_file[$parent_file];
313                         return $parent_file;
314                 }
315         }
316
317         if ( isset( $plugin_page ) && isset( $_wp_submenu_nopriv[$pagenow][$plugin_page] ) ) {
318                 $parent_file = $pagenow;
319                 if ( isset( $_wp_real_parent_file[$parent_file] ) )
320                         $parent_file = $_wp_real_parent_file[$parent_file];
321                 return $parent_file;
322         }
323
324         foreach (array_keys( $submenu ) as $parent) {
325                 foreach ( $submenu[$parent] as $submenu_array ) {
326                         if ( isset( $_wp_real_parent_file[$parent] ) )
327                                 $parent = $_wp_real_parent_file[$parent];
328                         if ( $submenu_array[2] == $pagenow ) {
329                                 $parent_file = $parent;
330                                 return $parent;
331                         } else
332                                 if ( isset( $plugin_page ) && ($plugin_page == $submenu_array[2] ) ) {
333                                         $parent_file = $parent;
334                                         return $parent;
335                                 }
336                 }
337         }
338
339         $parent_file = '';
340         return '';
341 }
342
343 function get_admin_page_title() {
344         global $title;
345         global $menu;
346         global $submenu;
347         global $pagenow;
348         global $plugin_page;
349
350         if ( isset( $title ) && !empty ( $title ) ) {
351                 return $title;
352         }
353
354         $hook = get_plugin_page_hook( $plugin_page, $pagenow );
355
356         $parent = $parent1 = get_admin_page_parent();
357         if ( empty ( $parent) ) {
358                 foreach ( $menu as $menu_array ) {
359                         if ( isset( $menu_array[3] ) ) {
360                                 if ( $menu_array[2] == $pagenow ) {
361                                         $title = $menu_array[3];
362                                         return $menu_array[3];
363                                 } else
364                                         if ( isset( $plugin_page ) && ($plugin_page == $menu_array[2] ) && ($hook == $menu_array[3] ) ) {
365                                                 $title = $menu_array[3];
366                                                 return $menu_array[3];
367                                         }
368                         } else {
369                                 $title = $menu_array[0];
370                                 return $title;
371                         }
372                 }
373         } else {
374                 foreach (array_keys( $submenu ) as $parent) {
375                         foreach ( $submenu[$parent] as $submenu_array ) {
376                                 if ( isset( $plugin_page ) &&
377                                         ($plugin_page == $submenu_array[2] ) &&
378                                         (($parent == $pagenow ) || ($parent == $plugin_page ) || ($plugin_page == $hook ) || (($pagenow == 'admin.php' ) && ($parent1 != $submenu_array[2] ) ) )
379                                         ) {
380                                                 $title = $submenu_array[3];
381                                                 return $submenu_array[3];
382                                         }
383
384                                 if ( $submenu_array[2] != $pagenow || isset( $_GET['page'] ) ) // not the current page
385                                         continue;
386
387                                 if ( isset( $submenu_array[3] ) ) {
388                                         $title = $submenu_array[3];
389                                         return $submenu_array[3];
390                                 } else {
391                                         $title = $submenu_array[0];
392                                         return $title;
393                                 }
394                         }
395                 }
396         }
397
398         return $title;
399 }
400
401 function get_plugin_page_hook( $plugin_page, $parent_page ) {
402         $hook = get_plugin_page_hookname( $plugin_page, $parent_page );
403         if ( has_action($hook) )
404                 return $hook;
405         else
406                 return null;
407 }
408
409 function get_plugin_page_hookname( $plugin_page, $parent_page ) {
410         global $admin_page_hooks;
411
412         $parent = get_admin_page_parent();
413
414         if ( empty ( $parent_page ) || 'admin.php' == $parent_page ) {
415                 if ( isset( $admin_page_hooks[$plugin_page] ))
416                         $page_type = 'toplevel';
417                 else
418                         if ( isset( $admin_page_hooks[$parent] ))
419                                 $page_type = $admin_page_hooks[$parent];
420         } else
421                 if ( isset( $admin_page_hooks[$parent_page] ) ) {
422                         $page_type = $admin_page_hooks[$parent_page];
423                 } else {
424                         $page_type = 'admin';
425                 }
426
427         $plugin_name = preg_replace( '!\.php!', '', $plugin_page );
428
429         return $page_type.'_page_'.$plugin_name;
430 }
431
432 function user_can_access_admin_page() {
433         global $pagenow;
434         global $menu;
435         global $submenu;
436         global $_wp_menu_nopriv;
437         global $_wp_submenu_nopriv;
438         global $plugin_page;
439
440         $parent = get_admin_page_parent();
441
442         if ( isset( $_wp_submenu_nopriv[$parent][$pagenow] ) )
443                 return false;
444
445         if ( isset( $plugin_page ) && isset( $_wp_submenu_nopriv[$parent][$plugin_page] ) )
446                 return false;
447
448         if ( empty( $parent) ) {
449                 if ( isset( $_wp_menu_nopriv[$pagenow] ) )
450                         return false;
451                 if ( isset( $_wp_submenu_nopriv[$pagenow][$pagenow] ) )
452                         return false;
453                 if ( isset( $plugin_page ) && isset( $_wp_submenu_nopriv[$pagenow][$plugin_page] ) )
454                         return false;
455                 foreach (array_keys( $_wp_submenu_nopriv ) as $key ) {
456                         if ( isset( $_wp_submenu_nopriv[$key][$pagenow] ) )
457                                 return false;
458                         if ( isset( $plugin_page ) && isset( $_wp_submenu_nopriv[$key][$plugin_page] ) )
459                         return false;
460                 }
461                 return true;
462         }
463
464         if ( isset( $submenu[$parent] ) ) {
465                 foreach ( $submenu[$parent] as $submenu_array ) {
466                         if ( isset( $plugin_page ) && ( $submenu_array[2] == $plugin_page ) ) {
467                                 if ( current_user_can( $submenu_array[1] ))
468                                         return true;
469                                 else
470                                         return false;
471                         } else if ( $submenu_array[2] == $pagenow ) {
472                                 if ( current_user_can( $submenu_array[1] ))
473                                         return true;
474                                 else
475                                         return false;
476                         }
477                 }
478         }
479
480         foreach ( $menu as $menu_array ) {
481                 if ( $menu_array[2] == $parent) {
482                         if ( current_user_can( $menu_array[1] ))
483                                 return true;
484                         else
485                                 return false;
486                 }
487         }
488
489         return true;
490 }
491
492 ?>