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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
|
<?php
/**
* REST API: WP_REST_Site_Health_Controller class
*
* @package WordPress
* @subpackage REST_API
* @since 5.6.0
*/
/**
* Core class for interacting with Site Health tests.
*
* @since 5.6.0
*
* @see WP_REST_Controller
*/
class WP_REST_Site_Health_Controller extends WP_REST_Controller {
/**
* An instance of the site health class.
*
* @since 5.6.0
*
* @var WP_Site_Health
*/
private $site_health;
/**
* Site Health controller constructor.
*
* @since 5.6.0
*
* @param WP_Site_Health $site_health An instance of the site health class.
*/
public function __construct( $site_health ) {
$this->namespace = 'wp-site-health/v1';
$this->rest_base = 'tests';
$this->site_health = $site_health;
}
/**
* Registers API routes.
*
* @since 5.6.0
* @since 6.1.0 Adds page-cache async test.
*
* @see register_rest_route()
*/
public function register_routes() {
register_rest_route(
$this->namespace,
sprintf(
'/%s/%s',
$this->rest_base,
'background-updates'
),
array(
array(
'methods' => 'GET',
'callback' => array( $this, 'test_background_updates' ),
'permission_callback' => function () {
return $this->validate_request_permission( 'background_updates' );
},
),
'schema' => array( $this, 'get_public_item_schema' ),
)
);
register_rest_route(
$this->namespace,
sprintf(
'/%s/%s',
$this->rest_base,
'loopback-requests'
),
array(
array(
'methods' => 'GET',
'callback' => array( $this, 'test_loopback_requests' ),
'permission_callback' => function () {
return $this->validate_request_permission( 'loopback_requests' );
},
),
'schema' => array( $this, 'get_public_item_schema' ),
)
);
register_rest_route(
$this->namespace,
sprintf(
'/%s/%s',
$this->rest_base,
'https-status'
),
array(
array(
'methods' => 'GET',
'callback' => array( $this, 'test_https_status' ),
'permission_callback' => function () {
return $this->validate_request_permission( 'https_status' );
},
),
'schema' => array( $this, 'get_public_item_schema' ),
)
);
register_rest_route(
$this->namespace,
sprintf(
'/%s/%s',
$this->rest_base,
'dotorg-communication'
),
array(
array(
'methods' => 'GET',
'callback' => array( $this, 'test_dotorg_communication' ),
'permission_callback' => function () {
return $this->validate_request_permission( 'dotorg_communication' );
},
),
'schema' => array( $this, 'get_public_item_schema' ),
)
);
register_rest_route(
$this->namespace,
sprintf(
'/%s/%s',
$this->rest_base,
'authorization-header'
),
array(
array(
'methods' => 'GET',
'callback' => array( $this, 'test_authorization_header' ),
'permission_callback' => function () {
return $this->validate_request_permission( 'authorization_header' );
},
),
'schema' => array( $this, 'get_public_item_schema' ),
)
);
register_rest_route(
$this->namespace,
sprintf(
'/%s',
'directory-sizes'
),
array(
'methods' => 'GET',
'callback' => array( $this, 'get_directory_sizes' ),
'permission_callback' => function () {
return $this->validate_request_permission( 'directory_sizes' ) && ! is_multisite();
},
)
);
register_rest_route(
$this->namespace,
sprintf(
'/%s/%s',
$this->rest_base,
'page-cache'
),
array(
array(
'methods' => 'GET',
'callback' => array( $this, 'test_page_cache' ),
'permission_callback' => function () {
return $this->validate_request_permission( 'page_cache' );
},
),
)
);
}
/**
* Validates if the current user can request this REST endpoint.
*
* @since 5.6.0
*
* @param string $check The endpoint check being ran.
* @return bool
*/
protected function validate_request_permission( $check ) {
$default_capability = 'view_site_health_checks';
/**
* Filters the capability needed to run a given Site Health check.
*
* @since 5.6.0
*
* @param string $default_capability The default capability required for this check.
* @param string $check The Site Health check being performed.
*/
$capability = apply_filters( "site_health_test_rest_capability_{$check}", $default_capability, $check );
return current_user_can( $capability );
}
/**
* Checks if background updates work as expected.
*
* @since 5.6.0
*
* @return array
*/
public function test_background_updates() {
$this->load_admin_textdomain();
return $this->site_health->get_test_background_updates();
}
/**
* Checks that the site can reach the WordPress.org API.
*
* @since 5.6.0
*
* @return array
*/
public function test_dotorg_communication() {
$this->load_admin_textdomain();
return $this->site_health->get_test_dotorg_communication();
}
/**
* Checks that loopbacks can be performed.
*
* @since 5.6.0
*
* @return array
*/
public function test_loopback_requests() {
$this->load_admin_textdomain();
return $this->site_health->get_test_loopback_requests();
}
/**
* Checks that the site's frontend can be accessed over HTTPS.
*
* @since 5.7.0
*
* @return array
*/
public function test_https_status() {
$this->load_admin_textdomain();
return $this->site_health->get_test_https_status();
}
/**
* Checks that the authorization header is valid.
*
* @since 5.6.0
*
* @return array
*/
public function test_authorization_header() {
$this->load_admin_textdomain();
return $this->site_health->get_test_authorization_header();
}
/**
* Checks that full page cache is active.
*
* @since 6.1.0
*
* @return array The test result.
*/
public function test_page_cache() {
$this->load_admin_textdomain();
return $this->site_health->get_test_page_cache();
}
/**
* Gets the current directory sizes for this install.
*
* @since 5.6.0
*
* @return array|WP_Error
*/
public function get_directory_sizes() {
if ( ! class_exists( 'WP_Debug_Data' ) ) {
require_once ABSPATH . 'wp-admin/includes/class-wp-debug-data.php';
}
$this->load_admin_textdomain();
$sizes_data = WP_Debug_Data::get_sizes();
$all_sizes = array( 'raw' => 0 );
foreach ( $sizes_data as $name => $value ) {
$name = sanitize_text_field( $name );
$data = array();
if ( isset( $value['size'] ) ) {
if ( is_string( $value['size'] ) ) {
$data['size'] = sanitize_text_field( $value['size'] );
} else {
$data['size'] = (int) $value['size'];
}
}
if ( isset( $value['debug'] ) ) {
if ( is_string( $value['debug'] ) ) {
$data['debug'] = sanitize_text_field( $value['debug'] );
} else {
$data['debug'] = (int) $value['debug'];
}
}
if ( ! empty( $value['raw'] ) ) {
$data['raw'] = (int) $value['raw'];
}
$all_sizes[ $name ] = $data;
}
if ( isset( $all_sizes['total_size']['debug'] ) && 'not available' === $all_sizes['total_size']['debug'] ) {
return new WP_Error( 'not_available', __( 'Directory sizes could not be returned.' ), array( 'status' => 500 ) );
}
return $all_sizes;
}
/**
* Loads the admin textdomain for Site Health tests.
*
* The {@see WP_Site_Health} class is defined in WP-Admin, while the REST API operates in a front-end context.
* This means that the translations for Site Health won't be loaded by default in {@see load_default_textdomain()}.
*
* @since 5.6.0
*/
protected function load_admin_textdomain() {
// Accounts for inner REST API requests in the admin.
if ( ! is_admin() ) {
$locale = determine_locale();
load_textdomain( 'default', WP_LANG_DIR . "/admin-$locale.mo", $locale );
}
}
/**
* Gets the schema for each site health test.
*
* @since 5.6.0
*
* @return array The test schema.
*/
public function get_item_schema() {
if ( $this->schema ) {
return $this->schema;
}
$this->schema = array(
'$schema' => 'http://json-schema.org/draft-04/schema#',
'title' => 'wp-site-health-test',
'type' => 'object',
'properties' => array(
'test' => array(
'type' => 'string',
'description' => __( 'The name of the test being run.' ),
'readonly' => true,
),
'label' => array(
'type' => 'string',
'description' => __( 'A label describing the test.' ),
'readonly' => true,
),
'status' => array(
'type' => 'string',
'description' => __( 'The status of the test.' ),
'enum' => array( 'good', 'recommended', 'critical' ),
'readonly' => true,
),
'badge' => array(
'type' => 'object',
'description' => __( 'The category this test is grouped in.' ),
'properties' => array(
'label' => array(
'type' => 'string',
'readonly' => true,
),
'color' => array(
'type' => 'string',
'enum' => array( 'blue', 'orange', 'red', 'green', 'purple', 'gray' ),
'readonly' => true,
),
),
'readonly' => true,
),
'description' => array(
'type' => 'string',
'description' => __( 'A more descriptive explanation of what the test looks for, and why it is important for the user.' ),
'readonly' => true,
),
'actions' => array(
'type' => 'string',
'description' => __( 'HTML containing an action to direct the user to where they can resolve the issue.' ),
'readonly' => true,
),
),
);
return $this->schema;
}
}
|