Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix addon checks not being executed when running runtime checks #751

Merged
merged 7 commits into from
Oct 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions includes/Checker/Default_Check_Repository.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down
44 changes: 20 additions & 24 deletions tests/behat/features/plugin-check.feature
Original file line number Diff line number Diff line change
Expand Up @@ -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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice!

"""

# 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
Expand Down