-
Notifications
You must be signed in to change notification settings - Fork 0
/
ucf-rest-menus.php
338 lines (282 loc) · 9.15 KB
/
ucf-rest-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
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
<?php defined( 'ABSPATH' ) or die( 'No script kiddies please!' ); ?>
<?php
/*
Plugin Name: UCF Rest Menus
Plugin URI: http://github.com/UCF/ucf-rest-menus/
Description: Provides rest api for accessing WordPress menus.
Version: 1.0.5
GitHub Plugin URI: UCF/ucf-rest-menus
Author: UCF Web Communications
Author URI: http://github.com/UCF/
License: MIT
Note: This plugin is heavily based on the wp-api-menus plugin which can
be found here: https://wordpress.org/plugins/wp-api-menus/. In the initial
commit of the plugin, the code is almost exactly the same as the v2 code
of the original. Because functionality within UCF themes will be dependent
upon this plugin, the code was slightly modified and put within its own
repository to prevent incompatibilties from arising as the original plugin
author makes changes.
*/
?>
<?php
if ( ! class_exists( 'UCF_REST_Menus' ) ) {
/**
* UCF Rest Menus class
* WP API Menus support for WP API v2
* @author Jim Barnes
* @since 1.0.0
**/
class UCF_REST_Menus {
/*
* Get WP API namespace
* @since 1.0.0
* @return string
*/
public static function get_api_namespace() {
return 'wp/v2';
}
/*
* Get plugin namespace.
* @author Jim Barnes
* @since 1.0.0
* @return string
*/
public static function get_plugin_namespace() {
return 'ucf-rest-menus/v1';
}
/*
* Register API routes
* @author Jim Barnes
* @since 1.0.0
* @return array
*/
public static function register_routes() {
register_rest_route( self::get_plugin_namespace(), '/menus', array(
array(
'methods' => WP_REST_Server::READABLE,
'callback' => array( 'UCF_Rest_Menus', 'get_menus' )
)
) );
register_rest_route( self::get_plugin_namespace(), '/menus/(?P<id>\d+)', array(
array(
'methods' => WP_REST_Server::READABLE,
'callback' => array( 'UCF_Rest_Menus', 'get_menu' ),
'args' => array(
'context' => array(
'default' => 'view'
)
)
)
) );
register_rest_route( self::get_plugin_namespace(), '/menu-locations', array(
array(
'methods' => WP_REST_Server::READABLE,
'callback' => array( 'UCF_Rest_Menus', 'get_menu_locations' )
)
) );
register_rest_route( self::get_plugin_namespace(), '/menu-locations/(?P<location>[a-zA-Z0-9_-]+)', array(
array(
'methods' => WP_REST_Server::READABLE,
'callback' => array( 'UCF_Rest_Menus', 'get_menu_location' )
)
) );
}
/*
* Get Menus
* @author Jim Barnes
* @since 1.0.0
* @returns array
*/
public static function get_menus() {
$rest_url = trailingslashit( get_rest_url() . self::get_plugin_namespace() . '/menus/' );
$wp_menus = wp_get_nav_menus();
$retval = array();
foreach( $wp_menus as $i=>$wp_menu ) {
$menu = (array) $wp_menu;
$retval[$i] = array();
$retval[$i]['ID'] = $menu['term_id'];
$retval[$i]['name'] = $menu['name'];
$retval[$i]['slug'] = $menu['slug'];
$retval[$i]['description'] = $menu['description'];
$retval[$i]['count'] = $menu['count'];
$retval[$i]['meta']['links']['collection'] = $rest_url;
$retval[$i]['meta']['links']['self'] = $rest_url . $menu['term_id'];
}
return apply_filters( 'rest_menus_format_menus', $retval );
}
/*
* Get Menu Details
* @author Jim Barnes
* @since 1.0.0
* @return array
*/
public static function get_menu( $request ) {
$id = (int) $request['id'];
$rest_url = get_rest_url() . self::get_plugin_namespace() . '/menus/';
$wp_menu_object = isset( $id ) ? wp_get_nav_menu_object( $id ) : null;
$wp_menu_items = isset( $id ) ? wp_get_nav_menu_items( $id ) : array();
$retval = array();
if ( $wp_menu_object ) {
$menu = (array) $wp_menu_object;
$retval['ID'] = abs( $menu['term_id'] );
$retval['name'] = $menu['name'];
$retval['slug'] = $menu['slug'];
$retval['description'] = $menu['description'];
$retval['count'] = $menu['count'];
$menu_items = array();
foreach( $wp_menu_items as $item ) {
$menu_items[] = self::format_menu_item( $item );
}
$menu_items = self::nested_menu_items( $menu_items, 0 );
$retval['items'] = $menu_items;
$retval['meta']['links']['collection'] = $rest_url;
$retval['meta']['links']['self'] = $rest_url . $id;
}
return apply_filters( 'rest_menus_format_menu', $retval );
}
/*
* Get Menu Locations
* @since 1.0.0
* @author Jim Barnes
* @return array
*/
public static function get_menu_locations( $request ) {
$rest_url = get_rest_url() . self::get_plugin_namespace() . '/menu-locations/';
$locations = get_nav_menu_locations();
$registered_menus = get_registered_nav_menus();
$retval = array();
if ( $locations && $registered_menus ) {
foreach( $registered_menus as $slug=>$label ) {
$retval['slug']['ID'] = $locations[$slug];
$retval['slug']['label'] = $label;
$retval['meta']['links']['collection'] = $rest_url;
$retval['meta']['links']['self'] = $rest_url . $slug;
}
}
return $retval;
}
/*
* Get menu for location
* @since 1.0.0
* @author Jim Barnes
* return array
*/
public static function get_menu_location( $request ) {
$params = $request->get_params();
$location = $params['location'];
$locations = get_nav_menu_locations();
if ( ! isset( $locations[$location] ) ) {
return array();
}
$wp_menu_object = wp_get_nav_menu_object( $locations[$location] );
$wp_menu_items = wp_get_nav_menu_items( $wp_menu_object->term_id );
if ( $wp_menu_object ) {
$menu = (array) $wp_menu_object;
$retval['ID'] = abs( $menu['term_id'] );
$retval['name'] = $menu['name'];
$retval['slug'] = $menu['slug'];
$retval['description'] = $menu['description'];
$retval['count'] = $menu['count'];
$menu_items = array();
foreach( $wp_menu_items as $item ) {
$menu_items[] = self::format_menu_item( $item );
}
$menu_items = self::nested_menu_items( $menu_items, 0 );
$retval['items'] = $menu_items;
$retval['meta']['links']['collection'] = $rest_url;
$retval['meta']['links']['self'] = $rest_url . $id;
}
return apply_filters( 'rest_menus_format_menu', $retval );
}
/*
* Handle nested menu items.
* @since 1.0.0
* @author Jim Barnes
* @return string
*/
private static function nested_menu_items( &$menu_items, $parent=null ) {
$parents = array();
$children = array();
array_map( function( $i ) use ( $parent, &$children, &$parents ) {
if ( $i['id'] !== $parent && $i['parent'] == $parent ) {
$parents[] = $i;
} else {
$children[] = $i;
}
}, $menu_items );
foreach( $parents as &$parent ) {
if ( self::has_children( $children, $parent['id'] ) ) {
$parent['children'] = self::nested_menu_items( $children, $parent['id'] );
}
}
return $parents;
}
/*
* Check if a collection of menu items contains an item that is the parent if of id.
* @since 1.0.0
* @author Jim Barnes
* @return array
*/
private static function has_children( $items, $id ) {
return array_filter( $items, function( $i ) use ( $id ) {
return $i['parent'] == $id;
} );
}
/*
* Format a menu item for REST API
* @since 1.0.0
* @author Jim Barnes
* @return array
*/
public static function format_menu_item( $menu_item, $children=false, $menu=array() ) {
$item = (array) $menu_item;
$retval = array(
'id' => abs( $item['ID'] ),
'order' => (int) $item['menu_order'],
'parent' => abs( $item['menu_item_parent'] ),
'title' => $item['title'],
'url' => $item['url'],
'attr' => $item['attr_title'],
'target' => $item['target'],
'classes' => $item['classes'],
'xfn' => $item['xfn'],
'description' => $item['description'],
'object_id' => abs( $item['object_id'] ),
'object' => $item['object'],
'type' => $item['type'],
'type_label' => $item['type_label']
);
if ( $children === true && ! empty( $menu ) ) {
$retval['children'] = self::get_nav_menu_item_children( $item['ID'], $menu );
}
return apply_filters( 'rest_menus_format_menu_item', $retval );
}
/*
* Returns all child nav_menu_items under a specific parent.
* @since 1.0.0
* @author Jim Barnes
* @return array
*/
private static function get_nav_menu_item_children( $parent_id, $nav_menu_items, $depth=true ) {
$retval = array();
foreach( (array) $nav_menu_items as $nav_menu_item ) {
if ( $nav_menu_item->menu_item_parent == $parent_id ) {
$retval[] = self::format_menu_item( $nav_menu_item, true, $nav_menu_items );
if ( $depth ) {
if ( $children = self::get_nav_menu_item_children( $nav_menu_item->ID, $nav_menu_items ) ) {
$retval = array_merge( $retval, $children );
}
}
}
}
return $retval;
}
}
}
if ( ! function_exists( 'ucf_rest_menus_init' ) ) {
function ucf_rest_menus_init() {
add_filter( 'rest_api_init', array( 'UCF_REST_Menus', 'register_routes' ) );
}
add_action( 'init', 'ucf_rest_menus_init' );
}
?>