Skip to:
Content

BuddyPress.org


Ignore:
Timestamp:
11/29/2011 08:58:28 PM (12 years ago)
Author:
boonebgorges
Message:

Refactors URL canonicalization logic, moving it into the single bp_redirect_canonical() which is loaded from bp_core_load_template() instead of redirecting during the nav setup routine. See #1741. Combines all canonicalization into a single redirect. Fixes #3771. Ensures that query strings are preserved on redirects. Fixes #3777.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/bp-core/bp-core-functions.php

    r5406 r5412  
    16011601        remove_action( 'template_redirect', 'redirect_canonical' );
    16021602}
     1603
     1604/**
     1605 * Canonicalizes BuddyPress URLs
     1606 *
     1607 * This function ensures that requests for BuddyPress content are always redirected to their
     1608 * most specific, trailingslashed versions.
     1609 *
     1610 * @since 1.6
     1611 * @see BP_Members_Component::setup_globals() where $bp->redirect_stack['base_url'] and
     1612 *   ['component'] may be set
     1613 * @see bp_core_new_nav_item() where $bp->redirect_stack['action'] may be set
     1614 */
     1615function bp_redirect_canonical() {
     1616    global $bp;
     1617   
     1618    if ( !bp_is_blog_page() && apply_filters( 'bp_do_redirect_canonical', true ) ) {
     1619        // build the URL in the address bar
     1620        $requested_url  = is_ssl() ? 'https://' : 'http://';
     1621        $requested_url .= $_SERVER['HTTP_HOST'];
     1622        $requested_url .= $_SERVER['REQUEST_URI'];
     1623       
     1624        // Stash query args
     1625        $url_stack      = explode( '?', $requested_url );
     1626        $req_url_clean  = $url_stack[0];
     1627       
     1628        // Process the redirect stack
     1629        if ( isset( $bp->redirect_stack['base_url'] ) ) {
     1630            $url_stack[0] = $bp->redirect_stack['base_url'];
     1631        }
     1632               
     1633        if ( isset( $bp->redirect_stack['component'] ) ) {
     1634            $url_stack[0] = trailingslashit( $url_stack[0] . $bp->redirect_stack['component'] );
     1635        }
     1636               
     1637        if ( isset( $bp->redirect_stack['action'] ) ) {
     1638            $url_stack[0] = trailingslashit( $url_stack[0] . $bp->redirect_stack['action'] );
     1639        }
     1640                   
     1641        if ( !empty( $bp->redirect_stack['action_variables'] ) ) {
     1642            foreach( (array)$bp->redirect_stack['action_variables'] as $av ) {
     1643                $url_stack[0] = trailingslashit( $url_stack[0] . $av );
     1644            }
     1645        }
     1646       
     1647        // Add trailing slash
     1648        $url_stack[0] = trailingslashit( $url_stack[0] );
     1649       
     1650        // Only redirect if we've assembled a URL different from the request
     1651        if ( $url_stack[0] !== $req_url_clean ) {
     1652            bp_core_redirect( implode( '?', $url_stack ) );
     1653        }
     1654    }
     1655}
     1656add_action( 'bp_core_pre_load_template', 'bp_redirect_canonical' );
    16031657?>
Note: See TracChangeset for help on using the changeset viewer.