<?php
/**
 * Footer Navigation Menu widget.
 *
 * @package    ShoBe
 * @author     Boby Hermez
 * @copyright  Copyright (c) 2025, Boby Hermez
 * @license    http://www.gnu.org/licenses/gpl-2.0.html
 * @since      1.0.0
 */
class shobe_menu_Footer_Widget extends WP_Widget {

	/**
	 * Sets up the widgets.
	 *
	 * @since 1.0.0
	 */
	function __construct() {

		// Set up the widget options.
		$shobe_widget_options = array(
			'classname'   => 'widget-navigation widget_navigation_menu',
			'description' => __( 'Display Custom Footer Navigation Menu.', 'shobe' )
		);

		// Create the widget.
		parent::__construct(
			'custom_nav_menu',                                   // $this->id_base
			__( '&raquo; ShoBe Footer Navigation Menu', 'shobe' ), // $this->name
			$shobe_widget_options                                      // $this->shobe_widget_options
		);


	}

	/**
	 * Outputs the widget based on the arguments input through the widget controls.
	 *
	 * @since 1.0.0
	 */
	function widget( $args, $instance ) {
		extract( $args );

		// Output the theme's $before_widget wrapper.
		echo $before_widget;        
        
        $instance =  wp_parse_args( $instance, array(
            'title'         => '',
            'select_menu'   => '',
        ) );
		
		echo $before_title;
		echo empty( $instance['title'] ) ? _e( 'Quick Links', 'shobe' ) : apply_filters( 'widget_title', $instance['title'] );
        echo $after_title;

		if (has_nav_menu($instance['select_menu'])) {
			wp_nav_menu( array(
				'theme_location'	=> $instance['select_menu'],
				'menu_class'		=> "list-unstyled footer-list mt-3",
				'menu_id'			=> "",           
				'walker' => new shobe_Walker_Nav_Menu()
			));
		}
		?>
        
        <?php 
		// Close the theme's widget wrapper.
		echo $after_widget;

	}

	/**
	 * Updates the widget control options for the particular instance of the widget.
	 *
	 * @since 1.0.0
	 */
	function update( $new_instance, $old_instance ) {

	 //$instance = array();
     $new_instance['title'] = ( ! empty( $new_instance['title'] ) ) ? sanitize_text_field( strip_tags( $new_instance['title'] ) ): '';
     return $new_instance;
     // return $instance;
	}


	/**
	 * Displays the widget control options in the Widgets admin screen.
	 *
	 * @since 1.0.0
	 */
	function form( $instance ) {

		// Default value.
		$instance =  wp_parse_args( $instance, array(
            'title'         => '',
            'select_menu'   => '',
        ) );

        $title = ! empty( $instance['title'] ) ? $instance['title'] : '';

 
	?>

		<p>
			<label for="<?php echo $this->get_field_id( 'title' ); ?>">
				<?php _e( 'Title', 'shobe' ); ?>
			</label>
			<input type="text" class="widefat" id="<?php echo $this->get_field_id( 'title' ); ?>" name="<?php echo $this->get_field_name( 'title' ); ?>" value="<?php echo esc_attr( $instance['title'] ); ?>" />
		</p>
        

        <p>
            <label for="<?php echo $this->get_field_id( 'select_menu' ); ?>"><?php esc_html_e( 'Select Nav Menu:', 'shobe' ); ?></label>
            <select name="<?php echo $this->get_field_name( 'select_menu' ); ?>">
                <?php
                $a = get_registered_nav_menus();
                foreach ( $a as $k => $v ) {
                    echo '<option value="'.$k.'" '.selected( $instance['select_menu'], $k, false ).' >'.$v.'</option>';
                } ?>
            </select>
        </p>

	<?php

	}

}

function register_shobe_menu_Footer_Widget() {
	register_widget( 'shobe_menu_Footer_Widget' );
}
add_action( 'widgets_init', 'register_shobe_menu_Footer_Widget' );


/**
 * Custom walker class.
 */
class shobe_Walker_Nav_Menu extends Walker_Nav_Menu {

	/**
	 * Starts the list before the elements are added.
	 *
	 * Adds classes to the unordered list sub-menus.
	 *
	 * @param string $output Passed by reference. Used to append additional content.
	 * @param int    $depth  Depth of menu item. Used for padding.
	 * @param array  $args   An array of arguments. @see wp_nav_menu()
	 */
	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(
			'sub-menu',
			( $display_depth % 2  ? 'menu-odd' : 'menu-even' ),
			( $display_depth >=2 ? 'sub-sub-menu' : '' ),
			'menu-depth-' . $display_depth
		);
		$class_names = implode( ' ', $classes );

		// Build HTML for output.
		$output .= "\n" . $indent . '<ul class="' . $class_names . '">' . "\n";
	}

	/**
	 * Start the element output.
	 *
	 * Adds main/sub-classes to the list items and links.
	 *
	 * @param string $output Passed by reference. Used to append additional content.
	 * @param object $item   Menu item data object.
	 * @param int    $depth  Depth of menu item. Used for padding.
	 * @param array  $args   An array of arguments. @see wp_nav_menu()
	 * @param int    $id     Current item ID.
	 */
	function start_el( &$output, $item, $depth = 0, $args = array(), $id = 0 ) {
		global $wp_query;
		$indent = ( $depth > 0 ? str_repeat( "\t", $depth ) : '' ); // code indent


		// Build HTML.
		$output .= $indent . '<li class="py-1"><i class="bi bi-chevron-right"></i>';
		// Link attributes.
		$attributes  = ! empty( $item->attr_title ) ? ' title="'  . esc_attr( $item->attr_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        ) .'"' : '';

		// Build HTML output and pass through the proper filter.
		$item_output = sprintf( '%1$s<a%2$s>%3$s%4$s%5$s</a>%6$s',
			$args->before,
			$attributes,
			$args->link_before,
			apply_filters( 'the_title', $item->title, $item->ID ),
			$args->link_after,
			$args->after
		);
		$output .= apply_filters( 'walker_nav_menu_start_el', $item_output, $item, $depth, $args );
	}
}