diff --git a/includes/Checker/Abstract_Check_Runner.php b/includes/Checker/Abstract_Check_Runner.php index b52db568f..8cb612c5b 100644 --- a/includes/Checker/Abstract_Check_Runner.php +++ b/includes/Checker/Abstract_Check_Runner.php @@ -8,9 +8,9 @@ namespace WordPress\Plugin_Check\Checker; use Exception; +use WordPress\Plugin_Check\Checker\Exception\Invalid_Check_Slug_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. @@ -294,17 +294,31 @@ final public function set_categories( $categories ) { final public function prepare() { $cleanup_functions = array(); - 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; + if ( $this->initialized_early ) { + /* + * When initialized early, plugins are not loaded yet when this method is called. + * Therefore it could be that check slugs provided refer to addon checks that are not loaded yet. + * In that case, the only reliable option is to assume that it refers to an addon check and that the addon + * check is a runtime check. We don't know, but better to have the runtime preparations initialize + * unnecessarily rather than not having them when needed. + * + * The actual checks to run are retrieved later (once plugins are loaded), so if one of the provided slugs + * is actually invalid, the exception will still be thrown at that point. + */ + try { + $checks = $this->get_checks_to_run(); + $initialize_runtime = $this->has_runtime_check( $checks ); + } catch ( Invalid_Check_Slug_Exception $e ) { + $initialize_runtime = true; } + } else { + // When not initialized early, all checks are loaded, so we can simply see if there are runtime checks. + $initialize_runtime = $this->has_runtime_check( $this->get_checks_to_run() ); + } + + if ( $initialize_runtime ) { + $preparation = new Universal_Runtime_Preparation( $this->get_check_context() ); + $cleanup_functions[] = $preparation->prepare(); } if ( $this->delete_plugin_folder ) { diff --git a/includes/Checker/Check_Collection.php b/includes/Checker/Check_Collection.php index d6fb8290b..e86d0517e 100644 --- a/includes/Checker/Check_Collection.php +++ b/includes/Checker/Check_Collection.php @@ -9,8 +9,8 @@ use ArrayAccess; use Countable; -use Exception; use IteratorAggregate; +use WordPress\Plugin_Check\Checker\Exception\Invalid_Check_Slug_Exception; /** * Check Collection interface. @@ -82,7 +82,7 @@ public function exclude( array $check_slugs ): Check_Collection; * @param array $check_slugs List of slugs to limit to only those. If empty, the same collection is returned. * @return Check_Collection The unchanged check collection. * - * @throws Exception Thrown when any of the given check slugs is not present in the collection. + * @throws Invalid_Check_Slug_Exception Thrown when any of the given check slugs is not present in the collection. */ public function require( array $check_slugs ): Check_Collection; } diff --git a/includes/Checker/Default_Check_Collection.php b/includes/Checker/Default_Check_Collection.php index e135b61d7..a21ede83b 100644 --- a/includes/Checker/Default_Check_Collection.php +++ b/includes/Checker/Default_Check_Collection.php @@ -8,8 +8,8 @@ namespace WordPress\Plugin_Check\Checker; use ArrayIterator; -use Exception; use Traversable; +use WordPress\Plugin_Check\Checker\Exception\Invalid_Check_Slug_Exception; /** * Default Check Collection class. @@ -150,12 +150,12 @@ static function ( Check $check, $slug ) use ( $check_slugs ) { * @param array $check_slugs List of slugs to limit to only those. If empty, the same collection is returned. * @return Check_Collection The unchanged check collection. * - * @throws Exception Thrown when any of the given check slugs is not present in the collection. + * @throws Invalid_Check_Slug_Exception Thrown when any of the given check slugs is not present in the collection. */ public function require( array $check_slugs ): Check_Collection { foreach ( $check_slugs as $slug ) { if ( ! isset( $this->checks[ $slug ] ) ) { - throw new Exception( + throw new Invalid_Check_Slug_Exception( sprintf( /* translators: %s: The Check slug. */ __( 'Check with the slug "%s" does not exist.', 'plugin-check' ), diff --git a/includes/Checker/Exception/Invalid_Check_Slug_Exception.php b/includes/Checker/Exception/Invalid_Check_Slug_Exception.php new file mode 100644 index 000000000..cd1144f5f --- /dev/null +++ b/includes/Checker/Exception/Invalid_Check_Slug_Exception.php @@ -0,0 +1,19 @@ +