PageRenderTime 47ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

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

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