BP_Walker_Nav_Menu

Create HTML list of BP nav items.

Defined (1)

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

/bp-core/classes/class-bp-walker-nav-menu.php  
  1. class BP_Walker_Nav_Menu extends Walker_Nav_Menu { 
  2.  
  3. /** 
  4. * Description of fields indexes for building markup. 
  5. * @since 1.7.0 
  6. * @var array 
  7. */ 
  8. var $db_fields = array( 'id' => 'css_id', 'parent' => 'parent' ); 
  9.  
  10. /** 
  11. * Tree type. 
  12. * @since 1.7.0 
  13. * @var string 
  14. */ 
  15. var $tree_type = array(); 
  16.  
  17. /** 
  18. * Display array of elements hierarchically. 
  19. * This method is almost identical to the version in {@link Walker::walk()}. 
  20. * The only change is on one line which has been commented. An IF was 
  21. * comparing 0 to a non-empty string which was preventing child elements 
  22. * being grouped under their parent menu element. 
  23. * This caused a problem for BuddyPress because our primary/secondary 
  24. * navigations don't have a unique numerical ID that describes a 
  25. * hierarchy (we use a slug). Obviously, WordPress Menus use Posts, and 
  26. * those have ID/post_parent. 
  27. * @since 1.7.0 
  28. * @see Walker::walk() 
  29. * @param array $elements See {@link Walker::walk()}. 
  30. * @param int $max_depth See {@link Walker::walk()}. 
  31. * @return string See {@link Walker::walk()}. 
  32. */ 
  33. public function walk( $elements, $max_depth ) { 
  34. $args = array_slice( func_get_args(), 2 ); 
  35. $output = ''; 
  36.  
  37. if ( $max_depth < -1 ) // Invalid parameter. 
  38. return $output; 
  39.  
  40. if ( empty( $elements ) ) // Nothing to walk. 
  41. return $output; 
  42.  
  43. $parent_field = $this->db_fields['parent']; 
  44.  
  45. // Flat display. 
  46. if ( -1 == $max_depth ) { 
  47.  
  48. $empty_array = array(); 
  49. foreach ( $elements as $e ) 
  50. $this->display_element( $e, $empty_array, 1, 0, $args, $output ); 
  51.  
  52. return $output; 
  53.  
  54. /** 
  55. * Need to display in hierarchical order 
  56. * separate elements into two buckets: top level and children elements 
  57. * children_elements is two dimensional array, eg. 
  58. * children_elements[10][] contains all sub-elements whose parent is 10. 
  59. */ 
  60. $top_level_elements = array(); 
  61. $children_elements = array(); 
  62.  
  63. foreach ( $elements as $e ) { 
  64. // BuddyPress: changed '==' to '==='. This is the only change from version in Walker::walk(). 
  65. if ( 0 === $e->$parent_field ) 
  66. $top_level_elements[] = $e; 
  67. else 
  68. $children_elements[$e->$parent_field][] = $e; 
  69.  
  70. /** 
  71. * When none of the elements is top level 
  72. * assume the first one must be root of the sub elements. 
  73. */ 
  74. if ( empty( $top_level_elements ) ) { 
  75.  
  76. $first = array_slice( $elements, 0, 1 ); 
  77. $root = $first[0]; 
  78. $top_level_elements = array(); 
  79. $children_elements = array(); 
  80.  
  81. foreach ( $elements as $e ) { 
  82. if ( $root->$parent_field == $e->$parent_field ) 
  83. $top_level_elements[] = $e; 
  84. else 
  85. $children_elements[$e->$parent_field][] = $e; 
  86.  
  87. foreach ( $top_level_elements as $e ) 
  88. $this->display_element( $e, $children_elements, $max_depth, 0, $args, $output ); 
  89.  
  90. /** 
  91. * If we are displaying all levels, and remaining children_elements is not empty,  
  92. * then we got orphans, which should be displayed regardless. 
  93. */ 
  94. if ( ( $max_depth == 0 ) && count( $children_elements ) > 0 ) { 
  95. $empty_array = array(); 
  96.  
  97. foreach ( $children_elements as $orphans ) 
  98. foreach ( $orphans as $op ) 
  99. $this->display_element( $op, $empty_array, 1, 0, $args, $output ); 
  100.  
  101. return $output; 
  102.  
  103. /** 
  104. * Display the current <li> that we are on. 
  105. * @see Walker::start_el() for complete description of parameters. 
  106. * @since 1.7.0 
  107. * @param string $output Passed by reference. Used to append 
  108. * additional content. 
  109. * @param object $item Menu item data object. 
  110. * @param int $depth Depth of menu item. Used for padding. Optional,  
  111. * defaults to 0. 
  112. * @param array $args Optional. See {@link Walker::start_el()}. 
  113. * @param int $id Menu item ID. Optional. 
  114. */ 
  115. public function start_el( &$output, $item, $depth = 0, $args = array(), $id = 0 ) { 
  116. // If we're someway down the tree, indent the HTML with the appropriate number of tabs. 
  117. $indent = $depth ? str_repeat( "\t", $depth ) : ''; 
  118.  
  119. /** 
  120. * Filters the classes to be added to the nav menu markup. 
  121. * @since 1.7.0 
  122. * @param array $value Array of classes to be added. 
  123. * @param object $item Menu item data object. 
  124. * @param array $args Array of arguments for the item. 
  125. */ 
  126. $class_names = join( ' ', apply_filters( 'bp_nav_menu_css_class', array_filter( $item->class ), $item, $args ) ); 
  127. $class_names = ! empty( $class_names ) ? ' class="' . esc_attr( $class_names ) . '"' : ''; 
  128.  
  129. // Add HTML ID 
  130. $id = sanitize_html_class( $item->css_id . '-personal-li' ); // Backpat with BP pre-1.7. 
  131.  
  132. /** 
  133. * Filters the value to be used for the nav menu ID attribute. 
  134. * @since 1.7.0 
  135. * @param string $id ID attribute to be added to the menu item. 
  136. * @param object $item Menu item data object. 
  137. * @param array $args Array of arguments for the item. 
  138. */ 
  139. $id = apply_filters( 'bp_nav_menu_item_id', $id, $item, $args ); 
  140. $id = ! empty( $id ) ? ' id="' . esc_attr( $id ) . '"' : ''; 
  141.  
  142. // Opening tag; closing tag is handled in Walker_Nav_Menu::end_el(). 
  143. $output .= $indent . '<li' . $id . $class_names . '>'; 
  144.  
  145. // Add href attribute. 
  146. $attributes = ! empty( $item->link ) ? ' href="' . esc_url( $item->link ) . '"' : ''; 
  147.  
  148. // Construct the link. 
  149. $item_output = $args->before; 
  150. $item_output .= '<a' . $attributes . '>'; 
  151.  
  152. /** 
  153. * Filters the link text to be added to the item output. 
  154. * @since 1.7.0 
  155. * @param string $name Item text to be applied. 
  156. * @param int $value Post ID the title is for. 
  157. */ 
  158. $item_output .= $args->link_before . apply_filters( 'the_title', $item->name, 0 ) . $args->link_after; 
  159. $item_output .= '</a>'; 
  160. $item_output .= $args->after; 
  161.  
  162. /** 
  163. * Filters the final result for the menu item. 
  164. * @since 1.7.0 
  165. * @param string $item_output Constructed output for the menu item to append to output. 
  166. * @param object $item Menu item data object. 
  167. * @param int $depth Depth of menu item. Used for padding. 
  168. * @param array $args Array of arguments for the item. 
  169. */ 
  170. $output .= apply_filters( 'bp_walker_nav_menu_start_el', $item_output, $item, $depth, $args );