hp-nav-walker.php 11 KB

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