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