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 = '') {
36 if ( ! $cache_plugins = wp_cache_get('plugins', 'plugins') )
37 $cache_plugins = array();
39 if ( isset($cache_plugins[ $plugin_folder ]) )
40 return $cache_plugins[ $plugin_folder ];
42 $wp_plugins = array ();
43 $plugin_root = WP_PLUGIN_DIR;
44 if( !empty($plugin_folder) )
45 $plugin_root .= $plugin_folder;
47 // Files in wp-content/plugins directory
48 $plugins_dir = @ opendir( $plugin_root);
50 while (($file = readdir( $plugins_dir ) ) !== false ) {
51 if ( substr($file, 0, 1) == '.' )
53 if ( is_dir( $plugin_root.'/'.$file ) ) {
54 $plugins_subdir = @ opendir( $plugin_root.'/'.$file );
55 if ( $plugins_subdir ) {
56 while (($subfile = readdir( $plugins_subdir ) ) !== false ) {
57 if ( substr($subfile, 0, 1) == '.' )
59 if ( substr($subfile, -4) == '.php' )
60 $plugin_files[] = "$file/$subfile";
64 if ( substr($file, -4) == '.php' )
65 $plugin_files[] = $file;
69 @closedir( $plugins_dir );
70 @closedir( $plugins_subdir );
72 if ( !$plugins_dir || !$plugin_files )
75 foreach ( $plugin_files as $plugin_file ) {
76 if ( !is_readable( "$plugin_root/$plugin_file" ) )
79 $plugin_data = get_plugin_data( "$plugin_root/$plugin_file" );
81 if ( empty ( $plugin_data['Name'] ) )
84 $wp_plugins[plugin_basename( $plugin_file )] = $plugin_data;
87 uasort( $wp_plugins, create_function( '$a, $b', 'return strnatcasecmp( $a["Name"], $b["Name"] );' ));
89 $cache_plugins[ $plugin_folder ] = $wp_plugins;
90 wp_cache_set('plugins', $cache_plugins, 'plugins');
95 function is_plugin_active($plugin){
96 return in_array($plugin, get_option('active_plugins'));
99 function activate_plugin($plugin, $redirect = '') {
100 $current = get_option('active_plugins');
101 $plugin = trim($plugin);
103 $valid = validate_plugin($plugin);
104 if ( is_wp_error($valid) )
107 if ( !in_array($plugin, $current) ) {
108 if ( !empty($redirect) )
109 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
111 @include(WP_PLUGIN_DIR . '/' . $plugin);
112 $current[] = $plugin;
114 update_option('active_plugins', $current);
115 do_action('activate_' . $plugin);
122 function deactivate_plugins($plugins, $silent= false) {
123 $current = get_option('active_plugins');
125 if ( !is_array($plugins) )
126 $plugins = array($plugins);
128 foreach ( $plugins as $plugin ) {
129 if( ! is_plugin_active($plugin) )
131 array_splice($current, array_search( $plugin, $current), 1 ); // Fixed Array-fu!
132 if ( ! $silent ) //Used by Plugin updater to internally deactivate plugin, however, not to notify plugins of the fact to prevent plugin output.
133 do_action('deactivate_' . trim( $plugin ));
136 update_option('active_plugins', $current);
139 function activate_plugins($plugins, $redirect = '') {
140 if ( !is_array($plugins) )
141 $plugins = array($plugins);
144 foreach ( (array) $plugins as $plugin ) {
145 if ( !empty($redirect) )
146 $redirect = add_query_arg('plugin', $plugin, $redirect);
147 $result = activate_plugin($plugin, $redirect);
148 if ( is_wp_error($result) )
149 $errors[$plugin] = $result;
152 if ( !empty($errors) )
153 return new WP_Error('plugins_invalid', __('One of the plugins is invalid.'), $errors);
158 function delete_plugins($plugins, $redirect = '' ) {
159 global $wp_filesystem;
161 if( empty($plugins) )
165 foreach( $plugins as $plugin )
166 $checked[] = 'checked[]=' . $plugin;
169 $url = wp_nonce_url('plugins.php?action=delete-selected&verify-delete=1&' . implode('&', $checked), 'bulk-manage-plugins');
170 if ( false === ($credentials = request_filesystem_credentials($url)) ) {
171 $data = ob_get_contents();
173 if( ! empty($data) ){
174 include_once( ABSPATH . 'wp-admin/admin-header.php');
176 include( ABSPATH . 'wp-admin/admin-footer.php');
182 if ( ! WP_Filesystem($credentials) ) {
183 request_filesystem_credentials($url, '', true); //Failed to connect, Error and request again
184 $data = ob_get_contents();
186 if( ! empty($data) ){
187 include_once( ABSPATH . 'wp-admin/admin-header.php');
189 include( ABSPATH . 'wp-admin/admin-footer.php');
195 if ( $wp_filesystem->errors->get_error_code() ) {
196 return $wp_filesystem->errors;
199 if ( ! is_object($wp_filesystem) )
200 return new WP_Error('fs_unavailable', __('Could not access filesystem.'));
202 if ( $wp_filesystem->errors->get_error_code() )
203 return new WP_Error('fs_error', __('Filesystem error'), $wp_filesystem->errors);
205 //Get the base plugin folder
206 $plugins_dir = $wp_filesystem->wp_plugins_dir();
207 if ( empty($plugins_dir) )
208 return new WP_Error('fs_no_plugins_dir', __('Unable to locate WordPress Plugin directory.'));
210 $plugins_dir = trailingslashit( $plugins_dir );
214 foreach( $plugins as $plugin_file ) {
215 $this_plugin_dir = trailingslashit( dirname($plugins_dir . $plugin_file) );
216 // If plugin is in its own directory, recursively delete the directory.
217 if ( strpos($plugin_file, '/') && $this_plugin_dir != $plugins_dir ) //base check on if plugin includes directory seperator AND that its not the root plugin folder
218 $deleted = $wp_filesystem->delete($this_plugin_dir, true);
220 $deleted = $wp_filesystem->delete($plugins_dir . $plugin_file);
223 $errors[] = $plugin_file;
226 if( ! empty($errors) )
227 return new WP_Error('could_not_remove_plugin', sprintf(__('Could not fully remove the plugin(s) %s'), implode(', ', $errors)) );
232 function validate_active_plugins() {
233 $check_plugins = get_option('active_plugins');
235 // Sanity check. If the active plugin list is not an array, make it an
237 if ( !is_array($check_plugins) ) {
238 update_option('active_plugins', array());
242 //Invalid is any plugin that is deactivated due to error.
245 // If a plugin file does not exist, remove it from the list of active
247 foreach ( $check_plugins as $check_plugin ) {
248 $result = validate_plugin($check_plugin);
249 if ( is_wp_error( $result ) ) {
250 $invalid[$check_plugin] = $result;
251 deactivate_plugins( $check_plugin, true);
257 function validate_plugin($plugin) {
258 if ( validate_file($plugin) )
259 return new WP_Error('plugin_invalid', __('Invalid plugin.'));
260 if ( ! file_exists(WP_PLUGIN_DIR . '/' . $plugin) )
261 return new WP_Error('plugin_not_found', __('Plugin file does not exist.'));
270 function add_menu_page( $page_title, $menu_title, $access_level, $file, $function = '' ) {
271 global $menu, $admin_page_hooks;
273 $file = plugin_basename( $file );
275 $menu[] = array ( $menu_title, $access_level, $file, $page_title );
277 $admin_page_hooks[$file] = sanitize_title( $menu_title );
279 $hookname = get_plugin_page_hookname( $file, '' );
280 if (!empty ( $function ) && !empty ( $hookname ))
281 add_action( $hookname, $function );
286 function add_submenu_page( $parent, $page_title, $menu_title, $access_level, $file, $function = '' ) {
289 global $_wp_real_parent_file;
290 global $_wp_submenu_nopriv;
292 $file = plugin_basename( $file );
294 $parent = plugin_basename( $parent);
295 if ( isset( $_wp_real_parent_file[$parent] ) )
296 $parent = $_wp_real_parent_file[$parent];
298 if ( !current_user_can( $access_level ) ) {
299 $_wp_submenu_nopriv[$parent][$file] = true;
303 // If the parent doesn't already have a submenu, add a link to the parent
304 // as the first item in the submenu. If the submenu file is the same as the
305 // parent file someone is trying to link back to the parent manually. In
306 // this case, don't automatically add a link back to avoid duplication.
307 if (!isset( $submenu[$parent] ) && $file != $parent ) {
308 foreach ( $menu as $parent_menu ) {
309 if ( $parent_menu[2] == $parent && current_user_can( $parent_menu[1] ) )
310 $submenu[$parent][] = $parent_menu;
314 $submenu[$parent][] = array ( $menu_title, $access_level, $file, $page_title );
316 $hookname = get_plugin_page_hookname( $file, $parent);
317 if (!empty ( $function ) && !empty ( $hookname ))
318 add_action( $hookname, $function );
323 function add_management_page( $page_title, $menu_title, $access_level, $file, $function = '' ) {
324 return add_submenu_page( 'edit.php', $page_title, $menu_title, $access_level, $file, $function );
327 function add_options_page( $page_title, $menu_title, $access_level, $file, $function = '' ) {
328 return add_submenu_page( 'options-general.php', $page_title, $menu_title, $access_level, $file, $function );
331 function add_theme_page( $page_title, $menu_title, $access_level, $file, $function = '' ) {
332 return add_submenu_page( 'themes.php', $page_title, $menu_title, $access_level, $file, $function );
335 function add_users_page( $page_title, $menu_title, $access_level, $file, $function = '' ) {
336 if ( current_user_can('edit_users') )
337 $parent = 'users.php';
339 $parent = 'profile.php';
340 return add_submenu_page( $parent, $page_title, $menu_title, $access_level, $file, $function );
344 // Pluggable Menu Support -- Private
347 function get_admin_page_parent() {
353 global $_wp_real_parent_file;
354 global $_wp_menu_nopriv;
355 global $_wp_submenu_nopriv;
357 if ( !empty ( $parent_file ) ) {
358 if ( isset( $_wp_real_parent_file[$parent_file] ) )
359 $parent_file = $_wp_real_parent_file[$parent_file];
364 if ( $pagenow == 'admin.php' && isset( $plugin_page ) ) {
365 foreach ( $menu as $parent_menu ) {
366 if ( $parent_menu[2] == $plugin_page ) {
367 $parent_file = $plugin_page;
368 if ( isset( $_wp_real_parent_file[$parent_file] ) )
369 $parent_file = $_wp_real_parent_file[$parent_file];
373 if ( isset( $_wp_menu_nopriv[$plugin_page] ) ) {
374 $parent_file = $plugin_page;
375 if ( isset( $_wp_real_parent_file[$parent_file] ) )
376 $parent_file = $_wp_real_parent_file[$parent_file];
381 if ( isset( $plugin_page ) && isset( $_wp_submenu_nopriv[$pagenow][$plugin_page] ) ) {
382 $parent_file = $pagenow;
383 if ( isset( $_wp_real_parent_file[$parent_file] ) )
384 $parent_file = $_wp_real_parent_file[$parent_file];
388 foreach (array_keys( $submenu ) as $parent) {
389 foreach ( $submenu[$parent] as $submenu_array ) {
390 if ( isset( $_wp_real_parent_file[$parent] ) )
391 $parent = $_wp_real_parent_file[$parent];
392 if ( $submenu_array[2] == $pagenow ) {
393 $parent_file = $parent;
396 if ( isset( $plugin_page ) && ($plugin_page == $submenu_array[2] ) ) {
397 $parent_file = $parent;
407 function get_admin_page_title() {
414 if ( isset( $title ) && !empty ( $title ) ) {
418 $hook = get_plugin_page_hook( $plugin_page, $pagenow );
420 $parent = $parent1 = get_admin_page_parent();
421 if ( empty ( $parent) ) {
422 foreach ( $menu as $menu_array ) {
423 if ( isset( $menu_array[3] ) ) {
424 if ( $menu_array[2] == $pagenow ) {
425 $title = $menu_array[3];
426 return $menu_array[3];
428 if ( isset( $plugin_page ) && ($plugin_page == $menu_array[2] ) && ($hook == $menu_array[3] ) ) {
429 $title = $menu_array[3];
430 return $menu_array[3];
433 $title = $menu_array[0];
438 foreach (array_keys( $submenu ) as $parent) {
439 foreach ( $submenu[$parent] as $submenu_array ) {
440 if ( isset( $plugin_page ) &&
441 ($plugin_page == $submenu_array[2] ) &&
442 (($parent == $pagenow ) || ($parent == $plugin_page ) || ($plugin_page == $hook ) || (($pagenow == 'admin.php' ) && ($parent1 != $submenu_array[2] ) ) )
444 $title = $submenu_array[3];
445 return $submenu_array[3];
448 if ( $submenu_array[2] != $pagenow || isset( $_GET['page'] ) ) // not the current page
451 if ( isset( $submenu_array[3] ) ) {
452 $title = $submenu_array[3];
453 return $submenu_array[3];
455 $title = $submenu_array[0];
465 function get_plugin_page_hook( $plugin_page, $parent_page ) {
466 $hook = get_plugin_page_hookname( $plugin_page, $parent_page );
467 if ( has_action($hook) )
473 function get_plugin_page_hookname( $plugin_page, $parent_page ) {
474 global $admin_page_hooks;
476 $parent = get_admin_page_parent();
478 $page_type = 'admin';
479 if ( empty ( $parent_page ) || 'admin.php' == $parent_page ) {
480 if ( isset( $admin_page_hooks[$plugin_page] ))
481 $page_type = 'toplevel';
483 if ( isset( $admin_page_hooks[$parent] ))
484 $page_type = $admin_page_hooks[$parent];
485 } else if ( isset( $admin_page_hooks[$parent_page] ) ) {
486 $page_type = $admin_page_hooks[$parent_page];
489 $plugin_name = preg_replace( '!\.php!', '', $plugin_page );
491 return $page_type.'_page_'.$plugin_name;
494 function user_can_access_admin_page() {
498 global $_wp_menu_nopriv;
499 global $_wp_submenu_nopriv;
502 $parent = get_admin_page_parent();
504 if ( isset( $_wp_submenu_nopriv[$parent][$pagenow] ) )
507 if ( isset( $plugin_page ) && isset( $_wp_submenu_nopriv[$parent][$plugin_page] ) )
510 if ( empty( $parent) ) {
511 if ( isset( $_wp_menu_nopriv[$pagenow] ) )
513 if ( isset( $_wp_submenu_nopriv[$pagenow][$pagenow] ) )
515 if ( isset( $plugin_page ) && isset( $_wp_submenu_nopriv[$pagenow][$plugin_page] ) )
517 foreach (array_keys( $_wp_submenu_nopriv ) as $key ) {
518 if ( isset( $_wp_submenu_nopriv[$key][$pagenow] ) )
520 if ( isset( $plugin_page ) && isset( $_wp_submenu_nopriv[$key][$plugin_page] ) )
526 if ( isset( $submenu[$parent] ) ) {
527 foreach ( $submenu[$parent] as $submenu_array ) {
528 if ( isset( $plugin_page ) && ( $submenu_array[2] == $plugin_page ) ) {
529 if ( current_user_can( $submenu_array[1] ))
533 } else if ( $submenu_array[2] == $pagenow ) {
534 if ( current_user_can( $submenu_array[1] ))
542 foreach ( $menu as $menu_array ) {
543 if ( $menu_array[2] == $parent) {
544 if ( current_user_can( $menu_array[1] ))