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

Add Health Checks #36

Merged
merged 25 commits into from
Jan 29, 2025
Merged
Show file tree
Hide file tree
Changes from 11 commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
b3b3f20
Add Health Checks
bradp Dec 3, 2024
7e384b3
Tidy up
bradp Dec 10, 2024
a39d902
Update text
bradp Dec 10, 2024
cccc5bb
Implement remaining checks
bradp Dec 10, 2024
84f195c
Comment each check
bradp Dec 10, 2024
63facc4
Disabled checks for non-merged PRs
bradp Dec 10, 2024
df09f81
Update docblock
bradp Dec 10, 2024
b1fb76c
Update text
bradp Dec 10, 2024
629d15b
Update text, remove extra PR links
bradp Dec 11, 2024
0c27530
Update HealthCheckManager to allow passing an array of url, label, an…
bradp Dec 11, 2024
9871e74
Update Health Checks with consistent punctuation, link directly to th…
bradp Dec 11, 2024
9c1ce1b
Update Health Checks with consistent punctuation, link directly to th…
bradp Jan 8, 2025
04fea84
Update Performance.php
bradp Jan 23, 2025
8dda913
Update HealthCheckManager.php
bradp Jan 23, 2025
5f381b8
Update HealthCheckManager.php
bradp Jan 23, 2025
5010756
Update HealthChecks.php
bradp Jan 23, 2025
1bc16b6
Update Performance.php
bradp Jan 23, 2025
e98e1a0
Merge branch 'main' into feature/health-checks
bradp Jan 23, 2025
bc5dbc2
Update Performance.php
bradp Jan 23, 2025
8e3850e
Update HealthChecks.php
bradp Jan 23, 2025
c0043f0
Fix method name
bradp Jan 23, 2025
3430f68
fix method name
bradp Jan 23, 2025
697409a
Refactor Health Checks to use an abstract class and individual classe…
bradp Jan 27, 2025
791c80b
Add PHPUnit bootstrap and configuration files, add Health Check tests.
bradp Jan 27, 2025
aee37f1
Rename i18n namespace
arunshenoy99 Jan 29, 2025
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
176 changes: 176 additions & 0 deletions includes/HealthCheckManager.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,176 @@
<?php

namespace NewfoldLabs\WP\Module\Performance;

use NewfoldLabs\WP\ModuleLoader\Container;

/**
* Add performance health checks.
*/
class HealthCheckManager {

/**
* Health Checks to add.
*
* @var array
*/
public $checks = array();

/**
* Health Check ID prefix.
*
* @var string
*/
public $prefix = 'newfold_performance_';

/**
* Dependency injection container.
*
* @var Container
*/
protected $container;

/**
* Constructor.
*
* @param Container $container Dependency injection container.
*/
public function __construct( Container $container ) {
$this->container = $container;

if ( function_exists( 'add_filter' ) ) {
$this->addHooks();
}
}

/**
* Add hooks.
*/
public function addHooks() {
add_filter( 'site_status_tests', array( $this, 'registerHealthChecks' ) );
}

/**
* Add a health check.
*
* @param array $options Health check options.
*/
public function addHealthCheck( $options ) {
$options = wp_parse_args(
$options,
array(
'id' => '',
'title' => '',
'test' => '',
'label' => false, // Setting this will override pass/fail labels.
'pass' => '',
'fail' => '',
'text' => '',
'status' => false, // Override the status of the health check: default is good for pass, critical for fail.
'badge_label' => __( 'Performance', 'newfold-labs' ),
'badge_color' => 'blue',
'actions' => '',
)
);

// Make sure the health check is valid.
if ( ! ( empty( $options['id'] ) || empty( $options['title'] ) || ! is_callable( $options['test'] ) ) ) {
$this->checks[ $this->prefix . $options['id'] ] = $options;
}
}

/**
* Concatenate actions array into a string.
*
* @param array $actions Actions to concatenate. Should contain an array of 'label', 'url', and 'external'.
*
* @return string Concatenated actions.
*/
public function concatActions( $actions ) {
$actions_string = '';

foreach ( $actions as $action ) {
$action = wp_parse_args(
$action,
array(
'label' => '',
'url' => '',
'external' => false,
)
);

$actions_string .= sprintf(
'<a href="%1$s" %3$s>%2$s</a>%4$s',
esc_url( $action['url'] ),
esc_html( $action['label'] ),
$action['external'] ? 'target="_blank" rel="noopener"' : '',
$action['external'] ? sprintf(
'<span class="screen-reader-text"> (%s)</span><span aria-hidden="true" class="dashicons dashicons-external"></span>',
__( 'opens in a new tab', 'newfold-performance-module' )
) : ''
);
}

return $actions_string;
}

/**
* Run a health check.
*
* @param string $id Health check ID.
*
* @return array Health check results.
*/
public function runHealthCheck( $id ) {
$check = $this->checks[ $id ];

// Execute the test.
$passed = call_user_func( $check['test'] );

// Return the health check results.
return array(
'label' => $check['label'] ? $check['label'] : ( $passed ? $check['pass'] : $check['fail'] ),
'status' => $passed ? 'good' : ( 'critical' === $check['status'] ? 'critical' : 'recommended' ), // Will default to 'recommended', unless 'critical' is passed.
'description' => sprintf( '<p>%s</p>', $check['text'] ? $check['text'] : '' ),
'actions' => is_array( $check['actions'] ) ? $this->concatActions( $check['actions'] ) : ( $check['actions'] ? $check['actions'] : '' ),
'test' => $check['id'],
'badge' => array(
'label' => $check['badge_label'],
'color' => $check['badge_color'],
),
);
}

/**
* Add health checks.
*
* @param array $tests Site Health tests.
*
* @return array Site Health tests.
*/
public function registerHealthChecks( $tests ) {
// If there are no health checks, don't add any.
if ( ! is_array( $this->checks ) || empty( $this->checks ) ) {
return $tests;
}

foreach ( $this->checks as $id => $check ) {
/**
* Filter to enable/disable a health check.
*
* @param bool $do_check Whether to run the health check.
*/
$do_check = apply_filters( 'newfold/features/filter/isEnabled:healthChecks:' . $id, true ); // phpcs:ignore
if ( $do_check ) {
$tests['direct'][ $id ] = array(
'label' => $check['title'],
'test' => function () use ( $id ) {
return $this->runHealthCheck( $id );
},
);
}
}

return $tests;
}
}
Loading
Loading