| 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 | */ |
| 99 | function bp_core_create_nav_link( $args = '' ) { |
| 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 | */ |
| 183 | function 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 | |