This repository was archived by the owner on Jun 24, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNavWalker.php
More file actions
168 lines (113 loc) · 3.59 KB
/
Copy pathNavWalker.php
File metadata and controls
168 lines (113 loc) · 3.59 KB
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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
<?php
/**
* Clean Nav Walker
*
* @package ThemePlate
* @since 0.1.0
*/
namespace ThemePlate;
use stdClass;
use Walker_Nav_Menu;
use WP_Post;
if ( ! class_exists( 'Walker_Nav_Menu' ) ) {
return;
}
class NavWalker extends Walker_Nav_Menu {
public const DEFAULTS = array(
'sub-menu' => 'sub-menu',
'has-sub' => 'has-sub',
'active' => 'active',
'item' => '',
'depth' => '',
);
public array $classes = array();
public int $priority = 0;
public function __construct() {
$this->classes = array_merge( self::DEFAULTS, $this->classes );
add_filter( 'nav_menu_submenu_css_class', array( $this, 'submenu_css_class' ), $this->priority, 3 );
add_filter( 'nav_menu_css_class', array( $this, 'css_class' ), $this->priority, 4 );
add_filter( 'nav_menu_item_id', array( $this, 'item_id' ), $this->priority, 4 );
add_filter( 'nav_menu_link_attributes', array( $this, 'link_attributes' ), $this->priority, 4 );
}
public function submenu_css_class( array $classes, stdClass $args, int $depth ): array {
if ( ! $args->walker instanceof $this ) {
return $classes;
}
$classes = array( $this->classes['sub-menu'] );
if ( ! empty( $this->classes['depth'] ) ) {
$classes[] = $this->classes['depth'] . $depth;
}
return $classes;
}
public function css_class( array $classes, WP_Post $menu_item, stdClass $args, int $depth ): array {
if ( ! $args->walker instanceof $this ) {
return $classes;
}
$classes = array( $this->classes['item'] );
if ( $args->walker->has_children ) {
$classes[] = $this->classes['has-sub'];
}
if ( isset( $menu_item->current ) && $menu_item->current ) {
$classes[] = $this->classes['active'];
}
if ( ! empty( $this->classes['depth'] ) ) {
$classes[] = $this->classes['depth'] . $depth;
}
return array_filter( $classes );
}
public function item_id( string $menu_id, WP_Post $menu_item, stdClass $args, int $depth ): string {
if ( ! $args->walker instanceof $this ) {
return $menu_id;
}
if ( 'menu-item-' . $menu_item->ID === $menu_id ) {
$menu_id = '';
}
return $menu_id;
}
public function link_attributes( array $atts, WP_Post $menu_item, stdClass $args, int $depth ): array {
if ( ! $args->walker instanceof $this ) {
return $atts;
}
if ( method_exists( $this, 'attributes' ) ) {
$atts = array_merge( $atts, call_user_func_array( array( $this, 'attributes' ), func_get_args() ) );
}
return array_filter( $atts );
}
public static function fallback( $args ) {
if ( ! current_user_can( 'edit_theme_options' ) ) {
return false;
}
$output = '';
if ( $args['container'] ) {
$output .= '<' . $args['container'];
if ( $args['container_id'] ) {
$output .= ' id="' . $args['container_id'] . '"';
}
if ( $args['container_class'] ) {
$output .= ' class="' . $args['container_class'] . '"';
}
$output .= '>';
}
$output .= '<ul';
if ( $args['menu_id'] ) {
$output .= ' id="' . $args['menu_id'] . '"';
}
if ( $args['menu_class'] ) {
$output .= ' class="' . $args['menu_class'] . '"';
}
$output .= '>';
$output .= '<li><a href="' . esc_url( admin_url( 'nav-menus.php' ) ) . '">Click here</a></li>';
$output .= '<li><a href="' . esc_url( admin_url( 'nav-menus.php' ) ) . '">to add</a></li>';
$output .= '<li><a href="' . esc_url( admin_url( 'nav-menus.php' ) ) . '">a menu</a></li>';
$output .= '</ul>';
if ( $args['container'] ) {
$output .= '</' . $args['container'] . '>';
}
if ( $args['echo'] ) {
echo $output; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
} else {
return $output;
}
return true;
}
}