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