From 1868e6bf305e0eacc19719f3a452fff9501599e7 Mon Sep 17 00:00:00 2001 From: Aurovrata V Date: Wed, 23 May 2018 18:08:37 +0530 Subject: [PATCH 1/2] add archive and taxonomy terms --- includes/wp-api-menus-v2.php | 21 ++++++++++++++++----- 1 file changed, 16 insertions(+), 5 deletions(-) diff --git a/includes/wp-api-menus-v2.php b/includes/wp-api-menus-v2.php index ae7c82e..a876c68 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 ) { @@ -369,7 +367,19 @@ public function get_nav_menu_item_children( $parent_id, $nav_menu_items, $depth public function format_menu_item( $menu_item, $children = false, $menu = array() ) { $item = (array) $menu_item; - + $object_slug=null; + switch($item['type']){ + case 'post_type_archive': //archive. + $post_type_data = get_post_type_object( $item['object'] ); + $object_slug = $post_type_data->rewrite['slug']; + break; + case 'taxonomy'://taxonomy term. + $term = get_term_by( 'id', $item['object_id'], $item['object']); + $object_slug = $term->slug; + case 'post_type': + $object_slug = get_post( $item['object_id'] )->post_name; + break; + } $menu_item = array( 'id' => abs( $item['ID'] ), 'order' => (int) $item['menu_order'], @@ -383,7 +393,7 @@ 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'], ); @@ -400,3 +410,4 @@ public function format_menu_item( $menu_item, $children = false, $menu = array() endif; + From f93ca5b613e4dee26ddfd9a761d7ded722192f5a Mon Sep 17 00:00:00 2001 From: Aurovrata V Date: Thu, 31 May 2018 22:04:03 +0530 Subject: [PATCH 2/2] fixed rest base name --- includes/wp-api-menus-v2.php | 21 +++++++++++++++++++-- 1 file changed, 19 insertions(+), 2 deletions(-) diff --git a/includes/wp-api-menus-v2.php b/includes/wp-api-menus-v2.php index a876c68..824fc2f 100644 --- a/includes/wp-api-menus-v2.php +++ b/includes/wp-api-menus-v2.php @@ -365,21 +365,38 @@ 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'] ), 'order' => (int) $item['menu_order'], @@ -396,6 +413,7 @@ public function format_menu_item( $menu_item, $children = false, $menu = array() 'object_slug' => $object_slug, 'type' => $item['type'], 'type_label' => $item['type_label'], + '_links' => array('self'=>$rest_path) ); if ( $children === true && ! empty( $menu ) ) { @@ -410,4 +428,3 @@ public function format_menu_item( $menu_item, $children = false, $menu = array() endif; -