class-walker-nav-menu.php 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  1. <?php
  2. /**
  3. * Nav Menu API: Walker_Nav_Menu class
  4. *
  5. * @package WordPress
  6. * @subpackage Nav_Menus
  7. * @since 4.6.0
  8. */
  9. /**
  10. * Core class used to implement an HTML list of nav menu items.
  11. *
  12. * @since 3.0.0
  13. *
  14. * @see Walker
  15. */
  16. class Walker_Nav_Menu extends Walker {
  17. /**
  18. * What the class handles.
  19. *
  20. * @since 3.0.0
  21. * @var string
  22. *
  23. * @see Walker::$tree_type
  24. */
  25. public $tree_type = array( 'post_type', 'taxonomy', 'custom' );
  26. /**
  27. * Database fields to use.
  28. *
  29. * @since 3.0.0
  30. * @todo Decouple this.
  31. * @var array
  32. *
  33. * @see Walker::$db_fields
  34. */
  35. public $db_fields = array( 'parent' => 'menu_item_parent', 'id' => 'db_id' );
  36. /**
  37. * Starts the list before the elements are added.
  38. *
  39. * @since 3.0.0
  40. *
  41. * @see Walker::start_lvl()
  42. *
  43. * @param string $output Used to append additional content (passed by reference).
  44. * @param int $depth Depth of menu item. Used for padding.
  45. * @param stdClass $args An object of wp_nav_menu() arguments.
  46. */
  47. public function start_lvl( &$output, $depth = 0, $args = array() ) {
  48. if ( isset( $args->item_spacing ) && 'discard' === $args->item_spacing ) {
  49. $t = '';
  50. $n = '';
  51. } else {
  52. $t = "\t";
  53. $n = "\n";
  54. }
  55. $indent = str_repeat( $t, $depth );
  56. // Default class.
  57. $classes = array( 'sub-menu' );
  58. /**
  59. * Filters the CSS class(es) applied to a menu list element.
  60. *
  61. * @since 4.8.0
  62. *
  63. * @param array $classes The CSS classes that are applied to the menu `<ul>` element.
  64. * @param stdClass $args An object of `wp_nav_menu()` arguments.
  65. * @param int $depth Depth of menu item. Used for padding.
  66. */
  67. $class_names = join( ' ', apply_filters( 'nav_menu_submenu_css_class', $classes, $args, $depth ) );
  68. $class_names = $class_names ? ' class="' . esc_attr( $class_names ) . '"' : '';
  69. $output .= "{$n}{$indent}<ul$class_names>{$n}";
  70. }
  71. /**
  72. * Ends the list of after the elements are added.
  73. *
  74. * @since 3.0.0
  75. *
  76. * @see Walker::end_lvl()
  77. *
  78. * @param string $output Used to append additional content (passed by reference).
  79. * @param int $depth Depth of menu item. Used for padding.
  80. * @param stdClass $args An object of wp_nav_menu() arguments.
  81. */
  82. public function end_lvl( &$output, $depth = 0, $args = array() ) {
  83. if ( isset( $args->item_spacing ) && 'discard' === $args->item_spacing ) {
  84. $t = '';
  85. $n = '';
  86. } else {
  87. $t = "\t";
  88. $n = "\n";
  89. }
  90. $indent = str_repeat( $t, $depth );
  91. $output .= "$indent</ul>{$n}";
  92. }
  93. /**
  94. * Starts the element output.
  95. *
  96. * @since 3.0.0
  97. * @since 4.4.0 The {@see 'nav_menu_item_args'} filter was added.
  98. *
  99. * @see Walker::start_el()
  100. *
  101. * @param string $output Used to append additional content (passed by reference).
  102. * @param WP_Post $item Menu item data object.
  103. * @param int $depth Depth of menu item. Used for padding.
  104. * @param stdClass $args An object of wp_nav_menu() arguments.
  105. * @param int $id Current item ID.
  106. */
  107. public function start_el( &$output, $item, $depth = 0, $args = array(), $id = 0 ) {
  108. if ( isset( $args->item_spacing ) && 'discard' === $args->item_spacing ) {
  109. $t = '';
  110. $n = '';
  111. } else {
  112. $t = "\t";
  113. $n = "\n";
  114. }
  115. $indent = ( $depth ) ? str_repeat( $t, $depth ) : '';
  116. $classes = empty( $item->classes ) ? array() : (array) $item->classes;
  117. $classes[] = 'menu-item-' . $item->ID;
  118. /**
  119. * Filters the arguments for a single nav menu item.
  120. *
  121. * @since 4.4.0
  122. *
  123. * @param stdClass $args An object of wp_nav_menu() arguments.
  124. * @param WP_Post $item Menu item data object.
  125. * @param int $depth Depth of menu item. Used for padding.
  126. */
  127. $args = apply_filters( 'nav_menu_item_args', $args, $item, $depth );
  128. /**
  129. * Filters the CSS class(es) applied to a menu item's list item element.
  130. *
  131. * @since 3.0.0
  132. * @since 4.1.0 The `$depth` parameter was added.
  133. *
  134. * @param array $classes The CSS classes that are applied to the menu item's `<li>` element.
  135. * @param WP_Post $item The current menu item.
  136. * @param stdClass $args An object of wp_nav_menu() arguments.
  137. * @param int $depth Depth of menu item. Used for padding.
  138. */
  139. $class_names = join( ' ', apply_filters( 'nav_menu_css_class', array_filter( $classes ), $item, $args, $depth ) );
  140. $class_names = $class_names ? ' class="' . esc_attr( $class_names ) . '"' : '';
  141. /**
  142. * Filters the ID applied to a menu item's list item element.
  143. *
  144. * @since 3.0.1
  145. * @since 4.1.0 The `$depth` parameter was added.
  146. *
  147. * @param string $menu_id The ID that is applied to the menu item's `<li>` element.
  148. * @param WP_Post $item The current menu item.
  149. * @param stdClass $args An object of wp_nav_menu() arguments.
  150. * @param int $depth Depth of menu item. Used for padding.
  151. */
  152. $id = apply_filters( 'nav_menu_item_id', 'menu-item-'. $item->ID, $item, $args, $depth );
  153. $id = $id ? ' id="' . esc_attr( $id ) . '"' : '';
  154. $output .= $indent . '<li' . $id . $class_names .'>';
  155. $atts = array();
  156. $atts['title'] = ! empty( $item->attr_title ) ? $item->attr_title : '';
  157. $atts['target'] = ! empty( $item->target ) ? $item->target : '';
  158. $atts['rel'] = ! empty( $item->xfn ) ? $item->xfn : '';
  159. $atts['href'] = ! empty( $item->url ) ? $item->url : '';
  160. /**
  161. * Filters the HTML attributes applied to a menu item's anchor element.
  162. *
  163. * @since 3.6.0
  164. * @since 4.1.0 The `$depth` parameter was added.
  165. *
  166. * @param array $atts {
  167. * The HTML attributes applied to the menu item's `<a>` element, empty strings are ignored.
  168. *
  169. * @type string $title Title attribute.
  170. * @type string $target Target attribute.
  171. * @type string $rel The rel attribute.
  172. * @type string $href The href attribute.
  173. * }
  174. * @param WP_Post $item The current menu item.
  175. * @param stdClass $args An object of wp_nav_menu() arguments.
  176. * @param int $depth Depth of menu item. Used for padding.
  177. */
  178. $atts = apply_filters( 'nav_menu_link_attributes', $atts, $item, $args, $depth );
  179. $attributes = '';
  180. foreach ( $atts as $attr => $value ) {
  181. if ( ! empty( $value ) ) {
  182. $value = ( 'href' === $attr ) ? esc_url( $value ) : esc_attr( $value );
  183. $attributes .= ' ' . $attr . '="' . $value . '"';
  184. }
  185. }
  186. /** This filter is documented in wp-includes/post-template.php */
  187. $title = apply_filters( 'the_title', $item->title, $item->ID );
  188. /**
  189. * Filters a menu item's title.
  190. *
  191. * @since 4.4.0
  192. *
  193. * @param string $title The menu item's title.
  194. * @param WP_Post $item The current menu item.
  195. * @param stdClass $args An object of wp_nav_menu() arguments.
  196. * @param int $depth Depth of menu item. Used for padding.
  197. */
  198. $title = apply_filters( 'nav_menu_item_title', $title, $item, $args, $depth );
  199. $item_output = $args->before;
  200. $item_output .= '<a'. $attributes .'>';
  201. $item_output .= $args->link_before . $title . $args->link_after;
  202. $item_output .= '</a>';
  203. $item_output .= $args->after;
  204. /**
  205. * Filters a menu item's starting output.
  206. *
  207. * The menu item's starting output only includes `$args->before`, the opening `<a>`,
  208. * the menu item's title, the closing `</a>`, and `$args->after`. Currently, there is
  209. * no filter for modifying the opening and closing `<li>` for a menu item.
  210. *
  211. * @since 3.0.0
  212. *
  213. * @param string $item_output The menu item's starting HTML output.
  214. * @param WP_Post $item Menu item data object.
  215. * @param int $depth Depth of menu item. Used for padding.
  216. * @param stdClass $args An object of wp_nav_menu() arguments.
  217. */
  218. $output .= apply_filters( 'walker_nav_menu_start_el', $item_output, $item, $depth, $args );
  219. }
  220. /**
  221. * Ends the element output, if needed.
  222. *
  223. * @since 3.0.0
  224. *
  225. * @see Walker::end_el()
  226. *
  227. * @param string $output Used to append additional content (passed by reference).
  228. * @param WP_Post $item Page data object. Not used.
  229. * @param int $depth Depth of page. Not Used.
  230. * @param stdClass $args An object of wp_nav_menu() arguments.
  231. */
  232. public function end_el( &$output, $item, $depth = 0, $args = array() ) {
  233. if ( isset( $args->item_spacing ) && 'discard' === $args->item_spacing ) {
  234. $t = '';
  235. $n = '';
  236. } else {
  237. $t = "\t";
  238. $n = "\n";
  239. }
  240. $output .= "</li>{$n}";
  241. }
  242. } // Walker_Nav_Menu