Skip to:
Content

BuddyPress.org


Ignore:
Timestamp:
03/16/2012 12:30:11 AM (14 years ago)
Author:
johnjamesjacoby
Message:

More redundant URL tweaks

  • Do not include sub_nav slug in $link if it's the default for its parent
  • Add missing priorities to bp_screens actions to ensure adding/removing works correctly
  • Avoid double-negative is_object() checks
  • Add phpdoc header to bp-core-buddybar.php
  • Various code clean-up
  • Debug-notice dodging where missing
  • See #1741
File:
1 edited

Legend:

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

    r5895 r5922  
    11<?php
     2
     3/**
     4 * Core BuddyPress Navigational Functions
     5 *
     6 * @package BuddyPress
     7 * @subpackage Core
     8 * @todo Deprecate BuddyBar functions
     9 */
     10
    211// Exit if accessed directly
    312if ( !defined( 'ABSPATH' ) ) exit;
     
    3140
    3241    // If this is for site admins only and the user is not one, don't create the subnav item
    33     if ( $site_admin_only && !bp_current_user_can( 'bp_moderate' ) )
     42    if ( !empty( $site_admin_only ) && !bp_current_user_can( 'bp_moderate' ) )
    3443        return false;
    3544
     
    4857    );
    4958
    50     /***
     59    /**
    5160     * If this nav item is hidden for the displayed user, and
    5261     * the logged in user is not the displayed user
    5362     * looking at their own profile, don't create the nav item.
    5463     */
    55     if ( !$show_for_displayed_user && !bp_user_has_access() )
    56         return false;
    57 
    58     /***
     64    if ( empty( $show_for_displayed_user ) && !bp_user_has_access() )
     65        return false;
     66
     67    /**
    5968     * If the nav item is visible, we are not viewing a user, and this is a root
    6069     * component, don't attach the default subnav function so we can display a
     
    6675    // Look for current component
    6776    if ( bp_is_current_component( $slug ) ) {
     77
     78        // The requested URL has explicitly included the default subnav (eg
     79        // example.com/members/membername/activity/just-me/). The canonical
     80        // version will not contain this subnav slug.
    6881        if ( !empty( $default_subnav_slug ) && bp_is_current_action( $default_subnav_slug ) ) {
    69             // The requested URL has explicitly included the default subnav (eg
    70             // example.com/members/membername/activity/just-me/). The canonical
    71             // version will not contain this subnav slug.
    7282            unset( $bp->canonical_stack['action'] );
    7383        } else if ( !bp_current_action() ) {
    74             if ( !is_object( $screen_function[0] ) )
    75                 add_action( 'bp_screens', $screen_function );
    76             else
     84            if ( is_object( $screen_function[0] ) ) {
    7785                add_action( 'bp_screens', array( &$screen_function[0], $screen_function[1] ), 3 );
     86            } else {
     87                add_action( 'bp_screens', $screen_function, 3 );
     88            }
    7889   
    7990            if ( !empty( $default_subnav_slug ) ) {
    80                 $bp->current_action            = apply_filters( 'bp_default_component_subnav', $default_subnav_slug, $r );
     91                $bp->current_action = apply_filters( 'bp_default_component_subnav', $default_subnav_slug, $r );
    8192            }
    8293        }
     
    8495    // Look for current item
    8596    } elseif ( bp_is_current_item( $slug ) ) {
     97
     98        // The requested URL has explicitly included the default subnav
     99        // (eg: http://example.com/members/membername/activity/just-me/)
     100        // The canonical version will not contain this subnav slug.
    86101        if ( !empty( $default_subnav_slug ) && bp_is_current_action( $default_subnav_slug ) ) {
    87             // The requested URL has explicitly included the default subnav (eg
    88             // example.com/members/membername/activity/just-me/). The canonical
    89             // version will not contain this subnav slug.
    90102            unset( $bp->canonical_stack['action'] );
    91103        } else if ( !bp_current_action() ) {
    92             if ( !is_object( $screen_function[0] ) )
    93                 add_action( 'bp_screens', $screen_function );
    94             else
     104            if ( is_object( $screen_function[0] ) ) {
    95105                add_action( 'bp_screens', array( &$screen_function[0], $screen_function[1] ), 3 );
    96    
     106            } else {
     107                add_action( 'bp_screens', $screen_function, 3 );
     108            }
     109
    97110            if ( !empty( $default_subnav_slug ) ) {
    98                 $bp->current_action            = apply_filters( 'bp_default_component_subnav', $default_subnav_slug, $r );
     111                $bp->current_action = apply_filters( 'bp_default_component_subnav', $default_subnav_slug, $r );
    99112            }
    100113        }
     
    123136   
    124137    if ( $function = $bp->bp_nav[$parent_slug]['screen_function'] ) {
    125         if ( !is_object( $function[0] ) )
     138        if ( is_object( $function[0] ) ) {
     139            remove_action( 'bp_screens', array( &$function[0], $function[1] ), 3 );
     140        } else {
    126141            remove_action( 'bp_screens', $function, 3 );
    127         else
    128             remove_action( 'bp_screens', array( &$function[0], $function[1] ), 3 );
     142        }
    129143    }
    130144
     
    142156            }
    143157        }
    144        
     158
     159        // No subnav item has been requested in the URL, so set a new nav default
    145160        if ( empty( $unfiltered_action ) ) {
    146             // No subnav item has been requested in the URL, so set a new nav default
    147161            if ( !bp_is_current_action( $subnav_slug ) ) {
    148                 if ( !is_object( $screen_function[0] ) ) {
    149                     add_action( 'bp_screens', $screen_function );
     162                if ( is_object( $screen_function[0] ) ) {
     163                    add_action( 'bp_screens', array( &$screen_function[0], $screen_function[1] ), 3 );
    150164                } else {
    151                     add_action( 'bp_screens', array( &$screen_function[0], $screen_function[1] ) );
     165                    add_action( 'bp_screens', $screen_function, 3 );
    152166                }
    153167       
     
    155169                unset( $bp->canonical_stack['action'] );
    156170            }
    157         } else if ( $unfiltered_action == $subnav_slug ) {
    158             // The URL is explicitly requesting the new subnav item, but should be
    159             // directed to the canonical URL
     171
     172        // The URL is explicitly requesting the new subnav item, but should be
     173        // directed to the canonical URL
     174        } elseif ( $unfiltered_action == $subnav_slug ) {
    160175            unset( $bp->canonical_stack['action'] );
     176
     177        // In all other cases (including the case where the original subnav item
     178        // is explicitly called in the URL), the canonical URL will contain the
     179        // subnav slug
    161180        } else {
    162             // In all other cases (including the case where the original subnav item
    163             // is explicitly called in the URL), the canonical URL will contain the
    164             // subnav slug
    165181            $bp->canonical_stack['action'] = bp_current_action();
    166182        }
    167183    }
    168    
     184
    169185    return;
    170186}
     
    184200
    185201    foreach ( (array) $bp->bp_nav as $slug => $nav_item ) {
    186         if ( empty( $temp[$nav_item['position']]) )
     202        if ( empty( $temp[$nav_item['position']]) ) {
    187203            $temp[$nav_item['position']] = $nav_item;
    188         else {
     204        } else {
    189205            // increase numbers here to fit new items in.
    190206            do {
     
    231247        return false;
    232248
    233     if ( empty( $link ) )
     249    // Link was not forced, so create one
     250    if ( empty( $link ) ) {
    234251        $link = $parent_url . $slug;
    235252
     253        // If this sub item is the default for its parent, skip the slug
     254        if ( $slug == $bp->bp_nav[$parent_slug]['default_subnav_slug'] ) {
     255            $link = $parent_url;
     256        }
     257    }
     258
    236259    // If this is for site admins only and the user is not one, don't create the subnav item
    237     if ( $site_admin_only && !bp_current_user_can( 'bp_moderate' ) )
     260    if ( !empty( $site_admin_only ) && !bp_current_user_can( 'bp_moderate' ) )
    238261        return false;
    239262
     
    257280     * following two conditions:
    258281     *   (1) Either:
    259      *   (a) the parent slug matches the current_component, or
    260      *   (b) the parent slug matches the current_item
     282     *       (a) the parent slug matches the current_component, or
     283     *       (b) the parent slug matches the current_item
    261284     *   (2) And either:
    262      *  (a) the current_action matches $slug, or
     285     *      (a) the current_action matches $slug, or
    263286     *       (b) there is no current_action (ie, this is the default subnav for the parent nav)
    264287     *       and this subnav item is the default for the parent item (which we check by
     
    277300        // Before hooking the screen function, check user access
    278301        if ( !empty( $user_has_access ) ) {
    279             if ( !is_object( $screen_function[0] ) ) {
    280                 add_action( 'bp_screens', $screen_function );
     302            if ( is_object( $screen_function[0] ) ) {
     303                add_action( 'bp_screens', array( &$screen_function[0], $screen_function[1] ), 3 );
    281304            } else {
    282                 add_action( 'bp_screens', array( &$screen_function[0], $screen_function[1] ) );
     305                add_action( 'bp_screens', $screen_function, 3 );
    283306            }
    284307        } else {
    285             // When the content is off-limits, we handle the situation differently
    286             // depending on whether the current user is logged in
     308
     309            // When the content is off-limits, we handle the situation
     310            // differently depending on whether the current user is logged in
    287311            if ( is_user_logged_in() ) {
    288                 if ( !bp_is_my_profile() && !$bp->bp_nav[$bp->default_component]['show_for_displayed_user'] ) {
     312                if ( !bp_is_my_profile() && empty( $bp->bp_nav[$bp->default_component]['show_for_displayed_user'] ) ) {
     313
    289314                    // This covers the edge case where the default component is
    290315                    // a non-public tab, like 'messages'
     
    307332                    'redirect' => false
    308333                ) );
     334
     335            // Not logged in. Allow the user to log in, and attempt to redirect
    309336            } else {
    310                 // Not logged in. Allow the user to log in, and attempt to redirect
    311337                bp_core_no_access();
    312338            }
     
    383409
    384410    if ( $function = $bp->bp_nav[$parent_id]['screen_function'] ) {
    385         if ( !is_object( $function[0] ) ) {
    386             remove_action( 'bp_screens', $function );
     411        if ( is_object( $function[0] ) ) {
     412            remove_action( 'bp_screens', array( &$function[0], $function[1] ), 3 );
    387413        } else {
    388             remove_action( 'bp_screens', array( &$function[0], $function[1] ) );
     414            remove_action( 'bp_screens', $function, 3 );
    389415        }
    390416    }
     
    403429    global $bp;
    404430
    405     $screen_function = ( isset( $bp->bp_options_nav[$parent_id][$slug]['screen_function'] ) ) ? $bp->bp_options_nav[$parent_id][$slug]['screen_function'] : false;
    406 
    407     if ( $screen_function ) {
    408         if ( !is_object( $screen_function[0] ) )
    409             remove_action( 'bp_screens', $screen_function );
    410         else
    411             remove_action( 'bp_screens', array( &$screen_function[0], $screen_function[1] ) );
     431    $screen_function = isset( $bp->bp_options_nav[$parent_id][$slug]['screen_function'] ) ? $bp->bp_options_nav[$parent_id][$slug]['screen_function'] : false;
     432
     433    if ( !empty( $screen_function ) ) {
     434        if ( is_object( $screen_function[0] ) ) {
     435            remove_action( 'bp_screens', array( &$screen_function[0], $screen_function[1] ), 3 );
     436        } else {
     437            remove_action( 'bp_screens', $screen_function, 3 );
     438        }
    412439    }
    413440
     
    439466        return false;
    440467
    441     if ( (int)bp_get_option( 'hide-loggedout-adminbar' ) && !is_user_logged_in() )
     468    if ( (int) bp_get_option( 'hide-loggedout-adminbar' ) && !is_user_logged_in() )
    442469        return false;
    443470
Note: See TracChangeset for help on using the changeset viewer.