<?php
/**
 * Widget Name: AIO Texts
 *
 * @package All In One Custom Post
 * @since All In One Custom Post 1.0.0 
 */

if ( ! class_exists( 'vr_widget_menu' ) ) {
	class vr_widget_menu extends WP_Widget {

		// Widget Construct
		public function __construct() {
			parent::__construct(
				false,
				_x( 'AIO Menu' , 'backend', PLUGIN_NAME),
				array(
					'description' => _x( 'Display Menu.', 'backend', PLUGIN_NAME),
					'classname'   => 'aio-widget-menu',
				)
			);
		}

		// Widget Init
		public function widget( $args, $instance ) {

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


			echo $args['before_widget'];
			if ( $title ) { echo $args['before_title'] . $title . $args['after_title']; }
			?>
				
				<?php 
					wp_nav_menu( array( 
						'menu' => $menu,
						'container' => false,
						'menu_class' =>  'aio-menu'
					));
				?>
		
			<?php
			echo $args['after_widget'];
		}

		// Form Update
		public function update( $new_instance, $old_instance ) {
			$instance = array();

			$instance['title']		= wp_strip_all_tags( $new_instance['title'] );
			$instance['menu'] 		= esc_attr( $new_instance['menu'] );

			return $instance;
		}

		// Widget Form
		public function form( $instance ) {

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

			<p>
				<label for="<?php echo $this->get_field_id( 'title' ); ?>"><?php _e( 'Title :', PLUGIN_NAME ); ?></label>
				<input id="<?php echo $this->get_field_id( 'title' ); ?>" name="<?php echo $this->get_field_name( 'title' ); ?>" value="<?php echo esc_attr( $title ); ?>" style="width:100%;" type="text" />
			</p>
			<p>
				<label for="<?php echo $this->get_field_id( 'menu' ); ?>"><?php _e( 'Menu :', PLUGIN_NAME ); ?></label>
				<select id="<?php echo $this->get_field_id( 'menu' ); ?>" name="<?php echo $this->get_field_name( 'menu' ); ?>" style="width:100%;">			
				<?php
$aio_menus = get_terms( 'nav_menu', array( 'hide_empty' => true ) );
foreach ( $aio_menus as $aio_menu ) {
$aio_menu_name = $aio_menu->name;
				?>
					<option value="<?php echo $aio_menu_name ?>" <?php if ( $menu == $aio_menu_name ) { echo 'selected="selected"'; } ?>><?php echo $aio_menu_name ?></option>
				<?php } ?>
				</select>
			</p>			
			<?php
		}

	}
	add_action( 'widgets_init', create_function( '', 'register_widget( "vr_widget_menu" );' ) );
}