Completed
Pull Request — master (#27)
by
unknown
02:13
created
includes/wp-api-menus-v1.php 3 patches
Doc Comments   -1 removed lines patch added patch discarded remove patch
@@ -136,7 +136,6 @@
 block discarded – undo
136 136
 		 * Get a menu rendered in html.
137 137
 		 *
138 138
 		 * @since  1.0.0
139
-		 * @param  int   $id ID of the menu
140 139
 		 * @return array Menu data
141 140
 		 */
142 141
 		public function get_menu_html( $menu_id ) {
Please login to merge, or discard this patch.
Indentation   +6 added lines, -6 removed lines patch added patch discarded remove patch
@@ -161,10 +161,10 @@  discard block
 block discarded – undo
161 161
 				$json_menu['count']         = abs( $menu['count'] );
162 162
 
163 163
 				ob_start();
164
-           			wp_nav_menu( array( 'menu' => $menu_id ) );
165
-           		$json_menu['html']=ob_get_clean();
164
+		   			wp_nav_menu( array( 'menu' => $menu_id ) );
165
+		   		$json_menu['html']=ob_get_clean();
166 166
 
167
-           		$json_menu['meta']['links']['collection'] = $json_url_base . '/menus/';
167
+		   		$json_menu['meta']['links']['collection'] = $json_url_base . '/menus/';
168 168
 				$json_menu['meta']['links']['self']       = $json_url_base . '/menu-html/' . $menu_id;
169 169
 
170 170
 			endif;
@@ -293,10 +293,10 @@  discard block
 block discarded – undo
293 293
 				$json_menu['count']         = abs( $menu['count'] );
294 294
 
295 295
 				ob_start();
296
-           			wp_nav_menu( array( 'menu' => $location ) );
297
-           		$json_menu['html']=ob_get_clean();
296
+		   			wp_nav_menu( array( 'menu' => $location ) );
297
+		   		$json_menu['html']=ob_get_clean();
298 298
 
299
-           		$json_menu['meta']['links']['collection'] = $json_url_base . '/menu-locations/';
299
+		   		$json_menu['meta']['links']['collection'] = $json_url_base . '/menu-locations/';
300 300
 				$json_menu['meta']['links']['self']       = $json_url_base . '/menu-html-location/' . $location;
301 301
 
302 302
 			endif;
Please login to merge, or discard this patch.
Spacing   +91 added lines, -91 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,31 +30,31 @@  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
 			// a specific menu rendered in html
44 44
 			$routes['/menu-html/(?P<id>[a-zA-Z0-9_-]+)'] = array(
45
-				array( array( $this, 'get_menu_html' ), WP_JSON_Server::READABLE ),
45
+				array(array($this, 'get_menu_html'), WP_JSON_Server::READABLE),
46 46
 			);
47 47
 			// all registered menu locations
48 48
 			$routes['/menu-locations'] = array(
49
-				array( array( $this, 'get_menu_locations' ), WP_JSON_Server::READABLE ),
49
+				array(array($this, 'get_menu_locations'), WP_JSON_Server::READABLE),
50 50
 			);
51 51
 			// menu for given location
52 52
 			$routes['/menu-locations/(?P<location>[a-zA-Z0-9_-]+)'] = array(
53
-				array( array( $this, 'get_menu_location' ), WP_JSON_Server::READABLE ),
53
+				array(array($this, 'get_menu_location'), WP_JSON_Server::READABLE),
54 54
 			);
55 55
 			// menu for given location rendered in html
56 56
 			$routes['/menu-html-location/(?P<location>[a-zA-Z0-9_-]+)'] = array(
57
-				array( array( $this, 'get_menu_html_location' ), WP_JSON_Server::READABLE ),
57
+				array(array($this, 'get_menu_html_location'), WP_JSON_Server::READABLE),
58 58
 			);
59 59
 
60 60
 			return $routes;
@@ -69,28 +69,28 @@  discard block
 block discarded – undo
69 69
 		 */
70 70
 		public static function get_menus() {
71 71
 
72
-			$json_url = get_json_url() . '/menus/';
72
+			$json_url = get_json_url().'/menus/';
73 73
 			$json_url_base = get_json_url();
74 74
 			$wp_menus = wp_get_nav_menus();
75 75
 
76 76
 			$i = 0;
77 77
 			$json_menus = array();
78
-			foreach ( $wp_menus as $wp_menu ) :
78
+			foreach ($wp_menus as $wp_menu) :
79 79
 
80 80
 				$menu = (array) $wp_menu;
81 81
 
82
-				$json_menus[ $i ]                 = $menu;
83
-				$json_menus[ $i ]['ID']           = $menu['term_id'];
84
-				$json_menus[ $i ]['name']         = $menu['name'];
85
-				$json_menus[ $i ]['slug']         = $menu['slug'];
86
-				$json_menus[ $i ]['description']  = $menu['description'];
87
-				$json_menus[ $i ]['count']        = $menu['count'];
82
+				$json_menus[$i]                 = $menu;
83
+				$json_menus[$i]['ID']           = $menu['term_id'];
84
+				$json_menus[$i]['name']         = $menu['name'];
85
+				$json_menus[$i]['slug']         = $menu['slug'];
86
+				$json_menus[$i]['description']  = $menu['description'];
87
+				$json_menus[$i]['count']        = $menu['count'];
88 88
 
89
-				$json_menus[ $i ]['meta']['links']['collection'] = $json_url;
90
-				$json_menus[ $i ]['meta']['links']['self']       = $json_url . $menu['term_id'];
91
-				$json_menus[ $i ]['meta']['links']['self']       = $json_url_base . '/menu-html/' . $menu['term_id'];
89
+				$json_menus[$i]['meta']['links']['collection'] = $json_url;
90
+				$json_menus[$i]['meta']['links']['self']       = $json_url.$menu['term_id'];
91
+				$json_menus[$i]['meta']['links']['self']       = $json_url_base.'/menu-html/'.$menu['term_id'];
92 92
 
93
-				$i ++;
93
+				$i++;
94 94
 			endforeach;
95 95
 
96 96
 			return $json_menus;
@@ -104,31 +104,31 @@  discard block
 block discarded – undo
104 104
 		 * @param  int   $id ID of the menu
105 105
 		 * @return array Menu data
106 106
 		 */
107
-		public function get_menu( $id ) {
107
+		public function get_menu($id) {
108 108
 
109
-			$json_url       = get_json_url() . '/menus/';
110
-			$wp_menu_object = $id ? wp_get_nav_menu_object( $id ) : array();
111
-			$wp_menu_items  = $id ? wp_get_nav_menu_items( $id ) : array();
109
+			$json_url       = get_json_url().'/menus/';
110
+			$wp_menu_object = $id ? wp_get_nav_menu_object($id) : array();
111
+			$wp_menu_items  = $id ? wp_get_nav_menu_items($id) : array();
112 112
 
113 113
 			$json_menu = array();
114 114
 
115
-			if ( $wp_menu_object ) :
115
+			if ($wp_menu_object) :
116 116
 
117 117
 				$menu = (array) $wp_menu_object;
118
-				$json_menu['ID']            = abs( $menu['term_id'] );
118
+				$json_menu['ID']            = abs($menu['term_id']);
119 119
 				$json_menu['name']          = $menu['name'];
120 120
 				$json_menu['slug']          = $menu['slug'];
121 121
 				$json_menu['description']   = $menu['description'];
122
-				$json_menu['count']         = abs( $menu['count'] );
122
+				$json_menu['count']         = abs($menu['count']);
123 123
 
124 124
 				$json_menu_items = array();
125
-				foreach ( $wp_menu_items as $item_object ) {
126
-					$json_menu_items[] = $this->format_menu_item( $item_object );
125
+				foreach ($wp_menu_items as $item_object) {
126
+					$json_menu_items[] = $this->format_menu_item($item_object);
127 127
 				}
128 128
 
129 129
 				$json_menu['items']                       = $json_menu_items;
130 130
 				$json_menu['meta']['links']['collection'] = $json_url;
131
-				$json_menu['meta']['links']['self']       = $json_url . $id;
131
+				$json_menu['meta']['links']['self']       = $json_url.$id;
132 132
 
133 133
 			endif;
134 134
 
@@ -143,29 +143,29 @@  discard block
 block discarded – undo
143 143
 		 * @param  int   $id ID of the menu
144 144
 		 * @return array Menu data
145 145
 		 */
146
-		public function get_menu_html( $menu_id ) {
146
+		public function get_menu_html($menu_id) {
147 147
 
148 148
 			$json_url_base  = get_json_url();
149
-			$wp_menu_object = $menu_id ? wp_get_nav_menu_object( $menu_id ) : array();
150
-			$wp_menu_items  = $menu_id ? wp_get_nav_menu_items( $menu_id ) : array();
149
+			$wp_menu_object = $menu_id ? wp_get_nav_menu_object($menu_id) : array();
150
+			$wp_menu_items  = $menu_id ? wp_get_nav_menu_items($menu_id) : array();
151 151
 
152 152
 			$json_menu = array();
153 153
 
154
-			if ( $wp_menu_object ) :
154
+			if ($wp_menu_object) :
155 155
 
156 156
 				$menu = (array) $wp_menu_object;
157
-				$json_menu['ID']            = abs( $menu['term_id'] );
157
+				$json_menu['ID']            = abs($menu['term_id']);
158 158
 				$json_menu['name']          = $menu['name'];
159 159
 				$json_menu['slug']          = $menu['slug'];
160 160
 				$json_menu['description']   = $menu['description'];
161
-				$json_menu['count']         = abs( $menu['count'] );
161
+				$json_menu['count']         = abs($menu['count']);
162 162
 
163 163
 				ob_start();
164
-           			wp_nav_menu( array( 'menu' => $menu_id ) );
165
-           		$json_menu['html']=ob_get_clean();
164
+           			wp_nav_menu(array('menu' => $menu_id));
165
+           		$json_menu['html'] = ob_get_clean();
166 166
 
167
-           		$json_menu['meta']['links']['collection'] = $json_url_base . '/menus/';
168
-				$json_menu['meta']['links']['self']       = $json_url_base . '/menu-html/' . $menu_id;
167
+           		$json_menu['meta']['links']['collection'] = $json_url_base.'/menus/';
168
+				$json_menu['meta']['links']['self'] = $json_url_base.'/menu-html/'.$menu_id;
169 169
 
170 170
 			endif;
171 171
 
@@ -183,24 +183,24 @@  discard block
 block discarded – undo
183 183
 
184 184
 			$locations        = get_nav_menu_locations();
185 185
 			$registered_menus = get_registered_nav_menus();
186
-			$json_url         = get_json_url() . '/menu-locations/';
186
+			$json_url         = get_json_url().'/menu-locations/';
187 187
 			$json_url_base    = get_json_url();
188 188
 			$json_menus       = array();
189 189
 
190
-			if ( $locations && $registered_menus ) :
190
+			if ($locations && $registered_menus) :
191 191
 
192
-				foreach ( $registered_menus as $slug => $label ) :
192
+				foreach ($registered_menus as $slug => $label) :
193 193
 
194 194
 					// Sanity check
195
-					if ( ! isset( $locations[ $slug ] ) ) {
195
+					if ( ! isset($locations[$slug])) {
196 196
 						continue;
197 197
 					}
198 198
 
199
-					$json_menus[ $slug ]['ID']                          = $locations[ $slug ];
200
-					$json_menus[ $slug ]['label']                       = $label;
201
-					$json_menus[ $slug ]['meta']['links']['collection'] = $json_url;
202
-					$json_menus[ $slug ]['meta']['links']['self']       = $json_url . $slug;
203
-					$json_menus[ $slug ]['meta']['links']['html']       = $json_url_base . '/menu-html-location/' . $slug;
199
+					$json_menus[$slug]['ID']                          = $locations[$slug];
200
+					$json_menus[$slug]['label']                       = $label;
201
+					$json_menus[$slug]['meta']['links']['collection'] = $json_url;
202
+					$json_menus[$slug]['meta']['links']['self']       = $json_url.$slug;
203
+					$json_menus[$slug]['meta']['links']['html']       = $json_url_base.'/menu-html-location/'.$slug;
204 204
 
205 205
 				endforeach;
206 206
 
@@ -217,40 +217,40 @@  discard block
 block discarded – undo
217 217
 		 * @param  string $location The theme location menu name
218 218
 		 * @return array The menu for the corresponding location
219 219
 		 */
220
-		public function get_menu_location( $location ) {
220
+		public function get_menu_location($location) {
221 221
 
222 222
 			$locations = get_nav_menu_locations();
223
-			if ( ! isset( $locations[ $location ] ) ) {
223
+			if ( ! isset($locations[$location])) {
224 224
 				return array();
225 225
 			}
226 226
 
227
-			$wp_menu = wp_get_nav_menu_object( $locations[ $location ] );
228
-			$menu_items = wp_get_nav_menu_items( $wp_menu->term_id );
227
+			$wp_menu = wp_get_nav_menu_object($locations[$location]);
228
+			$menu_items = wp_get_nav_menu_items($wp_menu->term_id);
229 229
 
230 230
 			$sorted_menu_items = $top_level_menu_items = $menu_items_with_children = array();
231 231
 
232
-			foreach ( (array) $menu_items as $menu_item ) {
233
-				$sorted_menu_items[ $menu_item->menu_order ] = $menu_item;
232
+			foreach ((array) $menu_items as $menu_item) {
233
+				$sorted_menu_items[$menu_item->menu_order] = $menu_item;
234 234
 			}
235
-			foreach ( $sorted_menu_items as $menu_item ) {
236
-				if ( (int) $menu_item->menu_item_parent !== 0 ) {
237
-					$menu_items_with_children[ $menu_item->menu_item_parent ] = true;
235
+			foreach ($sorted_menu_items as $menu_item) {
236
+				if ((int) $menu_item->menu_item_parent !== 0) {
237
+					$menu_items_with_children[$menu_item->menu_item_parent] = true;
238 238
 				} else {
239 239
 					$top_level_menu_items[] = $menu_item;
240 240
 				}
241 241
 			}
242 242
 
243 243
 			$menu = array();
244
-			while ( $sorted_menu_items ) :
244
+			while ($sorted_menu_items) :
245 245
 
246 246
 				$i = 0;
247
-				foreach ( $top_level_menu_items as $top_item ) :
247
+				foreach ($top_level_menu_items as $top_item) :
248 248
 
249
-					$menu[ $i ] = $this->format_menu_item( $top_item, false );
250
-					if ( isset( $menu_items_with_children[ $top_item->ID ] ) ) {
251
-						$menu[ $i ]['children'] = $this->get_nav_menu_item_children( $top_item->ID, $menu_items, false );
249
+					$menu[$i] = $this->format_menu_item($top_item, false);
250
+					if (isset($menu_items_with_children[$top_item->ID])) {
251
+						$menu[$i]['children'] = $this->get_nav_menu_item_children($top_item->ID, $menu_items, false);
252 252
 					} else {
253
-						$menu[ $i ]['children'] = array();
253
+						$menu[$i]['children'] = array();
254 254
 					}
255 255
 
256 256
 					$i++;
@@ -270,34 +270,34 @@  discard block
 block discarded – undo
270 270
 		 * @param  string $location The theme location menu name
271 271
 		 * @return array The menu for the corresponding location
272 272
 		 */
273
-		public static function get_menu_html_location( $location ) {
273
+		public static function get_menu_html_location($location) {
274 274
 
275
-			$locations        = get_nav_menu_locations();
276
-			if ( ! isset( $locations[ $location ] ) ) {
275
+			$locations = get_nav_menu_locations();
276
+			if ( ! isset($locations[$location])) {
277 277
 				return array();
278 278
 			}
279 279
 
280
-			$wp_menu_object = get_term( $locations[$location], 'nav_menu' );
280
+			$wp_menu_object = get_term($locations[$location], 'nav_menu');
281 281
 			$json_url_base  = get_json_url();
282 282
 
283 283
 			$json_menu      = array();
284 284
 
285
-			if ( $wp_menu_object ) :
285
+			if ($wp_menu_object) :
286 286
 
287 287
 				$menu = (array) $wp_menu_object;
288
-				$json_menu['ID']            = abs( $menu['term_id'] );
288
+				$json_menu['ID']            = abs($menu['term_id']);
289 289
 				$json_menu['name']          = $menu['name'];
290 290
 				$json_menu['slug']          = $menu['slug'];
291 291
 				$json_menu['location_slug'] = $location;
292 292
 				$json_menu['description']   = $menu['description'];
293
-				$json_menu['count']         = abs( $menu['count'] );
293
+				$json_menu['count']         = abs($menu['count']);
294 294
 
295 295
 				ob_start();
296
-           			wp_nav_menu( array( 'menu' => $location ) );
297
-           		$json_menu['html']=ob_get_clean();
296
+           			wp_nav_menu(array('menu' => $location));
297
+           		$json_menu['html'] = ob_get_clean();
298 298
 
299
-           		$json_menu['meta']['links']['collection'] = $json_url_base . '/menu-locations/';
300
-				$json_menu['meta']['links']['self']       = $json_url_base . '/menu-html-location/' . $location;
299
+           		$json_menu['meta']['links']['collection'] = $json_url_base.'/menu-locations/';
300
+				$json_menu['meta']['links']['self'] = $json_url_base.'/menu-html-location/'.$location;
301 301
 
302 302
 			endif;
303 303
 
@@ -314,19 +314,19 @@  discard block
 block discarded – undo
314 314
 		 * @param  bool    $depth          gives all children or direct children only
315 315
 		 * @return array   returns filtered array of nav_menu_items
316 316
 		 */
317
-		public function get_nav_menu_item_children( $parent_id, $nav_menu_items, $depth = true ) {
317
+		public function get_nav_menu_item_children($parent_id, $nav_menu_items, $depth = true) {
318 318
 
319 319
 			$nav_menu_item_list = array();
320 320
 
321
-			foreach ( (array) $nav_menu_items as $nav_menu_item ) :
321
+			foreach ((array) $nav_menu_items as $nav_menu_item) :
322 322
 
323
-				if ( $nav_menu_item->menu_item_parent == $parent_id ) :
323
+				if ($nav_menu_item->menu_item_parent == $parent_id) :
324 324
 
325
-					$nav_menu_item_list[] = $this->format_menu_item( $nav_menu_item, true, $nav_menu_items );
325
+					$nav_menu_item_list[] = $this->format_menu_item($nav_menu_item, true, $nav_menu_items);
326 326
 
327
-					if ( $depth ) {
328
-						if ( $children = $this->get_nav_menu_item_children( $nav_menu_item->ID, $nav_menu_items ) ) {
329
-							$nav_menu_item_list = array_merge( $nav_menu_item_list, $children );
327
+					if ($depth) {
328
+						if ($children = $this->get_nav_menu_item_children($nav_menu_item->ID, $nav_menu_items)) {
329
+							$nav_menu_item_list = array_merge($nav_menu_item_list, $children);
330 330
 						}
331 331
 					}
332 332
 
@@ -347,32 +347,32 @@  discard block
 block discarded – undo
347 347
 		 * @param   array           $menu       the menu the item belongs to (used when $children is set to true)
348 348
 		 * @return  array   a formatted menu item for JSON
349 349
 		 */
350
-		public function format_menu_item( $menu_item, $children = false, $menu = array() ) {
350
+		public function format_menu_item($menu_item, $children = false, $menu = array()) {
351 351
 
352 352
 			$item = (array) $menu_item;
353 353
 
354 354
 			$menu_item = array(
355
-				'ID'          => abs( $item['ID'] ),
355
+				'ID'          => abs($item['ID']),
356 356
 				'order'       => (int) $item['menu_order'],
357
-				'parent'      => abs( $item['menu_item_parent'] ),
357
+				'parent'      => abs($item['menu_item_parent']),
358 358
 				'title'       => $item['title'],
359 359
 				'url'         => $item['url'],
360 360
 				'attr'        => $item['attr_title'],
361 361
 				'target'      => $item['target'],
362
-				'classes'     => implode( ' ', $item['classes'] ),
362
+				'classes'     => implode(' ', $item['classes']),
363 363
 				'xfn'         => $item['xfn'],
364 364
 				'description' => $item['description'],
365
-				'object_id'   => abs( $item['object_id'] ),
365
+				'object_id'   => abs($item['object_id']),
366 366
 				'object'      => $item['object'],
367 367
 				'type'        => $item['type'],
368 368
 				'type_label'  => $item['type_label'],
369 369
 			);
370 370
 
371
-			if ( $children === true && ! empty( $menu ) ) {
372
-				$menu_item['children'] = $this->get_nav_menu_item_children( $item['ID'], $menu );
371
+			if ($children === true && ! empty($menu)) {
372
+				$menu_item['children'] = $this->get_nav_menu_item_children($item['ID'], $menu);
373 373
 			}
374 374
 
375
-			return apply_filters( 'json_menus_format_menu_item', $menu_item );
375
+			return apply_filters('json_menus_format_menu_item', $menu_item);
376 376
 		}
377 377
 
378 378
 
Please login to merge, or discard this patch.
includes/wp-api-menus-v2.php 2 patches
Indentation   +418 added lines, -418 removed lines patch added patch discarded remove patch
@@ -6,333 +6,333 @@  discard block
 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
-
76
-            register_rest_route( self::get_plugin_namespace(), '/menu-html/(?P<menu_id>[a-zA-Z0-9_-]+)', array(
77
-                array(
78
-                    'methods'  => WP_REST_Server::READABLE,
79
-                    'callback' => array( $this, 'get_menu_html' ),
80
-                    'args'     => array(
81
-                        'context' => array(
82
-                        'default' => 'view',
83
-                        ),
84
-                    ),
85
-                )
86
-            ) );
87
-
88
-            register_rest_route( self::get_plugin_namespace(), '/menu-locations', array(
89
-                array(
90
-                    'methods'  => WP_REST_Server::READABLE,
91
-                    'callback' => array( $this, 'get_menu_locations' ),
92
-                )
93
-            ) );
94
-
95
-            register_rest_route( self::get_plugin_namespace(), '/menu-locations/(?P<location>[a-zA-Z0-9_-]+)', array(
96
-                array(
97
-                    'methods'  => WP_REST_Server::READABLE,
98
-                    'callback' => array( $this, 'get_menu_location' ),
99
-                )
100
-            ) );
101
-
102
-            register_rest_route( self::get_plugin_namespace(), '/menu-html-location/(?P<location>[a-zA-Z0-9_-]+)', array(
103
-                array(
104
-                    'methods'  => WP_REST_Server::READABLE,
105
-                    'callback' => array( $this, 'get_menu_html_location' ),
106
-                )
107
-            ) );
108
-
109
-        }
110
-
111
-
112
-        /**
113
-         * Get menus.
114
-         *
115
-         * @since  1.2.0
116
-         * @return array All registered menus
117
-         */
118
-        public static function get_menus() {
119
-
120
-            $rest_url = trailingslashit( get_rest_url() . self::get_plugin_namespace() . '/menus/' );
121
-            $rest_url_base = get_rest_url() . self::get_plugin_namespace();
122
-            $wp_menus = wp_get_nav_menus();
123
-
124
-            $i = 0;
125
-            $rest_menus = array();
126
-            foreach ( $wp_menus as $wp_menu ) :
127
-
128
-                $menu = (array) $wp_menu;
129
-
130
-                $rest_menus[ $i ]                = $menu;
131
-                $rest_menus[ $i ]['ID']          = $menu['term_id'];
132
-                $rest_menus[ $i ]['name']        = $menu['name'];
133
-                $rest_menus[ $i ]['slug']        = $menu['slug'];
134
-                $rest_menus[ $i ]['description'] = $menu['description'];
135
-                $rest_menus[ $i ]['count']       = $menu['count'];
136
-
137
-                $rest_menus[ $i ]['meta']['links']['collection'] = $rest_url;
138
-                $rest_menus[ $i ]['meta']['links']['self']       = $rest_url . $menu['term_id'];
139
-                $rest_menus[ $i ]['meta']['links']['html']       = $rest_url_base . '/menu-html/' . $menu['term_id'];
140
-
141
-                $i ++;
142
-            endforeach;
143
-
144
-            return apply_filters( 'rest_menus_format_menus', $rest_menus );
145
-        }
146
-
147
-
148
-        /**
149
-         * Get a menu.
150
-         *
151
-         * @since  1.2.0
152
-         * @param  $request
153
-         * @return array Menu data
154
-         */
155
-        public function get_menu( $request ) {
156
-
157
-            $id             = (int) $request['id'];
158
-            $rest_url       = get_rest_url() . self::get_plugin_namespace() . '/menus/';
159
-            $wp_menu_object = $id ? wp_get_nav_menu_object( $id ) : array();
160
-            $wp_menu_items  = $id ? wp_get_nav_menu_items( $id ) : array();
161
-
162
-            $rest_menu = array();
163
-
164
-            if ( $wp_menu_object ) :
165
-
166
-                $menu = (array) $wp_menu_object;
167
-                $rest_menu['ID']          = abs( $menu['term_id'] );
168
-                $rest_menu['name']        = $menu['name'];
169
-                $rest_menu['slug']        = $menu['slug'];
170
-                $rest_menu['description'] = $menu['description'];
171
-                $rest_menu['count']       = abs( $menu['count'] );
172
-
173
-                $rest_menu_items = array();
174
-                foreach ( $wp_menu_items as $item_object ) {
175
-	                $rest_menu_items[] = $this->format_menu_item( $item_object );
176
-                }
177
-
178
-                $rest_menu_items = $this->nested_menu_items($rest_menu_items, 0);
179
-
180
-                $rest_menu['items']                       = $rest_menu_items;
181
-                $rest_menu['meta']['links']['collection'] = $rest_url;
182
-                $rest_menu['meta']['links']['self']       = $rest_url . $id;
183
-
184
-            endif;
185
-
186
-            return apply_filters( 'rest_menus_format_menu', $rest_menu );
187
-        }
188
-
189
-        /**
190
-         * Get a menu rendered in html.
191
-         *
192
-         * @since  1.x.0
193
-         * @param  $request
194
-         * @return array Menu data
195
-         */
196
-        public function get_menu_html( $request ) {
197
-
198
-            $menu_id        = $request['menu_id'];
199
-            $rest_url_base  = get_rest_url() . self::get_plugin_namespace();
200
-            $wp_menu_object = $menu_id ? wp_get_nav_menu_object( $menu_id) : array();
201
-            $wp_menu_items  = $menu_id ? wp_get_nav_menu_items( $menu_id ) : array();
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
+
76
+			register_rest_route( self::get_plugin_namespace(), '/menu-html/(?P<menu_id>[a-zA-Z0-9_-]+)', array(
77
+				array(
78
+					'methods'  => WP_REST_Server::READABLE,
79
+					'callback' => array( $this, 'get_menu_html' ),
80
+					'args'     => array(
81
+						'context' => array(
82
+						'default' => 'view',
83
+						),
84
+					),
85
+				)
86
+			) );
87
+
88
+			register_rest_route( self::get_plugin_namespace(), '/menu-locations', array(
89
+				array(
90
+					'methods'  => WP_REST_Server::READABLE,
91
+					'callback' => array( $this, 'get_menu_locations' ),
92
+				)
93
+			) );
94
+
95
+			register_rest_route( self::get_plugin_namespace(), '/menu-locations/(?P<location>[a-zA-Z0-9_-]+)', array(
96
+				array(
97
+					'methods'  => WP_REST_Server::READABLE,
98
+					'callback' => array( $this, 'get_menu_location' ),
99
+				)
100
+			) );
101
+
102
+			register_rest_route( self::get_plugin_namespace(), '/menu-html-location/(?P<location>[a-zA-Z0-9_-]+)', array(
103
+				array(
104
+					'methods'  => WP_REST_Server::READABLE,
105
+					'callback' => array( $this, 'get_menu_html_location' ),
106
+				)
107
+			) );
108
+
109
+		}
110
+
111
+
112
+		/**
113
+		 * Get menus.
114
+		 *
115
+		 * @since  1.2.0
116
+		 * @return array All registered menus
117
+		 */
118
+		public static function get_menus() {
119
+
120
+			$rest_url = trailingslashit( get_rest_url() . self::get_plugin_namespace() . '/menus/' );
121
+			$rest_url_base = get_rest_url() . self::get_plugin_namespace();
122
+			$wp_menus = wp_get_nav_menus();
123
+
124
+			$i = 0;
125
+			$rest_menus = array();
126
+			foreach ( $wp_menus as $wp_menu ) :
127
+
128
+				$menu = (array) $wp_menu;
129
+
130
+				$rest_menus[ $i ]                = $menu;
131
+				$rest_menus[ $i ]['ID']          = $menu['term_id'];
132
+				$rest_menus[ $i ]['name']        = $menu['name'];
133
+				$rest_menus[ $i ]['slug']        = $menu['slug'];
134
+				$rest_menus[ $i ]['description'] = $menu['description'];
135
+				$rest_menus[ $i ]['count']       = $menu['count'];
136
+
137
+				$rest_menus[ $i ]['meta']['links']['collection'] = $rest_url;
138
+				$rest_menus[ $i ]['meta']['links']['self']       = $rest_url . $menu['term_id'];
139
+				$rest_menus[ $i ]['meta']['links']['html']       = $rest_url_base . '/menu-html/' . $menu['term_id'];
140
+
141
+				$i ++;
142
+			endforeach;
143
+
144
+			return apply_filters( 'rest_menus_format_menus', $rest_menus );
145
+		}
146
+
147
+
148
+		/**
149
+		 * Get a menu.
150
+		 *
151
+		 * @since  1.2.0
152
+		 * @param  $request
153
+		 * @return array Menu data
154
+		 */
155
+		public function get_menu( $request ) {
156
+
157
+			$id             = (int) $request['id'];
158
+			$rest_url       = get_rest_url() . self::get_plugin_namespace() . '/menus/';
159
+			$wp_menu_object = $id ? wp_get_nav_menu_object( $id ) : array();
160
+			$wp_menu_items  = $id ? wp_get_nav_menu_items( $id ) : array();
202 161
 
203 162
 			$rest_menu = array();
204 163
 
205
-            if ( $wp_menu_object ) :
206
-
207
-                $menu = (array) $wp_menu_object;
208
-                $rest_menu['ID']          = abs( $menu['term_id'] );
209
-                $rest_menu['name']        = $menu['name'];
210
-                $rest_menu['slug']        = $menu['slug'];
211
-                $rest_menu['description'] = $menu['description'];
212
-                $rest_menu['count']       = abs( $menu['count'] );
213
-
214
-                ob_start();
215
-           			wp_nav_menu( array( 'menu' => $menu_id ) );
216
-           		$rest_menu['html']=ob_get_clean();
164
+			if ( $wp_menu_object ) :
165
+
166
+				$menu = (array) $wp_menu_object;
167
+				$rest_menu['ID']          = abs( $menu['term_id'] );
168
+				$rest_menu['name']        = $menu['name'];
169
+				$rest_menu['slug']        = $menu['slug'];
170
+				$rest_menu['description'] = $menu['description'];
171
+				$rest_menu['count']       = abs( $menu['count'] );
172
+
173
+				$rest_menu_items = array();
174
+				foreach ( $wp_menu_items as $item_object ) {
175
+					$rest_menu_items[] = $this->format_menu_item( $item_object );
176
+				}
177
+
178
+				$rest_menu_items = $this->nested_menu_items($rest_menu_items, 0);
179
+
180
+				$rest_menu['items']                       = $rest_menu_items;
181
+				$rest_menu['meta']['links']['collection'] = $rest_url;
182
+				$rest_menu['meta']['links']['self']       = $rest_url . $id;
183
+
184
+			endif;
185
+
186
+			return apply_filters( 'rest_menus_format_menu', $rest_menu );
187
+		}
188
+
189
+		/**
190
+		 * Get a menu rendered in html.
191
+		 *
192
+		 * @since  1.x.0
193
+		 * @param  $request
194
+		 * @return array Menu data
195
+		 */
196
+		public function get_menu_html( $request ) {
197
+
198
+			$menu_id        = $request['menu_id'];
199
+			$rest_url_base  = get_rest_url() . self::get_plugin_namespace();
200
+			$wp_menu_object = $menu_id ? wp_get_nav_menu_object( $menu_id) : array();
201
+			$wp_menu_items  = $menu_id ? wp_get_nav_menu_items( $menu_id ) : array();
202
+
203
+			$rest_menu = array();
204
+
205
+			if ( $wp_menu_object ) :
206
+
207
+				$menu = (array) $wp_menu_object;
208
+				$rest_menu['ID']          = abs( $menu['term_id'] );
209
+				$rest_menu['name']        = $menu['name'];
210
+				$rest_menu['slug']        = $menu['slug'];
211
+				$rest_menu['description'] = $menu['description'];
212
+				$rest_menu['count']       = abs( $menu['count'] );
213
+
214
+				ob_start();
215
+		   			wp_nav_menu( array( 'menu' => $menu_id ) );
216
+		   		$rest_menu['html']=ob_get_clean();
217
+
218
+		   		$rest_menu['meta']['links']['collection'] = $rest_url_base . '/menus/';
219
+				$rest_menu['meta']['links']['self']       = $rest_url_base . '/menu-html/' . $menu_id;
220
+
221
+			endif;
222
+
223
+			return apply_filters( 'rest_menus_format_menu', $rest_menu );
224
+		}
225
+
226
+
227
+
228
+		/**
229
+		 * Handle nested menu items.
230
+		 *
231
+		 * Given a flat array of menu items, split them into parent/child items
232
+		 * and recurse over them to return children nested in their parent.
233
+		 *
234
+		 * @since  1.2.0
235
+		 * @param  $menu_items
236
+		 * @param  $parent
237
+		 * @return array
238
+		 */
239
+		private function nested_menu_items( &$menu_items, $parent = null ) {
217 240
 
218
-           		$rest_menu['meta']['links']['collection'] = $rest_url_base . '/menus/';
219
-                $rest_menu['meta']['links']['self']       = $rest_url_base . '/menu-html/' . $menu_id;
220
-
221
-            endif;
222
-
223
-            return apply_filters( 'rest_menus_format_menu', $rest_menu );
224
-        }
225
-
226
-
227
-
228
-        /**
229
-         * Handle nested menu items.
230
-         *
231
-         * Given a flat array of menu items, split them into parent/child items
232
-         * and recurse over them to return children nested in their parent.
233
-         *
234
-         * @since  1.2.0
235
-         * @param  $menu_items
236
-         * @param  $parent
237
-         * @return array
238
-         */
239
-        private function nested_menu_items( &$menu_items, $parent = null ) {
240
-
241
-            $parents = array();
242
-            $children = array();
243
-
244
-            // Separate menu_items into parents & children.
245
-            array_map( function( $i ) use ( $parent, &$children, &$parents ){
246
-                if ( $i['id'] != $parent && $i['parent'] == $parent ) {
247
-                    $parents[] = $i;
248
-                } else {
249
-                    $children[] = $i;
250
-                }
251
-            }, $menu_items );
252
-
253
-            foreach ( $parents as &$parent ) {
254
-
255
-                if ( $this->has_children( $children, $parent['id'] ) ) {
256
-                    $parent['children'] = $this->nested_menu_items( $children, $parent['id'] );
257
-                }
258
-            }
259
-
260
-            return $parents;
261
-        }
262
-
263
-
264
-        /**
265
-         * Check if a collection of menu items contains an item that is the parent id of 'id'.
266
-         *
267
-         * @since  1.2.0
268
-         * @param  array $items
269
-         * @param  int $id
270
-         * @return array
271
-         */
272
-        private function has_children( $items, $id ) {
273
-            return array_filter( $items, function( $i ) use ( $id ) {
274
-                return $i['parent'] == $id;
275
-            } );
276
-        }
277
-
278
-
279
-        /**
280
-         * Get menu locations.
281
-         *
282
-         * @since 1.2.0
283
-         * @param  $request
284
-         * @return array All registered menus locations
285
-         */
286
-        public static function get_menu_locations( $request ) {
287
-
288
-            $locations        = get_nav_menu_locations();
289
-            $registered_menus = get_registered_nav_menus();
290
-	        $rest_url         = get_rest_url() . self::get_plugin_namespace() . '/menu-locations/';
291
-	        $rest_url_base    = get_rest_url() . self::get_plugin_namespace();
292
-            $rest_menus       = array();
293
-
294
-            if ( $locations && $registered_menus ) :
295
-
296
-                foreach ( $registered_menus as $slug => $label ) :
297
-
298
-	                // Sanity check
299
-	                if ( ! isset( $locations[ $slug ] ) ) {
300
-		                continue;
301
-	                }
302
-
303
-	                $rest_menus[ $slug ]['ID']                          = $locations[ $slug ];
304
-                    $rest_menus[ $slug ]['label']                       = $label;
305
-                    $rest_menus[ $slug ]['meta']['links']['collection'] = $rest_url;
306
-                    $rest_menus[ $slug ]['meta']['links']['self']       = $rest_url . $slug;
307
-                    $rest_menus[ $slug ]['meta']['links']['html']       = $rest_url_base . '/menu-html-location/' . $slug;
308
-
309
-                endforeach;
310
-
311
-            endif;
312
-
313
-            return $rest_menus;
314
-        }
315
-
316
-
317
-        /**
318
-         * Get menu for location.
319
-         *
320
-         * @since 1.2.0
321
-         * @param  $request
322
-         * @return array The menu for the corresponding location
323
-         */
324
-        public function get_menu_location( $request ) {
325
-
326
-            $params     = $request->get_params();
327
-            $location   = $params['location'];
328
-            $locations  = get_nav_menu_locations();
329
-
330
-            if ( ! isset( $locations[ $location ] ) ) {
331
-	            return array();
332
-            }
333
-
334
-            $wp_menu = wp_get_nav_menu_object( $locations[ $location ] );
335
-            $menu_items = wp_get_nav_menu_items( $wp_menu->term_id );
241
+			$parents = array();
242
+			$children = array();
243
+
244
+			// Separate menu_items into parents & children.
245
+			array_map( function( $i ) use ( $parent, &$children, &$parents ){
246
+				if ( $i['id'] != $parent && $i['parent'] == $parent ) {
247
+					$parents[] = $i;
248
+				} else {
249
+					$children[] = $i;
250
+				}
251
+			}, $menu_items );
252
+
253
+			foreach ( $parents as &$parent ) {
254
+
255
+				if ( $this->has_children( $children, $parent['id'] ) ) {
256
+					$parent['children'] = $this->nested_menu_items( $children, $parent['id'] );
257
+				}
258
+			}
259
+
260
+			return $parents;
261
+		}
262
+
263
+
264
+		/**
265
+		 * Check if a collection of menu items contains an item that is the parent id of 'id'.
266
+		 *
267
+		 * @since  1.2.0
268
+		 * @param  array $items
269
+		 * @param  int $id
270
+		 * @return array
271
+		 */
272
+		private function has_children( $items, $id ) {
273
+			return array_filter( $items, function( $i ) use ( $id ) {
274
+				return $i['parent'] == $id;
275
+			} );
276
+		}
277
+
278
+
279
+		/**
280
+		 * Get menu locations.
281
+		 *
282
+		 * @since 1.2.0
283
+		 * @param  $request
284
+		 * @return array All registered menus locations
285
+		 */
286
+		public static function get_menu_locations( $request ) {
287
+
288
+			$locations        = get_nav_menu_locations();
289
+			$registered_menus = get_registered_nav_menus();
290
+			$rest_url         = get_rest_url() . self::get_plugin_namespace() . '/menu-locations/';
291
+			$rest_url_base    = get_rest_url() . self::get_plugin_namespace();
292
+			$rest_menus       = array();
293
+
294
+			if ( $locations && $registered_menus ) :
295
+
296
+				foreach ( $registered_menus as $slug => $label ) :
297
+
298
+					// Sanity check
299
+					if ( ! isset( $locations[ $slug ] ) ) {
300
+						continue;
301
+					}
302
+
303
+					$rest_menus[ $slug ]['ID']                          = $locations[ $slug ];
304
+					$rest_menus[ $slug ]['label']                       = $label;
305
+					$rest_menus[ $slug ]['meta']['links']['collection'] = $rest_url;
306
+					$rest_menus[ $slug ]['meta']['links']['self']       = $rest_url . $slug;
307
+					$rest_menus[ $slug ]['meta']['links']['html']       = $rest_url_base . '/menu-html-location/' . $slug;
308
+
309
+				endforeach;
310
+
311
+			endif;
312
+
313
+			return $rest_menus;
314
+		}
315
+
316
+
317
+		/**
318
+		 * Get menu for location.
319
+		 *
320
+		 * @since 1.2.0
321
+		 * @param  $request
322
+		 * @return array The menu for the corresponding location
323
+		 */
324
+		public function get_menu_location( $request ) {
325
+
326
+			$params     = $request->get_params();
327
+			$location   = $params['location'];
328
+			$locations  = get_nav_menu_locations();
329
+
330
+			if ( ! isset( $locations[ $location ] ) ) {
331
+				return array();
332
+			}
333
+
334
+			$wp_menu = wp_get_nav_menu_object( $locations[ $location ] );
335
+			$menu_items = wp_get_nav_menu_items( $wp_menu->term_id );
336 336
 
337 337
 			/**
338 338
 			 * wp_get_nav_menu_items() outputs a list that's already sequenced correctly.
@@ -365,7 +365,7 @@  discard block
 block discarded – undo
365 365
 					$formatted['children'] = array_reverse ( $cache[ $item->ID ] );
366 366
 				}
367 367
 
368
-            	$formatted = apply_filters( 'rest_menus_format_menu_item', $formatted );
368
+				$formatted = apply_filters( 'rest_menus_format_menu_item', $formatted );
369 369
 
370 370
 				if ( $item->menu_item_parent != 0 ) {
371 371
 					// Wait for parent to pick me up
@@ -379,126 +379,126 @@  discard block
 block discarded – undo
379 379
 				}
380 380
 			endforeach;
381 381
 			return array_reverse ( $rev_menu );
382
-        }
383
-
384
-
385
-        /**
386
-         * Get menu rendered in html for location.
387
-         *
388
-         * @since 1.x.0
389
-         * @param  $request
390
-         * @return array The menu for the corresponding location
391
-         */
392
-        public function get_menu_html_location( $request ) {
393
-
394
-            $params     = $request->get_params();
395
-            $location   = $params['location'];
396
-            $locations  = get_nav_menu_locations();
397
-
398
-            //Check if location exists and return empty array if it doesn't
399
-            if ( ! isset( $locations[ $location ] ) ) {
400
-	            return array();
401
-            }
402
-
403
-            $rest_url_base  = get_rest_url() . self::get_plugin_namespace();
404
-            $wp_menu_object = get_term( $locations[$location], 'nav_menu' );
405
-            $rest_menu = array();
406
-
407
-            if ( $wp_menu_object ) :
408
-
409
-                $menu = (array) $wp_menu_object;
410
-                $rest_menu['ID']          	= abs( $menu['term_id'] );
411
-                $rest_menu['name']        	= $menu['name'];
412
-                $rest_menu['slug']        	= $menu['slug'];
413
-                $rest_menu['location_slug'] = $location;
414
-                $rest_menu['description'] 	= $menu['description'];
415
-                $rest_menu['count']       	= abs( $menu['count'] );
416
-
417
-                ob_start();
418
-					wp_nav_menu( array( 'theme_location' => $location ) );
419
-				$rest_menu['html']=ob_get_clean();
382
+		}
420 383
 
421
-           		$rest_menu['meta']['links']['collection'] = $rest_url_base . '/menu-locations/';
422
-                $rest_menu['meta']['links']['self']       = $rest_url_base . '/menu-html-location/' . $location;
423 384
 
424
-            endif;
385
+		/**
386
+		 * Get menu rendered in html for location.
387
+		 *
388
+		 * @since 1.x.0
389
+		 * @param  $request
390
+		 * @return array The menu for the corresponding location
391
+		 */
392
+		public function get_menu_html_location( $request ) {
425 393
 
426
-			return $rest_menu;
427
-        }
394
+			$params     = $request->get_params();
395
+			$location   = $params['location'];
396
+			$locations  = get_nav_menu_locations();
428 397
 
398
+			//Check if location exists and return empty array if it doesn't
399
+			if ( ! isset( $locations[ $location ] ) ) {
400
+				return array();
401
+			}
429 402
 
430
-        /**
431
-         * Returns all child nav_menu_items under a specific parent.
432
-         *
433
-         * @since   1.2.0
434
-         * @param int   $parent_id      The parent nav_menu_item ID
435
-         * @param array $nav_menu_items Navigation menu items
436
-         * @param bool  $depth          Gives all children or direct children only
437
-         * @return  array   returns filtered array of nav_menu_items
438
-         */
439
-        public function get_nav_menu_item_children( $parent_id, $nav_menu_items, $depth = true ) {
403
+			$rest_url_base  = get_rest_url() . self::get_plugin_namespace();
404
+			$wp_menu_object = get_term( $locations[$location], 'nav_menu' );
405
+			$rest_menu = array();
440 406
 
441
-            $nav_menu_item_list = array();
407
+			if ( $wp_menu_object ) :
442 408
 
443
-            foreach ( (array) $nav_menu_items as $nav_menu_item ) :
409
+				$menu = (array) $wp_menu_object;
410
+				$rest_menu['ID']          	= abs( $menu['term_id'] );
411
+				$rest_menu['name']        	= $menu['name'];
412
+				$rest_menu['slug']        	= $menu['slug'];
413
+				$rest_menu['location_slug'] = $location;
414
+				$rest_menu['description'] 	= $menu['description'];
415
+				$rest_menu['count']       	= abs( $menu['count'] );
444 416
 
445
-                if ( $nav_menu_item->menu_item_parent == $parent_id ) :
417
+				ob_start();
418
+					wp_nav_menu( array( 'theme_location' => $location ) );
419
+				$rest_menu['html']=ob_get_clean();
446 420
 
447
-                    $nav_menu_item_list[] = $this->format_menu_item( $nav_menu_item, true, $nav_menu_items );
421
+		   		$rest_menu['meta']['links']['collection'] = $rest_url_base . '/menu-locations/';
422
+				$rest_menu['meta']['links']['self']       = $rest_url_base . '/menu-html-location/' . $location;
448 423
 
449
-                    if ( $depth ) {
450
-                        if ( $children = $this->get_nav_menu_item_children( $nav_menu_item->ID, $nav_menu_items ) ) {
451
-                            $nav_menu_item_list = array_merge( $nav_menu_item_list, $children );
452
-                        }
453
-                    }
424
+			endif;
454 425
 
455
-                endif;
426
+			return $rest_menu;
427
+		}
456 428
 
457
-            endforeach;
458 429
 
459
-            return $nav_menu_item_list;
460
-        }
430
+		/**
431
+		 * Returns all child nav_menu_items under a specific parent.
432
+		 *
433
+		 * @since   1.2.0
434
+		 * @param int   $parent_id      The parent nav_menu_item ID
435
+		 * @param array $nav_menu_items Navigation menu items
436
+		 * @param bool  $depth          Gives all children or direct children only
437
+		 * @return  array   returns filtered array of nav_menu_items
438
+		 */
439
+		public function get_nav_menu_item_children( $parent_id, $nav_menu_items, $depth = true ) {
461 440
 
441
+			$nav_menu_item_list = array();
462 442
 
463
-        /**
464
-         * Format a menu item for REST API consumption.
465
-         *
466
-         * @since  1.2.0
467
-         * @param  object|array $menu_item  The menu item
468
-         * @param  bool         $children   Get menu item children (default false)
469
-         * @param  array        $menu       The menu the item belongs to (used when $children is set to true)
470
-         * @return array   a formatted menu item for REST
471
-         */
472
-        public function format_menu_item( $menu_item, $children = false, $menu = array() ) {
443
+			foreach ( (array) $nav_menu_items as $nav_menu_item ) :
473 444
 
474
-            $item = (array) $menu_item;
445
+				if ( $nav_menu_item->menu_item_parent == $parent_id ) :
475 446
 
476
-            $menu_item = array(
477
-                'id'          => abs( $item['ID'] ),
478
-                'order'       => (int) $item['menu_order'],
479
-                'parent'      => abs( $item['menu_item_parent'] ),
480
-                'title'       => $item['title'],
481
-                'url'         => $item['url'],
482
-                'attr'        => $item['attr_title'],
483
-                'target'      => $item['target'],
484
-                'classes'     => implode( ' ', $item['classes'] ),
485
-                'xfn'         => $item['xfn'],
486
-                'description' => $item['description'],
487
-                'object_id'   => abs( $item['object_id'] ),
488
-                'object'      => $item['object'],
489
-                'type'        => $item['type'],
490
-                'type_label'  => $item['type_label'],
491
-            );
447
+					$nav_menu_item_list[] = $this->format_menu_item( $nav_menu_item, true, $nav_menu_items );
492 448
 
493
-            if ( $children === true && ! empty( $menu ) ) {
494
-	            $menu_item['children'] = $this->get_nav_menu_item_children( $item['ID'], $menu );
495
-            }
449
+					if ( $depth ) {
450
+						if ( $children = $this->get_nav_menu_item_children( $nav_menu_item->ID, $nav_menu_items ) ) {
451
+							$nav_menu_item_list = array_merge( $nav_menu_item_list, $children );
452
+						}
453
+					}
496 454
 
497
-            return apply_filters( 'rest_menus_format_menu_item', $menu_item );
498
-        }
455
+				endif;
499 456
 
457
+			endforeach;
500 458
 
501
-    }
459
+			return $nav_menu_item_list;
460
+		}
461
+
462
+
463
+		/**
464
+		 * Format a menu item for REST API consumption.
465
+		 *
466
+		 * @since  1.2.0
467
+		 * @param  object|array $menu_item  The menu item
468
+		 * @param  bool         $children   Get menu item children (default false)
469
+		 * @param  array        $menu       The menu the item belongs to (used when $children is set to true)
470
+		 * @return array   a formatted menu item for REST
471
+		 */
472
+		public function format_menu_item( $menu_item, $children = false, $menu = array() ) {
473
+
474
+			$item = (array) $menu_item;
475
+
476
+			$menu_item = array(
477
+				'id'          => abs( $item['ID'] ),
478
+				'order'       => (int) $item['menu_order'],
479
+				'parent'      => abs( $item['menu_item_parent'] ),
480
+				'title'       => $item['title'],
481
+				'url'         => $item['url'],
482
+				'attr'        => $item['attr_title'],
483
+				'target'      => $item['target'],
484
+				'classes'     => implode( ' ', $item['classes'] ),
485
+				'xfn'         => $item['xfn'],
486
+				'description' => $item['description'],
487
+				'object_id'   => abs( $item['object_id'] ),
488
+				'object'      => $item['object'],
489
+				'type'        => $item['type'],
490
+				'type_label'  => $item['type_label'],
491
+			);
492
+
493
+			if ( $children === true && ! empty( $menu ) ) {
494
+				$menu_item['children'] = $this->get_nav_menu_item_children( $item['ID'], $menu );
495
+			}
496
+
497
+			return apply_filters( 'rest_menus_format_menu_item', $menu_item );
498
+		}
499
+
500
+
501
+	}
502 502
 
503 503
 
504 504
 endif;
Please login to merge, or discard this patch.
Spacing   +122 added lines, -122 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_REST_Menus' ) ) :
12
+if ( ! class_exists('WP_REST_Menus')) :
13 13
 
14 14
 
15 15
     /**
@@ -53,58 +53,58 @@  discard block
 block discarded – undo
53 53
          */
54 54
         public function register_routes() {
55 55
 
56
-            register_rest_route( self::get_plugin_namespace(), '/menus', array(
56
+            register_rest_route(self::get_plugin_namespace(), '/menus', array(
57 57
                 array(
58 58
                     'methods'  => WP_REST_Server::READABLE,
59
-                    'callback' => array( $this, 'get_menus' ),
59
+                    'callback' => array($this, 'get_menus'),
60 60
                 )
61
-            ) );
61
+            ));
62 62
 
63
-            register_rest_route( self::get_plugin_namespace(), '/menus/(?P<id>\d+)', array(
63
+            register_rest_route(self::get_plugin_namespace(), '/menus/(?P<id>\d+)', array(
64 64
                 array(
65 65
                     'methods'  => WP_REST_Server::READABLE,
66
-                    'callback' => array( $this, 'get_menu' ),
66
+                    'callback' => array($this, 'get_menu'),
67 67
                     'args'     => array(
68 68
                         'context' => array(
69 69
                         'default' => 'view',
70 70
                         ),
71 71
                     ),
72 72
                 )
73
-            ) );
73
+            ));
74 74
 
75 75
 
76
-            register_rest_route( self::get_plugin_namespace(), '/menu-html/(?P<menu_id>[a-zA-Z0-9_-]+)', array(
76
+            register_rest_route(self::get_plugin_namespace(), '/menu-html/(?P<menu_id>[a-zA-Z0-9_-]+)', array(
77 77
                 array(
78 78
                     'methods'  => WP_REST_Server::READABLE,
79
-                    'callback' => array( $this, 'get_menu_html' ),
79
+                    'callback' => array($this, 'get_menu_html'),
80 80
                     'args'     => array(
81 81
                         'context' => array(
82 82
                         'default' => 'view',
83 83
                         ),
84 84
                     ),
85 85
                 )
86
-            ) );
86
+            ));
87 87
 
88
-            register_rest_route( self::get_plugin_namespace(), '/menu-locations', array(
88
+            register_rest_route(self::get_plugin_namespace(), '/menu-locations', array(
89 89
                 array(
90 90
                     'methods'  => WP_REST_Server::READABLE,
91
-                    'callback' => array( $this, 'get_menu_locations' ),
91
+                    'callback' => array($this, 'get_menu_locations'),
92 92
                 )
93
-            ) );
93
+            ));
94 94
 
95
-            register_rest_route( self::get_plugin_namespace(), '/menu-locations/(?P<location>[a-zA-Z0-9_-]+)', array(
95
+            register_rest_route(self::get_plugin_namespace(), '/menu-locations/(?P<location>[a-zA-Z0-9_-]+)', array(
96 96
                 array(
97 97
                     'methods'  => WP_REST_Server::READABLE,
98
-                    'callback' => array( $this, 'get_menu_location' ),
98
+                    'callback' => array($this, 'get_menu_location'),
99 99
                 )
100
-            ) );
100
+            ));
101 101
 
102
-            register_rest_route( self::get_plugin_namespace(), '/menu-html-location/(?P<location>[a-zA-Z0-9_-]+)', array(
102
+            register_rest_route(self::get_plugin_namespace(), '/menu-html-location/(?P<location>[a-zA-Z0-9_-]+)', array(
103 103
                 array(
104 104
                     'methods'  => WP_REST_Server::READABLE,
105
-                    'callback' => array( $this, 'get_menu_html_location' ),
105
+                    'callback' => array($this, 'get_menu_html_location'),
106 106
                 )
107
-            ) );
107
+            ));
108 108
 
109 109
         }
110 110
 
@@ -117,31 +117,31 @@  discard block
 block discarded – undo
117 117
          */
118 118
         public static function get_menus() {
119 119
 
120
-            $rest_url = trailingslashit( get_rest_url() . self::get_plugin_namespace() . '/menus/' );
121
-            $rest_url_base = get_rest_url() . self::get_plugin_namespace();
120
+            $rest_url = trailingslashit(get_rest_url().self::get_plugin_namespace().'/menus/');
121
+            $rest_url_base = get_rest_url().self::get_plugin_namespace();
122 122
             $wp_menus = wp_get_nav_menus();
123 123
 
124 124
             $i = 0;
125 125
             $rest_menus = array();
126
-            foreach ( $wp_menus as $wp_menu ) :
126
+            foreach ($wp_menus as $wp_menu) :
127 127
 
128 128
                 $menu = (array) $wp_menu;
129 129
 
130
-                $rest_menus[ $i ]                = $menu;
131
-                $rest_menus[ $i ]['ID']          = $menu['term_id'];
132
-                $rest_menus[ $i ]['name']        = $menu['name'];
133
-                $rest_menus[ $i ]['slug']        = $menu['slug'];
134
-                $rest_menus[ $i ]['description'] = $menu['description'];
135
-                $rest_menus[ $i ]['count']       = $menu['count'];
130
+                $rest_menus[$i]                = $menu;
131
+                $rest_menus[$i]['ID']          = $menu['term_id'];
132
+                $rest_menus[$i]['name']        = $menu['name'];
133
+                $rest_menus[$i]['slug']        = $menu['slug'];
134
+                $rest_menus[$i]['description'] = $menu['description'];
135
+                $rest_menus[$i]['count']       = $menu['count'];
136 136
 
137
-                $rest_menus[ $i ]['meta']['links']['collection'] = $rest_url;
138
-                $rest_menus[ $i ]['meta']['links']['self']       = $rest_url . $menu['term_id'];
139
-                $rest_menus[ $i ]['meta']['links']['html']       = $rest_url_base . '/menu-html/' . $menu['term_id'];
137
+                $rest_menus[$i]['meta']['links']['collection'] = $rest_url;
138
+                $rest_menus[$i]['meta']['links']['self']       = $rest_url.$menu['term_id'];
139
+                $rest_menus[$i]['meta']['links']['html']       = $rest_url_base.'/menu-html/'.$menu['term_id'];
140 140
 
141
-                $i ++;
141
+                $i++;
142 142
             endforeach;
143 143
 
144
-            return apply_filters( 'rest_menus_format_menus', $rest_menus );
144
+            return apply_filters('rest_menus_format_menus', $rest_menus);
145 145
         }
146 146
 
147 147
 
@@ -152,38 +152,38 @@  discard block
 block discarded – undo
152 152
          * @param  $request
153 153
          * @return array Menu data
154 154
          */
155
-        public function get_menu( $request ) {
155
+        public function get_menu($request) {
156 156
 
157 157
             $id             = (int) $request['id'];
158
-            $rest_url       = get_rest_url() . self::get_plugin_namespace() . '/menus/';
159
-            $wp_menu_object = $id ? wp_get_nav_menu_object( $id ) : array();
160
-            $wp_menu_items  = $id ? wp_get_nav_menu_items( $id ) : array();
158
+            $rest_url       = get_rest_url().self::get_plugin_namespace().'/menus/';
159
+            $wp_menu_object = $id ? wp_get_nav_menu_object($id) : array();
160
+            $wp_menu_items  = $id ? wp_get_nav_menu_items($id) : array();
161 161
 
162 162
             $rest_menu = array();
163 163
 
164
-            if ( $wp_menu_object ) :
164
+            if ($wp_menu_object) :
165 165
 
166 166
                 $menu = (array) $wp_menu_object;
167
-                $rest_menu['ID']          = abs( $menu['term_id'] );
167
+                $rest_menu['ID']          = abs($menu['term_id']);
168 168
                 $rest_menu['name']        = $menu['name'];
169 169
                 $rest_menu['slug']        = $menu['slug'];
170 170
                 $rest_menu['description'] = $menu['description'];
171
-                $rest_menu['count']       = abs( $menu['count'] );
171
+                $rest_menu['count']       = abs($menu['count']);
172 172
 
173 173
                 $rest_menu_items = array();
174
-                foreach ( $wp_menu_items as $item_object ) {
175
-	                $rest_menu_items[] = $this->format_menu_item( $item_object );
174
+                foreach ($wp_menu_items as $item_object) {
175
+	                $rest_menu_items[] = $this->format_menu_item($item_object);
176 176
                 }
177 177
 
178 178
                 $rest_menu_items = $this->nested_menu_items($rest_menu_items, 0);
179 179
 
180 180
                 $rest_menu['items']                       = $rest_menu_items;
181 181
                 $rest_menu['meta']['links']['collection'] = $rest_url;
182
-                $rest_menu['meta']['links']['self']       = $rest_url . $id;
182
+                $rest_menu['meta']['links']['self']       = $rest_url.$id;
183 183
 
184 184
             endif;
185 185
 
186
-            return apply_filters( 'rest_menus_format_menu', $rest_menu );
186
+            return apply_filters('rest_menus_format_menu', $rest_menu);
187 187
         }
188 188
 
189 189
         /**
@@ -193,34 +193,34 @@  discard block
 block discarded – undo
193 193
          * @param  $request
194 194
          * @return array Menu data
195 195
          */
196
-        public function get_menu_html( $request ) {
196
+        public function get_menu_html($request) {
197 197
 
198 198
             $menu_id        = $request['menu_id'];
199
-            $rest_url_base  = get_rest_url() . self::get_plugin_namespace();
200
-            $wp_menu_object = $menu_id ? wp_get_nav_menu_object( $menu_id) : array();
201
-            $wp_menu_items  = $menu_id ? wp_get_nav_menu_items( $menu_id ) : array();
199
+            $rest_url_base  = get_rest_url().self::get_plugin_namespace();
200
+            $wp_menu_object = $menu_id ? wp_get_nav_menu_object($menu_id) : array();
201
+            $wp_menu_items  = $menu_id ? wp_get_nav_menu_items($menu_id) : array();
202 202
 
203 203
 			$rest_menu = array();
204 204
 
205
-            if ( $wp_menu_object ) :
205
+            if ($wp_menu_object) :
206 206
 
207 207
                 $menu = (array) $wp_menu_object;
208
-                $rest_menu['ID']          = abs( $menu['term_id'] );
208
+                $rest_menu['ID']          = abs($menu['term_id']);
209 209
                 $rest_menu['name']        = $menu['name'];
210 210
                 $rest_menu['slug']        = $menu['slug'];
211 211
                 $rest_menu['description'] = $menu['description'];
212
-                $rest_menu['count']       = abs( $menu['count'] );
212
+                $rest_menu['count']       = abs($menu['count']);
213 213
 
214 214
                 ob_start();
215
-           			wp_nav_menu( array( 'menu' => $menu_id ) );
216
-           		$rest_menu['html']=ob_get_clean();
215
+           			wp_nav_menu(array('menu' => $menu_id));
216
+           		$rest_menu['html'] = ob_get_clean();
217 217
 
218
-           		$rest_menu['meta']['links']['collection'] = $rest_url_base . '/menus/';
219
-                $rest_menu['meta']['links']['self']       = $rest_url_base . '/menu-html/' . $menu_id;
218
+           		$rest_menu['meta']['links']['collection'] = $rest_url_base.'/menus/';
219
+                $rest_menu['meta']['links']['self'] = $rest_url_base.'/menu-html/'.$menu_id;
220 220
 
221 221
             endif;
222 222
 
223
-            return apply_filters( 'rest_menus_format_menu', $rest_menu );
223
+            return apply_filters('rest_menus_format_menu', $rest_menu);
224 224
         }
225 225
 
226 226
 
@@ -236,24 +236,24 @@  discard block
 block discarded – undo
236 236
          * @param  $parent
237 237
          * @return array
238 238
          */
239
-        private function nested_menu_items( &$menu_items, $parent = null ) {
239
+        private function nested_menu_items(&$menu_items, $parent = null) {
240 240
 
241 241
             $parents = array();
242 242
             $children = array();
243 243
 
244 244
             // Separate menu_items into parents & children.
245
-            array_map( function( $i ) use ( $parent, &$children, &$parents ){
246
-                if ( $i['id'] != $parent && $i['parent'] == $parent ) {
245
+            array_map(function($i) use ($parent, &$children, &$parents){
246
+                if ($i['id'] != $parent && $i['parent'] == $parent) {
247 247
                     $parents[] = $i;
248 248
                 } else {
249 249
                     $children[] = $i;
250 250
                 }
251
-            }, $menu_items );
251
+            }, $menu_items);
252 252
 
253
-            foreach ( $parents as &$parent ) {
253
+            foreach ($parents as &$parent) {
254 254
 
255
-                if ( $this->has_children( $children, $parent['id'] ) ) {
256
-                    $parent['children'] = $this->nested_menu_items( $children, $parent['id'] );
255
+                if ($this->has_children($children, $parent['id'])) {
256
+                    $parent['children'] = $this->nested_menu_items($children, $parent['id']);
257 257
                 }
258 258
             }
259 259
 
@@ -269,8 +269,8 @@  discard block
 block discarded – undo
269 269
          * @param  int $id
270 270
          * @return array
271 271
          */
272
-        private function has_children( $items, $id ) {
273
-            return array_filter( $items, function( $i ) use ( $id ) {
272
+        private function has_children($items, $id) {
273
+            return array_filter($items, function($i) use ($id) {
274 274
                 return $i['parent'] == $id;
275 275
             } );
276 276
         }
@@ -283,28 +283,28 @@  discard block
 block discarded – undo
283 283
          * @param  $request
284 284
          * @return array All registered menus locations
285 285
          */
286
-        public static function get_menu_locations( $request ) {
286
+        public static function get_menu_locations($request) {
287 287
 
288 288
             $locations        = get_nav_menu_locations();
289 289
             $registered_menus = get_registered_nav_menus();
290
-	        $rest_url         = get_rest_url() . self::get_plugin_namespace() . '/menu-locations/';
291
-	        $rest_url_base    = get_rest_url() . self::get_plugin_namespace();
290
+	        $rest_url         = get_rest_url().self::get_plugin_namespace().'/menu-locations/';
291
+	        $rest_url_base    = get_rest_url().self::get_plugin_namespace();
292 292
             $rest_menus       = array();
293 293
 
294
-            if ( $locations && $registered_menus ) :
294
+            if ($locations && $registered_menus) :
295 295
 
296
-                foreach ( $registered_menus as $slug => $label ) :
296
+                foreach ($registered_menus as $slug => $label) :
297 297
 
298 298
 	                // Sanity check
299
-	                if ( ! isset( $locations[ $slug ] ) ) {
299
+	                if ( ! isset($locations[$slug])) {
300 300
 		                continue;
301 301
 	                }
302 302
 
303
-	                $rest_menus[ $slug ]['ID']                          = $locations[ $slug ];
304
-                    $rest_menus[ $slug ]['label']                       = $label;
305
-                    $rest_menus[ $slug ]['meta']['links']['collection'] = $rest_url;
306
-                    $rest_menus[ $slug ]['meta']['links']['self']       = $rest_url . $slug;
307
-                    $rest_menus[ $slug ]['meta']['links']['html']       = $rest_url_base . '/menu-html-location/' . $slug;
303
+	                $rest_menus[$slug]['ID'] = $locations[$slug];
304
+                    $rest_menus[$slug]['label']                       = $label;
305
+                    $rest_menus[$slug]['meta']['links']['collection'] = $rest_url;
306
+                    $rest_menus[$slug]['meta']['links']['self']       = $rest_url.$slug;
307
+                    $rest_menus[$slug]['meta']['links']['html']       = $rest_url_base.'/menu-html-location/'.$slug;
308 308
 
309 309
                 endforeach;
310 310
 
@@ -321,64 +321,64 @@  discard block
 block discarded – undo
321 321
          * @param  $request
322 322
          * @return array The menu for the corresponding location
323 323
          */
324
-        public function get_menu_location( $request ) {
324
+        public function get_menu_location($request) {
325 325
 
326 326
             $params     = $request->get_params();
327 327
             $location   = $params['location'];
328 328
             $locations  = get_nav_menu_locations();
329 329
 
330
-            if ( ! isset( $locations[ $location ] ) ) {
330
+            if ( ! isset($locations[$location])) {
331 331
 	            return array();
332 332
             }
333 333
 
334
-            $wp_menu = wp_get_nav_menu_object( $locations[ $location ] );
335
-            $menu_items = wp_get_nav_menu_items( $wp_menu->term_id );
334
+            $wp_menu = wp_get_nav_menu_object($locations[$location]);
335
+            $menu_items = wp_get_nav_menu_items($wp_menu->term_id);
336 336
 
337 337
 			/**
338 338
 			 * wp_get_nav_menu_items() outputs a list that's already sequenced correctly.
339 339
 			 * So the easiest thing to do is to reverse the list and then build our tree
340 340
 			 * from the ground up
341 341
 			 */
342
-			$rev_items = array_reverse ( $menu_items );
342
+			$rev_items = array_reverse($menu_items);
343 343
 			$rev_menu = array();
344 344
 			$cache = array();
345
-			foreach ( $rev_items as $item ) :
345
+			foreach ($rev_items as $item) :
346 346
 				$formatted = array(
347
-					'ID'          => abs( $item->ID ),
347
+					'ID'          => abs($item->ID),
348 348
 					'order'       => (int) $item->menu_order,
349
-					'parent'      => abs( $item->menu_item_parent ),
349
+					'parent'      => abs($item->menu_item_parent),
350 350
 					'title'       => $item->title,
351 351
 					'url'         => $item->url,
352 352
 					'attr'        => $item->attr_title,
353 353
 					'target'      => $item->target,
354
-					'classes'     => implode( ' ', $item->classes ),
354
+					'classes'     => implode(' ', $item->classes),
355 355
 					'xfn'         => $item->xfn,
356 356
 					'description' => $item->description,
357
-					'object_id'   => abs( $item->object_id ),
357
+					'object_id'   => abs($item->object_id),
358 358
 					'object'      => $item->object,
359 359
 					'type'        => $item->type,
360 360
 					'type_label'  => $item->type_label,
361 361
 					'children'    => array(),
362 362
 				);
363 363
 				// Pickup my children
364
-				if ( array_key_exists ( $item->ID , $cache ) ) {
365
-					$formatted['children'] = array_reverse ( $cache[ $item->ID ] );
364
+				if (array_key_exists($item->ID, $cache)) {
365
+					$formatted['children'] = array_reverse($cache[$item->ID]);
366 366
 				}
367 367
 
368
-            	$formatted = apply_filters( 'rest_menus_format_menu_item', $formatted );
368
+            	$formatted = apply_filters('rest_menus_format_menu_item', $formatted);
369 369
 
370
-				if ( $item->menu_item_parent != 0 ) {
370
+				if ($item->menu_item_parent != 0) {
371 371
 					// Wait for parent to pick me up
372
-					if ( array_key_exists ( $item->menu_item_parent , $cache ) ) {
373
-						array_push( $cache[ $item->menu_item_parent ], $formatted );
372
+					if (array_key_exists($item->menu_item_parent, $cache)) {
373
+						array_push($cache[$item->menu_item_parent], $formatted);
374 374
 					} else {
375
-						$cache[ $item->menu_item_parent ] = array( $formatted, );
375
+						$cache[$item->menu_item_parent] = array($formatted,);
376 376
 					}
377 377
 				} else {
378
-					array_push( $rev_menu, $formatted );
378
+					array_push($rev_menu, $formatted);
379 379
 				}
380 380
 			endforeach;
381
-			return array_reverse ( $rev_menu );
381
+			return array_reverse($rev_menu);
382 382
         }
383 383
 
384 384
 
@@ -389,37 +389,37 @@  discard block
 block discarded – undo
389 389
          * @param  $request
390 390
          * @return array The menu for the corresponding location
391 391
          */
392
-        public function get_menu_html_location( $request ) {
392
+        public function get_menu_html_location($request) {
393 393
 
394 394
             $params     = $request->get_params();
395 395
             $location   = $params['location'];
396 396
             $locations  = get_nav_menu_locations();
397 397
 
398 398
             //Check if location exists and return empty array if it doesn't
399
-            if ( ! isset( $locations[ $location ] ) ) {
399
+            if ( ! isset($locations[$location])) {
400 400
 	            return array();
401 401
             }
402 402
 
403
-            $rest_url_base  = get_rest_url() . self::get_plugin_namespace();
404
-            $wp_menu_object = get_term( $locations[$location], 'nav_menu' );
403
+            $rest_url_base  = get_rest_url().self::get_plugin_namespace();
404
+            $wp_menu_object = get_term($locations[$location], 'nav_menu');
405 405
             $rest_menu = array();
406 406
 
407
-            if ( $wp_menu_object ) :
407
+            if ($wp_menu_object) :
408 408
 
409 409
                 $menu = (array) $wp_menu_object;
410
-                $rest_menu['ID']          	= abs( $menu['term_id'] );
410
+                $rest_menu['ID']          	= abs($menu['term_id']);
411 411
                 $rest_menu['name']        	= $menu['name'];
412 412
                 $rest_menu['slug']        	= $menu['slug'];
413 413
                 $rest_menu['location_slug'] = $location;
414 414
                 $rest_menu['description'] 	= $menu['description'];
415
-                $rest_menu['count']       	= abs( $menu['count'] );
415
+                $rest_menu['count']       	= abs($menu['count']);
416 416
 
417 417
                 ob_start();
418
-					wp_nav_menu( array( 'theme_location' => $location ) );
419
-				$rest_menu['html']=ob_get_clean();
418
+					wp_nav_menu(array('theme_location' => $location));
419
+				$rest_menu['html'] = ob_get_clean();
420 420
 
421
-           		$rest_menu['meta']['links']['collection'] = $rest_url_base . '/menu-locations/';
422
-                $rest_menu['meta']['links']['self']       = $rest_url_base . '/menu-html-location/' . $location;
421
+           		$rest_menu['meta']['links']['collection'] = $rest_url_base.'/menu-locations/';
422
+                $rest_menu['meta']['links']['self'] = $rest_url_base.'/menu-html-location/'.$location;
423 423
 
424 424
             endif;
425 425
 
@@ -436,19 +436,19 @@  discard block
 block discarded – undo
436 436
          * @param bool  $depth          Gives all children or direct children only
437 437
          * @return  array   returns filtered array of nav_menu_items
438 438
          */
439
-        public function get_nav_menu_item_children( $parent_id, $nav_menu_items, $depth = true ) {
439
+        public function get_nav_menu_item_children($parent_id, $nav_menu_items, $depth = true) {
440 440
 
441 441
             $nav_menu_item_list = array();
442 442
 
443
-            foreach ( (array) $nav_menu_items as $nav_menu_item ) :
443
+            foreach ((array) $nav_menu_items as $nav_menu_item) :
444 444
 
445
-                if ( $nav_menu_item->menu_item_parent == $parent_id ) :
445
+                if ($nav_menu_item->menu_item_parent == $parent_id) :
446 446
 
447
-                    $nav_menu_item_list[] = $this->format_menu_item( $nav_menu_item, true, $nav_menu_items );
447
+                    $nav_menu_item_list[] = $this->format_menu_item($nav_menu_item, true, $nav_menu_items);
448 448
 
449
-                    if ( $depth ) {
450
-                        if ( $children = $this->get_nav_menu_item_children( $nav_menu_item->ID, $nav_menu_items ) ) {
451
-                            $nav_menu_item_list = array_merge( $nav_menu_item_list, $children );
449
+                    if ($depth) {
450
+                        if ($children = $this->get_nav_menu_item_children($nav_menu_item->ID, $nav_menu_items)) {
451
+                            $nav_menu_item_list = array_merge($nav_menu_item_list, $children);
452 452
                         }
453 453
                     }
454 454
 
@@ -469,32 +469,32 @@  discard block
 block discarded – undo
469 469
          * @param  array        $menu       The menu the item belongs to (used when $children is set to true)
470 470
          * @return array   a formatted menu item for REST
471 471
          */
472
-        public function format_menu_item( $menu_item, $children = false, $menu = array() ) {
472
+        public function format_menu_item($menu_item, $children = false, $menu = array()) {
473 473
 
474 474
             $item = (array) $menu_item;
475 475
 
476 476
             $menu_item = array(
477
-                'id'          => abs( $item['ID'] ),
477
+                'id'          => abs($item['ID']),
478 478
                 'order'       => (int) $item['menu_order'],
479
-                'parent'      => abs( $item['menu_item_parent'] ),
479
+                'parent'      => abs($item['menu_item_parent']),
480 480
                 'title'       => $item['title'],
481 481
                 'url'         => $item['url'],
482 482
                 'attr'        => $item['attr_title'],
483 483
                 'target'      => $item['target'],
484
-                'classes'     => implode( ' ', $item['classes'] ),
484
+                'classes'     => implode(' ', $item['classes']),
485 485
                 'xfn'         => $item['xfn'],
486 486
                 'description' => $item['description'],
487
-                'object_id'   => abs( $item['object_id'] ),
487
+                'object_id'   => abs($item['object_id']),
488 488
                 'object'      => $item['object'],
489 489
                 'type'        => $item['type'],
490 490
                 'type_label'  => $item['type_label'],
491 491
             );
492 492
 
493
-            if ( $children === true && ! empty( $menu ) ) {
494
-	            $menu_item['children'] = $this->get_nav_menu_item_children( $item['ID'], $menu );
493
+            if ($children === true && ! empty($menu)) {
494
+	            $menu_item['children'] = $this->get_nav_menu_item_children($item['ID'], $menu);
495 495
             }
496 496
 
497
-            return apply_filters( 'rest_menus_format_menu_item', $menu_item );
497
+            return apply_filters('rest_menus_format_menu_item', $menu_item);
498 498
         }
499 499
 
500 500
 
Please login to merge, or discard this patch.