Skip to:
Content

BuddyPress.org

Ticket #5720: 5720.patch

File 5720.patch, 5.6 KB (added by boonebgorges, 11 years ago)
  • src/bp-core/bp-core-buddybar.php

    diff --git src/bp-core/bp-core-buddybar.php src/bp-core/bp-core-buddybar.php
    index a854fd3..a307af4 100644
    function bp_core_new_subnav_item( $args = '' ) { 
    266266                'parent_url'      => false, // URL of the parent item
    267267                'item_css_id'     => false, // The CSS ID to apply to the HTML of the nav item
    268268                'user_has_access' => true,  // Can the logged in user see this nav item?
     269                'no_access_url'   => '',
    269270                'site_admin_only' => false, // Can only site admins see this nav item?
    270271                'position'        => 90,    // Index of where this nav item should be positioned
    271272                'screen_function' => false, // The name of the function to run when clicked
    function bp_core_new_subnav_item( $args = '' ) { 
    302303                'css_id'          => $item_css_id,
    303304                'position'        => $position,
    304305                'user_has_access' => $user_has_access,
     306                'no_access_url'   => $no_access_url,
    305307                'screen_function' => &$screen_function
    306308        );
    307309        $bp->bp_options_nav[$parent_slug][$slug] = $subnav_item;
    function bp_core_maybe_hook_new_subnav_screen_function( $subnav_item ) { 
    375377
    376378                        $bp = buddypress();
    377379
    378                         // Redirect to the displayed user's default component
    379                         // If the displayed user is different from the logged-
    380                         // in user, we first check to ensure that the user has
    381                         // access to the default component
    382                         if ( bp_is_my_profile() || ! empty( $bp->bp_nav[ $bp->default_component ]['show_for_displayed_user'] ) ) {
     380                        // If a redirect URL has been passed to the subnav
     381                        // item, respect it
     382                        if ( ! empty( $subnav_item['no_access_url'] ) ) {
    383383                                $message     = __( 'You do not have access to this page.', 'buddypress' );
    384                                 $redirect_to = bp_displayed_user_domain();
    385 
    386                         // In some cases, the default tab is not accessible to
    387                         // the logged-in user. So we look for a fallback.
    388                         } else {
    389                                 // Try 'activity' first
    390                                 if ( bp_is_active( 'activity' ) && isset( $bp->pages->activity ) ) {
    391                                         $redirect_to = trailingslashit( bp_displayed_user_domain() . bp_get_activity_slug() );
    392                                 // Then try 'profile'
     384                                $redirect_to = trailingslashit( $subnav_item['no_access_url'] );
     385
     386                        // In the case of a user page, we try to assume a
     387                        // redirect URL
     388                        } else if ( bp_is_user() ) {
     389
     390                                // Redirect to the displayed user's default
     391                                // component, as long as that component is
     392                                // publicly accessible.
     393                                if ( bp_is_my_profile() || ! empty( $bp->bp_nav[ $bp->default_component ]['show_for_displayed_user'] ) ) {
     394                                        $message     = __( 'You do not have access to this page.', 'buddypress' );
     395                                        $redirect_to = bp_displayed_user_domain();
     396
     397                                // In some cases, the default tab is not accessible to
     398                                // the logged-in user. So we fall back on a tab that we
     399                                // know will be accessible.
    393400                                } else {
    394                                         $redirect_to = trailingslashit( bp_displayed_user_domain() . ( 'xprofile' == $bp->profile->id ? 'profile' : $bp->profile->id ) );
     401                                        // Try 'activity' first
     402                                        if ( bp_is_active( 'activity' ) && isset( $bp->pages->activity ) ) {
     403                                                $redirect_to = trailingslashit( bp_displayed_user_domain() . bp_get_activity_slug() );
     404                                        // Then try 'profile'
     405                                        } else {
     406                                                $redirect_to = trailingslashit( bp_displayed_user_domain() . ( 'xprofile' == $bp->profile->id ? 'profile' : $bp->profile->id ) );
     407                                        }
     408
     409                                        $message     = '';
    395410                                }
    396411
    397                                 $message     = '';
     412                        // Fall back to the home page
     413                        } else {
     414                                $message     = __( 'You do not have access to this page.', 'buddypress' );
     415                                $redirect_to = bp_get_root_domain();
    398416                        }
    399417
    400418                        $retval['redirect_args'] = array(
  • tests/phpunit/testcases/core/nav.php

    diff --git tests/phpunit/testcases/core/nav.php tests/phpunit/testcases/core/nav.php
    index 8da0dcd..9af82ca 100644
    class BP_Tests_Core_Nav extends BP_UnitTestCase { 
    284284                buddypress()->default_component = $old_default_component;
    285285                buddypress()->bp_nav = $old_bp_nav;
    286286        }
     287
     288        /**
     289         * @group bp_core_maybe_hook_new_subnav_screen_function
     290         */
     291        public function test_maybe_hook_new_subnav_screen_function_user_has_access_false_user_logged_in_group() {
     292                $u = $this->create_user();
     293                $g = $this->factory->group->create();
     294                $old_current_user = get_current_user_id();
     295                $this->set_current_user( $u );
     296
     297                $group = groups_get_group( array(
     298                        'group_id' => $g,
     299                ) );
     300
     301                $this->go_to( bp_get_group_permalink( $group ) );
     302
     303                $subnav_item = array(
     304                        'user_has_access' => false,
     305                        'no_access_url' => bp_get_group_permalink( $group ),
     306                );
     307
     308                // Just test relevant info
     309                $found = bp_core_maybe_hook_new_subnav_screen_function( $subnav_item );
     310                $this->assertSame( 'failure', $found['status'] );
     311                $this->assertSame( bp_get_group_permalink( $group ), $found['redirect_args']['root'] );
     312
     313                // Clean up
     314                $this->set_current_user( $old_current_user );
     315        }
     316
     317        /**
     318         * @group bp_core_maybe_hook_new_subnav_screen_function
     319         */
     320        public function test_maybe_hook_new_subnav_screen_function_user_has_access_false_user_logged_in_group_no_redirect_url_provided() {
     321                $u = $this->create_user();
     322                $g = $this->factory->group->create();
     323                $old_current_user = get_current_user_id();
     324                $this->set_current_user( $u );
     325
     326                $group = groups_get_group( array(
     327                        'group_id' => $g,
     328                ) );
     329
     330                $this->go_to( bp_get_group_permalink( $group ) );
     331
     332                $subnav_item = array(
     333                        'user_has_access' => false,
     334                );
     335
     336                // Just test relevant info
     337                $found = bp_core_maybe_hook_new_subnav_screen_function( $subnav_item );
     338                $this->assertSame( 'failure', $found['status'] );
     339                $this->assertSame( bp_get_root_domain(), $found['redirect_args']['root'] );
     340
     341                // Clean up
     342                $this->set_current_user( $old_current_user );
     343        }
    287344}