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