Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve active_plugins filter #608

Merged
merged 10 commits into from
Sep 5, 2024
15 changes: 12 additions & 3 deletions includes/Checker/Abstract_Check_Runner.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use Exception;
use WordPress\Plugin_Check\Checker\Preparations\Universal_Runtime_Preparation;
use WordPress\Plugin_Check\Utilities\Plugin_Request_Utility;
use WP_CLI;

/**
* Abstract Check Runner class.
Expand Down Expand Up @@ -293,9 +294,17 @@
final public function prepare() {
$cleanup_functions = array();

if ( $this->has_runtime_check( $this->get_checks_to_run() ) ) {
$preparation = new Universal_Runtime_Preparation( $this->get_check_context() );
$cleanup_functions[] = $preparation->prepare();
try {
if ( $this->has_runtime_check( $this->get_checks_to_run() ) ) {
$preparation = new Universal_Runtime_Preparation( $this->get_check_context() );
$cleanup_functions[] = $preparation->prepare();
}
} catch ( Exception $error ) {
if ( defined( 'WP_CLI' ) && WP_CLI ) {
WP_CLI::error( $error->getMessage() );
} else {
throw $error;

Check warning on line 306 in includes/Checker/Abstract_Check_Runner.php

View check run for this annotation

Codecov / codecov/patch

includes/Checker/Abstract_Check_Runner.php#L306

Added line #L306 was not covered by tests
}
}

if ( $this->delete_plugin_folder ) {
Expand Down
72 changes: 50 additions & 22 deletions includes/Checker/Preparations/Force_Single_Plugin_Preparation.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

use Exception;
use WordPress\Plugin_Check\Checker\Preparation;
use WP_Plugin_Dependencies;

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

/**
* Plugin slug.
* Plugin slug of the plugin to check.
*
* @since 1.0.0
* @var string
Expand Down Expand Up @@ -74,39 +75,66 @@ public function prepare() {
}

/**
* Filter active plugins.
* Filters active plugins to only include required ones.
*
* This means:
*
* * The plugin being tested
* * All dependencies of the plugin being tested
* * Plugin Check itself
* * All plugins depending on Plugin Check (they could be adding new checks)
*
* @since 1.0.0
swissspidy marked this conversation as resolved.
Show resolved Hide resolved
* @since 1.2.0 Now includes dependencies and dependents.
*
* @param array $active_plugins List of active plugins.
* @return array List of active plugins.
* @param mixed $active_plugins List of active plugins.
* @return mixed List of active plugins.
*/
public function filter_active_plugins( $active_plugins ) {
if ( is_array( $active_plugins ) && in_array( $this->plugin_basename, $active_plugins, true ) ) {
if ( ! is_array( $active_plugins ) ) {
return $active_plugins;
}

if ( defined( 'WP_PLUGIN_CHECK_MAIN_FILE' ) ) {
$plugin_check_file = WP_PLUGIN_CHECK_MAIN_FILE;
} else {
$plugins_dir = defined( 'WP_PLUGIN_DIR' ) ? WP_PLUGIN_DIR : WP_CONTENT_DIR . '/plugins';
$plugin_check_file = $plugins_dir . '/plugin-check/plugin.php';
}
// The plugin being tested isn't actually active yet.
if ( ! in_array( $this->plugin_basename, $active_plugins, true ) ) {
return $active_plugins;
}

$plugin_base_file = plugin_basename( $plugin_check_file );
if ( defined( 'WP_PLUGIN_CHECK_MAIN_FILE' ) ) {
$plugin_check_file = WP_PLUGIN_CHECK_MAIN_FILE;
} else {
$plugin_check_file = basename( dirname( __DIR__, 3 ) ) . '/plugin.php';
}

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

return array(
$plugin_base_file,
);
}
$new_active_plugins = array(
$this->plugin_basename, // Plugin to test.
$plugin_check_basename, // Plugin Check itself.
);

return array(
$this->plugin_basename,
$plugin_base_file,
// Plugin dependencies support was added in WordPress 6.5.
if ( class_exists( 'WP_Plugin_Dependencies' ) ) {
WP_Plugin_Dependencies::initialize();

$new_active_plugins = array_merge(
$new_active_plugins,
WP_Plugin_Dependencies::get_dependencies( $this->plugin_basename )
);

$new_active_plugins = array_merge(
$new_active_plugins,
// Include any dependents of Plugin Check, but only if they were already active.
array_filter(
WP_Plugin_Dependencies::get_dependents( dirname( $plugin_check_basename ) ),
swissspidy marked this conversation as resolved.
Show resolved Hide resolved
static function ( $dependent ) use ( $active_plugins ) {
return in_array( $dependent, $active_plugins, true );
}
)
);
}

return $active_plugins;
// Removes duplicates, for example if Plugin Check is the plugin being tested.
return array_unique( $new_active_plugins );
}
}
19 changes: 17 additions & 2 deletions tests/behat/features/plugin-check.feature
Original file line number Diff line number Diff line change
Expand Up @@ -440,6 +440,20 @@ Feature: Test that the WP-CLI command works.
Given a WP install with the Plugin Check plugin
And a Plugin Check add-on being installed

And a wp-content/plugins/foo-dependency/foo-dependency.php file:
"""
<?php
/**
* Plugin Name: Foo Dependency
* Plugin URI: https://example.com
* Description: Sample plugin.
* Version: 0.1.0
* Author: WordPress Performance Team
* Author URI: https://make.wordpress.org/performance/
* License: GPL-2.0+
* License URI: https://www.gnu.org/licenses/gpl-2.0.txt
*/
"""
And a wp-content/plugins/foo-sample/foo-sample.php file:
"""
<?php
Expand All @@ -451,7 +465,8 @@ Feature: Test that the WP-CLI command works.
* Author: WordPress Performance Team
* Author URI: https://make.wordpress.org/performance/
* License: GPL-2.0+
* License URI: http://www.gnu.org/licenses/gpl-2.0.txt
* License URI: https://www.gnu.org/licenses/gpl-2.0.txt
* Requires Plugins: foo-dependency
*/

// This should trigger the error.
Expand All @@ -462,7 +477,7 @@ Feature: Test that the WP-CLI command works.
}
);
"""
And I run the WP-CLI command `plugin activate foo-sample`
And I run the WP-CLI command `plugin activate foo-dependency foo-sample`

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