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