Completed
Pull Request — master (#62)
by
unknown
01:53
created
includes/wp-api-menus-v2.php 1 patch
Spacing   +97 added lines, -97 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,20 +52,20 @@  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
                     'args'     => array(
60 60
                         'include' => array(
61 61
                             'required' => false,
62
-                            'validate_callback' => function($val){
63
-                                $list = explode(',',$val);
64
-                                if(!is_array( $list))
65
-                                    return new WP_Error( 'rest_invalid_param', 'include should be an array of menu ID numbers', array( 'status' => 500 ) );
66
-                                foreach($list as $id){
67
-                                    if(!is_nav_menu($id))
68
-                                        return new WP_Error( 'rest_invalid_param', $id.' is not a nav menu', array( 'status' => 500 ) );
62
+                            'validate_callback' => function($val) {
63
+                                $list = explode(',', $val);
64
+                                if ( ! is_array($list))
65
+                                    return new WP_Error('rest_invalid_param', 'include should be an array of menu ID numbers', array('status' => 500));
66
+                                foreach ($list as $id) {
67
+                                    if ( ! is_nav_menu($id))
68
+                                        return new WP_Error('rest_invalid_param', $id.' is not a nav menu', array('status' => 500));
69 69
                                 }
70 70
                                 return true;
71 71
                             }
@@ -76,12 +76,12 @@  discard block
 block discarded – undo
76 76
                     ),
77 77
                     'permission_callback' => '__return_true',
78 78
                 )
79
-            ) );
79
+            ));
80 80
 
81
-            register_rest_route( self::get_plugin_namespace(), '/menus/(?P<id>\d+)', array(
81
+            register_rest_route(self::get_plugin_namespace(), '/menus/(?P<id>\d+)', array(
82 82
                 array(
83 83
                     'methods'  => WP_REST_Server::READABLE,
84
-                    'callback' => array( $this, 'get_menu' ),
84
+                    'callback' => array($this, 'get_menu'),
85 85
                     'permission_callback' => '__return_true',
86 86
                     'args'     => array(
87 87
                         'context' => array(
@@ -89,23 +89,23 @@  discard block
 block discarded – undo
89 89
                         ),
90 90
                     ),
91 91
                 )
92
-            ) );
92
+            ));
93 93
 
94
-            register_rest_route( self::get_plugin_namespace(), '/menu-locations', array(
94
+            register_rest_route(self::get_plugin_namespace(), '/menu-locations', array(
95 95
                 array(
96 96
                     'methods'  => WP_REST_Server::READABLE,
97
-                    'callback' => array( $this, 'get_menu_locations' ),
97
+                    'callback' => array($this, 'get_menu_locations'),
98 98
                     'permission_callback' => '__return_true',
99 99
                 )
100
-            ) );
100
+            ));
101 101
 
102
-            register_rest_route( self::get_plugin_namespace(), '/menu-locations/(?P<location>[a-zA-Z0-9_-]+)', array(
102
+            register_rest_route(self::get_plugin_namespace(), '/menu-locations/(?P<location>[a-zA-Z0-9_-]+)', array(
103 103
                 array(
104 104
                     'methods'  => WP_REST_Server::READABLE,
105
-                    'callback' => array( $this, 'get_menu_location' ),
105
+                    'callback' => array($this, 'get_menu_location'),
106 106
                     'permission_callback' => '__return_true',
107 107
                 )
108
-            ) );
108
+            ));
109 109
         }
110 110
 
111 111
 
@@ -115,31 +115,31 @@  discard block
 block discarded – undo
115 115
          * @since  1.2.0
116 116
          * @return array All registered menus
117 117
          */
118
-        public static function get_menus($request =false) {
119
-            if(!$request)
118
+        public static function get_menus($request = false) {
119
+            if ( ! $request)
120 120
                 return [];
121 121
 
122 122
             $params = $request->get_params();
123 123
             
124 124
             $query_args = [];
125
-            if(isset($params['include']))
126
-                $query_args['include'] = explode(',',$params['include']);
125
+            if (isset($params['include']))
126
+                $query_args['include'] = explode(',', $params['include']);
127 127
 
128
-            $rest_url = trailingslashit( get_rest_url() . self::get_plugin_namespace() . '/menus/' );
128
+            $rest_url = trailingslashit(get_rest_url().self::get_plugin_namespace().'/menus/');
129 129
             $wp_menus = wp_get_nav_menus($query_args);
130 130
 
131 131
             // check if we should also include the actual menu; default to false
132
-            $include_items= (isset($params['include_menu_items']) && ($params['include_menu_items'] == "true")) ? true : false;
132
+            $include_items = (isset($params['include_menu_items']) && ($params['include_menu_items'] == "true")) ? true : false;
133 133
             
134 134
             $i = 0;
135 135
             $rest_menus = array();
136
-            foreach ( $wp_menus as $wp_menu ) :
136
+            foreach ($wp_menus as $wp_menu) :
137 137
 
138
-                $rest_menus[ $i ] = array_merge((array)$wp_menu,(new WP_REST_Menus)->get_menu(['id'=>$wp_menu->term_id],$include_items));
139
-                $i ++;
138
+                $rest_menus[$i] = array_merge((array) $wp_menu, (new WP_REST_Menus)->get_menu(['id'=>$wp_menu->term_id], $include_items));
139
+                $i++;
140 140
             endforeach;
141 141
 
142
-            return apply_filters( 'rest_menus_format_menus', $rest_menus );
142
+            return apply_filters('rest_menus_format_menus', $rest_menus);
143 143
         }
144 144
 
145 145
 
@@ -150,46 +150,46 @@  discard block
 block discarded – undo
150 150
          * @param  $request
151 151
          * @return array Menu data
152 152
          */
153
-        public function get_menu( $request,$include_items=true ) {
153
+        public function get_menu($request, $include_items = true) {
154 154
 
155 155
             $id             = (int) $request['id'];
156
-            $rest_url       = get_rest_url() . self::get_api_namespace() . '/menus/';
157
-            $wp_menu_object = $id ? wp_get_nav_menu_object( $id ) : array();            
156
+            $rest_url       = get_rest_url().self::get_api_namespace().'/menus/';
157
+            $wp_menu_object = $id ? wp_get_nav_menu_object($id) : array();            
158 158
 
159 159
             $rest_menu = array();
160 160
 
161
-            if ( $wp_menu_object ) :
161
+            if ($wp_menu_object) :
162 162
 
163 163
                 $menu = (array) $wp_menu_object;
164
-                $rest_menu['ID']          = abs( $menu['term_id'] );
164
+                $rest_menu['ID']          = abs($menu['term_id']);
165 165
                 $rest_menu['name']        = $menu['name'];
166 166
                 $rest_menu['slug']        = $menu['slug'];
167 167
                 $rest_menu['description'] = $menu['description'];
168
-                $rest_menu['count']       = abs( $menu['count'] );
168
+                $rest_menu['count']       = abs($menu['count']);
169 169
 
170 170
 
171
-                if($include_items){
172
-                    $wp_menu_items  = $id ? wp_get_nav_menu_items( $id ) : array();
171
+                if ($include_items) {
172
+                    $wp_menu_items = $id ? wp_get_nav_menu_items($id) : array();
173 173
 
174 174
                     $rest_menu_items = array();
175
-                    foreach ( $wp_menu_items as $item_object ) {
175
+                    foreach ($wp_menu_items as $item_object) {
176 176
                     
177
-	                    $rest_menu_items[] = $this->format_menu_item( $item_object );
177
+	                    $rest_menu_items[] = $this->format_menu_item($item_object);
178 178
                     }
179 179
 
180 180
                 
181 181
                     $rest_menu_items = $this->nested_menu_items($rest_menu_items, 0);
182 182
 
183
-                    $rest_menu['items']                       = $rest_menu_items;
183
+                    $rest_menu['items'] = $rest_menu_items;
184 184
                 }
185 185
 
186 186
                 // wp_die(print_r($rest_menu));
187 187
                 $rest_menu['meta']['links']['collection'] = $rest_url;
188
-                $rest_menu['meta']['links']['self']       = $rest_url . $id;
188
+                $rest_menu['meta']['links']['self']       = $rest_url.$id;
189 189
 
190 190
             endif;
191 191
 
192
-            return apply_filters( 'rest_menus_format_menu', $rest_menu );
192
+            return apply_filters('rest_menus_format_menu', $rest_menu);
193 193
         }
194 194
 
195 195
 
@@ -204,24 +204,24 @@  discard block
 block discarded – undo
204 204
          * @param  $parent
205 205
          * @return array
206 206
          */
207
-        private function nested_menu_items( &$menu_items, $parent = null ) {
207
+        private function nested_menu_items(&$menu_items, $parent = null) {
208 208
 
209 209
             $parents = array();
210 210
             $children = array();
211 211
 
212 212
             // Separate menu_items into parents & children.
213
-            array_map( function( $i ) use ( $parent, &$children, &$parents ){
214
-                if ( $i['id'] != $parent && $i['parent'] == $parent ) {
213
+            array_map(function($i) use ($parent, &$children, &$parents){
214
+                if ($i['id'] != $parent && $i['parent'] == $parent) {
215 215
                     $parents[] = $i;
216 216
                 } else {
217 217
                     $children[] = $i;
218 218
                 }
219
-            }, $menu_items );
219
+            }, $menu_items);
220 220
 
221
-            foreach ( $parents as &$parent ) {
221
+            foreach ($parents as &$parent) {
222 222
 
223
-                if ( $this->has_children( $children, $parent['id'] ) ) {
224
-                    $parent['children'] = $this->nested_menu_items( $children, $parent['id'] );
223
+                if ($this->has_children($children, $parent['id'])) {
224
+                    $parent['children'] = $this->nested_menu_items($children, $parent['id']);
225 225
                 }
226 226
             }
227 227
 
@@ -237,8 +237,8 @@  discard block
 block discarded – undo
237 237
          * @param  int $id
238 238
          * @return array
239 239
          */
240
-        private function has_children( $items, $id ) {
241
-            return array_filter( $items, function( $i ) use ( $id ) {
240
+        private function has_children($items, $id) {
241
+            return array_filter($items, function($i) use ($id) {
242 242
                 return $i['parent'] == $id;
243 243
             } );
244 244
         }
@@ -251,26 +251,26 @@  discard block
 block discarded – undo
251 251
          * @param  $request
252 252
          * @return array All registered menus locations
253 253
          */
254
-        public static function get_menu_locations( $request ) {
254
+        public static function get_menu_locations($request) {
255 255
 
256 256
             $locations        = get_nav_menu_locations();
257 257
             $registered_menus = get_registered_nav_menus();
258
-	        $rest_url         = get_rest_url() . self::get_api_namespace() . '/menu-locations/';
258
+	        $rest_url = get_rest_url().self::get_api_namespace().'/menu-locations/';
259 259
             $rest_menus       = array();
260 260
 
261
-            if ( $locations && $registered_menus ) :
261
+            if ($locations && $registered_menus) :
262 262
 
263
-                foreach ( $registered_menus as $slug => $label ) :
263
+                foreach ($registered_menus as $slug => $label) :
264 264
 
265 265
 	                // Sanity check
266
-	                if ( ! isset( $locations[ $slug ] ) ) {
266
+	                if ( ! isset($locations[$slug])) {
267 267
 		                continue;
268 268
 	                }
269 269
 
270
-	                $rest_menus[ $slug ]['ID']                          = $locations[ $slug ];
271
-                    $rest_menus[ $slug ]['label']                       = $label;
272
-                    $rest_menus[ $slug ]['meta']['links']['collection'] = $rest_url;
273
-                    $rest_menus[ $slug ]['meta']['links']['self']       = $rest_url . $slug;
270
+	                $rest_menus[$slug]['ID'] = $locations[$slug];
271
+                    $rest_menus[$slug]['label']                       = $label;
272
+                    $rest_menus[$slug]['meta']['links']['collection'] = $rest_url;
273
+                    $rest_menus[$slug]['meta']['links']['self']       = $rest_url.$slug;
274 274
 
275 275
                 endforeach;
276 276
 
@@ -287,68 +287,68 @@  discard block
 block discarded – undo
287 287
          * @param  $request
288 288
          * @return array The menu for the corresponding location
289 289
          */
290
-        public function get_menu_location( $request ) {
290
+        public function get_menu_location($request) {
291 291
 
292 292
             $params     = $request->get_params();
293 293
             $location   = $params['location'];
294 294
             $locations  = get_nav_menu_locations();
295 295
 
296
-            if ( ! isset( $locations[ $location ] ) ) {
296
+            if ( ! isset($locations[$location])) {
297 297
 	            return array();
298 298
             }
299 299
 
300
-            $wp_menu = wp_get_nav_menu_object( $locations[ $location ] );
301
-            $menu_items = wp_get_nav_menu_items( $wp_menu->term_id );
300
+            $wp_menu = wp_get_nav_menu_object($locations[$location]);
301
+            $menu_items = wp_get_nav_menu_items($wp_menu->term_id);
302 302
 			/**
303 303
 			 * wp_get_nav_menu_items() outputs a list that's already sequenced correctly.
304 304
 			 * So the easiest thing to do is to reverse the list and then build our tree
305 305
 			 * from the ground up
306 306
 			 */
307
-			$rev_items = array_reverse ( $menu_items );
307
+			$rev_items = array_reverse($menu_items);
308 308
 			$rev_menu  = array();
309 309
 			$cache     = array();
310 310
 
311
-			foreach ( $rev_items as $item ) :
311
+			foreach ($rev_items as $item) :
312 312
 				$formatted = array(
313
-					'ID'          => abs( $item->ID ),
313
+					'ID'          => abs($item->ID),
314 314
 					'order'       => (int) $item->menu_order,
315
-					'parent'      => abs( $item->menu_item_parent ),
315
+					'parent'      => abs($item->menu_item_parent),
316 316
 					'title'       => $item->title,
317 317
 					'url'         => $item->url,
318 318
 					'attr'        => $item->attr_title,
319 319
 					'target'      => $item->target,
320
-					'classes'     => implode( ' ', $item->classes ),
320
+					'classes'     => implode(' ', $item->classes),
321 321
 					'xfn'         => $item->xfn,
322 322
 					'description' => $item->description,
323
-					'object_id'   => abs( $item->object_id ),
323
+					'object_id'   => abs($item->object_id),
324 324
 					'object'      => $item->object,
325 325
 					'type'        => $item->type,
326 326
                     'type_label'  => $item->type_label,
327 327
 					'children'    => array(),
328 328
 				);
329 329
 
330
-				if ( array_key_exists( $item->ID , $cache ) ) {
331
-					$formatted['children'] = array_reverse( $cache[ $item->ID ] );
330
+				if (array_key_exists($item->ID, $cache)) {
331
+					$formatted['children'] = array_reverse($cache[$item->ID]);
332 332
 				}
333 333
 
334
-            	$formatted = apply_filters( 'rest_menus_format_menu_item', $formatted );
334
+            	$formatted = apply_filters('rest_menus_format_menu_item', $formatted);
335 335
 
336
-				if ( $item->menu_item_parent != 0 ) {
336
+				if ($item->menu_item_parent != 0) {
337 337
 
338
-					if ( array_key_exists( $item->menu_item_parent , $cache ) ) {
339
-						array_push( $cache[ $item->menu_item_parent ], $formatted );
338
+					if (array_key_exists($item->menu_item_parent, $cache)) {
339
+						array_push($cache[$item->menu_item_parent], $formatted);
340 340
 					} else {
341
-						$cache[ $item->menu_item_parent ] = array( $formatted, );
341
+						$cache[$item->menu_item_parent] = array($formatted,);
342 342
 					}
343 343
 
344 344
 				} else {
345 345
 
346
-					array_push( $rev_menu, $formatted );
346
+					array_push($rev_menu, $formatted);
347 347
 				}
348 348
 
349 349
 			endforeach;
350 350
 
351
-			return array_reverse ( $rev_menu );
351
+			return array_reverse($rev_menu);
352 352
         }
353 353
 
354 354
 
@@ -361,19 +361,19 @@  discard block
 block discarded – undo
361 361
          * @param bool  $depth          Gives all children or direct children only
362 362
          * @return array	returns filtered array of nav_menu_items
363 363
          */
364
-        public function get_nav_menu_item_children( $parent_id, $nav_menu_items, $depth = true ) {
364
+        public function get_nav_menu_item_children($parent_id, $nav_menu_items, $depth = true) {
365 365
 
366 366
             $nav_menu_item_list = array();
367 367
 
368
-            foreach ( (array) $nav_menu_items as $nav_menu_item ) :
368
+            foreach ((array) $nav_menu_items as $nav_menu_item) :
369 369
 
370
-                if ( $nav_menu_item->menu_item_parent == $parent_id ) :
370
+                if ($nav_menu_item->menu_item_parent == $parent_id) :
371 371
 
372
-                    $nav_menu_item_list[] = $this->format_menu_item( $nav_menu_item, true, $nav_menu_items );
372
+                    $nav_menu_item_list[] = $this->format_menu_item($nav_menu_item, true, $nav_menu_items);
373 373
 
374
-                    if ( $depth ) {
375
-                        if ( $children = $this->get_nav_menu_item_children( $nav_menu_item->ID, $nav_menu_items ) ) {
376
-                            $nav_menu_item_list = array_merge( $nav_menu_item_list, $children );
374
+                    if ($depth) {
375
+                        if ($children = $this->get_nav_menu_item_children($nav_menu_item->ID, $nav_menu_items)) {
376
+                            $nav_menu_item_list = array_merge($nav_menu_item_list, $children);
377 377
                         }
378 378
                     }
379 379
 
@@ -394,33 +394,33 @@  discard block
 block discarded – undo
394 394
          * @param  array        $menu       The menu the item belongs to (used when $children is set to true)
395 395
          * @return array	a formatted menu item for REST
396 396
          */
397
-        public function format_menu_item( $menu_item, $children = false, $menu = array() ) {
397
+        public function format_menu_item($menu_item, $children = false, $menu = array()) {
398 398
 
399 399
             $item = (array) $menu_item;
400
-            $id = (int)$item['ID'];
400
+            $id = (int) $item['ID'];
401 401
             $menu_item = array(
402
-                'id'          => abs( $item['ID'] ),
402
+                'id'          => abs($item['ID']),
403 403
                 'order'       => (int) $item['menu_order'],
404
-                'parent'      => abs( $item['menu_item_parent'] ),
404
+                'parent'      => abs($item['menu_item_parent']),
405 405
                 'title'       => $item['title'],
406 406
                 'url'         => $item['url'],
407 407
                 'attr'        => $item['attr_title'],
408 408
                 'target'      => $item['target'],
409
-                'classes'     => implode( ' ', $item['classes'] ),
409
+                'classes'     => implode(' ', $item['classes']),
410 410
                 'xfn'         => $item['xfn'],
411 411
                 'description' => $item['description'],
412
-                'object_id'   => abs( $item['object_id'] ),
412
+                'object_id'   => abs($item['object_id']),
413 413
                 'object'      => $item['object'],
414 414
                 'object_slug' => $item['post_name'],
415 415
                 'type'        => $item['type'],
416 416
                 'type_label'  => $item['type_label']
417 417
             );
418 418
 
419
-            if ( $children === true && ! empty( $menu ) ) {
420
-	            $menu_item['children'] = $this->get_nav_menu_item_children( $item['ID'], $menu );
419
+            if ($children === true && ! empty($menu)) {
420
+	            $menu_item['children'] = $this->get_nav_menu_item_children($item['ID'], $menu);
421 421
             }
422 422
 
423
-            return apply_filters( 'rest_menus_format_menu_item', $menu_item );
423
+            return apply_filters('rest_menus_format_menu_item', $menu_item);
424 424
         }
425 425
 
426 426
 
Please login to merge, or discard this patch.