Completed
Pull Request — master (#53)
by
unknown
02:00
created
includes/wp-api-menus-v1.php 1 patch
Indentation   +1 added lines, -1 removed lines patch added patch discarded remove patch
@@ -274,7 +274,7 @@
 block discarded – undo
274 274
 				'description' => $item['description'],
275 275
 				'object_id'   => abs( $item['object_id'] ),
276 276
 				'object'      => $item['object'],
277
-                'object_slug' => get_post($item['object_id'])->post_name,
277
+				'object_slug' => get_post($item['object_id'])->post_name,
278 278
 				'type'        => $item['type'],
279 279
 				'type_label'  => $item['type_label'],
280 280
 			);
Please login to merge, or discard this patch.
wp-api-menus.php 1 patch
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.
includes/wp-api-menus-v2.php 1 patch
Indentation   +362 added lines, -362 removed lines patch added patch discarded remove patch
@@ -6,267 +6,267 @@  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
-                )
60
-            ) );
61
-
62
-            register_rest_route( self::get_plugin_namespace(), '/menus/(?P<id>\d+)', array(
63
-                array(
64
-                    'methods'  => WP_REST_Server::READABLE,
65
-                    'callback' => array( $this, 'get_menu' ),
66
-                    'args'     => array(
67
-                        'context' => array(
68
-                        'default' => 'view',
69
-                        ),
70
-                    ),
71
-                )
72
-            ) );
73
-
74
-            register_rest_route( self::get_plugin_namespace(), '/menu-locations', array(
75
-                array(
76
-                    'methods'  => WP_REST_Server::READABLE,
77
-                    'callback' => array( $this, 'get_menu_locations' ),
78
-                )
79
-            ) );
80
-
81
-            register_rest_route( self::get_plugin_namespace(), '/menu-locations/(?P<location>[a-zA-Z0-9_-]+)', array(
82
-                array(
83
-                    'methods'  => WP_REST_Server::READABLE,
84
-                    'callback' => array( $this, 'get_menu_location' ),
85
-                )
86
-            ) );
87
-        }
88
-
89
-
90
-        /**
91
-         * Get menus.
92
-         *
93
-         * @since  1.2.0
94
-         * @return array All registered menus
95
-         */
96
-        public static function get_menus() {
97
-
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_api_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
-            return apply_filters( 'rest_menus_format_menu', $rest_menu );
161
-        }
162
-
163
-
164
-        /**
165
-         * Handle nested menu items.
166
-         *
167
-         * Given a flat array of menu items, split them into parent/child items
168
-         * and recurse over them to return children nested in their parent.
169
-         *
170
-         * @since  1.2.0
171
-         * @param  $menu_items
172
-         * @param  $parent
173
-         * @return array
174
-         */
175
-        private function nested_menu_items( &$menu_items, $parent = null ) {
176
-
177
-            $parents = array();
178
-            $children = array();
179
-
180
-            // Separate menu_items into parents & children.
181
-            array_map( function( $i ) use ( $parent, &$children, &$parents ){
182
-                if ( $i['id'] != $parent && $i['parent'] == $parent ) {
183
-                    $parents[] = $i;
184
-                } else {
185
-                    $children[] = $i;
186
-                }
187
-            }, $menu_items );
188
-
189
-            foreach ( $parents as &$parent ) {
190
-
191
-                if ( $this->has_children( $children, $parent['id'] ) ) {
192
-                    $parent['children'] = $this->nested_menu_items( $children, $parent['id'] );
193
-                }
194
-            }
195
-
196
-            return $parents;
197
-        }
198
-
199
-
200
-        /**
201
-         * Check if a collection of menu items contains an item that is the parent id of 'id'.
202
-         *
203
-         * @since  1.2.0
204
-         * @param  array $items
205
-         * @param  int $id
206
-         * @return array
207
-         */
208
-        private function has_children( $items, $id ) {
209
-            return array_filter( $items, function( $i ) use ( $id ) {
210
-                return $i['parent'] == $id;
211
-            } );
212
-        }
213
-
214
-
215
-        /**
216
-         * Get menu locations.
217
-         *
218
-         * @since 1.2.0
219
-         * @param  $request
220
-         * @return array All registered menus locations
221
-         */
222
-        public static function get_menu_locations( $request ) {
223
-
224
-            $locations        = get_nav_menu_locations();
225
-            $registered_menus = get_registered_nav_menus();
226
-	        $rest_url         = get_rest_url() . self::get_api_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
-
260
-            $params     = $request->get_params();
261
-            $location   = $params['location'];
262
-            $locations  = get_nav_menu_locations();
263
-
264
-            if ( ! isset( $locations[ $location ] ) ) {
265
-	            return array();
266
-            }
267
-
268
-            $wp_menu = wp_get_nav_menu_object( $locations[ $location ] );
269
-            $menu_items = wp_get_nav_menu_items( $wp_menu->term_id );
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
+				)
60
+			) );
61
+
62
+			register_rest_route( self::get_plugin_namespace(), '/menus/(?P<id>\d+)', array(
63
+				array(
64
+					'methods'  => WP_REST_Server::READABLE,
65
+					'callback' => array( $this, 'get_menu' ),
66
+					'args'     => array(
67
+						'context' => array(
68
+						'default' => 'view',
69
+						),
70
+					),
71
+				)
72
+			) );
73
+
74
+			register_rest_route( self::get_plugin_namespace(), '/menu-locations', array(
75
+				array(
76
+					'methods'  => WP_REST_Server::READABLE,
77
+					'callback' => array( $this, 'get_menu_locations' ),
78
+				)
79
+			) );
80
+
81
+			register_rest_route( self::get_plugin_namespace(), '/menu-locations/(?P<location>[a-zA-Z0-9_-]+)', array(
82
+				array(
83
+					'methods'  => WP_REST_Server::READABLE,
84
+					'callback' => array( $this, 'get_menu_location' ),
85
+				)
86
+			) );
87
+		}
88
+
89
+
90
+		/**
91
+		 * Get menus.
92
+		 *
93
+		 * @since  1.2.0
94
+		 * @return array All registered menus
95
+		 */
96
+		public static function get_menus() {
97
+
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_api_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
+			return apply_filters( 'rest_menus_format_menu', $rest_menu );
161
+		}
162
+
163
+
164
+		/**
165
+		 * Handle nested menu items.
166
+		 *
167
+		 * Given a flat array of menu items, split them into parent/child items
168
+		 * and recurse over them to return children nested in their parent.
169
+		 *
170
+		 * @since  1.2.0
171
+		 * @param  $menu_items
172
+		 * @param  $parent
173
+		 * @return array
174
+		 */
175
+		private function nested_menu_items( &$menu_items, $parent = null ) {
176
+
177
+			$parents = array();
178
+			$children = array();
179
+
180
+			// Separate menu_items into parents & children.
181
+			array_map( function( $i ) use ( $parent, &$children, &$parents ){
182
+				if ( $i['id'] != $parent && $i['parent'] == $parent ) {
183
+					$parents[] = $i;
184
+				} else {
185
+					$children[] = $i;
186
+				}
187
+			}, $menu_items );
188
+
189
+			foreach ( $parents as &$parent ) {
190
+
191
+				if ( $this->has_children( $children, $parent['id'] ) ) {
192
+					$parent['children'] = $this->nested_menu_items( $children, $parent['id'] );
193
+				}
194
+			}
195
+
196
+			return $parents;
197
+		}
198
+
199
+
200
+		/**
201
+		 * Check if a collection of menu items contains an item that is the parent id of 'id'.
202
+		 *
203
+		 * @since  1.2.0
204
+		 * @param  array $items
205
+		 * @param  int $id
206
+		 * @return array
207
+		 */
208
+		private function has_children( $items, $id ) {
209
+			return array_filter( $items, function( $i ) use ( $id ) {
210
+				return $i['parent'] == $id;
211
+			} );
212
+		}
213
+
214
+
215
+		/**
216
+		 * Get menu locations.
217
+		 *
218
+		 * @since 1.2.0
219
+		 * @param  $request
220
+		 * @return array All registered menus locations
221
+		 */
222
+		public static function get_menu_locations( $request ) {
223
+
224
+			$locations        = get_nav_menu_locations();
225
+			$registered_menus = get_registered_nav_menus();
226
+			$rest_url         = get_rest_url() . self::get_api_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
+
260
+			$params     = $request->get_params();
261
+			$location   = $params['location'];
262
+			$locations  = get_nav_menu_locations();
263
+
264
+			if ( ! isset( $locations[ $location ] ) ) {
265
+				return array();
266
+			}
267
+
268
+			$wp_menu = wp_get_nav_menu_object( $locations[ $location ] );
269
+			$menu_items = wp_get_nav_menu_items( $wp_menu->term_id );
270 270
 
271 271
 			/**
272 272
 			 * wp_get_nav_menu_items() outputs a list that's already sequenced correctly.
@@ -301,7 +301,7 @@  discard block
 block discarded – undo
301 301
 					$formatted['children'] = array_reverse( $cache[ $item->ID ] );
302 302
 				}
303 303
 
304
-              	$formatted = apply_filters( 'rest_menus_format_menu_item', $formatted );
304
+			  	$formatted = apply_filters( 'rest_menus_format_menu_item', $formatted );
305 305
 
306 306
 				if ( $item->menu_item_parent != 0 ) {
307 307
 
@@ -319,112 +319,112 @@  discard block
 block discarded – undo
319 319
 			endforeach;
320 320
 
321 321
 			return array_reverse ( $rev_menu );
322
-        }
323
-
324
-
325
-        /**
326
-         * Returns all child nav_menu_items under a specific parent.
327
-         *
328
-         * @since   1.2.0
329
-         * @param int   $parent_id      The parent nav_menu_item ID
330
-         * @param array $nav_menu_items Navigation menu items
331
-         * @param bool  $depth          Gives all children or direct children only
332
-         * @return array	returns filtered array of nav_menu_items
333
-         */
334
-        public function get_nav_menu_item_children( $parent_id, $nav_menu_items, $depth = true ) {
335
-
336
-            $nav_menu_item_list = array();
337
-
338
-            foreach ( (array) $nav_menu_items as $nav_menu_item ) :
339
-
340
-                if ( $nav_menu_item->menu_item_parent == $parent_id ) :
341
-
342
-                    $nav_menu_item_list[] = $this->format_menu_item( $nav_menu_item, true, $nav_menu_items );
343
-
344
-                    if ( $depth ) {
345
-                        if ( $children = $this->get_nav_menu_item_children( $nav_menu_item->ID, $nav_menu_items ) ) {
346
-                            $nav_menu_item_list = array_merge( $nav_menu_item_list, $children );
347
-                        }
348
-                    }
349
-
350
-                endif;
351
-
352
-            endforeach;
353
-
354
-            return $nav_menu_item_list;
355
-        }
356
-
357
-
358
-        /**
359
-         * Format a menu item for REST API consumption.
360
-         *
361
-         * @since  1.2.0
362
-         * @param  object|array $menu_item  The menu item
363
-         * @param  bool         $children   Get menu item children (default false)
364
-         * @param  array        $menu       The menu the item belongs to (used when $children is set to true)
365
-         * @return array	a formatted menu item for REST
366
-         */
367
-        public function format_menu_item( $menu_item, $children = false, $menu = array() ) {
368
-            $item = (array) $menu_item;
369
-            $object_slug=null;
370
-            $rest_path = '';
371
-            switch($item['type']){
372
-                case 'post_type_archive': //archive.
373
-                    $post_type_data = get_post_type_object( $item['object'] );
374
-                    $object = empty($post_type_data->rest_base) ? $item['object'] : $post_type_data->rest_base;
375
-                    $object_slug = $post_type_data->rewrite['slug'];
376
-
377
-                    $rest_path = rest_url( '/wp/v2/'.$object.'/');
378
-                    break;
379
-                case 'taxonomy'://taxonomy term.
380
-                    $term = get_term_by( 'id', $item['object_id'], $item['object']);
381
-                    $object_slug = $term->slug;
382
-                    $taxonomy = get_taxonomy( $item['object']);
383
-                    $object =  ! empty( $taxonomy->rest_base ) ? $taxonomy->rest_base : $taxonomy->name;
322
+		}
323
+
324
+
325
+		/**
326
+		 * Returns all child nav_menu_items under a specific parent.
327
+		 *
328
+		 * @since   1.2.0
329
+		 * @param int   $parent_id      The parent nav_menu_item ID
330
+		 * @param array $nav_menu_items Navigation menu items
331
+		 * @param bool  $depth          Gives all children or direct children only
332
+		 * @return array	returns filtered array of nav_menu_items
333
+		 */
334
+		public function get_nav_menu_item_children( $parent_id, $nav_menu_items, $depth = true ) {
335
+
336
+			$nav_menu_item_list = array();
337
+
338
+			foreach ( (array) $nav_menu_items as $nav_menu_item ) :
339
+
340
+				if ( $nav_menu_item->menu_item_parent == $parent_id ) :
341
+
342
+					$nav_menu_item_list[] = $this->format_menu_item( $nav_menu_item, true, $nav_menu_items );
343
+
344
+					if ( $depth ) {
345
+						if ( $children = $this->get_nav_menu_item_children( $nav_menu_item->ID, $nav_menu_items ) ) {
346
+							$nav_menu_item_list = array_merge( $nav_menu_item_list, $children );
347
+						}
348
+					}
349
+
350
+				endif;
351
+
352
+			endforeach;
353
+
354
+			return $nav_menu_item_list;
355
+		}
356
+
357
+
358
+		/**
359
+		 * Format a menu item for REST API consumption.
360
+		 *
361
+		 * @since  1.2.0
362
+		 * @param  object|array $menu_item  The menu item
363
+		 * @param  bool         $children   Get menu item children (default false)
364
+		 * @param  array        $menu       The menu the item belongs to (used when $children is set to true)
365
+		 * @return array	a formatted menu item for REST
366
+		 */
367
+		public function format_menu_item( $menu_item, $children = false, $menu = array() ) {
368
+			$item = (array) $menu_item;
369
+			$object_slug=null;
370
+			$rest_path = '';
371
+			switch($item['type']){
372
+				case 'post_type_archive': //archive.
373
+					$post_type_data = get_post_type_object( $item['object'] );
374
+					$object = empty($post_type_data->rest_base) ? $item['object'] : $post_type_data->rest_base;
375
+					$object_slug = $post_type_data->rewrite['slug'];
376
+
377
+					$rest_path = rest_url( '/wp/v2/'.$object.'/');
378
+					break;
379
+				case 'taxonomy'://taxonomy term.
380
+					$term = get_term_by( 'id', $item['object_id'], $item['object']);
381
+					$object_slug = $term->slug;
382
+					$taxonomy = get_taxonomy( $item['object']);
383
+					$object =  ! empty( $taxonomy->rest_base ) ? $taxonomy->rest_base : $taxonomy->name;
384 384
                     
385
-                    $rest_path = rest_url( '/wp/v2/'.$object.'/'.$item['object_id']);
386
-                    break;
387
-                case 'post_type':
388
-                    $post_type_data = get_post_type_object( $item['object'] );
389
-                    $object = empty($post_type_data->rest_base) ? $item['object'] : $post_type_data->rest_base;
390
-                    $object_slug = get_post( $item['object_id'] )->post_name;
391
-
392
-                    $rest_path = rest_url( '/wp/v2/'.$object.'/'.$item['object_id']);
393
-                    break;
394
-                default: //try with the object and id.
395
-                    $rest_path = rest_url( '/wp/v2/'.$item['object'].'/'.$item['object_id']);
396
-                    break;
397
-            }
398
-            //get_rest_url( int $blog_id = null, string $path = '/', string $scheme = 'rest' )
399
-
400
-            $menu_item = array(
401
-                'id'          => abs( $item['ID'] ),
402
-                'order'       => (int) $item['menu_order'],
403
-                'parent'      => abs( $item['menu_item_parent'] ),
404
-                'title'       => $item['title'],
405
-                'url'         => $item['url'],
406
-                'attr'        => $item['attr_title'],
407
-                'target'      => $item['target'],
408
-                'classes'     => implode( ' ', $item['classes'] ),
409
-                'xfn'         => $item['xfn'],
410
-                'description' => $item['description'],
411
-                'object_id'   => abs( $item['object_id'] ),
412
-                'object'      => $item['object'],
413
-                'object_slug' => $object_slug,
414
-                'type'        => $item['type'],
415
-                'type_label'  => $item['type_label'],
416
-                '_links'      => array('self'=>$rest_path)
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
-    }
385
+					$rest_path = rest_url( '/wp/v2/'.$object.'/'.$item['object_id']);
386
+					break;
387
+				case 'post_type':
388
+					$post_type_data = get_post_type_object( $item['object'] );
389
+					$object = empty($post_type_data->rest_base) ? $item['object'] : $post_type_data->rest_base;
390
+					$object_slug = get_post( $item['object_id'] )->post_name;
391
+
392
+					$rest_path = rest_url( '/wp/v2/'.$object.'/'.$item['object_id']);
393
+					break;
394
+				default: //try with the object and id.
395
+					$rest_path = rest_url( '/wp/v2/'.$item['object'].'/'.$item['object_id']);
396
+					break;
397
+			}
398
+			//get_rest_url( int $blog_id = null, string $path = '/', string $scheme = 'rest' )
399
+
400
+			$menu_item = array(
401
+				'id'          => abs( $item['ID'] ),
402
+				'order'       => (int) $item['menu_order'],
403
+				'parent'      => abs( $item['menu_item_parent'] ),
404
+				'title'       => $item['title'],
405
+				'url'         => $item['url'],
406
+				'attr'        => $item['attr_title'],
407
+				'target'      => $item['target'],
408
+				'classes'     => implode( ' ', $item['classes'] ),
409
+				'xfn'         => $item['xfn'],
410
+				'description' => $item['description'],
411
+				'object_id'   => abs( $item['object_id'] ),
412
+				'object'      => $item['object'],
413
+				'object_slug' => $object_slug,
414
+				'type'        => $item['type'],
415
+				'type_label'  => $item['type_label'],
416
+				'_links'      => array('self'=>$rest_path)
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.