Plugins: Introduce singular capabilities for activating and deactivating individual plugins.

This introduces the following meta capabilities:

* `activate_plugin`
* `deactivate_plugin`
* `deactivate_plugins`

The singular `activate_plugin` and `deactivate_plugin` capabilities are used along with the corresponding plugin name when
determining whether or not a user can activate or deactivate an individual plugin.

The plural `deactivate_plugins` capability is used in place of the existing `activate_plugins` capability when determining
whether a user can deactivate plugins.

Each of these new meta capabilities map to the existing `activate_plugins` primitive capability, which means there is no
change in existing behaviour, but plugins can now filter the capabilities required to activate and deactivate individual
plugins.

Fixes #38652 


git-svn-id: https://develop.svn.wordpress.org/trunk@41290 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
John Blackbourn
2017-08-22 14:01:36 +00:00
parent 8df2151660
commit 9990abec14
8 changed files with 43 additions and 16 deletions
+23 -7
View File
@@ -29,8 +29,9 @@ if ( $action ) {
switch ( $action ) {
case 'activate':
if ( ! current_user_can('activate_plugins') )
wp_die(__('Sorry, you are not allowed to activate plugins for this site.'));
if ( ! current_user_can( 'activate_plugin', $plugin ) ) {
wp_die( __( 'Sorry, you are not allowed to activate this plugin.' ) );
}
if ( is_multisite() && ! is_network_admin() && is_network_only_plugin( $plugin ) ) {
wp_redirect( self_admin_url("plugins.php?plugin_status=$status&paged=$page&s=$s") );
@@ -88,6 +89,10 @@ if ( $action ) {
if ( is_plugin_active( $plugin ) || ( is_multisite() && is_network_only_plugin( $plugin ) ) ) {
unset( $plugins[ $i ] );
}
// Only activate plugins which the user can activate.
if ( ! current_user_can( 'activate_plugin', $plugin ) ) {
unset( $plugins[ $i ] );
}
}
}
@@ -146,8 +151,9 @@ if ( $action ) {
exit;
case 'error_scrape':
if ( ! current_user_can('activate_plugins') )
wp_die(__('Sorry, you are not allowed to activate plugins for this site.'));
if ( ! current_user_can( 'activate_plugin', $plugin ) ) {
wp_die( __( 'Sorry, you are not allowed to activate this plugin.' ) );
}
check_admin_referer('plugin-activation-error_' . $plugin);
@@ -167,8 +173,9 @@ if ( $action ) {
exit;
case 'deactivate':
if ( ! current_user_can('activate_plugins') )
wp_die(__('Sorry, you are not allowed to deactivate plugins for this site.'));
if ( ! current_user_can( 'deactivate_plugin', $plugin ) ) {
wp_die( __( 'Sorry, you are not allowed to deactivate this plugin.' ) );
}
check_admin_referer('deactivate-plugin_' . $plugin);
@@ -192,8 +199,9 @@ if ( $action ) {
exit;
case 'deactivate-selected':
if ( ! current_user_can('activate_plugins') )
if ( ! current_user_can( 'deactivate_plugins' ) ) {
wp_die(__('Sorry, you are not allowed to deactivate plugins for this site.'));
}
check_admin_referer('bulk-plugins');
@@ -204,6 +212,14 @@ if ( $action ) {
} else {
$plugins = array_filter( $plugins, 'is_plugin_active' );
$plugins = array_diff( $plugins, array_filter( $plugins, 'is_plugin_active_for_network' ) );
foreach ( $plugins as $i => $plugin ) {
// Only deactivate plugins which the user can deactivate.
if ( ! current_user_can( 'deactivate_plugin', $plugin ) ) {
unset( $plugins[ $i ] );
}
}
}
if ( empty($plugins) ) {
wp_redirect( self_admin_url("plugins.php?plugin_status=$status&paged=$page&s=$s") );