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