<?php
/*
	SHRK Custom mega menu
*/

// Exit if accessed directly
if ( ! defined( 'ABSPATH' ) ) {
	exit;
}






class shrk_custom_mega_menu {

	/*--------------------------------------------*
	 * Constructor
	 *--------------------------------------------*/

	/**
	 * Initializes the plugin by setting localization, filters, and administration functions.
	 */
	function __construct() {

		// add custom menu fields to menu
		add_filter( 'wp_setup_nav_menu_item', array( $this, 'shrk_menu_add_custom_nav_fields' ) );

		// save menu custom fields
		add_action( 'wp_update_nav_menu_item', array( $this, 'shrk_menu_update_custom_nav_fields'), 10, 3 );
		
		// edit menu walker
		add_filter( 'wp_edit_nav_menu_walker', array( $this, 'shrk_menu_edit_walker'), 10, 2 );

	} // end constructor
	
	
	/**
	 * Add custom fields to $item nav object
	 * in order to be used in custom Walker
	 *
	 * @access      public
	 * @since       1.0 
	 * @return      void
	*/
	function shrk_menu_add_custom_nav_fields( $menu_item ) {
	
		$menu_item->mmenu_active = get_post_meta( $menu_item->ID, '_shrk_menu_mmenu_active', true );
		$menu_item->mmenu_fullwidth = get_post_meta( $menu_item->ID, '_shrk_menu_mmenu_fullwidth', true );
		return $menu_item;	
	}
	
	/**
	 * Save menu custom fields
	 *
	 * @access      public
	 * @since       1.0 
	 * @return      void
	*/
	function shrk_menu_update_custom_nav_fields( $menu_id, $menu_item_db_id, $args ) {
	
		$mmenu_active_value = $mmenu_fullwidth_value = false;

		//Checkbox
		if ( isset($_REQUEST['menu-item-mmenu_active'][$menu_item_db_id]) ) {
			$mmenu_active_value = $_REQUEST['menu-item-mmenu_active'][$menu_item_db_id];
		}
		update_post_meta( $menu_item_db_id, '_shrk_menu_mmenu_active', $mmenu_active_value ? 1 : 0 );

		//Checkbox
		if ( isset($_REQUEST['menu-item-mmenu_fullwidth'][$menu_item_db_id]) ) {
			$mmenu_fullwidth_value = $_REQUEST['menu-item-mmenu_fullwidth'][$menu_item_db_id];
		}	
		update_post_meta( $menu_item_db_id, '_shrk_menu_mmenu_fullwidth', $mmenu_fullwidth_value ? 1 : 0 );	
	}
	
	/**
	 * Define new Walker edit
	 *
	 * @access      public
	 * @since       1.0 
	 * @return      void
	*/
	function shrk_menu_edit_walker( $walker, $menu_id = null ) {
		return 'Walker_Nav_Menu_Edit_Custom';
	}
}

if( !function_exists('shrk_fallback_menu') ) {
	function shrk_fallback_menu() {
		?>
		<div class="navbar-collapse navbar-responsive-collapse hidden-xs">
			<ul id="main-menu" class="nav navbar-nav navbar-right shrk_fallback_menu">
				<li class="menu-item menu-item-type-post_type menu-item-object-page"><a href="<?php echo esc_url( home_url() ); ?>"><?php esc_html_e('Home', 'ux-shop'); ?></a> </li>
				<li class="menu-item menu-item-type-custom menu-item-object-custom dropdown"><a href="#" data-toggle="dropdown" data-target="#" class="dropdown-toggle"><?php esc_html_e('Pages', 'ux-shop'); ?> <span class="caret"></span></a> 
					<ul class="sub-menu">
						<?php  
						$args = array(
							'post_type'    => 'page',
							'sort_column'  => 'menu_order, post_title',
							'title_li'     => '', 
							);
						wp_list_pages( $args );
						?>
					</ul>
				</li>
			</ul>
		</div>
		<?php
	}
}

if( !function_exists('shrk_no_menu_message') ) {
	function shrk_no_menu_message() {
		echo '<div>' . esc_html__('Please define your menu in Apperance > Menus', 'ux-shop') . '</div>';
	}
}

if( !function_exists('shrk_no_menu_no_output') ) {
	function shrk_no_menu_no_output() {
		// Silence
	}
}

// instantiate plugin's class
$GLOBALS['shrk_custom_menu'] = new shrk_custom_mega_menu();

include_once( 'edit_custom_walker.php' );
include_once( 'custom_walker.php' );

?>