Skip to:
Content

BuddyPress.org

Changeset 5412


Ignore:
Timestamp:
11/29/2011 08:58:28 PM (13 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.

Location:
trunk
Files:
5 edited

Legend:

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

    r5385 r5412  
    7171
    7272        if ( !empty( $default_subnav_slug ) ) {
    73             /**
    74              * BuddyPress will attempt to resolve to the most specific URL possible,
    75              * to avoid search-engine-unfriendly content reduplication. Filter
    76              * bp_guarantee_unique_uris (and return false) to avoid this behavior
    77              */
    78             if ( apply_filters( 'bp_guarantee_unique_uris', true ) ) {
    79                 bp_core_redirect( bp_displayed_user_domain() . $slug . '/' . apply_filters( 'bp_default_component_subnav', $default_subnav_slug, $r ) );
    80             } else {
    81                  $bp->current_action = apply_filters( 'bp_default_component_subnav', $default_subnav_slug, $r );
    82             }
     73            $bp->current_action           = apply_filters( 'bp_default_component_subnav', $default_subnav_slug, $r );
     74            $bp->redirect_stack['action'] = $bp->current_action;
    8375        }
    8476
     
    9183
    9284        if ( !empty( $default_subnav_slug ) ) {
    93             /**
    94              * BuddyPress will attempt to resolve to the most specific URL possible,
    95              * to avoid search-engine-unfriendly content reduplication. Filter
    96              * bp_guarantee_unique_uris (and return false) to avoid this behavior
    97              */
    98             if ( apply_filters( 'bp_guarantee_unique_uris', true ) ) {
    99                 bp_core_redirect( bp_get_root_domain() . '/' . bp_get_root_slug( bp_current_component() ) . '/' . $slug . '/' . apply_filters( 'bp_default_component_subnav', $default_subnav_slug, $r ) );
    100             } else {
    101                 $bp->current_action = apply_filters( 'bp_default_component_subnav', $default_subnav_slug, $r );
    102             }
     85            $bp->current_action           = apply_filters( 'bp_default_component_subnav', $default_subnav_slug, $r );
     86            $bp->redirect_stack['action'] = $bp->current_action;
    10387        }
    10488    }
  • trunk/bp-core/bp-core-catchuri.php

    r5400 r5412  
    368368        $post                        = $wp_query->queried_object;
    369369    }
    370 
     370   
    371371    // Define local variables
    372372    $located_template   = false;
     
    386386        $wp_query->is_404  = false;
    387387
     388        do_action( 'bp_core_pre_load_template', $located_template );
     389
    388390        load_template( apply_filters( 'bp_load_template', $located_template ) );
     391   
     392        do_action( 'bp_core_post_load_template', $located_template );
    389393    }
    390394
  • 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?>
  • trunk/bp-groups/bp-groups-loader.php

    r5393 r5412  
    165165       
    166166        if ( bp_is_groups_component() && !empty( $this->current_group ) && !bp_current_action() ) {
    167             $bp->current_action = apply_filters( 'bp_groups_default_extension', defined( 'BP_GROUPS_DEFAULT_EXTENSION' ) ? BP_GROUPS_DEFAULT_EXTENSION : 'home' );
     167            $bp->current_action              = apply_filters( 'bp_groups_default_extension', defined( 'BP_GROUPS_DEFAULT_EXTENSION' ) ? BP_GROUPS_DEFAULT_EXTENSION : 'home' );
     168           
     169            // Prepare for a redirect to the canonical URL
     170            $bp->redirect_stack['base_url']  = bp_get_group_permalink( $this->current_group );
     171            $bp->redirect_stack['action']    = $bp->current_action;
    168172        }
    169173
  • trunk/bp-members/bp-members-loader.php

    r5330 r5412  
    116116
    117117        if ( !bp_current_component() && bp_displayed_user_id() ) {
    118             /**
    119              * BuddyPress will attempt to resolve to the most specific URL possible,
    120              * to avoid search-engine-unfriendly content reduplication. Filter
    121              * bp_guarantee_unique_uris (and return false) to avoid this behavior
    122              */
    123             if ( apply_filters( 'bp_guarantee_unique_uris', true ) ) {
    124                 bp_core_redirect( bp_displayed_user_domain() . $bp->default_component );
    125             } else {
    126                 $bp->current_component = $bp->default_component;
    127             }
     118            $bp->current_component       = $bp->default_component;
     119           
     120            // Prepare for a redirect to the canonical URL
     121            $bp->redirect_stack['base_url']  = bp_displayed_user_domain();
     122            $bp->redirect_stack['component'] = $bp->default_component;
    128123        }
    129124    }
Note: See TracChangeset for help on using the changeset viewer.