2ea55b3d by Chris Boden

Added some wp function and Menu component

1 parent 2c940cfa
1 <?php
2 namespace Tz\WordPress\Tools\Menu;
3 use Exception, Countable, Iterator;
4
5 function get_nav_menu($location) {
6 $locations = get_nav_menu_locations();
7 if (!isset($locations[$location])) {
8 throw new Exception("{$location} is not a defined menu location");
9 }
10 $menu_location = _get_nav_menu_object($locations[$location])->slug;
11 $menu = _get_nav_menu_items($menu_location);
12 _wp_menu_item_classes_by_context($menu);
13
14 return $menu;
15 }
16
17
18 class HierarchicalMenu implements Countable, Iterator {
19 private $contents = Array();
20 private $current = 0;
21
22 private $id_lookup = Array();
23 private $target_lookup = Array();
24
25 public function __construct(Array $menu, $parent = 0) {
26 $i = 0;
27 $last_id = -1;
28
29 foreach ($menu as $key => $item) {
30 $item->_children = false;
31
32 if ($item->menu_item_parent == $parent) {
33 $this->contents[$i] = $item;
34 $this->id_lookup[$item->ID] = $i;
35 $this->target_lookup[$item->object_id] = $i;
36
37 $i++;
38 $last_id = $item->ID;
39 } elseif ($item->menu_item_parent == $last_id) {
40 $this->contents[$this->id_lookup[$last_id]]->_children = true;
41 }
42 }
43
44 foreach ($this->contents as $key => $item) {
45 if (true === $item->_children) {
46 $item->HierarchicalMenu = new HierarchicalMenu($menu, $item->ID);
47 }
48 unset($this->contents[$key]->_children);
49 }
50 }
51
52 public function count() {
53 return count($this->contents);
54 }
55
56 public function current() {
57 return $this->contents[$this->current];
58 }
59
60 public function key() {
61 return $this->current;
62 }
63
64 public function next() {
65 $this->current++;
66 }
67
68 public function rewind() {
69 $this->rewind = 0;
70 }
71
72 public function valid() {
73 return (isset($this->contents[$this->current]) ? true : false);
74 }
75
76 public function getItemByID($id) {
77 return $this->id_lookup[$id] ?: false;
78 }
79
80 public function getItemByTarget($id) {
81 return $this->target_lookup[$id] ?: false;
82 }
83 }
84 ?>
...\ No newline at end of file ...\ No newline at end of file
...@@ -180,4 +180,19 @@ function _nav_menu() { ...@@ -180,4 +180,19 @@ function _nav_menu() {
180 $params = func_get_args(); 180 $params = func_get_args();
181 return call_user_func_array('wp' . __FUNCTION__, $params); 181 return call_user_func_array('wp' . __FUNCTION__, $params);
182 } 182 }
183 ?> 183
184 function _get_nav_menu_object() {
185 $params = func_get_args();
186 return call_user_func_array('wp' . __FUNCTION__, $params);
187 }
188
189 function _get_nav_menu_items() {
190 $params = func_get_args();
191 return call_user_func_array('wp' . __FUNCTION__, $params);
192 }
193
194 function _reset_postdata() {
195 $params = func_get_args();
196 return call_user_func_array('wp' . __FUNCTION__, $params);
197 }
198 ?>
...\ No newline at end of file ...\ No newline at end of file
......