Skip to:
Content

BuddyPress.org

Ticket #6503: 6503.split-new-nav-item.patch

File 6503.split-new-nav-item.patch, 6.4 KB (added by dcavins, 11 years ago)

Separate the link creation from registering the screen function in bp_core_new_nav_item().

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

    diff --git src/bp-core/bp-core-buddybar.php src/bp-core/bp-core-buddybar.php
    index 5121385..108321e 100644
    defined( 'ABSPATH' ) || exit; 
    3232 * @return bool|null Returns false on failure.
    3333 */
    3434function bp_core_new_nav_item( $args = '' ) {
     35
     36        // First, add the nav item link to the bp_nav array.
     37        $created = bp_core_create_nav_link( $args );
     38
     39        // To mimic the existing behavior, if bp_core_create_nav_link()
     40        // returns false, we make an early exit and don't attempt to register
     41        // the screen function.
     42        if ( false === $created ) {
     43                return false;
     44        }
     45
     46        // Then, hook the screen function for the added nav item.
     47        bp_core_register_nav_screen_function( $args );
     48
     49}
     50
     51/**
     52 * Add a link to the main BuddyPress navigation array.
     53 *
     54 * @param array $args {
     55 *     Array describing the new nav item.
     56 *     @type string      $name                    Display name for the nav item.
     57 *     @type string      $slug                    Unique URL slug for the nav item.
     58 *     @type bool|string $item_css_id             Optional. 'id' attribute for the nav item. Default: the value of `$slug`.
     59 *     @type bool        $show_for_displayed_user Optional. Whether the nav item should be visible when viewing a
     60 *                                                member profile other than your own. Default: true.
     61 *     @type bool        $site_admin_only         Optional. Whether the nav item should be visible only to site admins
     62 *                                                (those with the 'bp_moderate' cap). Default: false.
     63 *     @type int         $position                Optional. Numerical index specifying where the item should appear in
     64 *                                                the nav array. Default: 99.
     65 *     @type callable    $screen_function         The callback function that will run when the nav item is clicked.
     66 *     @type bool|string $default_subnav_slug     Optional. The slug of the default subnav item to select when the nav
     67 *                                                item is clicked.
     68 * }
     69 * @return bool|null Returns false on failure.
     70 */
     71function bp_core_create_nav_link( $args = '' ) {
    3572        $bp = buddypress();
    3673
    37         $defaults = array(
     74        $r = wp_parse_args( $args, array(
    3875                'name'                    => false, // Display name for the nav item
    3976                'slug'                    => false, // URL slug for the nav item
    4077                'item_css_id'             => false, // The CSS ID to apply to the HTML of the nav item
    function bp_core_new_nav_item( $args = '' ) { 
    4380                'position'                => 99,    // Index of where this nav item should be positioned
    4481                'screen_function'         => false, // The name of the function to run when clicked
    4582                'default_subnav_slug'     => false  // The slug of the default subnav item to select when clicked
    46         );
    47 
    48         $r = wp_parse_args( $args, $defaults );
     83        ) );
    4984
    50         // If we don't have the required info we need, don't create this subnav item
     85        // If we don't have the required info we need, don't create this nav item
    5186        if ( empty( $r['name'] ) || empty( $r['slug'] ) ) {
    5287                return false;
    5388        }
    function bp_core_new_nav_item( $args = '' ) { 
    5792                return false;
    5893        }
    5994
     95        /**
     96         * If this nav item is hidden for the displayed user, and
     97         * the logged in user is not the displayed user
     98         * looking at their own profile, don't create the nav item.
     99         */
     100        if ( empty( $r['show_for_displayed_user'] ) && ! bp_user_has_access() ) {
     101                return false;
     102        }
     103
    60104        if ( empty( $r['item_css_id'] ) ) {
    61105                $r['item_css_id'] = $r['slug'];
    62106        }
    function bp_core_new_nav_item( $args = '' ) { 
    72116                'default_subnav_slug'     => $r['default_subnav_slug']
    73117        );
    74118
     119}
     120
     121/**
     122 * Register a screen function, whether or not a related nav link exists.
     123 *
     124 * @param array $args {
     125 *     Array describing the new nav item.
     126 *     @type string      $name                    Display name for the nav item.
     127 *     @type string      $slug                    Unique URL slug for the nav item.
     128 *     @type bool|string $item_css_id             Optional. 'id' attribute for the nav item. Default: the value of `$slug`.
     129 *     @type bool        $show_for_displayed_user Optional. Whether the nav item should be visible when viewing a
     130 *                                                member profile other than your own. Default: true.
     131 *     @type bool        $site_admin_only         Optional. Whether the nav item should be visible only to site admins
     132 *                                                (those with the 'bp_moderate' cap). Default: false.
     133 *     @type int         $position                Optional. Numerical index specifying where the item should appear in
     134 *                                                the nav array. Default: 99.
     135 *     @type callable    $screen_function         The callback function that will run when the nav item is clicked.
     136 *     @type bool|string $default_subnav_slug     Optional. The slug of the default subnav item to select when the nav
     137 *                                                item is clicked.
     138 * }
     139 * @return bool|null Returns false on failure.
     140 */
     141function bp_core_register_nav_screen_function( $args = '' ) {
     142        $bp = buddypress();
     143
     144        $defaults = array(
     145                'name'                    => false, // Display name for the nav item
     146                'slug'                    => false, // URL slug for the nav item
     147                'item_css_id'             => false, // The CSS ID to apply to the HTML of the nav item
     148                'show_for_displayed_user' => true,  // When viewing another user does this nav item show up?
     149                'site_admin_only'         => false, // Can only site admins see this nav item?
     150                'position'                => 99,    // Index of where this nav item should be positioned
     151                'screen_function'         => false, // The name of the function to run when clicked
     152                'default_subnav_slug'     => false  // The slug of the default subnav item to select when clicked
     153        );
     154
     155        $r = wp_parse_args( $args, $defaults );
     156
     157        // If we don't have the required info we need, don't create this subnav item
     158        if ( empty( $r['slug'] ) ) {
     159                return false;
     160        }
     161
     162        // If this is for site admins only and the user is not one, don't create the subnav item
     163        if ( ! empty( $r['site_admin_only'] ) && ! bp_current_user_can( 'bp_moderate' ) ) {
     164                return false;
     165        }
     166
    75167        /**
    76168         * If this nav item is hidden for the displayed user, and
    77169         * the logged in user is not the displayed user