[wp-trac] [WordPress Trac] #49943: Async Site Health tests can throw PHP notices
WordPress Trac
noreply at wordpress.org
Sat Apr 18 06:12:34 UTC 2020
#49943: Async Site Health tests can throw PHP notices
--------------------------+-----------------------------
Reporter: schlessera | Owner: (none)
Type: defect (bug) | Status: new
Priority: normal | Milestone: Awaiting Review
Component: Site Health | Version: 5.4
Severity: normal | Keywords:
Focuses: |
--------------------------+-----------------------------
In the logic for checking asynchronous tests in the
`wp_cron_scheduled_check()` method of the `WP_Site_Health` class, the
following code is used to fetch the test result:
{{{#!php
<?php
if ( ! is_wp_error( $result_fetch ) ) {
$results[] = json_decode( wp_remote_retrieve_body( $result_fetch )
);
} else {
$results[] = array(
'status' => 'recommended',
'label' => __( 'A test is unavailable' ),
);
}
}}}
If that request "succeeds" (meaning it didn't produce a `WP_Error`), but
the response is not what was expected (like returning a page of HTML), the
code immediately following will throw a PHP notice because it
`json_decode()` will fail to decode the response's body and thus return
`null`:
{{{#!php
<?php
foreach ( $results as $result ) {
if ( 'critical' === $result['status'] ) {
$site_status['critical']++;
} elseif ( 'recommended' === $result['status'] ) {
$site_status['recommended']++;
} else {
$site_status['good']++;
}
}
}}}
The notice that is thrown is:
{{{
Notice: Trying to access array offset on value of type null in wp-
admin/includes/class-wp-site-health.php on line 2303
}}}
--
Ticket URL: <https://core.trac.wordpress.org/ticket/49943>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform
More information about the wp-trac
mailing list