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

Feature: Site Health #57

Merged
merged 5 commits into from
Dec 7, 2023
Merged
Show file tree
Hide file tree
Changes from 2 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
60 changes: 60 additions & 0 deletions src/alley/wp/alleyvate/features/class-site-health.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
<?php
/**
* Class file for Clean_Admin_Bar
srtfisher marked this conversation as resolved.
Show resolved Hide resolved
*
* (c) Alley <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* @package wp-alleyvate
*/

namespace Alley\WP\Alleyvate\Features;

use Alley\WP\Alleyvate\Feature;

use function Alley\WP\Alleyvate\available_features;
use function Alley\WP\Alleyvate\should_load_feature;

/**
* Site Health feature.
*/
final class Site_Health implements Feature {
/**
* Boot the feature.
*/
public function boot(): void {
add_filter( 'debug_information', [ $this, 'add_debug_information' ] );
}

/**
* Add debug information to the Site Health screen.
*
* @param array $info Debug information.
* @return array
*/
public function add_debug_information( $info ): array {
if ( ! is_array( $info ) ) {
$info = [];
}

$info['wp-alleyvate'] = [
'label' => __( 'Alleyvate', 'alley' ),
'description' => __( 'Diagnostic information about the Alleyvate plugin and which features are enabled.', 'alley' ),
'fields' => array_map(
fn ( string $handle ) => [
'label' => sprintf(
/* translators: %s: Feature name. */
__( 'Feature: %s', 'alley' ),
$handle,
),
'value' => should_load_feature( $handle ) ? __( 'Enabled', 'alley' ) : __( 'Disabled', 'alley' ),
],
array_keys( available_features() ),
),
];

return $info;
}
}
77 changes: 45 additions & 32 deletions src/alley/wp/alleyvate/load.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,52 +13,65 @@
namespace Alley\WP\Alleyvate;

/**
* Load plugin features.
* Get the available features to load.
*
* @return array<string, Feature>
*/
function load(): void {
// Bail if the Alleyvate feature interface isn't loaded to prevent a fatal error.
if ( ! interface_exists( Feature::class ) ) {
return;
}

/**
* Features to load.
*
* @var Feature[] $features
*/
$features = [
function available_features(): array {
return [
'clean_admin_bar' => new Features\Clean_Admin_Bar(),
'disable_comments' => new Features\Disable_Comments(),
'disable_dashboard_widgets' => new Features\Disable_Dashboard_Widgets(),
'disable_sticky_posts' => new Features\Disable_Sticky_Posts(),
'disable_trackbacks' => new Features\Disable_Trackbacks(),
'disallow_file_edit' => new Features\Disallow_File_Edit(),
'redirect_guess_shortcircuit' => new Features\Redirect_Guess_Shortcircuit(),
'site_health' => new Features\Site_Health(),
'user_enumeration_restrictions' => new Features\User_Enumeration_Restrictions(),
];
}

foreach ( $features as $handle => $feature ) {
$load = true;
/**
* Determine whether to load a feature.
*
* @param string $handle Feature handle.
* @return bool
*/
function should_load_feature( string $handle ): bool {
$load = true;

/**
* Filters whether to load an Alleyvate feature.
*
* @param bool $load Whether to load the feature. Default true.
* @param string $handle Feature handle.
*/
$load = apply_filters( 'alleyvate_load_feature', $load, $handle );
/**
* Filters whether to load an Alleyvate feature.
*
* @param bool $load Whether to load the feature. Default true.
* @param string $handle Feature handle.
*/
$load = apply_filters( 'alleyvate_load_feature', $load, $handle );

/**
* Filters whether to load the given Alleyvate feature.
*
* The dynamic portion of the hook name, `$handle`, refers to the
* machine name for the feature.
*
* @param bool $load Whether to load the feature. Default true.
*/
$load = apply_filters( "alleyvate_load_{$handle}", $load );

return (bool) $load;
}

/**
* Filters whether to load the given Alleyvate feature.
*
* The dynamic portion of the hook name, `$handle`, refers to the
* machine name for the feature.
*
* @param bool $load Whether to load the feature. Default true.
*/
$load = apply_filters( "alleyvate_load_{$handle}", $load );
/**
* Load plugin features.
*/
function load(): void {
// Bail if the Alleyvate feature interface isn't loaded to prevent a fatal error.
if ( ! interface_exists( Feature::class ) ) {
return;
}

if ( $load ) {
foreach ( available_features() as $handle => $feature ) {
if ( should_load_feature( $handle ) ) {
$feature->boot();
}
}
Expand Down
61 changes: 61 additions & 0 deletions tests/alley/wp/alleyvate/features/test-site-health.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
<?php
/**
* Class file for Test_Redirect_Guess_Shortcircuit
srtfisher marked this conversation as resolved.
Show resolved Hide resolved
*
* (c) Alley <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* @package wp-alleyvate
*/

namespace Alley\WP\Alleyvate\Features;

use Alley\WP\Alleyvate\Feature;
use Mantle\Testkit\Test_Case;

use function Alley\WP\Alleyvate\available_features;

/**
* Test for site health feature.
*/
final class Test_Site_Health extends Test_Case {
/**
* Feature instance.
*
* @var Feature
*/
private Feature $feature;

/**
* Set up.
*/
protected function setUp(): void {
parent::setUp();

$this->feature = new Site_Health();
}

/**
* Test the site health feature.
*/
public function test_site_health_feature() {
$features = available_features();

$this->expectApplied( 'alleyvate_load_feature' )->times( count( $features ) );

foreach ( $features as $handle => $class ) {
$this->expectApplied( "alleyvate_load_{$handle}" )->once();
}

$this->feature->boot();

// Mock the Site Health screen.
$data = apply_filters( 'debug_information', [] ); // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedHooknameFound

$this->assertNotEmpty( $data['wp-alleyvate'] ?? null );
$this->assertNotEmpty( $data['wp-alleyvate']['fields'] ?? null );
$this->assertCount( count( $features ), $data['wp-alleyvate']['fields'] );
}
}
2 changes: 2 additions & 0 deletions tests/bootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
// Fires on 'muplugins_loaded'.
->loaded(
function () {
require_once __DIR__ . '/../wp-alleyvate.php';

/*
* Turn off all features by default so that we can verify that the behavior
* of WordPress changes after we turn the feature on.
Expand Down
Loading