Skip to content

Commit

Permalink
Merge pull request #612 from WordPress/fix/early-check-retrieval
Browse files Browse the repository at this point in the history
Co-authored-by: felixarntz <[email protected]>
Co-authored-by: swissspidy <[email protected]>
Co-authored-by: ernilambar <[email protected]>
  • Loading branch information
4 people authored Sep 6, 2024
2 parents 75be00e + c532f10 commit 9d72214
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 17 deletions.
36 changes: 25 additions & 11 deletions includes/Checker/Abstract_Check_Runner.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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 ) {
Expand Down
4 changes: 2 additions & 2 deletions includes/Checker/Check_Collection.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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;
}
6 changes: 3 additions & 3 deletions includes/Checker/Default_Check_Collection.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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' ),
Expand Down
19 changes: 19 additions & 0 deletions includes/Checker/Exception/Invalid_Check_Slug_Exception.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php
/**
* Class WordPress\Plugin_Check\Checker\Exception\Invalid_Check_Slug_Exception
*
* @package plugin-check
*/

namespace WordPress\Plugin_Check\Checker\Exception;

use InvalidArgumentException;

/**
* Class for an exception thrown when an invalid check slug is provided.
*
* @since n.e.x.t
*/
class Invalid_Check_Slug_Exception extends InvalidArgumentException {

}
10 changes: 9 additions & 1 deletion tests/behat/features/plugin-check.feature
Original file line number Diff line number Diff line change
Expand Up @@ -514,10 +514,18 @@ Feature: Test that the WP-CLI command works.
WordPress.WP.EnqueuedResourceParameters.NotInFooter,WARNING
"""

# This doesn't currently work, because we are not actually loading any other plugins, including pcp-addon.
# This doesn't currently work, because we are not actually loading any other plugins, including pcp-addon.
# And STDOUT should contain:
# """
# ExampleRuntimeCheck.ForbiddenScript,WARNING
# """

# This doesn't currently work.
# Run one runtime check from PCP and one from pcp-addon.
# When I run the WP-CLI command `plugin check foo-sample --checks=non_blocking_scripts,example_runtime --fields=code,type --format=csv --require=./wp-content/plugins/plugin-check/cli.php`
# Then STDOUT should contain:
# """
# ExampleRuntimeCheck.ForbiddenScript,WARNING
# """

# This doesn't currently work, because we are not actually loading any other plugins, including pcp-addon.
Expand Down

0 comments on commit 9d72214

Please sign in to comment.