Make WordPress Core

Opened 4 years ago

Closed 4 years ago

Last modified 4 years ago

#49943 closed defect (bug) (fixed)

Async Site Health tests can throw PHP notices

Reported by: schlessera's profile schlessera Owned by: sergeybiryukov's profile SergeyBiryukov
Milestone: 5.4.2 Priority: normal
Severity: normal Version: 5.4
Component: Site Health Keywords: has-patch good-first-bug fixed-major
Focuses: Cc:

Description

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
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
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

Attachments (1)

49943.diff (791 bytes) - added by oakesjosh 4 years ago.

Download all attachments as: .zip

Change History (9)

#1 @SergeyBiryukov
4 years ago

  • Keywords needs-patch good-first-bug added
  • Milestone changed from Awaiting Review to 5.5

@oakesjosh
4 years ago

#2 @SergeyBiryukov
4 years ago

  • Owner set to SergeyBiryukov
  • Resolution set to fixed
  • Status changed from new to closed

In 47628:

Site Health: Avoid a PHP notice in WP_Site_Health::wp_cron_scheduled_check() if async test response does not contain the expected result.

Additionally, avoid a PHP notice in ::get_test_php_version() if the minimum recommended version of PHP could not be determined.

Props schlessera, oakesjosh.
Fixes #49943.

#3 @SergeyBiryukov
4 years ago

#50341 was marked as a duplicate.

#4 @SergeyBiryukov
4 years ago

#50237 was marked as a duplicate.

#5 @SergeyBiryukov
4 years ago

  • Keywords has-patch fixed-major added; needs-patch removed
  • Resolution fixed deleted
  • Status changed from closed to reopened

Since this was introduced in 5.4 and there are already a few reports of these notices, let's backport the fix to 5.4.2.

Last edited 4 years ago by SergeyBiryukov (previous) (diff)

#6 @SergeyBiryukov
4 years ago

  • Resolution set to fixed
  • Status changed from reopened to closed

In 47931:

Site Health: Avoid a PHP notice in WP_Site_Health::wp_cron_scheduled_check() if async test response does not contain the expected result.

Additionally, avoid a PHP notice in ::get_test_php_version() if the minimum recommended version of PHP could not be determined.

Props schlessera, oakesjosh.
Merges [47628] to the 5.4 branch.
Fixes #49943.

#7 @SergeyBiryukov
4 years ago

  • Milestone changed from 5.5 to 5.4.2

#8 @SergeyBiryukov
4 years ago

#50378 was marked as a duplicate.

Note: See TracTickets for help on using tickets.