W3cubDocs

/WordPress

WP_Site_Health::check_for_page_caching(): WP_Error|array

Checks if site has page cache enabled or not.

Return

WP_Error|array Page cache detection details or else error information.
  • advanced_cache_present bool
    Whether a page cache plugin is present.
  • page_caching_response_headers array[]
    Sets of client caching headers for the responses.
  • response_timing float[]
    Response timings.

Source

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|string $ssl_verify, string $url )

Filters whether SSL should be verified for local HTTP API requests.

Changelog

Version Description
6.1.0 Introduced.

© 2003–2024 WordPress Foundation
Licensed under the GNU GPLv2+ License.
https://developer.wordpress.org/reference/classes/wp_site_health/check_for_page_caching