<?php

if( is_admin() ){
    /** Wyde Mega Menu Edit class **/
    include_once( WYDE_PLUGIN_DIR .'inc/megamenu/class-wyde-walker-mega-menu-edit.php' );
}

if ( ! class_exists( 'Wyde_Mega_Menu' ) ){

    class Wyde_Mega_Menu{
     
        function __construct() {
            add_filter( 'wp_setup_nav_menu_item', array( $this, 'add_custom_nav_fields' ) );
            add_action( 'wp_update_nav_menu_item', array( $this, 'update_custom_nav_fields'), 10, 3 );
            add_filter( 'wp_edit_nav_menu_walker', array( $this, 'edit_walker'), 10, 2 );
            add_action( 'admin_enqueue_scripts', array( $this, 'load_admin_scripts') );
        }

        function load_admin_scripts(){
            wp_enqueue_style('wyde-megamenu-style', WYDE_PLUGIN_URI. 'assets/css/wyde-megamenu.css', null, WYDE_VERSION);
        }

        function add_custom_nav_fields( $menu_item ) {
            $menu_item->megamenu = get_post_meta( $menu_item->ID, '_w_megamenu', true );
            return $menu_item;

        }

        function update_custom_nav_fields( $menu_id = '', $menu_item_db_id = '', $args = array() ) {

            if ( isset( $_REQUEST['menu-item-megamenu'] ) && is_array( $_REQUEST['menu-item-megamenu'] ) ) {
                if( isset( $_REQUEST['menu-item-megamenu'][$menu_item_db_id] ) ){
                    $megamenu = $_REQUEST['menu-item-megamenu'][$menu_item_db_id];
                    update_post_meta( $menu_item_db_id, '_w_megamenu', $megamenu );
                }
            }

        }

        function edit_walker( $walker = '', $menu_id = '' ) {            
	        return 'Wyde_Walker_MegaMenu_Edit';
	    }
    }

    new Wyde_Mega_Menu();

}