Skip to:
Content

BuddyPress.org

Ticket #6503: 6503-refactor-bp_core_new_subnav_item.patch

File 6503-refactor-bp_core_new_subnav_item.patch, 8.5 KB (added by dcavins, 11 years ago)

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

  • src/bp-core/bp-core-buddybar.php

    diff --git a/src/bp-core/bp-core-buddybar.php b/src/bp-core/bp-core-buddybar.php
    index ebcd9e7..18bf79c 100644
    a b add_action( 'admin_head', 'bp_core_sort_nav_items' ); 
    275275 * @return bool|null Returns false on failure.
    276276 */
    277277function bp_core_new_subnav_item( $args = '' ) {
     278
     279        // First, add the subnav item link to the bp_options_nav array.
     280        $created = bp_core_create_subnav_link( $args );
     281
     282        // To mimic the existing behavior, if bp_core_create_subnav_link()
     283        // returns false, we make an early exit and don't attempt to register
     284        // the screen function.
     285        if ( false === $created ) {
     286                return false;
     287        }
     288
     289        // Then, hook the screen function for the added subnav item.
     290        bp_core_register_subnav_screen_function( $args );
     291
     292}
     293
     294/**
     295 * Add a subnav link to the BuddyPress navigation.
     296 *
     297 * @param array $args {
     298 *     Array describing the new subnav item.
     299 *     @type string $name Display name for the subnav item.
     300 *     @type string $slug Unique URL slug for the subnav item.
     301 *     @type string $parent_slug Slug of the top-level nav item under which the
     302 *           new subnav item should be added.
     303 *     @type string $parent_url URL of the parent nav item.
     304 *     @type bool|string $item_css_id Optional. 'id' attribute for the nav
     305 *           item. Default: the value of $slug.
     306 *     @type bool $user_has_access Optional. True if the logged-in user has
     307 *           access to the subnav item, otherwise false. Can be set dynamically
     308 *           when registering the subnav; eg, use bp_is_my_profile() to restrict
     309 *           access to profile owners only. Default: true.
     310 *     @type bool $site_admin_only Optional. Whether the nav item should be
     311 *           visible only to site admins (those with the 'bp_moderate' cap).
     312 *           Default: false.
     313 *     @type int $position Optional. Numerical index specifying where the item
     314 *           should appear in the subnav array. Default: 90.
     315 *     @type callable $screen_function The callback function that will run
     316 *           when the nav item is clicked.
     317 *     @type string $link Optional. The URL that the subnav item should point
     318 *           to. Defaults to a value generated from the $parent_url + $slug.
     319 *     @type bool $show_in_admin_bar Optional. Whether the nav item should be
     320 *           added into the group's "Edit" Admin Bar menu for group admins.
     321 *           Default: false.
     322 * }
     323 * @return bool|null Returns false on failure.
     324 */
     325function bp_core_create_subnav_link( $args = '' ) {
    278326        $bp = buddypress();
    279327
    280328        $r = wp_parse_args( $args, array(
    function bp_core_new_subnav_item( $args = '' ) { 
    328376        );
    329377
    330378        $bp->bp_options_nav[$r['parent_slug']][$r['slug']] = $subnav_item;
     379}
     380
     381/**
     382 * Register a screen function, whether or not a related subnav link exists.
     383 *
     384 * @param array $args {
     385 *     Array describing the new subnav item.
     386 *     @type string $slug Unique URL slug for the subnav item.
     387 *     @type string $parent_slug Slug of the top-level nav item under which the
     388 *           new subnav item should be added.
     389 *     @type string $parent_url URL of the parent nav item.
     390 *     @type bool $user_has_access Optional. True if the logged-in user has
     391 *           access to the subnav item, otherwise false. Can be set dynamically
     392 *           when registering the subnav; eg, use bp_is_my_profile() to restrict
     393 *           access to profile owners only. Default: true.
     394 *     @type bool $site_admin_only Optional. Whether the nav item should be
     395 *           visible only to site admins (those with the 'bp_moderate' cap).
     396 *           Default: false.
     397 *     @type int $position Optional. Numerical index specifying where the item
     398 *           should appear in the subnav array. Default: 90.
     399 *     @type callable $screen_function The callback function that will run
     400 *           when the nav item is clicked.
     401 *     @type string $link Optional. The URL that the subnav item should point
     402 *           to. Defaults to a value generated from the $parent_url + $slug.
     403 *     @type bool $show_in_admin_bar Optional. Whether the nav item should be
     404 *           added into the group's "Edit" Admin Bar menu for group admins.
     405 *           Default: false.
     406 * }
     407 * @return bool|null Returns false on failure.
     408 */
     409function bp_core_register_subnav_screen_function( $args = '' ) {
     410        $r = wp_parse_args( $args, array(
     411                'slug'              => false, // URL slug for the screen
     412                'parent_slug'       => false, // URL slug of the parent screen
     413                'user_has_access'   => true,  // Can the user visit this screen?
     414                'no_access_url'     => '',
     415                'site_admin_only'   => false, // Can only site admins visit this screen?
     416                'screen_function'   => false, // The name of the function to run when clicked
     417        ) );
    331418
    332419        /**
    333          * The last step is to hook the screen function for the added subnav item. But this only
    334          * needs to be done if this subnav item is the current view, and the user has access to the
    335          * subnav item. We figure out whether we're currently viewing this subnav by checking the
    336          * following two conditions:
     420         * Hook the screen function for the added subnav item. But this only needs to
     421         * be done if this subnav item is the current view, and the user has access to the
     422         * subnav item. We figure out whether we're currently viewing this subnav by
     423         * checking the following two conditions:
    337424         *   (1) Either:
    338425         *           (a) the parent slug matches the current_component, or
    339426         *           (b) the parent slug matches the current_item
    function bp_core_new_subnav_item( $args = '' ) { 
    354441        // If we *do* meet condition (2), then the added subnav item is currently being requested
    355442        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'] ) ) ) {
    356443
    357                 $hooked = bp_core_maybe_hook_new_subnav_screen_function( $subnav_item );
     444                // If this is for site admins only and the user is not one, don't create the subnav item
     445                if ( ! empty( $r['site_admin_only'] ) && ! bp_current_user_can( 'bp_moderate' ) ) {
     446                        return false;
     447                }
     448
     449                $hooked = bp_core_maybe_hook_new_subnav_screen_function( $r );
    358450
    359451                // If redirect args have been returned, perform the redirect now
    360452                if ( ! empty( $hooked['status'] ) && 'failure' === $hooked['status'] && isset( $hooked['redirect_args'] ) ) {
    function bp_core_maybe_hook_new_subnav_screen_function( $subnav_item ) { 
    378470                'status' => '',
    379471        );
    380472
     473        // Is this accessible by site admins only?
     474        $site_admin_restricted = false;
     475        if ( ! empty( $subnav_item['site_admin_only'] ) && ! bp_current_user_can( 'bp_moderate' ) ) {
     476                $site_admin_restricted = true;
     477        }
     478
    381479        // User has access, so let's try to hook the display callback
    382         if ( ! empty( $subnav_item['user_has_access'] ) ) {
     480        if ( ! empty( $subnav_item['user_has_access'] ) && ! $site_admin_restricted ) {
    383481
    384482                // Screen function is invalid
    385483                if ( ! is_callable( $subnav_item['screen_function'] ) ) {
  • src/bp-groups/classes/class-bp-group-extension.php

    diff --git a/src/bp-groups/classes/class-bp-group-extension.php b/src/bp-groups/classes/class-bp-group-extension.php
    index 02374dd..8f36af6 100644
    a b class BP_Group_Extension { 
    735735                        return;
    736736                }
    737737
     738                // If the user can see the nav item, we create it.
    738739                $user_can_see_nav_item = $this->user_can_see_nav_item();
    739740
    740741                if ( $user_can_see_nav_item ) {
    741742                        $group_permalink = bp_get_group_permalink( groups_get_current_group() );
    742743
    743                         bp_core_new_subnav_item( array(
     744                        bp_core_create_subnav_link( array(
    744745                                'name'            => ! $this->nav_item_name ? $this->name : $this->nav_item_name,
    745746                                'slug'            => $this->slug,
    746747                                'parent_slug'     => bp_get_current_group_slug(),
    class BP_Group_Extension { 
    751752                                'user_has_access' => $user_can_see_nav_item,
    752753                                'no_access_url'   => $group_permalink,
    753754                        ) );
     755                }
     756
     757                // If the user can visit the screen, we register it.
     758                $user_can_visit = $this->user_can_visit();
     759
     760                if ( $user_can_visit ) {
     761                        $group_permalink = bp_get_group_permalink( groups_get_current_group() );
     762
     763                        bp_core_register_subnav_screen_function( array(
     764                                'slug'            => $this->slug,
     765                                'parent_slug'     => bp_get_current_group_slug(),
     766                                'screen_function' => array( &$this, '_display_hook' ),
     767                                'user_has_access' => $user_can_visit,
     768                                'no_access_url'   => $group_permalink,
     769                        ) );
    754770
    755771                        // When we are viewing the extension display page, set the title and options title
    756772                        if ( bp_is_current_action( $this->slug ) ) {