Skip to:
Content

BuddyPress.org

Changeset 10003


Ignore:
Timestamp:
07/10/2015 02:54:03 AM (9 years ago)
Author:
dcavins
Message:

Refactor bp_core_new_nav_item().

Use new functions bp_core_create_nav_link() and
bp_core_register_nav_screen_function() within
bp_core_new_nav_item(). Also introduces two new
action hooks: bp_core_create_nav_link and
bp_core_register_nav_screen_function.

See #6503.

File:
1 edited

Legend:

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

    r10001 r10003  
    3333 */
    3434function bp_core_new_nav_item( $args = '' ) {
    35     $bp = buddypress();
    3635
    3736    $defaults = array(
     
    4847    $r = wp_parse_args( $args, $defaults );
    4948
    50     // If we don't have the required info we need, don't create this subnav item
     49    // First, add the nav item link to the bp_nav array.
     50    $created = bp_core_create_nav_link( $r );
     51
     52    // To mimic the existing behavior, if bp_core_create_nav_link()
     53    // returns false, we make an early exit and don't attempt to register
     54    // the screen function.
     55    if ( false === $created ) {
     56        return false;
     57    }
     58
     59    // Then, hook the screen function for the added nav item.
     60    bp_core_register_nav_screen_function( $r );
     61
     62    /**
     63     * Fires after adding an item to the main BuddyPress navigation array.
     64     * Note that, when possible, the more specific action hooks
     65     * `bp_core_create_nav_link` or `bp_core_register_nav_screen_function`
     66     * should be used.
     67     *
     68     * @since BuddyPress (1.5.0)
     69     *
     70     * @param array $r        Parsed arguments for the nav item.
     71     * @param array $args     Originally passed in arguments for the nav item.
     72     * @param array $defaults Default arguments for a nav item.
     73     */
     74    do_action( 'bp_core_new_nav_item', $r, $args, $defaults );
     75}
     76
     77/**
     78 * Add a link to the main BuddyPress navigation array.
     79 *
     80 * @since BuddyPress (2.4.0)
     81 *
     82 * @param array $args {
     83 *     Array describing the new nav item.
     84 *     @type string      $name                    Display name for the nav item.
     85 *     @type string      $slug                    Unique URL slug for the nav item.
     86 *     @type bool|string $item_css_id             Optional. 'id' attribute for the nav item. Default: the value of `$slug`.
     87 *     @type bool        $show_for_displayed_user Optional. Whether the nav item should be visible when viewing a
     88 *                                                member profile other than your own. Default: true.
     89 *     @type bool        $site_admin_only         Optional. Whether the nav item should be visible only to site admins
     90 *                                                (those with the 'bp_moderate' cap). Default: false.
     91 *     @type int         $position                Optional. Numerical index specifying where the item should appear in
     92 *                                                the nav array. Default: 99.
     93 *     @type callable    $screen_function         The callback function that will run when the nav item is clicked.
     94 *     @type bool|string $default_subnav_slug     Optional. The slug of the default subnav item to select when the nav
     95 *                                                item is clicked.
     96 * }
     97 * @return bool|null Returns false on failure.
     98 */
     99function bp_core_create_nav_link( $args = '' ) {
     100    $bp = buddypress();
     101
     102    $defaults = array(
     103        'name'                    => false, // Display name for the nav item
     104        'slug'                    => false, // URL slug for the nav item
     105        'item_css_id'             => false, // The CSS ID to apply to the HTML of the nav item
     106        'show_for_displayed_user' => true,  // When viewing another user does this nav item show up?
     107        'site_admin_only'         => false, // Can only site admins see this nav item?
     108        'position'                => 99,    // Index of where this nav item should be positioned
     109        'screen_function'         => false, // The name of the function to run when clicked
     110        'default_subnav_slug'     => false  // The slug of the default subnav item to select when clicked
     111    );
     112
     113    $r = wp_parse_args( $args, $defaults );
     114
     115    // If we don't have the required info we need, don't create this nav item.
    51116    if ( empty( $r['name'] ) || empty( $r['slug'] ) ) {
    52117        return false;
    53118    }
    54119
    55     // If this is for site admins only and the user is not one, don't create the subnav item
     120    // If this is for site admins only and the user is not one, don't create the nav item.
    56121    if ( ! empty( $r['site_admin_only'] ) && ! bp_current_user_can( 'bp_moderate' ) ) {
     122        return false;
     123    }
     124
     125    /**
     126     * If this nav item is hidden for the displayed user, and
     127     * the logged in user is not the displayed user
     128     * looking at their own profile, don't create the nav item.
     129     */
     130    if ( empty( $r['show_for_displayed_user'] ) && ! bp_user_has_access() ) {
    57131        return false;
    58132    }
     
    73147    );
    74148
     149    /**
     150     * Fires after a link is added to the main BuddyPress navigation array.
     151     *
     152     * @since BuddyPress (2.4.0)
     153     *
     154     * @param array $r        Parsed arguments for the nav item.
     155     * @param array $args     Originally passed in arguments for the nav item.
     156     * @param array $defaults Default arguments for a nav item.
     157     */
     158    do_action( 'bp_core_create_nav_link', $r, $args, $defaults );
     159}
     160
     161/**
     162 * Register a screen function for an item in the main nav array.
     163 *
     164 * @since BuddyPress (2.4.0)
     165 *
     166 * @param array $args {
     167 *     Array describing the new nav item.
     168 *     @type string      $name                    Display name for the nav item.
     169 *     @type string      $slug                    Unique URL slug for the nav item.
     170 *     @type bool|string $item_css_id             Optional. 'id' attribute for the nav item. Default: the value of `$slug`.
     171 *     @type bool        $show_for_displayed_user Optional. Whether the nav item should be visible when viewing a
     172 *                                                member profile other than your own. Default: true.
     173 *     @type bool        $site_admin_only         Optional. Whether the nav item should be visible only to site admins
     174 *                                                (those with the 'bp_moderate' cap). Default: false.
     175 *     @type int         $position                Optional. Numerical index specifying where the item should appear in
     176 *                                                the nav array. Default: 99.
     177 *     @type callable    $screen_function         The callback function that will run when the nav item is clicked.
     178 *     @type bool|string $default_subnav_slug     Optional. The slug of the default subnav item to select when the nav
     179 *                                                item is clicked.
     180 * }
     181 * @return bool|null Returns false on failure.
     182 */
     183function bp_core_register_nav_screen_function( $args = '' ) {
     184    $bp = buddypress();
     185
     186    $defaults = array(
     187        'name'                    => false, // Display name for the nav item
     188        'slug'                    => false, // URL slug for the nav item
     189        'item_css_id'             => false, // The CSS ID to apply to the HTML of the nav item
     190        'show_for_displayed_user' => true,  // When viewing another user does this nav item show up?
     191        'site_admin_only'         => false, // Can only site admins see this nav item?
     192        'position'                => 99,    // Index of where this nav item should be positioned
     193        'screen_function'         => false, // The name of the function to run when clicked
     194        'default_subnav_slug'     => false  // The slug of the default subnav item to select when clicked
     195    );
     196
     197    $r = wp_parse_args( $args, $defaults );
     198
     199    // If we don't have the required info we need, don't register this screen function.
     200    if ( empty( $r['slug'] ) ) {
     201        return false;
     202    }
     203
     204    /**
     205    * If this is for site admins only and the user is not one,
     206    * don't register this screen function.
     207    */
     208    if ( ! empty( $r['site_admin_only'] ) && ! bp_current_user_can( 'bp_moderate' ) ) {
     209        return false;
     210    }
     211
    75212    /**
    76213     * If this nav item is hidden for the displayed user, and
    77214     * the logged in user is not the displayed user
    78      * looking at their own profile, don't create the nav item.
     215     * looking at their own profile, don't don't register this screen function.
    79216     */
    80217    if ( empty( $r['show_for_displayed_user'] ) && ! bp_user_has_access() ) {
     
    97234        // (eg: http://example.com/members/membername/activity/just-me/)
    98235        // The canonical version will not contain this subnav slug.
    99         if ( ! empty( $r['default_subnav_slug'] ) && bp_is_current_action( $r['default_subnav_slug'] ) && !bp_action_variable( 0 ) ) {
     236        if ( ! empty( $r['default_subnav_slug'] ) && bp_is_current_action( $r['default_subnav_slug'] ) && ! bp_action_variable( 0 ) ) {
    100237            unset( $bp->canonical_stack['action'] );
    101238        } elseif ( ! bp_current_action() ) {
     
    123260
    124261    /**
    125      * Fires after adding an item to the main BuddyPress navigation array.
     262     * Fires after the screen function for an item in the BuddyPress main
     263     * navigation is registered.
    126264     *
    127      * @since BuddyPress (1.5.0)
     265     * @since BuddyPress (2.4.0)
    128266     *
    129267     * @param array $r        Parsed arguments for the nav item.
     
    131269     * @param array $defaults Default arguments for a nav item.
    132270     */
    133     do_action( 'bp_core_new_nav_item', $r, $args, $defaults );
     271    do_action( 'bp_core_register_nav_screen_function', $r, $args, $defaults );
    134272}
    135273
Note: See TracChangeset for help on using the changeset viewer.