Skip to:
Content

BuddyPress.org

Changeset 8544


Ignore:
Timestamp:
06/22/2014 06:30:38 PM (9 years ago)
Author:
boonebgorges
Message:

Break redirect/display hook logic out of bp_core_new_subnav_item()

By moving some of the critical logic into bp_core_maybe_hook_new_subnav_screen_function(),
it becomes possible to write unit tests for it.

See #5720

File:
1 edited

Legend:

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

    r8539 r8544  
    296296        $item_css_id = $slug;
    297297
    298     $bp->bp_options_nav[$parent_slug][$slug] = array(
     298    $subnav_item = array(
    299299        'name'            => $name,
    300300        'link'            => trailingslashit( $link ),
     
    305305        'screen_function' => &$screen_function
    306306    );
     307    $bp->bp_options_nav[$parent_slug][$slug] = $subnav_item;
    307308
    308309    /**
     
    330331    if ( ( bp_current_action() && bp_is_current_action( $slug ) ) || ( bp_is_user() && ! bp_current_action() && ( $screen_function == $bp->bp_nav[$parent_slug]['screen_function'] ) ) ) {
    331332
    332         // Before hooking the screen function, check user access
    333         if ( !empty( $user_has_access ) ) {
    334             // Add our screen hook if screen function is callable
    335             if ( is_callable( $screen_function ) ) {
    336                 add_action( 'bp_screens', $screen_function, 3 );
     333        $hooked = bp_core_maybe_hook_new_subnav_screen_function( $subnav_item );
     334
     335        // If redirect args have been returned, perform the redirect now
     336        if ( ! empty( $hooked['status'] ) && 'failure' === $hooked['status'] && isset( $hooked['redirect_args'] ) ) {
     337            bp_core_no_access( $hooked['redirect_args'] );
     338        }
     339    }
     340}
     341
     342/**
     343 * For a given subnav item, either hook the screen function or generate redirect arguments, as necessary.
     344 *
     345 * @since BuddyPress (2.1.0)
     346 *
     347 * @param array $subnav_item The subnav array added to bp_options_nav in
     348 *        bp_core_new_subnav_item().
     349 * @return array
     350 */
     351function bp_core_maybe_hook_new_subnav_screen_function( $subnav_item ) {
     352    $retval = array(
     353        'status' => '',
     354    );
     355
     356    // User has access, so let's try to hook the display callback
     357    if ( ! empty( $subnav_item['user_has_access'] ) ) {
     358
     359        // Screen function is invalid
     360        if ( ! is_callable( $subnav_item['screen_function'] ) ) {
     361            $retval['status'] = 'failure';
     362
     363        // Success - hook to bp_screens
     364        } else {
     365            add_action( 'bp_screens', $subnav_item['screen_function'], 3 );
     366            $retval['status'] = 'success';
     367        }
     368
     369    // User doesn't have access. Determine redirect arguments based on
     370    // user status
     371    } else {
     372        $retval['status'] = 'failure';
     373
     374        if ( is_user_logged_in() ) {
     375
     376            $bp = buddypress();
     377
     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'] ) ) {
     383                $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'
     393                } else {
     394                    $redirect_to = trailingslashit( bp_displayed_user_domain() . ( 'xprofile' == $bp->profile->id ? 'profile' : $bp->profile->id ) );
     395                }
     396
     397                $message     = '';
    337398            }
     399
     400            $retval['redirect_args'] = array(
     401                'message'  => $message,
     402                'root'     => $redirect_to,
     403                'redirect' => false,
     404            );
     405
    338406        } else {
    339 
    340             // When the content is off-limits, we handle the situation
    341             // differently depending on whether the current user is logged in
    342             if ( is_user_logged_in() ) {
    343                 if ( !bp_is_my_profile() && empty( $bp->bp_nav[$bp->default_component]['show_for_displayed_user'] ) ) {
    344 
    345                     // This covers the edge case where the default component is
    346                     // a non-public tab, like 'messages'
    347                     if ( bp_is_active( 'activity' ) && isset( $bp->pages->activity ) ) {
    348                         $redirect_to = trailingslashit( bp_displayed_user_domain() . bp_get_activity_slug() );
    349                     } else {
    350                         $redirect_to = trailingslashit( bp_displayed_user_domain() . ( 'xprofile' == $bp->profile->id ? 'profile' : $bp->profile->id ) );
    351                     }
    352 
    353                     $message     = '';
    354                 } else {
    355                     $message     = __( 'You do not have access to this page.', 'buddypress' );
    356                     $redirect_to = bp_displayed_user_domain();
    357                 }
    358 
    359                 // Off-limits to this user. Throw an error and redirect to the displayed user's domain
    360                 bp_core_no_access( array(
    361                     'message'  => $message,
    362                     'root'     => $redirect_to,
    363                     'redirect' => false
    364                 ) );
    365 
    366             // Not logged in. Allow the user to log in, and attempt to redirect
    367             } else {
    368                 bp_core_no_access();
    369             }
    370         }
    371     }
     407            // When the user is logged out, pass an empty array
     408            // This indicates that the default arguments should be
     409            // used in bp_core_no_access()
     410            $retval['redirect_args'] = array();
     411        }
     412    }
     413
     414    return $retval;
    372415}
    373416
Note: See TracChangeset for help on using the changeset viewer.