<?php
/**
 * Custom Navwalker for our theme.
 *
 * This class generates custom navwalker based on bootstrap 3.7 for our theme.
 * Add various required bootstrap classes and data attributes to default navigation bar
 * generated by WordPress.
 *
 * @package Friendly_Lite
 * @since 1.0.0
 * @version 1.0.0
 */

if ( ! class_exists( 'Friendly_Lite_Navwalker' ) ) {
	
	/**
	 * Friendly_Lite_Navwalker class.
	 * Custom Navwalker class for our theme. Output navigation menu with bootstrap 
	 * classes and data attributes.
	 * 
	 * @extends Walker_Nav_Menu
	 */ 
	class Friendly_Lite_Navwalker extends Walker_Nav_Menu {

		/**
		 * Starts a new level in navigation before the elements are added.
		 *
		 * @see Walker_Nav_Menu::start_lvl() 
		 *
		 * @param string   $output Used to append additional content (passed by reference).
		 * @param int      $depth  Depth of menu item. Used for padding.
		 * @param stdclass $args   An object of wp_nav_menu() arguments.
		 */
		public function start_lvl( &$output, $depth = 0, $args = array() ) {
			$indent = str_repeat( "\t", $depth );
			// If $depth is greater than 0 it means we are at sub menu level.
			$submenu = ( $depth >= 0 ) ? 'sub-menu' : '';
			// Add bootstrap dropdown menu to the ul element.
			$output .= "\n$indent<ul class=\"$submenu depth_$depth\">\n";
		}

		/**
		 * Starts or add new element in navigation.
		 * 
		 * @see Walker_Nav_Menu::start_el()
		 * 
		 * @param string   $output Used to append additional content (passed by reference).
		 * @param WP_Post  $item   Menu item data object.
		 * @param int      $depth  Depth of menu item. Used for padding.
		 * @param stdClass $args   An object of wp_nav_menu() arguments.
		 * @param int      $id     Current item ID.
		 */
		public function start_el( &$output, $item, $depth = 0, $args = array(), $id = 0 ) {
			$li_attributes = '';
			$class_names   = '';
			$values        = '';

			$indent = ( $depth ) ? str_repeat( "\t", $depth ) : '';

			$classes[] = empty( $item->classes ) ? '' : join( ' ', $item->classes );

			// $classes[] = ( $args->walker->has_children ) ? 'dropdown' : '';
			$classes[] = ( $item->current || $item->current_item_ancestor ) ? 'active' : '';
			$classes[] = 'menu-item-' . $item->ID;

			$class_names = join( ' ', apply_filters( 'nav_menu_css_class', array_filter( $classes ), $item, $args ) );
			$class_names = 'class="' . esc_attr( $class_names ) . '"';

			$id = apply_filters( 'nav_menu_item_id', 'menu-id-' . $item->ID, $item, $args );
			$id = 'id="' . esc_attr( $id ) . '"';

			$output .= $indent . '<li ' . $id . $values . $class_names . $li_attributes . '>';

			$a_attributes  = ! empty( $item->attr_title ) ? 'title="' . $item->attr_title . '"' : '';
			$a_attributes .= ! empty( $item->target ) ? 'target="' . $item->target . '"' : '';
			$a_attributes .= ! empty( $item->xfn ) ? 'rel="' . $item->xfn . '"' : '';
			$a_attributes .= ! empty( $item->url ) ? 'href="' . $item->url . '"' : '';

			$a_attributes .= ( $args->walker->has_children ) ? 'class="submenu-parent"' : '';

			$menu_item  = $args->before;
			$menu_item .= '<a ' . $a_attributes . '>';
			$menu_item .= $args->link_before . apply_filters( 'the_title', $item->title, $item->ID ) . $args->link_after;

			$menu_item .= '</a>';
			$menu_item .= $args->after;

			$output .= apply_filters( 'walker_nav_menu_start_el', $menu_item, $item, $depth, $args );
		}
	}
}
