-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnavigation.php
More file actions
360 lines (323 loc) · 11.2 KB
/
navigation.php
File metadata and controls
360 lines (323 loc) · 11.2 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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
<?php
/**
* Template Name: Post
* Template Post Type: Navigation
* Description: .
* @package
*/
if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly.
}
if ( ! function_exists( 'creativity_navigation_position' ) ) {
/**
* Build the navigation.
*
*/
function creativity_navigation_position() {
?>
<nav itemtype="https://schema.org/SiteNavigationElement" itemscope="itemscope" id="site-navigation" <?php creativity_navigation_class(); ?>>
<div <?php creativity_inside_navigation_class(); ?>>
<?php
/**
* creativity_inside_navigation hook.
*
*
* @hooked creativity_navigation_search - 10
* @hooked creativity_mobile_menu_search_icon - 10
*/
do_action( 'creativity_inside_navigation' );
?>
<button class="menu-toggle" aria-controls="primary-menu" aria-expanded="false">
<?php do_action( 'creativity_inside_mobile_menu' ); ?>
<span class="mobile-menu"><?php esc_html_e( 'Menu', 'creativity' ); ?></span>
</button>
<?php
wp_nav_menu(
array(
'theme_location' => 'primary',
'container' => 'div',
'container_class' => 'main-nav',
'container_id' => 'primary-menu',
'menu_class' => '',
'fallback_cb' => 'creativity_menu_fallback',
'items_wrap' => '<ul id="%1$s" class="%2$s ' . join( ' ', creativity_get_menu_class() ) . '">%3$s</ul>'
)
);
?>
</div><!-- .inside-navigation -->
</nav><!-- #site-navigation -->
<?php
}
}
if ( ! function_exists( 'creativity_menu_fallback' ) ) {
/**
* Menu fallback.
*
*
* @param array $args
* @return string
*/
function creativity_menu_fallback( $args ) {
$creativity_settings = wp_parse_args(
get_option( 'creativity_settings', array() ),
creativity_get_defaults()
);
?>
<div id="primary-menu" class="main-nav">
<ul <?php creativity_menu_class(); ?>>
<?php
$args = array(
'sort_column' => 'menu_order',
'title_li' => '',
'walker' => new creativity_Page_Walker()
);
wp_list_pages( $args );
if ( 'enable' == $creativity_settings['nav_search'] ) {
echo '<li class="search-item" title="' . esc_attr_x( 'Search', 'submit button', 'creativity' ) . '"><a href="#"><span class="screen-reader-text">' . esc_html_x( 'Search', 'submit button', 'creativity' ) . '</span></a></li>';
}
?>
</ul>
</div><!-- .main-nav -->
<?php
}
}
/**
* Generate the navigation based on settings
*
* It would be better to have all of these inside one action, but these
* are kept this way to maintain backward compatibility for people
* un-hooking and moving the navigation/changing the priority.
*
*/
if ( ! function_exists( 'creativity_add_navigation_after_header' ) ) {
add_action( 'creativity_after_header', 'creativity_add_navigation_after_header', 5 );
function creativity_add_navigation_after_header() {
if ( 'nav-below-header' == creativity_get_navigation_location() ) {
creativity_navigation_position();
}
}
}
if ( ! function_exists( 'creativity_add_navigation_before_header' ) ) {
add_action( 'creativity_before_header', 'creativity_add_navigation_before_header', 5 );
function creativity_add_navigation_before_header() {
if ( 'nav-above-header' == creativity_get_navigation_location() ) {
creativity_navigation_position();
}
}
}
if ( ! function_exists( 'creativity_add_navigation_float_right' ) ) {
add_action( 'creativity_after_header_content', 'creativity_add_navigation_float_right', 5 );
function creativity_add_navigation_float_right() {
if ( 'nav-float-right' == creativity_get_navigation_location() || 'nav-float-left' == creativity_get_navigation_location() ) {
creativity_navigation_position();
}
}
}
if ( ! function_exists( 'creativity_add_navigation_before_right_sidebar' ) ) {
add_action( 'creativity_before_right_sidebar_content', 'creativity_add_navigation_before_right_sidebar', 5 );
function creativity_add_navigation_before_right_sidebar() {
if ( 'nav-right-sidebar' == creativity_get_navigation_location() ) {
echo '<div class="gen-sidebar-nav">';
creativity_navigation_position();
echo '</div>';
}
}
}
if ( ! function_exists( 'creativity_add_navigation_before_left_sidebar' ) ) {
add_action( 'creativity_before_left_sidebar_content', 'creativity_add_navigation_before_left_sidebar', 5 );
function creativity_add_navigation_before_left_sidebar() {
if ( 'nav-left-sidebar' == creativity_get_navigation_location() ) {
echo '<div class="gen-sidebar-nav">';
creativity_navigation_position();
echo '</div>';
}
}
}
if ( ! class_exists( 'creativity_Page_Walker' ) && class_exists( 'Walker_Page' ) ) {
/**
* Add current-menu-item to the current item if no theme location is set
* This means we don't have to duplicate CSS properties for current_page_item and current-menu-item
*
*/
class creativity_Page_Walker extends Walker_Page {
function start_el( &$output, $page, $depth = 0, $args = array(), $current_page = 0 ) {
$css_class = array( 'page_item', 'page-item-' . $page->ID );
$button = '';
if ( isset( $args['pages_with_children'][ $page->ID ] ) ) {
$css_class[] = 'menu-item-has-children';
$button = '<span role="button" class="dropdown-menu-toggle" aria-expanded="false"></span>';
}
if ( ! empty( $current_page ) ) {
$_current_page = get_post( $current_page );
if ( $_current_page && in_array( $page->ID, $_current_page->ancestors ) ) {
$css_class[] = 'current-menu-ancestor';
}
if ( $page->ID == $current_page ) {
$css_class[] = 'current-menu-item';
} elseif ( $_current_page && $page->ID == $_current_page->post_parent ) {
$css_class[] = 'current-menu-parent';
}
} elseif ( $page->ID == get_option('page_for_posts') ) {
$css_class[] = 'current-menu-parent';
}
$css_classes = implode( ' ', apply_filters( 'page_css_class', $css_class, $page, $depth, $args, $current_page ) );
$args['link_before'] = empty( $args['link_before'] ) ? '' : $args['link_before'];
$args['link_after'] = empty( $args['link_after'] ) ? '' : $args['link_after'];
$output .= sprintf(
'<li class="%s"><a href="%s">%s%s%s%s</a>',
$css_classes,
get_permalink( $page->ID ),
$args['link_before'],
apply_filters( 'the_title', $page->post_title, $page->ID ),
$args['link_after'],
$button
);
}
}
}
if ( ! function_exists( 'creativity_dropdown_icon_to_menu_link' ) ) {
add_filter( 'nav_menu_item_title', 'creativity_dropdown_icon_to_menu_link', 10, 4 );
/**
* Add dropdown icon if menu item has children.
*
*
* @param string $title The menu item title.
* @param WP_Post $item All of our menu item data.
* @param stdClass $args All of our menu item args.
* @param int $dept Depth of menu item.
* @return string The menu item.
*/
function creativity_dropdown_icon_to_menu_link( $title, $item, $args, $depth ) {
$role = 'presentation';
$tabindex = '';
if ( 'click-arrow' === creativity_get_setting( 'nav_dropdown_type' ) ) {
$role = 'button';
$tabindex = ' tabindex="0"';
}
// Loop through our menu items and add our dropdown icons.
if ( 'main-nav' === $args->container_class ) {
foreach ( $item->classes as $value ) {
if ( 'menu-item-has-children' === $value ) {
$title = $title . '<span role="' . $role . '" class="dropdown-menu-toggle"' . $tabindex .'></span>';
}
}
}
// Return our title.
return $title;
}
}
if ( ! function_exists( 'creativity_navigation_search' ) ) {
add_action( 'creativity_inside_navigation', 'creativity_navigation_search' );
/**
* Add the search bar to the navigation.
*
*/
function creativity_navigation_search() {
$creativity_settings = wp_parse_args(
get_option( 'creativity_settings', array() ),
creativity_get_defaults()
);
if ( 'enable' !== $creativity_settings['nav_search'] ) {
return;
}
echo get_search_form();
}
}
if ( ! function_exists( 'creativity_menu_search_icon' ) ) {
add_filter( 'wp_nav_menu_items', 'creativity_menu_search_icon', 10, 2 );
/**
* Add search icon to primary menu if set
*
*
* @param string $nav The HTML list content for the menu items.
* @param stdClass $args An object containing wp_nav_menu() arguments.
* @return string The search icon menu item.
*/
function creativity_menu_search_icon( $nav, $args ) {
$creativity_settings = wp_parse_args(
get_option( 'creativity_settings', array() ),
creativity_get_defaults()
);
// If the search icon isn't enabled, return the regular nav.
if ( 'enable' !== $creativity_settings['nav_search'] ) {
return $nav;
}
// If our primary menu is set, add the search icon.
if ( $args->theme_location == 'primary' ) {
return $nav . '<li class="search-item" title="' . esc_attr_x( 'Search', 'submit button', 'creativity' ) . '"><a href="#"><span class="screen-reader-text">' . _x( 'Search', 'submit button', 'creativity' ) . '</span></a></li>';
}
// Our primary menu isn't set, return the regular nav.
// In this case, the search icon is added to the creativity_menu_fallback() function in navigation.php.
return $nav;
}
}
if ( ! function_exists( 'creativity_mobile_menu_search_icon' ) ) {
add_action( 'creativity_inside_navigation', 'creativity_mobile_menu_search_icon' );
/**
* Add search icon to mobile menu bar
*
*/
function creativity_mobile_menu_search_icon() {
$creativity_settings = wp_parse_args(
get_option( 'creativity_settings', array() ),
creativity_get_defaults()
);
// If the search icon isn't enabled, return the regular nav.
if ( 'enable' !== $creativity_settings['nav_search'] ) {
return;
}
?>
<div class="mobile-bar-items">
<?php do_action( 'creativity_inside_mobile_menu_bar' ); ?>
<span class="search-item" title="<?php echo esc_attr_x( 'Search', 'submit button', 'creativity' ); ?>">
<a href="#">
<span class="screen-reader-text"><?php echo esc_attr_x( 'Search', 'submit button', 'creativity' ); ?></span>
</a>
</span>
</div><!-- .mobile-bar-items -->
<?php
}
}
add_action( 'wp_footer', 'creativity_clone_sidebar_navigation' );
/**
* Clone our sidebar navigation and place it below the header.
* This places our mobile menu in a more user-friendly location.
*
* We're not using wp_add_inline_script() as this needs to happens
* before menu.js is enqueued.
*
*/
function creativity_clone_sidebar_navigation() {
if ( 'nav-left-sidebar' !== creativity_get_navigation_location() && 'nav-right-sidebar' !== creativity_get_navigation_location() ) {
return;
}
?>
<script>
var target, nav, clone;
nav = document.getElementById( 'site-navigation' );
if ( nav ) {
clone = nav.cloneNode( true );
clone.className += ' sidebar-nav-mobile';
clone.setAttribute( 'aria-label', '<?php esc_attr_e( 'Mobile Menu', 'creativity' ); ?>' );
target = document.getElementById( 'masthead' );
if ( target ) {
target.insertAdjacentHTML( 'afterend', clone.outerHTML );
} else {
document.body.insertAdjacentHTML( 'afterbegin', clone.outerHTML )
}
}
</script>
<?php
}
function creativity_nav_menu_link_attributes( $atts, $item, $args ) {
$creativity_settings = wp_parse_args(
get_option( 'creativity_settings', array() ),
creativity_get_defaults()
);
$naveffect = $creativity_settings[ 'nav_effect' ];
if ( $naveffect == 'styled' ) {
$atts['data-hover'] = $item->title;
}
return $atts;
}
add_filter( 'nav_menu_link_attributes', 'creativity_nav_menu_link_attributes', 10, 3 );