Skip to content
46 changes: 46 additions & 0 deletions features/check.feature
Original file line number Diff line number Diff line change
Expand Up @@ -185,3 +185,49 @@ Feature: Basic check usage
And STDOUT should be a table containing rows:
| name | status |
| autoload-options-size | success |

Scenario: Use --debug=doctor flag to see check progress
Given a WP install

When I try `wp doctor check autoload-options-size --debug=doctor`
Then STDERR should contain:
"""
Running check: autoload-options-size
"""
And STDERR should contain:
"""
Status:
"""

Scenario: Use --debug=doctor flag with multiple checks
Given a WP install

When I try `wp doctor check autoload-options-size plugin-deactivated --debug=doctor`
Then STDERR should contain:
"""
Running check: autoload-options-size
"""
And STDERR should contain:
"""
Running check: plugin-deactivated
"""

Scenario: Use --debug=doctor flag with file checks
Given a WP install
And a wp-content/plugins/foo.php file:
"""
<?php
// Plugin Name: Foo Plugin

wp_cache_flush();
"""

When I try `wp doctor check cache-flush --debug=doctor`
Then STDERR should contain:
"""
Scanning filesystem for file checks...
"""
And STDERR should contain:
"""
Running check: cache-flush
"""
18 changes: 18 additions & 0 deletions src/Command.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,11 @@
*/
class Command {

/**
* Number of files to scan before showing progress in debug mode.
*/
const DEBUG_FILE_SCAN_INTERVAL = 1000;

/**
* Run a series of checks against WordPress to diagnose issues.
*
Expand Down Expand Up @@ -134,11 +139,14 @@ public function check( $args, $assoc_args ) {
WP_CLI::add_hook(
$when,
static function () use ( $name, $check, &$completed, &$progress ) {
WP_CLI::debug( "Running check: {$name}", 'doctor' );
$check->run();
$completed[ $name ] = $check;
if ( $progress ) {
$progress->tick();
}
$results = $check->get_results();
WP_CLI::debug( " Status: {$results['status']}", 'doctor' );
}
);
} else {
Expand All @@ -152,11 +160,17 @@ static function () use ( $name, $check, &$completed, &$progress ) {
WP_CLI::add_hook(
'after_wp_config_load',
static function () use ( $file_checks, &$completed, &$progress ) {
WP_CLI::debug( 'Scanning filesystem for file checks...', 'doctor' );
try {
$directory = new RecursiveDirectoryIterator( ABSPATH, RecursiveDirectoryIterator::SKIP_DOTS );
$iterator = new RecursiveIteratorIterator( $directory, RecursiveIteratorIterator::CHILD_FIRST );
$wp_content_dir = defined( 'WP_CONTENT_DIR' ) ? WP_CONTENT_DIR : ABSPATH . 'wp-content';
$item_count = 0;
foreach ( $iterator as $file ) {
++$item_count;
if ( 0 === $item_count % self::DEBUG_FILE_SCAN_INTERVAL ) {
WP_CLI::debug( " Visited {$item_count} items...", 'doctor' );
}
foreach ( $file_checks as $name => $check ) {
$options = $check->get_options();
if ( ! empty( $options['only_wp_content'] )
Expand All @@ -174,15 +188,19 @@ static function () use ( $file_checks, &$completed, &$progress ) {
$check->check_file( $file );
}
}
WP_CLI::debug( " Total items visited: {$item_count}", 'doctor' );
} catch ( Exception $e ) {
WP_CLI::warning( $e->getMessage() );
}
foreach ( $file_checks as $name => $check ) {
WP_CLI::debug( "Running check: {$name}", 'doctor' );
$check->run();
$completed[ $name ] = $check;
if ( $progress ) {
$progress->tick();
}
$results = $check->get_results();
WP_CLI::debug( " Status: {$results['status']}", 'doctor' );
}
}
);
Expand Down