From e22682846f30e83784af78f3757fb124de62b2b0 Mon Sep 17 00:00:00 2001 From: Felix Arntz Date: Fri, 25 Oct 2024 13:50:57 -0700 Subject: [PATCH 1/6] Fix bug where addon checks would not be loaded when running any runtime checks. --- includes/Checker/Default_Check_Repository.php | 32 +++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/includes/Checker/Default_Check_Repository.php b/includes/Checker/Default_Check_Repository.php index 7b042186..976b4c2d 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 n.e.x.t + * @var bool + */ + protected $fully_initialized; + /** * Initializes checks. * * @since 1.0.0 */ public function __construct() { + $this->fully_initialized = did_action( 'plugins_loaded' ); $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. * From e6335ee8b52909dad4a495c4f1710db4103d3b6f Mon Sep 17 00:00:00 2001 From: Felix Arntz Date: Fri, 25 Oct 2024 13:53:33 -0700 Subject: [PATCH 2/6] Remove comments around Behat tests that didn't work. --- tests/behat/features/plugin-check.feature | 40 ++++++++++------------- 1 file changed, 18 insertions(+), 22 deletions(-) diff --git a/tests/behat/features/plugin-check.feature b/tests/behat/features/plugin-check.feature index 287c14a5..dbe8bb90 100644 --- a/tests/behat/features/plugin-check.feature +++ b/tests/behat/features/plugin-check.feature @@ -551,11 +551,10 @@ 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` @@ -564,27 +563,24 @@ 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 + """ - # 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 From 06eeb61e428886d7371c737c21a1c2e085f1d086 Mon Sep 17 00:00:00 2001 From: Felix Arntz Date: Fri, 25 Oct 2024 14:02:37 -0700 Subject: [PATCH 3/6] Improve Behat test to verify object-cache.php drop-in was removed. --- tests/behat/features/plugin-check.feature | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/tests/behat/features/plugin-check.feature b/tests/behat/features/plugin-check.feature index dbe8bb90..1e35f714 100644 --- a/tests/behat/features/plugin-check.feature +++ b/tests/behat/features/plugin-check.feature @@ -556,14 +556,14 @@ Feature: Test that the WP-CLI command works. 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 """ - And STDOUT should contain: + And STDOUT should not contain: """ ExampleRuntimeCheck.ForbiddenScript,WARNING """ From 45a3d9e89e9cfc9442b22cbf73a297cde77b2ebd Mon Sep 17 00:00:00 2001 From: Felix Arntz Date: Fri, 25 Oct 2024 14:05:38 -0700 Subject: [PATCH 4/6] Fix type warning. --- includes/Checker/Default_Check_Repository.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/includes/Checker/Default_Check_Repository.php b/includes/Checker/Default_Check_Repository.php index 976b4c2d..800ac312 100644 --- a/includes/Checker/Default_Check_Repository.php +++ b/includes/Checker/Default_Check_Repository.php @@ -31,7 +31,7 @@ class Default_Check_Repository extends Empty_Check_Repository { * @since 1.0.0 */ public function __construct() { - $this->fully_initialized = did_action( 'plugins_loaded' ); + $this->fully_initialized = did_action( 'plugins_loaded' ) === 0; $this->register_default_checks(); } From c48e1c3295bafeca0606f06aebc0ccb5dd94fbb3 Mon Sep 17 00:00:00 2001 From: Felix Arntz Date: Fri, 25 Oct 2024 14:10:45 -0700 Subject: [PATCH 5/6] Fix inversed condition. --- includes/Checker/Default_Check_Repository.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/includes/Checker/Default_Check_Repository.php b/includes/Checker/Default_Check_Repository.php index 800ac312..8a83a0bb 100644 --- a/includes/Checker/Default_Check_Repository.php +++ b/includes/Checker/Default_Check_Repository.php @@ -31,7 +31,7 @@ class Default_Check_Repository extends Empty_Check_Repository { * @since 1.0.0 */ public function __construct() { - $this->fully_initialized = did_action( 'plugins_loaded' ) === 0; + $this->fully_initialized = did_action( 'plugins_loaded' ) > 0; $this->register_default_checks(); } From 4f930808cd5450cab34a3d1a7e66ba147b7b49d0 Mon Sep 17 00:00:00 2001 From: Felix Arntz Date: Fri, 25 Oct 2024 15:25:54 -0700 Subject: [PATCH 6/6] Use 1.3.0 version annotation. --- includes/Checker/Default_Check_Repository.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/includes/Checker/Default_Check_Repository.php b/includes/Checker/Default_Check_Repository.php index 8a83a0bb..24714067 100644 --- a/includes/Checker/Default_Check_Repository.php +++ b/includes/Checker/Default_Check_Repository.php @@ -20,7 +20,7 @@ class Default_Check_Repository extends Empty_Check_Repository { * 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 n.e.x.t + * @since 1.3.0 * @var bool */ protected $fully_initialized;