<!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][8556] trunk: Introduce 'no_access_url' param to bp_core_new_subnav_item()</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 { text-indent: -1em; padding-left: 1em; }#logmsg ol { text-indent: -1.5em; padding-left: 1.5em; }
#logmsg > ul, #logmsg > ol { 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/8556">8556</a></dd>
<dt>Author</dt> <dd>boonebgorges</dd>
<dt>Date</dt> <dd>2014-07-03 12:04:40 +0000 (Thu, 03 Jul 2014)</dd>
</dl>

<h3>Log Message</h3>
<pre>Introduce 'no_access_url' param to bp_core_new_subnav_item()

bp_core_new_subnav_item() allows access to be controlled using the
'show_for_displayed_user' parameter. When this param is false for a given user,
the function attempts to determine a proper redirect URL. But this URL
determining logic assumes that we are looking at a user profile, while BP uses
bp_core_new_subnav_item() for group navigation as well. Rather than attempting
to bake more URL determination logic into bp_core_new_subnav_item() itself, we
introduce a 'no_access_url' parameter that allows calling functions to specify
dynamically the URL that users should be directed to when they do not have
permission to access a requested subnav item.

Fixes <a href="http://buddypress.trac.wordpress.org/ticket/5720">#5720</a></pre>

<h3>Modified Paths</h3>
<ul>
<li><a href="#trunksrcbpcorebpcorebuddybarphp">trunk/src/bp-core/bp-core-buddybar.php</a></li>
<li><a href="#trunktestsphpunittestcasescorenavphp">trunk/tests/phpunit/testcases/core/nav.php</a></li>
</ul>

</div>
<div id="patch">
<h3>Diff</h3>
<a id="trunksrcbpcorebpcorebuddybarphp"></a>
<div class="modfile"><h4>Modified: trunk/src/bp-core/bp-core-buddybar.php (8555 => 8556)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/src/bp-core/bp-core-buddybar.php   2014-06-30 17:20:03 UTC (rev 8555)
+++ trunk/src/bp-core/bp-core-buddybar.php      2014-07-03 12:04:40 UTC (rev 8556)
</span><span class="lines">@@ -266,6 +266,7 @@
</span><span class="cx">          'parent_url'      => false, // URL of the parent item
</span><span class="cx">          'item_css_id'     => false, // The CSS ID to apply to the HTML of the nav item
</span><span class="cx">          'user_has_access' => true,  // Can the logged in user see this nav item?
</span><ins>+               'no_access_url'   => '',
</ins><span class="cx">           'site_admin_only' => false, // Can only site admins see this nav item?
</span><span class="cx">          'position'        => 90,    // Index of where this nav item should be positioned
</span><span class="cx">          'screen_function' => false, // The name of the function to run when clicked
</span><span class="lines">@@ -302,6 +303,7 @@
</span><span class="cx">          'css_id'          => $item_css_id,
</span><span class="cx">          'position'        => $position,
</span><span class="cx">          'user_has_access' => $user_has_access,
</span><ins>+               'no_access_url'   => $no_access_url,
</ins><span class="cx">           'screen_function' => &$screen_function
</span><span class="cx">  );
</span><span class="cx">  $bp->bp_options_nav[$parent_slug][$slug] = $subnav_item;
</span><span class="lines">@@ -375,26 +377,42 @@
</span><span class="cx"> 
</span><span class="cx">                  $bp = buddypress();
</span><span class="cx"> 
</span><del>-                       // Redirect to the displayed user's default component
-                       // If the displayed user is different from the logged-
-                       // in user, we first check to ensure that the user has
-                       // access to the default component
-                       if ( bp_is_my_profile() || ! empty( $bp->bp_nav[ $bp->default_component ]['show_for_displayed_user'] ) ) {
</del><ins>+                        // If a redirect URL has been passed to the subnav
+                       // item, respect it
+                       if ( ! empty( $subnav_item['no_access_url'] ) ) {
</ins><span class="cx">                           $message     = __( 'You do not have access to this page.', 'buddypress' );
</span><del>-                               $redirect_to = bp_displayed_user_domain();
</del><ins>+                                $redirect_to = trailingslashit( $subnav_item['no_access_url'] );
</ins><span class="cx"> 
</span><del>-                       // In some cases, the default tab is not accessible to
-                       // the logged-in user. So we look for a fallback.
-                       } else {
-                               // Try 'activity' first
-                               if ( bp_is_active( 'activity' ) && isset( $bp->pages->activity ) ) {
-                                       $redirect_to = trailingslashit( bp_displayed_user_domain() . bp_get_activity_slug() );
-                               // Then try 'profile'
</del><ins>+                        // In the case of a user page, we try to assume a
+                       // redirect URL
+                       } else if ( bp_is_user() ) {
+
+                               // Redirect to the displayed user's default
+                               // component, as long as that component is
+                               // publicly accessible.
+                               if ( bp_is_my_profile() || ! empty( $bp->bp_nav[ $bp->default_component ]['show_for_displayed_user'] ) ) {
+                                       $message     = __( 'You do not have access to this page.', 'buddypress' );
+                                       $redirect_to = bp_displayed_user_domain();
+
+                               // In some cases, the default tab is not accessible to
+                               // the logged-in user. So we fall back on a tab that we
+                               // know will be accessible.
</ins><span class="cx">                           } else {
</span><del>-                                       $redirect_to = trailingslashit( bp_displayed_user_domain() . ( 'xprofile' == $bp->profile->id ? 'profile' : $bp->profile->id ) );
</del><ins>+                                        // Try 'activity' first
+                                       if ( bp_is_active( 'activity' ) && isset( $bp->pages->activity ) ) {
+                                               $redirect_to = trailingslashit( bp_displayed_user_domain() . bp_get_activity_slug() );
+                                       // Then try 'profile'
+                                       } else {
+                                               $redirect_to = trailingslashit( bp_displayed_user_domain() . ( 'xprofile' == $bp->profile->id ? 'profile' : $bp->profile->id ) );
+                                       }
+
+                                       $message     = '';
</ins><span class="cx">                           }
</span><span class="cx"> 
</span><del>-                               $message     = '';
</del><ins>+                        // Fall back to the home page
+                       } else {
+                               $message     = __( 'You do not have access to this page.', 'buddypress' );
+                               $redirect_to = bp_get_root_domain();
</ins><span class="cx">                   }
</span><span class="cx"> 
</span><span class="cx">                  $retval['redirect_args'] = array(
</span></span></pre></div>
<a id="trunktestsphpunittestcasescorenavphp"></a>
<div class="modfile"><h4>Modified: trunk/tests/phpunit/testcases/core/nav.php (8555 => 8556)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/tests/phpunit/testcases/core/nav.php       2014-06-30 17:20:03 UTC (rev 8555)
+++ trunk/tests/phpunit/testcases/core/nav.php  2014-07-03 12:04:40 UTC (rev 8556)
</span><span class="lines">@@ -284,4 +284,61 @@
</span><span class="cx">          buddypress()->default_component = $old_default_component;
</span><span class="cx">          buddypress()->bp_nav = $old_bp_nav;
</span><span class="cx">  }
</span><ins>+
+       /**
+        * @group bp_core_maybe_hook_new_subnav_screen_function
+        */
+       public function test_maybe_hook_new_subnav_screen_function_user_has_access_false_user_logged_in_group() {
+               $u = $this->create_user();
+               $g = $this->factory->group->create();
+               $old_current_user = get_current_user_id();
+               $this->set_current_user( $u );
+
+               $group = groups_get_group( array(
+                       'group_id' => $g,
+               ) );
+
+               $this->go_to( bp_get_group_permalink( $group ) );
+
+               $subnav_item = array(
+                       'user_has_access' => false,
+                       'no_access_url' => bp_get_group_permalink( $group ),
+               );
+
+               // Just test relevant info
+               $found = bp_core_maybe_hook_new_subnav_screen_function( $subnav_item );
+               $this->assertSame( 'failure', $found['status'] );
+               $this->assertSame( bp_get_group_permalink( $group ), $found['redirect_args']['root'] );
+
+               // Clean up
+               $this->set_current_user( $old_current_user );
+       }
+
+       /**
+        * @group bp_core_maybe_hook_new_subnav_screen_function
+        */
+       public function test_maybe_hook_new_subnav_screen_function_user_has_access_false_user_logged_in_group_no_redirect_url_provided() {
+               $u = $this->create_user();
+               $g = $this->factory->group->create();
+               $old_current_user = get_current_user_id();
+               $this->set_current_user( $u );
+
+               $group = groups_get_group( array(
+                       'group_id' => $g,
+               ) );
+
+               $this->go_to( bp_get_group_permalink( $group ) );
+
+               $subnav_item = array(
+                       'user_has_access' => false,
+               );
+
+               // Just test relevant info
+               $found = bp_core_maybe_hook_new_subnav_screen_function( $subnav_item );
+               $this->assertSame( 'failure', $found['status'] );
+               $this->assertSame( bp_get_root_domain(), $found['redirect_args']['root'] );
+
+               // Clean up
+               $this->set_current_user( $old_current_user );
+       }
</ins><span class="cx"> }
</span></span></pre>
</div>
</div>

</body>
</html>