<?php

/**

 * Sidebar off-canvas menu walker.

 *

 * @package storecraft

 * @since 1.0

 */



if ( ! class_exists( 'storecraft_Walker_Sidebar_Menu' ) ) {



	/**

	 * Walker for the sidebar navigation menu.

	 */

	class storecraft_Walker_Sidebar_Menu extends Walker_Nav_Menu {



		/**

		 * Starts the list before the elements are added.

		 *

		 * @param string   $output Used to append additional content.

		 * @param int      $depth  Depth of menu item.

		 * @param stdClass $args   An object of wp_nav_menu() arguments.

		 */

		public function start_lvl( &$output, $depth = 0, $args = null ) {

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

			$output .= "\n$indent<ul class=\"sub-menu hbs-sidebar-sub-menu\">\n";

		}



		/**

		 * Ends the list of after the elements are added.

		 *

		 * @param string   $output Used to append additional content.

		 * @param int      $depth  Depth of menu item.

		 * @param stdClass $args   An object of wp_nav_menu() arguments.

		 */

		public function end_lvl( &$output, $depth = 0, $args = null ) {

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

			$output .= "$indent</ul>\n";

		}



		/**

		 * Starts the element output.

		 *

		 * @param string   $output Used to append additional content.

		 * @param WP_Post  $item   Menu item data object.

		 * @param int      $depth  Depth of menu item.

		 * @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 = null, $id = 0 ) {

			$classes   = empty( $item->classes ) ? array() : (array) $item->classes;

			$classes[] = 'menu-item-' . $item->ID;



			$class_names = implode( ' ', array_map( 'sanitize_html_class', array_filter( $classes ) ) );

			$class_names = $class_names ? ' class="' . esc_attr( $class_names ) . '"' : '';



			$output .= '<li' . $class_names . '>';



			$atts           = array();

			$atts['title']  = ! empty( $item->attr_title ) ? $item->attr_title : '';

			$atts['target'] = ! empty( $item->target ) ? $item->target : '';

			$atts['rel']    = ! empty( $item->xfn ) ? $item->xfn : '';

			$atts['href']   = ! empty( $item->url ) ? $item->url : '';



			$atts = apply_filters( 'nav_menu_link_attributes', $atts, $item, $args, $depth );



			$attributes = '';

			foreach ( $atts as $attr => $value ) {

				if ( ! empty( $value ) ) {

					$value       = ( 'href' === $attr ) ? esc_url( $value ) : esc_attr( $value );

					$attributes .= ' ' . $attr . '="' . $value . '"';

				}

			}



			$title         = apply_filters( 'the_title', $item->title, $item->ID );

			$has_children  = in_array( 'menu-item-has-children', $classes, true );

			$item_output   = $args->before;

			$item_output  .= '<a' . $attributes . '>';



			if ( 0 === (int) $depth ) {

				$icon_class    = storecraft_get_sidebar_menu_icon( $item );

				$item_output  .= '<span class="hbs-sidebar-menu-icon" aria-hidden="true"><i class="' . esc_attr( $icon_class ) . '"></i></span>';

				$item_output  .= '<span class="hbs-sidebar-menu-text">' . esc_html( $title ) . '</span>';



				if ( $has_children ) {

					$item_output .= '<span class="hbs-sidebar-menu-chevron" aria-hidden="true"><i class="fas fa-chevron-down"></i></span>';

				}

			} else {

				$item_output .= '<span class="hbs-sidebar-menu-text hbs-sidebar-submenu-text">' . esc_html( $title ) . '</span>';

			}



			$item_output .= '</a>';

			$item_output .= $args->after;



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

		}

	}

}


