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