<!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][5895] trunk: Reconfigures canonical redirection so that URIs canonicalize up (less specific) rather than down (more specific).</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/5895">5895</a></dd>
<dt>Author</dt> <dd>boonebgorges</dd>
<dt>Date</dt> <dd>2012-03-09 15:25:20 +0000 (Fri, 09 Mar 2012)</dd>
</dl>

<h3>Log Message</h3>
<pre>Reconfigures canonical redirection so that URIs canonicalize up (less specific) rather than down (more specific). Fixes <a href="http://buddypress.trac.wordpress.org/ticket/1741">#1741</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="#trunkbpgroupsbpgroupsloaderphp">trunk/bp-groups/bp-groups-loader.php</a></li>
<li><a href="#trunkbploaderphp">trunk/bp-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 (5894 => 5895)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/bp-core/bp-core-buddybar.php        2012-03-09 14:26:23 UTC (rev 5894)
+++ trunk/bp-core/bp-core-buddybar.php        2012-03-09 15:25:20 UTC (rev 5895)
</span><span class="lines">@@ -64,27 +64,39 @@
</span><span class="cx">                 return;
</span><span class="cx"> 
</span><span class="cx">         // Look for current component
</span><del>-        if ( bp_is_current_component( $slug ) &amp;&amp; !bp_current_action() ) {
-                if ( !is_object( $screen_function[0] ) )
-                        add_action( 'bp_screens', $screen_function );
-                else
-                        add_action( 'bp_screens', array( &amp;$screen_function[0], $screen_function[1] ), 3 );
-
-                if ( !empty( $default_subnav_slug ) ) {
-                        $bp-&gt;current_action               = apply_filters( 'bp_default_component_subnav', $default_subnav_slug, $r );
-                        $bp-&gt;redirect_stack['action'] = $bp-&gt;current_action;
</del><ins>+        if ( bp_is_current_component( $slug ) ) {
+                if ( !empty( $default_subnav_slug ) &amp;&amp; bp_is_current_action( $default_subnav_slug ) ) {
+                        // The requested URL has explicitly included the default subnav (eg
+                        // example.com/members/membername/activity/just-me/). The canonical
+                        // version will not contain this subnav slug.
+                        unset( $bp-&gt;canonical_stack['action'] );
+                } else if ( !bp_current_action() ) {
+                        if ( !is_object( $screen_function[0] ) )
+                                add_action( 'bp_screens', $screen_function );
+                        else
+                                add_action( 'bp_screens', array( &amp;$screen_function[0], $screen_function[1] ), 3 );
+        
+                        if ( !empty( $default_subnav_slug ) ) {
+                                $bp-&gt;current_action                = apply_filters( 'bp_default_component_subnav', $default_subnav_slug, $r );
+                        }
</ins><span class="cx">                 }
</span><span class="cx"> 
</span><span class="cx">         // Look for current item
</span><del>-        } elseif ( bp_is_current_item( $slug ) &amp;&amp; !bp_current_action() ) {
-                if ( !is_object( $screen_function[0] ) )
-                        add_action( 'bp_screens', $screen_function );
-                else
-                        add_action( 'bp_screens', array( &amp;$screen_function[0], $screen_function[1] ), 3 );
-
-                if ( !empty( $default_subnav_slug ) ) {
-                        $bp-&gt;current_action               = apply_filters( 'bp_default_component_subnav', $default_subnav_slug, $r );
-                        $bp-&gt;redirect_stack['action'] = $bp-&gt;current_action;
</del><ins>+        } elseif ( bp_is_current_item( $slug ) ) {
+                if ( !empty( $default_subnav_slug ) &amp;&amp; bp_is_current_action( $default_subnav_slug ) ) {
+                        // The requested URL has explicitly included the default subnav (eg
+                        // example.com/members/membername/activity/just-me/). The canonical
+                        // version will not contain this subnav slug.
+                        unset( $bp-&gt;canonical_stack['action'] );
+                } else if ( !bp_current_action() ) {
+                        if ( !is_object( $screen_function[0] ) )
+                                add_action( 'bp_screens', $screen_function );
+                        else
+                                add_action( 'bp_screens', array( &amp;$screen_function[0], $screen_function[1] ), 3 );
+        
+                        if ( !empty( $default_subnav_slug ) ) {
+                                $bp-&gt;current_action                = apply_filters( 'bp_default_component_subnav', $default_subnav_slug, $r );
+                        }
</ins><span class="cx">                 }
</span><span class="cx">         }
</span><span class="cx"> 
</span><span class="lines">@@ -108,7 +120,7 @@
</span><span class="cx"> 
</span><span class="cx">         $r = wp_parse_args( $args, $defaults );
</span><span class="cx">         extract( $r, EXTR_SKIP );
</span><del>-
</del><ins>+        
</ins><span class="cx">         if ( $function = $bp-&gt;bp_nav[$parent_slug]['screen_function'] ) {
</span><span class="cx">                 if ( !is_object( $function[0] ) )
</span><span class="cx">                         remove_action( 'bp_screens', $function, 3 );
</span><span class="lines">@@ -118,24 +130,43 @@
</span><span class="cx"> 
</span><span class="cx">         $bp-&gt;bp_nav[$parent_slug]['screen_function'] = &amp;$screen_function;
</span><span class="cx"> 
</span><del>-        // If the current_action has been set to the default_subnav_slug, it will be reflected
-        // in the redirect_stack. Unset the action manually so that the new nav default can be set
-        if ( !empty( $bp-&gt;redirect_stack['action'] ) &amp;&amp; $bp-&gt;redirect_stack['action'] == $bp-&gt;bp_nav[$parent_slug]['default_subnav_slug'] ) {
-                $bp-&gt;current_action = '';
-        }
-
-        if ( bp_is_current_component( $parent_slug ) &amp;&amp; !bp_current_action() ) {
-                if ( !is_object( $screen_function[0] ) ) {
-                        add_action( 'bp_screens', $screen_function );
</del><ins>+        if ( bp_is_current_component( $parent_slug ) ) {
+                
+                // The only way to tell whether to set the subnav is to peek at the unfiltered_uri
+                // Find the component
+                $component_uri_key = array_search( $parent_slug, $bp-&gt;unfiltered_uri );
+                
+                if ( false !== $component_uri_key ) {
+                        if ( !empty( $bp-&gt;unfiltered_uri[$component_uri_key + 1] ) ) {
+                                $unfiltered_action = $bp-&gt;unfiltered_uri[$component_uri_key + 1];
+                        }
+                }
+                
+                if ( empty( $unfiltered_action ) ) {
+                        // No subnav item has been requested in the URL, so set a new nav default
+                        if ( !bp_is_current_action( $subnav_slug ) ) {
+                                if ( !is_object( $screen_function[0] ) ) {
+                                        add_action( 'bp_screens', $screen_function );
+                                } else {
+                                        add_action( 'bp_screens', array( &amp;$screen_function[0], $screen_function[1] ) );
+                                }
+                
+                                $bp-&gt;current_action = $subnav_slug;
+                                unset( $bp-&gt;canonical_stack['action'] );
+                        }
+                } else if ( $unfiltered_action == $subnav_slug ) {
+                        // The URL is explicitly requesting the new subnav item, but should be
+                        // directed to the canonical URL
+                        unset( $bp-&gt;canonical_stack['action'] );
</ins><span class="cx">                 } else {
</span><del>-                        add_action( 'bp_screens', array( &amp;$screen_function[0], $screen_function[1] ) );
</del><ins>+                        // In all other cases (including the case where the original subnav item
+                        // is explicitly called in the URL), the canonical URL will contain the
+                        // subnav slug
+                        $bp-&gt;canonical_stack['action'] = bp_current_action();
</ins><span class="cx">                 }
</span><del>-
-                if ( $subnav_slug ) {
-                        $bp-&gt;current_action               = $subnav_slug;
-                        $bp-&gt;redirect_stack['action'] = $bp-&gt;current_action;
-                }
</del><span class="cx">         }
</span><ins>+        
+        return;
</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 (5894 => 5895)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/bp-core/bp-core-catchuri.php        2012-03-09 14:26:23 UTC (rev 5894)
+++ trunk/bp-core/bp-core-catchuri.php        2012-03-09 15:25:20 UTC (rev 5895)
</span><span class="lines">@@ -520,12 +520,14 @@
</span><span class="cx">  * Canonicalizes BuddyPress URLs
</span><span class="cx">  *
</span><span class="cx">  * This function ensures that requests for BuddyPress content are always redirected to their
</span><del>- * most specific, trailingslashed versions.
</del><ins>+ * canonical versions. Canonical versions are always trailingslashed, and are typically the most
+ * general possible versions of the URL - eg, example.com/groups/mygroup/ instead of
+ * example.com/groups/mygroup/home/
</ins><span class="cx">  *
</span><span class="cx">  * @since 1.6
</span><del>- * @see BP_Members_Component::setup_globals() where $bp-&gt;redirect_stack['base_url'] and
</del><ins>+ * @see BP_Members_Component::setup_globals() where $bp-&gt;canonical_stack['base_url'] and
</ins><span class="cx">  *   ['component'] may be set
</span><del>- * @see bp_core_new_nav_item() where $bp-&gt;redirect_stack['action'] may be set
</del><ins>+ * @see bp_core_new_nav_item() where $bp-&gt;canonical_stack['action'] may be set
</ins><span class="cx">  */
</span><span class="cx"> function bp_redirect_canonical() {
</span><span class="cx">         global $bp;
</span><span class="lines">@@ -548,28 +550,28 @@
</span><span class="cx">                 $url_stack      = explode( '?', $requested_url );
</span><span class="cx">                 $req_url_clean  = $url_stack[0];
</span><span class="cx"> 
</span><del>-                // Process the redirect stack
-                if ( isset( $bp-&gt;redirect_stack['base_url'] ) ) {
-                        $url_stack[0] = $bp-&gt;redirect_stack['base_url'];
</del><ins>+                // Build the canonical URL out of the redirect stack
+                if ( isset( $bp-&gt;canonical_stack['base_url'] ) ) {
+                        $url_stack[0] = $bp-&gt;canonical_stack['base_url'];
</ins><span class="cx">                 }
</span><span class="cx"> 
</span><del>-                if ( isset( $bp-&gt;redirect_stack['component'] ) ) {
-                        $url_stack[0] = trailingslashit( $url_stack[0] . $bp-&gt;redirect_stack['component'] );
</del><ins>+                if ( isset( $bp-&gt;canonical_stack['component'] ) ) {
+                        $url_stack[0] = trailingslashit( $url_stack[0] . $bp-&gt;canonical_stack['component'] );
</ins><span class="cx">                 }
</span><span class="cx"> 
</span><del>-                if ( isset( $bp-&gt;redirect_stack['action'] ) ) {
-                        $url_stack[0] = trailingslashit( $url_stack[0] . $bp-&gt;redirect_stack['action'] );
</del><ins>+                if ( isset( $bp-&gt;canonical_stack['action'] ) ) {
+                        $url_stack[0] = trailingslashit( $url_stack[0] . $bp-&gt;canonical_stack['action'] );
</ins><span class="cx">                 }
</span><span class="cx"> 
</span><del>-                if ( !empty( $bp-&gt;redirect_stack['action_variables'] ) ) {
-                        foreach( (array) $bp-&gt;redirect_stack['action_variables'] as $av ) {
</del><ins>+                if ( !empty( $bp-&gt;canonical_stack['action_variables'] ) ) {
+                        foreach( (array) $bp-&gt;canonical_stack['action_variables'] as $av ) {
</ins><span class="cx">                                 $url_stack[0] = trailingslashit( $url_stack[0] . $av );
</span><span class="cx">                         }
</span><span class="cx">                 }
</span><span class="cx"> 
</span><span class="cx">                 // Add trailing slash
</span><span class="cx">                 $url_stack[0] = trailingslashit( $url_stack[0] );
</span><del>-
</del><ins>+                
</ins><span class="cx">                 // Only redirect if we've assembled a URL different from the request
</span><span class="cx">                 if ( $url_stack[0] !== $req_url_clean ) {
</span><span class="cx"> 
</span></span></pre></div>
<a id="trunkbpgroupsbpgroupsloaderphp"></a>
<div class="modfile"><h4>Modified: trunk/bp-groups/bp-groups-loader.php (5894 => 5895)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/bp-groups/bp-groups-loader.php        2012-03-09 14:26:23 UTC (rev 5894)
+++ trunk/bp-groups/bp-groups-loader.php        2012-03-09 15:25:20 UTC (rev 5895)
</span><span class="lines">@@ -163,12 +163,26 @@
</span><span class="cx">                         return;
</span><span class="cx">                 }
</span><span class="cx">                 
</span><del>-                if ( bp_is_groups_component() &amp;&amp; !empty( $this-&gt;current_group ) &amp;&amp; !bp_current_action() ) {
-                        $bp-&gt;current_action                  = apply_filters( 'bp_groups_default_extension', defined( 'BP_GROUPS_DEFAULT_EXTENSION' ) ? BP_GROUPS_DEFAULT_EXTENSION : 'home' );
</del><ins>+                if ( bp_is_groups_component() &amp;&amp; !empty( $this-&gt;current_group ) ) {
+                                                
+                        // Prepare for a redirect to the canonical URL
+                        $bp-&gt;canonical_stack['base_url'] = bp_get_group_permalink( $this-&gt;current_group );
</ins><span class="cx">                         
</span><del>-                        // Prepare for a redirect to the canonical URL
-                        $bp-&gt;redirect_stack['base_url']  = bp_get_group_permalink( $this-&gt;current_group );
-                        $bp-&gt;redirect_stack['action']          = bp_current_action();
</del><ins>+                        if ( bp_current_action() ) {
+                                $bp-&gt;canonical_stack['action'] = bp_current_action();
+                        }
+                        
+                        if ( !empty( $bp-&gt;action_variables ) ) {
+                                $bp-&gt;canonical_stack['action_variables'] = bp_action_variables();
+                        }
+                        
+                        $groups_default_extension = apply_filters( 'bp_groups_default_extension', defined( 'BP_GROUPS_DEFAULT_EXTENSION' ) ? BP_GROUPS_DEFAULT_EXTENSION : 'home' );
+                        
+                        if ( !bp_current_action() ) {
+                                $bp-&gt;current_action = $groups_default_extension;
+                        } else if ( bp_is_current_action( $groups_default_extension ) &amp;&amp; !empty( $bp-&gt;action_variables ) )  {
+                                unset( $bp-&gt;canonical_stack['action'] );
+                        }
</ins><span class="cx">                 }
</span><span class="cx"> 
</span><span class="cx">                 // Group access control
</span></span></pre></div>
<a id="trunkbploaderphp"></a>
<div class="modfile"><h4>Modified: trunk/bp-loader.php (5894 => 5895)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/bp-loader.php        2012-03-09 14:26:23 UTC (rev 5894)
+++ trunk/bp-loader.php        2012-03-09 15:25:20 UTC (rev 5895)
</span><span class="lines">@@ -164,6 +164,13 @@
</span><span class="cx">          * @var bool Are status headers already sent?
</span><span class="cx">          */
</span><span class="cx">         public $no_status_set = false;
</span><ins>+        
+        /**
+         * @var array The canonical URI stack
+         * @see bp_redirect_canonical()
+         * @see bp_core_new_nav_item()
+         */
+        public $canonical_stack = array();
</ins><span class="cx"> 
</span><span class="cx">         /** Components ************************************************************/
</span><span class="cx"> 
</span></span></pre></div>
<a id="trunkbpmembersbpmembersloaderphp"></a>
<div class="modfile"><h4>Modified: trunk/bp-members/bp-members-loader.php (5894 => 5895)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/bp-members/bp-members-loader.php        2012-03-09 14:26:23 UTC (rev 5894)
+++ trunk/bp-members/bp-members-loader.php        2012-03-09 15:25:20 UTC (rev 5895)
</span><span class="lines">@@ -112,13 +112,28 @@
</span><span class="cx">                 } else {
</span><span class="cx">                         $bp-&gt;default_component = BP_DEFAULT_COMPONENT;
</span><span class="cx">                 }
</span><ins>+                
+                if ( bp_displayed_user_id() ) {
+                        $bp-&gt;canonical_stack['base_url'] = bp_displayed_user_domain();
+                
+                        if ( bp_current_component() ) {
+                                $bp-&gt;canonical_stack['component'] = bp_current_component();
+                        }
+                        
+                        if ( bp_current_action() ) {
+                                $bp-&gt;canonical_stack['action'] = bp_current_action();
+                        }
+                        
+                        if ( !empty( $bp-&gt;action_variables ) ) {
+                                $bp-&gt;canonical_stack['action_variables'] = bp_action_variables();
+                        }
</ins><span class="cx"> 
</span><del>-                if ( !bp_current_component() &amp;&amp; bp_displayed_user_id() ) {
-                        $bp-&gt;current_component = $bp-&gt;default_component;
-                        
-                        // Prepare for a redirect to the canonical URL
-                        $bp-&gt;redirect_stack['base_url']  = bp_displayed_user_domain();
-                        $bp-&gt;redirect_stack['component'] = $bp-&gt;default_component;
</del><ins>+                        if ( !bp_current_component() ) {
+                                $bp-&gt;current_component = $bp-&gt;default_component;
+                        } else if ( bp_is_current_component( $bp-&gt;default_component ) &amp;&amp; !bp_current_action() ) {                        
+                                // The canonical URL will not contain the default component
+                                unset( $bp-&gt;canonical_stack['component'] );
+                        }
</ins><span class="cx">                 }
</span><span class="cx">         }
</span><span class="cx"> 
</span></span></pre>
</div>
</div>

</body>
</html>