BP_Core_Nav

BuddyPress Nav.

Defined (1)

The class is defined in the following location(s).

/bp-core/classes/class-bp-core-nav.php  
  1. class BP_Core_Nav { 
  2. /** 
  3. * An associative array containing the nav items for the object ID. 
  4. * @since 2.6.0 
  5. * @var array 
  6. */ 
  7. protected $nav; 
  8.  
  9. /** 
  10. * The current object ID. 
  11. * @since 2.6.0 
  12. * @var int 
  13. */ 
  14. private $object_id; 
  15.  
  16. /** 
  17. * Initializes the Nav belonging to the specified object. 
  18. * @since 2.6.0 
  19. * @param int $object_id The item ID to build the nav for. Default is the displayed user ID. 
  20. */ 
  21. public function __construct( $object_id = 0 ) { 
  22. if ( empty( $object_id ) ) { 
  23. $this->object_id = (int) bp_displayed_user_id(); 
  24. } else { 
  25. $this->object_id = (int) $object_id; 
  26.  
  27. $this->nav[ $this->object_id ] = array(); 
  28.  
  29. /** 
  30. * Checks whether a nav item is set. 
  31. * @since 2.6.0 
  32. * @param string $key The requested nav slug. 
  33. * @return bool True if the nav item is set, false otherwise. 
  34. */ 
  35. public function __isset( $key ) { 
  36. return isset( $this->nav[ $this->object_id ][ $key ] ); 
  37.  
  38. /** 
  39. * Gets a nav item. 
  40. * @since 2.6.0 
  41. * @param string $key The requested nav slug. 
  42. * @return mixed The value corresponding to the requested nav item. 
  43. */ 
  44. public function __get( $key ) { 
  45. if ( ! isset( $this->nav[ $this->object_id ][ $key ] ) ) { 
  46. $this->nav[ $this->object_id ][ $key ] = null; 
  47.  
  48. return $this->nav[ $this->object_id ][ $key ]; 
  49.  
  50. /** 
  51. * Sets a nav item. 
  52. * @since 2.6.0 
  53. * @param string $key The requested nav slug. 
  54. * @param mixed $value The value of the nav item. 
  55. */ 
  56. public function __set( $key, $value ) { 
  57. if ( is_array( $value ) ) { 
  58. $value['primary'] = true; 
  59.  
  60. $this->nav[ $this->object_id ][ $key ] = new BP_Core_Nav_Item( $value ); 
  61.  
  62. /** 
  63. * Gets a specific nav item or array of nav items. 
  64. * @since 2.6.0 
  65. * @param string $key The nav item slug to get. Optional. 
  66. * @return mixed An array of nav item, a single nav item, or null if none found. 
  67. */ 
  68. public function get( $key = '' ) { 
  69. $return = null; 
  70.  
  71. // Return the requested nav item. 
  72. if ( ! empty( $key ) ) { 
  73. if ( ! isset( $this->nav[ $this->object_id ][ $key ] ) ) { 
  74. $return = null; 
  75. } else { 
  76. $return = $this->nav[ $this->object_id ][ $key ]; 
  77.  
  78. // Return all nav item items. 
  79. } else { 
  80. $return = $this->nav[ $this->object_id ]; 
  81.  
  82. return $return; 
  83.  
  84. /** 
  85. * Adds a new nav item. 
  86. * @since 2.6.0 
  87. * @param array $args The nav item's arguments. 
  88. * @return BP_Core_Nav_Item 
  89. */ 
  90. public function add_nav( $args ) { 
  91. if ( empty( $args['slug'] ) ) { 
  92. return false; 
  93.  
  94. // We have a child and the parent exists. 
  95. if ( ! empty( $args['parent_slug'] ) ) { 
  96. $slug = $args['parent_slug'] . '/' . $args['slug']; 
  97. $args['secondary'] = true; 
  98.  
  99. // This is a parent. 
  100. } else { 
  101. $slug = $args['slug']; 
  102. $args['primary'] = true; 
  103.  
  104. // Add to the nav. 
  105. $this->nav[ $this->object_id ][ $slug ] = new BP_Core_Nav_Item( $args ); 
  106.  
  107. return $this->nav[ $this->object_id ][ $slug ]; 
  108.  
  109. /** 
  110. * Edits a nav item. 
  111. * @since 2.6.0 
  112. * @param array $args The nav item's arguments. 
  113. * @param string $slug The slug of the nav item. 
  114. * @param string $parent_slug The slug of the parent nav item (required to edit a child). 
  115. * @return BP_Core_Nav_Item 
  116. */ 
  117. public function edit_nav( $args = array(), $slug = '', $parent_slug = '' ) { 
  118. if ( empty( $slug ) ) { 
  119. return false; 
  120.  
  121. // We're editing a parent! 
  122. if ( empty( $parent_slug ) ) { 
  123. $nav_items = $this->get_primary( array( 'slug' => $slug ), false ); 
  124.  
  125. if ( ! $nav_items ) { 
  126. return false; 
  127.  
  128. $nav_item = reset( $nav_items ); 
  129. $this->nav[ $this->object_id ][ $slug ] = new BP_Core_Nav_Item( wp_parse_args( $args, (array) $nav_item ) ); 
  130.  
  131. // Return the edited object. 
  132. return $this->nav[ $this->object_id ][ $slug ]; 
  133.  
  134. // We're editing a child. 
  135. } else { 
  136. $sub_items = $this->get_secondary( array( 'parent_slug' => $parent_slug, 'slug' => $slug ), false ); 
  137.  
  138. if ( ! $sub_items ) { 
  139. return false; 
  140.  
  141. $sub_item = reset( $sub_items ); 
  142.  
  143. $params = wp_parse_args( $args, (array) $sub_item ); 
  144.  
  145. // When we have parents, it's for life, we can't change them! 
  146. if ( empty( $params['parent_slug'] ) || $parent_slug !== $params['parent_slug'] ) { 
  147. return false; 
  148.  
  149. $this->nav[ $this->object_id ][ $parent_slug . '/' . $slug ] = new BP_Core_Nav_Item( $params ); 
  150.  
  151. // Return the edited object. 
  152. return $this->nav[ $this->object_id ][ $parent_slug . '/' . $slug ]; 
  153.  
  154. /** 
  155. * Unset an item or a subitem of the nav. 
  156. * @since 2.6.0 
  157. * @param string $slug The slug of the main item. 
  158. * @param string $parent_slug The slug of the sub item. 
  159. * @return bool|callable|array False on failure, the screen function(s) on success. 
  160. */ 
  161. public function delete_nav( $slug = '', $parent_slug = '' ) { 
  162.  
  163. // Bail if slug is empty 
  164. if ( empty( $slug ) ) { 
  165. return false; 
  166.  
  167. // We're deleting a child 
  168. if ( ! empty( $parent_slug ) ) { 
  169.  
  170. // Validate the subnav 
  171. $sub_items = $this->get_secondary( array( 'parent_slug' => $parent_slug, 'slug' => $slug ), false ); 
  172.  
  173. if ( ! $sub_items ) { 
  174. return false; 
  175.  
  176. $sub_item = reset( $sub_items ); 
  177.  
  178. if ( empty( $sub_item->slug ) ) { 
  179. return false; 
  180.  
  181. // Delete the child 
  182. unset( $this->nav[ $this->object_id ][ $parent_slug . '/' . $slug ] ); 
  183.  
  184. // Return the deleted item's screen function 
  185. return array( $sub_item->screen_function ); 
  186.  
  187. // We're deleting a parent 
  188. } else { 
  189. // Validate the nav 
  190. $nav_items = $this->get_primary( array( 'slug' => $slug ), false ); 
  191.  
  192. if ( ! $nav_items ) { 
  193. return false; 
  194.  
  195. $nav_item = reset( $nav_items ); 
  196.  
  197. if ( empty( $nav_item->slug ) ) { 
  198. return false; 
  199.  
  200. $screen_functions = array( $nav_item->screen_function ); 
  201.  
  202. // Life's unfair, children won't survive the parent :( 
  203. $sub_items = $this->get_secondary( array( 'parent_slug' => $nav_item->slug ), false ); 
  204.  
  205. if ( ! empty( $sub_items ) ) { 
  206. foreach ( $sub_items as $sub_item ) { 
  207. $screen_functions[] = $sub_item->screen_function; 
  208.  
  209. // Delete the child 
  210. unset( $this->nav[ $this->object_id ][ $nav_item->slug . '/' . $sub_item->slug ] ); 
  211.  
  212. // Delete the parent. 
  213. unset( $this->nav[ $this->object_id ][ $nav_item->slug ] ); 
  214.  
  215. // Return the deleted item's screen functions. 
  216. return $screen_functions; 
  217.  
  218. /** 
  219. * Sorts a list of nav items. 
  220. * @since 2.6.0 
  221. * @param array $items The nav items to sort. 
  222. * @return array 
  223. */ 
  224. public function sort_nav( $items ) { 
  225. $sorted = array(); 
  226.  
  227. foreach ( $items as $item ) { 
  228. // Default position 
  229. $position = 99; 
  230.  
  231. if ( isset( $item->position ) ) { 
  232. $position = (int) $item->position; 
  233.  
  234. // If position is already taken, move to the first next available 
  235. if ( isset( $sorted[ $position ] ) ) { 
  236. $sorted_keys = array_keys( $sorted ); 
  237.  
  238. do { 
  239. $position += 1; 
  240. } while ( in_array( $position, $sorted_keys ) ); 
  241.  
  242. $sorted[ $position ] = $item; 
  243.  
  244. ksort( $sorted ); 
  245. return $sorted; 
  246.  
  247. /** 
  248. * Gets the primary nav items. 
  249. * @since 2.6.0 
  250. * @param array $args Filters to select the specific primary items. See wp_list_filter(). 
  251. * @param bool $sort True to sort the nav items. False otherwise. 
  252. * @return array The list of primary objects nav 
  253. */ 
  254. public function get_primary( $args = array(), $sort = true ) { 
  255. $params = wp_parse_args( $args, array( 'primary' => true ) ); 
  256.  
  257. // This parameter is not overridable. 
  258. if ( empty( $params['primary'] ) ) { 
  259. return false; 
  260.  
  261. $primary_nav = wp_list_filter( $this->nav[ $this->object_id ], $params ); 
  262.  
  263. if ( ! $primary_nav ) { 
  264. return false; 
  265.  
  266. if ( true !== $sort ) { 
  267. return $primary_nav; 
  268.  
  269. return $this->sort_nav( $primary_nav ); 
  270.  
  271. /** 
  272. * Gets the secondary nav items. 
  273. * @since 2.6.0 
  274. * @param array $args Filters to select the specific secondary items. See wp_list_filter(). 
  275. * @param bool $sort True to sort the nav items. False otherwise. 
  276. * @return array The list of secondary objects nav 
  277. */ 
  278. public function get_secondary( $args = array(), $sort = true ) { 
  279. $params = wp_parse_args( $args, array( 'parent_slug' => '' ) ); 
  280.  
  281. // No need to search children if the parent is not set. 
  282. if ( empty( $params['parent_slug'] ) && empty( $params['secondary'] ) ) { 
  283. return false; 
  284.  
  285. $secondary_nav = wp_list_filter( $this->nav[ $this->object_id ], $params ); 
  286.  
  287. if ( ! $secondary_nav ) { 
  288. return false; 
  289.  
  290. if ( true !== $sort ) { 
  291. return $secondary_nav; 
  292.  
  293. return $this->sort_nav( $secondary_nav ); 
  294.  
  295. /** 
  296. * Gets a nested list of visible nav items. 
  297. * @since 2.6.0 
  298. * @return array The list of visible nav items. 
  299. */ 
  300. public function get_item_nav() { 
  301. $primary_nav_items = $this->get_primary( array( 'show_for_displayed_user' => true ) ); 
  302.  
  303. if ( $primary_nav_items ) { 
  304. foreach( $primary_nav_items as $key_nav => $primary_nav ) { 
  305. // Try to get the children. 
  306. $children = $this->get_secondary( array( 'parent_slug' => $primary_nav->slug, 'user_has_access' => true ) ); 
  307.  
  308. if ( $children ) { 
  309. $primary_nav_items[ $key_nav ] = clone $primary_nav; 
  310. $primary_nav_items[ $key_nav ]->children = $children; 
  311.  
  312. return $primary_nav_items;