/**
  * Populate a lookup array with default values (templates) from $menu and $submenu.
  * Used later to merge a custom menu with the native WordPress menu structure.
  *
  * @param array $menu
  * @param array $submenu
  * @return array An array of menu templates and their default values.
  */
 function build_templates($menu, $submenu)
 {
     $templates = array();
     $name_lookup = array();
     foreach ($menu as $pos => $item) {
         $item = ameMenuItem::fromWpItem($item, $pos);
         if ($item['separator']) {
             continue;
         }
         $name = $this->sanitize_menu_title($item['menu_title']);
         $name_lookup[$item['file']] = $name;
         $templates[ameMenuItem::template_id($item)] = array('name' => $name, 'used' => false, 'defaults' => $item);
     }
     foreach ($submenu as $parent => $items) {
         //Skip sub-menus attached to non-existent parents. This should theoretically never happen,
         //but a buggy plugin can cause such a situation.
         if (!isset($name_lookup[$parent])) {
             continue;
         }
         foreach ($items as $pos => $item) {
             $item = ameMenuItem::fromWpItem($item, $pos, $parent);
             $templates[ameMenuItem::template_id($item)] = array('name' => $name_lookup[$parent] . ' -> ' . $this->sanitize_menu_title($item['menu_title']), 'used' => false, 'defaults' => $item);
         }
     }
     return $templates;
 }
 /**
  * Add a menu item as a template.
  *
  * @param array $wpItem
  * @param int $position
  * @param string|null $parent
  */
 private function addItem($wpItem, $position, $parent = null)
 {
     $item = ameMenuItem::fromWpItem($wpItem, $position, $parent);
     //Skip separators.
     if ($item['separator']) {
         $this->wasPreviousItemSeparated = true;
         return;
     }
     //Skip blacklisted menus.
     if (isset($item['url'], $this->blacklist[$item['url']])) {
         return;
     }
     $name = $this->sanitizeMenuTitle($item['menu_title']);
     if ($parent === null) {
         $this->parentNames[$item['file']] = $name;
     } else {
         $name = $this->parentNames[$parent] . ' -> ' . $name;
     }
     $templateId = ameMenuItem::template_id($item);
     $this->templates[$templateId] = array('name' => $name, 'used' => false, 'defaults' => $item);
     //Remember the relative order of menu items. It's a bit like a linked list.
     $this->templateOrder[$templateId] = array('previous_item' => $this->previousItemId, 'was_previous_item_separated' => $this->wasPreviousItemSeparated);
     $this->previousItemId = $templateId;
     $this->wasPreviousItemSeparated = false;
 }
예제 #3
0
 /**
  * Convert the WP menu structure to the internal representation. All properties set as defaults.
  *
  * @param array $menu
  * @param array $submenu
  * @param array $blacklist
  * @return array Menu in the internal tree format.
  */
 public static function wp2tree($menu, $submenu, $blacklist = array())
 {
     $tree = array();
     foreach ($menu as $pos => $item) {
         $tree_item = ameMenuItem::blank_menu();
         $tree_item['defaults'] = ameMenuItem::fromWpItem($item, $pos);
         $tree_item['separator'] = $tree_item['defaults']['separator'];
         //Attach sub-menu items
         $parent = $tree_item['defaults']['file'];
         if (isset($submenu[$parent])) {
             foreach ($submenu[$parent] as $position => $subitem) {
                 $defaults = ameMenuItem::fromWpItem($subitem, $position, $parent);
                 //Skip blacklisted items.
                 if (isset($defaults['url'], $blacklist[$defaults['url']])) {
                     continue;
                 }
                 $tree_item['items'][] = array_merge(ameMenuItem::blank_menu(), array('defaults' => $defaults));
             }
         }
         //Skip blacklisted top level menus (only if they have no submenus).
         if (empty($tree_item['items']) && isset($tree_item['defaults']['url'], $blacklist[$tree_item['defaults']['url']])) {
             continue;
         }
         $tree[$parent] = $tree_item;
     }
     $tree = self::sort_menu_tree($tree);
     return $tree;
 }
예제 #4
0
 /**
  * Convert the WP menu structure to the internal representation. All properties set as defaults.
  *
  * @param array $menu
  * @param array $submenu
  * @return array Menu in the internal tree format.
  */
 public static function wp2tree($menu, $submenu)
 {
     $tree = array();
     foreach ($menu as $pos => $item) {
         $tree_item = ameMenuItem::blank_menu();
         $tree_item['defaults'] = ameMenuItem::fromWpItem($item, $pos);
         $tree_item['separator'] = $tree_item['defaults']['separator'];
         //Attach sub-menu items
         $parent = $tree_item['defaults']['file'];
         if (isset($submenu[$parent])) {
             foreach ($submenu[$parent] as $position => $subitem) {
                 $tree_item['items'][] = array_merge(ameMenuItem::blank_menu(), array('defaults' => ameMenuItem::fromWpItem($subitem, $position, $parent)));
             }
         }
         $tree[$parent] = $tree_item;
     }
     $tree = self::sort_menu_tree($tree);
     return $tree;
 }