@@ -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,26 +60,26 @@ 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'] ] ); |
|
71 | - $menu->items = wp_api_v2_menus_get_menu_items( $locations[ $data['id'] ] ); |
|
70 | + $menu = get_term($locations[$data['id']]); |
|
71 | + $menu->items = wp_api_v2_menus_get_menu_items($locations[$data['id']]); |
|
72 | 72 | } else { |
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 ) ); |
|
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 | 76 | // check if there is acf installed |
77 | - if ( class_exists( 'acf' ) ) { |
|
78 | - $fields = get_fields( $menu ); |
|
79 | - if ( ! empty( $fields ) ) { |
|
77 | + if (class_exists('acf')) { |
|
78 | + $fields = get_fields($menu); |
|
79 | + if ( ! empty($fields)) { |
|
80 | 80 | $menu->acf = new stdClass(); |
81 | 81 | |
82 | - foreach ( $fields as $field_key => $item ) { |
|
82 | + foreach ($fields as $field_key => $item) { |
|
83 | 83 | // add all acf custom fields |
84 | 84 | $menu->acf->$field_key = $item; |
85 | 85 | } |
@@ -97,18 +97,18 @@ discard block |
||
97 | 97 | * |
98 | 98 | * @return bool True if the parent is found, false otherwise |
99 | 99 | */ |
100 | -function wp_api_v2_menus_dna_test( &$parents, $child ) { |
|
101 | - foreach ( $parents as $key => $item ) { |
|
102 | - if ( $child->menu_item_parent == $item->ID ) { |
|
103 | - if ( ! $item->child_items ) { |
|
100 | +function wp_api_v2_menus_dna_test(&$parents, $child) { |
|
101 | + foreach ($parents as $key => $item) { |
|
102 | + if ($child->menu_item_parent == $item->ID) { |
|
103 | + if ( ! $item->child_items) { |
|
104 | 104 | $item->child_items = []; |
105 | 105 | } |
106 | - array_push( $item->child_items, $child ); |
|
106 | + array_push($item->child_items, $child); |
|
107 | 107 | return true; |
108 | 108 | } |
109 | 109 | |
110 | - if($item->child_items) { |
|
111 | - if(wp_api_v2_menus_dna_test($item->child_items, $child)) { |
|
110 | + if ($item->child_items) { |
|
111 | + if (wp_api_v2_menus_dna_test($item->child_items, $child)) { |
|
112 | 112 | return true; |
113 | 113 | } |
114 | 114 | } |
@@ -120,9 +120,9 @@ discard block |
||
120 | 120 | /** |
121 | 121 | * Search object in an array by ID |
122 | 122 | */ |
123 | -function wp_api_v2_find_object_by_id( $array, $id ) { |
|
124 | - foreach ( $array as $element ) { |
|
125 | - if ( $id == $element->ID ) { |
|
123 | +function wp_api_v2_find_object_by_id($array, $id) { |
|
124 | + foreach ($array as $element) { |
|
125 | + if ($id == $element->ID) { |
|
126 | 126 | return $element; |
127 | 127 | } |
128 | 128 | } |
@@ -137,26 +137,26 @@ discard block |
||
137 | 137 | * |
138 | 138 | * @return array List of menu items |
139 | 139 | */ |
140 | -function wp_api_v2_menus_get_menu_items( $id ) { |
|
141 | - $menu_items = wp_get_nav_menu_items( $id ); |
|
140 | +function wp_api_v2_menus_get_menu_items($id) { |
|
141 | + $menu_items = wp_get_nav_menu_items($id); |
|
142 | 142 | |
143 | 143 | // fallback: if menu_items is null then return empty array |
144 | - if($menu_items === false) { |
|
144 | + if ($menu_items === false) { |
|
145 | 145 | return []; |
146 | 146 | } |
147 | 147 | |
148 | 148 | $all_menu_items = $menu_items; |
149 | 149 | |
150 | 150 | // check if there is acf installed |
151 | - if ( class_exists( 'acf' ) ) { |
|
152 | - foreach ( $menu_items as $menu_key => $menu_item ) { |
|
153 | - $fields = get_fields( $menu_item->ID ); |
|
154 | - if ( ! empty( $fields ) ) { |
|
151 | + if (class_exists('acf')) { |
|
152 | + foreach ($menu_items as $menu_key => $menu_item) { |
|
153 | + $fields = get_fields($menu_item->ID); |
|
154 | + if ( ! empty($fields)) { |
|
155 | 155 | $menu_items[$menu_key]->acf = new stdClass(); |
156 | 156 | |
157 | - foreach ( $fields as $field_key => $item ) { |
|
157 | + foreach ($fields as $field_key => $item) { |
|
158 | 158 | // add all acf custom fields |
159 | - $menu_items[ $menu_key ]->acf->$field_key = $item; |
|
159 | + $menu_items[$menu_key]->acf->$field_key = $item; |
|
160 | 160 | } |
161 | 161 | } |
162 | 162 | } |
@@ -165,16 +165,16 @@ discard block |
||
165 | 165 | // wordpress does not group child menu items with parent menu items |
166 | 166 | $child_items = []; |
167 | 167 | // pull all child menu items into separate object |
168 | - foreach ( $menu_items as $key => $item ) { |
|
168 | + foreach ($menu_items as $key => $item) { |
|
169 | 169 | |
170 | - if($item->type == 'post_type') { |
|
170 | + if ($item->type == 'post_type') { |
|
171 | 171 | // add slug to menu items |
172 | - $slug = get_post_field( 'post_name', $item->object_id ); |
|
172 | + $slug = get_post_field('post_name', $item->object_id); |
|
173 | 173 | $item->slug = $slug; |
174 | - } else if($item->type == 'taxonomy') { |
|
174 | + } else if ($item->type == 'taxonomy') { |
|
175 | 175 | $cat = get_term($item->object_id); |
176 | 176 | $item->slug = $cat->slug; |
177 | - } else if($item->type == 'post_type_archive') { |
|
177 | + } else if ($item->type == 'post_type_archive') { |
|
178 | 178 | $post_type_data = get_post_type_object($item->object); |
179 | 179 | |
180 | 180 | if ($post_type_data->has_archive) { |
@@ -189,19 +189,19 @@ discard block |
||
189 | 189 | $item->thumbnail_hover_src = wp_get_attachment_image_url(intval($item->thumbnail_hover_id), 'post-thumbnail'); |
190 | 190 | } |
191 | 191 | |
192 | - if ( $item->menu_item_parent ) { |
|
193 | - array_push( $child_items, $item ); |
|
194 | - unset( $menu_items[ $key ] ); |
|
192 | + if ($item->menu_item_parent) { |
|
193 | + array_push($child_items, $item); |
|
194 | + unset($menu_items[$key]); |
|
195 | 195 | } |
196 | 196 | |
197 | 197 | } |
198 | 198 | |
199 | 199 | // push child items into their parent item in the original object |
200 | 200 | do { |
201 | - foreach($child_items as $key => $child_item) { |
|
202 | - $parent = wp_api_v2_find_object_by_id( $all_menu_items, $child_item->menu_item_parent ); |
|
201 | + foreach ($child_items as $key => $child_item) { |
|
202 | + $parent = wp_api_v2_find_object_by_id($all_menu_items, $child_item->menu_item_parent); |
|
203 | 203 | |
204 | - if ( empty( $parent ) ) { |
|
204 | + if (empty($parent)) { |
|
205 | 205 | unset($child_items[$key]); |
206 | 206 | } |
207 | 207 | |
@@ -209,7 +209,7 @@ discard block |
||
209 | 209 | unset($child_items[$key]); |
210 | 210 | } |
211 | 211 | } |
212 | - } while(count($child_items)); |
|
212 | + } while (count($child_items)); |
|
213 | 213 | |
214 | 214 | return apply_filters('wp_api_v2_menus__menu_items', array_values($menu_items)); |
215 | 215 | } |
@@ -223,30 +223,30 @@ discard block |
||
223 | 223 | * |
224 | 224 | * @return object Menu's data with his items |
225 | 225 | */ |
226 | -function wp_api_v2_menus_get_menu_data( $data ) { |
|
226 | +function wp_api_v2_menus_get_menu_data($data) { |
|
227 | 227 | // This ensure retro compatibility with versions `<= 0.5` when this endpoint |
228 | 228 | // was allowing locations id in place of menus id |
229 | - if ( has_nav_menu( $data['id'] ) ) { |
|
230 | - $menu = wp_api_v2_locations_get_menu_data( $data ); |
|
231 | - } else if ( is_nav_menu( $data['id'] ) ) { |
|
232 | - if ( is_int( $data['id'] ) ) { |
|
229 | + if (has_nav_menu($data['id'])) { |
|
230 | + $menu = wp_api_v2_locations_get_menu_data($data); |
|
231 | + } else if (is_nav_menu($data['id'])) { |
|
232 | + if (is_int($data['id'])) { |
|
233 | 233 | $id = $data['id']; |
234 | 234 | } else { |
235 | - $id = wp_get_nav_menu_object( $data['id'] ); |
|
235 | + $id = wp_get_nav_menu_object($data['id']); |
|
236 | 236 | } |
237 | - $menu = get_term( $id ); |
|
238 | - $menu->items = wp_api_v2_menus_get_menu_items( $id ); |
|
237 | + $menu = get_term($id); |
|
238 | + $menu->items = wp_api_v2_menus_get_menu_items($id); |
|
239 | 239 | } else { |
240 | - 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 ) ); |
|
240 | + 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)); |
|
241 | 241 | } |
242 | 242 | |
243 | 243 | // check if there is acf installed |
244 | - if ( class_exists( 'acf' ) ) { |
|
245 | - $fields = get_fields( $menu ); |
|
246 | - if ( ! empty( $fields ) ) { |
|
244 | + if (class_exists('acf')) { |
|
245 | + $fields = get_fields($menu); |
|
246 | + if ( ! empty($fields)) { |
|
247 | 247 | $menu->acf = new stdClass(); |
248 | 248 | |
249 | - foreach ( $fields as $field_key => $item ) { |
|
249 | + foreach ($fields as $field_key => $item) { |
|
250 | 250 | // add all acf custom fields |
251 | 251 | $menu->acf->$field_key = $item; |
252 | 252 | } |
@@ -256,28 +256,28 @@ discard block |
||
256 | 256 | return apply_filters('wp_api_v2_menus__menu', $menu); |
257 | 257 | } |
258 | 258 | |
259 | -add_action( 'rest_api_init', function () { |
|
260 | - register_rest_route( 'menus/v1', '/menus', array( |
|
259 | +add_action('rest_api_init', function() { |
|
260 | + register_rest_route('menus/v1', '/menus', array( |
|
261 | 261 | 'methods' => 'GET', |
262 | 262 | 'callback' => 'wp_api_v2_menus_get_all_menus', |
263 | 263 | 'permission_callback' => '__return_true' |
264 | - ) ); |
|
264 | + )); |
|
265 | 265 | |
266 | - register_rest_route( 'menus/v1', '/menus/(?P<id>[a-zA-Z0-9_-]+)', array( |
|
266 | + register_rest_route('menus/v1', '/menus/(?P<id>[a-zA-Z0-9_-]+)', array( |
|
267 | 267 | 'methods' => 'GET', |
268 | 268 | 'callback' => 'wp_api_v2_menus_get_menu_data', |
269 | 269 | 'permission_callback' => '__return_true' |
270 | - ) ); |
|
270 | + )); |
|
271 | 271 | |
272 | - register_rest_route( 'menus/v1', '/locations/(?P<id>[a-zA-Z0-9_-]+)', array( |
|
272 | + register_rest_route('menus/v1', '/locations/(?P<id>[a-zA-Z0-9_-]+)', array( |
|
273 | 273 | 'methods' => 'GET', |
274 | 274 | 'callback' => 'wp_api_v2_locations_get_menu_data', |
275 | 275 | 'permission_callback' => '__return_true' |
276 | - ) ); |
|
276 | + )); |
|
277 | 277 | |
278 | - register_rest_route( 'menus/v1', '/locations', array( |
|
278 | + register_rest_route('menus/v1', '/locations', array( |
|
279 | 279 | 'methods' => 'GET', |
280 | 280 | 'callback' => 'wp_api_v2_menu_get_all_locations', |
281 | 281 | 'permission_callback' => '__return_true' |
282 | - ) ); |
|
282 | + )); |
|
283 | 283 | } ); |