Skip to:
Content

BuddyPress.org

Ticket #6503: 6503-split_new_nav_item.02.patch

File 6503-split_new_nav_item.02.patch, 8.8 KB (added by dcavins, 9 years ago)

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.

  • 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..87d1b20 100644
    defined( 'ABSPATH' ) || exit; 
    3232 * @return bool|null Returns false on failure.
    3333 */
    3434function bp_core_new_nav_item( $args = '' ) {
     35
     36        $defaults = array(
     37                'name'                    => false, // Display name for the nav item
     38                'slug'                    => false, // URL slug for the nav item
     39                'item_css_id'             => false, // The CSS ID to apply to the HTML of the nav item
     40                'show_for_displayed_user' => true,  // When viewing another user does this nav item show up?
     41                'site_admin_only'         => false, // Can only site admins see this nav item?
     42                'position'                => 99,    // Index of where this nav item should be positioned
     43                'screen_function'         => false, // The name of the function to run when clicked
     44                'default_subnav_slug'     => false  // The slug of the default subnav item to select when clicked
     45        );
     46
     47        $r = wp_parse_args( $args, $defaults );
     48
     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 = '' ) {
    35100        $bp = buddypress();
    36101
    37102        $defaults = array(
    function bp_core_new_nav_item( $args = '' ) { 
    47112
    48113        $r = wp_parse_args( $args, $defaults );
    49114
    50         // If we don't have the required info we need, don't create this subnav item
     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        }
    function bp_core_new_nav_item( $args = '' ) { 
    57122                return false;
    58123        }
    59124
     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() ) {
     131                return false;
     132        }
     133
    60134        if ( empty( $r['item_css_id'] ) ) {
    61135                $r['item_css_id'] = $r['slug'];
    62136        }
    function bp_core_new_nav_item( $args = '' ) { 
    72146                'default_subnav_slug'     => $r['default_subnav_slug']
    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, whether or not a related nav link exists.
     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 create this subnav item
     200        if ( empty( $r['slug'] ) ) {
     201                return false;
     202        }
     203
     204        // If this is for site admins only and the user is not one, don't create the subnav item
     205        if ( ! empty( $r['site_admin_only'] ) && ! bp_current_user_can( 'bp_moderate' ) ) {
     206                return false;
     207        }
     208
    75209        /**
    76210         * If this nav item is hidden for the displayed user, and
    77211         * the logged in user is not the displayed user
    function bp_core_new_nav_item( $args = '' ) { 
    96230                // The requested URL has explicitly included the default subnav
    97231                // (eg: http://example.com/members/membername/activity/just-me/)
    98232                // 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 ) ) {
     233                if ( ! empty( $r['default_subnav_slug'] ) && bp_is_current_action( $r['default_subnav_slug'] ) && ! bp_action_variable( 0 ) ) {
    100234                        unset( $bp->canonical_stack['action'] );
    101235                } elseif ( ! bp_current_action() ) {
    102236
    function bp_core_new_nav_item( $args = '' ) { 
    122256        }
    123257
    124258        /**
    125          * Fires after adding an item to the main BuddyPress navigation array.
     259         * Fires after the screen function for an item in the BuddyPress main
     260         * navigation is registered.
    126261         *
    127          * @since BuddyPress (1.5.0)
     262         * @since BuddyPress (2.4.0)
    128263         *
    129264         * @param array $r        Parsed arguments for the nav item.
    130265         * @param array $args     Originally passed in arguments for the nav item.
    131266         * @param array $defaults Default arguments for a nav item.
    132267         */
    133         do_action( 'bp_core_new_nav_item', $r, $args, $defaults );
     268        do_action( 'bp_core_register_nav_screen_function', $r, $args, $defaults );
    134269}
    135270
    136271/**