<?php
/**
 * Menu Item Widget Class
 *
 * Creates a 'Menu Item' widget which can be dragged on to a sidebar that simulates a navigation menu.
 * Widget controls allow the page link to be selected from a drop down menu together with a custom title.
 * Outputs a menu item which is a link wrapped in a list item, the same HTML as wp_list_pages().
 */

class Widget_Menu_Item extends WP_Widget {
	
	/**
	 * Set up the widget's unique name and options
	 */
	function Widget_Menu_Item() {
		$widget_ops = array( 'classname' => 'widget_menuitem', 'description' => __( 'A navigation item for a menu') );
		$this->WP_Widget( 'menuitem', __( 'Menu Item' ), $widget_ops );
	}
	
	/**
	 * Outputs the widget HTML
	 */
	function widget( $args, $instance ) {
		
		extract( $args );
		
		$title = apply_filters( 'widget_title', $instance['title'] );
		$page_id = apply_filters( 'widget_page_id', $instance['page_id'] );
		
		echo $before_widget;
		
		// Store CSS classes
		$classes[] = 'page_item';
		
		// Unique ID for each link
		if ( $page_id > 0 ) {
			$classes[] = 'page-item-' . $page_id;
		} else {
			$classes[] = 'page-item-front';
		}
		
		// Identify the current page
		if ( $page_id == -1 ) {
			if ( is_front_page() && !is_paged() ) {
				$classes[] = 'current_page_item';
			}
		} else {
			if ( get_option( 'show_on_front' ) == 'page' && get_option( 'page_for_posts' ) == $page_id ) {
				if ( is_home() ) {
					$classes[] = 'current_page_item';
				}
			} else {
				if ( is_page( $page_id ) ) {
					$classes[] = 'current_page_item';
				}
			}
		}
		
		$class = implode( ' ', $classes );
		
		// URL for menu item
		if ( $page_id == -1 ) {
			$url = get_bloginfo( 'url' );
		} else  {
			$url = get_page_link( $page_id );
		}
		
		// Output the menu item
		echo  '<li class="' . $class . '"><a href="' . $url . '" title="' . $title . '">' . $link_before . $title . $link_after . '</a></li>';
		
		echo $after_widget;
		
	}
	
	/**
	 * Updates the widget control options
	 */
	function update( $new_instance, $old_instance ) {
	
		$instance = $old_instance;
		
		// Set the widget title as the page title by default
		if ( empty( $new_instance['title'] ) ) {
			if ( $new_instance['page_id'] > 0 ) {
				$instance['title'] = strip_tags( get_the_title( $new_instance['page_id'] ) );
			} else {
				$instance['title'] = 'Front Page';
			}
		} else {
			$instance['title'] = strip_tags( $new_instance['title'] );
		}
		
		// Overwrite old options with new
		$instance['page_id'] = $new_instance['page_id'];
		
		return $instance;
		
	}
	
	/**
	 * Displays the widget control options
	 */
	function form( $instance ) {
		$title = esc_attr( $instance['title'] );
		$page_id = esc_attr( $instance['page_id'] );
		?>
			<style type="text/css">#pages-dropdown-menu select {width:100%;}</style>
			<p><label for="<?php echo $this->get_field_id( 'title' ); ?>"><?php _e( 'Title:' ); ?> <input class="widefat" id="<?php echo $this->get_field_id( 'title' ); ?>" name="<?php echo $this->get_field_name( 'title' ); ?>" type="text" value="<?php echo $title; ?>" /></label></p>
			<p id="pages-dropdown-menu"><?php wp_dropdown_pages( array( 'name' => $this->get_field_name( 'page_id' ), 'id' => $this->get_field_id( 'page_id' ), 'selected' => $page_id, 'show_option_none' => __( 'Front Page' ), 'option_none_value' => -1, 'sort_column' => 'post_title' ) ); ?></p>
		<?php
	}
	
}

register_widget( 'Widget_Menu_Item' );
?>