<?php
class dataInstallMenus{

    public $structure;
    public $custom_menu_items;
    public $tax_menu_items;
    public $post_menu_items;

    public $navMenus = array();
    
    function addToWP( $menu_name = '' ){
        if( !empty( $menu_name ) ){
            foreach ($this->structure as $index=>$value) {
                if( $menu_name == $value['menu_name'] ){
                    $this->insertMenuItem($index);
                }
            }
        }else{
            foreach ($this->structure as $index=>$value) {
                $this->insertMenuItem($index);
            }
        }
    }

    function insertMenuItem( $index ){
        $menuitem = $this->structure[$index];
        
        $menutitle = $menuitem[ 'menu_item_title' ];

        $menu_id = $this->getNavMenuID( $menuitem['menu_name']);

        $menu_item_id = wp_insert_post( array( 'post_type' => 'nav_menu_item'), TRUE );
        $this->structure[$index]['ID'] = $menu_item_id;

        $menu_settings = array();
        
        $parent = $menuitem['menu_item_parent'];
        if( $parent ){
            $parent = $this->structure[ $menuitem['menu_item_parent'] ] ['ID'];
        }
        $menu_settings[ 'menu-item-parent-id' ] = $parent;

        $menu_settings[ 'menu-item-title' ] = $menutitle;
        $menu_settings[ 'menu-item-description' ] = $menuitem[ 'menu_item_description' ];

        if( isSet( $this->custom_menu_items[$index] ) ){
            $menu_settings[ 'menu-item-type' ] = 'custom';
            $menu_settings[ 'menu-item-object' ] = 'custom';
            $menu_settings[ 'menu-item-url' ] = $this->custom_menu_items[$index];
        }else if( isSet( $this->tax_menu_items[$index] ) ){
            $menu_settings[ 'menu-item-type' ] = 'taxonomy';
            $menu_settings[ 'menu-item-object' ] = 'category';
            $menu_settings[ 'menu-item-object-id' ] = ffGetByTitle::term( $this->tax_menu_items[$index] );

        }else if( isSet( $this->post_menu_items[$index] ) ){
                $menu_settings[ 'menu-item-type' ] = 'post_type';
                $menu_settings[ 'menu-item-object' ]    = ffGetByTitle::post_type( $this->post_menu_items[$index] );
                $menu_settings[ 'menu-item-object-id' ] = ffGetByTitle::post( $this->post_menu_items[$index] );
        }

        wp_update_nav_menu_item( $menu_id, $menu_item_id, $menu_settings );
        //echo "<hr>wp_update_nav_menu_item( $menu_id, $menu_item_id, <pre>";print_r( $menu_settings ); echo "</pre><hr>";

        if( empty($menu_id) ){
            echo "wp_update_nav_menu_item( $menu_id, $menu_item_id, ";print_r($menu_settings);echo " );<br><hr><br>";
            echo "<p>Unable to find menu";
            exit;
        }
    }
    
    function getNavMenuID( $title ){
        if( isSet( $this->navMenus[ $title ] ) ){
            return $this->navMenus[ $title ];
        }

        $menu_obj = get_term_by( 'name', $title, 'nav_menu' );

        if( $menu_obj ){
            return $this->navMenus[ $title ] = $menu_obj->term_id;
        }

        return $this->navMenus[ $title ] = wp_create_nav_menu( $title );
    }

    function __construct(){

        $this->structure = array(

              78 => array( 'menu_item_title' => 'Blog ','menu_item_description' => '','order' => '1','menu_item_parent' => '0','menu_name' => 'blog cat widget'),
              79 => array( 'menu_item_title' => 'Portfolio ','menu_item_description' => '','order' => '2','menu_item_parent' => '0','menu_name' => 'blog cat widget'),
              80 => array( 'menu_item_title' => 'Portfolio Fullwidth ','menu_item_description' => '','order' => '3','menu_item_parent' => '0','menu_name' => 'blog cat widget'),
              
              
              51 => array( 'menu_item_title' => 'Home','menu_item_description' => '','order' => '1','menu_item_parent' => '0','menu_name' => 'Navigation'),
              52 => array( 'menu_item_title' => 'About ','menu_item_description' => '','order' => '2','menu_item_parent' => '0','menu_name' => 'Navigation'),
              60 => array( 'menu_item_title' => 'functions','menu_item_description' => '','order' => '9','menu_item_parent' => '0','menu_name' => 'Navigation'),
              76 => array( 'menu_item_title' => 'Blog ','menu_item_description' => '','order' => '18','menu_item_parent' => '0','menu_name' => 'Navigation'),
              77 => array( 'menu_item_title' => 'Contact ','menu_item_description' => '','order' => '19','menu_item_parent' => '0','menu_name' => 'Navigation'),

              57 => array( 'menu_item_title' => 'dropdown test','menu_item_description' => '','order' => '3','menu_item_parent' => '52','menu_name' => 'Navigation'),
              53 => array( 'menu_item_title' => 'dropdown test','menu_item_description' => '','order' => '4','menu_item_parent' => '52','menu_name' => 'Navigation'),
              58 => array( 'menu_item_title' => 'dropdown test','menu_item_description' => '','order' => '8','menu_item_parent' => '52','menu_name' => 'Navigation'),

              54 => array( 'menu_item_title' => 'dropdown test','menu_item_description' => '','order' => '5','menu_item_parent' => '53','menu_name' => 'Navigation'),
              55 => array( 'menu_item_title' => 'dropdown test','menu_item_description' => '','order' => '6','menu_item_parent' => '53','menu_name' => 'Navigation'),
              56 => array( 'menu_item_title' => 'dropdown test','menu_item_description' => '','order' => '7','menu_item_parent' => '53','menu_name' => 'Navigation'),
              
              61 => array( 'menu_item_title' => 'Fullwidth Page ','menu_item_description' => '','order' => '10','menu_item_parent' => '60','menu_name' => 'Navigation'),
              64 => array( 'menu_item_title' => 'HTML Test Page ','menu_item_description' => '','order' => '11','menu_item_parent' => '60','menu_name' => 'Navigation'),
              63 => array( 'menu_item_title' => 'Gallery ','menu_item_description' => '','order' => '12','menu_item_parent' => '60','menu_name' => 'Navigation'),
              72 => array( 'menu_item_title' => 'Portfolio ','menu_item_description' => '','order' => '15','menu_item_parent' => '60','menu_name' => 'Navigation'),

              65 => array( 'menu_item_title' => 'Gallery w/ sidebar ','menu_item_description' => '','order' => '13','menu_item_parent' => '63','menu_name' => 'Navigation'),
              62 => array( 'menu_item_title' => 'Gallery Fullwidth ','menu_item_description' => '','order' => '14','menu_item_parent' => '63','menu_name' => 'Navigation'),

              74 => array( 'menu_item_title' => 'Portfolio w/ sidebar ','menu_item_description' => '','order' => '16','menu_item_parent' => '72','menu_name' => 'Navigation'),
              73 => array( 'menu_item_title' => 'Portfolio Fullwidth ','menu_item_description' => '','order' => '17','menu_item_parent' => '72','menu_name' => 'Navigation')

        );
        
        $this->custom_menu_items = array(

              53 => '#',
              54 => '#',
              55 => '#',
              56 => '#',
              57 => '#',
              58 => '#',
              60 => '#',

        );

        $this->tax_menu_items = array(
              51 => 'Blog',
              72 => 'Portfolio',
              73 => 'Portfolio Fullwidth',
              74 => 'Portfolio',
              76 => 'Blog',
              78 => 'Blog',
              79 => 'Portfolio',
              80 => 'Portfolio Fullwidth',
        );
        
        $this->post_menu_items = array(
              52 => 'About',
              61 => 'Fullwidth Page',
              62 => 'Gallery Fullwidth',
              63 => 'Gallery',
              64 => 'HTML Test Page',
              65 => 'Gallery',
              77 => 'Contact',
        );
    }
}