-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathsite-health-tool-manager.php
159 lines (142 loc) · 5.18 KB
/
site-health-tool-manager.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
<?php
/**
* Plugin Name: Site Health Tool Manager
* Plugin URI: https://github.com/earnjam/site-health-tool-manager
* Description: Control which tests appear in the the Site Health Tool
* Version: 1.1
* Author: William Earnhardt
* Author URI: https://wearnhardt.com
* License: GPL2
* License URI: https://www.gnu.org/licenses/gpl-2.0.html
* Text Domain: site-health-tool-manager
*/
/**
* Register the Site Health Tool settings page
*/
function shtm_add_settings_page() {
add_submenu_page(
'options-general.php',
__( 'Site Health Tool Settings', 'site-health-tool-manager' ),
__( 'Site Health', 'site-health-tool-manager' ),
'manage_options',
'shtm-settings',
'shtm_settings_page'
);
}
add_action( 'admin_menu', 'shtm_add_settings_page', 10 );
/**
* Filters the list of registered Site Health Tool tests
*
* @param array $tests The array of registered Site Health tests
* @return array The filtered list of tests.
*/
function shtm_filter_tests( $tests ) {
// Don't filter on the plugin settings page
if ( get_current_screen()->base !== 'settings_page_shtm-settings' ) {
$hidden_tests = (array) maybe_unserialize( get_option( 'shtm_hidden_tests' ) );
foreach ( $hidden_tests as $test ) {
unset( $tests['direct'][ $test ] );
unset( $tests['async'][ $test ] );
}
}
return $tests;
}
add_filter( 'site_status_tests', 'shtm_filter_tests', 10000 );
/**
* Disable the dashboard widget
*/
function shtm_filter_dashboard_widget() {
if ( ! get_option( 'shtm_widget_enabled', 1 ) ) {
global $wp_meta_boxes;
unset( $wp_meta_boxes['dashboard']['normal']['core']['dashboard_site_health'] );
}
}
add_action( 'wp_dashboard_setup', 'shtm_filter_dashboard_widget' );
/**
* Output for the Site Health Tool Settings page
*/
function shtm_settings_page() { ?>
<div class="wrap">
<h1><?php _e( 'Site Health Tool Settings', 'site-health-tool-manager' ); ?></h1>
<?php
// Verify user has proper capability to view this page
if ( ! current_user_can( 'manage_options' ) ) {
wp_die( __( 'Sorry, you are not allowed to manage Site Health tests for this site.', 'site-health-tool-manager' ) );
}
include_once ABSPATH . 'wp-admin/includes/class-wp-site-health.php';
$tests = WP_Site_Health::get_tests();
$disabled = get_option( 'shtm_hidden_tests', array() );
$widget = get_option( 'shtm_widget_enabled', 1 );
$enabled = array();
// If tests have been submitted, process the form data
if ( isset( $_POST['submit'] ) ) {
// Verify form nonce before saving
if ( isset( $_POST['shtm-disable-tests-nonce'] ) && wp_verify_nonce( $_POST['shtm-disable-tests-nonce'], 'shtm-disable-tests' ) ) {
// Validate that submitted tests are actually registered
$test_names = array_merge( $tests['direct'], $tests['async'] );
if ( isset( $_POST['checked'] ) ) {
foreach ( $_POST['checked'] as $name ) {
if ( isset( $test_names[ $name ] ) ) {
$enabled[] = $name;
}
}
}
// Only save the list of which tests were not checked.
// This ensures that any tests that are added after this setting is
// saved will still get run.
$new_disabled = array_keys( array_diff_key( $test_names, array_flip( $enabled ) ) );
update_option( 'shtm_hidden_tests', $new_disabled );
$disabled = $new_disabled;
$widget = ( isset( $_POST['widget'] ) ) ? 1 : 0;
update_option( 'shtm_widget_enabled', $widget );
$classes = 'notice notice-success is-dismissible';
$message = __( 'Settings saved.', 'site-health-tool-manager' );
printf( '<div class="%1$s"><p>%2$s</p></div>', esc_attr( $classes ), esc_html( $message ) );
} else {
// Invalid or missing nonce
$classes = 'notice notice-error is-dismissible';
$message = __( 'Unable to submit this form, please try again. Your changes have not been saved.', 'site-health-tool-manager' );
printf( '<div class="%1$s"><p>%2$s</p></div>', esc_attr( $classes ), esc_html( $message ) );
}
}
?>
<form method="POST" action="">
<h2><?php _e( 'Tests Enabled', 'site-health-tool-manager' ); ?></h2>
<p><?php _e( 'Certain tests may not be relevant to your environment. Uncheck a test to remove it from the Site Health Status screen.', 'site-health-tool-manager' ); ?></p>
<?php wp_nonce_field( 'shtm-disable-tests', 'shtm-disable-tests-nonce' ); ?>
<ul>
<?php
foreach ( $tests as $type ) {
$checked = false;
foreach ( $type as $test => $details ) {
$checked = ( ! in_array( $test, $disabled ) );
echo '<li><input type="checkbox" ';
if ( $checked ) {
echo 'checked="checked" ';
}
echo 'name="checked[]" id="' . $test . '" value="' . $test . '" />';
echo '<label for="' . $test . '">' . $details['label'] . '</label></li>';
}
}
?>
</ul>
<h2><?php _e( 'Other Settings', 'site-health-tool-manager' ); ?></h2>
<ul>
<li>
<input type="checkbox" name="widget" id="widget-setting"
<?php
if ( $widget ) {
echo 'checked="checked"';
}
?>
/>
<label for="widget-setting"><?php _e( 'Dashboard Widget Enabled', 'site-health-tool-manager' ); ?></label>
</li>
</ul>
<p>
<input class="button button-primary" type="submit" value="<?php _e( 'Save Settings', 'site-health-tool-manager' ); ?>" name="submit" />
</p>
</form>
</div>
<?php
}