Skip to content

Commit

Permalink
Merge branch 'trunk' into fix/early-check-retrieval
Browse files Browse the repository at this point in the history
  • Loading branch information
swissspidy committed Sep 5, 2024
2 parents f4a8dd0 + 25c033e commit 5875a80
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 24 deletions.
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
* @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 ) ),
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

0 comments on commit 5875a80

Please sign in to comment.