Skip to content

Site Health test for page caching does not account for wp_remote_retrieve_header() returning arrays #6937

@westonruter

Description

@westonruter

Bug Description

As reported in a support forum topic, an error can occur in the page caching detection test:

An error of type E_ERROR was caused in line 548 of the file /var/www/html/a-c-d.net/public_html/wp-content/plugins/amp/src/Admin/SiteHealth.php. Error message: Uncaught TypeError: preg_match(): Argument #2 ($subject) must be of type string, array given in /var/www/html/a-c-d.net/public_html/wp-content/plugins/amp/src/Admin/SiteHealth.php:548
Stack trace:
#0 /var/www/html/a-c-d.net/public_html/wp-content/plugins/amp/src/Admin/SiteHealth.php(548): preg_match()
#1 /var/www/html/a-c-d.net/public_html/wp-content/plugins/amp/src/Admin/SiteHealth.php(634): AmpProject\AmpWP\Admin\SiteHealth::AmpProject\AmpWP\Admin\{closure}()
#2 /var/www/html/a-c-d.net/public_html/wp-content/plugins/amp/src/Admin/SiteHealth.php(491): AmpProject\AmpWP\Admin\SiteHealth->check_for_page_caching()
#3 /var/www/html/a-c-d.net/public_html/wp-content/plugins/amp/src/Admin/OptionsMenu.php(230): AmpProject\AmpWP\Admin\SiteHealth->get_page_cache_detail()
#4 /var/www/html/a-c-d.net/public_html/wp-includes/class-wp-hook.php(307): AmpProject\AmpWP\Admin\OptionsMenu->enqueue_assets()
#5 /var/www/html/a-c-d.net/public_html/wp-includes/class-wp-hook.php(331): WP_Hook->apply_filters()
#6 /var/www/html/a-c-d.net/public_html/wp-includes/plugin.php(474): WP_Hook->do_action()
#7 /var/www/html/a-c-d.net/public_html/wp-admin/admin-header.php(118): do_action()
#8 /var/www/html/a-c-d.net/public_html/wp-admin/admin.php(239): require_once(‘…’)

The preg_match() call in question is here:

'cache-control' => static function ( $header_value ) {
return (bool) preg_match( '/max-age=[1-9]/', $header_value );
},

The $header_value is being passed here:

foreach ( $caching_headers as $header => $callback ) {
$header_value = wp_remote_retrieve_header( $http_response, $header );
if (
$header_value
&&
(
empty( $callback )
||
( is_callable( $callback ) && true === $callback( $header_value ) )
)
) {
$response_headers[ $header ] = $header_value;
}
}

So apparently we're not accounting for wp_remote_retrieve_header() returning an array instead of a string.

Note the site is running PHP 8.1 which is the cause for the fatal due to the additional strictness.

Expected Behaviour

No error should occur.

Screenshots

No response

PHP Version

8.1

Plugin Version

2.1.4

AMP plugin template mode

Standard, Transitional, Reader

WordPress Version

No response

Site Health

No response

Gutenberg Version

No response

OS(s) Affected

No response

Browser(s) Affected

No response

Device(s) Affected

No response

Acceptance Criteria

No response

Implementation Brief

No response

QA Testing Instructions

No response

Demo

No response

Changelog Entry

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    BugSomething isn't workingChangeloggedWhether the issue/PR has been added to release notes.P0High prioritySite Health

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions