Skip to:
Content

BuddyPress.org

Changeset 7477


Ignore:
Timestamp:
10/24/2013 08:51:38 PM (13 years ago)
Author:
r-a-y
Message:

Set better HTTP headers for activity RSS feeds.

Previously, BP's RSS feeds would use WP's native HTTP headers set in the
WP::send_headers() method. This would cause BuddyPress to inherit WP
feed's "ETag" and HTTP status headers.

When trying to refresh BP's RSS feeds, this would not work correctly due
to BP relying on WP's post last modified date instead of BP's activity last
updated date. This would lead to a HTTP 304 Not Modified status header
being sent by WP, meaning the client (browser, RSS reader) would not fetch
the latest items from the activity stream.

This commit sets the appropriate HTTP headers for BP's activity RSS feeds
and uses a new, internal class method - http_headers() - to set them.

Fixes #5213.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/bp-activity/bp-activity-classes.php

    r7466 r7477  
    12711271        }
    12721272
    1273         /** OUTPUT ***************************************************************/
    1274 
    1275         /**
    1276          * Output the RSS feed.
     1273        /**
     1274         * Sets various HTTP headers related to Content-Type and browser caching.
     1275         *
     1276         * Most of this class method is derived from {@link WP::send_headers()}.
     1277         *
     1278         * @since BuddyPress (1.9.0)
    12771279         *
    12781280         * @access protected
    12791281         */
    1280         protected function output() {
     1282        protected function http_headers() {
    12811283                // set up some additional headers if not on a directory page
    12821284                // this is done b/c BP uses pseudo-pages
     
    12881290                }
    12891291
    1290                 header( 'Content-Type: text/xml; charset=' . get_option( 'blog_charset' ), true );
     1292                // Set content-type
     1293                @header( 'Content-Type: text/xml; charset=' . get_option( 'blog_charset' ), true );
     1294
     1295                // Cache-related variables
     1296                $last_modified      = mysql2date( 'D, d M Y H:i:s O', bp_activity_get_last_updated(), false );
     1297                $modified_timestamp = strtotime( $last_modified );
     1298                $etag               = md5( $last_modified );
     1299       
     1300                // Set cache-related headers
     1301                @header( 'Last-Modified: ' . $last_modified );
     1302                @header( 'Pragma: no-cache' );
     1303                @header( 'ETag: ' . '"' . $etag . '"' );
     1304       
     1305                // First commit of BuddyPress! (Easter egg)
     1306                @header( 'Expires: Tue, 25 Mar 2008 17:13:55 GMT');
     1307       
     1308                // Get ETag from supported user agents
     1309                if ( isset( $_SERVER['HTTP_IF_NONE_MATCH'] ) ) {
     1310                        $client_etag = wp_unslash( $_SERVER['HTTP_IF_NONE_MATCH'] );
     1311       
     1312                        // Remove quotes from ETag
     1313                        $client_etag = trim( $client_etag, '"' );
     1314       
     1315                        // Strip suffixes from ETag if they exist (eg. "-gzip")
     1316                        if ( $etag_suffix_pos = strpos( $client_etag, '-' ) ) {
     1317                                $client_etag = substr( $client_etag, 0, $etag_suffix_pos );
     1318                        }
     1319
     1320                // No ETag found
     1321                } else {
     1322                        $client_etag = false;
     1323                }
     1324       
     1325                // Get client last modified timestamp from supported user agents
     1326                $client_last_modified      = empty( $_SERVER['HTTP_IF_MODIFIED_SINCE'] ) ? '' : trim( $_SERVER['HTTP_IF_MODIFIED_SINCE'] );
     1327                $client_modified_timestamp = $client_last_modified ? strtotime( $client_last_modified ) : 0;
     1328       
     1329                // Set 304 status if feed hasn't been updated since last fetch
     1330                if ( ( $client_last_modified && $client_etag ) ?
     1331                                 ( ( $client_modified_timestamp >= $modified_timestamp ) && ( $client_etag == $etag ) ) :
     1332                                 ( ( $client_modified_timestamp >= $modified_timestamp ) || ( $client_etag == $etag ) ) ) {
     1333                        $status = 304;
     1334                } else {
     1335                        $status = false;
     1336                }
     1337       
     1338                // If feed hasn't changed as reported by the user agent, set 304 status header
     1339                if ( ! empty( $status ) ) {
     1340                        status_header( $status );
     1341       
     1342                        // cached response, so stop now!
     1343                        if ( $status == 304 ) {
     1344                                exit();
     1345                        }
     1346                }
     1347        }
     1348
     1349        /** OUTPUT ***************************************************************/
     1350
     1351        /**
     1352         * Output the RSS feed.
     1353         *
     1354         * @access protected
     1355         */
     1356        protected function output() {
     1357                $this->http_headers();
    12911358                echo '<?xml version="1.0" encoding="' . get_option( 'blog_charset' ) . '"?'.'>';
    12921359        ?>
Note: See TracChangeset for help on using the changeset viewer.