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