diff --git a/includes/wp-api-menus-v2.php b/includes/wp-api-menus-v2.php index ae7c82e..824fc2f 100644 --- a/includes/wp-api-menus-v2.php +++ b/includes/wp-api-menus-v2.php @@ -129,7 +129,6 @@ public static function get_menus() { * @return array Menu data */ public function get_menu( $request ) { - $id = (int) $request['id']; $rest_url = get_rest_url() . self::get_api_namespace() . '/menus/'; $wp_menu_object = $id ? wp_get_nav_menu_object( $id ) : array(); @@ -158,7 +157,6 @@ public function get_menu( $request ) { $rest_menu['meta']['links']['self'] = $rest_url . $id; endif; - return apply_filters( 'rest_menus_format_menu', $rest_menu ); } @@ -303,7 +301,7 @@ public function get_menu_location( $request ) { $formatted['children'] = array_reverse( $cache[ $item->ID ] ); } - $formatted = apply_filters( 'rest_menus_format_menu_item', $formatted ); + $formatted = apply_filters( 'rest_menus_format_menu_item', $formatted ); if ( $item->menu_item_parent != 0 ) { @@ -367,8 +365,37 @@ public function get_nav_menu_item_children( $parent_id, $nav_menu_items, $depth * @return array a formatted menu item for REST */ public function format_menu_item( $menu_item, $children = false, $menu = array() ) { - $item = (array) $menu_item; + $object_slug=null; + $rest_path = ''; + switch($item['type']){ + case 'post_type_archive': //archive. + $post_type_data = get_post_type_object( $item['object'] ); + $object = empty($post_type_data->rest_base) ? $item['object'] : $post_type_data->rest_base; + $object_slug = $post_type_data->rewrite['slug']; + + $rest_path = rest_url( '/wp/v2/'.$object.'/'); + break; + case 'taxonomy'://taxonomy term. + $term = get_term_by( 'id', $item['object_id'], $item['object']); + $object_slug = $term->slug; + $taxonomy = get_taxonomy( $item['object']); + $object = ! empty( $taxonomy->rest_base ) ? $taxonomy->rest_base : $taxonomy->name; + + $rest_path = rest_url( '/wp/v2/'.$object.'/'.$item['object_id']); + break; + case 'post_type': + $post_type_data = get_post_type_object( $item['object'] ); + $object = empty($post_type_data->rest_base) ? $item['object'] : $post_type_data->rest_base; + $object_slug = get_post( $item['object_id'] )->post_name; + + $rest_path = rest_url( '/wp/v2/'.$object.'/'.$item['object_id']); + break; + default: //try with the object and id. + $rest_path = rest_url( '/wp/v2/'.$item['object'].'/'.$item['object_id']); + break; + } + //get_rest_url( int $blog_id = null, string $path = '/', string $scheme = 'rest' ) $menu_item = array( 'id' => abs( $item['ID'] ), @@ -383,9 +410,10 @@ public function format_menu_item( $menu_item, $children = false, $menu = array() 'description' => $item['description'], 'object_id' => abs( $item['object_id'] ), 'object' => $item['object'], - 'object_slug' => get_post( $item['object_id'] )->post_name, + 'object_slug' => $object_slug, 'type' => $item['type'], 'type_label' => $item['type_label'], + '_links' => array('self'=>$rest_path) ); if ( $children === true && ! empty( $menu ) ) {