/wp-includes/class-walker-nav-menu.php

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