Skip to content

Commit f76fc76

Browse files
committed
Plugins: Clarify dependency notes on the Plugins screen.
Updates `WP_Plugins_List_Table::add_dependents_to_dependency_plugin_row()` to display a note matching the action blocked by a plugin's dependents: * Active plugins with active dependents note that the plugin cannot be deactivated until the plugins requiring it are deactivated. * Inactive plugins note that the plugin cannot be deleted until the plugins requiring it are deleted. * No note is displayed if neither action is blocked, including for plugins with a circular dependency. Previously the note stated that the plugin could not be deactivated or deleted until the plugins requiring it were deactivated or deleted, regardless of the plugin's state. This was inaccurate as deactivating the dependents does not allow a plugin to be deleted. The note is now displayed as an inline warning via `wp_get_admin_notice()`. The tests are updated to account for the user's capabilities and to cover the notices on the network Plugins screen separately. Developed in: WordPress#12732 Props hbhalodia, joedolson, huzaifaalmesbah, softglaze. Fixes #64493. git-svn-id: https://develop.svn.wordpress.org/trunk@65531 602fd350-edb4-49c9-b593-d223f7449a82
1 parent 322bffc commit f76fc76

2 files changed

Lines changed: 99 additions & 9 deletions

File tree

‎src/wp-admin/includes/class-wp-plugins-list-table.php‎

Lines changed: 32 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1636,7 +1636,36 @@ protected function add_dependents_to_dependency_plugin_row( $dependency ) {
16361636
return;
16371637
}
16381638

1639-
$dependency_note = __( 'Note: This plugin cannot be deactivated or deleted until the plugins that require it are deactivated or deleted.' );
1639+
$dependency_note = '';
1640+
1641+
if ( $this->screen->in_admin( 'network' ) ) {
1642+
$is_network_active = is_plugin_active_for_network( $dependency );
1643+
1644+
if ( $is_network_active && current_user_can( 'manage_network_plugins' ) && WP_Plugin_Dependencies::has_active_dependents( $dependency ) ) {
1645+
$dependency_note = __( 'Note: This plugin cannot be deactivated until the plugins that require it are deactivated.' );
1646+
} elseif ( ! $is_network_active && ! is_plugin_active( $dependency ) && current_user_can( 'delete_plugins' ) && ! WP_Plugin_Dependencies::has_circular_dependency( $dependency ) ) {
1647+
$dependency_note = __( 'Note: This plugin cannot be deleted until the plugins that require it are deleted.' );
1648+
}
1649+
} else {
1650+
$is_active = is_plugin_active( $dependency );
1651+
1652+
if ( $is_active && ! is_plugin_active_for_network( $dependency ) && current_user_can( 'deactivate_plugin', $dependency ) && WP_Plugin_Dependencies::has_active_dependents( $dependency ) ) {
1653+
$dependency_note = __( 'Note: This plugin cannot be deactivated until the plugins that require it are deactivated.' );
1654+
} elseif ( ! $is_active && ! is_multisite() && current_user_can( 'delete_plugins' ) && ! WP_Plugin_Dependencies::has_circular_dependency( $dependency ) ) {
1655+
$dependency_note = __( 'Note: This plugin cannot be deleted until the plugins that require it are deleted.' );
1656+
}
1657+
}
1658+
1659+
$dependency_notice = '';
1660+
if ( '' !== $dependency_note ) {
1661+
$dependency_notice = wp_get_admin_notice(
1662+
$dependency_note,
1663+
array(
1664+
'type' => 'warning',
1665+
'additional_classes' => array( 'inline' ),
1666+
)
1667+
);
1668+
}
16401669

16411670
$comma = wp_get_list_item_separator();
16421671
$required_by = sprintf(
@@ -1646,9 +1675,9 @@ protected function add_dependents_to_dependency_plugin_row( $dependency ) {
16461675
);
16471676

16481677
printf(
1649-
'<div class="required-by"><p>%1$s</p><p>%2$s</p></div>',
1678+
'<div class="required-by"><p>%1$s</p>%2$s</div>',
16501679
$required_by,
1651-
$dependency_note
1680+
$dependency_notice
16521681
);
16531682
}
16541683

‎tests/phpunit/tests/admin/wpPluginsListTableRequiredByRow.php‎

Lines changed: 67 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,12 @@
1212
*/
1313
class Tests_Admin_wpPluginsListTableRequiredByRow extends WP_PluginDependencies_UnitTestCase {
1414

15+
protected static $admin_id;
16+
17+
public static function wpSetUpBeforeClass( WP_UnitTest_Factory $factory ) {
18+
self::$admin_id = $factory->user->create( array( 'role' => 'administrator' ) );
19+
}
20+
1521
/**
1622
* Tests that nothing is output for a plugin that no plugin requires.
1723
*
@@ -24,7 +30,7 @@ public function test_should_output_nothing_without_dependents() {
2430
}
2531

2632
/**
27-
* Tests the row for one plugin that requires the dependency.
33+
* Tests the row for one deactivated plugin that requires the dependency.
2834
*
2935
* @ticket 65819
3036
*/
@@ -36,14 +42,65 @@ public function test_should_list_dependent() {
3642
'unrelated/unrelated.php' => array( 'other' ),
3743
)
3844
);
45+
wp_set_current_user( self::$admin_id );
46+
47+
$notice = '';
48+
if ( ! is_multisite() ) {
49+
$notice = '<div class="notice notice-warning inline"><p>Note: This plugin cannot be deleted until the plugins that require it are deleted.</p></div>';
50+
}
3951

4052
$this->assertSame(
4153
'<div class="required-by"><p><strong>Required by:</strong> Alpha Plugin, Zeta Plugin</p>'
42-
. '<p>Note: This plugin cannot be deactivated or deleted until the plugins that require it are deactivated or deleted.</p></div>',
54+
. $notice . '</div>',
4355
$this->get_row( 'dependency/dependency.php' )
4456
);
4557
}
4658

59+
/**
60+
* Tests the deletion notice on the network Plugins screen.
61+
*
62+
* @ticket 65819
63+
* @group ms-required
64+
*
65+
* @dataProvider data_should_show_network_deletion_notice_only_to_super_admins
66+
*
67+
* @param bool $is_super_admin Whether the current user is a super admin.
68+
*/
69+
public function test_should_show_network_deletion_notice_only_to_super_admins( $is_super_admin ) {
70+
$this->skipWithoutMultisite();
71+
$this->set_dependencies( array( 'dependent/dependent.php' => array( 'dependency' ) ) );
72+
wp_set_current_user( self::$admin_id );
73+
74+
if ( $is_super_admin ) {
75+
$this->assertTrue( grant_super_admin( self::$admin_id ) );
76+
}
77+
78+
$this->assertSame( $is_super_admin, current_user_can( 'delete_plugins' ) );
79+
80+
$notice = '';
81+
if ( $is_super_admin ) {
82+
$notice = '<div class="notice notice-warning inline"><p>Note: This plugin cannot be deleted until the plugins that require it are deleted.</p></div>';
83+
}
84+
85+
$this->assertSame(
86+
'<div class="required-by"><p><strong>Required by:</strong> Dependent Plugin</p>'
87+
. $notice . '</div>',
88+
$this->get_row( 'dependency/dependency.php', 'plugins-network' )
89+
);
90+
}
91+
92+
/**
93+
* Provides users with and without network plugin deletion permissions.
94+
*
95+
* @return array[]
96+
*/
97+
public static function data_should_show_network_deletion_notice_only_to_super_admins() {
98+
return array(
99+
'site administrator' => array( false ),
100+
'super admin' => array( true ),
101+
);
102+
}
103+
47104
/**
48105
* Tests that a single-file plugin is matched by its slug.
49106
*
@@ -78,23 +135,27 @@ public static function data_single_file_plugins() {
78135
* @param array $dependencies The dependency slugs of each plugin, keyed by plugin file.
79136
*/
80137
private function set_dependencies( $dependencies ) {
81-
$plugins = array();
138+
$plugins = array();
139+
$dependent_slugs = array();
82140
foreach ( array_keys( $dependencies ) as $file ) {
83-
$plugins[ $file ] = array( 'Name' => ucfirst( dirname( $file ) ) . ' Plugin' );
141+
$plugins[ $file ] = array( 'Name' => ucfirst( dirname( $file ) ) . ' Plugin' );
142+
$dependent_slugs[ $file ] = dirname( $file );
84143
}
85144

86145
$this->set_property_value( 'plugins', $plugins );
87146
$this->set_property_value( 'dependencies', $dependencies );
147+
$this->set_property_value( 'dependent_slugs', $dependent_slugs );
88148
}
89149

90150
/**
91151
* Returns the "Required by" row of a plugin.
92152
*
93153
* @param string $plugin_file The plugin file.
154+
* @param string $screen The Plugins screen.
94155
* @return string The output.
95156
*/
96-
private function get_row( $plugin_file ) {
97-
$table = _get_list_table( 'WP_Plugins_List_Table', array( 'screen' => 'plugins' ) );
157+
private function get_row( $plugin_file, $screen = 'plugins' ) {
158+
$table = _get_list_table( 'WP_Plugins_List_Table', array( 'screen' => $screen ) );
98159

99160
$method = new ReflectionMethod( $table, 'add_dependents_to_dependency_plugin_row' );
100161
if ( PHP_VERSION_ID < 80100 ) {

0 commit comments

Comments
 (0)