/wp-content/plugins/buddypress/bp-core/classes/class-bp-walker-nav-menu.php

https://github.com/livinglab/openlab · PHP · 227 lines · 68 code · 29 blank · 130 comment · 12 complexity · d8fe2484aa88e0ebd55dc2cd0316069c MD5 · raw file

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