WP_Site_Health::check_for_page_caching()privateWP 6.1.0

Checks if site has page cache enabled or not.

Method of the class: WP_Site_Health{}

Return

WP_Error|Array. Page cache detection details or else error information.

Usage

// private - for code of main (parent) class only
$result = $this->check_for_page_caching();

Changelog

Since 6.1.0 Introduced.

WP_Site_Health::check_for_page_caching() code WP 6.4.3

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,
	);
}