Skip to content

Commit

Permalink
CLI: Patch up WP-CLI command that was causing fatal errors due to inc…
Browse files Browse the repository at this point in the history
…orrect pathing.

We have no autoloader, so relying on one in the CLI code was leading to unexpected errors.
  • Loading branch information
Clorith committed Aug 6, 2023
1 parent c8c2b1e commit da19f5c
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 21 deletions.
66 changes: 45 additions & 21 deletions HealthCheck/WP_CLI/class-status.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,39 +11,63 @@ public function __construct( $format ) {
}

public function run() {
global $health_check_site_status;
$health_check_site_status = \WP_Site_Health::get_instance();;

$all_tests = $health_check_site_status::get_tests();
$tests = $health_check_site_status::get_tests();

$test_result = array();

foreach ( $all_tests['direct'] as $test ) {
$test_output = call_user_func( $test['test'] );
foreach ( $tests['direct'] as $test ) {
if ( is_string( $test['test'] ) ) {
$test_function = sprintf(
'get_test_%s',
$test['test']
);

$test_result[] = array(
'test' => $test['label'],
'type' => wp_kses( $test_output['badge']['label'], array() ),
'result' => wp_kses( $test_output['status'], array() ),
);
if ( method_exists( $health_check_site_status, $test_function ) && is_callable( array( $health_check_site_status, $test_function ) ) ) {
$test_output = apply_filters( 'site_status_test_result', call_user_func( array( $health_check_site_status, $test_function ) ) );

$test_result[] = array(
'test' => $test['label'],
'type' => wp_kses( $test_output['badge']['label'], array() ),
'result' => wp_kses( $test_output['status'], array() ),
);

continue;
}
}

if ( is_callable( $test['test'] ) ) {
$test_output = apply_filters( 'site_status_test_result', call_user_func( $test['test'] ) );

$test_result[] = array(
'test' => $test['label'],
'type' => wp_kses( $test_output['badge']['label'], array() ),
'result' => wp_kses( $test_output['status'], array() ),
);
}
}
foreach ( $all_tests['async'] as $test ) {
$test_output = call_user_func( array( $health_check_site_status, 'get_test_' . $test['test'] ) );

$test_result[] = array(
'test' => $test['label'],
'type' => wp_kses( $test_output['badge']['label'], array() ),
'result' => wp_kses( $test_output['status'], array() ),
);

foreach ( $tests['async'] as $test ) {
if ( isset( $test['async_direct_test'] ) && is_callable( $test['async_direct_test'] ) ) {
$test_output = apply_filters( 'site_status_test_result', call_user_func( $test['async_direct_test'] ) );

$test_result[] = array(
'test' => $test['label'],
'type' => wp_kses( $test_output['badge']['label'], array() ),
'result' => wp_kses( $test_output['status'], array() ),
);
}
}

if ( 'json' === $this->format ) {
WP_CLI\Utils\format_items( 'json', $test_result, array( 'test', 'type', 'result' ) );
\WP_CLI\Utils\format_items( 'json', $test_result, array( 'test', 'type', 'result' ) );
} elseif ( 'csv' === $this->format ) {
WP_CLI\Utils\format_items( 'csv', $test_result, array( 'test', 'type', 'result' ) );
\WP_CLI\Utils\format_items( 'csv', $test_result, array( 'test', 'type', 'result' ) );
} elseif ( 'yaml' === $this->format ) {
WP_CLI\Utils\format_items( 'yaml', $test_result, array( 'test', 'type', 'result' ) );
\WP_CLI\Utils\format_items( 'yaml', $test_result, array( 'test', 'type', 'result' ) );
} else {
WP_CLI\Utils\format_items( 'table', $test_result, array( 'test', 'type', 'result' ) );
\WP_CLI\Utils\format_items( 'table', $test_result, array( 'test', 'type', 'result' ) );
}
}

Expand Down
4 changes: 4 additions & 0 deletions HealthCheck/class-cli.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@
die( 'We\'re sorry, but you can not directly access this file.' );
}

if ( ! class_exists( 'HealthCheck\WP_CLI\Status' ) ) {
require_once __DIR__ . '/WP_CLI/class-status.php';
}

/**
* Class Health_Check_CLI
*/
Expand Down

0 comments on commit da19f5c

Please sign in to comment.