Skip to content

Commit 25c033e

Browse files
authored
Merge pull request #608 from WordPress/fix/597-filter-active-plugins
Improve `active_plugins` filter
2 parents 9f1b19f + 106767c commit 25c033e

3 files changed

Lines changed: 79 additions & 27 deletions

File tree

includes/Checker/Abstract_Check_Runner.php

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
use Exception;
1111
use WordPress\Plugin_Check\Checker\Preparations\Universal_Runtime_Preparation;
1212
use WordPress\Plugin_Check\Utilities\Plugin_Request_Utility;
13+
use WP_CLI;
1314

1415
/**
1516
* Abstract Check Runner class.
@@ -293,9 +294,17 @@ final public function set_categories( $categories ) {
293294
final public function prepare() {
294295
$cleanup_functions = array();
295296

296-
if ( $this->has_runtime_check( $this->get_checks_to_run() ) ) {
297-
$preparation = new Universal_Runtime_Preparation( $this->get_check_context() );
298-
$cleanup_functions[] = $preparation->prepare();
297+
try {
298+
if ( $this->has_runtime_check( $this->get_checks_to_run() ) ) {
299+
$preparation = new Universal_Runtime_Preparation( $this->get_check_context() );
300+
$cleanup_functions[] = $preparation->prepare();
301+
}
302+
} catch ( Exception $error ) {
303+
if ( defined( 'WP_CLI' ) && WP_CLI ) {
304+
WP_CLI::error( $error->getMessage() );
305+
} else {
306+
throw $error;
307+
}
299308
}
300309

301310
if ( $this->delete_plugin_folder ) {

includes/Checker/Preparations/Force_Single_Plugin_Preparation.php

Lines changed: 50 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
use Exception;
1111
use WordPress\Plugin_Check\Checker\Preparation;
12+
use WP_Plugin_Dependencies;
1213

1314
/**
1415
* Class for the preparation to force the plugin to be checked as the only active plugin.
@@ -20,7 +21,7 @@
2021
class Force_Single_Plugin_Preparation implements Preparation {
2122

2223
/**
23-
* Plugin slug.
24+
* Plugin slug of the plugin to check.
2425
*
2526
* @since 1.0.0
2627
* @var string
@@ -74,39 +75,66 @@ public function prepare() {
7475
}
7576

7677
/**
77-
* Filter active plugins.
78+
* Filters active plugins to only include required ones.
79+
*
80+
* This means:
81+
*
82+
* * The plugin being tested
83+
* * All dependencies of the plugin being tested
84+
* * Plugin Check itself
85+
* * All plugins depending on Plugin Check (they could be adding new checks)
7886
*
7987
* @since 1.0.0
88+
* @since 1.2.0 Now includes dependencies and dependents.
8089
*
81-
* @param array $active_plugins List of active plugins.
82-
* @return array List of active plugins.
90+
* @param mixed $active_plugins List of active plugins.
91+
* @return mixed List of active plugins.
8392
*/
8493
public function filter_active_plugins( $active_plugins ) {
85-
if ( is_array( $active_plugins ) && in_array( $this->plugin_basename, $active_plugins, true ) ) {
94+
if ( ! is_array( $active_plugins ) ) {
95+
return $active_plugins;
96+
}
8697

87-
if ( defined( 'WP_PLUGIN_CHECK_MAIN_FILE' ) ) {
88-
$plugin_check_file = WP_PLUGIN_CHECK_MAIN_FILE;
89-
} else {
90-
$plugins_dir = defined( 'WP_PLUGIN_DIR' ) ? WP_PLUGIN_DIR : WP_CONTENT_DIR . '/plugins';
91-
$plugin_check_file = $plugins_dir . '/plugin-check/plugin.php';
92-
}
98+
// The plugin being tested isn't actually active yet.
99+
if ( ! in_array( $this->plugin_basename, $active_plugins, true ) ) {
100+
return $active_plugins;
101+
}
93102

94-
$plugin_base_file = plugin_basename( $plugin_check_file );
103+
if ( defined( 'WP_PLUGIN_CHECK_MAIN_FILE' ) ) {
104+
$plugin_check_file = WP_PLUGIN_CHECK_MAIN_FILE;
105+
} else {
106+
$plugin_check_file = basename( dirname( __DIR__, 3 ) ) . '/plugin.php';
107+
}
95108

96-
// If the plugin-check is the only available plugin then return that one only.
97-
if ( $this->plugin_basename === $plugin_base_file ) {
109+
$plugin_check_basename = plugin_basename( $plugin_check_file );
98110

99-
return array(
100-
$plugin_base_file,
101-
);
102-
}
111+
$new_active_plugins = array(
112+
$this->plugin_basename, // Plugin to test.
113+
$plugin_check_basename, // Plugin Check itself.
114+
);
103115

104-
return array(
105-
$this->plugin_basename,
106-
$plugin_base_file,
116+
// Plugin dependencies support was added in WordPress 6.5.
117+
if ( class_exists( 'WP_Plugin_Dependencies' ) ) {
118+
WP_Plugin_Dependencies::initialize();
119+
120+
$new_active_plugins = array_merge(
121+
$new_active_plugins,
122+
WP_Plugin_Dependencies::get_dependencies( $this->plugin_basename )
123+
);
124+
125+
$new_active_plugins = array_merge(
126+
$new_active_plugins,
127+
// Include any dependents of Plugin Check, but only if they were already active.
128+
array_filter(
129+
WP_Plugin_Dependencies::get_dependents( dirname( $plugin_check_basename ) ),
130+
static function ( $dependent ) use ( $active_plugins ) {
131+
return in_array( $dependent, $active_plugins, true );
132+
}
133+
)
107134
);
108135
}
109136

110-
return $active_plugins;
137+
// Removes duplicates, for example if Plugin Check is the plugin being tested.
138+
return array_unique( $new_active_plugins );
111139
}
112140
}

tests/behat/features/plugin-check.feature

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -440,6 +440,20 @@ Feature: Test that the WP-CLI command works.
440440
Given a WP install with the Plugin Check plugin
441441
And a Plugin Check add-on being installed
442442

443+
And a wp-content/plugins/foo-dependency/foo-dependency.php file:
444+
"""
445+
<?php
446+
/**
447+
* Plugin Name: Foo Dependency
448+
* Plugin URI: https://example.com
449+
* Description: Sample plugin.
450+
* Version: 0.1.0
451+
* Author: WordPress Performance Team
452+
* Author URI: https://make.wordpress.org/performance/
453+
* License: GPL-2.0+
454+
* License URI: https://www.gnu.org/licenses/gpl-2.0.txt
455+
*/
456+
"""
443457
And a wp-content/plugins/foo-sample/foo-sample.php file:
444458
"""
445459
<?php
@@ -451,7 +465,8 @@ Feature: Test that the WP-CLI command works.
451465
* Author: WordPress Performance Team
452466
* Author URI: https://make.wordpress.org/performance/
453467
* License: GPL-2.0+
454-
* License URI: http://www.gnu.org/licenses/gpl-2.0.txt
468+
* License URI: https://www.gnu.org/licenses/gpl-2.0.txt
469+
* Requires Plugins: foo-dependency
455470
*/
456471
457472
// This should trigger the error.
@@ -462,7 +477,7 @@ Feature: Test that the WP-CLI command works.
462477
}
463478
);
464479
"""
465-
And I run the WP-CLI command `plugin activate foo-sample`
480+
And I run the WP-CLI command `plugin activate foo-dependency foo-sample`
466481

467482
# Running runtime checks, including the one from pcp-addon
468483
When I run the WP-CLI command `plugin check foo-sample --fields=code,type --format=csv --require=./wp-content/plugins/plugin-check/cli.php`

0 commit comments

Comments
 (0)