<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head><meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>[BuddyPress][5412] trunk: 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.</title>
</head>
<body>
<style type="text/css"><!--
#msg dl.meta { border: 1px #006 solid; background: #369; padding: 6px; color: #fff; }
#msg dl.meta dt { float: left; width: 6em; font-weight: bold; }
#msg dt:after { content:':';}
#msg dl, #msg dt, #msg ul, #msg li, #header, #footer, #logmsg { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt; }
#msg dl a { font-weight: bold}
#msg dl a:link { color:#fc3; }
#msg dl a:active { color:#ff0; }
#msg dl a:visited { color:#cc6; }
h3 { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt; font-weight: bold; }
#msg pre { overflow: auto; background: #ffc; border: 1px #fa0 solid; padding: 6px; }
#logmsg { background: #ffc; border: 1px #fa0 solid; padding: 1em 1em 0 1em; }
#logmsg p, #logmsg pre, #logmsg blockquote { margin: 0 0 1em 0; }
#logmsg p, #logmsg li, #logmsg dt, #logmsg dd { line-height: 14pt; }
#logmsg h1, #logmsg h2, #logmsg h3, #logmsg h4, #logmsg h5, #logmsg h6 { margin: .5em 0; }
#logmsg h1:first-child, #logmsg h2:first-child, #logmsg h3:first-child, #logmsg h4:first-child, #logmsg h5:first-child, #logmsg h6:first-child { margin-top: 0; }
#logmsg ul, #logmsg ol { padding: 0; list-style-position: inside; margin: 0 0 0 1em; }
#logmsg > ul, #logmsg > ol { margin-left: 0; margin: 0 0 1em 0; }
#logmsg pre { background: #eee; padding: 1em; }
#logmsg blockquote { border: 1px solid #fa0; border-left-width: 10px; padding: 1em 1em 0 1em; background: white;}
#logmsg dl { margin: 0; }
#logmsg dt { font-weight: bold; }
#logmsg dd { margin: 0; padding: 0 0 0.5em 0; }
#logmsg dd:before { content:'\00bb';}
#logmsg table { border-spacing: 0px; border-collapse: collapse; border-top: 4px solid #fa0; border-bottom: 1px solid #fa0; background: #fff; }
#logmsg table th { text-align: left; font-weight: normal; padding: 0.2em 0.5em; border-top: 1px dotted #fa0; }
#logmsg table td { text-align: right; border-top: 1px dotted #fa0; padding: 0.2em 0.5em; }
#logmsg table thead th { text-align: center; border-bottom: 1px solid #fa0; }
#logmsg table th.Corner { text-align: left; }
#logmsg hr { border: none 0; border-top: 2px dashed #fa0; height: 1px; }
#header, #footer { color: #fff; background: #636; border: 1px #300 solid; padding: 6px; }
#patch { width: 100%; }
#patch h4 {font-family: verdana,arial,helvetica,sans-serif;font-size:10pt;padding:8px;background:#369;color:#fff;margin:0;}
#patch .propset h4, #patch .binary h4 {margin:0;}
#patch pre {padding:0;line-height:1.2em;margin:0;}
#patch .diff {width:100%;background:#eee;padding: 0 0 10px 0;overflow:auto;}
#patch .propset .diff, #patch .binary .diff {padding:10px 0;}
#patch span {display:block;padding:0 10px;}
#patch .modfile, #patch .addfile, #patch .delfile, #patch .propset, #patch .binary, #patch .copfile {border:1px solid #ccc;margin:10px 0;}
#patch ins {background:#dfd;text-decoration:none;display:block;padding:0 10px;}
#patch del {background:#fdd;text-decoration:none;display:block;padding:0 10px;}
#patch .lines, .info {color:#888;background:#fff;}
--></style>
<div id="msg">
<dl class="meta">
<dt>Revision</dt> <dd><a href="http://buddypress.trac.wordpress.org/changeset/5412">5412</a></dd>
<dt>Author</dt> <dd>boonebgorges</dd>
<dt>Date</dt> <dd>2011-11-29 20:58:28 +0000 (Tue, 29 Nov 2011)</dd>
</dl>
<h3>Log Message</h3>
<pre>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 <a href="http://buddypress.trac.wordpress.org/ticket/1741">#1741</a>. Combines all canonicalization into a single redirect. Fixes <a href="http://buddypress.trac.wordpress.org/ticket/3771">#3771</a>. Ensures that query strings are preserved on redirects. Fixes <a href="http://buddypress.trac.wordpress.org/ticket/3777">#3777</a>.</pre>
<h3>Modified Paths</h3>
<ul>
<li><a href="#trunkbpcorebpcorebuddybarphp">trunk/bp-core/bp-core-buddybar.php</a></li>
<li><a href="#trunkbpcorebpcorecatchuriphp">trunk/bp-core/bp-core-catchuri.php</a></li>
<li><a href="#trunkbpcorebpcorefunctionsphp">trunk/bp-core/bp-core-functions.php</a></li>
<li><a href="#trunkbpgroupsbpgroupsloaderphp">trunk/bp-groups/bp-groups-loader.php</a></li>
<li><a href="#trunkbpmembersbpmembersloaderphp">trunk/bp-members/bp-members-loader.php</a></li>
</ul>
</div>
<div id="patch">
<h3>Diff</h3>
<a id="trunkbpcorebpcorebuddybarphp"></a>
<div class="modfile"><h4>Modified: trunk/bp-core/bp-core-buddybar.php (5411 => 5412)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/bp-core/bp-core-buddybar.php        2011-11-29 20:53:22 UTC (rev 5411)
+++ trunk/bp-core/bp-core-buddybar.php        2011-11-29 20:58:28 UTC (rev 5412)
</span><span class="lines">@@ -70,16 +70,8 @@
</span><span class="cx">                         add_action( 'bp_screens', array( &$screen_function[0], $screen_function[1] ), 3 );
</span><span class="cx">
</span><span class="cx">                 if ( !empty( $default_subnav_slug ) ) {
</span><del>-                        /**
-                         * BuddyPress will attempt to resolve to the most specific URL possible,
-                         * to avoid search-engine-unfriendly content reduplication. Filter
-                         * bp_guarantee_unique_uris (and return false) to avoid this behavior
-                         */
-                        if ( apply_filters( 'bp_guarantee_unique_uris', true ) ) {
-                                bp_core_redirect( bp_displayed_user_domain() . $slug . '/' . apply_filters( 'bp_default_component_subnav', $default_subnav_slug, $r ) );
-                        } else {
-                                 $bp->current_action = apply_filters( 'bp_default_component_subnav', $default_subnav_slug, $r );
-                        }
</del><ins>+                        $bp->current_action          = apply_filters( 'bp_default_component_subnav', $default_subnav_slug, $r );
+                        $bp->redirect_stack['action'] = $bp->current_action;
</ins><span class="cx">                 }
</span><span class="cx">
</span><span class="cx">         // Look for current item
</span><span class="lines">@@ -90,16 +82,8 @@
</span><span class="cx">                         add_action( 'bp_screens', array( &$screen_function[0], $screen_function[1] ), 3 );
</span><span class="cx">
</span><span class="cx">                 if ( !empty( $default_subnav_slug ) ) {
</span><del>-                        /**
-                         * BuddyPress will attempt to resolve to the most specific URL possible,
-                         * to avoid search-engine-unfriendly content reduplication. Filter
-                         * bp_guarantee_unique_uris (and return false) to avoid this behavior
-                         */
-                        if ( apply_filters( 'bp_guarantee_unique_uris', true ) ) {
-                                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 ) );
-                        } else {
-                                $bp->current_action = apply_filters( 'bp_default_component_subnav', $default_subnav_slug, $r );
-                        }
</del><ins>+                        $bp->current_action          = apply_filters( 'bp_default_component_subnav', $default_subnav_slug, $r );
+                        $bp->redirect_stack['action'] = $bp->current_action;
</ins><span class="cx">                 }
</span><span class="cx">         }
</span><span class="cx">
</span></span></pre></div>
<a id="trunkbpcorebpcorecatchuriphp"></a>
<div class="modfile"><h4>Modified: trunk/bp-core/bp-core-catchuri.php (5411 => 5412)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/bp-core/bp-core-catchuri.php        2011-11-29 20:53:22 UTC (rev 5411)
+++ trunk/bp-core/bp-core-catchuri.php        2011-11-29 20:58:28 UTC (rev 5412)
</span><span class="lines">@@ -367,7 +367,7 @@
</span><span class="cx">                 $wp_query->queried_object_id = $object_id;
</span><span class="cx">                 $post = $wp_query->queried_object;
</span><span class="cx">         }
</span><del>-
</del><ins>+        
</ins><span class="cx">         // Define local variables
</span><span class="cx">         $located_template = false;
</span><span class="cx">         $filtered_templates = array();
</span><span class="lines">@@ -385,7 +385,11 @@
</span><span class="cx">                 $wp_query->is_page = $wp_query->is_singular = true;
</span><span class="cx">                 $wp_query->is_404 = false;
</span><span class="cx">
</span><ins>+                do_action( 'bp_core_pre_load_template', $located_template );
+
</ins><span class="cx">                 load_template( apply_filters( 'bp_load_template', $located_template ) );
</span><ins>+        
+                do_action( 'bp_core_post_load_template', $located_template );
</ins><span class="cx">         }
</span><span class="cx">
</span><span class="cx">         // Kill any other output after this.
</span></span></pre></div>
<a id="trunkbpcorebpcorefunctionsphp"></a>
<div class="modfile"><h4>Modified: trunk/bp-core/bp-core-functions.php (5411 => 5412)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/bp-core/bp-core-functions.php        2011-11-29 20:53:22 UTC (rev 5411)
+++ trunk/bp-core/bp-core-functions.php        2011-11-29 20:58:28 UTC (rev 5412)
</span><span class="lines">@@ -1600,4 +1600,58 @@
</span><span class="cx">         if ( 'remove_canonical_direct' == $redirect )
</span><span class="cx">                 remove_action( 'template_redirect', 'redirect_canonical' );
</span><span class="cx"> }
</span><ins>+
+/**
+ * Canonicalizes BuddyPress URLs
+ *
+ * This function ensures that requests for BuddyPress content are always redirected to their
+ * most specific, trailingslashed versions.
+ *
+ * @since 1.6
+ * @see BP_Members_Component::setup_globals() where $bp->redirect_stack['base_url'] and
+ * ['component'] may be set
+ * @see bp_core_new_nav_item() where $bp->redirect_stack['action'] may be set
+ */
+function bp_redirect_canonical() {
+        global $bp;
+        
+        if ( !bp_is_blog_page() && apply_filters( 'bp_do_redirect_canonical', true ) ) {
+                // build the URL in the address bar
+                $requested_url = is_ssl() ? 'https://' : 'http://';
+                $requested_url .= $_SERVER['HTTP_HOST'];
+                $requested_url .= $_SERVER['REQUEST_URI'];
+                
+                // Stash query args
+                $url_stack = explode( '?', $requested_url );
+                $req_url_clean = $url_stack[0];
+                
+                // Process the redirect stack
+                if ( isset( $bp->redirect_stack['base_url'] ) ) {
+                        $url_stack[0] = $bp->redirect_stack['base_url'];
+                }
+                                
+                if ( isset( $bp->redirect_stack['component'] ) ) {
+                        $url_stack[0] = trailingslashit( $url_stack[0] . $bp->redirect_stack['component'] );
+                }
+                                
+                if ( isset( $bp->redirect_stack['action'] ) ) {
+                        $url_stack[0] = trailingslashit( $url_stack[0] . $bp->redirect_stack['action'] );
+                }
+                                        
+                if ( !empty( $bp->redirect_stack['action_variables'] ) ) {
+                        foreach( (array)$bp->redirect_stack['action_variables'] as $av ) {
+                                $url_stack[0] = trailingslashit( $url_stack[0] . $av );        
+                        }
+                }
+                
+                // Add trailing slash
+                $url_stack[0] = trailingslashit( $url_stack[0] );
+                
+                // Only redirect if we've assembled a URL different from the request
+                if ( $url_stack[0] !== $req_url_clean ) {
+                        bp_core_redirect( implode( '?', $url_stack ) );
+                }
+        }
+}
+add_action( 'bp_core_pre_load_template', 'bp_redirect_canonical' );
</ins><span class="cx"> ?>
</span></span></pre></div>
<a id="trunkbpgroupsbpgroupsloaderphp"></a>
<div class="modfile"><h4>Modified: trunk/bp-groups/bp-groups-loader.php (5411 => 5412)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/bp-groups/bp-groups-loader.php        2011-11-29 20:53:22 UTC (rev 5411)
+++ trunk/bp-groups/bp-groups-loader.php        2011-11-29 20:58:28 UTC (rev 5412)
</span><span class="lines">@@ -164,7 +164,11 @@
</span><span class="cx">                 }
</span><span class="cx">                 
</span><span class="cx">                 if ( bp_is_groups_component() && !empty( $this->current_group ) && !bp_current_action() ) {
</span><del>-                        $bp->current_action = apply_filters( 'bp_groups_default_extension', defined( 'BP_GROUPS_DEFAULT_EXTENSION' ) ? BP_GROUPS_DEFAULT_EXTENSION : 'home' );
</del><ins>+                        $bp->current_action          = apply_filters( 'bp_groups_default_extension', defined( 'BP_GROUPS_DEFAULT_EXTENSION' ) ? BP_GROUPS_DEFAULT_EXTENSION : 'home' );
+                        
+                        // Prepare for a redirect to the canonical URL
+                        $bp->redirect_stack['base_url'] = bp_get_group_permalink( $this->current_group );
+                        $bp->redirect_stack['action']          = $bp->current_action;
</ins><span class="cx">                 }
</span><span class="cx">
</span><span class="cx">                 // Group access control
</span></span></pre></div>
<a id="trunkbpmembersbpmembersloaderphp"></a>
<div class="modfile"><h4>Modified: trunk/bp-members/bp-members-loader.php (5411 => 5412)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/bp-members/bp-members-loader.php        2011-11-29 20:53:22 UTC (rev 5411)
+++ trunk/bp-members/bp-members-loader.php        2011-11-29 20:58:28 UTC (rev 5412)
</span><span class="lines">@@ -115,16 +115,11 @@
</span><span class="cx">                 }
</span><span class="cx">
</span><span class="cx">                 if ( !bp_current_component() && bp_displayed_user_id() ) {
</span><del>-                        /**
-                         * BuddyPress will attempt to resolve to the most specific URL possible,
-                         * to avoid search-engine-unfriendly content reduplication. Filter
-                         * bp_guarantee_unique_uris (and return false) to avoid this behavior
-                         */
-                        if ( apply_filters( 'bp_guarantee_unique_uris', true ) ) {
-                                bp_core_redirect( bp_displayed_user_domain() . $bp->default_component );
-                        } else {
-                                $bp->current_component = $bp->default_component;
-                        }
</del><ins>+                        $bp->current_component                  = $bp->default_component;
+                        
+                        // Prepare for a redirect to the canonical URL
+                        $bp->redirect_stack['base_url'] = bp_displayed_user_domain();
+                        $bp->redirect_stack['component'] = $bp->default_component;
</ins><span class="cx">                 }
</span><span class="cx">         }
</span><span class="cx">
</span></span></pre>
</div>
</div>
</body>
</html>