WP::send_headers()publicWP 2.0.0

Sends additional HTTP headers for caching, content type, etc.

Sets the Content-Type header. Sets the 'error' status (if passed) and optionally exits. If showing a feed, it will also send Last-Modified, ETag, and 304 status if needed.

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

Хуки из метода

Возвращает

null. Ничего (null).

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

global $wp;
$wp->send_headers();

Заметки

  • Global. WP_Query. $wp_query WordPress Query object.

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

С версии 2.0.0 Введена.
С версии 4.4.0 X-Pingback header is added conditionally for single posts that allow pings.
С версии 6.1.0 Runs after posts have been queried.

Код WP::send_headers() WP 6.6.2

public function send_headers() {
	global $wp_query;

	$headers       = array();
	$status        = null;
	$exit_required = false;
	$date_format   = 'D, d M Y H:i:s';

	if ( is_user_logged_in() ) {
		$headers = array_merge( $headers, wp_get_nocache_headers() );
	} elseif ( ! empty( $_GET['unapproved'] ) && ! empty( $_GET['moderation-hash'] ) ) {
		// Unmoderated comments are only visible for 10 minutes via the moderation hash.
		$expires = 10 * MINUTE_IN_SECONDS;

		$headers['Expires']       = gmdate( $date_format, time() + $expires );
		$headers['Cache-Control'] = sprintf(
			'max-age=%d, must-revalidate',
			$expires
		);
	}
	if ( ! empty( $this->query_vars['error'] ) ) {
		$status = (int) $this->query_vars['error'];

		if ( 404 === $status ) {
			if ( ! is_user_logged_in() ) {
				$headers = array_merge( $headers, wp_get_nocache_headers() );
			}

			$headers['Content-Type'] = get_option( 'html_type' ) . '; charset=' . get_option( 'blog_charset' );
		} elseif ( in_array( $status, array( 403, 500, 502, 503 ), true ) ) {
			$exit_required = true;
		}
	} elseif ( empty( $this->query_vars['feed'] ) ) {
		$headers['Content-Type'] = get_option( 'html_type' ) . '; charset=' . get_option( 'blog_charset' );
	} else {
		// Set the correct content type for feeds.
		$type = $this->query_vars['feed'];
		if ( 'feed' === $this->query_vars['feed'] ) {
			$type = get_default_feed();
		}

		$headers['Content-Type'] = feed_content_type( $type ) . '; charset=' . get_option( 'blog_charset' );

		// We're showing a feed, so WP is indeed the only thing that last changed.
		if ( ! empty( $this->query_vars['withcomments'] )
			|| str_contains( $this->query_vars['feed'], 'comments-' )
			|| ( empty( $this->query_vars['withoutcomments'] )
				&& ( ! empty( $this->query_vars['p'] )
					|| ! empty( $this->query_vars['name'] )
					|| ! empty( $this->query_vars['page_id'] )
					|| ! empty( $this->query_vars['pagename'] )
					|| ! empty( $this->query_vars['attachment'] )
					|| ! empty( $this->query_vars['attachment_id'] )
				)
			)
		) {
			$wp_last_modified_post    = mysql2date( $date_format, get_lastpostmodified( 'GMT' ), false );
			$wp_last_modified_comment = mysql2date( $date_format, get_lastcommentmodified( 'GMT' ), false );

			if ( strtotime( $wp_last_modified_post ) > strtotime( $wp_last_modified_comment ) ) {
				$wp_last_modified = $wp_last_modified_post;
			} else {
				$wp_last_modified = $wp_last_modified_comment;
			}
		} else {
			$wp_last_modified = mysql2date( $date_format, get_lastpostmodified( 'GMT' ), false );
		}

		if ( ! $wp_last_modified ) {
			$wp_last_modified = gmdate( $date_format );
		}

		$wp_last_modified .= ' GMT';
		$wp_etag           = '"' . md5( $wp_last_modified ) . '"';

		$headers['Last-Modified'] = $wp_last_modified;
		$headers['ETag']          = $wp_etag;

		// Support for conditional GET.
		if ( isset( $_SERVER['HTTP_IF_NONE_MATCH'] ) ) {
			$client_etag = wp_unslash( $_SERVER['HTTP_IF_NONE_MATCH'] );
		} else {
			$client_etag = '';
		}

		if ( isset( $_SERVER['HTTP_IF_MODIFIED_SINCE'] ) ) {
			$client_last_modified = trim( $_SERVER['HTTP_IF_MODIFIED_SINCE'] );
		} else {
			$client_last_modified = '';
		}

		// If string is empty, return 0. If not, attempt to parse into a timestamp.
		$client_modified_timestamp = $client_last_modified ? strtotime( $client_last_modified ) : 0;

		// Make a timestamp for our most recent modification.
		$wp_modified_timestamp = strtotime( $wp_last_modified );

		if ( ( $client_last_modified && $client_etag )
			? ( ( $client_modified_timestamp >= $wp_modified_timestamp ) && ( $client_etag === $wp_etag ) )
			: ( ( $client_modified_timestamp >= $wp_modified_timestamp ) || ( $client_etag === $wp_etag ) )
		) {
			$status        = 304;
			$exit_required = true;
		}
	}

	if ( is_singular() ) {
		$post = isset( $wp_query->post ) ? $wp_query->post : null;

		// Only set X-Pingback for single posts that allow pings.
		if ( $post && pings_open( $post ) ) {
			$headers['X-Pingback'] = get_bloginfo( 'pingback_url', 'display' );
		}
	}

	/**
	 * Filters the HTTP headers before they're sent to the browser.
	 *
	 * @since 2.8.0
	 *
	 * @param string[] $headers Associative array of headers to be sent.
	 * @param WP       $wp      Current WordPress environment instance.
	 */
	$headers = apply_filters( 'wp_headers', $headers, $this );

	if ( ! empty( $status ) ) {
		status_header( $status );
	}

	// If Last-Modified is set to false, it should not be sent (no-cache situation).
	if ( isset( $headers['Last-Modified'] ) && false === $headers['Last-Modified'] ) {
		unset( $headers['Last-Modified'] );

		if ( ! headers_sent() ) {
			header_remove( 'Last-Modified' );
		}
	}

	if ( ! headers_sent() ) {
		foreach ( (array) $headers as $name => $field_value ) {
			header( "{$name}: {$field_value}" );
		}
	}

	if ( $exit_required ) {
		exit;
	}

	/**
	 * Fires once the requested HTTP headers for caching, content type, etc. have been sent.
	 *
	 * @since 2.1.0
	 *
	 * @param WP $wp Current WordPress environment instance (passed by reference).
	 */
	do_action_ref_array( 'send_headers', array( &$this ) );
}