<?php
 
/*---------------------------------------
---------Icon Menu-----------------
-----------------------------------------*/
function next_custom_icon_menu($image = NULL,$onep = 0)
{
  $locations = get_nav_menu_locations();
  
  if(isset( $locations['primary'] )) { $menu = wp_get_nav_menu_object( $locations['primary']); }else { $menu = '';} 
  _wp_menu_item_classes_by_context( $menu_items );


  $return = '<div id="sidebar-nav" class="sq-side-menu hidden-xs">
    <ul>';

if($menu != '')
  {

    $menu_items = wp_get_nav_menu_items($menu->term_id);  

    $menunu = array();
    foreach((array)$menu_items as $key => $menu_item )
    {
      $menunu[ (int) $menu_item->db_id ] = $menu_item;
    }
    unset($menu_items);

  foreach ($menunu as $i => $men ){

    if($men->menu_item_parent == '0')
    {
      //Stripping out
      // $newlink   = strtolower(preg_replace('/[^a-zA-Z0-9]/s', '', $men->title)); 
      $post_finder = get_post($men->object_id);
      $page_slug = $post_finder->post_name;
      $newlink   = strtolower(preg_replace('/[^a-zA-Z]/s', '', $page_slug)); 


        //For Page Menu Items
        if( ( 'page' == $men->object ))
        {
          //Include to Onepage switch   
             $href =  '#'.$newlink;     
             $incl_onepage = get_post_meta($men->object_id,'next_include_onepage',true );
             if($incl_onepage == 1)
             {
               $href =  '#'.$newlink;
               $identifyClass = "is_onepage";
             }
             else
              {
                       $href = $men->url;
                       $identifyClass = "not_onepage";
              } 

          //Get the icon file for menu
            $menu_icon_page = get_post_meta($men->object_id,'next_menu_icon',true);
            if(!empty($menu_icon_page)):
              $ic          = wp_get_attachment_image_src($menu_icon_page,'full');
              $icmage_path = $ic[0];
            else:
              $icmage_path = '';
            endif; 

            //Prepare Image
            if($icmage_path !=''):
              $menu_side_icon = '<img alt="" title="" src="'.$icmage_path.'"/>';
            else:
              $menu_side_icon = '';
            endif;  

            //Pin-Menu Color Meta Option as style
            $menu_bg_color  = get_post_meta($men->object_id, 'next_pagemenu_color',true );
            //Set default color
            if($menu_bg_color!=''):$menu_bg_color = $menu_bg_color; else: $menu_bg_color = '#FD5253'; endif;
            //Begin Menu
            $return .= '<li  style="background:'.$menu_bg_color.'">';



        } 
        //For other menu items
        else 
        {
          $href =  $men->url;
          $identifyClass = "not_onepage";
          

          //Check the plugin active and continue

            //Get the custom field values
            $custom_menu_bg = $men->menubgcolor;
            if($custom_menu_bg!=''):$custom_menu_bg = $custom_menu_bg; else: $custom_menu_bg = '#555'; endif;
            //Begin Menu
            $return .= '<li  style="background:'.$custom_menu_bg.'">';

            $custom_menu_icon = $men->customicon;
            if($custom_menu_icon!=''): $menu_side_icon = '<img alt="" title="" src="'.$custom_menu_icon.'"/>'; else: $menu_side_icon = $men->title; endif;

        }

      //Final Menu Item Composition
      $return .= '
        <a class="scroll-link '.$identifyClass.'" id="'.$newlink.'-link" href="'.$href.'" data-soffset="0">
          <div class="first">
            <div class="arrow-right"></div>
            <div class="content">'.$menu_side_icon.'</div>
          </div>
          <div class="second">
            <div class="arrow-left"></div>
            <div class="content">'.$men->title.'</div>
          </div>
        </a>
      </li>
      <!-- Ending : Nav Block -->';

    }

  }
}
  else
  {
    $return .= '<li class="next-bg-one">
        <a class="scroll-link" id="home-link" href="'.site_url().'/wp-admin/nav-menus.php" data-soffset="0">
          <div class="first">
            <div class="arrow-right"></div>
            <div class="content"></div>
          </div>
          <div class="second">
            <div class="arrow-left"></div>
            <div class="content">Config Menu</div>
          </div>
        </a>
      </li>';  
    $return .= '' . "\n";  
  }

  unset($menunu); 
  $return .= '</ul>
    <!-- end sq-side-menu ul -->
  </div>
  <!-- end sq-side-menu -->' . "\n";

  echo $return;
}