Skip to content
Draft
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
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 --verbose flag to see check progress
Given a WP install

When I run `wp doctor check autoload-options-size --verbose`
Then STDOUT should contain:
"""
Running check: autoload-options-size
"""
And STDOUT should contain:
"""
Status:
"""

Scenario: Use --verbose flag with multiple checks
Given a WP install

When I run `wp doctor check autoload-options-size core-update --verbose`
Then STDOUT should contain:
"""
Running check: autoload-options-size
"""
And STDOUT should contain:
"""
Running check: core-update
"""

Scenario: Use --verbose 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 run `wp doctor check cache-flush --verbose`
Then STDOUT should contain:
"""
Scanning filesystem for file checks...
"""
And STDOUT should contain:
"""
Running check: cache-flush
"""
41 changes: 38 additions & 3 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 verbose mode.
*/
const VERBOSE_FILE_SCAN_INTERVAL = 1000;

/**
* Run a series of checks against WordPress to diagnose issues.
*
Expand Down Expand Up @@ -76,6 +81,9 @@ class Command {
* - count
* ---
*
* [--verbose]
* : Display more information about what each check is doing.
*
* ## AVAILABLE FIELDS
*
* These fields will be displayed by default for each check:
Expand Down Expand Up @@ -114,6 +122,8 @@ public function check( $args, $assoc_args ) {
WP_CLI::error( 'Please specify one or more checks, or use --all.' );
}

$verbose = Utils\get_flag_value( $assoc_args, 'verbose', false );

$completed = array();
$checks = Checks::get_checks( array( 'name' => $args ) );
if ( empty( $checks ) ) {
Expand All @@ -125,20 +135,27 @@ public function check( $args, $assoc_args ) {
}
$file_checks = array();
$progress = false;
if ( $all && 'table' === $assoc_args['format'] ) {
if ( $all && 'table' === $assoc_args['format'] && ! $verbose ) {
$progress = Utils\make_progress_bar( 'Running checks', count( $checks ) );
}
foreach ( $checks as $name => $check ) {
$when = $check->get_when();
if ( $when ) {
WP_CLI::add_hook(
$when,
static function () use ( $name, $check, &$completed, &$progress ) {
static function () use ( $name, $check, &$completed, &$progress, $verbose ) {
if ( $verbose ) {
WP_CLI::log( "Running check: {$name}" );
}
$check->run();
$completed[ $name ] = $check;
if ( $progress ) {
$progress->tick();
}
if ( $verbose ) {
$results = $check->get_results();
WP_CLI::log( " Status: {$results['status']}" );
}
}
);
} else {
Expand All @@ -151,12 +168,20 @@ static function () use ( $name, $check, &$completed, &$progress ) {
if ( ! empty( $file_checks ) ) {
WP_CLI::add_hook(
'after_wp_config_load',
static function () use ( $file_checks, &$completed, &$progress ) {
static function () use ( $file_checks, &$completed, &$progress, $verbose ) {
if ( $verbose ) {
WP_CLI::log( 'Scanning filesystem for file checks...' );
}
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';
$file_count = 0;
foreach ( $iterator as $file ) {
++$file_count;
if ( $verbose && 0 === $file_count % self::VERBOSE_FILE_SCAN_INTERVAL ) {
WP_CLI::log( " Scanned {$file_count} files..." );
}
foreach ( $file_checks as $name => $check ) {
$options = $check->get_options();
if ( ! empty( $options['only_wp_content'] )
Expand All @@ -174,15 +199,25 @@ static function () use ( $file_checks, &$completed, &$progress ) {
$check->check_file( $file );
}
}
if ( $verbose ) {
WP_CLI::log( " Total files scanned: {$file_count}" );
}
} catch ( Exception $e ) {
WP_CLI::warning( $e->getMessage() );
}
foreach ( $file_checks as $name => $check ) {
if ( $verbose ) {
WP_CLI::log( "Running check: {$name}" );
}
$check->run();
$completed[ $name ] = $check;
if ( $progress ) {
$progress->tick();
}
if ( $verbose ) {
$results = $check->get_results();
WP_CLI::log( " Status: {$results['status']}" );
}
}
}
);
Expand Down
Loading