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