class Menu_tree{
public $menu_list;
public $menu_items;
function __construct() {
$menu = wp_get_nav_menu_object( ‘main-menu’ );
$this->menu_items = wp_get_nav_menu_items($menu->term_id);
$this->menu_list = array();
$this->getMenu();
}
function getMenu(){
foreach( $this->menu_items as $key => $current ) {
$current->children = array();
if($current->menu_item_parent == 0) // first parent
{
$this->menu_list[ $current->ID ] = $current;
}
else
{
if(isset($this->menu_list[ $current->menu_item_parent ])) // if child of first parent
{
$this->menu_list[ $current->menu_item_parent ]->children[ $current->ID ] = $current;
}
else
{
$this->current = $current;
$this->checkChildren(); // Setting up CHILDREN
}
}
}
return $this->menu_list;
}
function checkChildren($data = array()){
$last_arr = end($this->menu_list);
if(empty($data)) // FIRST CHILDREN LOOP
{
foreach( $last_arr->children as $key => $current ) {
if( $current->ID == $this->current->menu_item_parent )
{
$current->children[ $this->current->ID ] = $this->current;
break;
}
if(!empty($current->children))
{
$this->checkChildren($current->children);
}
}
}
else // RELOOP TO Children
{
foreach( $data as $key => $current ) {
if( $current->ID == $this->current->menu_item_parent )
{
$current->children[ $this->current->ID ] = $this->current;
break;
}
if(!empty($current->children))
{
$this->checkChildren($current->children);
}
}
}
}
}