diff --git a/includes/Checker/Default_Check_Repository.php b/includes/Checker/Default_Check_Repository.php index 7b042186..24714067 100644 --- a/includes/Checker/Default_Check_Repository.php +++ b/includes/Checker/Default_Check_Repository.php @@ -14,15 +14,47 @@ */ class Default_Check_Repository extends Empty_Check_Repository { + /** + * True if the class was fully initialized. + * + * If the class is instantiated before plugins are loaded, this will be set to false. + * This way the checks will be re-initialized once plugins have been loaded, and only then it is set to true. + * + * @since 1.3.0 + * @var bool + */ + protected $fully_initialized; + /** * Initializes checks. * * @since 1.0.0 */ public function __construct() { + $this->fully_initialized = did_action( 'plugins_loaded' ) > 0; $this->register_default_checks(); } + /** + * Returns an array of checks. + * + * @since 1.0.0 + * + * @param int $flags The check type flag. + * @return Check_Collection Check collection providing an indexed array of check instances. + */ + public function get_checks( $flags = self::TYPE_ALL ) { + // Once plugins have been loaded, re-initialize the checks. + if ( ! $this->fully_initialized && did_action( 'plugins_loaded' ) ) { + $this->fully_initialized = true; + $this->runtime_checks = array(); + $this->static_checks = array(); + $this->register_default_checks(); + } + + return parent::get_checks( $flags ); + } + /** * Registers Checks. * diff --git a/tests/behat/features/plugin-check.feature b/tests/behat/features/plugin-check.feature index 287c14a5..1e35f714 100644 --- a/tests/behat/features/plugin-check.feature +++ b/tests/behat/features/plugin-check.feature @@ -551,40 +551,36 @@ 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. -# And STDOUT should contain: -# """ -# ExampleRuntimeCheck.ForbiddenScript,WARNING -# """ + And STDOUT should contain: + """ + ExampleRuntimeCheck.ForbiddenScript,WARNING + """ - # Same again, to verify object-cache.php was properly cleared again - When I run the WP-CLI command `plugin check foo-sample --fields=code,type --format=csv --require=./wp-content/plugins/plugin-check/cli.php` + # Same again but without requiring the cli.php file, to verify object-cache.php was properly cleared again + When I run the WP-CLI command `plugin check foo-sample --fields=code,type --format=csv` Then STDOUT should contain: """ WordPress.WP.EnqueuedResourceParameters.NotInFooter,WARNING """ - # This doesn't currently work, because we are not actually loading any other plugins, including pcp-addon. -# And STDOUT should contain: -# """ -# ExampleRuntimeCheck.ForbiddenScript,WARNING -# """ + And STDOUT should not 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 -# """ + 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. # Run only the runtime check from pcp-addon, no others -# When I run the WP-CLI command `plugin check foo-sample --checks=example_runtime --fields=code,type --format=csv --require=./wp-content/plugins/plugin-check/cli.php` -# Then STDOUT should contain: -# """ -# ExampleRuntimeCheck.ForbiddenScript,WARNING -# """ + When I run the WP-CLI command `plugin check foo-sample --checks=example_runtime --fields=code,type --format=csv --require=./wp-content/plugins/plugin-check/cli.php` + Then STDOUT should contain: + """ + ExampleRuntimeCheck.ForbiddenScript,WARNING + """ Scenario: Check custom single file plugin that has no errors or warnings Given a WP install with the Plugin Check plugin