Skip to:
Content

BuddyPress.org


Ignore:
Timestamp:
11/30/2011 09:43:32 PM (13 years ago)
Author:
boonebgorges
Message:

Moves bp_redirect_canonical to bp-core-catchuri.php with other URI-related functions. Readds error/success messages before redirect so that they're not lost in a cookie black hole. Fixes #3786

File:
1 edited

Legend:

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

    r5412 r5425  
    509509add_action( 'login_form_bpnoaccess', 'bp_core_no_access_wp_login_error' );
    510510
     511/**
     512 * Canonicalizes BuddyPress URLs
     513 *
     514 * This function ensures that requests for BuddyPress content are always redirected to their
     515 * most specific, trailingslashed versions.
     516 *
     517 * @since 1.6
     518 * @see BP_Members_Component::setup_globals() where $bp->redirect_stack['base_url'] and
     519 *   ['component'] may be set
     520 * @see bp_core_new_nav_item() where $bp->redirect_stack['action'] may be set
     521 */
     522function bp_redirect_canonical() {
     523    global $bp;
     524   
     525    if ( !bp_is_blog_page() && apply_filters( 'bp_do_redirect_canonical', true ) ) {
     526        // build the URL in the address bar
     527        $requested_url  = is_ssl() ? 'https://' : 'http://';
     528        $requested_url .= $_SERVER['HTTP_HOST'];
     529        $requested_url .= $_SERVER['REQUEST_URI'];
     530       
     531        // Stash query args
     532        $url_stack      = explode( '?', $requested_url );
     533        $req_url_clean  = $url_stack[0];
     534       
     535        // Process the redirect stack
     536        if ( isset( $bp->redirect_stack['base_url'] ) ) {
     537            $url_stack[0] = $bp->redirect_stack['base_url'];
     538        }
     539               
     540        if ( isset( $bp->redirect_stack['component'] ) ) {
     541            $url_stack[0] = trailingslashit( $url_stack[0] . $bp->redirect_stack['component'] );
     542        }
     543               
     544        if ( isset( $bp->redirect_stack['action'] ) ) {
     545            $url_stack[0] = trailingslashit( $url_stack[0] . $bp->redirect_stack['action'] );
     546        }
     547                   
     548        if ( !empty( $bp->redirect_stack['action_variables'] ) ) {
     549            foreach( (array)$bp->redirect_stack['action_variables'] as $av ) {
     550                $url_stack[0] = trailingslashit( $url_stack[0] . $av );
     551            }
     552        }
     553       
     554        // Add trailing slash
     555        $url_stack[0] = trailingslashit( $url_stack[0] );
     556       
     557        // Only redirect if we've assembled a URL different from the request
     558        if ( $url_stack[0] !== $req_url_clean ) {
     559           
     560            // Template messages have been deleted from the cookie by this point, so
     561            // they must be readded before redirecting
     562            if ( isset( $bp->template_message ) ) {
     563                $message      = stripslashes( $bp->template_message );
     564                $message_type = isset( $bp->template_message_type ) ? $bp->template_message_type : 'success';
     565               
     566                bp_core_add_message( $message, $message_type );
     567            }
     568           
     569            bp_core_redirect( implode( '?', $url_stack ) );
     570        }
     571    }
     572}
     573add_action( 'bp_core_pre_load_template', 'bp_redirect_canonical' );
     574
    511575?>
Note: See TracChangeset for help on using the changeset viewer.