Completed
Push — master ( 7000bd...9da1e7 )
by Fulvio
03:23
created
includes/wp-api-menus-v1.php 1 patch
Spacing   +66 added lines, -66 removed lines patch added patch discarded remove patch
@@ -5,11 +5,11 @@  discard block
 block discarded – undo
5 5
  * @package WP_API_Menus
6 6
  */
7 7
 
8
-if ( ! defined( 'ABSPATH' ) ) {
8
+if ( ! defined('ABSPATH')) {
9 9
 	exit; // Exit if accessed directly
10 10
 }
11 11
 
12
-if ( ! class_exists( 'WP_JSON_Menus' ) ) :
12
+if ( ! class_exists('WP_JSON_Menus')) :
13 13
 
14 14
 
15 15
 	/**
@@ -30,23 +30,23 @@  discard block
 block discarded – undo
30 30
 		 * @param  array $routes Existing routes
31 31
 		 * @return array Modified routes
32 32
 		 */
33
-		public function register_routes( $routes ) {
33
+		public function register_routes($routes) {
34 34
 
35 35
 			// all registered menus
36 36
 			$routes['/menus'] = array(
37
-				array( array( $this, 'get_menus' ), WP_JSON_Server::READABLE ),
37
+				array(array($this, 'get_menus'), WP_JSON_Server::READABLE),
38 38
 			);
39 39
 			// a specific menu
40 40
 			$routes['/menus/(?P<id>\d+)'] = array(
41
-				array( array( $this, 'get_menu' ), WP_JSON_Server::READABLE ),
41
+				array(array($this, 'get_menu'), WP_JSON_Server::READABLE),
42 42
 			);
43 43
 			// all registered menu locations
44 44
 			$routes['/menu-locations'] = array(
45
-				array( array( $this, 'get_menu_locations' ), WP_JSON_Server::READABLE ),
45
+				array(array($this, 'get_menu_locations'), WP_JSON_Server::READABLE),
46 46
 			);
47 47
 			// menu for given location
48 48
 			$routes['/menu-locations/(?P<location>[a-zA-Z0-9_-]+)'] = array(
49
-				array( array( $this, 'get_menu_location' ), WP_JSON_Server::READABLE ),
49
+				array(array($this, 'get_menu_location'), WP_JSON_Server::READABLE),
50 50
 			);
51 51
 
52 52
 			return $routes;
@@ -61,26 +61,26 @@  discard block
 block discarded – undo
61 61
 		 */
62 62
 		public static function get_menus() {
63 63
 
64
-			$json_url = get_json_url() . '/menus/';
64
+			$json_url = get_json_url().'/menus/';
65 65
 			$wp_menus = wp_get_nav_menus();
66 66
 
67 67
 			$i = 0;
68 68
 			$json_menus = array();
69
-			foreach ( $wp_menus as $wp_menu ) :
69
+			foreach ($wp_menus as $wp_menu) :
70 70
 
71 71
 				$menu = (array) $wp_menu;
72 72
 
73
-				$json_menus[ $i ]                 = $menu;
74
-				$json_menus[ $i ]['ID']           = $menu['term_id'];
75
-				$json_menus[ $i ]['name']         = $menu['name'];
76
-				$json_menus[ $i ]['slug']         = $menu['slug'];
77
-				$json_menus[ $i ]['description']  = $menu['description'];
78
-				$json_menus[ $i ]['count']        = $menu['count'];
73
+				$json_menus[$i]                 = $menu;
74
+				$json_menus[$i]['ID']           = $menu['term_id'];
75
+				$json_menus[$i]['name']         = $menu['name'];
76
+				$json_menus[$i]['slug']         = $menu['slug'];
77
+				$json_menus[$i]['description']  = $menu['description'];
78
+				$json_menus[$i]['count']        = $menu['count'];
79 79
 
80
-				$json_menus[ $i ]['meta']['links']['collection'] = $json_url;
81
-				$json_menus[ $i ]['meta']['links']['self']       = $json_url . $menu['term_id'];
80
+				$json_menus[$i]['meta']['links']['collection'] = $json_url;
81
+				$json_menus[$i]['meta']['links']['self']       = $json_url.$menu['term_id'];
82 82
 
83
-				$i ++;
83
+				$i++;
84 84
 			endforeach;
85 85
 
86 86
 			return $json_menus;
@@ -94,31 +94,31 @@  discard block
 block discarded – undo
94 94
 		 * @param  int   $id ID of the menu
95 95
 		 * @return array Menu data
96 96
 		 */
97
-		public function get_menu( $id ) {
97
+		public function get_menu($id) {
98 98
 
99
-			$json_url       = get_json_url() . '/menus/';
100
-			$wp_menu_object = $id ? wp_get_nav_menu_object( $id ) : array();
101
-			$wp_menu_items  = $id ? wp_get_nav_menu_items( $id ) : array();
99
+			$json_url       = get_json_url().'/menus/';
100
+			$wp_menu_object = $id ? wp_get_nav_menu_object($id) : array();
101
+			$wp_menu_items  = $id ? wp_get_nav_menu_items($id) : array();
102 102
 
103 103
 			$json_menu = array();
104 104
 
105
-			if ( $wp_menu_object ) :
105
+			if ($wp_menu_object) :
106 106
 
107 107
 				$menu = (array) $wp_menu_object;
108
-				$json_menu['ID']            = abs( $menu['term_id'] );
108
+				$json_menu['ID']            = abs($menu['term_id']);
109 109
 				$json_menu['name']          = $menu['name'];
110 110
 				$json_menu['slug']          = $menu['slug'];
111 111
 				$json_menu['description']   = $menu['description'];
112
-				$json_menu['count']         = abs( $menu['count'] );
112
+				$json_menu['count']         = abs($menu['count']);
113 113
 
114 114
 				$json_menu_items = array();
115
-				foreach ( $wp_menu_items as $item_object ) {
116
-					$json_menu_items[] = $this->format_menu_item( $item_object );
115
+				foreach ($wp_menu_items as $item_object) {
116
+					$json_menu_items[] = $this->format_menu_item($item_object);
117 117
 				}
118 118
 
119 119
 				$json_menu['items']                       = $json_menu_items;
120 120
 				$json_menu['meta']['links']['collection'] = $json_url;
121
-				$json_menu['meta']['links']['self']       = $json_url . $id;
121
+				$json_menu['meta']['links']['self']       = $json_url.$id;
122 122
 
123 123
 			endif;
124 124
 
@@ -136,22 +136,22 @@  discard block
 block discarded – undo
136 136
 
137 137
 			$locations        = get_nav_menu_locations();
138 138
 			$registered_menus = get_registered_nav_menus();
139
-			$json_url         = get_json_url() . '/menu-locations/';
139
+			$json_url         = get_json_url().'/menu-locations/';
140 140
 			$json_menus       = array();
141 141
 
142
-			if ( $locations && $registered_menus ) :
142
+			if ($locations && $registered_menus) :
143 143
 
144
-				foreach ( $registered_menus as $slug => $label ) :
144
+				foreach ($registered_menus as $slug => $label) :
145 145
 
146 146
 					// Sanity check
147
-					if ( ! isset( $locations[ $slug ] ) ) {
147
+					if ( ! isset($locations[$slug])) {
148 148
 						continue;
149 149
 					}
150 150
 
151
-					$json_menus[ $slug ]['ID']                          = $locations[ $slug ];
152
-					$json_menus[ $slug ]['label']                       = $label;
153
-					$json_menus[ $slug ]['meta']['links']['collection'] = $json_url;
154
-					$json_menus[ $slug ]['meta']['links']['self']       = $json_url . $slug;
151
+					$json_menus[$slug]['ID']                          = $locations[$slug];
152
+					$json_menus[$slug]['label']                       = $label;
153
+					$json_menus[$slug]['meta']['links']['collection'] = $json_url;
154
+					$json_menus[$slug]['meta']['links']['self']       = $json_url.$slug;
155 155
 
156 156
 				endforeach;
157 157
 
@@ -168,40 +168,40 @@  discard block
 block discarded – undo
168 168
 		 * @param  string $location The theme location menu name
169 169
 		 * @return array The menu for the corresponding location
170 170
 		 */
171
-		public function get_menu_location( $location ) {
171
+		public function get_menu_location($location) {
172 172
 
173 173
 			$locations = get_nav_menu_locations();
174
-			if ( ! isset( $locations[ $location ] ) ) {
174
+			if ( ! isset($locations[$location])) {
175 175
 				return array();
176 176
 			}
177 177
 
178
-			$wp_menu = wp_get_nav_menu_object( $locations[ $location ] );
179
-			$menu_items = wp_get_nav_menu_items( $wp_menu->term_id );
178
+			$wp_menu = wp_get_nav_menu_object($locations[$location]);
179
+			$menu_items = wp_get_nav_menu_items($wp_menu->term_id);
180 180
 
181 181
 			$sorted_menu_items = $top_level_menu_items = $menu_items_with_children = array();
182 182
 
183
-			foreach ( (array) $menu_items as $menu_item ) {
184
-				$sorted_menu_items[ $menu_item->menu_order ] = $menu_item;
183
+			foreach ((array) $menu_items as $menu_item) {
184
+				$sorted_menu_items[$menu_item->menu_order] = $menu_item;
185 185
 			}
186
-			foreach ( $sorted_menu_items as $menu_item ) {
187
-				if ( (int) $menu_item->menu_item_parent !== 0 ) {
188
-					$menu_items_with_children[ $menu_item->menu_item_parent ] = true;
186
+			foreach ($sorted_menu_items as $menu_item) {
187
+				if ((int) $menu_item->menu_item_parent !== 0) {
188
+					$menu_items_with_children[$menu_item->menu_item_parent] = true;
189 189
 				} else {
190 190
 					$top_level_menu_items[] = $menu_item;
191 191
 				}
192 192
 			}
193 193
 
194 194
 			$menu = array();
195
-			while ( $sorted_menu_items ) :
195
+			while ($sorted_menu_items) :
196 196
 
197 197
 				$i = 0;
198
-				foreach ( $top_level_menu_items as $top_item ) :
198
+				foreach ($top_level_menu_items as $top_item) :
199 199
 
200
-					$menu[ $i ] = $this->format_menu_item( $top_item, false );
201
-					if ( isset( $menu_items_with_children[ $top_item->ID ] ) ) {
202
-						$menu[ $i ]['children'] = $this->get_nav_menu_item_children( $top_item->ID, $menu_items, false );
200
+					$menu[$i] = $this->format_menu_item($top_item, false);
201
+					if (isset($menu_items_with_children[$top_item->ID])) {
202
+						$menu[$i]['children'] = $this->get_nav_menu_item_children($top_item->ID, $menu_items, false);
203 203
 					} else {
204
-						$menu[ $i ]['children'] = array();
204
+						$menu[$i]['children'] = array();
205 205
 					}
206 206
 
207 207
 					$i++;
@@ -224,19 +224,19 @@  discard block
 block discarded – undo
224 224
 		 * @param  bool    $depth          gives all children or direct children only
225 225
 		 * @return array   returns filtered array of nav_menu_items
226 226
 		 */
227
-		public function get_nav_menu_item_children( $parent_id, $nav_menu_items, $depth = true ) {
227
+		public function get_nav_menu_item_children($parent_id, $nav_menu_items, $depth = true) {
228 228
 
229 229
 			$nav_menu_item_list = array();
230 230
 
231
-			foreach ( (array) $nav_menu_items as $nav_menu_item ) :
231
+			foreach ((array) $nav_menu_items as $nav_menu_item) :
232 232
 
233
-				if ( $nav_menu_item->menu_item_parent == $parent_id ) :
233
+				if ($nav_menu_item->menu_item_parent == $parent_id) :
234 234
 
235
-					$nav_menu_item_list[] = $this->format_menu_item( $nav_menu_item, true, $nav_menu_items );
235
+					$nav_menu_item_list[] = $this->format_menu_item($nav_menu_item, true, $nav_menu_items);
236 236
 
237
-					if ( $depth ) {
238
-						if ( $children = $this->get_nav_menu_item_children( $nav_menu_item->ID, $nav_menu_items ) ) {
239
-							$nav_menu_item_list = array_merge( $nav_menu_item_list, $children );
237
+					if ($depth) {
238
+						if ($children = $this->get_nav_menu_item_children($nav_menu_item->ID, $nav_menu_items)) {
239
+							$nav_menu_item_list = array_merge($nav_menu_item_list, $children);
240 240
 						}
241 241
 					}
242 242
 
@@ -257,32 +257,32 @@  discard block
 block discarded – undo
257 257
 		 * @param   array           $menu       the menu the item belongs to (used when $children is set to true)
258 258
 		 * @return  array   a formatted menu item for JSON
259 259
 		 */
260
-		public function format_menu_item( $menu_item, $children = false, $menu = array() ) {
260
+		public function format_menu_item($menu_item, $children = false, $menu = array()) {
261 261
 
262 262
 			$item = (array) $menu_item;
263 263
 
264 264
 			$menu_item = array(
265
-				'ID'          => abs( $item['ID'] ),
265
+				'ID'          => abs($item['ID']),
266 266
 				'order'       => (int) $item['menu_order'],
267
-				'parent'      => abs( $item['menu_item_parent'] ),
267
+				'parent'      => abs($item['menu_item_parent']),
268 268
 				'title'       => $item['title'],
269 269
 				'url'         => $item['url'],
270 270
 				'attr'        => $item['attr_title'],
271 271
 				'target'      => $item['target'],
272
-				'classes'     => implode( ' ', $item['classes'] ),
272
+				'classes'     => implode(' ', $item['classes']),
273 273
 				'xfn'         => $item['xfn'],
274 274
 				'description' => $item['description'],
275
-				'object_id'   => abs( $item['object_id'] ),
275
+				'object_id'   => abs($item['object_id']),
276 276
 				'object'      => $item['object'],
277 277
 				'type'        => $item['type'],
278 278
 				'type_label'  => $item['type_label'],
279 279
 			);
280 280
 
281
-			if ( $children === true && ! empty( $menu ) ) {
282
-				$menu_item['children'] = $this->get_nav_menu_item_children( $item['ID'], $menu );
281
+			if ($children === true && ! empty($menu)) {
282
+				$menu_item['children'] = $this->get_nav_menu_item_children($item['ID'], $menu);
283 283
 			}
284 284
 
285
-			return apply_filters( 'json_menus_format_menu_item', $menu_item );
285
+			return apply_filters('json_menus_format_menu_item', $menu_item);
286 286
 		}
287 287
 
288 288
 
Please login to merge, or discard this patch.
includes/wp-api-menus-v2.php 2 patches
Indentation   +352 added lines, -352 removed lines patch added patch discarded remove patch
@@ -6,381 +6,381 @@
 block discarded – undo
6 6
  */
7 7
 
8 8
 if ( ! defined( 'ABSPATH' ) ) {
9
-    exit; // Exit if accessed directly
9
+	exit; // Exit if accessed directly
10 10
 }
11 11
 
12 12
 if ( ! class_exists( 'WP_REST_Menus' ) ) :
13 13
 
14 14
 
15
-    /**
16
-     * WP REST Menus class.
17
-     *
18
-     * WP API Menus support for WP API v2.
19
-     *
20
-     * @package WP_API_Menus
21
-     * @since 1.2.0
22
-     */
23
-    class WP_REST_Menus {
24
-
25
-
26
-	    /**
27
-	     * Get WP API namespace.
28
-	     *
29
-	     * @since 1.2.0
30
-	     * @return string
31
-	     */
32
-        public static function get_api_namespace() {
33
-            return 'wp/v2';
34
-        }
35
-
36
-
37
-	    /**
38
-	     * Get WP API Menus namespace.
39
-	     *
40
-	     * @since 1.2.1
41
-	     * @return string
42
-	     */
43
-	    public static function get_plugin_namespace() {
44
-		    return 'wp-api-menus/v2';
45
-	    }
46
-
47
-
48
-        /**
49
-         * Register menu routes for WP API v2.
50
-         *
51
-         * @since  1.2.0
52
-         * @return array
53
-         */
54
-        public function register_routes() {
55
-
56
-            register_rest_route( self::get_plugin_namespace(), '/menus', array(
57
-                array(
58
-                    'methods'  => WP_REST_Server::READABLE,
59
-                    'callback' => array( $this, 'get_menus' ),
60
-                )
61
-            ) );
62
-
63
-            register_rest_route( self::get_plugin_namespace(), '/menus/(?P<id>\d+)', array(
64
-                array(
65
-                    'methods'  => WP_REST_Server::READABLE,
66
-                    'callback' => array( $this, 'get_menu' ),
67
-                    'args'     => array(
68
-                        'context' => array(
69
-                        'default' => 'view',
70
-                        ),
71
-                    ),
72
-                )
73
-            ) );
74
-
75
-            register_rest_route( self::get_plugin_namespace(), '/menu-locations', array(
76
-                array(
77
-                    'methods'  => WP_REST_Server::READABLE,
78
-                    'callback' => array( $this, 'get_menu_locations' ),
79
-                )
80
-            ) );
81
-
82
-            register_rest_route( self::get_plugin_namespace(), '/menu-locations/(?P<location>[a-zA-Z0-9_-]+)', array(
83
-                array(
84
-                    'methods'  => WP_REST_Server::READABLE,
85
-                    'callback' => array( $this, 'get_menu_location' ),
86
-                )
87
-            ) );
88
-
89
-        }
90
-
91
-
92
-        /**
93
-         * Get menus.
94
-         *
95
-         * @since  1.2.0
96
-         * @return array All registered menus
97
-         */
98
-        public static function get_menus() {
99
-
100
-            $rest_url = trailingslashit( get_rest_url() . self::get_plugin_namespace() . '/menus/' );
101
-            $wp_menus = wp_get_nav_menus();
102
-
103
-            $i = 0;
104
-            $rest_menus = array();
105
-            foreach ( $wp_menus as $wp_menu ) :
106
-
107
-                $menu = (array) $wp_menu;
108
-
109
-                $rest_menus[ $i ]                = $menu;
110
-                $rest_menus[ $i ]['ID']          = $menu['term_id'];
111
-                $rest_menus[ $i ]['name']        = $menu['name'];
112
-                $rest_menus[ $i ]['slug']        = $menu['slug'];
113
-                $rest_menus[ $i ]['description'] = $menu['description'];
114
-                $rest_menus[ $i ]['count']       = $menu['count'];
115
-
116
-                $rest_menus[ $i ]['meta']['links']['collection'] = $rest_url;
117
-                $rest_menus[ $i ]['meta']['links']['self']       = $rest_url . $menu['term_id'];
118
-
119
-                $i ++;
120
-            endforeach;
121
-
122
-            return apply_filters( 'rest_menus_format_menus', $rest_menus );
123
-        }
124
-
125
-
126
-        /**
127
-         * Get a menu.
128
-         *
129
-         * @since  1.2.0
130
-         * @param  $request
131
-         * @return array Menu data
132
-         */
133
-        public function get_menu( $request ) {
134
-
135
-            $id             = (int) $request['id'];
136
-            $rest_url       = get_rest_url() . self::get_api_namespace() . '/menus/';
137
-            $wp_menu_object = $id ? wp_get_nav_menu_object( $id ) : array();
138
-            $wp_menu_items  = $id ? wp_get_nav_menu_items( $id ) : array();
139
-
140
-            $rest_menu = array();
141
-
142
-            if ( $wp_menu_object ) :
143
-
144
-                $menu = (array) $wp_menu_object;
145
-                $rest_menu['ID']          = abs( $menu['term_id'] );
146
-                $rest_menu['name']        = $menu['name'];
147
-                $rest_menu['slug']        = $menu['slug'];
148
-                $rest_menu['description'] = $menu['description'];
149
-                $rest_menu['count']       = abs( $menu['count'] );
150
-
151
-                $rest_menu_items = array();
152
-                foreach ( $wp_menu_items as $item_object ) {
153
-	                $rest_menu_items[] = $this->format_menu_item( $item_object );
154
-                }
155
-
156
-                $rest_menu_items = $this->nested_menu_items($rest_menu_items, 0);
157
-
158
-                $rest_menu['items']                       = $rest_menu_items;
159
-                $rest_menu['meta']['links']['collection'] = $rest_url;
160
-                $rest_menu['meta']['links']['self']       = $rest_url . $id;
161
-
162
-            endif;
163
-
164
-            return apply_filters( 'rest_menus_format_menu', $rest_menu );
165
-        }
166
-
167
-
168
-        /**
169
-         * Handle nested menu items.
170
-         *
171
-         * Given a flat array of menu items, split them into parent/child items
172
-         * and recurse over them to return children nested in their parent.
173
-         *
174
-         * @since  1.2.0
175
-         * @param  $menu_items
176
-         * @param  $parent
177
-         * @return array
178
-         */
179
-        private function nested_menu_items( &$menu_items, $parent = null ) {
180
-
181
-            $parents = array();
182
-            $children = array();
183
-
184
-            // Separate menu_items into parents & children.
185
-            array_map( function( $i ) use ( $parent, &$children, &$parents ){
186
-                if ( $i['ID'] != $parent && $i['parent'] == $parent ) {
187
-                    $parents[] = $i;
188
-                } else {
189
-                    $children[] = $i;
190
-                }
191
-            }, $menu_items );
192
-
193
-            foreach ( $parents as &$parent ) {
194
-
195
-                if ( $this->has_children( $children, $parent['ID'] ) ) {
196
-                    $parent['children'] = $this->nested_menu_items( $children, $parent['ID'] );
197
-                }
198
-            }
199
-
200
-            return $parents;
201
-        }
202
-
203
-
204
-        /**
205
-         * Check if a collection of menu items contains an item that is the parent id of 'id'.
206
-         *
207
-         * @since  1.2.0
208
-         * @param  array $items
209
-         * @param  int $id
210
-         * @return array
211
-         */
212
-        private function has_children( $items, $id ) {
213
-            return array_filter( $items, function( $i ) use ( $id ) {
214
-                return $i['parent'] == $id;
215
-            } );
216
-        }
217
-
218
-
219
-        /**
220
-         * Get menu locations.
221
-         *
222
-         * @since 1.2.0
223
-         * @param  $request
224
-         * @return array All registered menus locations
225
-         */
226
-        public static function get_menu_locations( $request ) {
227
-
228
-            $locations        = get_nav_menu_locations();
229
-            $registered_menus = get_registered_nav_menus();
230
-	        $rest_url         = get_rest_url() . self::get_api_namespace() . '/menu-locations/';
231
-            $rest_menus       = array();
232
-
233
-            if ( $locations && $registered_menus ) :
234
-
235
-                foreach ( $registered_menus as $slug => $label ) :
236
-
237
-	                // Sanity check
238
-	                if ( ! isset( $locations[ $slug ] ) ) {
239
-		                continue;
240
-	                }
15
+	/**
16
+	 * WP REST Menus class.
17
+	 *
18
+	 * WP API Menus support for WP API v2.
19
+	 *
20
+	 * @package WP_API_Menus
21
+	 * @since 1.2.0
22
+	 */
23
+	class WP_REST_Menus {
24
+
25
+
26
+		/**
27
+		 * Get WP API namespace.
28
+		 *
29
+		 * @since 1.2.0
30
+		 * @return string
31
+		 */
32
+		public static function get_api_namespace() {
33
+			return 'wp/v2';
34
+		}
35
+
36
+
37
+		/**
38
+		 * Get WP API Menus namespace.
39
+		 *
40
+		 * @since 1.2.1
41
+		 * @return string
42
+		 */
43
+		public static function get_plugin_namespace() {
44
+			return 'wp-api-menus/v2';
45
+		}
46
+
47
+
48
+		/**
49
+		 * Register menu routes for WP API v2.
50
+		 *
51
+		 * @since  1.2.0
52
+		 * @return array
53
+		 */
54
+		public function register_routes() {
55
+
56
+			register_rest_route( self::get_plugin_namespace(), '/menus', array(
57
+				array(
58
+					'methods'  => WP_REST_Server::READABLE,
59
+					'callback' => array( $this, 'get_menus' ),
60
+				)
61
+			) );
62
+
63
+			register_rest_route( self::get_plugin_namespace(), '/menus/(?P<id>\d+)', array(
64
+				array(
65
+					'methods'  => WP_REST_Server::READABLE,
66
+					'callback' => array( $this, 'get_menu' ),
67
+					'args'     => array(
68
+						'context' => array(
69
+						'default' => 'view',
70
+						),
71
+					),
72
+				)
73
+			) );
74
+
75
+			register_rest_route( self::get_plugin_namespace(), '/menu-locations', array(
76
+				array(
77
+					'methods'  => WP_REST_Server::READABLE,
78
+					'callback' => array( $this, 'get_menu_locations' ),
79
+				)
80
+			) );
81
+
82
+			register_rest_route( self::get_plugin_namespace(), '/menu-locations/(?P<location>[a-zA-Z0-9_-]+)', array(
83
+				array(
84
+					'methods'  => WP_REST_Server::READABLE,
85
+					'callback' => array( $this, 'get_menu_location' ),
86
+				)
87
+			) );
88
+
89
+		}
90
+
91
+
92
+		/**
93
+		 * Get menus.
94
+		 *
95
+		 * @since  1.2.0
96
+		 * @return array All registered menus
97
+		 */
98
+		public static function get_menus() {
99
+
100
+			$rest_url = trailingslashit( get_rest_url() . self::get_plugin_namespace() . '/menus/' );
101
+			$wp_menus = wp_get_nav_menus();
102
+
103
+			$i = 0;
104
+			$rest_menus = array();
105
+			foreach ( $wp_menus as $wp_menu ) :
106
+
107
+				$menu = (array) $wp_menu;
108
+
109
+				$rest_menus[ $i ]                = $menu;
110
+				$rest_menus[ $i ]['ID']          = $menu['term_id'];
111
+				$rest_menus[ $i ]['name']        = $menu['name'];
112
+				$rest_menus[ $i ]['slug']        = $menu['slug'];
113
+				$rest_menus[ $i ]['description'] = $menu['description'];
114
+				$rest_menus[ $i ]['count']       = $menu['count'];
115
+
116
+				$rest_menus[ $i ]['meta']['links']['collection'] = $rest_url;
117
+				$rest_menus[ $i ]['meta']['links']['self']       = $rest_url . $menu['term_id'];
118
+
119
+				$i ++;
120
+			endforeach;
121
+
122
+			return apply_filters( 'rest_menus_format_menus', $rest_menus );
123
+		}
124
+
125
+
126
+		/**
127
+		 * Get a menu.
128
+		 *
129
+		 * @since  1.2.0
130
+		 * @param  $request
131
+		 * @return array Menu data
132
+		 */
133
+		public function get_menu( $request ) {
134
+
135
+			$id             = (int) $request['id'];
136
+			$rest_url       = get_rest_url() . self::get_api_namespace() . '/menus/';
137
+			$wp_menu_object = $id ? wp_get_nav_menu_object( $id ) : array();
138
+			$wp_menu_items  = $id ? wp_get_nav_menu_items( $id ) : array();
139
+
140
+			$rest_menu = array();
141
+
142
+			if ( $wp_menu_object ) :
143
+
144
+				$menu = (array) $wp_menu_object;
145
+				$rest_menu['ID']          = abs( $menu['term_id'] );
146
+				$rest_menu['name']        = $menu['name'];
147
+				$rest_menu['slug']        = $menu['slug'];
148
+				$rest_menu['description'] = $menu['description'];
149
+				$rest_menu['count']       = abs( $menu['count'] );
150
+
151
+				$rest_menu_items = array();
152
+				foreach ( $wp_menu_items as $item_object ) {
153
+					$rest_menu_items[] = $this->format_menu_item( $item_object );
154
+				}
155
+
156
+				$rest_menu_items = $this->nested_menu_items($rest_menu_items, 0);
157
+
158
+				$rest_menu['items']                       = $rest_menu_items;
159
+				$rest_menu['meta']['links']['collection'] = $rest_url;
160
+				$rest_menu['meta']['links']['self']       = $rest_url . $id;
161
+
162
+			endif;
163
+
164
+			return apply_filters( 'rest_menus_format_menu', $rest_menu );
165
+		}
166
+
167
+
168
+		/**
169
+		 * Handle nested menu items.
170
+		 *
171
+		 * Given a flat array of menu items, split them into parent/child items
172
+		 * and recurse over them to return children nested in their parent.
173
+		 *
174
+		 * @since  1.2.0
175
+		 * @param  $menu_items
176
+		 * @param  $parent
177
+		 * @return array
178
+		 */
179
+		private function nested_menu_items( &$menu_items, $parent = null ) {
180
+
181
+			$parents = array();
182
+			$children = array();
183
+
184
+			// Separate menu_items into parents & children.
185
+			array_map( function( $i ) use ( $parent, &$children, &$parents ){
186
+				if ( $i['ID'] != $parent && $i['parent'] == $parent ) {
187
+					$parents[] = $i;
188
+				} else {
189
+					$children[] = $i;
190
+				}
191
+			}, $menu_items );
192
+
193
+			foreach ( $parents as &$parent ) {
194
+
195
+				if ( $this->has_children( $children, $parent['ID'] ) ) {
196
+					$parent['children'] = $this->nested_menu_items( $children, $parent['ID'] );
197
+				}
198
+			}
199
+
200
+			return $parents;
201
+		}
202
+
203
+
204
+		/**
205
+		 * Check if a collection of menu items contains an item that is the parent id of 'id'.
206
+		 *
207
+		 * @since  1.2.0
208
+		 * @param  array $items
209
+		 * @param  int $id
210
+		 * @return array
211
+		 */
212
+		private function has_children( $items, $id ) {
213
+			return array_filter( $items, function( $i ) use ( $id ) {
214
+				return $i['parent'] == $id;
215
+			} );
216
+		}
217
+
218
+
219
+		/**
220
+		 * Get menu locations.
221
+		 *
222
+		 * @since 1.2.0
223
+		 * @param  $request
224
+		 * @return array All registered menus locations
225
+		 */
226
+		public static function get_menu_locations( $request ) {
227
+
228
+			$locations        = get_nav_menu_locations();
229
+			$registered_menus = get_registered_nav_menus();
230
+			$rest_url         = get_rest_url() . self::get_api_namespace() . '/menu-locations/';
231
+			$rest_menus       = array();
232
+
233
+			if ( $locations && $registered_menus ) :
234
+
235
+				foreach ( $registered_menus as $slug => $label ) :
236
+
237
+					// Sanity check
238
+					if ( ! isset( $locations[ $slug ] ) ) {
239
+						continue;
240
+					}
241 241
 
242
-	                $rest_menus[ $slug ]['ID']                          = $locations[ $slug ];
243
-                    $rest_menus[ $slug ]['label']                       = $label;
244
-                    $rest_menus[ $slug ]['meta']['links']['collection'] = $rest_url;
245
-                    $rest_menus[ $slug ]['meta']['links']['self']       = $rest_url . $slug;
242
+					$rest_menus[ $slug ]['ID']                          = $locations[ $slug ];
243
+					$rest_menus[ $slug ]['label']                       = $label;
244
+					$rest_menus[ $slug ]['meta']['links']['collection'] = $rest_url;
245
+					$rest_menus[ $slug ]['meta']['links']['self']       = $rest_url . $slug;
246 246
 
247
-                endforeach;
247
+				endforeach;
248 248
 
249
-            endif;
249
+			endif;
250 250
 
251
-            return $rest_menus;
252
-        }
253
-
254
-
255
-        /**
256
-         * Get menu for location.
257
-         *
258
-         * @since 1.2.0
259
-         * @param  $request
260
-         * @return array The menu for the corresponding location
261
-         */
262
-        public function get_menu_location( $request ) {
263
-
264
-            $params     = $request->get_params();
265
-            $location   = $params['location'];
266
-            $locations  = get_nav_menu_locations();
267
-
268
-            if ( ! isset( $locations[ $location ] ) ) {
269
-	            return array();
270
-            }
271
-
272
-            $wp_menu = wp_get_nav_menu_object( $locations[ $location ] );
273
-            $menu_items = wp_get_nav_menu_items( $wp_menu->term_id );
274
-            $sorted_menu_items = $top_level_menu_items = $menu_items_with_children = array();
275
-
276
-            foreach ( (array) $menu_items as $menu_item ) {
277
-	            $sorted_menu_items[ $menu_item->menu_order ] = $menu_item;
278
-            }
279
-            foreach ( $sorted_menu_items as $menu_item ) {
280
-	            if ( (int) $menu_item->menu_item_parent !== 0 ) {
281
-		            $menu_items_with_children[ $menu_item->menu_item_parent ] = true;
282
-	            } else {
283
-		            $top_level_menu_items[] = $menu_item;
284
-	            }
285
-            }
286
-
287
-            $menu = array();
288
-
289
-            while ( $sorted_menu_items ) :
290
-
291
-                $i = 0;
292
-                foreach ( $top_level_menu_items as $top_item ) :
293
-
294
-                    $menu[ $i ] = $this->format_menu_item( $top_item, false );
295
-                    if ( isset( $menu_items_with_children[ $top_item->ID ] ) ) {
296
-	                    $menu[ $i ]['children'] = $this->get_nav_menu_item_children( $top_item->ID, $menu_items, false );
297
-                    } else {
298
-	                    $menu[ $i ]['children'] = array();
299
-                    }
300
-
301
-                    $i++;
302
-                endforeach;
303
-
304
-                break;
305
-
306
-            endwhile;
307
-
308
-            return $menu;
309
-        }
310
-
311
-
312
-        /**
313
-         * Returns all child nav_menu_items under a specific parent.
314
-         *
315
-         * @since   1.2.0
316
-         * @param int   $parent_id      The parent nav_menu_item ID
317
-         * @param array $nav_menu_items Navigation menu items
318
-         * @param bool  $depth          Gives all children or direct children only
319
-         * @return  array   returns filtered array of nav_menu_items
320
-         */
321
-        public function get_nav_menu_item_children( $parent_id, $nav_menu_items, $depth = true ) {
251
+			return $rest_menus;
252
+		}
253
+
254
+
255
+		/**
256
+		 * Get menu for location.
257
+		 *
258
+		 * @since 1.2.0
259
+		 * @param  $request
260
+		 * @return array The menu for the corresponding location
261
+		 */
262
+		public function get_menu_location( $request ) {
263
+
264
+			$params     = $request->get_params();
265
+			$location   = $params['location'];
266
+			$locations  = get_nav_menu_locations();
267
+
268
+			if ( ! isset( $locations[ $location ] ) ) {
269
+				return array();
270
+			}
271
+
272
+			$wp_menu = wp_get_nav_menu_object( $locations[ $location ] );
273
+			$menu_items = wp_get_nav_menu_items( $wp_menu->term_id );
274
+			$sorted_menu_items = $top_level_menu_items = $menu_items_with_children = array();
275
+
276
+			foreach ( (array) $menu_items as $menu_item ) {
277
+				$sorted_menu_items[ $menu_item->menu_order ] = $menu_item;
278
+			}
279
+			foreach ( $sorted_menu_items as $menu_item ) {
280
+				if ( (int) $menu_item->menu_item_parent !== 0 ) {
281
+					$menu_items_with_children[ $menu_item->menu_item_parent ] = true;
282
+				} else {
283
+					$top_level_menu_items[] = $menu_item;
284
+				}
285
+			}
286
+
287
+			$menu = array();
288
+
289
+			while ( $sorted_menu_items ) :
290
+
291
+				$i = 0;
292
+				foreach ( $top_level_menu_items as $top_item ) :
293
+
294
+					$menu[ $i ] = $this->format_menu_item( $top_item, false );
295
+					if ( isset( $menu_items_with_children[ $top_item->ID ] ) ) {
296
+						$menu[ $i ]['children'] = $this->get_nav_menu_item_children( $top_item->ID, $menu_items, false );
297
+					} else {
298
+						$menu[ $i ]['children'] = array();
299
+					}
300
+
301
+					$i++;
302
+				endforeach;
303
+
304
+				break;
305
+
306
+			endwhile;
307
+
308
+			return $menu;
309
+		}
310
+
311
+
312
+		/**
313
+		 * Returns all child nav_menu_items under a specific parent.
314
+		 *
315
+		 * @since   1.2.0
316
+		 * @param int   $parent_id      The parent nav_menu_item ID
317
+		 * @param array $nav_menu_items Navigation menu items
318
+		 * @param bool  $depth          Gives all children or direct children only
319
+		 * @return  array   returns filtered array of nav_menu_items
320
+		 */
321
+		public function get_nav_menu_item_children( $parent_id, $nav_menu_items, $depth = true ) {
322 322
 
323
-            $nav_menu_item_list = array();
323
+			$nav_menu_item_list = array();
324 324
 
325
-            foreach ( (array) $nav_menu_items as $nav_menu_item ) :
325
+			foreach ( (array) $nav_menu_items as $nav_menu_item ) :
326 326
 
327
-                if ( $nav_menu_item->menu_item_parent == $parent_id ) :
327
+				if ( $nav_menu_item->menu_item_parent == $parent_id ) :
328 328