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