-
Notifications
You must be signed in to change notification settings - Fork 4
/
walker.php
executable file
·123 lines (93 loc) · 3.72 KB
/
walker.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
<?php
namespace CP\BEM;
class navWalker extends \Walker_Nav_Menu
{
/**
* @var string
*/
protected $prefix = '';
/**
* @var string
*/
protected $navListClass = 'nav__list';
/**
* @var string
*/
protected $navItemClass = 'nav__item';
/**
* @var string
*/
protected $navLinkClass = 'nav__link';
/**
* @var string
*/
protected $subNavClass = 'sub-nav';
/**
* @var string
*/
protected $subNavItemClass = 'sub-nav__item';
/**
* @var string
*/
protected $subNavLinkClass = 'sub-nav__link';
public function __construct()
{
add_filter('wp_nav_menu_args', function ($args) {
$args['items_wrap'] = '<ul id="%1$s" class="' . $args['menu_class'] . ' ' . $this->getPrefix() . $this->navListClass . '">%3$s</ul>';
return $args;
});
}
public function start_lvl(&$output, $depth = 0, $args = [])
{
$output .= sprintf('<ul class="%s">', $this->getPrefix() . $this->subNavClass);
}
public function start_el(&$output, $item, $depth = 0, $args = [], $id = 0)
{
$classes = empty($item->classes) ? [] : (array) $item->classes;
array_walk($classes, function (&$value) use ($depth) {
$replacement = $depth ? $this->getPrefix() . $this->subNavItemClass : $this->getPrefix() . $this->navItemClass;
$value = str_replace('menu-item-', sprintf('%s--', $replacement), $value);
$value = str_replace('menu-item', $replacement, $value);
});
$classes[] = sprintf('%s--%s', $this->getPrefix() . $this->navItemClass, $item->ID);
$args = apply_filters('nav_menu_item_args', $args, $item, $depth);
$class_names = join(' ', apply_filters('nav_menu_css_class', array_filter($classes), $item, $args, $depth));
$class_names = $class_names ? sprintf(' class="%s"', esc_attr($class_names)) : '';
$id = apply_filters('nav_menu_item_id', sprintf('%s--%s', $this->getPrefix() . $this->navItemClass, $item->ID), $item, $args, $depth);
$id = $id ? sprintf(' id="%s"', esc_attr($id)) : '';
$output .= sprintf('<li%s%s>', $id, $class_names);
$atts = [];
$atts['title'] = !empty($item->attr_title) ? $item->attr_title : '';
$atts['target'] = !empty($item->target) ? $item->target : '';
$atts['rel'] = !empty($item->xfn) ? $item->xfn : '';
$atts['href'] = !empty($item->url) ? $item->url : '';
$atts['class'] = $depth ? $this->getPrefix() . $this->subNavLinkClass : $this->getPrefix() . $this->navLinkClass;
$atts = apply_filters('nav_menu_link_attributes', $atts, $item, $args, $depth);
$attributes = '';
foreach ($atts as $attr => $value) {
if (empty($value)) {
continue;
}
$value = 'href' === $attr ? esc_url($value) : esc_attr($value);
$attributes .= sprintf('%s="%s"', $attr, $value);
}
$title = apply_filters('the_title', $item->title, $item->ID);
$title = apply_filters('nav_menu_item_title', $title, $item, $args, $depth);
$item_output = $args->before;
$item_output .= sprintf('<a %s>', $attributes);
$item_output .= $args->link_before . $title . $args->link_after;
$item_output .= '</a>';
$item_output .= $args->after;
$output .= apply_filters('walker_nav_menu_start_el', $item_output, $item, $depth, $args);
}
/**
* @return string
*/
protected function getPrefix()
{
if (empty($this->prefix)) {
return '';
}
return $this->prefix;
}
}