BP_Core_Nav
BuddyPress Nav.
Description
This class is used to build each component’s navigation.
Source
File: bp-core/classes/class-bp-core-nav.php
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 | class BP_Core_Nav { /** * An associative array containing the nav items for the object ID. * * @since BuddyPress 2.6.0 * @var array */ protected $nav ; /** * The current object ID. * * @since BuddyPress 2.6.0 * @var int */ private $object_id ; /** * Initializes the Nav belonging to the specified object. * * @since BuddyPress 2.6.0 * * @param int $object_id The item ID to build the nav for. Default is the displayed user ID. */ public function __construct( $object_id = 0 ) { if ( empty ( $object_id ) ) { $this ->object_id = (int) bp_displayed_user_id(); } else { $this ->object_id = (int) $object_id ; } $this ->nav[ $this ->object_id ] = array (); } /** * Checks whether a nav item is set. * * @since BuddyPress 2.6.0 * * @param string $key The requested nav slug. * @return bool True if the nav item is set, false otherwise. */ public function __isset( $key ) { return isset( $this ->nav[ $this ->object_id ][ $key ] ); } /** * Gets a nav item. * * @since BuddyPress 2.6.0 * * @param string $key The requested nav slug. * @return mixed The value corresponding to the requested nav item. */ public function __get( $key ) { if ( ! isset( $this ->nav[ $this ->object_id ][ $key ] ) ) { $this ->nav[ $this ->object_id ][ $key ] = null; } return $this ->nav[ $this ->object_id ][ $key ]; } /** * Sets a nav item. * * @since BuddyPress 2.6.0 * * @param string $key The requested nav slug. * @param mixed $value The value of the nav item. */ public function __set( $key , $value ) { if ( is_array ( $value ) ) { $value [ 'primary' ] = true; } $this ->nav[ $this ->object_id ][ $key ] = new BP_Core_Nav_Item( $value ); } /** * Gets a specific nav item or array of nav items. * * @since BuddyPress 2.6.0 * * @param string $key The nav item slug to get. Optional. * @return mixed An array of nav item, a single nav item, or null if none found. */ public function get( $key = '' ) { $return = null; // Return the requested nav item. if ( ! empty ( $key ) ) { if ( ! isset( $this ->nav[ $this ->object_id ][ $key ] ) ) { $return = null; } else { $return = $this ->nav[ $this ->object_id ][ $key ]; } // Return all nav item items. } else { $return = $this ->nav[ $this ->object_id ]; } return $return ; } /** * Adds a new nav item. * * @since BuddyPress 2.6.0 * * @param array $args The nav item's arguments. * @return BP_Core_Nav_Item */ public function add_nav( $args ) { if ( empty ( $args [ 'slug' ] ) ) { return false; } // We have a child and the parent exists. if ( ! empty ( $args [ 'parent_slug' ] ) ) { $slug = $args [ 'parent_slug' ] . '/' . $args [ 'slug' ]; $args [ 'secondary' ] = true; // This is a parent. } else { $slug = $args [ 'slug' ]; $args [ 'primary' ] = true; } // Add to the nav. $this ->nav[ $this ->object_id ][ $slug ] = new BP_Core_Nav_Item( $args ); return $this ->nav[ $this ->object_id ][ $slug ]; } /** * Edits a nav item. * * @since BuddyPress 2.6.0 * * @param array $args The nav item's arguments. * @param string $slug The slug of the nav item. * @param string $parent_slug The slug of the parent nav item (required to edit a child). * @return BP_Core_Nav_Item */ public function edit_nav( $args = array (), $slug = '' , $parent_slug = '' ) { if ( empty ( $slug ) ) { return false; } // We're editing a parent! if ( empty ( $parent_slug ) ) { $nav_items = $this ->get_primary( array ( 'slug' => $slug ), false ); if ( ! $nav_items ) { return false; } $nav_item = reset( $nav_items ); $this ->nav[ $this ->object_id ][ $slug ] = new BP_Core_Nav_Item( wp_parse_args( $args , ( array ) $nav_item ) ); // Return the edited object. return $this ->nav[ $this ->object_id ][ $slug ]; // We're editing a child. } else { $sub_items = $this ->get_secondary( array ( 'parent_slug' => $parent_slug , 'slug' => $slug ), false ); if ( ! $sub_items ) { return false; } $sub_item = reset( $sub_items ); $params = wp_parse_args( $args , ( array ) $sub_item ); // When we have parents, it's for life, we can't change them! if ( empty ( $params [ 'parent_slug' ] ) || $parent_slug !== $params [ 'parent_slug' ] ) { return false; } $this ->nav[ $this ->object_id ][ $parent_slug . '/' . $slug ] = new BP_Core_Nav_Item( $params ); // Return the edited object. return $this ->nav[ $this ->object_id ][ $parent_slug . '/' . $slug ]; } } /** * Unset an item or a subitem of the nav. * * @since BuddyPress 2.6.0 * * @param string $slug The slug of the main item. * @param string $parent_slug The slug of the sub item. * @return false|callable|array False on failure, the screen function(s) on success. */ public function delete_nav( $slug = '' , $parent_slug = '' ) { // Bail if slug is empty if ( empty ( $slug ) ) { return false; } // We're deleting a child if ( ! empty ( $parent_slug ) ) { // Validate the subnav $sub_items = $this ->get_secondary( array ( 'parent_slug' => $parent_slug , 'slug' => $slug ), false ); if ( ! $sub_items ) { return false; } $sub_item = reset( $sub_items ); if ( empty ( $sub_item ->slug ) ) { return false; } // Delete the child unset( $this ->nav[ $this ->object_id ][ $parent_slug . '/' . $slug ] ); // Return the deleted item's screen function return array ( $sub_item ->screen_function ); // We're deleting a parent } else { // Validate the nav $nav_items = $this ->get_primary( array ( 'slug' => $slug ), false ); if ( ! $nav_items ) { return false; } $nav_item = reset( $nav_items ); if ( empty ( $nav_item ->slug ) ) { return false; } $screen_functions = array ( $nav_item ->screen_function ); // Life's unfair, children won't survive the parent :( $sub_items = $this ->get_secondary( array ( 'parent_slug' => $nav_item ->slug ), false ); if ( ! empty ( $sub_items ) ) { foreach ( $sub_items as $sub_item ) { $screen_functions [] = $sub_item ->screen_function; // Delete the child unset( $this ->nav[ $this ->object_id ][ $nav_item ->slug . '/' . $sub_item ->slug ] ); } } // Delete the parent. unset( $this ->nav[ $this ->object_id ][ $nav_item ->slug ] ); // Return the deleted item's screen functions. return $screen_functions ; } } /** * Sorts a list of nav items. * * @since BuddyPress 2.6.0 * * @param array $items The nav items to sort. * @return array */ public function sort_nav( $items ) { $sorted = array (); foreach ( $items as $item ) { // Default position $position = 99; if ( isset( $item ->position ) ) { $position = (int) $item ->position; } // If position is already taken, move to the first next available if ( isset( $sorted [ $position ] ) ) { $sorted_keys = array_keys ( $sorted ); do { $position += 1; } while ( in_array( $position , $sorted_keys ) ); } $sorted [ $position ] = $item ; } ksort( $sorted ); return $sorted ; } /** * Gets the primary nav items. * * @since BuddyPress 2.6.0 * * @param array $args Filters to select the specific primary items. See wp_list_filter(). * @param bool $sort True to sort the nav items. False otherwise. * @return array The list of primary objects nav */ public function get_primary( $args = array (), $sort = true ) { $params = wp_parse_args( $args , array ( 'primary' => true ) ); // This parameter is not overridable. if ( empty ( $params [ 'primary' ] ) ) { return false; } $primary_nav = wp_list_filter( $this ->nav[ $this ->object_id ], $params ); if ( ! $primary_nav ) { return false; } if ( true !== $sort ) { return $primary_nav ; } return $this ->sort_nav( $primary_nav ); } /** * Gets the secondary nav items. * * @since BuddyPress 2.6.0 * * @param array $args Filters to select the specific secondary items. See wp_list_filter(). * @param bool $sort True to sort the nav items. False otherwise. * @return bool|array The list of secondary objects nav, or false if none set. */ public function get_secondary( $args = array (), $sort = true ) { $params = wp_parse_args( $args , array ( 'parent_slug' => '' ) ); // No need to search children if the parent is not set. if ( empty ( $params [ 'parent_slug' ] ) && empty ( $params [ 'secondary' ] ) ) { return false; } $secondary_nav = wp_list_filter( $this ->nav[ $this ->object_id ], $params ); if ( ! $secondary_nav ) { return false; } if ( true !== $sort ) { return $secondary_nav ; } return $this ->sort_nav( $secondary_nav ); } /** * Gets a nested list of visible nav items. * * @since BuddyPress 2.6.0 * * @return array The list of visible nav items. */ public function get_item_nav() { $primary_nav_items = $this ->get_primary( array ( 'show_for_displayed_user' => true ) ); if ( $primary_nav_items ) { foreach ( $primary_nav_items as $key_nav => $primary_nav ) { // Try to get the children. $children = $this ->get_secondary( array ( 'parent_slug' => $primary_nav ->slug, 'user_has_access' => true ) ); if ( $children ) { $primary_nav_items [ $key_nav ] = clone $primary_nav ; $primary_nav_items [ $key_nav ]->children = $children ; } } } return $primary_nav_items ; } } |
Changelog
Version | Description |
---|---|
BuddyPress 2.6.0 | Introduced. |
Methods
- __construct — Initializes the Nav belonging to the specified object.
- __get — Gets a nav item.
- __isset — Checks whether a nav item is set.
- __set — Sets a nav item.
- add_nav — Adds a new nav item.
- delete_nav — Unset an item or a subitem of the nav.
- edit_nav — Edits a nav item.
- get — Gets a specific nav item or array of nav items.
- get_item_nav — Gets a nested list of visible nav items.
- get_primary — Gets the primary nav items.
- get_secondary — Gets the secondary nav items.
- sort_nav — Sorts a list of nav items.
Questions?
We're always happy to help with code or other questions you might have! Search our developer docs, contact support, or connect with our sales team.