WP_Site_Health::get_page_cache_headerspublicWP 6.1.0

Returns a mapping from response headers to an optional callback to verify if page cache is enabled or not.

Метод класса: WP_Site_Health{}

Возвращает

Массив<Строку,. ?callable> Mapping of page caching headers and their (optional) verification callbacks. A null value means a simple existence check is used for the header.

Использование

$WP_Site_Health = new WP_Site_Health();
$WP_Site_Health->get_page_cache_headers(): array;

Список изменений

С версии 6.1.0 Введена.

Код WP_Site_Health::get_page_cache_headers() WP 7.0

public function get_page_cache_headers(): array {

	$cache_hit_callback = static function ( $header_value ) {
		return 1 === preg_match( '/(^| |,)HIT(,| |$)/i', $header_value );
	};

	$cache_headers = array(
		// Standard HTTP caching headers.
		'cache-control'          => static function ( $header_value ) {
			return (bool) preg_match( '/max-age=[1-9]/', $header_value );
		},
		'expires'                => static function ( $header_value ) {
			return strtotime( $header_value ) > time();
		},
		'age'                    => static function ( $header_value ) {
			return is_numeric( $header_value ) && $header_value > 0;
		},
		'last-modified'          => null,
		'etag'                   => null,
		'via'                    => null,

		/**
		 * Custom caching headers.
		 *
		 * These do not seem to be actually used by any caching layers. There were first introduced in a Site Health
		 * test in the AMP plugin. They were copied into the Performance Lab plugin's Site Health test before they
		 * were merged into core.
		 *
		 * @link https://github.com/ampproject/amp-wp/pull/6849
		 * @link https://github.com/WordPress/performance/pull/263
		 * @link https://core.trac.wordpress.org/changeset/54043
		 */
		'x-cache-enabled'        => static function ( $header_value ) {
			return ( 'true' === strtolower( $header_value ) );
		},
		'x-cache-disabled'       => static function ( $header_value ) {
			return ( 'on' !== strtolower( $header_value ) );
		},

		/**
		 * CloudFlare.
		 *
		 * @link https://developers.cloudflare.com/cache/concepts/cache-responses/
		 */
		'cf-cache-status'        => $cache_hit_callback,

		/**
		 * Fastly.
		 *
		 * @link https://www.fastly.com/documentation/reference/http/http-headers/X-Cache/
		 */
		'x-cache'                => $cache_hit_callback,

		/**
		 * LightSpeed.
		 *
		 * @link https://docs.litespeedtech.com/lscache/devguide/controls/#x-litespeed-cache
		 */
		'x-litespeed-cache'      => $cache_hit_callback,

		/**
		 * OpenResty srcache-nginx-module.
		 *
		 * The `x-srcache-store-status` header indicates if the response was stored in the cache.
		 * Valid values include `STORE` and `BYPASS`.
		 *
		 * The `x-srcache-fetch-status` header indicates if the response was fetched from the cache.
		 * Valid values include `HIT`, `MISS`, and `BYPASS`.
		 *
		 * @link https://github.com/openresty/srcache-nginx-module
		 */
		'x-srcache-store-status' => static function ( $header_value ) {
			return 'store' === strtolower( $header_value );
		},
		'x-srcache-fetch-status' => $cache_hit_callback,

		/**
		 * Nginx.
		 *
		 * @link https://blog.nginx.org/blog/nginx-caching-guide
		 * @link https://www.inmotionhosting.com/support/website/nginx-cache-management/
		 */
		'x-cache-status'         => $cache_hit_callback,
		'x-proxy-cache'          => $cache_hit_callback,

		/**
		 * Varnish Cache.
		 *
		 * A header with a single number indicates it was not cached. If there are two numbers (or more), then this
		 * indicates the response was cached.
		 *
		 * @link https://vinyl-cache.org/docs/2.1/faq/http.html
		 * @link https://www.fastly.com/documentation/reference/http/http-headers/X-Varnish/
		 * @link https://www.linuxjournal.com/content/speed-your-web-site-varnish
		 */
		'x-varnish'              => static function ( $header_value ) {
			return 1 === preg_match( '/^\d+ \d+/', $header_value );
		},
	);

	/**
	 * Filters the list of cache headers supported by core.
	 *
	 * This list indicates how each of the specified headers will be checked to indicate if a page cache is enabled
	 * or not. WordPress checks for each of the headers in the returned array. If the callback is provided, it will
	 * be passed the value for the corresponding header and return a boolean value indicating if the header suggests
	 * that a cache is active. If the value is `null` for the header, then WordPress will assume that a cache is
	 * active if the header is present, regardless of its value.
	 *
	 * @since 6.1.0
	 *
	 * @param array<string, ?callable> $cache_headers Mapping from cache-related HTTP headers to whether they
	 *                                                indicate if a page cache is enabled for the site. `null`
	 *                                                indicates caching in the presence of the header; a callback is
	 *                                                provided the header’s value and should return `true` if it
	 *                                                implies that a cache is active.
	 */
	return (array) apply_filters( 'site_status_page_cache_supported_cache_headers', $cache_headers );
}