Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
westonruter opened this issue Feb 24, 2022 · 2 comments · Fixed by #6938
Labels
Bug Something isn't working Changelogged Whether the issue/PR has been added to release notes. P0 High priority Site Health
Milestone

Comments

@westonruter
Copy link
Member

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

@milindmore22
Copy link
Collaborator

another user reported error

@dhaval-parekh
Copy link
Collaborator

QA Passed

With then new changes Fatal error is not occurring when wp_remote_retrieve_header() returning arrays here.

Notes: I couldn't able to reproduce this issue without modifying the code, And testing is done after tweaking the code to reproduce this issue.

@westonruter westonruter added the Changelogged Whether the issue/PR has been added to release notes. label Apr 4, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug Something isn't working Changelogged Whether the issue/PR has been added to release notes. P0 High priority Site Health
Projects
None yet
3 participants