Skip to content

Commit 9649a81

Browse files
Copilotswissspidy
andcommitted
Add warning when plugin is in active_plugins but file doesn't exist
Co-authored-by: swissspidy <[email protected]>
1 parent 5ec2278 commit 9649a81

File tree

2 files changed

+96
-0
lines changed

2 files changed

+96
-0
lines changed

features/plugin-is-active.feature

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
Feature: Check if a WordPress plugin is active
2+
3+
Background:
4+
Given a WP install
5+
6+
Scenario: Check if an active plugin is active
7+
When I run `wp plugin activate akismet`
8+
Then STDOUT should contain:
9+
"""
10+
Success:
11+
"""
12+
13+
When I run `wp plugin is-active akismet`
14+
Then the return code should be 0
15+
16+
Scenario: Check if an inactive plugin is not active
17+
When I run `wp plugin deactivate akismet`
18+
Then STDOUT should contain:
19+
"""
20+
Success:
21+
"""
22+
23+
When I try `wp plugin is-active akismet`
24+
Then the return code should be 1
25+
26+
Scenario: Check if a non-existent plugin is not active
27+
When I try `wp plugin is-active non-existent-plugin`
28+
Then the return code should be 1
29+
30+
Scenario: Warn when plugin is in active_plugins but file does not exist
31+
When I run `wp plugin activate akismet`
32+
Then STDOUT should contain:
33+
"""
34+
Success:
35+
"""
36+
37+
When I run `wp plugin is-active akismet`
38+
Then the return code should be 0
39+
40+
# Remove the plugin directory
41+
When I run `wp plugin path akismet --dir`
42+
Then save STDOUT as {PLUGIN_PATH}
43+
44+
When I run `rm -rf {PLUGIN_PATH}`
45+
Then the return code should be 0
46+
47+
# Now the plugin file is gone but still in active_plugins
48+
When I try `wp plugin is-active akismet`
49+
Then STDERR should contain:
50+
"""
51+
Warning: Plugin 'akismet' is in the active_plugins option but the plugin file does not exist.
52+
"""
53+
And the return code should be 1

src/Plugin_Command.php

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1371,6 +1371,49 @@ public function is_active( $args, $assoc_args ) {
13711371
$plugin = $this->fetcher->get( $args[0] );
13721372

13731373
if ( ! $plugin ) {
1374+
// Plugin not found via fetcher, but it might still be in active_plugins option
1375+
// Check if it's in the active_plugins list
1376+
$input_name = $args[0];
1377+
$active_plugins = $network_wide ? get_site_option( 'active_sitewide_plugins', [] ) : get_option( 'active_plugins', [] );
1378+
1379+
// Ensure we have an array to work with
1380+
if ( ! is_array( $active_plugins ) ) {
1381+
$active_plugins = [];
1382+
}
1383+
1384+
// For network-wide plugins, the array is keyed differently
1385+
if ( $network_wide ) {
1386+
$active_plugin_files = array_keys( $active_plugins );
1387+
} else {
1388+
$active_plugin_files = $active_plugins;
1389+
}
1390+
1391+
// Try to find a matching plugin file in active_plugins
1392+
$found_in_active = false;
1393+
foreach ( $active_plugin_files as $plugin_file ) {
1394+
// Ensure plugin_file is a string
1395+
if ( ! is_string( $plugin_file ) ) {
1396+
continue;
1397+
}
1398+
1399+
// Check if the input matches the plugin file in various ways
1400+
if ( "$input_name.php" === $plugin_file ||
1401+
$plugin_file === $input_name ||
1402+
( dirname( $plugin_file ) === $input_name && '.' !== $input_name ) ) {
1403+
$found_in_active = $plugin_file;
1404+
break;
1405+
}
1406+
}
1407+
1408+
if ( is_string( $found_in_active ) && $found_in_active ) {
1409+
// Plugin is in active_plugins but file doesn't exist
1410+
// Use validate_plugin to confirm the file is missing
1411+
$validation = validate_plugin( $found_in_active );
1412+
if ( is_wp_error( $validation ) ) {
1413+
WP_CLI::warning( "Plugin '{$input_name}' is in the active_plugins option but the plugin file does not exist." );
1414+
}
1415+
}
1416+
13741417
WP_CLI::halt( 1 );
13751418
}
13761419

0 commit comments

Comments
 (0)