On this page
WP_Site_Health::check_for_page_caching(): WP_Error|array
This function’s access is marked private. This means it is not intended for use by plugin or theme developers, only in other core functions. It is listed here for completeness.
Checks if site has page cache enabled or not.
Return
WP_Error|array Page cache detection details or else error information.
advanced_cache_presentboolWhether a page cache plugin is present.page_caching_response_headersarray[]Sets of client caching headers for the responses.response_timingfloat[]Response timings.
Source
File: wp-admin/includes/class-wp-site-health.php. View all references
private function check_for_page_caching() {
/** This filter is documented in wp-includes/class-wp-http-streams.php */
$sslverify = apply_filters( 'https_local_ssl_verify', false );
$headers = array();
// Include basic auth in loopback requests. Note that this will only pass along basic auth when user is
// initiating the test. If a site requires basic auth, the test will fail when it runs in WP Cron as part of
// wp_site_health_scheduled_check. This logic is copied from WP_Site_Health::can_perform_loopback().
if ( isset( $_SERVER['PHP_AUTH_USER'] ) && isset( $_SERVER['PHP_AUTH_PW'] ) ) {
$headers['Authorization'] = 'Basic ' . base64_encode( wp_unslash( $_SERVER['PHP_AUTH_USER'] ) . ':' . wp_unslash( $_SERVER['PHP_AUTH_PW'] ) );
}
$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' ) );
$end_time = microtime( true );
if ( is_wp_error( $http_response ) ) {
return $http_response;
}
if ( wp_remote_retrieve_response_code( $http_response ) !== 200 ) {
return new WP_Error(
'http_' . wp_remote_retrieve_response_code( $http_response ),
wp_remote_retrieve_response_message( $http_response )
);
}
$response_headers = array();
foreach ( $caching_headers as $header => $callback ) {
$header_values = wp_remote_retrieve_header( $http_response, $header );
if ( empty( $header_values ) ) {
continue;
}
$header_values = (array) $header_values;
if ( empty( $callback ) || ( is_callable( $callback ) && count( array_filter( $header_values, $callback ) ) > 0 ) ) {
$response_headers[ $header ] = $header_values;
}
}
$page_caching_response_headers[] = $response_headers;
$response_timing[] = ( $end_time - $start_time ) * 1000;
}
return array(
'advanced_cache_present' => (
file_exists( WP_CONTENT_DIR . '/advanced-cache.php' )
&&
( defined( 'WP_CACHE' ) && WP_CACHE )
&&
/** This filter is documented in wp-settings.php */
apply_filters( 'enable_loading_advanced_cache_dropin', true )
),
'page_caching_response_headers' => $page_caching_response_headers,
'response_timing' => $response_timing,
);
}
Hooks
- apply_filters( 'enable_loading_advanced_cache_dropin',
bool $enable_advanced_cache ) -
Filters whether to enable loading of the advanced-cache.php drop-in.
- apply_filters( 'https_local_ssl_verify',
bool $ssl_verify ,string $url ) -
Filters whether SSL should be verified for local HTTP API requests.
Related
Uses
| Uses | Description |
|---|---|
| WP_Site_Health::get_page_cache_headers() wp-admin/includes/class-wp-site-health.php | Returns a list of headers and its verification callback to verify if page cache is enabled or not. |
| wp_remote_get() wp-includes/http.php | Performs an HTTP request using the GET method and returns its response. |
| wp_remote_retrieve_response_code() wp-includes/http.php | Retrieve only the response code from the raw response. |
| wp_remote_retrieve_response_message() wp-includes/http.php | Retrieve only the response message from the raw response. |
| wp_remote_retrieve_header() wp-includes/http.php | Retrieve a single header by name from the raw response. |
| wp_unslash() wp-includes/formatting.php | Removes slashes from a string or recursively removes slashes from strings within an array. |
| home_url() wp-includes/link-template.php | Retrieves the URL for the current site where the front end is accessible. |
| apply_filters() wp-includes/plugin.php | Calls the callback functions that have been added to a filter hook. |
| is_wp_error() wp-includes/load.php | Checks whether the given variable is a WordPress Error. |
| WP_Error::__construct() wp-includes/class-wp-error.php | Initializes the error. |
Used By
| Used By | Description |
|---|---|
| WP_Site_Health::get_page_cache_detail() wp-admin/includes/class-wp-site-health.php | Gets page cache details. |
Changelog
| Version | Description |
|---|---|
| 6.1.0 | Introduced. |
© 2003–2022 WordPress Foundation
Licensed under the GNU GPLv2+ License.
https://developer.wordpress.org/reference/classes/wp_site_health/check_for_page_caching