Skip to content

Commit

Permalink
Merge pull request #751 from WordPress/fix/runtime-checks
Browse files Browse the repository at this point in the history
Fix addon checks not being executed when running runtime checks
  • Loading branch information
ernilambar authored Oct 26, 2024
2 parents afbe0a7 + 4f93080 commit cf7bfa5
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 24 deletions.
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
"""

# 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

0 comments on commit cf7bfa5

Please sign in to comment.