From 271e0f7742c129c52f7662157ab327cedf2cca69 Mon Sep 17 00:00:00 2001 From: Peter Westwood Date: Sat, 21 Feb 2009 21:39:06 +0000 Subject: [PATCH] Allow for the HTTP headers returned by WordPress to be filtered by a plugin. Fixes #9205 props filosofo. git-svn-id: https://develop.svn.wordpress.org/trunk@10619 602fd350-edb4-49c9-b593-d223f7449a82 --- wp-includes/classes.php | 28 ++++++++++++++++++---------- wp-includes/functions.php | 30 +++++++++++++++++++++++++----- 2 files changed, 43 insertions(+), 15 deletions(-) diff --git a/wp-includes/classes.php b/wp-includes/classes.php index d91dd3e4d3..9b5efd7af4 100644 --- a/wp-includes/classes.php +++ b/wp-includes/classes.php @@ -302,16 +302,17 @@ class WP { * @since 2.0.0 */ function send_headers() { - @header('X-Pingback: '. get_bloginfo('pingback_url')); + $headers = array('X-Pingback' => get_bloginfo('pingback_url')); + $status = null; if ( is_user_logged_in() ) - nocache_headers(); + $headers = array_merge($headers, wp_get_nocache_headers()); if ( !empty($this->query_vars['error']) && '404' == $this->query_vars['error'] ) { - status_header( 404 ); + $status = 404; if ( !is_user_logged_in() ) - nocache_headers(); - @header('Content-Type: ' . get_option('html_type') . '; charset=' . get_option('blog_charset')); + $headers = array_merge($headers, wp_get_nocache_headers()); + $headers['Content-Type'] = get_option('html_type') . '; charset=' . get_option('blog_charset'); } else if ( empty($this->query_vars['feed']) ) { - @header('Content-Type: ' . get_option('html_type') . '; charset=' . get_option('blog_charset')); + $headers['Content-Type'] = get_option('html_type') . '; charset=' . get_option('blog_charset'); } else { // We're showing a feed, so WP is indeed the only thing that last changed if ( !empty($this->query_vars['withcomments']) @@ -329,8 +330,8 @@ class WP { else $wp_last_modified = mysql2date('D, d M Y H:i:s', get_lastpostmodified('GMT'), 0).' GMT'; $wp_etag = '"' . md5($wp_last_modified) . '"'; - @header("Last-Modified: $wp_last_modified"); - @header("ETag: $wp_etag"); + $headers['Last-Modified'] = $wp_last_modified; + $headers['ETag'] = $wp_etag; // Support for Conditional GET if (isset($_SERVER['HTTP_IF_NONE_MATCH'])) @@ -347,11 +348,18 @@ class WP { 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_header( 304 ); - exit; + $status = 304; + add_action('send_headers', 'exit', 1); } } + $headers = apply_filters('wp_headers', $headers, $this); + + if ( ! empty( $status ) ) + status_header( $status ); + foreach( (array) $headers as $name => $field_value ) + @header("{$name}: {$field_value}"); + do_action_ref_array('send_headers', array(&$this)); } diff --git a/wp-includes/functions.php b/wp-includes/functions.php index 067d66ebb0..3aed0597f6 100644 --- a/wp-includes/functions.php +++ b/wp-includes/functions.php @@ -1459,6 +1459,27 @@ function status_header( $header ) { return @header( $status_header, true, $header ); } +/** + * Gets the header information to prevent caching. + * + * The several different headers cover the different ways cache prevention is handled + * by different browsers + * + * @since 2.8 + * + * @uses apply_filters() + * @return array The associative array of header names and field values. + */ +function wp_get_nocache_headers() { + $headers = array( + 'Expires' => 'Wed, 11 Jan 1984 05:00:00 GMT', + 'Last-Modified' => gmdate( 'D, d M Y H:i:s' ) . ' GMT', + 'Cache-Control' => 'no-cache, must-revalidate, max-age=0', + 'Pragma' => 'no-cache', + ); + return apply_filters('nocache_headers', $headers); +} + /** * Sets the headers to prevent caching for the different browsers. * @@ -1466,13 +1487,12 @@ function status_header( $header ) { * be sent so that all of them get the point that no caching should occur. * * @since 2.0.0 + * @uses wp_get_nocache_headers() */ function nocache_headers() { - // why are these @-silenced when other header calls aren't? - @header( 'Expires: Wed, 11 Jan 1984 05:00:00 GMT' ); - @header( 'Last-Modified: ' . gmdate( 'D, d M Y H:i:s' ) . ' GMT' ); - @header( 'Cache-Control: no-cache, must-revalidate, max-age=0' ); - @header( 'Pragma: no-cache' ); + $headers = wp_get_nocache_headers(); + foreach( (array) $headers as $name => $field_value ) + @header("{$name}: {$field_value}"); } /**