Skip to:
Content

BuddyPress.org

Changeset 9997


Ignore:
Timestamp:
07/05/2015 05:36:58 PM (9 years ago)
Author:
dcavins
Message:

Refactor bp_core_new_subnav_item().

Use new functions bp_core_create_subnav_link()
and bp_core_register_subnav_screen_function()
within bp_core_new_subnav_item().
Switch group extension usage to take advantage of new granularity.

See #6503.

Location:
trunk/src
Files:
2 edited

Legend:

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

    r9996 r9997  
    266266 */
    267267function bp_core_new_subnav_item( $args = '' ) {
     268
     269    // First, add the subnav item link to the bp_options_nav array.
     270    $created = bp_core_create_subnav_link( $args );
     271
     272    // To mimic the existing behavior, if bp_core_create_subnav_link()
     273    // returns false, we make an early exit and don't attempt to register
     274    // the screen function.
     275    if ( false === $created ) {
     276        return false;
     277    }
     278
     279    // Then, hook the screen function for the added subnav item.
     280    bp_core_register_subnav_screen_function( $args );
     281
     282}
     283
     284/**
     285 * Add a subnav link to the BuddyPress navigation.
     286 *
     287 * @param array $args {
     288 *     Array describing the new subnav item.
     289 *     @type string $name Display name for the subnav item.
     290 *     @type string $slug Unique URL slug for the subnav item.
     291 *     @type string $parent_slug Slug of the top-level nav item under which the
     292 *           new subnav item should be added.
     293 *     @type string $parent_url URL of the parent nav item.
     294 *     @type bool|string $item_css_id Optional. 'id' attribute for the nav
     295 *           item. Default: the value of $slug.
     296 *     @type bool $user_has_access Optional. True if the logged-in user has
     297 *           access to the subnav item, otherwise false. Can be set dynamically
     298 *           when registering the subnav; eg, use bp_is_my_profile() to restrict
     299 *           access to profile owners only. Default: true.
     300 *     @type bool $site_admin_only Optional. Whether the nav item should be
     301 *           visible only to site admins (those with the 'bp_moderate' cap).
     302 *           Default: false.
     303 *     @type int $position Optional. Numerical index specifying where the item
     304 *           should appear in the subnav array. Default: 90.
     305 *     @type callable $screen_function The callback function that will run
     306 *           when the nav item is clicked.
     307 *     @type string $link Optional. The URL that the subnav item should point
     308 *           to. Defaults to a value generated from the $parent_url + $slug.
     309 *     @type bool $show_in_admin_bar Optional. Whether the nav item should be
     310 *           added into the group's "Edit" Admin Bar menu for group admins.
     311 *           Default: false.
     312 * }
     313 * @return bool|null Returns false on failure.
     314 */
     315function bp_core_create_subnav_link( $args = '' ) {
    268316    $bp = buddypress();
    269317
     
    319367
    320368    $bp->bp_options_nav[$r['parent_slug']][$r['slug']] = $subnav_item;
     369}
     370
     371/**
     372 * Register a screen function, whether or not a related subnav link exists.
     373 *
     374 * @param array $args {
     375 *     Array describing the new subnav item.
     376 *     @type string $slug Unique URL slug for the subnav item.
     377 *     @type string $parent_slug Slug of the top-level nav item under which the
     378 *           new subnav item should be added.
     379 *     @type string $parent_url URL of the parent nav item.
     380 *     @type bool $user_has_access Optional. True if the logged-in user has
     381 *           access to the subnav item, otherwise false. Can be set dynamically
     382 *           when registering the subnav; eg, use bp_is_my_profile() to restrict
     383 *           access to profile owners only. Default: true.
     384 *     @type bool $site_admin_only Optional. Whether the nav item should be
     385 *           visible only to site admins (those with the 'bp_moderate' cap).
     386 *           Default: false.
     387 *     @type int $position Optional. Numerical index specifying where the item
     388 *           should appear in the subnav array. Default: 90.
     389 *     @type callable $screen_function The callback function that will run
     390 *           when the nav item is clicked.
     391 *     @type string $link Optional. The URL that the subnav item should point
     392 *           to. Defaults to a value generated from the $parent_url + $slug.
     393 *     @type bool $show_in_admin_bar Optional. Whether the nav item should be
     394 *           added into the group's "Edit" Admin Bar menu for group admins.
     395 *           Default: false.
     396 * }
     397 * @return bool|null Returns false on failure.
     398 */
     399function bp_core_register_subnav_screen_function( $args = '' ) {
     400    $r = wp_parse_args( $args, array(
     401        'slug'              => false, // URL slug for the screen
     402        'parent_slug'       => false, // URL slug of the parent screen
     403        'user_has_access'   => true,  // Can the user visit this screen?
     404        'no_access_url'     => '',
     405        'site_admin_only'   => false, // Can only site admins visit this screen?
     406        'screen_function'   => false, // The name of the function to run when clicked
     407    ) );
    321408
    322409    /**
    323      * The last step is to hook the screen function for the added subnav item. But this only
    324      * needs to be done if this subnav item is the current view, and the user has access to the
    325      * subnav item. We figure out whether we're currently viewing this subnav by checking the
    326      * following two conditions:
     410     * Hook the screen function for the added subnav item. But this only needs to
     411     * be done if this subnav item is the current view, and the user has access to the
     412     * subnav item. We figure out whether we're currently viewing this subnav by
     413     * checking the following two conditions:
    327414     *   (1) Either:
    328415     *       (a) the parent slug matches the current_component, or
     
    345432    if ( ( bp_current_action() && bp_is_current_action( $r['slug'] ) ) || ( bp_is_user() && ! bp_current_action() && ( $r['screen_function'] == $bp->bp_nav[$parent_slug]['screen_function'] ) ) ) {
    346433
    347         $hooked = bp_core_maybe_hook_new_subnav_screen_function( $subnav_item );
     434        // If this is for site admins only and the user is not one, don't create the subnav item
     435        if ( ! empty( $r['site_admin_only'] ) && ! bp_current_user_can( 'bp_moderate' ) ) {
     436            return false;
     437        }
     438
     439        $hooked = bp_core_maybe_hook_new_subnav_screen_function( $r );
    348440
    349441        // If redirect args have been returned, perform the redirect now
     
    367459    );
    368460
     461    // Is this accessible by site admins only?
     462    $site_admin_restricted = false;
     463    if ( ! empty( $subnav_item['site_admin_only'] ) && ! bp_current_user_can( 'bp_moderate' ) ) {
     464        $site_admin_restricted = true;
     465    }
     466
    369467    // User has access, so let's try to hook the display callback
    370     if ( ! empty( $subnav_item['user_has_access'] ) ) {
     468    if ( ! empty( $subnav_item['user_has_access'] ) && ! $site_admin_restricted ) {
    371469
    372470        // Screen function is invalid
  • trunk/src/bp-groups/classes/class-bp-group-extension.php

    r9982 r9997  
    735735        }
    736736
     737        // If the user can see the nav item, we create it.
    737738        $user_can_see_nav_item = $this->user_can_see_nav_item();
    738739
     
    740741            $group_permalink = bp_get_group_permalink( groups_get_current_group() );
    741742
    742             bp_core_new_subnav_item( array(
     743            bp_core_create_subnav_link( array(
    743744                'name'            => ! $this->nav_item_name ? $this->name : $this->nav_item_name,
    744745                'slug'            => $this->slug,
     
    749750                'screen_function' => array( &$this, '_display_hook' ),
    750751                'user_has_access' => $user_can_see_nav_item,
     752                'no_access_url'   => $group_permalink,
     753            ) );
     754        }
     755
     756        // If the user can visit the screen, we register it.
     757        $user_can_visit = $this->user_can_visit();
     758
     759        if ( $user_can_visit ) {
     760            $group_permalink = bp_get_group_permalink( groups_get_current_group() );
     761
     762            bp_core_register_subnav_screen_function( array(
     763                'slug'            => $this->slug,
     764                'parent_slug'     => bp_get_current_group_slug(),
     765                'screen_function' => array( &$this, '_display_hook' ),
     766                'user_has_access' => $user_can_visit,
    751767                'no_access_url'   => $group_permalink,
    752768            ) );
Note: See TracChangeset for help on using the changeset viewer.