Completed
Pull Request — master (#49)
by Duan
01:27
created
includes/wp-api-menus-v2.php 3 patches
Doc Comments   +1 added lines, -1 removed lines patch added patch discarded remove patch
@@ -173,7 +173,7 @@
 block discarded – undo
173 173
          *
174 174
          * @since  1.2.0
175 175
          * @param  $menu_items
176
-         * @param  $parent
176
+         * @param  integer $parent
177 177
          * @return array
178 178
          */
179 179
         private function nested_menu_items( &$menu_items, $parent = null ) {
Please login to merge, or discard this patch.
Indentation   +338 added lines, -338 removed lines patch added patch discarded remove patch
@@ -6,269 +6,269 @@  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
-         */
53
-        public function register_routes() {
54
-
55
-            register_rest_route( self::get_plugin_namespace(), '/menus', array(
56
-                array(
57
-                    'methods'  => WP_REST_Server::READABLE,
58
-                    'callback' => array( $this, 'get_menus' ),
59
-                )
60
-            ) );
61
-
62
-            register_rest_route( self::get_plugin_namespace(), '/menus/(?P<id>\d+)', array(
63
-                array(
64
-                    'methods'  => WP_REST_Server::READABLE,
65
-                    'callback' => array( $this, 'get_menu' ),
66
-                    'args'     => array(
67
-                        'context' => array(
68
-                        'default' => 'view',
69
-                        ),
70
-                    ),
71
-                )
72
-            ) );
73
-
74
-            register_rest_route( self::get_plugin_namespace(), '/menu-locations', array(
75
-                array(
76
-                    'methods'  => WP_REST_Server::READABLE,
77
-                    'callback' => array( $this, 'get_menu_locations' ),
78
-                )
79
-            ) );
80
-
81
-            register_rest_route( self::get_plugin_namespace(), '/menu-locations/(?P<location>[a-zA-Z0-9_-]+)', array(
82
-                array(
83
-                    'methods'  => WP_REST_Server::READABLE,
84
-                    'callback' => array( $this, 'get_menu_location' ),
85
-                )
86
-            ) );
87
-        }
88
-
89
-
90
-        /**
91
-         * Get menus.
92
-         *
93
-         * @since  1.2.0
94
-         * @return array All registered menus
95
-         */
96
-        public static function get_menus() {
97
-
98
-            $rest_url = trailingslashit( get_rest_url() . self::get_plugin_namespace() . '/menus/' );
99
-            $wp_menus = wp_get_nav_menus();
100
-
101
-            $i = 0;
102
-            $rest_menus = array();
103
-            foreach ( $wp_menus as $wp_menu ) :
104
-
105
-                $menu = (array) $wp_menu;
106
-
107
-                $rest_menus[ $i ]                = $menu;
108
-                $rest_menus[ $i ]['ID']          = $menu['term_id'];
109
-                $rest_menus[ $i ]['name']        = $menu['name'];
110
-                $rest_menus[ $i ]['slug']        = $menu['slug'];
111
-                $rest_menus[ $i ]['description'] = $menu['description'];
112
-                $rest_menus[ $i ]['count']       = $menu['count'];
113
-
114
-                $rest_menus[ $i ]['meta']['links']['collection'] = $rest_url;
115
-                $rest_menus[ $i ]['meta']['links']['self']       = $rest_url . $menu['term_id'];
116
-
117
-                $i ++;
118
-            endforeach;
119
-
120
-            return apply_filters( 'rest_menus_format_menus', $rest_menus );
121
-        }
122
-
123
-
124
-        /**
125
-         * Get a menu.
126
-         *
127
-         * @since  1.2.0
128
-         * @param  $request
129
-         * @return array Menu data
130
-         */
131
-        public function get_menu( $request ) {
132
-
133
-            $id             = (int) $request['id'];
134
-            $rest_url       = get_rest_url() . self::get_api_namespace() . '/menus/';
135
-            $wp_menu_object = $id ? wp_get_nav_menu_object( $id ) : array();
136
-            $wp_menu_items  = $id ? wp_get_nav_menu_items( $id ) : array();
137
-
138
-            $rest_menu = array();
139
-
140
-            if ( $wp_menu_object ) :
141
-
142
-                $menu = (array) $wp_menu_object;
143
-                $rest_menu['ID']          = abs( $menu['term_id'] );
144
-                $rest_menu['name']        = $menu['name'];
145
-                $rest_menu['slug']        = $menu['slug'];
146
-                $rest_menu['description'] = $menu['description'];
147
-                $rest_menu['count']       = abs( $menu['count'] );
148
-
149
-                $rest_menu_items = array();
150
-                foreach ( $wp_menu_items as $item_object ) {
151
-	                $rest_menu_items[] = $this->format_menu_item( $item_object );
152
-                }
153
-
154
-                $rest_menu_items = $this->nested_menu_items($rest_menu_items, 0);
155
-
156
-                $rest_menu['items']                       = $rest_menu_items;
157
-                $rest_menu['meta']['links']['collection'] = $rest_url;
158
-                $rest_menu['meta']['links']['self']       = $rest_url . $id;
159
-
160
-            endif;
161
-
162
-            return apply_filters( 'rest_menus_format_menu', $rest_menu );
163
-        }
164
-
165
-
166
-        /**
167
-         * Handle nested menu items.
168
-         *
169
-         * Given a flat array of menu items, split them into parent/child items
170
-         * and recurse over them to return children nested in their parent.
171
-         *
172
-         * @since  1.2.0
173
-         * @param  $menu_items
174
-         * @param  $parent
175
-         * @return array
176
-         */
177
-        private function nested_menu_items( &$menu_items, $parent = null ) {
178
-
179
-            $parents = array();
180
-            $children = array();
181
-
182
-            // Separate menu_items into parents & children.
183
-            array_map( function( $i ) use ( $parent, &$children, &$parents ){
184
-                if ( $i['id'] != $parent && $i['parent'] == $parent ) {
185
-                    $parents[] = $i;
186
-                } else {
187
-                    $children[] = $i;
188
-                }
189
-            }, $menu_items );
190
-
191
-            foreach ( $parents as &$parent ) {
192
-
193
-                if ( $this->has_children( $children, $parent['id'] ) ) {
194
-                    $parent['children'] = $this->nested_menu_items( $children, $parent['id'] );
195
-                }
196
-            }
197
-
198
-            return $parents;
199
-        }
200
-
201
-
202
-        /**
203
-         * Check if a collection of menu items contains an item that is the parent id of 'id'.
204
-         *
205
-         * @since  1.2.0
206
-         * @param  array $items
207
-         * @param  int $id
208
-         * @return array
209
-         */
210
-        private function has_children( $items, $id ) {
211
-            return array_filter( $items, function( $i ) use ( $id ) {
212
-                return $i['parent'] == $id;
213
-            } );
214
-        }
215
-
216
-
217
-        /**
218
-         * Get menu locations.
219
-         *
220
-         * @since 1.2.0
221
-         * @param  $request
222
-         * @return array All registered menus locations
223
-         */
224
-        public static function get_menu_locations( $request ) {
225
-
226
-            $locations        = get_nav_menu_locations();
227
-            $registered_menus = get_registered_nav_menus();
228
-	        $rest_url         = get_rest_url() . self::get_api_namespace() . '/menu-locations/';
229
-            $rest_menus       = array();
230
-
231
-            if ( $locations && $registered_menus ) :
232
-
233
-                foreach ( $registered_menus as $slug => $label ) :
234
-
235
-	                // Sanity check
236
-	                if ( ! isset( $locations[ $slug ] ) ) {
237
-		                continue;
238
-	                }
239
-
240
-	                $rest_menus[ $slug ]['ID']                          = $locations[ $slug ];
241
-                    $rest_menus[ $slug ]['label']                       = $label;
242
-                    $rest_menus[ $slug ]['meta']['links']['collection'] = $rest_url;
243
-                    $rest_menus[ $slug ]['meta']['links']['self']       = $rest_url . $slug;
244
-
245
-                endforeach;
246
-
247
-            endif;
248
-
249
-            return $rest_menus;
250
-        }
251
-
252
-
253
-        /**
254
-         * Get menu for location.
255
-         *
256
-         * @since 1.2.0
257
-         * @param  $request
258
-         * @return array The menu for the corresponding location
259
-         */
260
-        public function get_menu_location( $request ) {
261
-
262
-            $params     = $request->get_params();
263
-            $location   = $params['location'];
264
-            $locations  = get_nav_menu_locations();
265
-
266
-            if ( ! isset( $locations[ $location ] ) ) {
267
-	            return array();
268
-            }
269
-
270
-            $wp_menu = wp_get_nav_menu_object( $locations[ $location ] );
271
-            $menu_items = wp_get_nav_menu_items( $wp_menu->term_id );
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
+		 */
53
+		public function register_routes() {
54
+
55
+			register_rest_route( self::get_plugin_namespace(), '/menus', array(
56
+				array(
57
+					'methods'  => WP_REST_Server::READABLE,
58
+					'callback' => array( $this, 'get_menus' ),
59
+				)
60
+			) );
61
+
62
+			register_rest_route( self::get_plugin_namespace(), '/menus/(?P<id>\d+)', array(
63
+				array(
64
+					'methods'  => WP_REST_Server::READABLE,
65
+					'callback' => array( $this, 'get_menu' ),
66
+					'args'     => array(
67
+						'context' => array(
68
+						'default' => 'view',
69
+						),
70
+					),
71
+				)
72
+			) );
73
+
74
+			register_rest_route( self::get_plugin_namespace(), '/menu-locations', array(
75
+				array(
76
+					'methods'  => WP_REST_Server::READABLE,
77
+					'callback' => array( $this, 'get_menu_locations' ),
78
+				)
79
+			) );
80
+
81
+			register_rest_route( self::get_plugin_namespace(), '/menu-locations/(?P<location>[a-zA-Z0-9_-]+)', array(
82
+				array(
83
+					'methods'  => WP_REST_Server::READABLE,
84
+					'callback' => array( $this, 'get_menu_location' ),
85
+				)
86
+			) );
87
+		}
88
+
89
+
90
+		/**
91
+		 * Get menus.
92
+		 *
93
+		 * @since  1.2.0
94
+		 * @return array All registered menus
95
+		 */
96
+		public static function get_menus() {
97
+
98
+			$rest_url = trailingslashit( get_rest_url() . self::get_plugin_namespace() . '/menus/' );
99
+			$wp_menus = wp_get_nav_menus();
100
+
101
+			$i = 0;
102
+			$rest_menus = array();
103
+			foreach ( $wp_menus as $wp_menu ) :
104
+
105
+				$menu = (array) $wp_menu;
106
+
107
+				$rest_menus[ $i ]                = $menu;
108
+				$rest_menus[ $i ]['ID']          = $menu['term_id'];
109
+				$rest_menus[ $i ]['name']        = $menu['name'];
110
+				$rest_menus[ $i ]['slug']        = $menu['slug'];
111
+				$rest_menus[ $i ]['description'] = $menu['description'];
112
+				$rest_menus[ $i ]['count']       = $menu['count'];
113
+
114
+				$rest_menus[ $i ]['meta']['links']['collection'] = $rest_url;
115
+				$rest_menus[ $i ]['meta']['links']['self']       = $rest_url . $menu['term_id'];
116
+
117
+				$i ++;
118
+			endforeach;
119
+
120
+			return apply_filters( 'rest_menus_format_menus', $rest_menus );
121
+		}
122
+
123
+
124
+		/**
125
+		 * Get a menu.
126
+		 *
127
+		 * @since  1.2.0
128
+		 * @param  $request
129
+		 * @return array Menu data
130
+		 */
131
+		public function get_menu( $request ) {
132
+
133
+			$id             = (int) $request['id'];
134
+			$rest_url       = get_rest_url() . self::get_api_namespace() . '/menus/';
135
+			$wp_menu_object = $id ? wp_get_nav_menu_object( $id ) : array();
136
+			$wp_menu_items  = $id ? wp_get_nav_menu_items( $id ) : array();
137
+
138
+			$rest_menu = array();
139
+
140
+			if ( $wp_menu_object ) :
141
+
142
+				$menu = (array) $wp_menu_object;
143
+				$rest_menu['ID']          = abs( $menu['term_id'] );
144
+				$rest_menu['name']        = $menu['name'];
145
+				$rest_menu['slug']        = $menu['slug'];
146
+				$rest_menu['description'] = $menu['description'];
147
+				$rest_menu['count']       = abs( $menu['count'] );
148
+
149
+				$rest_menu_items = array();
150
+				foreach ( $wp_menu_items as $item_object ) {
151
+					$rest_menu_items[] = $this->format_menu_item( $item_object );
152
+				}
153
+
154
+				$rest_menu_items = $this->nested_menu_items($rest_menu_items, 0);
155
+
156
+				$rest_menu['items']                       = $rest_menu_items;
157
+				$rest_menu['meta']['links']['collection'] = $rest_url;
158
+				$rest_menu['meta']['links']['self']       = $rest_url . $id;
159
+
160
+			endif;
161
+
162
+			return apply_filters( 'rest_menus_format_menu', $rest_menu );
163
+		}
164
+
165
+
166
+		/**
167
+		 * Handle nested menu items.
168
+		 *
169
+		 * Given a flat array of menu items, split them into parent/child items
170
+		 * and recurse over them to return children nested in their parent.
171
+		 *
172
+		 * @since  1.2.0
173
+		 * @param  $menu_items
174
+		 * @param  $parent
175
+		 * @return array
176
+		 */
177
+		private function nested_menu_items( &$menu_items, $parent = null ) {
178
+
179
+			$parents = array();
180
+			$children = array();
181
+
182
+			// Separate menu_items into parents & children.
183
+			array_map( function( $i ) use ( $parent, &$children, &$parents ){
184
+				if ( $i['id'] != $parent && $i['parent'] == $parent ) {
185
+					$parents[] = $i;
186
+				} else {
187
+					$children[] = $i;
188
+				}
189
+			}, $menu_items );
190
+
191
+			foreach ( $parents as &$parent ) {
192
+
193
+				if ( $this->has_children( $children, $parent['id'] ) ) {
194
+					$parent['children'] = $this->nested_menu_items( $children, $parent['id'] );
195
+				}
196
+			}
197
+
198
+			return $parents;
199
+		}
200
+
201
+
202
+		/**
203
+		 * Check if a collection of menu items contains an item that is the parent id of 'id'.
204
+		 *
205
+		 * @since  1.2.0
206
+		 * @param  array $items
207
+		 * @param  int $id
208
+		 * @return array
209
+		 */
210
+		private function has_children( $items, $id ) {
211
+			return array_filter( $items, function( $i ) use ( $id ) {
212
+				return $i['parent'] == $id;
213
+			} );
214
+		}
215
+
216
+
217
+		/**
218
+		 * Get menu locations.
219
+		 *
220
+		 * @since 1.2.0
221
+		 * @param  $request
222
+		 * @return array All registered menus locations
223
+		 */
224
+		public static function get_menu_locations( $request ) {
225
+
226
+			$locations        = get_nav_menu_locations();
227
+			$registered_menus = get_registered_nav_menus();
228
+			$rest_url         = get_rest_url() . self::get_api_namespace() . '/menu-locations/';
229
+			$rest_menus       = array();
230
+
231
+			if ( $locations && $registered_menus ) :
232
+
233
+				foreach ( $registered_menus as $slug => $label ) :
234
+
235
+					// Sanity check
236
+					if ( ! isset( $locations[ $slug ] ) ) {
237
+						continue;
238
+					}
239
+
240
+					$rest_menus[ $slug ]['ID']                          = $locations[ $slug ];
241
+					$rest_menus[ $slug ]['label']                       = $label;
242
+					$rest_menus[ $slug ]['meta']['links']['collection'] = $rest_url;
243
+					$rest_menus[ $slug ]['meta']['links']['self']       = $rest_url . $slug;
244
+
245
+				endforeach;
246
+
247
+			endif;
248
+
249
+			return $rest_menus;
250
+		}
251
+
252
+
253
+		/**
254
+		 * Get menu for location.
255
+		 *
256
+		 * @since 1.2.0
257
+		 * @param  $request
258
+		 * @return array The menu for the corresponding location
259
+		 */
260
+		public function get_menu_location( $request ) {
261
+
262
+			$params     = $request->get_params();
263
+			$location   = $params['location'];
264
+			$locations  = get_nav_menu_locations();
265
+
266
+			if ( ! isset( $locations[ $location ] ) ) {
267
+				return array();
268
+			}
269
+
270
+			$wp_menu = wp_get_nav_menu_object( $locations[ $location ] );
271
+			$menu_items = wp_get_nav_menu_items( $wp_menu->term_id );
272 272
 
273 273
 			/**
274 274
 			 * wp_get_nav_menu_items() outputs a list that's already sequenced correctly.
@@ -300,16 +300,16 @@  discard block
 block discarded – undo
300 300
 					'children'    => array(),
301 301
 				);
302 302
 
303
-                if ( $formatted['object'] == 'category' ) {
303
+				if ( $formatted['object'] == 'category' ) {
304 304
 
305
-                    $formatted['object_slug'] = get_category($item->object_id)->slug;
306
-                }
305
+					$formatted['object_slug'] = get_category($item->object_id)->slug;
306
+				}
307 307
 			
308 308
 				if ( array_key_exists( $item->ID , $cache ) ) {
309 309
 					$formatted['children'] = array_reverse( $cache[ $item->ID ] );
310 310
 				}
311 311
 
312
-            	$formatted = apply_filters( 'rest_menus_format_menu_item', $formatted );
312
+				$formatted = apply_filters( 'rest_menus_format_menu_item', $formatted );
313 313
 
314 314
 				if ( $item->menu_item_parent != 0 ) {
315 315
 
@@ -327,82 +327,82 @@  discard block
 block discarded – undo
327 327
 			endforeach;
328 328
 
329 329
 			return array_reverse ( $rev_menu );
330
-        }
331
-
332
-
333
-        /**
334
-         * Returns all child nav_menu_items under a specific parent.
335
-         *
336
-         * @since   1.2.0
337
-         * @param int   $parent_id      The parent nav_menu_item ID
338
-         * @param array $nav_menu_items Navigation menu items
339
-         * @param bool  $depth          Gives all children or direct children only
340
-         * @return array	returns filtered array of nav_menu_items
341
-         */
342
-        public function get_nav_menu_item_children( $parent_id, $nav_menu_items, $depth = true ) {
343
-
344
-            $nav_menu_item_list = array();
345
-
346
-            foreach ( (array) $nav_menu_items as $nav_menu_item ) :
347
-
348
-                if ( $nav_menu_item->menu_item_parent == $parent_id ) :
349
-
350
-                    $nav_menu_item_list[] = $this->format_menu_item( $nav_menu_item, true, $nav_menu_items );
351
-
352
-                    if ( $depth ) {
353
-                        if ( $children = $this->get_nav_menu_item_children( $nav_menu_item->ID, $nav_menu_items ) ) {
354
-                            $nav_menu_item_list = array_merge( $nav_menu_item_list, $children );
355
-                        }
356
-                    }
357
-
358
-                endif;
359
-
360
-            endforeach;
361
-
362
-            return $nav_menu_item_list;
363
-        }
364
-
365
-
366
-        /**
367
-         * Format a menu item for REST API consumption.
368
-         *
369
-         * @since  1.2.0
370
-         * @param  object|array $menu_item  The menu item
371
-         * @param  bool         $children   Get menu item children (default false)
372
-         * @param  array        $menu       The menu the item belongs to (used when $children is set to true)
373
-         * @return array	a formatted menu item for REST
374
-         */
375
-        public function format_menu_item( $menu_item, $children = false, $menu = array() ) {
376
-
377
-            $item = (array) $menu_item;
378
-
379
-            $menu_item = array(
380
-                'id'          => abs( $item['ID'] ),
381
-                'order'       => (int) $item['menu_order'],
382
-                'parent'      => abs( $item['menu_item_parent'] ),
383
-                'title'       => $item['title'],
384
-                'url'         => $item['url'],
385
-                'attr'        => $item['attr_title'],
386
-                'target'      => $item['target'],
387
-                'classes'     => implode( ' ', $item['classes'] ),
388
-                'xfn'         => $item['xfn'],
389
-                'description' => $item['description'],
390
-                'object_id'   => abs( $item['object_id'] ),
391
-                'object'      => $item['object'],
392
-                'object_slug' => get_post( $item['object_id'] )->post_name,
393
-                'type'        => $item['type'],
394
-                'type_label'  => $item['type_label'],
395
-            );
396
-
397
-            if ( $children === true && ! empty( $menu ) ) {
398
-	            $menu_item['children'] = $this->get_nav_menu_item_children( $item['ID'], $menu );
399
-            }
400
-
401
-            return apply_filters( 'rest_menus_format_menu_item', $menu_item );
402
-        }
403
-
404
-
405
-    }
330
+		}
331
+
332
+
333
+		/**
334
+		 * Returns all child nav_menu_items under a specific parent.
335
+		 *
336
+		 * @since   1.2.0
337
+		 * @param int   $parent_id      The parent nav_menu_item ID
338
+		 * @param array $nav_menu_items Navigation menu items
339
+		 * @param bool  $depth          Gives all children or direct children only
340
+		 * @return array	returns filtered array of nav_menu_items
341
+		 */
342
+		public function get_nav_menu_item_children( $parent_id, $nav_menu_items, $depth = true ) {
343
+
344
+			$nav_menu_item_list = array();
345
+
346
+			foreach ( (array) $nav_menu_items as $nav_menu_item ) :
347
+
348
+				if ( $nav_menu_item->menu_item_parent == $parent_id ) :
349
+
350
+					$nav_menu_item_list[] = $this->format_menu_item( $nav_menu_item, true, $nav_menu_items );
351
+
352
+					if ( $depth ) {
353
+						if ( $children = $this->get_nav_menu_item_children( $nav_menu_item->ID, $nav_menu_items ) ) {
354
+							$nav_menu_item_list = array_merge( $nav_menu_item_list, $children );
355
+						}
356
+					}
357
+
358
+				endif;
359
+
360
+			endforeach;
361
+
362
+			return $nav_menu_item_list;
363
+		}
364
+
365
+
366
+		/**
367
+		 * Format a menu item for REST API consumption.
368
+		 *
369
+		 * @since  1.2.0
370
+		 * @param  object|array $menu_item  The menu item
371
+		 * @param  bool         $children   Get menu item children (default false)
372
+		 * @param  array        $menu       The menu the item belongs to (used when $children is set to true)
373
+		 * @return array	a formatted menu item for REST
374
+		 */
375
+		public function format_menu_item( $menu_item, $children = false, $menu = array() ) {
376
+
377
+			$item = (array) $menu_item;
378
+
379
+			$menu_item = array(
380
+				'id'          => abs( $item['ID'] ),
381
+				'order'       => (int) $item['menu_order'],
382
+				'parent'      => abs( $item['menu_item_parent'] ),
383
+				'title'       => $item['title'],
384
+				'url'         => $item['url'],
385
+				'attr'        => $item['attr_title'],
386
+				'target'      => $item['target'],
387
+				'classes'     => implode( ' ', $item['classes'] ),
388
+				'xfn'         => $item['xfn'],
389
+				'description' => $item['description'],
390
+				'object_id'   => abs( $item['object_id'] ),
391
+				'object'      => $item['object'],
392
+				'object_slug' => get_post( $item['object_id'] )->post_name,
393
+				'type'        => $item['type'],
394
+				'type_label'  => $item['type_label'],
395
+			);
396
+
397
+			if ( $children === true && ! empty( $menu ) ) {
398
+				$menu_item['children'] = $this->get_nav_menu_item_children( $item['ID'], $menu );
399
+			}
400
+
401
+			return apply_filters( 'rest_menus_format_menu_item', $menu_item );
402
+		}
403
+
404
+
405
+	}
406 406
 
407 407
 
408 408
 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_REST_Menus' ) ) :
12
+if ( ! class_exists('WP_REST_Menus')) :
13 13
 
14 14
 
15 15
     /**
@@ -52,38 +52,38 @@  discard block
 block discarded – undo
52 52
          */
53 53
         public function register_routes() {
54 54
 
55
-            register_rest_route( self::get_plugin_namespace(), '/menus', array(
55
+            register_rest_route(self::get_plugin_namespace(), '/menus', array(
56 56
                 array(
57 57
                     'methods'  => WP_REST_Server::READABLE,
58
-                    'callback' => array( $this, 'get_menus' ),
58
+                    'callback' => array($this, 'get_menus'),
59 59
                 )
60
-            ) );
60
+            ));
61 61
 
62
-            register_rest_route( self::get_plugin_namespace(), '/menus/(?P<id>\d+)', array(
62
+            register_rest_route(self::get_plugin_namespace(), '/menus/(?P<id>\d+)', array(
63 63
                 array(
64 64
                     'methods'  => WP_REST_Server::READABLE,
65
-                    'callback' => array( $this, 'get_menu' ),
65
+                    'callback' => array($this, 'get_menu'),
66 66
                     'args'     => array(
67 67
                         'context' => array(
68 68
                         'default' => 'view',
69 69
                         ),
70 70
                     ),
71 71
                 )
72
-            ) );
72
+            ));
73 73
 
74
-            register_rest_route( self::get_plugin_namespace(), '/menu-locations', array(
74
+            register_rest_route(self::get_plugin_namespace(), '/menu-locations', array(
75 75
                 array(
76 76
                     'methods'  => WP_REST_Server::READABLE,
77
-                    'callback' => array( $this, 'get_menu_locations' ),
77
+                    'callback' => array($this, 'get_menu_locations'),
78 78
                 )
79
-            ) );
79
+            ));
80 80
 
81
-            register_rest_route( self::get_plugin_namespace(), '/menu-locations/(?P<location>[a-zA-Z0-9_-]+)', array(
81
+            register_rest_route(self::get_plugin_namespace(), '/menu-locations/(?P<location>[a-zA-Z0-9_-]+)', array(
82 82
                 array(
83 83
                     'methods'  => WP_REST_Server::READABLE,
84
-                    'callback' => array( $this, 'get_menu_location' ),
84
+                    'callback' => array($this, 'get_menu_location'),
85 85
                 )
86
-            ) );
86
+            ));
87 87
         }
88 88
 
89 89
 
@@ -95,29 +95,29 @@  discard block
 block discarded – undo
95 95
          */
96 96
         public static function get_menus() {
97 97
 
98
-            $rest_url = trailingslashit( get_rest_url() . self::get_plugin_namespace() . '/menus/' );
98
+            $rest_url = trailingslashit(get_rest_url().self::get_plugin_namespace().'/menus/');
99 99
             $wp_menus = wp_get_nav_menus();
100 100
 
101 101
             $i = 0;
102 102
             $rest_menus = array();
103
-            foreach ( $wp_menus as $wp_menu ) :
103
+            foreach ($wp_menus as $wp_menu) :
104 104
 
105 105
                 $menu = (array) $wp_menu;
106 106
 
107
-                $rest_menus[ $i ]                = $menu;
108
-                $rest_menus[ $i ]['ID']          = $menu['term_id'];
109
-                $rest_menus[ $i ]['name']        = $menu['name'];
110
-                $rest_menus[ $i ]['slug']        = $menu['slug'];
111
-                $rest_menus[ $i ]['description'] = $menu['description'];
112
-                $rest_menus[ $i ]['count']       = $menu['count'];
107
+                $rest_menus[$i]                = $menu;
108
+                $rest_menus[$i]['ID']          = $menu['term_id'];
109
+                $rest_menus[$i]['name']        = $menu['name'];
110
+                $rest_menus[$i]['slug']        = $menu['slug'];
111
+                $rest_menus[$i]['description'] = $menu['description'];
112
+                $rest_menus[$i]['count']       = $menu['count'];
113 113
 
114
-                $rest_menus[ $i ]['meta']['links']['collection'] = $rest_url;
115
-                $rest_menus[ $i ]['meta']['links']['self']       = $rest_url . $menu['term_id'];
114
+                $rest_menus[$i]['meta']['links']['collection'] = $rest_url;
115
+                $rest_menus[$i]['meta']['links']['self']       = $rest_url.$menu['term_id'];
116 116
 
117
-                $i ++;
117
+                $i++;
118 118
             endforeach;
119 119
 
120
-            return apply_filters( 'rest_menus_format_menus', $rest_menus );
120
+            return apply_filters('rest_menus_format_menus', $rest_menus);
121 121
         }
122 122
 
123 123
 
@@ -128,38 +128,38 @@  discard block
 block discarded – undo
128 128
          * @param  $request
129 129
          * @return array Menu data
130 130
          */
131
-        public function get_menu( $request ) {
131
+        public function get_menu($request) {
132 132
 
133 133
             $id             = (int) $request['id'];
134
-            $rest_url       = get_rest_url() . self::get_api_namespace() . '/menus/';
135
-            $wp_menu_object = $id ? wp_get_nav_menu_object( $id ) : array();
136
-            $wp_menu_items  = $id ? wp_get_nav_menu_items( $id ) : array();
134
+            $rest_url       = get_rest_url().self::get_api_namespace().'/menus/';
135
+            $wp_menu_object = $id ? wp_get_nav_menu_object($id) : array();
136
+            $wp_menu_items  = $id ? wp_get_nav_menu_items($id) : array();
137 137
 
138 138
             $rest_menu = array();
139 139
 
140
-            if ( $wp_menu_object ) :
140
+            if ($wp_menu_object) :
141 141
 
142 142
                 $menu = (array) $wp_menu_object;
143
-                $rest_menu['ID']          = abs( $menu['term_id'] );
143
+                $rest_menu['ID']          = abs($menu['term_id']);
144 144
                 $rest_menu['name']        = $menu['name'];
145 145
                 $rest_menu['slug']        = $menu['slug'];
146 146
                 $rest_menu['description'] = $menu['description'];
147
-                $rest_menu['count']       = abs( $menu['count'] );
147
+                $rest_menu['count']       = abs($menu['count']);
148 148
 
149 149
                 $rest_menu_items = array();
150
-                foreach ( $wp_menu_items as $item_object ) {
151
-	                $rest_menu_items[] = $this->format_menu_item( $item_object );
150
+                foreach ($wp_menu_items as $item_object) {
151
+	                $rest_menu_items[] = $this->format_menu_item($item_object);
152 152
                 }
153 153
 
154 154
                 $rest_menu_items = $this->nested_menu_items($rest_menu_items, 0);
155 155
 
156 156
                 $rest_menu['items']                       = $rest_menu_items;
157 157
                 $rest_menu['meta']['links']['collection'] = $rest_url;
158
-                $rest_menu['meta']['links']['self']       = $rest_url . $id;
158
+                $rest_menu['meta']['links']['self']       = $rest_url.$id;
159 159
 
160 160
             endif;
161 161
 
162
-            return apply_filters( 'rest_menus_format_menu', $rest_menu );
162
+            return apply_filters('rest_menus_format_menu', $rest_menu);
163 163
         }
164 164
 
165 165
 
@@ -174,24 +174,24 @@  discard block
 block discarded – undo
174 174
          * @param  $parent
175 175
          * @return array
176 176
          */
177
-        private function nested_menu_items( &$menu_items, $parent = null ) {
177
+        private function nested_menu_items(&$menu_items, $parent = null) {
178 178
 
179 179
             $parents = array();
180 180
             $children = array();
181 181
 
182 182
             // Separate menu_items into parents & children.
183
-            array_map( function( $i ) use ( $parent, &$children, &$parents ){
184
-                if ( $i['id'] != $parent && $i['parent'] == $parent ) {
183
+            array_map(function($i) use ($parent, &$children, &$parents){
184
+                if ($i['id'] != $parent && $i['parent'] == $parent) {
185 185
                     $parents[] = $i;
186 186
                 } else {
187 187
                     $children[] = $i;
188 188
                 }
189
-            }, $menu_items );
189
+            }, $menu_items);
190 190
 
191
-            foreach ( $parents as &$parent ) {
191
+            foreach ($parents as &$parent) {
192 192
 
193
-                if ( $this->has_children( $children, $parent['id'] ) ) {
194
-                    $parent['children'] = $this->nested_menu_items( $children, $parent['id'] );
193
+                if ($this->has_children($children, $parent['id'])) {
194
+                    $parent['children'] = $this->nested_menu_items($children, $parent['id']);
195 195
                 }
196 196
             }
197 197
 
@@ -207,8 +207,8 @@  discard block
 block discarded – undo
207 207
          * @param  int $id
208 208
          * @return array
209 209
          */
210
-        private function has_children( $items, $id ) {
211
-            return array_filter( $items, function( $i ) use ( $id ) {
210
+        private function has_children($items, $id) {
211
+            return array_filter($items, function($i) use ($id) {
212 212
                 return $i['parent'] == $id;
213 213
             } );
214 214
         }
@@ -221,26 +221,26 @@  discard block
 block discarded – undo
221 221
          * @param  $request
222 222
          * @return array All registered menus locations
223 223
          */
224
-        public static function get_menu_locations( $request ) {
224
+        public static function get_menu_locations($request) {
225 225
 
226 226
             $locations        = get_nav_menu_locations();
227 227
             $registered_menus = get_registered_nav_menus();
228
-	        $rest_url         = get_rest_url() . self::get_api_namespace() . '/menu-locations/';
228
+	        $rest_url = get_rest_url().self::get_api_namespace().'/menu-locations/';
229 229
             $rest_menus       = array();
230 230
 
231
-            if ( $locations && $registered_menus ) :
231
+            if ($locations && $registered_menus) :
232 232
 
233
-                foreach ( $registered_menus as $slug => $label ) :
233
+                foreach ($registered_menus as $slug => $label) :
234 234
 
235 235
 	                // Sanity check
236
-	                if ( ! isset( $locations[ $slug ] ) ) {
236
+	                if ( ! isset($locations[$slug])) {
237 237
 		                continue;
238 238
 	                }
239 239
 
240
-	                $rest_menus[ $slug ]['ID']                          = $locations[ $slug ];
241
-                    $rest_menus[ $slug ]['label']                       = $label;
242
-                    $rest_menus[ $slug ]['meta']['links']['collection'] = $rest_url;
243
-                    $rest_menus[ $slug ]['meta']['links']['self']       = $rest_url . $slug;
240
+	                $rest_menus[$slug]['ID'] = $locations[$slug];
241
+                    $rest_menus[$slug]['label']                       = $label;
242
+                    $rest_menus[$slug]['meta']['links']['collection'] = $rest_url;
243
+                    $rest_menus[$slug]['meta']['links']['self']       = $rest_url.$slug;
244 244
 
245 245
                 endforeach;
246 246
 
@@ -257,42 +257,42 @@  discard block
 block discarded – undo
257 257
          * @param  $request
258 258
          * @return array The menu for the corresponding location
259 259
          */
260
-        public function get_menu_location( $request ) {
260
+        public function get_menu_location($request) {
261 261
 
262 262
             $params     = $request->get_params();
263 263
             $location   = $params['location'];
264 264
             $locations  = get_nav_menu_locations();
265 265
 
266
-            if ( ! isset( $locations[ $location ] ) ) {
266
+            if ( ! isset($locations[$location])) {
267 267
 	            return array();
268 268
             }
269 269
 
270
-            $wp_menu = wp_get_nav_menu_object( $locations[ $location ] );
271
-            $menu_items = wp_get_nav_menu_items( $wp_menu->term_id );
270
+            $wp_menu = wp_get_nav_menu_object($locations[$location]);
271
+            $menu_items = wp_get_nav_menu_items($wp_menu->term_id);
272 272
 
273 273
 			/**
274 274
 			 * wp_get_nav_menu_items() outputs a list that's already sequenced correctly.
275 275
 			 * So the easiest thing to do is to reverse the list and then build our tree
276 276
 			 * from the ground up
277 277
 			 */
278
-			$rev_items = array_reverse ( $menu_items );
278
+			$rev_items = array_reverse($menu_items);
279 279
 			$rev_menu  = array();
280 280
 			$cache     = array();
281 281
 
282
-			foreach ( $rev_items as $item ) :
282
+			foreach ($rev_items as $item) :
283 283
 
284 284
 				$formatted = array(
285
-					'ID'          => abs( $item->ID ),
285
+					'ID'          => abs($item->ID),
286 286
 					'order'       => (int) $item->menu_order,
287
-					'parent'      => abs( $item->menu_item_parent ),
287
+					'parent'      => abs($item->menu_item_parent),
288 288
 					'title'       => $item->title,
289 289
 					'url'         => $item->url,
290 290
 					'attr'        => $item->attr_title,
291 291
 					'target'      => $item->target,
292
-					'classes'     => implode( ' ', $item->classes ),
292
+					'classes'     => implode(' ', $item->classes),
293 293
 					'xfn'         => $item->xfn,
294 294
 					'description' => $item->description,
295
-					'object_id'   => abs( $item->object_id ),
295
+					'object_id'   => abs($item->object_id),
296 296
 					'object'      => $item->object,
297 297
 					'object_slug' => array(),
298 298
 					'type'        => $item->type,
@@ -300,33 +300,33 @@  discard block
 block discarded – undo
300 300
 					'children'    => array(),
301 301
 				);
302 302
 
303
-                if ( $formatted['object'] == 'category' ) {
303
+                if ($formatted['object'] == 'category') {
304 304
 
305 305
                     $formatted['object_slug'] = get_category($item->object_id)->slug;
306 306
                 }
307 307
 			
308
-				if ( array_key_exists( $item->ID , $cache ) ) {
309
-					$formatted['children'] = array_reverse( $cache[ $item->ID ] );
308
+				if (array_key_exists($item->ID, $cache)) {
309
+					$formatted['children'] = array_reverse($cache[$item->ID]);
310 310
 				}
311 311
 
312
-            	$formatted = apply_filters( 'rest_menus_format_menu_item', $formatted );
312
+            	$formatted = apply_filters('rest_menus_format_menu_item', $formatted);
313 313
 
314
-				if ( $item->menu_item_parent != 0 ) {
314
+				if ($item->menu_item_parent != 0) {
315 315
 
316
-					if ( array_key_exists( $item->menu_item_parent , $cache ) ) {
317
-						array_push( $cache[ $item->menu_item_parent ], $formatted );
316
+					if (array_key_exists($item->menu_item_parent, $cache)) {
317
+						array_push($cache[$item->menu_item_parent], $formatted);
318 318
 					} else {
319
-						$cache[ $item->menu_item_parent ] = array( $formatted, );
319
+						$cache[$item->menu_item_parent] = array($formatted,);
320 320
 					}
321 321
 
322 322
 				} else {
323 323
 
324
-					array_push( $rev_menu, $formatted );
324
+					array_push($rev_menu, $formatted);
325 325
 				}
326 326
 
327 327
 			endforeach;
328 328
 
329
-			return array_reverse ( $rev_menu );
329
+			return array_reverse($rev_menu);
330 330
         }
331 331
 
332 332
 
@@ -339,19 +339,19 @@  discard block
 block discarded – undo
339 339
          * @param bool  $depth          Gives all children or direct children only
340 340
          * @return array	returns filtered array of nav_menu_items
341 341
          */
342
-        public function get_nav_menu_item_children( $parent_id, $nav_menu_items, $depth = true ) {
342
+        public function get_nav_menu_item_children($parent_id, $nav_menu_items, $depth = true) {
343 343
 
344 344
             $nav_menu_item_list = array();
345 345
 
346
-            foreach ( (array) $nav_menu_items as $nav_menu_item ) :
346
+            foreach ((array) $nav_menu_items as $nav_menu_item) :
347 347
 
348
-                if ( $nav_menu_item->menu_item_parent == $parent_id ) :
348
+                if ($nav_menu_item->menu_item_parent == $parent_id) :
349 349
 
350
-                    $nav_menu_item_list[] = $this->format_menu_item( $nav_menu_item, true, $nav_menu_items );
350
+                    $nav_menu_item_list[] = $this->format_menu_item($nav_menu_item, true, $nav_menu_items);
351 351
 
352
-                    if ( $depth ) {
353
-                        if ( $children = $this->get_nav_menu_item_children( $nav_menu_item->ID, $nav_menu_items ) ) {
354
-                            $nav_menu_item_list = array_merge( $nav_menu_item_list, $children );
352
+                    if ($depth) {
353
+                        if ($children = $this->get_nav_menu_item_children($nav_menu_item->ID, $nav_menu_items)) {
354
+                            $nav_menu_item_list = array_merge($nav_menu_item_list, $children);
355 355
                         }
356 356
                     }
357 357
 
@@ -372,33 +372,33 @@  discard block
 block discarded – undo
372 372
          * @param  array        $menu       The menu the item belongs to (used when $children is set to true)
373 373
          * @return array	a formatted menu item for REST
374 374
          */
375
-        public function format_menu_item( $menu_item, $children = false, $menu = array() ) {
375
+        public function format_menu_item($menu_item, $children = false, $menu = array()) {
376 376
 
377 377
             $item = (array) $menu_item;
378 378
 
379 379
             $menu_item = array(
380
-                'id'          => abs( $item['ID'] ),
380
+                'id'          => abs($item['ID']),
381 381
                 'order'       => (int) $item['menu_order'],
382
-                'parent'      => abs( $item['menu_item_parent'] ),
382
+                'parent'      => abs($item['menu_item_parent']),
383 383
                 'title'       => $item['title'],
384 384
                 'url'         => $item['url'],
385 385
                 'attr'        => $item['attr_title'],
386 386
                 'target'      => $item['target'],
387
-                'classes'     => implode( ' ', $item['classes'] ),
387
+                'classes'     => implode(' ', $item['classes']),
388 388
                 'xfn'         => $item['xfn'],
389 389
                 'description' => $item['description'],
390
-                'object_id'   => abs( $item['object_id'] ),
390
+                'object_id'   => abs($item['object_id']),
391 391
                 'object'      => $item['object'],
392
-                'object_slug' => get_post( $item['object_id'] )->post_name,
392
+                'object_slug' => get_post($item['object_id'])->post_name,
393 393
                 'type'        => $item['type'],
394 394
                 'type_label'  => $item['type_label'],
395 395
             );
396 396
 
397
-            if ( $children === true && ! empty( $menu ) ) {
398
-	            $menu_item['children'] = $this->get_nav_menu_item_children( $item['ID'], $menu );
397
+            if ($children === true && ! empty($menu)) {
398
+	            $menu_item['children'] = $this->get_nav_menu_item_children($item['ID'], $menu);
399 399
             }
400 400
 
401
-            return apply_filters( 'rest_menus_format_menu_item', $menu_item );
401
+            return apply_filters('rest_menus_format_menu_item', $menu_item);
402 402
         }
403 403
 
404 404
 
Please login to merge, or discard this patch.
includes/wp-api-menus-v1.php 2 patches
Indentation   +1 added lines, -1 removed lines patch added patch discarded remove patch
@@ -274,7 +274,7 @@
 block discarded – undo
274 274
 				'description' => $item['description'],
275 275
 				'object_id'   => abs( $item['object_id'] ),
276 276
 				'object'      => $item['object'],
277
-                'object_slug' => get_post($item['object_id'])->post_name,
277
+				'object_slug' => get_post($item['object_id'])->post_name,
278 278
 				'type'        => $item['type'],
279 279
 				'type_label'  => $item['type_label'],
280 280
 			);
Please login to merge, or discard this 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,27 +61,27 @@  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 69
 
70
-			foreach ( $wp_menus as $wp_menu ) :
70
+			foreach ($wp_menus as $wp_menu) :
71 71
 
72 72
 				$menu = (array) $wp_menu;
73 73
 
74
-				$json_menus[ $i ]                 = $menu;
75
-				$json_menus[ $i ]['ID']           = $menu['term_id'];
76
-				$json_menus[ $i ]['name']         = $menu['name'];
77
-				$json_menus[ $i ]['slug']         = $menu['slug'];
78
-				$json_menus[ $i ]['description']  = $menu['description'];
79
-				$json_menus[ $i ]['count']        = $menu['count'];
74
+				$json_menus[$i]                 = $menu;
75
+				$json_menus[$i]['ID']           = $menu['term_id'];
76
+				$json_menus[$i]['name']         = $menu['name'];
77
+				$json_menus[$i]['slug']         = $menu['slug'];
78
+				$json_menus[$i]['description']  = $menu['description'];
79
+				$json_menus[$i]['count']        = $menu['count'];
80 80
 
81
-				$json_menus[ $i ]['meta']['links']['collection'] = $json_url;
82
-				$json_menus[ $i ]['meta']['links']['self']       = $json_url . $menu['term_id'];
81
+				$json_menus[$i]['meta']['links']['collection'] = $json_url;
82
+				$json_menus[$i]['meta']['links']['self']       = $json_url.$menu['term_id'];
83 83
 
84
-				$i ++;
84
+				$i++;
85 85
 
86 86
 			endforeach;
87 87
 
@@ -96,33 +96,33 @@  discard block
 block discarded – undo
96 96
 		 * @param  int   $id ID of the menu
97 97
 		 * @return array Menu data
98 98
 		 */
99
-		public function get_menu( $id ) {
99
+		public function get_menu($id) {
100 100
 
101
-			$json_url       = get_json_url() . '/menus/';
102
-			$wp_menu_object = $id ? wp_get_nav_menu_object( $id ) : array();
103
-			$wp_menu_items  = $id ? wp_get_nav_menu_items( $id ) : array();
101
+			$json_url       = get_json_url().'/menus/';
102
+			$wp_menu_object = $id ? wp_get_nav_menu_object($id) : array();
103
+			$wp_menu_items  = $id ? wp_get_nav_menu_items($id) : array();
104 104
 
105 105
 			$json_menu = array();
106 106
 
107
-			if ( $wp_menu_object ) :
107
+			if ($wp_menu_object) :
108 108
 
109 109
 				$menu = (array) $wp_menu_object;
110
-				$json_menu['ID']            = abs( $menu['term_id'] );
110
+				$json_menu['ID']            = abs($menu['term_id']);
111 111
 				$json_menu['name']          = $menu['name'];
112 112
 				$json_menu['slug']          = $menu['slug'];
113 113
 				$json_menu['description']   = $menu['description'];
114
-				$json_menu['count']         = abs( $menu['count'] );
114
+				$json_menu['count']         = abs($menu['count']);
115 115
 
116 116
 				$json_menu_items = array();
117 117
 
118
-				foreach ( $wp_menu_items as $item_object ) {
118
+				foreach ($wp_menu_items as $item_object) {
119 119
 
120
-					$json_menu_items[] = $this->format_menu_item( $item_object );
120
+					$json_menu_items[] = $this->format_menu_item($item_object);
121 121
 				}
122 122
 
123 123
 				$json_menu['items']                       = $json_menu_items;
124 124
 				$json_menu['meta']['links']['collection'] = $json_url;
125
-				$json_menu['meta']['links']['self']       = $json_url . $id;
125
+				$json_menu['meta']['links']['self']       = $json_url.$id;
126 126
 
127 127
 			endif;
128 128
 
@@ -140,22 +140,22 @@  discard block
 block discarded – undo
140 140
 
141 141
 			$locations        = get_nav_menu_locations();
142 142
 			$registered_menus = get_registered_nav_menus();
143
-			$json_url         = get_json_url() . '/menu-locations/';
143
+			$json_url         = get_json_url().'/menu-locations/';
144 144
 			$json_menus       = array();
145 145
 
146
-			if ( $locations && $registered_menus ) :
146
+			if ($locations && $registered_menus) :
147 147
 
148
-				foreach ( $registered_menus as $slug => $label ) :
148
+				foreach ($registered_menus as $slug => $label) :
149 149
 
150 150
 					// Sanity check
151
-					if ( ! isset( $locations[ $slug ] ) ) {
151
+					if ( ! isset($locations[$slug])) {
152 152
 						continue;
153 153
 					}
154 154
 
155
-					$json_menus[ $slug ]['ID']                          = $locations[ $slug ];
156
-					$json_menus[ $slug ]['label']                       = $label;
157
-					$json_menus[ $slug ]['meta']['links']['collection'] = $json_url;
158
-					$json_menus[ $slug ]['meta']['links']['self']       = $json_url . $slug;
155
+					$json_menus[$slug]['ID']                          = $locations[$slug];
156
+					$json_menus[$slug]['label']                       = $label;
157
+					$json_menus[$slug]['meta']['links']['collection'] = $json_url;
158
+					$json_menus[$slug]['meta']['links']['self']       = $json_url.$slug;
159 159
 
160 160
 				endforeach;
161 161
 
@@ -172,29 +172,29 @@  discard block
 block discarded – undo
172 172
 		 * @param  string $location The theme location menu name
173 173
 		 * @return array The menu for the corresponding location
174 174
 		 */
175
-		public function get_menu_location( $location ) {
175
+		public function get_menu_location($location) {
176 176
 
177 177
 			$locations = get_nav_menu_locations();
178 178
 
179
-			if ( ! isset( $locations[ $location ] ) ) {
179
+			if ( ! isset($locations[$location])) {
180 180
 
181 181
 				return array();
182 182
 			}
183 183
 
184
-			$wp_menu = wp_get_nav_menu_object( $locations[ $location ] );
185
-			$menu_items = wp_get_nav_menu_items( $wp_menu->term_id );
184
+			$wp_menu = wp_get_nav_menu_object($locations[$location]);
185
+			$menu_items = wp_get_nav_menu_items($wp_menu->term_id);
186 186
 
187 187
 			$sorted_menu_items = $top_level_menu_items = $menu_items_with_children = array();
188 188
 
189
-			foreach ( (array) $menu_items as $menu_item ) {
189
+			foreach ((array) $menu_items as $menu_item) {
190 190
 
191
-				$sorted_menu_items[ $menu_item->menu_order ] = $menu_item;
191
+				$sorted_menu_items[$menu_item->menu_order] = $menu_item;
192 192
 			}
193 193
 
194
-			foreach ( $sorted_menu_items as $menu_item ) {
194
+			foreach ($sorted_menu_items as $menu_item) {
195 195
 
196
-				if ( (int) $menu_item->menu_item_parent !== 0 ) {
197
-					$menu_items_with_children[ $menu_item->menu_item_parent ] = true;
196
+				if ((int) $menu_item->menu_item_parent !== 0) {
197
+					$menu_items_with_children[$menu_item->menu_item_parent] = true;
198 198
 				} else {
199 199
 					$top_level_menu_items[] = $menu_item;
200 200
 				}
@@ -202,18 +202,18 @@  discard block
 block discarded – undo
202 202
 
203 203
 			$menu = array();
204 204
 
205
-			while ( $sorted_menu_items ) :
205
+			while ($sorted_menu_items) :
206 206
 
207 207
 				$i = 0;
208 208
 
209
-				foreach ( $top_level_menu_items as $top_item ) :
209
+				foreach ($top_level_menu_items as $top_item) :
210 210
 
211
-					$menu[ $i ] = $this->format_menu_item( $top_item, false );
211
+					$menu[$i] = $this->format_menu_item($top_item, false);
212 212
 
213
-					if ( isset( $menu_items_with_children[ $top_item->ID ] ) ) {
214
-						$menu[ $i ]['children'] = $this->get_nav_menu_item_children( $top_item->ID, $menu_items, false );
213
+					if (isset($menu_items_with_children[$top_item->ID])) {
214
+						$menu[$i]['children'] = $this->get_nav_menu_item_children($top_item->ID, $menu_items, false);
215 215
 					} else {
216
-						$menu[ $i ]['children'] = array();
216
+						$menu[$i]['children'] = array();
217 217
 					}
218 218
 
219 219
 					$i++;
@@ -237,21 +237,21 @@  discard block
 block discarded – undo
237 237
 		 * @param  bool    $depth          gives all children or direct children only
238 238
 		 * @return array   returns filtered array of nav_menu_items
239 239
 		 */
240
-		public function get_nav_menu_item_children( $parent_id, $nav_menu_items, $depth = true ) {
240
+		public function get_nav_menu_item_children($parent_id, $nav_menu_items, $depth = true) {
241 241
 
242 242
 			$nav_menu_item_list = array();
243 243
 
244
-			foreach ( (array) $nav_menu_items as $nav_menu_item ) :
244
+			foreach ((array) $nav_menu_items as $nav_menu_item) :
245 245
 
246
-				if ( $nav_menu_item->menu_item_parent == $parent_id ) :
246
+				if ($nav_menu_item->menu_item_parent == $parent_id) :
247 247
 
248
-					$nav_menu_item_list[] = $this->format_menu_item( $nav_menu_item, true, $nav_menu_items );
248
+					$nav_menu_item_list[] = $this->format_menu_item($nav_menu_item, true, $nav_menu_items);
249 249
 
250
-					if ( $depth ) {
250
+					if ($depth) {
251 251
 
252
-						if ( $children = $this->get_nav_menu_item_children( $nav_menu_item->ID, $nav_menu_items ) ) {
252
+						if ($children = $this->get_nav_menu_item_children($nav_menu_item->ID, $nav_menu_items)) {
253 253
 
254
-							$nav_menu_item_list = array_merge( $nav_menu_item_list, $children );
254
+							$nav_menu_item_list = array_merge($nav_menu_item_list, $children);
255 255
 						}
256 256
 					}
257 257
 
@@ -272,34 +272,34 @@  discard block
 block discarded – undo
272 272
 		 * @param   array           $menu       the menu the item belongs to (used when $children is set to true)
273 273
 		 * @return  array   a formatted menu item for JSON
274 274
 		 */
275
-		public function format_menu_item( $menu_item, $children = false, $menu = array() ) {
275
+		public function format_menu_item($menu_item, $children = false, $menu = array()) {
276 276
 
277 277
 			$item = (array) $menu_item;
278 278
 
279 279
 			$menu_item = array(
280
-				'ID'          => abs( $item['ID'] ),
280
+				'ID'          => abs($item['ID']),
281 281
 				'order'       => (int) $item['menu_order'],
282
-				'parent'      => abs( $item['menu_item_parent'] ),
282
+				'parent'      => abs($item['menu_item_parent']),
283 283
 				'title'       => $item['title'],
284 284
 				'url'         => $item['url'],
285 285
 				'attr'        => $item['attr_title'],
286 286
 				'target'      => $item['target'],
287
-				'classes'     => implode( ' ', $item['classes'] ),
287
+				'classes'     => implode(' ', $item['classes']),
288 288
 				'xfn'         => $item['xfn'],
289 289
 				'description' => $item['description'],
290
-				'object_id'   => abs( $item['object_id'] ),
290
+				'object_id'   => abs($item['object_id']),
291 291
 				'object'      => $item['object'],
292 292
                 'object_slug' => get_post($item['object_id'])->post_name,
293 293
 				'type'        => $item['type'],
294 294
 				'type_label'  => $item['type_label'],
295 295
 			);
296 296
 
297
-			if ( $children === true && ! empty( $menu ) ) {
297
+			if ($children === true && ! empty($menu)) {
298 298
 
299
-				$menu_item['children'] = $this->get_nav_menu_item_children( $item['ID'], $menu );
299
+				$menu_item['children'] = $this->get_nav_menu_item_children($item['ID'], $menu);
300 300
 			}
301 301
 
302
-			return apply_filters( 'json_menus_format_menu_item', $menu_item );
302
+			return apply_filters('json_menus_format_menu_item', $menu_item);
303 303
 		}
304 304
 
305 305
 
Please login to merge, or discard this patch.
wp-api-menus.php 2 patches
Indentation   +1 added lines, -1 removed lines patch added patch discarded remove patch
@@ -48,7 +48,7 @@
 block discarded – undo
48 48
 	 */
49 49
 	function wp_rest_menus_init() {
50 50
 
51
-        if ( ! defined( 'JSON_API_VERSION' ) && ! in_array( 'json-rest-api/plugin.php', get_option( 'active_plugins' ) ) ) {
51
+		if ( ! defined( 'JSON_API_VERSION' ) && ! in_array( 'json-rest-api/plugin.php', get_option( 'active_plugins' ) ) ) {
52 52
 			$class = new WP_REST_Menus();
53 53
 			 add_filter( 'rest_api_init', array( $class, 'register_routes' ) );
54 54
 		} else {
Please login to merge, or discard this patch.
Spacing   +6 added lines, -6 removed lines patch added patch discarded remove patch
@@ -30,7 +30,7 @@  discard block
 block discarded – undo
30 30
  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
31 31
  */
32 32
 
33
-if ( ! defined( 'ABSPATH' ) ) {
33
+if ( ! defined('ABSPATH')) {
34 34
 	exit; // Exit if accessed directly
35 35
 }
36 36
 
@@ -39,7 +39,7 @@  discard block
 block discarded – undo
39 39
 // WP API v2.
40 40
 include_once 'includes/wp-api-menus-v2.php';
41 41
 
42
-if ( ! function_exists ( 'wp_rest_menus_init' ) ) :
42
+if ( ! function_exists('wp_rest_menus_init')) :
43 43
 
44 44
 	/**
45 45
 	 * Init JSON REST API Menu routes.
@@ -48,15 +48,15 @@  discard block
 block discarded – undo
48 48
 	 */
49 49
 	function wp_rest_menus_init() {
50 50
 
51
-        if ( ! defined( 'JSON_API_VERSION' ) && ! in_array( 'json-rest-api/plugin.php', get_option( 'active_plugins' ) ) ) {
51
+        if ( ! defined('JSON_API_VERSION') && ! in_array('json-rest-api/plugin.php', get_option('active_plugins'))) {
52 52
 			$class = new WP_REST_Menus();
53
-			 add_filter( 'rest_api_init', array( $class, 'register_routes' ) );
53
+			 add_filter('rest_api_init', array($class, 'register_routes'));
54 54
 		} else {
55 55
 			$class = new WP_JSON_Menus();
56
-			add_filter( 'json_endpoints', array( $class, 'register_routes' ) );
56
+			add_filter('json_endpoints', array($class, 'register_routes'));
57 57
 		}
58 58
 	}
59 59
 
60
-	add_action( 'init', 'wp_rest_menus_init' );
60
+	add_action('init', 'wp_rest_menus_init');
61 61
 
62 62
 endif;
Please login to merge, or discard this patch.