Skip to content

Instantly share code, notes, and snippets.

@jlavoie13
Created January 7, 2016 16:10
Show Gist options
  • Save jlavoie13/9a3552208d0db15a979e to your computer and use it in GitHub Desktop.
Save jlavoie13/9a3552208d0db15a979e to your computer and use it in GitHub Desktop.
class scaffolding_walker_nav_menu extends Walker_Nav_Menu {
private $ulclasses = array();
private $liclasses = array();
// add classes to ul sub-menus
function start_lvl(&$output, $depth = 0, $args = Array()) {
// depth dependent classes
$indent = ( $depth > 0 ? str_repeat( "\t", $depth ) : '' ); // code indent
$display_depth = ( $depth + 1); // because it counts the first submenu as 0
$classes = array(
( $display_depth % 2 ? 'menu-odd' : 'menu-even' ),
( (in_array('toprow', $this->liclasses)) ? 'toprow-menu' : (in_array('bottomrow', $this->liclasses)) ? 'bottomrow-menu' : 'sub-menu' ),
'menu-depth-' . $display_depth
);
$class_names = implode( ' ', $classes );
// build html
if ($depth > 1) {
$output .= "\n" . $indent . '<ul class="' . $class_names . '"><li><a class="menu-back-button" title="Click to Go Back a Menu"><i class="fa fa-chevron-left"></i> Back</a></li>' . "\n";
} else {
$output .= "\n" . $indent . '<ul class="' . $class_names . '">' . "\n";
}
$this->ulclasses = $classes;
}
function start_el(&$output, $item, $depth = 0, $args = Array(), $id = 0) {
global $wp_query;
$indent = ( $depth ) ? str_repeat( "\t", $depth ) : '';
$class_names = $value = '';
//set <li> classes
$classes = empty( $item->classes ) ? array() : (array) $item->classes;
$classes[] = 'menu-item-' . $item->ID;
if ($args->has_children) { $classes[] = 'menu-has-children'; }
if (!$args->has_children) { $classes[] = 'menu-item-no-children'; }
//to use in start_lvl
$this->liclasses = $classes;
//combine the class array into a string
$class_names = join( ' ', apply_filters( 'nav_menu_css_class', array_filter( $classes ), $item, $args ) );
$class_names = ' class="' . esc_attr( $class_names ) . '"';
//set <li> id
$id = apply_filters( 'nav_menu_item_id', 'menu-item-'. $item->ID, $item, $args );
$id = strlen( $id ) ? ' id="' . esc_attr( $id ) . '"' : '';
//set outer <li> and it's attributes
$output .= $indent . '<li' . $id . $value . $class_names .'>';
$my_account_page = get_permalink( get_option('woocommerce_myaccount_page_id') );
$logout_url = wp_logout_url( home_url() );
//set <a> attributes
if (in_array('user-login', $classes)) {
if (is_user_logged_in()) {
$attributes = ' title="My Account"';
$attributes .= ! empty( $item->target ) ? ' target="' . esc_attr( $item->target ) . '"' : '';
$attributes .= ! empty( $item->xfn ) ? ' rel="' . esc_attr( $item->xfn ) . '"' : '';
$attributes .= ' href="' . $my_account_page . '"';
} else {
$attributes = ' title="Login"';
$attributes .= ! empty( $item->target ) ? ' target="' . esc_attr( $item->target ) . '"' : '';
$attributes .= ! empty( $item->xfn ) ? ' rel="' . esc_attr( $item->xfn ) . '"' : '';
$attributes .= ' href="' . $my_account_page . '"';
}
} else {
$attributes = ! empty( $item->attr_title ) ? ' title="' . esc_attr( $item->attr_title ) .'"' : ' title="' . esc_attr( strip_tags($item->title) ) . '"';
$attributes .= ! empty( $item->target ) ? ' target="' . esc_attr( $item->target ) . '"' : '';
$attributes .= ! empty( $item->xfn ) ? ' rel="' . esc_attr( $item->xfn ) . '"' : '';
$attributes .= ! empty( $item->url ) ? ' href="' . esc_attr( $item->url ) . '"' : '';
}
//Add menu button links to items with children
if ( $args->has_children ) {
$menu_pull_link = '<a class="menu-button" title="Click to Open Menu"><i class="fa fa-chevron-right"></i></a>';
} else {
$menu_pull_link = '';
}
//No Link Shown
if (in_array('no-link', $classes)) {
$item_output = NULL;
} elseif (in_array('user-login', $classes)) {
$item_output = $args->before;
$item_output .= '<a'. $attributes .'>';
if (is_user_logged_in()) {
$item_output .= $args->link_before . 'My Account' . $args->link_after;
} else {
$item_output .= $args->link_before . 'Login' . $args->link_after;
}
$item_output .= '</a>';
$item_output .= $menu_pull_link.$args->after;
//Social Media Link
} elseif (in_array('social-link', $classes)) {
$item_output = $args->before;
$item_output .= '<a'. $attributes .'>';
$item_output .= $args->link_before . $args->link_after;
$item_output .= '</a>';
} else {
$item_output = $args->before;
$item_output .= '<a'. $attributes .'>';
$item_output .= $args->link_before . apply_filters( 'the_title', $item->title, $item->ID ) . $args->link_after;
$item_output .= '</a>';
$item_output .= $menu_pull_link.$args->after;
}
$output .= apply_filters( 'walker_nav_menu_start_el', $item_output, $item, $depth, $args );
}
function end_el(&$output, $item, $depth=0, $args=array()) {
$output .= "</li>\n";
}
function display_element( $element, &$children_elements, $max_depth, $depth=0, $args, &$output ) {
//Set custom arg to tell if item has children
$id_field = $this->db_fields['id'];
if ( is_object( $args[0] ) ) {
$args[0]->has_children = ! empty( $children_elements[$element->$id_field] );
}
return parent::display_element( $element, $children_elements, $max_depth, $depth, $args, $output );
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment