|
8 | 8 | namespace WordPress\Plugin_Check\Checker; |
9 | 9 |
|
10 | 10 | use Exception; |
| 11 | +use WordPress\Plugin_Check\Checker\Exception\Invalid_Check_Slug_Exception; |
11 | 12 | use WordPress\Plugin_Check\Checker\Preparations\Universal_Runtime_Preparation; |
12 | 13 | use WordPress\Plugin_Check\Utilities\Plugin_Request_Utility; |
13 | | -use WP_CLI; |
14 | 14 |
|
15 | 15 | /** |
16 | 16 | * Abstract Check Runner class. |
@@ -294,17 +294,31 @@ final public function set_categories( $categories ) { |
294 | 294 | final public function prepare() { |
295 | 295 | $cleanup_functions = array(); |
296 | 296 |
|
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; |
| 297 | + if ( $this->initialized_early ) { |
| 298 | + /* |
| 299 | + * When initialized early, plugins are not loaded yet when this method is called. |
| 300 | + * Therefore it could be that check slugs provided refer to addon checks that are not loaded yet. |
| 301 | + * In that case, the only reliable option is to assume that it refers to an addon check and that the addon |
| 302 | + * check is a runtime check. We don't know, but better to have the runtime preparations initialize |
| 303 | + * unnecessarily rather than not having them when needed. |
| 304 | + * |
| 305 | + * The actual checks to run are retrieved later (once plugins are loaded), so if one of the provided slugs |
| 306 | + * is actually invalid, the exception will still be thrown at that point. |
| 307 | + */ |
| 308 | + try { |
| 309 | + $checks = $this->get_checks_to_run(); |
| 310 | + $initialize_runtime = $this->has_runtime_check( $checks ); |
| 311 | + } catch ( Invalid_Check_Slug_Exception $e ) { |
| 312 | + $initialize_runtime = true; |
307 | 313 | } |
| 314 | + } else { |
| 315 | + // When not initialized early, all checks are loaded, so we can simply see if there are runtime checks. |
| 316 | + $initialize_runtime = $this->has_runtime_check( $this->get_checks_to_run() ); |
| 317 | + } |
| 318 | + |
| 319 | + if ( $initialize_runtime ) { |
| 320 | + $preparation = new Universal_Runtime_Preparation( $this->get_check_context() ); |
| 321 | + $cleanup_functions[] = $preparation->prepare(); |
308 | 322 | } |
309 | 323 |
|
310 | 324 | if ( $this->delete_plugin_folder ) { |
|
0 commit comments