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 );
11 if ( preg_match( "|Version:(.*)|i", $plugin_data, $version ))
12 $version = trim( $version[1] );
16 $description = wptexturize( trim( $description[1] ));
18 $name = $plugin_name[1];
19 $name = trim( $name );
21 if ('' != trim($plugin_uri[1]) && '' != $name ) {
22 $plugin = '<a href="' . trim( $plugin_uri[1] ) . '" title="'.__( 'Visit plugin homepage' ).'">'.$plugin.'</a>';
25 if ('' == $author_uri[1] ) {
26 $author = trim( $author_name[1] );
28 $author = '<a href="' . trim( $author_uri[1] ) . '" title="'.__( 'Visit author homepage' ).'">' . trim( $author_name[1] ) . '</a>';
31 return array('Name' => $name, 'Title' => $plugin, 'Description' => $description, 'Author' => $author, 'Version' => $version);
34 function get_plugins($plugin_folder = '') {
37 if ( isset( $wp_plugins ) ) {
41 $wp_plugins = array ();
42 $plugin_root = ABSPATH . PLUGINDIR;
43 if( !empty($plugin_folder) )
44 $plugin_root .= $plugin_folder;
46 // Files in wp-content/plugins directory
47 $plugins_dir = @ opendir( $plugin_root);
49 while (($file = readdir( $plugins_dir ) ) !== false ) {
50 if ( substr($file, 0, 1) == '.' )
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) == '.' )
58 if ( substr($subfile, -4) == '.php' )
59 $plugin_files[] = "$file/$subfile";
63 if ( substr($file, -4) == '.php' )
64 $plugin_files[] = $file;
68 @closedir( $plugins_dir );
69 @closedir( $plugins_subdir );
71 if ( !$plugins_dir || !$plugin_files )
74 foreach ( $plugin_files as $plugin_file ) {
75 if ( !is_readable( "$plugin_root/$plugin_file" ) )
78 $plugin_data = get_plugin_data( "$plugin_root/$plugin_file" );
80 if ( empty ( $plugin_data['Name'] ) )
83 $wp_plugins[plugin_basename( $plugin_file )] = $plugin_data;
86 uasort( $wp_plugins, create_function( '$a, $b', 'return strnatcasecmp( $a["Name"], $b["Name"] );' ));
91 function is_plugin_active($plugin){
92 return in_array($plugin, get_option('active_plugins'));
95 function activate_plugin($plugin, $redirect = '') {
96 $current = get_option('active_plugins');
97 $plugin = trim($plugin);
99 $valid = validate_plugin($plugin);
100 if ( is_wp_error($valid) )
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
107 @include(ABSPATH . PLUGINDIR . '/' . $plugin);
108 $current[] = $plugin;
110 update_option('active_plugins', $current);
111 do_action('activate_' . $plugin);
118 function deactivate_plugins($plugins, $silent= false) {
119 $current = get_option('active_plugins');
121 if ( !is_array($plugins) )
122 $plugins = array($plugins);
124 foreach ( $plugins as $plugin ) {
125 if( ! is_plugin_active($plugin) )
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 ));
132 update_option('active_plugins', $current);
135 function deactivate_all_plugins() {
136 $current = get_option('active_plugins');
137 if ( empty($current) )
140 deactivate_plugins($current);
142 update_option('deactivated_plugins', $current);
145 function reactivate_all_plugins($redirect = '') {
146 $plugins = get_option('deactivated_plugins');
148 if ( empty($plugins) )
151 if ( !empty($redirect) )
152 wp_redirect(add_query_arg('_error_nonce', wp_create_nonce('plugin-activation-error_' . $plugin), $redirect));
155 foreach ( (array) $plugins as $plugin ) {
156 $result = activate_plugin($plugin);
157 if ( is_wp_error($result) )
158 $errors[$plugin] = $result;
161 delete_option('deactivated_plugins');
163 if ( !empty($errors) )
164 return new WP_Error('plugins_invalid', __('One of the plugins is invalid.'), $errors);
169 function validate_active_plugins() {
170 $check_plugins = get_option('active_plugins');
172 // Sanity check. If the active plugin list is not an array, make it an
174 if ( !is_array($check_plugins) ) {
175 update_option('active_plugins', array());
179 // If a plugin file does not exist, remove it from the list of active
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);
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.'));
206 function add_menu_page( $page_title, $menu_title, $access_level, $file, $function = '' ) {
207 global $menu, $admin_page_hooks;
209 $file = plugin_basename( $file );
211 $menu[] = array ( $menu_title, $access_level, $file, $page_title );
213 $admin_page_hooks[$file] = sanitize_title( $menu_title );
215 $hookname = get_plugin_page_hookname( $file, '' );
216 if (!empty ( $function ) && !empty ( $hookname ))
217 add_action( $hookname, $function );
222 function add_submenu_page( $parent, $page_title, $menu_title, $access_level, $file, $function = '' ) {
225 global $_wp_real_parent_file;
226 global $_wp_submenu_nopriv;
228 $file = plugin_basename( $file );
230 $parent = plugin_basename( $parent);
231 if ( isset( $_wp_real_parent_file[$parent] ) )
232 $parent = $_wp_real_parent_file[$parent];
234 if ( !current_user_can( $access_level ) ) {
235 $_wp_submenu_nopriv[$parent][$file] = true;
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;
250 $submenu[$parent][] = array ( $menu_title, $access_level, $file, $page_title );
252 $hookname = get_plugin_page_hookname( $file, $parent);
253 if (!empty ( $function ) && !empty ( $hookname ))
254 add_action( $hookname, $function );
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 );
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 );
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 );
271 function add_users_page( $page_title, $menu_title, $access_level, $file, $function = '' ) {
272 if ( current_user_can('edit_users') )
273 $parent = 'users.php';
275 $parent = 'profile.php';
276 return add_submenu_page( $parent, $page_title, $menu_title, $access_level, $file, $function );
280 // Pluggable Menu Support -- Private
283 function get_admin_page_parent() {
289 global $_wp_real_parent_file;
290 global $_wp_menu_nopriv;
291 global $_wp_submenu_nopriv;
293 if ( !empty ( $parent_file ) ) {
294 if ( isset( $_wp_real_parent_file[$parent_file] ) )
295 $parent_file = $_wp_real_parent_file[$parent_file];
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];
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];
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];
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;
332 if ( isset( $plugin_page ) && ($plugin_page == $submenu_array[2] ) ) {
333 $parent_file = $parent;
343 function get_admin_page_title() {
350 if ( isset( $title ) && !empty ( $title ) ) {
354 $hook = get_plugin_page_hook( $plugin_page, $pagenow );
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];
364 if ( isset( $plugin_page ) && ($plugin_page == $menu_array[2] ) && ($hook == $menu_array[3] ) ) {
365 $title = $menu_array[3];
366 return $menu_array[3];
369 $title = $menu_array[0];
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] ) ) )
380 $title = $submenu_array[3];
381 return $submenu_array[3];
384 if ( $submenu_array[2] != $pagenow || isset( $_GET['page'] ) ) // not the current page
387 if ( isset( $submenu_array[3] ) ) {
388 $title = $submenu_array[3];
389 return $submenu_array[3];
391 $title = $submenu_array[0];
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) )
409 function get_plugin_page_hookname( $plugin_page, $parent_page ) {
410 global $admin_page_hooks;
412 $parent = get_admin_page_parent();
414 if ( empty ( $parent_page ) || 'admin.php' == $parent_page ) {
415 if ( isset( $admin_page_hooks[$plugin_page] ))
416 $page_type = 'toplevel';
418 if ( isset( $admin_page_hooks[$parent] ))
419 $page_type = $admin_page_hooks[$parent];
421 if ( isset( $admin_page_hooks[$parent_page] ) ) {
422 $page_type = $admin_page_hooks[$parent_page];
424 $page_type = 'admin';
427 $plugin_name = preg_replace( '!\.php!', '', $plugin_page );
429 return $page_type.'_page_'.$plugin_name;
432 function user_can_access_admin_page() {
436 global $_wp_menu_nopriv;
437 global $_wp_submenu_nopriv;
440 $parent = get_admin_page_parent();
442 if ( isset( $_wp_submenu_nopriv[$parent][$pagenow] ) )
445 if ( isset( $plugin_page ) && isset( $_wp_submenu_nopriv[$parent][$plugin_page] ) )
448 if ( empty( $parent) ) {
449 if ( isset( $_wp_menu_nopriv[$pagenow] ) )
451 if ( isset( $_wp_submenu_nopriv[$pagenow][$pagenow] ) )
453 if ( isset( $plugin_page ) && isset( $_wp_submenu_nopriv[$pagenow][$plugin_page] ) )
455 foreach (array_keys( $_wp_submenu_nopriv ) as $key ) {
456 if ( isset( $_wp_submenu_nopriv[$key][$pagenow] ) )
458 if ( isset( $plugin_page ) && isset( $_wp_submenu_nopriv[$key][$plugin_page] ) )
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] ))
471 } else if ( $submenu_array[2] == $pagenow ) {
472 if ( current_user_can( $submenu_array[1] ))
480 foreach ( $menu as $menu_array ) {
481 if ( $menu_array[2] == $parent) {
482 if ( current_user_can( $menu_array[1] ))