Skip to content

Commit

Permalink
Add a filter to customize the URL for page caching check in Site Health
Browse files Browse the repository at this point in the history
  • Loading branch information
zetrider committed Sep 23, 2024
1 parent fd104ae commit 1033825
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 4 deletions.
3 changes: 2 additions & 1 deletion src/wp-admin/includes/class-wp-site-health.php
Original file line number Diff line number Diff line change
Expand Up @@ -3382,13 +3382,14 @@ private function check_for_page_caching() {
$headers['Authorization'] = 'Basic ' . base64_encode( wp_unslash( $_SERVER['PHP_AUTH_USER'] ) . ':' . wp_unslash( $_SERVER['PHP_AUTH_PW'] ) );
}

$caching_url = apply_filters( 'site_status_persistent_page_cache_url', home_url( '/' ) );
$caching_headers = $this->get_page_cache_headers();
$page_caching_response_headers = array();
$response_timing = array();

for ( $i = 1; $i <= 3; $i++ ) {
$start_time = microtime( true );
$http_response = wp_remote_get( home_url( '/' ), compact( 'sslverify', 'headers' ) );
$http_response = wp_remote_get( $caching_url, compact( 'sslverify', 'headers' ) );
$end_time = microtime( true );

if ( is_wp_error( $http_response ) ) {
Expand Down
27 changes: 24 additions & 3 deletions tests/phpunit/tests/admin/wpSiteHealth.php
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ public function data_cron_health_checks() {
* @covers ::get_page_cache_headers()
* @covers ::check_for_page_caching()
*/
public function test_get_page_cache( $responses, $expected_status, $expected_label, $good_basic_auth = null, $delay_the_response = false ) {
public function test_get_page_cache( $responses, $expected_status, $expected_label, $good_basic_auth = null, $delay_the_response = false, $expected_url = null ) {
$expected_props = array(
'badge' => array(
'label' => __( 'Performance' ),
Expand All @@ -195,9 +195,20 @@ static function () use ( $threshold ) {
);
}

if ( $expected_url ) {
add_filter(
'site_status_persistent_page_cache_url',
static function () use ( $expected_url ) {
return $expected_url;
}
);
} else {
$expected_url = home_url( '/' );
}

add_filter(
'pre_http_request',
function ( $response, $parsed_args ) use ( &$responses, &$is_unauthorized, $good_basic_auth, $delay_the_response, $threshold ) {
function ( $response, $parsed_args, $url ) use ( &$responses, &$is_unauthorized, $good_basic_auth, $delay_the_response, $threshold, $expected_url ) {

$expected_response = array_shift( $responses );

Expand Down Expand Up @@ -225,6 +236,8 @@ function ( $response, $parsed_args ) use ( &$responses, &$is_unauthorized, $good

$this->assertIsArray( $expected_response );

$this->assertEquals( $expected_url, $url );

return array(
'headers' => $expected_response,
'response' => array(
Expand All @@ -234,7 +247,7 @@ function ( $response, $parsed_args ) use ( &$responses, &$is_unauthorized, $good
);
},
20,
2
3
);

$actual = $this->instance->get_test_page_cache();
Expand Down Expand Up @@ -284,6 +297,14 @@ public function data_get_page_cache() {
'good_basic_auth' => null,
'delay_the_response' => true,
),
'set-persistent-page-cache-url' => array(
'responses' => array_fill( 0, 3, array( 'cache-control' => 'no-cache' ) ),
'expected_status' => 'recommended',
'expected_label' => $recommended_label,
'good_basic_auth' => null,
'delay_the_response' => false,
'expected_url' => 'https://example.com/?page=7',
),
'no-cache' => array(
'responses' => array_fill( 0, 3, array( 'cache-control' => 'no-cache' ) ),
'expected_status' => 'recommended',
Expand Down

0 comments on commit 1033825

Please sign in to comment.