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