Skip to content

Commit 7a5c9b0

Browse files
committed
Ensure feature can be activated when missing dependencies
1 parent 880a160 commit 7a5c9b0

File tree

1 file changed

+18
-10
lines changed

1 file changed

+18
-10
lines changed

‎includes/admin/plugins.php‎

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@ function perflab_render_plugins_ui() {
158158
* @see perflab_install_and_activate_plugin()
159159
*
160160
* @param array{name: string, slug: string, short_description: string, requires_php: string|false, requires: string|false, requires_plugins: string[], version: string} $plugin_data Plugin data from the WordPress.org API.
161-
* @return array{compatible_php: bool, compatible_wp: bool, can_install: bool, can_activate: bool} Availability.
161+
* @return array{compatible_php: bool, compatible_wp: bool, can_install: bool, can_activate: bool, activated: bool, installed: bool} Availability.
162162
*/
163163
function perflab_get_plugin_availability( array $plugin_data ): array {
164164
$availability = array(
@@ -174,16 +174,21 @@ function perflab_get_plugin_availability( array $plugin_data ): array {
174174

175175
$plugin_status = install_plugin_install_status( $plugin_data );
176176

177+
$availability['installed'] = ( 'install' !== $plugin_status['status'] );
178+
$availability['activated'] = $plugin_status['file'] && is_plugin_active( $plugin_status['file'] );
179+
177180
// The plugin is already installed or the user can install plugins.
178181
$availability['can_install'] = (
179-
'install' !== $plugin_status['status'] ||
182+
$availability['installed'] ||
180183
current_user_can( 'install_plugins' )
181184
);
182185

183-
// The plugin is installed and the user can activate it, or the user can activate plugins in general.
186+
// The plugin is activated or the user can activate plugins.
184187
$availability['can_activate'] = (
185-
( $plugin_status['file'] && current_user_can( 'activate_plugin', $plugin_status['file'] ) ) ||
186-
current_user_can( 'activate_plugins' )
188+
$availability['activated'] ||
189+
$plugin_status['file'] // When not false, the plugin is installed.
190+
? current_user_can( 'activate_plugin', $plugin_status['file'] )
191+
: current_user_can( 'activate_plugins' )
187192
);
188193

189194
foreach ( $plugin_data['requires_plugins'] as $requires_plugin ) {
@@ -193,7 +198,7 @@ function perflab_get_plugin_availability( array $plugin_data ): array {
193198
}
194199

195200
$dependency_availability = perflab_get_plugin_availability( $dependency_plugin_data );
196-
foreach ( array( 'compatible_php', 'compatible_wp', 'can_install', 'can_activate' ) as $key ) {
201+
foreach ( array( 'compatible_php', 'compatible_wp', 'can_install', 'can_activate', 'installed', 'activated' ) as $key ) {
197202
$availability[ $key ] = $availability[ $key ] && $dependency_availability[ $key ];
198203
}
199204
}
@@ -298,14 +303,17 @@ function perflab_render_plugin_card( array $plugin_data ) {
298303

299304
$action_links = array();
300305

301-
$status = install_plugin_install_status( $plugin_data );
302-
303-
if ( is_plugin_active( $status['file'] ) ) {
306+
if ( $availability['activated'] ) {
304307
$action_links[] = sprintf(
305308
'<button type="button" class="button button-disabled" disabled="disabled">%s</button>',
306309
esc_html( _x( 'Active', 'plugin', 'default' ) )
307310
);
308-
} elseif ( ! in_array( false, $availability, true ) ) {
311+
} elseif (
312+
$availability['compatible_php'] &&
313+
$availability['compatible_wp'] &&
314+
$availability['can_install'] &&
315+
$availability['can_activate']
316+
) {
309317
$url = esc_url_raw(
310318
add_query_arg(
311319
array(

0 commit comments

Comments
 (0)