wp-nav-walker.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  1. <?php
  2. /***********************************************************
  3. // https://github.com/wp-bootstrap/wp-bootstrap-navwalker //
  4. // License URI: http://www.gnu.org/licenses/gpl-3.0.txt //
  5. // note: line 95 add newline to <li> for clean output //
  6. ***********************************************************/
  7. // Check if Class Exists.
  8. if ( ! class_exists( 'WP_Bootstrap_Navwalker' ) ) :
  9. class WP_Bootstrap_Navwalker extends Walker_Nav_Menu {
  10. private $has_schema = false;
  11. public function __construct() {
  12. if ( ! has_filter( 'wp_nav_menu_args', array( $this, 'add_schema_to_navbar_ul' ) ) ) {
  13. add_filter( 'wp_nav_menu_args', array( $this, 'add_schema_to_navbar_ul' ) );
  14. }
  15. }
  16. public function start_lvl( &$output, $depth = 0, $args = null ) {
  17. if ( isset( $args->item_spacing ) && 'discard' === $args->item_spacing ) {
  18. $t = '';
  19. $n = '';
  20. } else {
  21. $t = "\t";
  22. $n = "\n";
  23. }
  24. $indent = str_repeat( $t, $depth );
  25. $classes = array( 'dropdown-menu' );
  26. $class_names = join( ' ', apply_filters( 'nav_menu_submenu_css_class', $classes, $args, $depth ) );
  27. $class_names = $class_names ? ' class="' . esc_attr( $class_names ) . '"' : '';
  28. $labelledby = '';
  29. preg_match_all( '/(<a.*?id=\"|\')(.*?)\"|\'.*?>/im', $output, $matches );
  30. if ( end( $matches[2] ) ) {
  31. $labelledby = 'aria-labelledby="' . esc_attr( end( $matches[2] ) ) . '"';
  32. }
  33. $output .= "{$n}{$indent}<ul$class_names $labelledby>{$n}";
  34. }
  35. public function start_el( &$output, $item, $depth = 0, $args = null, $id = 0 ) {
  36. if ( isset( $args->item_spacing ) && 'discard' === $args->item_spacing ) {
  37. $t = '';
  38. $n = '';
  39. } else {
  40. $t = "\t";
  41. $n = "\n";
  42. }
  43. $indent = ( $depth ) ? str_repeat( $t, $depth ) : '';
  44. if ( false !== strpos( $args->items_wrap, 'itemscope' ) && false === $this->has_schema ) {
  45. $this->has_schema = true;
  46. $args->link_before = '<span itemprop="name">' . $args->link_before;
  47. $args->link_after .= '</span>';
  48. }
  49. $classes = empty( $item->classes ) ? array() : (array) $item->classes;
  50. $split_on_spaces = function ( $class ) {
  51. return preg_split( '/\s+/', $class );
  52. };
  53. $classes = $this->flatten( array_map( $split_on_spaces, $classes ) );
  54. $linkmod_classes = array();
  55. $icon_classes = array();
  56. $classes = self::separate_linkmods_and_icons_from_classes( $classes, $linkmod_classes, $icon_classes, $depth );
  57. $icon_class_string = join( ' ', $icon_classes );
  58. $args = apply_filters( 'nav_menu_item_args', $args, $item, $depth );
  59. if ( $this->has_children ) {
  60. $classes[] = 'dropdown';
  61. }
  62. if ( in_array( 'current-menu-item', $classes, true ) || in_array( 'current-menu-parent', $classes, true ) ) {
  63. $classes[] = 'active';
  64. }
  65. $classes[] = 'menu-item-' . $item->ID;
  66. $classes[] = 'nav-item';
  67. $classes = apply_filters( 'nav_menu_css_class', array_filter( $classes ), $item, $args, $depth );
  68. $class_names = join( ' ', $classes );
  69. $class_names = $class_names ? ' class="' . esc_attr( $class_names ) . '"' : '';
  70. $id = apply_filters( 'nav_menu_item_id', 'menu-item-' . $item->ID, $item, $args, $depth );
  71. $id = $id ? ' id="' . esc_attr( $id ) . '"' : '';
  72. $output .= $indent . '<li ' . $id . $class_names . '>' . $n;
  73. $atts = array();
  74. $atts['title'] = ! empty( $item->attr_title ) ? $item->attr_title : '';
  75. $atts['target'] = ! empty( $item->target ) ? $item->target : '';
  76. if ( '_blank' === $item->target && empty( $item->xfn ) ) {
  77. $atts['rel'] = 'noopener noreferrer';
  78. } else {
  79. $atts['rel'] = ! empty( $item->xfn ) ? $item->xfn : '';
  80. }
  81. if ( $this->has_children && 0 === $depth ) {
  82. $atts['href'] = '#';
  83. $atts['data-bs-toggle'] = 'dropdown';
  84. $atts['aria-haspopup'] = 'true';
  85. $atts['aria-expanded'] = 'false';
  86. $atts['class'] = 'dropdown-toggle nav-link';
  87. $atts['id'] = 'menu-item-dropdown-' . $item->ID;
  88. } else {
  89. if ( true === $this->has_schema ) {
  90. $atts['itemprop'] = 'url';
  91. }
  92. $atts['href'] = ! empty( $item->url ) ? $item->url : '#';
  93. if ( $depth > 0 ) {
  94. $atts['class'] = 'dropdown-item';
  95. } else {
  96. $atts['class'] = 'nav-link';
  97. }
  98. }
  99. $atts['aria-current'] = $item->current ? 'page' : '';
  100. $atts = self::update_atts_for_linkmod_type( $atts, $linkmod_classes );
  101. $atts = apply_filters( 'nav_menu_link_attributes', $atts, $item, $args, $depth );
  102. $attributes = '';
  103. foreach ( $atts as $attr => $value ) {
  104. if ( ! empty( $value ) ) {
  105. $value = ( 'href' === $attr ) ? esc_url( $value ) : esc_attr( $value );
  106. $attributes .= ' ' . $attr . '="' . $value . '"';
  107. }
  108. }
  109. $linkmod_type = self::get_linkmod_type( $linkmod_classes );
  110. $item_output = isset( $args->before ) ? $args->before : '';
  111. if ( '' !== $linkmod_type ) {
  112. $item_output .= self::linkmod_element_open( $linkmod_type, $attributes );
  113. } else {
  114. $item_output .= '<a' . $attributes . '>';
  115. }
  116. $icon_html = '';
  117. if ( ! empty( $icon_class_string ) ) {
  118. $icon_html = '<i class="' . esc_attr( $icon_class_string ) . '" aria-hidden="true"></i> ';
  119. }
  120. $title = apply_filters( 'the_title', $item->title, $item->ID );
  121. $title = apply_filters( 'nav_menu_item_title', $title, $item, $args, $depth );
  122. if ( in_array( 'sr-only', $linkmod_classes, true ) ) {
  123. $title = self::wrap_for_screen_reader( $title );
  124. $keys_to_unset = array_keys( $linkmod_classes, 'sr-only', true );
  125. foreach ( $keys_to_unset as $k ) {
  126. unset( $linkmod_classes[ $k ] );
  127. }
  128. }
  129. $item_output .= isset( $args->link_before ) ? $args->link_before . $icon_html . $title . $args->link_after : '';
  130. if ( '' !== $linkmod_type ) {
  131. $item_output .= self::linkmod_element_close( $linkmod_type );
  132. } else {
  133. $item_output .= '</a>';
  134. }
  135. $item_output .= isset( $args->after ) ? $args->after : '';
  136. $output .= apply_filters( 'walker_nav_menu_start_el', $item_output, $item, $depth, $args );
  137. }
  138. public static function fallback( $args ) {
  139. if ( ! current_user_can( 'edit_theme_options' ) ) {
  140. return;
  141. }
  142. $fallback_output = '';
  143. $show_container = false;
  144. if ( $args['container'] ) {
  145. $allowed_tags = apply_filters( 'wp_nav_menu_container_allowedtags', array( 'div', 'nav' ) );
  146. if ( is_string( $args['container'] ) && in_array( $args['container'], $allowed_tags, true ) ) {
  147. $show_container = true;
  148. $class = $args['container_class'] ? ' class="menu-fallback-container ' . esc_attr( $args['container_class'] ) . '"' : ' class="menu-fallback-container"';
  149. $id = $args['container_id'] ? ' id="' . esc_attr( $args['container_id'] ) . '"' : '';
  150. $fallback_output .= '<' . $args['container'] . $id . $class . '>';
  151. }
  152. }
  153. $class = $args['menu_class'] ? ' class="menu-fallback-menu ' . esc_attr( $args['menu_class'] ) . '"' : ' class="menu-fallback-menu"';
  154. $id = $args['menu_id'] ? ' id="' . esc_attr( $args['menu_id'] ) . '"' : '';
  155. $fallback_output .= '<ul' . $id . $class . '>';
  156. $fallback_output .= '<li class="nav-item"><a href="' . esc_url( admin_url( 'nav-menus.php' ) ) . '" class="nav-link" title="' . esc_attr__( 'Add a menu', 'wp-bootstrap-navwalker' ) . '">' . esc_html__( 'Add a menu', 'wp-bootstrap-navwalker' ) . '</a></li>';
  157. $fallback_output .= '</ul>';
  158. if ( $show_container ) {
  159. $fallback_output .= '</' . $args['container'] . '>';
  160. }
  161. if ( array_key_exists( 'echo', $args ) && $args['echo'] ) {
  162. echo $fallback_output;
  163. } else {
  164. return $fallback_output;
  165. }
  166. }
  167. public function add_schema_to_navbar_ul( $args ) {
  168. $wrap = $args['items_wrap'];
  169. if ( strpos( $wrap, 'SiteNavigationElement' ) === false ) {
  170. $args['items_wrap'] = preg_replace( '/(>).*>?\%3\$s/', ' itemscope itemtype="http://www.schema.org/SiteNavigationElement"$0', $wrap );
  171. }
  172. return $args;
  173. }
  174. private function separate_linkmods_and_icons_from_classes( $classes, &$linkmod_classes, &$icon_classes, $depth ) {
  175. foreach ( $classes as $key => $class ) {
  176. if ( preg_match( '/^disabled|^sr-only/i', $class ) ) {
  177. // Test for .disabled or .sr-only classes.
  178. $linkmod_classes[] = $class;
  179. unset( $classes[ $key ] );
  180. } elseif ( preg_match( '/^dropdown-header|^dropdown-divider|^dropdown-item-text/i', $class ) && $depth > 0 ) {
  181. $linkmod_classes[] = $class;
  182. unset( $classes[ $key ] );
  183. } elseif ( preg_match( '/^fa-(\S*)?|^fa(s|r|l|b)?(\s?)?$/i', $class ) ) {
  184. // Font Awesome.
  185. $icon_classes[] = $class;
  186. unset( $classes[ $key ] );
  187. } elseif ( preg_match( '/^glyphicon-(\S*)?|^glyphicon(\s?)$/i', $class ) ) {
  188. // Glyphicons.
  189. $icon_classes[] = $class;
  190. unset( $classes[ $key ] );
  191. }
  192. }
  193. return $classes;
  194. }
  195. private function get_linkmod_type( $linkmod_classes = array() ) {
  196. $linkmod_type = '';
  197. // Loop through array of linkmod classes to handle their $atts.
  198. if ( ! empty( $linkmod_classes ) ) {
  199. foreach ( $linkmod_classes as $link_class ) {
  200. if ( ! empty( $link_class ) ) {
  201. // Check for special class types and set a flag for them.
  202. if ( 'dropdown-header' === $link_class ) {
  203. $linkmod_type = 'dropdown-header';
  204. } elseif ( 'dropdown-divider' === $link_class ) {
  205. $linkmod_type = 'dropdown-divider';
  206. } elseif ( 'dropdown-item-text' === $link_class ) {
  207. $linkmod_type = 'dropdown-item-text';
  208. }
  209. }
  210. }
  211. }
  212. return $linkmod_type;
  213. }
  214. private function update_atts_for_linkmod_type( $atts = array(), $linkmod_classes = array() ) {
  215. if ( ! empty( $linkmod_classes ) ) {
  216. foreach ( $linkmod_classes as $link_class ) {
  217. if ( ! empty( $link_class ) ) {
  218. if ( 'sr-only' !== $link_class ) {
  219. $atts['class'] .= ' ' . esc_attr( $link_class );
  220. }
  221. if ( 'disabled' === $link_class ) {
  222. $atts['href'] = '#';
  223. unset( $atts['target'] );
  224. } elseif ( 'dropdown-header' === $link_class || 'dropdown-divider' === $link_class || 'dropdown-item-text' === $link_class ) {
  225. unset( $atts['href'] );
  226. unset( $atts['target'] );
  227. }
  228. }
  229. }
  230. }
  231. return $atts;
  232. }
  233. private function wrap_for_screen_reader( $text = '' ) {
  234. if ( $text ) {
  235. $text = '<span class="sr-only">' . $text . '</span>';
  236. }
  237. return $text;
  238. }
  239. private function linkmod_element_open( $linkmod_type, $attributes = '' ) {
  240. $output = '';
  241. if ( 'dropdown-item-text' === $linkmod_type ) {
  242. $output .= '<span class="dropdown-item-text"' . $attributes . '>';
  243. } elseif ( 'dropdown-header' === $linkmod_type ) {
  244. $output .= '<span class="dropdown-header h6"' . $attributes . '>';
  245. } elseif ( 'dropdown-divider' === $linkmod_type ) {
  246. // This is a divider.
  247. $output .= '<div class="dropdown-divider"' . $attributes . '>';
  248. }
  249. return $output;
  250. }
  251. private function linkmod_element_close( $linkmod_type ) {
  252. $output = '';
  253. if ( 'dropdown-header' === $linkmod_type || 'dropdown-item-text' === $linkmod_type ) {
  254. $output .= '</span>';
  255. } elseif ( 'dropdown-divider' === $linkmod_type ) {
  256. // This is a divider.
  257. $output .= '</div>';
  258. }
  259. return $output;
  260. }
  261. public function flatten( $array ) {
  262. $result = array();
  263. foreach ( $array as $element ) {
  264. if ( is_array( $element ) ) {
  265. array_push( $result, ...$this->flatten( $element ) );
  266. } else {
  267. $result[] = $element;
  268. }
  269. }
  270. return $result;
  271. }
  272. }
  273. endif;