@@ -191,9 +191,7 @@ |
||
191 | 191 | |
192 | 192 | if ( empty( $parent ) ) { |
193 | 193 | unset($child_items[$key]); |
194 | - } |
|
195 | - |
|
196 | - else if (wp_api_v2_menus_dna_test($menu_items, $child_item)) { |
|
194 | + } else if (wp_api_v2_menus_dna_test($menu_items, $child_item)) { |
|
197 | 195 | unset($child_items[$key]); |
198 | 196 | } |
199 | 197 | } |
@@ -140,10 +140,10 @@ |
||
140 | 140 | function wp_api_v2_menus_get_menu_items( $id ) { |
141 | 141 | $menu_items = wp_get_nav_menu_items( $id ); |
142 | 142 | |
143 | - // fallback: if menu_items is null then return empty array |
|
144 | - if($menu_items === false) { |
|
145 | - return []; |
|
146 | - } |
|
143 | + // fallback: if menu_items is null then return empty array |
|
144 | + if($menu_items === false) { |
|
145 | + return []; |
|
146 | + } |
|
147 | 147 | |
148 | 148 | $all_menu_items = $menu_items; |
149 | 149 |
@@ -12,18 +12,18 @@ discard block |
||
12 | 12 | * @return array List of menus with slug and description |
13 | 13 | */ |
14 | 14 | function wp_api_v2_menus_get_all_menus() { |
15 | - $menus = get_terms( 'nav_menu', array( 'hide_empty' => true ) ); |
|
15 | + $menus = get_terms('nav_menu', array('hide_empty' => true)); |
|
16 | 16 | |
17 | - foreach ( $menus as $key => $menu ) { |
|
17 | + foreach ($menus as $key => $menu) { |
|
18 | 18 | // check if there is acf installed |
19 | - if ( class_exists( 'acf' ) ) { |
|
20 | - $fields = get_fields( $menu ); |
|
21 | - if ( ! empty( $fields ) ) { |
|
22 | - $menus[ $key ]->acf = new stdClass(); |
|
19 | + if (class_exists('acf')) { |
|
20 | + $fields = get_fields($menu); |
|
21 | + if ( ! empty($fields)) { |
|
22 | + $menus[$key]->acf = new stdClass(); |
|
23 | 23 | |
24 | - foreach ( $fields as $field_key => $item ) { |
|
24 | + foreach ($fields as $field_key => $item) { |
|
25 | 25 | // add all acf custom fields |
26 | - $menus[ $key ]->acf->$field_key = $item; |
|
26 | + $menus[$key]->acf->$field_key = $item; |
|
27 | 27 | } |
28 | 28 | } |
29 | 29 | } |
@@ -40,14 +40,14 @@ discard block |
||
40 | 40 | function wp_api_v2_menu_get_all_locations() { |
41 | 41 | $nav_menu_locations = get_nav_menu_locations(); |
42 | 42 | $locations = new stdClass; |
43 | - foreach ( $nav_menu_locations as $location_slug => $menu_id ) { |
|
44 | - if ( get_term( $location_slug ) !== null ) { |
|
45 | - $locations->{$location_slug} = get_term( $location_slug ); |
|
43 | + foreach ($nav_menu_locations as $location_slug => $menu_id) { |
|
44 | + if (get_term($location_slug) !== null) { |
|
45 | + $locations->{$location_slug} = get_term($location_slug); |
|
46 | 46 | } else { |
47 | 47 | $locations->{$location_slug} = new stdClass; |
48 | 48 | } |
49 | 49 | $locations->{$location_slug}->slug = $location_slug; |
50 | - $locations->{$location_slug}->menu = get_term( $menu_id ); |
|
50 | + $locations->{$location_slug}->menu = get_term($menu_id); |
|
51 | 51 | } |
52 | 52 | |
53 | 53 | return apply_filters('wp_api_v2_menus__locations', $locations); |
@@ -60,31 +60,31 @@ discard block |
||
60 | 60 | * |
61 | 61 | * @return object Menu's data with his items |
62 | 62 | */ |
63 | -function wp_api_v2_locations_get_menu_data( $data ) { |
|
63 | +function wp_api_v2_locations_get_menu_data($data) { |
|
64 | 64 | // Create default empty object |
65 | 65 | $menu = new stdClass; |
66 | 66 | |
67 | 67 | // this could be replaced with `if (has_nav_menu($data['id']))` |
68 | - if ( ( $locations = get_nav_menu_locations() ) && isset( $locations[ $data['id'] ] ) ) { |
|
68 | + if (($locations = get_nav_menu_locations()) && isset($locations[$data['id']])) { |
|
69 | 69 | // Replace default empty object with the location object |
70 | - $menu = get_term( $locations[ $data['id'] ] ); |
|
70 | + $menu = get_term($locations[$data['id']]); |
|
71 | 71 | |
72 | - if ( is_wp_error( $menu ) || null === $menu ) { |
|
73 | - return new WP_Error( 'not_found', 'No location has been found with this id or slug: `' . $data['id'] . '`. Please ensure you passed an existing location ID or location slug.', array( 'status' => 404 ) ); |
|
72 | + if (is_wp_error($menu) || null === $menu) { |
|
73 | + return new WP_Error('not_found', 'No location has been found with this id or slug: `' . $data['id'] . '`. Please ensure you passed an existing location ID or location slug.', array('status' => 404)); |
|
74 | 74 | } |
75 | 75 | |
76 | - $menu->items = wp_api_v2_menus_get_menu_items( $locations[ $data['id'] ] ); |
|
76 | + $menu->items = wp_api_v2_menus_get_menu_items($locations[$data['id']]); |
|
77 | 77 | } else { |
78 | - return new WP_Error( 'not_found', 'No location has been found with this id or slug: `' . $data['id'] . '`. Please ensure you passed an existing location ID or location slug.', array( 'status' => 404 ) ); |
|
78 | + return new WP_Error('not_found', 'No location has been found with this id or slug: `' . $data['id'] . '`. Please ensure you passed an existing location ID or location slug.', array('status' => 404)); |
|
79 | 79 | } |
80 | 80 | |
81 | 81 | // check if there is acf installed |
82 | - if ( class_exists( 'acf' ) ) { |
|
83 | - $fields = get_fields( $menu ); |
|
84 | - if ( ! empty( $fields ) ) { |
|
82 | + if (class_exists('acf')) { |
|
83 | + $fields = get_fields($menu); |
|
84 | + if ( ! empty($fields)) { |
|
85 | 85 | $menu->acf = new stdClass(); |
86 | 86 | |
87 | - foreach ( $fields as $field_key => $item ) { |
|
87 | + foreach ($fields as $field_key => $item) { |
|
88 | 88 | // add all acf custom fields |
89 | 89 | $menu->acf->$field_key = $item; |
90 | 90 | } |
@@ -102,18 +102,18 @@ discard block |
||
102 | 102 | * |
103 | 103 | * @return bool True if the parent is found, false otherwise |
104 | 104 | */ |
105 | -function wp_api_v2_menus_dna_test( &$parents, $child ) { |
|
106 | - foreach ( $parents as $key => $item ) { |
|
107 | - if ( $child->menu_item_parent == $item->ID ) { |
|
108 | - if ( ! $item->child_items ) { |
|
105 | +function wp_api_v2_menus_dna_test(&$parents, $child) { |
|
106 | + foreach ($parents as $key => $item) { |
|
107 | + if ($child->menu_item_parent == $item->ID) { |
|
108 | + if ( ! $item->child_items) { |
|
109 | 109 | $item->child_items = []; |
110 | 110 | } |
111 | - array_push( $item->child_items, $child ); |
|
111 | + array_push($item->child_items, $child); |
|
112 | 112 | return true; |
113 | 113 | } |
114 | 114 | |
115 | - if($item->child_items) { |
|
116 | - if(wp_api_v2_menus_dna_test($item->child_items, $child)) { |
|
115 | + if ($item->child_items) { |
|
116 | + if (wp_api_v2_menus_dna_test($item->child_items, $child)) { |
|
117 | 117 | return true; |
118 | 118 | } |
119 | 119 | } |
@@ -125,9 +125,9 @@ discard block |
||
125 | 125 | /** |
126 | 126 | * Search object in an array by ID |
127 | 127 | */ |
128 | -function wp_api_v2_find_object_by_id( $array, $id ) { |
|
129 | - foreach ( $array as $element ) { |
|
130 | - if ( $id == $element->ID ) { |
|
128 | +function wp_api_v2_find_object_by_id($array, $id) { |
|
129 | + foreach ($array as $element) { |
|
130 | + if ($id == $element->ID) { |
|
131 | 131 | return $element; |
132 | 132 | } |
133 | 133 | } |
@@ -142,26 +142,26 @@ discard block |
||
142 | 142 | * |
143 | 143 | * @return array List of menu items |
144 | 144 | */ |
145 | -function wp_api_v2_menus_get_menu_items( $id ) { |
|
146 | - $menu_items = wp_get_nav_menu_items( $id ); |
|
145 | +function wp_api_v2_menus_get_menu_items($id) { |
|
146 | + $menu_items = wp_get_nav_menu_items($id); |
|
147 | 147 | |
148 | 148 | // fallback: if menu_items is null then return empty array |
149 | - if($menu_items === false) { |
|
149 | + if ($menu_items === false) { |
|
150 | 150 | return []; |
151 | 151 | } |
152 | 152 | |
153 | 153 | $all_menu_items = $menu_items; |
154 | 154 | |
155 | 155 | // check if there is acf installed |
156 | - if ( class_exists( 'acf' ) ) { |
|
157 | - foreach ( $menu_items as $menu_key => $menu_item ) { |
|
158 | - $fields = get_fields( $menu_item->ID ); |
|
159 | - if ( ! empty( $fields ) ) { |
|
156 | + if (class_exists('acf')) { |
|
157 | + foreach ($menu_items as $menu_key => $menu_item) { |
|
158 | + $fields = get_fields($menu_item->ID); |
|
159 | + if ( ! empty($fields)) { |
|
160 | 160 | $menu_items[$menu_key]->acf = new stdClass(); |
161 | 161 | |
162 | - foreach ( $fields as $field_key => $item ) { |
|
162 | + foreach ($fields as $field_key => $item) { |
|
163 | 163 | // add all acf custom fields |
164 | - $menu_items[ $menu_key ]->acf->$field_key = $item; |
|
164 | + $menu_items[$menu_key]->acf->$field_key = $item; |
|
165 | 165 | } |
166 | 166 | } |
167 | 167 | } |
@@ -170,16 +170,16 @@ discard block |
||
170 | 170 | // wordpress does not group child menu items with parent menu items |
171 | 171 | $child_items = []; |
172 | 172 | // pull all child menu items into separate object |
173 | - foreach ( $menu_items as $key => $item ) { |
|
173 | + foreach ($menu_items as $key => $item) { |
|
174 | 174 | |
175 | - if($item->type == 'post_type') { |
|
175 | + if ($item->type == 'post_type') { |
|
176 | 176 | // add slug to menu items |
177 | - $slug = get_post_field( 'post_name', $item->object_id ); |
|
177 | + $slug = get_post_field('post_name', $item->object_id); |
|
178 | 178 | $item->slug = $slug; |
179 | - } else if($item->type == 'taxonomy') { |
|
179 | + } else if ($item->type == 'taxonomy') { |
|
180 | 180 | $cat = get_term($item->object_id); |
181 | 181 | $item->slug = $cat->slug; |
182 | - } else if($item->type == 'post_type_archive') { |
|
182 | + } else if ($item->type == 'post_type_archive') { |
|
183 | 183 | $post_type_data = get_post_type_object($item->object); |
184 | 184 | |
185 | 185 | if ($post_type_data->has_archive) { |
@@ -194,19 +194,19 @@ discard block |
||
194 | 194 | $item->thumbnail_hover_src = wp_get_attachment_image_url(intval($item->thumbnail_hover_id), 'post-thumbnail'); |
195 | 195 | } |
196 | 196 | |
197 | - if ( $item->menu_item_parent ) { |
|
198 | - array_push( $child_items, $item ); |
|
199 | - unset( $menu_items[ $key ] ); |
|
197 | + if ($item->menu_item_parent) { |
|
198 | + array_push($child_items, $item); |
|
199 | + unset($menu_items[$key]); |
|
200 | 200 | } |
201 | 201 | |
202 | 202 | } |
203 | 203 | |
204 | 204 | // push child items into their parent item in the original object |
205 | 205 | do { |
206 | - foreach($child_items as $key => $child_item) { |
|
207 | - $parent = wp_api_v2_find_object_by_id( $all_menu_items, $child_item->menu_item_parent ); |
|
206 | + foreach ($child_items as $key => $child_item) { |
|
207 | + $parent = wp_api_v2_find_object_by_id($all_menu_items, $child_item->menu_item_parent); |
|
208 | 208 | |
209 | - if ( empty( $parent ) ) { |
|
209 | + if (empty($parent)) { |
|
210 | 210 | unset($child_items[$key]); |
211 | 211 | } |
212 | 212 | |
@@ -214,7 +214,7 @@ discard block |
||
214 | 214 | unset($child_items[$key]); |
215 | 215 | } |
216 | 216 | } |
217 | - } while(count($child_items)); |
|
217 | + } while (count($child_items)); |
|
218 | 218 | |
219 | 219 | return apply_filters('wp_api_v2_menus__menu_items', array_values($menu_items)); |
220 | 220 | } |
@@ -228,30 +228,30 @@ discard block |
||
228 | 228 | * |
229 | 229 | * @return object Menu's data with his items |
230 | 230 | */ |
231 | -function wp_api_v2_menus_get_menu_data( $data ) { |
|
231 | +function wp_api_v2_menus_get_menu_data($data) { |
|
232 | 232 | // This ensure retro compatibility with versions `<= 0.5` when this endpoint |
233 | 233 | // was allowing locations id in place of menus id |
234 | - if ( has_nav_menu( $data['id'] ) ) { |
|
235 | - $menu = wp_api_v2_locations_get_menu_data( $data ); |
|
236 | - } else if ( is_nav_menu( $data['id'] ) ) { |
|
237 | - if ( is_int( $data['id'] ) ) { |
|
234 | + if (has_nav_menu($data['id'])) { |
|
235 | + $menu = wp_api_v2_locations_get_menu_data($data); |
|
236 | + } else if (is_nav_menu($data['id'])) { |
|
237 | + if (is_int($data['id'])) { |
|
238 | 238 | $id = $data['id']; |
239 | 239 | } else { |
240 | - $id = wp_get_nav_menu_object( $data['id'] ); |
|
240 | + $id = wp_get_nav_menu_object($data['id']); |
|
241 | 241 | } |
242 | - $menu = get_term( $id ); |
|
243 | - $menu->items = wp_api_v2_menus_get_menu_items( $id ); |
|
242 | + $menu = get_term($id); |
|
243 | + $menu->items = wp_api_v2_menus_get_menu_items($id); |
|
244 | 244 | } else { |
245 | - return new WP_Error( 'not_found', 'No menu has been found with this id or slug: `' . $data['id'] . '`. Please ensure you passed an existing menu ID, menu slug, location ID or location slug.', array( 'status' => 404 ) ); |
|
245 | + return new WP_Error('not_found', 'No menu has been found with this id or slug: `' . $data['id'] . '`. Please ensure you passed an existing menu ID, menu slug, location ID or location slug.', array('status' => 404)); |
|
246 | 246 | } |
247 | 247 | |
248 | 248 | // check if there is acf installed |
249 | - if ( class_exists( 'acf' ) ) { |
|
250 | - $fields = get_fields( $menu ); |
|
251 | - if ( ! empty( $fields ) ) { |
|
249 | + if (class_exists('acf')) { |
|
250 | + $fields = get_fields($menu); |
|
251 | + if ( ! empty($fields)) { |
|
252 | 252 | $menu->acf = new stdClass(); |
253 | 253 | |
254 | - foreach ( $fields as $field_key => $item ) { |
|
254 | + foreach ($fields as $field_key => $item) { |
|
255 | 255 | // add all acf custom fields |
256 | 256 | $menu->acf->$field_key = $item; |
257 | 257 | } |
@@ -261,28 +261,28 @@ discard block |
||
261 | 261 | return apply_filters('wp_api_v2_menus__menu', $menu); |
262 | 262 | } |
263 | 263 | |
264 | -add_action( 'rest_api_init', function () { |
|
265 | - register_rest_route( 'menus/v1', '/menus', array( |
|
264 | +add_action('rest_api_init', function() { |
|
265 | + register_rest_route('menus/v1', '/menus', array( |
|
266 | 266 | 'methods' => 'GET', |
267 | 267 | 'callback' => 'wp_api_v2_menus_get_all_menus', |
268 | 268 | 'permission_callback' => '__return_true' |
269 | - ) ); |
|
269 | + )); |
|
270 | 270 | |
271 | - register_rest_route( 'menus/v1', '/menus/(?P<id>[a-zA-Z0-9_-]+)', array( |
|
271 | + register_rest_route('menus/v1', '/menus/(?P<id>[a-zA-Z0-9_-]+)', array( |
|
272 | 272 | 'methods' => 'GET', |
273 | 273 | 'callback' => 'wp_api_v2_menus_get_menu_data', |
274 | 274 | 'permission_callback' => '__return_true' |
275 | - ) ); |
|
275 | + )); |
|
276 | 276 | |
277 | - register_rest_route( 'menus/v1', '/locations/(?P<id>[a-zA-Z0-9_-]+)', array( |
|
277 | + register_rest_route('menus/v1', '/locations/(?P<id>[a-zA-Z0-9_-]+)', array( |
|
278 | 278 | 'methods' => 'GET', |
279 | 279 | 'callback' => 'wp_api_v2_locations_get_menu_data', |
280 | 280 | 'permission_callback' => '__return_true' |
281 | - ) ); |
|
281 | + )); |
|
282 | 282 | |
283 | - register_rest_route( 'menus/v1', '/locations', array( |
|
283 | + register_rest_route('menus/v1', '/locations', array( |
|
284 | 284 | 'methods' => 'GET', |
285 | 285 | 'callback' => 'wp_api_v2_menu_get_all_locations', |
286 | 286 | 'permission_callback' => '__return_true' |
287 | - ) ); |
|
287 | + )); |
|
288 | 288 | } ); |