<?php

//last-modified header
add_action( 'template_redirect', 'add_header_last_modified' );

function add_header_last_modified() {
	if ( ( defined( 'DOING_AJAX' ) && DOING_AJAX ) || ( defined( 'XMLRPC_REQUEST' ) && XMLRPC_REQUEST ) || ( defined( 'REST_REQUEST' ) && REST_REQUEST ) || ( is_admin() ) ) {
		return;
	}
	$last_modified = '';
	// Для страниц и записей
	if ( is_singular() ) {
		global $post;
		$post_time     = strtotime( $post->post_modified_gmt );
		$modified_time = $post_time;
		$last_modified = str_replace( '+0000', 'GMT', gmdate( 'r', $modified_time ) );
	}
	// Cтраницы архивов: рубрики, метки, даты и тому подобное
	if ( is_archive() || is_home() ) {
		global $posts;
		if ( empty( $posts ) ) {
			return;
		}
		$post = $posts[0];
		if ( ! isset( $post->post_modified_gmt ) ) {
			return;
		}
		$post_time     = strtotime( $post->post_modified_gmt );
		$modified_time = $post_time;
		$last_modified = str_replace( '+0000', 'GMT', gmdate( 'r', $modified_time ) );
	}
	// Если заголовки уже отправлены - ничего не делаем
	if ( headers_sent() ) {
		return;
	}
	if ( ! empty( $last_modified ) ) {
		header( 'Last-Modified: ' . $last_modified );
		if ( ! is_user_logged_in() ) {
			if ( isset( $_SERVER['HTTP_IF_MODIFIED_SINCE'] ) && strtotime( $_SERVER['HTTP_IF_MODIFIED_SINCE'] ) >= $modified_time ) {
				$protocol = ( isset( $_SERVER['SERVER_PROTOCOL'] ) ? $_SERVER['SERVER_PROTOCOL'] : 'HTTP/1.1' );
				header( $protocol . ' 304 Not Modified' );
			}
		}
	}
}
