Skip to content

Commit

Permalink
Merge branch 'get-menu-by-id-name-and-slug' of https://github.com/Fah…
Browse files Browse the repository at this point in the history
…rradflucht/wp-api-menus into feature/get-menu-by-id-name-and-slug

See unfulvio#30
  • Loading branch information
westonruter committed Jul 15, 2016
2 parents 2b721fc + 44259df commit c8241bd
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 14 deletions.
11 changes: 6 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,20 +11,21 @@
#### New routes available:

- `/menus` list of every registered menu.
- `/menus/<id>` data for a specific menu.
- (V2 only:) `item` argument whether to fetch the menu items. Default: `false`
- `/menus/<menu>` data for a specific menu. Takes either an id, a name or a slug. Note that v1 only supports ids.
- `/menu-locations` list of all registered theme locations.
- `/menu-locations/<location>` data for menu in specified menu in theme location.
- `/menu-locations/<location>` data for menu in specified menu in theme location.

Currently, the `menu-locations/<location>` route for individual menus will return a tree with full menu hierarchy, with correct menu item order and listing children for each menu item. The `menus/<id>` route will output menu details and a flat array of menu items. Item order or if each item has a parent will be indicated in each item attributes, but this route won't output items as a tree.

You can alter the data arrangement of each individual menu items and children using the filter hook `json_menus_format_menu_item`.

#### WP API V2

In V1 of the REST API the routes are located by default at `wp-json/menus/` etc.

In V2 the routes by default are at `wp-json/wp-api-menus/v2/` (e.g. `wp-json/wp-api-menus/v2/menus/`, etc.) since V2 encourages prefixing and version namespacing.
In V2 the routes by default are at `wp-json/wp-api-menus/v2/` (e.g. `wp-json/wp-api-menus/v2/menus/`, etc.) since V2 encourages prefixing and version namespacing.

#### Contributing

* Submit a pull request or open a ticket here on GitHub.
* Submit a pull request or open a ticket here on GitHub.
31 changes: 25 additions & 6 deletions includes/wp-api-menus-v2.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,10 +57,16 @@ public function register_routes() {
array(
'methods' => WP_REST_Server::READABLE,
'callback' => array( $this, 'get_menus' ),
'args' => array(
'items' => array(
'description' => __( 'Whether to fetch the menu items.' ),
'default' => false,
),
),
)
) );

register_rest_route( self::get_plugin_namespace(), '/menus/(?P<id>\d+)', array(
register_rest_route( self::get_plugin_namespace(), '/menus/(?P<menu>[a-zA-Z0-9_-]+)', array(
array(
'methods' => WP_REST_Server::READABLE,
'callback' => array( $this, 'get_menu' ),
Expand Down Expand Up @@ -93,10 +99,12 @@ public function register_routes() {
* Get menus.
*
* @since 1.2.0
* @param $request
* @return array All registered menus
*/
public static function get_menus() {
public function get_menus( $request ) {

$items = (bool) $request['items'];
$rest_url = trailingslashit( get_rest_url() . self::get_plugin_namespace() . '/menus/' );
$wp_menus = wp_get_nav_menus();

Expand All @@ -113,6 +121,17 @@ public static function get_menus() {
$rest_menus[ $i ]['description'] = $menu['description'];
$rest_menus[ $i ]['count'] = $menu['count'];

if ($items) {
$wp_menu_items = wp_get_nav_menu_items( $menu['term_id'] );
$rest_menu_items = array();
foreach ( $wp_menu_items as $item_object ) {
$rest_menu_items[] = $this->format_menu_item( $item_object );
}
$rest_menu_items = $this->nested_menu_items($rest_menu_items, 0);

$rest_menus[ $i ]['items'] = $rest_menu_items;
}

$rest_menus[ $i ]['meta']['links']['collection'] = $rest_url;
$rest_menus[ $i ]['meta']['links']['self'] = $rest_url . $menu['term_id'];

Expand All @@ -132,10 +151,10 @@ public static function get_menus() {
*/
public function get_menu( $request ) {

$id = (int) $request['id'];
$menu = (string) $request['menu'];
$rest_url = get_rest_url() . self::get_api_namespace() . '/menus/';
$wp_menu_object = $id ? wp_get_nav_menu_object( $id ) : array();
$wp_menu_items = $id ? wp_get_nav_menu_items( $id ) : array();
$wp_menu_object = $menu ? wp_get_nav_menu_object( $menu ) : array();
$wp_menu_items = $menu ? wp_get_nav_menu_items( $menu ) : array();

$rest_menu = array();

Expand All @@ -157,7 +176,7 @@ public function get_menu( $request ) {

$rest_menu['items'] = $rest_menu_items;
$rest_menu['meta']['links']['collection'] = $rest_url;
$rest_menu['meta']['links']['self'] = $rest_url . $id;
$rest_menu['meta']['links']['self'] = $rest_url . abs( $menu['term_id'] );

endif;

Expand Down
7 changes: 4 additions & 3 deletions readme.txt
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ This plugin extends the [WordPress JSON REST API](https://wordpress.org/plugins/
The new routes available will be:

* `/menus` list of every registered menu.
* `/menus/<id>` data for a specific menu.
* (V2 only:) `item` argument whether to fetch the menu items. Default: `false`
* `/menus/<menu>` data for a specific menu. Takes either an id, a name or a slug. Note that v1 only supports ids.
* `/menu-locations` list of all registered theme locations.
* `/menu-locations/<location>` data for menu in specified menu in theme location.

Expand Down Expand Up @@ -90,7 +91,7 @@ Nothing to show really, this plugin has no settings or frontend, it just extends
= 1.1.0 =
* Enhancement: Routes for menus in theme locations now include complete tree with item order and nested children
* Tweak: `description` attribute for individual items is now included in results
* Fix: Fixed typo confusing `parent` with `collection` in meta
* Fix: Fixed typo confusing `parent` with `collection` in meta

= 1.0.0 =
* First public release
Expand All @@ -99,4 +100,4 @@ Nothing to show really, this plugin has no settings or frontend, it just extends

= 1.2.1 =

API V2 only: mind lowercase `id` instead of uppercase `ID` in API responses, to match the standard for `id` used across WP REST API.
API V2 only: mind lowercase `id` instead of uppercase `ID` in API responses, to match the standard for `id` used across WP REST API.

0 comments on commit c8241bd

Please sign in to comment.