[ 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( wp_parse_args( $args, (array) $nav_item ) ); 180 181 // Return the edited object. 182 return $this->nav[ $this->object_id ][ $slug ]; 183 184 // We're editing a child. 185 } else { 186 $sub_items = $this->get_secondary( array( 'parent_slug' => $parent_slug, 'slug' => $slug ), false ); 187 188 if ( ! $sub_items ) { 189 return false; 190 } 191 192 $sub_item = reset( $sub_items ); 193 194 $params = wp_parse_args( $args, (array) $sub_item ); 195 196 // When we have parents, it's for life, we can't change them! 197 if ( empty( $params['parent_slug'] ) || $parent_slug !== $params['parent_slug'] ) { 198 return false; 199 } 200 201 $this->nav[ $this->object_id ][ $parent_slug . '/' . $slug ] = new BP_Core_Nav_Item( $params ); 202 203 // Return the edited object. 204 return $this->nav[ $this->object_id ][ $parent_slug . '/' . $slug ]; 205 } 206 } 207 208 /** 209 * Unset an item or a subitem of the nav. 210 * 211 * @since 2.6.0 212 * 213 * @param string $slug The slug of the main item. 214 * @param string $parent_slug The slug of the sub item. 215 * @return false|callable|array False on failure, the screen function(s) on success. 216 */ 217 public function delete_nav( $slug = '', $parent_slug = '' ) { 218 219 // Bail if slug is empty 220 if ( empty( $slug ) ) { 221 return false; 222 } 223 224 // We're deleting a child 225 if ( ! empty( $parent_slug ) ) { 226 227 // Validate the subnav 228 $sub_items = $this->get_secondary( array( 'parent_slug' => $parent_slug, 'slug' => $slug ), false ); 229 230 if ( ! $sub_items ) { 231 return false; 232 } 233 234 $sub_item = reset( $sub_items ); 235 236 if ( empty( $sub_item->slug ) ) { 237 return false; 238 } 239 240 // Delete the child 241 unset( $this->nav[ $this->object_id ][ $parent_slug . '/' . $slug ] ); 242 243 // Return the deleted item's screen function 244 return array( $sub_item->screen_function ); 245 246 // We're deleting a parent 247 } else { 248 // Validate the nav 249 $nav_items = $this->get_primary( array( 'slug' => $slug ), false ); 250 251 if ( ! $nav_items ) { 252 return false; 253 } 254 255 $nav_item = reset( $nav_items ); 256 257 if ( empty( $nav_item->slug ) ) { 258 return false; 259 } 260 261 $screen_functions = array( $nav_item->screen_function ); 262 263 // Life's unfair, children won't survive the parent :( 264 $sub_items = $this->get_secondary( array( 'parent_slug' => $nav_item->slug ), false ); 265 266 if ( ! empty( $sub_items ) ) { 267 foreach ( $sub_items as $sub_item ) { 268 $screen_functions[] = $sub_item->screen_function; 269 270 // Delete the child 271 unset( $this->nav[ $this->object_id ][ $nav_item->slug . '/' . $sub_item->slug ] ); 272 } 273 } 274 275 // Delete the parent. 276 unset( $this->nav[ $this->object_id ][ $nav_item->slug ] ); 277 278 // Return the deleted item's screen functions. 279 return $screen_functions; 280 } 281 } 282 283 /** 284 * Sorts a list of nav items. 285 * 286 * @since 2.6.0 287 * 288 * @param array $items The nav items to sort. 289 * @return array 290 */ 291 public function sort_nav( $items ) { 292 $sorted = array(); 293 294 foreach ( $items as $item ) { 295 // Default position 296 $position = 99; 297 298 if ( isset( $item->position ) ) { 299 $position = (int) $item->position; 300 } 301 302 // If position is already taken, move to the first next available 303 if ( isset( $sorted[ $position ] ) ) { 304 $sorted_keys = array_keys( $sorted ); 305 306 do { 307 $position += 1; 308 } while ( in_array( $position, $sorted_keys ) ); 309 } 310 311 $sorted[ $position ] = $item; 312 } 313 314 ksort( $sorted ); 315 return $sorted; 316 } 317 318 /** 319 * Gets the primary nav items. 320 * 321 * @since 2.6.0 322 * 323 * @param array $args Filters to select the specific primary items. See wp_list_filter(). 324 * @param bool $sort True to sort the nav items. False otherwise. 325 * @return array The list of primary objects nav 326 */ 327 public function get_primary( $args = array(), $sort = true ) { 328 $params = wp_parse_args( $args, array( 'primary' => true ) ); 329 330 // This parameter is not overridable. 331 if ( empty( $params['primary'] ) ) { 332 return false; 333 } 334 335 $primary_nav = wp_list_filter( $this->nav[ $this->object_id ], $params ); 336 337 if ( ! $primary_nav ) { 338 return false; 339 } 340 341 if ( true !== $sort ) { 342 return $primary_nav; 343 } 344 345 return $this->sort_nav( $primary_nav ); 346 } 347 348 /** 349 * Gets the secondary nav items. 350 * 351 * @since 2.6.0 352 * 353 * @param array $args Filters to select the specific secondary items. See wp_list_filter(). 354 * @param bool $sort True to sort the nav items. False otherwise. 355 * @return bool|array The list of secondary objects nav, or false if none set. 356 */ 357 public function get_secondary( $args = array(), $sort = true ) { 358 $params = wp_parse_args( $args, array( 'parent_slug' => '' ) ); 359 360 // No need to search children if the parent is not set. 361 if ( empty( $params['parent_slug'] ) && empty( $params['secondary'] ) ) { 362 return false; 363 } 364 365 $secondary_nav = wp_list_filter( $this->nav[ $this->object_id ], $params ); 366 367 if ( ! $secondary_nav ) { 368 return false; 369 } 370 371 if ( true !== $sort ) { 372 return $secondary_nav; 373 } 374 375 return $this->sort_nav( $secondary_nav ); 376 } 377 378 /** 379 * Gets a nested list of visible nav items. 380 * 381 * @since 2.6.0 382 * 383 * @return array The list of visible nav items. 384 */ 385 public function get_item_nav() { 386 $primary_nav_items = $this->get_primary( array( 'show_for_displayed_user' => true ) ); 387 388 if ( $primary_nav_items ) { 389 foreach( $primary_nav_items as $key_nav => $primary_nav ) { 390 // Try to get the children. 391 $children = $this->get_secondary( array( 'parent_slug' => $primary_nav->slug, 'user_has_access' => true ) ); 392 393 if ( $children ) { 394 $primary_nav_items[ $key_nav ] = clone $primary_nav; 395 $primary_nav_items[ $key_nav ]->children = $children; 396 } 397 } 398 } 399 400 return $primary_nav_items; 401 } 402 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
Generated: Mon Dec 9 01:01:38 2019 | Cross-referenced by PHPXref 0.7.1 |