|
9 | 9 |
|
10 | 10 | use Exception; |
11 | 11 | use WordPress\Plugin_Check\Checker\Preparation; |
| 12 | +use WP_Plugin_Dependencies; |
12 | 13 |
|
13 | 14 | /** |
14 | 15 | * Class for the preparation to force the plugin to be checked as the only active plugin. |
|
20 | 21 | class Force_Single_Plugin_Preparation implements Preparation { |
21 | 22 |
|
22 | 23 | /** |
23 | | - * Plugin slug. |
| 24 | + * Plugin slug of the plugin to check. |
24 | 25 | * |
25 | 26 | * @since 1.0.0 |
26 | 27 | * @var string |
@@ -74,39 +75,66 @@ public function prepare() { |
74 | 75 | } |
75 | 76 |
|
76 | 77 | /** |
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) |
78 | 86 | * |
79 | 87 | * @since 1.0.0 |
| 88 | + * @since 1.2.0 Now includes dependencies and dependents. |
80 | 89 | * |
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. |
83 | 92 | */ |
84 | 93 | 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 | + } |
86 | 97 |
|
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 | + } |
93 | 102 |
|
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 | + } |
95 | 108 |
|
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 ); |
98 | 110 |
|
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 | + ); |
103 | 115 |
|
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 | + ) |
107 | 134 | ); |
108 | 135 | } |
109 | 136 |
|
110 | | - return $active_plugins; |
| 137 | + // Removes duplicates, for example if Plugin Check is the plugin being tested. |
| 138 | + return array_unique( $new_active_plugins ); |
111 | 139 | } |
112 | 140 | } |
0 commit comments