-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathwp-rest-api-v2-menus.php
288 lines (246 loc) · 8.08 KB
/
wp-rest-api-v2-menus.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
<?php
/*
Plugin Name: WP-REST-API V2 Menus
Version: 0.12.1
Description: Adding menus endpoints on WP REST API v2
Author: Claudio La Barbera
Author URI: https://thebatclaud.io
*/
/**
* Get all registered menus
* @return array List of menus with slug and description
*/
function wp_api_v2_menus_get_all_menus() {
$menus = get_terms( 'nav_menu', array( 'hide_empty' => true ) );
foreach ( $menus as $key => $menu ) {
// check if there is acf installed
if ( class_exists( 'acf' ) ) {
$fields = get_fields( $menu );
if ( ! empty( $fields ) ) {
$menus[ $key ]->acf = new stdClass();
foreach ( $fields as $field_key => $item ) {
// add all acf custom fields
$menus[ $key ]->acf->$field_key = $item;
}
}
}
}
return apply_filters('wp_api_v2_menus__menus', $menus);
}
/**
* Get all locations
* @return array List of locations
**/
function wp_api_v2_menu_get_all_locations() {
$nav_menu_locations = get_nav_menu_locations();
$locations = new stdClass;
foreach ( $nav_menu_locations as $location_slug => $menu_id ) {
if ( get_term( $location_slug ) !== null ) {
$locations->{$location_slug} = get_term( $location_slug );
} else {
$locations->{$location_slug} = new stdClass;
}
$locations->{$location_slug}->slug = $location_slug;
$locations->{$location_slug}->menu = get_term( $menu_id );
}
return apply_filters('wp_api_v2_menus__locations', $locations);
}
/**
* Get menu's data from his id
*
* @param array $data WP REST API data variable
*
* @return object Menu's data with his items
*/
function wp_api_v2_locations_get_menu_data( $data ) {
// Create default empty object
$menu = new stdClass;
// this could be replaced with `if (has_nav_menu($data['id']))`
if ( ( $locations = get_nav_menu_locations() ) && isset( $locations[ $data['id'] ] ) ) {
// Replace default empty object with the location object
$menu = get_term( $locations[ $data['id'] ] );
if ( is_wp_error( $menu ) || null === $menu ) {
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 ) );
}
$menu->items = wp_api_v2_menus_get_menu_items( $locations[ $data['id'] ] );
} else {
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 ) );
}
// check if there is acf installed
if ( class_exists( 'acf' ) ) {
$fields = get_fields( $menu );
if ( ! empty( $fields ) ) {
$menu->acf = new stdClass();
foreach ( $fields as $field_key => $item ) {
// add all acf custom fields
$menu->acf->$field_key = $item;
}
}
}
return apply_filters('wp_api_v2_menus__menu', $menu);
}
/**
* Check if a menu item is child of one of the menu's element passed as reference
*
* @param $parents Menu's items
* @param $child Menu's item to check
*
* @return bool True if the parent is found, false otherwise
*/
function wp_api_v2_menus_dna_test( &$parents, $child ) {
foreach ( $parents as $key => $item ) {
if ( $child->menu_item_parent == $item->ID ) {
if ( ! $item->child_items ) {
$item->child_items = [];
}
array_push( $item->child_items, $child );
return true;
}
if($item->child_items) {
if(wp_api_v2_menus_dna_test($item->child_items, $child)) {
return true;
}
}
}
return false;
}
/**
* Search object in an array by ID
*/
function wp_api_v2_find_object_by_id( $array, $id ) {
foreach ( $array as $element ) {
if ( $id == $element->ID ) {
return $element;
}
}
return false;
}
/**
* Retrieve items for a specific menu
*
* @param $id Menu id
*
* @return array List of menu items
*/
function wp_api_v2_menus_get_menu_items( $id ) {
$menu_items = wp_get_nav_menu_items( $id );
// fallback: if menu_items is null then return empty array
if($menu_items === false) {
return [];
}
$all_menu_items = $menu_items;
// check if there is acf installed
if ( class_exists( 'acf' ) ) {
foreach ( $menu_items as $menu_key => $menu_item ) {
$fields = get_fields( $menu_item->ID );
if ( ! empty( $fields ) ) {
$menu_items[$menu_key]->acf = new stdClass();
foreach ( $fields as $field_key => $item ) {
// add all acf custom fields
$menu_items[ $menu_key ]->acf->$field_key = $item;
}
}
}
}
// wordpress does not group child menu items with parent menu items
$child_items = [];
// pull all child menu items into separate object
foreach ( $menu_items as $key => $item ) {
if($item->type == 'post_type') {
// add slug to menu items
$slug = get_post_field( 'post_name', $item->object_id );
$item->slug = $slug;
} else if($item->type == 'taxonomy') {
$cat = get_term($item->object_id);
$item->slug = $cat->slug;
} else if($item->type == 'post_type_archive') {
$post_type_data = get_post_type_object($item->object);
if ($post_type_data->has_archive) {
$item->slug = $post_type_data->rewrite['slug'];
}
}
if (isset($item->thumbnail_id) && $item->thumbnail_id) {
$item->thumbnail_src = wp_get_attachment_image_url(intval($item->thumbnail_id), 'post-thumbnail');
}
if (isset($item->thumbnail_hover_id) && $item->thumbnail_hover_id) {
$item->thumbnail_hover_src = wp_get_attachment_image_url(intval($item->thumbnail_hover_id), 'post-thumbnail');
}
if ( $item->menu_item_parent ) {
array_push( $child_items, $item );
unset( $menu_items[ $key ] );
}
}
// push child items into their parent item in the original object
do {
foreach($child_items as $key => $child_item) {
$parent = wp_api_v2_find_object_by_id( $all_menu_items, $child_item->menu_item_parent );
if ( empty( $parent ) ) {
unset($child_items[$key]);
}
else if (wp_api_v2_menus_dna_test($menu_items, $child_item)) {
unset($child_items[$key]);
}
}
} while(count($child_items));
return apply_filters('wp_api_v2_menus__menu_items', array_values($menu_items));
}
/**
* Get menu's data from his id.
* It ensures compatibility for previous versions when this endpoint
* was allowing locations id in place of menus id)
*
* @param array $data WP REST API data variable
*
* @return object Menu's data with his items
*/
function wp_api_v2_menus_get_menu_data( $data ) {
// This ensure retro compatibility with versions `<= 0.5` when this endpoint
// was allowing locations id in place of menus id
if ( has_nav_menu( $data['id'] ) ) {
$menu = wp_api_v2_locations_get_menu_data( $data );
} else if ( is_nav_menu( $data['id'] ) ) {
if ( is_int( $data['id'] ) ) {
$id = $data['id'];
} else {
$id = wp_get_nav_menu_object( $data['id'] );
}
$menu = get_term( $id );
$menu->items = wp_api_v2_menus_get_menu_items( $id );
} else {
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 ) );
}
// check if there is acf installed
if ( class_exists( 'acf' ) ) {
$fields = get_fields( $menu );
if ( ! empty( $fields ) ) {
$menu->acf = new stdClass();
foreach ( $fields as $field_key => $item ) {
// add all acf custom fields
$menu->acf->$field_key = $item;
}
}
}
return apply_filters('wp_api_v2_menus__menu', $menu);
}
add_action( 'rest_api_init', function () {
register_rest_route( 'menus/v1', '/menus', array(
'methods' => 'GET',
'callback' => 'wp_api_v2_menus_get_all_menus',
'permission_callback' => '__return_true'
) );
register_rest_route( 'menus/v1', '/menus/(?P<id>[a-zA-Z0-9_-]+)', array(
'methods' => 'GET',
'callback' => 'wp_api_v2_menus_get_menu_data',
'permission_callback' => '__return_true'
) );
register_rest_route( 'menus/v1', '/locations/(?P<id>[a-zA-Z0-9_-]+)', array(
'methods' => 'GET',
'callback' => 'wp_api_v2_locations_get_menu_data',
'permission_callback' => '__return_true'
) );
register_rest_route( 'menus/v1', '/locations', array(
'methods' => 'GET',
'callback' => 'wp_api_v2_menu_get_all_locations',
'permission_callback' => '__return_true'
) );
} );