<?php

/**
Menu Item Custom Fields
http://kucrut.org/
*/

if ( ! class_exists( 'Menu_Item_Custom_Fields' ) ) :
	/**
	* Menu Item Custom Fields Loader
	*/
	class Menu_Item_Custom_Fields {

		/**
		* Add filter
		*
		* @wp_hook action wp_loaded
		*/
		public static function load() {
			add_filter( 'wp_edit_nav_menu_walker', array( __CLASS__, '_filter_walker' ), 99 );
		}


		/**
		* Replace default menu editor walker with ours
		*
		* We don't actually replace the default walker. We're still using it and
		* only injecting some HTMLs.
		*
		* @since   0.1.0
		* @access  private
		* @wp_hook filter wp_edit_nav_menu_walker
		* @param   string $walker Walker class name
		* @return  string Walker class name
		*/
		public static function _filter_walker( $walker ) {
			$walker = 'Menu_Item_Custom_Fields_Walker';
			if ( ! class_exists( $walker ) ) {
				require_once dirname( __FILE__ ) . '/walker-nav-menu-edit.php';
			}

			return $walker;
		}
	}
	add_action( 'wp_loaded', array( 'Menu_Item_Custom_Fields', 'load' ), 9 );
endif; // class_exists( 'Menu_Item_Custom_Fields' )

class Menu_Item_Custom_Fields_Example {

	/**
	 * Holds our custom fields
	 *
	 * @var    array
	 * @access protected
	 * @since  Menu_Item_Custom_Fields_Example 0.2.0
	 */
	protected static $fields = array();


	/**
	 * Initialize plugin
	 */
	public static function init() {
		add_action( 'wp_nav_menu_item_custom_fields', array( __CLASS__, '_fields' ), 10, 4 );
		add_action( 'wp_update_nav_menu_item', array( __CLASS__, '_save' ), 10, 3 );
		add_filter( 'manage_nav-menus_columns', array( __CLASS__, '_columns' ), 99 );

		self::$fields = array(
			'menu-type' => __( 'Menu Type', 'gp_lang' ),
			'columns' => __( 'Columns', 'gp_lang' ),
			'content' => __( 'Content', 'gp_lang' ),
		);
	}


	/**
	 * Save custom field value
	 *
	 * @wp_hook action wp_update_nav_menu_item
	 *
	 * @param int   $menu_id         Nav menu ID
	 * @param int   $menu_item_db_id Menu item ID
	 * @param array $menu_item_args  Menu item data
	 */
	public static function _save( $menu_id, $menu_item_db_id, $menu_item_args ) {
		if ( defined( 'DOING_AJAX' ) && DOING_AJAX ) {
			return;
		}

		//check_admin_referer( 'update-nav_menu', 'update-nav-menu-nonce' );

		foreach ( self::$fields as $_key => $label ) {
			$key = sprintf( 'menu-item-%s', $_key );

			// Sanitize
			if ( ! empty( $_POST[ $key ][ $menu_item_db_id ] ) ) {
				// Do some checks here...
				$value = $_POST[ $key ][ $menu_item_db_id ];
			}
			else {
				$value = null;
			}

			// Update
			if ( ! is_null( $value ) ) {
				update_post_meta( $menu_item_db_id, $key, $value );
			}
			else {
				delete_post_meta( $menu_item_db_id, $key );
			}
		}
	}


	/**
	 * Print field
	 *
	 * @param object $item  Menu item data object.
	 * @param int    $depth  Depth of menu item. Used for padding.
	 * @param array  $args  Menu item args.
	 * @param int    $id    Nav menu ID.
	 *
	 * @return string Form fields
	 */
	public static function _fields( $id, $item, $depth, $args ) {
		foreach ( self::$fields as $_key => $label ) :
			$key   = sprintf( 'menu-item-%s', $_key );
			$id    = sprintf( 'edit-%s-%s', $key, $item->ID );
			$name  = sprintf( '%s[%s]', $key, $item->ID );
			$value = get_post_meta( $item->ID, $key, true );
			$class = sprintf( 'field-%s', $_key );
			?>
				<?php if ( ( $_key == 'menu-type' OR $_key == 'columns' ) && $depth < 1 ) { ?>
					<p class="description description-wide <?php echo esc_attr( $class ) ?>">
						<label for="<?php echo esc_attr( $id ); ?>"><?php echo esc_attr( $label ); ?></label>
						<br/><select id="<?php echo esc_attr( $id ); ?>" name="<?php echo esc_attr( $name ); ?>">
							<?php if ( $_key == 'menu-type' ) { ?>
								<option value="standard-menu"<?php if ( $value == 'standard-menu' ) { echo 'selected'; } ?>><?php _e( 'Standard Menu', 'gp_lang' ); ?></option>
								<option value="megamenu"<?php if ( $value == 'megamenu' ) { echo 'selected'; } ?>><?php _e( 'Mega Menu', 'gp_lang' ); ?></option>
								<?php if( $item->type == 'taxonomy' ) { ?>
									<option value="content-menu"<?php if ( $value == 'content-menu' ) { echo 'selected'; } ?>><?php _e( 'Content Menu', 'gp_lang' ); ?></option>
									<option value="tab-content-menu"<?php if ( $value == 'tab-content-menu' ) { echo 'selected'; } ?>><?php _e( 'Tab Content Menu', 'gp_lang' ); ?></option>
								<?php } ?>
							<?php } else { ?>
								<option value="columns-1"<?php if ( $value == 'columns-1' ) { echo 'selected'; } ?>><?php _e( '1 Column', 'gp_lang' ); ?></option>
								<option value="columns-2"<?php if ( $value == 'columns-2' ) { echo 'selected'; } ?>><?php _e( '2 Columns', 'gp_lang' ); ?></option>
								<option value="columns-3"<?php if ( $value == 'columns-3' ) { echo 'selected'; } ?>><?php _e( '3 Columns', 'gp_lang' ); ?></option>
								<option value="columns-4"<?php if ( $value == 'columns-4' ) { echo 'selected'; } ?>><?php _e( '4 Columns', 'gp_lang' ); ?></option>
								<option value="columns-5"<?php if ( $value == 'columns-5' ) { echo 'selected'; } ?>><?php _e( '5 Columns', 'gp_lang' ); ?></option>
							<?php } ?>	
						</select>		
						<?php if ( $_key == 'columns' ) { ?>		
							<br/><span class="description"><?php _e( 'Set the number of columns when using the mega menu option only.', 'gp_lang' ); ?></span>
						<?php } ?>	
					</p>
				<?php } ?>

				<?php if ( $_key == 'content' && $depth >= 1 ) { ?>
					<p class="description description-wide <?php echo esc_attr( $class ) ?>">
						<label for="<?php echo esc_attr( $id ); ?>"><?php echo esc_attr( $label ); ?></label>
						<br/><select id="<?php echo esc_attr( $id ); ?>" name="<?php echo esc_attr( $name ); ?>">
								<option value="menu-link"<?php if ( $value == 'menu-link' ) { echo 'selected'; } ?>><?php _e( 'Menu Link', 'gp_lang' ); ?></option>
								<option value="menu-text"<?php if ( $value == 'menu-text' ) { echo 'selected'; } ?>><?php _e( 'Menu Text', 'gp_lang' ); ?></option>
								<option value="menu-image"<?php if ( $value == 'menu-image' ) { echo 'selected'; } ?>><?php _e( 'Menu Image', 'gp_lang' ); ?></option>
						</select>		
						<br/><span class="description"><?php _e( 'Choose to replace the menu link with text or an image. Add your text to "Navigation Label" text field or image URL to the "URL" tetx field.', 'gp_lang' ); ?></span>	
					</p>
				<?php } ?>			
								
			<?php
		endforeach;
	}


	/**
	 * Add our fields to the screen options toggle
	 *
	 * @param array $columns Menu item columns
	 * @return array
	 */
	public static function _columns( $columns ) {
		$columns = array_merge( $columns, self::$fields );

		return $columns;
	}
}
Menu_Item_Custom_Fields_Example::init();