Skip to content

Commit

Permalink
Refactoring nav_menus classes
Browse files Browse the repository at this point in the history
  • Loading branch information
inc2734 committed Nov 17, 2016
1 parent f6ba569 commit d248668
Show file tree
Hide file tree
Showing 4 changed files with 116 additions and 86 deletions.
53 changes: 53 additions & 0 deletions app/setup/drawer-nav.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<?php
/**
* @package mimizuku
* @author inc2734
* @license GPL-2.0+
*/

namespace Mimizuku\App\Setup;

class Drawer_Nav {

public function __construct() {
add_filter( 'wp_nav_menu', [ $this, 'wp_nav_menu' ], 10, 2 );
add_filter( 'nav_menu_css_class', [ $this, 'classes' ], 10, 4 );
}

/**
* Sets up attributs
*
* @return void
* @see https://developer.wordpress.org/reference/functions/wp_nav_menu/
*/
public function wp_nav_menu( $nav_menu, $args ) {
if ( 'drawer-nav' !== $args->theme_location ) {
return $nav_menu;
}

return preg_replace(
'/<ul class="sub-menu">/ms',
'<div class="_p-drawer__toggle" data-c="drawer__toggle" aria-expanded="false"><i class="fa fa-angle-right"></i></div><ul class="_p-drawer__submenu" data-c="drawer__submenu" aria-hidden="true">',
$nav_menu
);
}

/**
* Sets up classes
*
* @return void
* @see https://developer.wordpress.org/reference/classes/walker_nav_menu/
*/
public function classes( $classes, $item, $args, $depth ) {
if ( 'drawer-nav' !== $args->theme_location ) {
return $classes;
}

if ( $depth > 0 ) {
$classes[] = '_p-drawer__subitem';
} else {
$classes[] = '_p-drawer__item';
}
return $classes;
}
}
61 changes: 61 additions & 0 deletions app/setup/global-nav.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
<?php
/**
* @package mimizuku
* @author inc2734
* @license GPL-2.0+
*/

namespace Mimizuku\App\Setup;

class Global_Nav {

public function __construct() {
add_filter( 'wp_nav_menu', [ $this, 'wp_nav_menu' ], 10, 2 );
add_filter( 'nav_menu_css_class', [ $this, 'classes' ], 10, 4 );
}

/**
* Sets up attributs
*
* @return void
* @see https://developer.wordpress.org/reference/functions/wp_nav_menu/
*/
public function wp_nav_menu( $nav_menu, $args ) {
if ( 'global-nav' !== $args->theme_location ) {
return $nav_menu;
}

$nav_menu = preg_replace(
'/menu-item-has-children(.*?)"/ms',
'menu-item-has-children$1" aria-haspopup="true"',
$nav_menu
);

$nav_menu = preg_replace(
'/<ul class="sub-menu">/ms',
'<ul class="_c-menu__submenu" data-c="menu__submenu" aria-hidden="true">',
$nav_menu
);

return $nav_menu;
}

/**
* Sets up classes
*
* @return void
* @see https://developer.wordpress.org/reference/classes/walker_nav_menu/
*/
public function classes( $classes, $item, $args, $depth ) {
if ( 'global-nav' !== $args->theme_location ) {
return $classes;
}

if ( $depth > 0 ) {
$classes[] = '_c-menu__subitem';
} else {
$classes[] = '_c-menu__item';
}
return $classes;
}
}
2 changes: 2 additions & 0 deletions app/setup/loader.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@

new \Mimizuku\App\Setup\Init();
new \Mimizuku\App\Setup\Nav_Menus();
new \Mimizuku\App\Setup\Drawer_Nav();
new \Mimizuku\App\Setup\Global_Nav();
new \Mimizuku\App\Setup\Widgets();
new \Mimizuku\App\Setup\Body_Class();
new \Mimizuku\App\Setup\Post_Class();
Expand Down
86 changes: 0 additions & 86 deletions app/setup/nav-menus.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,6 @@ class Nav_Menus {

public function __construct() {
add_action( 'after_setup_theme', [ $this, 'register_nav_menus' ] );
add_filter( 'wp_nav_menu', [ $this, 'global_nav' ], 10, 2 );
add_filter( 'wp_nav_menu', [ $this, 'drawer_nav' ], 10, 2 );
add_filter( 'nav_menu_css_class', [ $this, 'global_nav_classes' ], 10, 4 );
add_filter( 'nav_menu_css_class', [ $this, 'drawer_nav_classes' ], 10, 4 );
}

/**
Expand All @@ -30,86 +26,4 @@ public function register_nav_menus() {
'footer-nav' => esc_html__( 'Footer Navigation', 'mimizuku' ),
] );
}

/**
* Sets up global navigation attributs
*
* @return void
* @see https://developer.wordpress.org/reference/functions/wp_nav_menu/
*/
public function global_nav( $nav_menu, $args ) {
if ( 'global-nav' !== $args->theme_location ) {
return $nav_menu;
}

$nav_menu = preg_replace(
'/menu-item-has-children(.*?)"/ms',
'menu-item-has-children$1" aria-haspopup="true"',
$nav_menu
);

$nav_menu = preg_replace(
'/<ul class="sub-menu">/ms',
'<ul class="_c-menu__submenu" data-c="menu__submenu" aria-hidden="true">',
$nav_menu
);

return $nav_menu;
}

/**
* Sets up drawer navigation attributs
*
* @return void
* @see https://developer.wordpress.org/reference/functions/wp_nav_menu/
*/
public function drawer_nav( $nav_menu, $args ) {
if ( 'drawer-nav' !== $args->theme_location ) {
return $nav_menu;
}

return preg_replace(
'/<ul class="sub-menu">/ms',
'<div class="_p-drawer__toggle" data-c="drawer__toggle" aria-expanded="false"><i class="fa fa-angle-right"></i></div><ul class="_p-drawer__submenu" data-c="drawer__submenu" aria-hidden="true">',
$nav_menu
);
}

/**
* Sets up global navigation classes
*
* @return void
* @see https://developer.wordpress.org/reference/classes/walker_nav_menu/
*/
public function global_nav_classes( $classes, $item, $args, $depth ) {
if ( 'global-nav' !== $args->theme_location ) {
return $classes;
}

if ( $depth > 0 ) {
$classes[] = '_c-menu__subitem';
} else {
$classes[] = '_c-menu__item';
}
return $classes;
}

/**
* Sets up drawer navigation classes
*
* @return void
* @see https://developer.wordpress.org/reference/classes/walker_nav_menu/
*/
public function drawer_nav_classes( $classes, $item, $args, $depth ) {
if ( 'drawer-nav' !== $args->theme_location ) {
return $classes;
}

if ( $depth > 0 ) {
$classes[] = '_p-drawer__subitem';
} else {
$classes[] = '_p-drawer__item';
}
return $classes;
}
}

0 comments on commit d248668

Please sign in to comment.