From 1033825688f0f7e1deb35073235d0622365bfa35 Mon Sep 17 00:00:00 2001 From: ZetRider Date: Mon, 23 Sep 2024 22:55:35 +0200 Subject: [PATCH] Add a filter to customize the URL for page caching check in Site Health --- .../includes/class-wp-site-health.php | 3 ++- tests/phpunit/tests/admin/wpSiteHealth.php | 27 ++++++++++++++++--- 2 files changed, 26 insertions(+), 4 deletions(-) diff --git a/src/wp-admin/includes/class-wp-site-health.php b/src/wp-admin/includes/class-wp-site-health.php index b35c595c3cf61..0877fe86b50eb 100644 --- a/src/wp-admin/includes/class-wp-site-health.php +++ b/src/wp-admin/includes/class-wp-site-health.php @@ -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 ) ) { diff --git a/tests/phpunit/tests/admin/wpSiteHealth.php b/tests/phpunit/tests/admin/wpSiteHealth.php index 4f3ac99cf2489..a855151623633 100644 --- a/tests/phpunit/tests/admin/wpSiteHealth.php +++ b/tests/phpunit/tests/admin/wpSiteHealth.php @@ -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' ), @@ -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 ); @@ -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( @@ -234,7 +247,7 @@ function ( $response, $parsed_args ) use ( &$responses, &$is_unauthorized, $good ); }, 20, - 2 + 3 ); $actual = $this->instance->get_test_page_cache(); @@ -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',