1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* LSX functions and definitions - Navigation Walker. |
4
|
|
|
* |
5
|
|
|
* @package lsx |
6
|
|
|
* @subpackage navigation |
7
|
|
|
* @category bootstrap-walker |
8
|
|
|
*/ |
9
|
|
|
|
10
|
|
|
if ( ! defined( 'ABSPATH' ) ) { |
11
|
|
|
exit; |
12
|
|
|
} |
13
|
|
|
|
14
|
|
|
if ( ! class_exists( 'Walker_Nav_Menu' ) ) { |
15
|
|
|
return; |
16
|
|
|
} |
17
|
|
|
|
18
|
|
|
if ( ! class_exists( 'LSX_Nav_Walker' ) ) : |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* Cleaner walker for wp_nav_menu() |
22
|
|
|
* |
23
|
|
|
* Walker_Nav_Menu (WordPress default) example output: |
24
|
|
|
* <li id="menu-item-8" class="menu-item menu-item-type-post_type menu-item-object-page menu-item-8"><a href="/">Home</a></li> |
25
|
|
|
* <li id="menu-item-9" class="menu-item menu-item-type-post_type menu-item-object-page menu-item-9"><a href="/sample-page/">Sample Page</a></l |
26
|
|
|
* |
27
|
|
|
* LSX_Nav_Walker example output: |
28
|
|
|
* <li class="menu-home"><a href="/">Home</a></li> |
29
|
|
|
* <li class="menu-sample-page"><a href="/sample-page/">Sample Page</a></li> |
30
|
|
|
* |
31
|
|
|
* @package lsx |
32
|
|
|
* @subpackage navigation |
33
|
|
|
* @category bootstrap-walker |
34
|
|
|
*/ |
35
|
|
|
class LSX_Nav_Walker extends Walker_Nav_Menu { |
36
|
|
|
|
37
|
|
|
function check_current( $classes ) { |
|
|
|
|
38
|
|
|
return preg_match( '/^(current[-_])|active|dropdown$/', $classes ); |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
function start_lvl( &$output, $depth = 0, $args = array() ) { |
|
|
|
|
42
|
|
|
$output .= "\n<ul class=\"dropdown-menu\">\n"; |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
function start_el( &$output, $item, $depth = 0, $args = array(), $id = 0 ) { |
|
|
|
|
46
|
|
|
$item_html = ''; |
47
|
|
|
|
48
|
|
|
if ( isset( $item->title ) ) { |
49
|
|
|
parent::start_el( $item_html, $item, $depth, $args ); |
50
|
|
|
|
51
|
|
|
if ( $item->is_dropdown && ( 0 === $depth ) ) { |
52
|
|
|
$item_html = str_replace( '<a', '<a class="dropdown-toggle" data-target="#"', $item_html ); |
53
|
|
|
$item_html = str_replace( '</a>', ' <b class="caret"></b></a>', $item_html ); |
54
|
|
|
} elseif ( stristr( $item_html, 'li class="divider"' ) ) { |
55
|
|
|
$item_html = preg_replace( '/<a[^>]*>.*?<\/a>/iU', '', $item_html ); |
56
|
|
|
} elseif ( stristr( $item_html, 'li class="dropdown-header"' ) ) { |
57
|
|
|
$item_html = preg_replace( '/<a[^>]*>(.*)<\/a>/iU', '$1', $item_html ); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
$item_html = apply_filters( 'lsx_wp_nav_menu_item', $item_html ); |
61
|
|
|
$output .= $item_html; |
62
|
|
|
} |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
function display_element( $element, &$children_elements, $max_depth, $depth = 0, $args, &$output ) { |
|
|
|
|
66
|
|
|
$element->is_dropdown = ( ( ! empty( $children_elements[ $element->ID ] ) && ( ( $depth + 1 ) < $max_depth || ( 0 === $max_depth ) ) ) ); |
67
|
|
|
|
68
|
|
|
if ( $element->is_dropdown ) { |
69
|
|
|
if ( $depth > 0 ) { |
70
|
|
|
$element->classes[] = 'dropdown-submenu'; |
71
|
|
|
} else { |
72
|
|
|
$element->classes[] = 'dropdown'; |
73
|
|
|
} |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
parent::display_element( $element, $children_elements, $max_depth, $depth, $args, $output ); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
endif; |
82
|
|
|
|
Adding explicit visibility (
private
,protected
, orpublic
) is generally recommend to communicate to other developers how, and from where this method is intended to be used.